Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <math.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <utils/Log.h> |
| 30 | #include <binder/Parcel.h> |
| 31 | #include <binder/IPCThreadState.h> |
| 32 | #include <utils/String16.h> |
| 33 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 34 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 38 | #include <cutils/compiler.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 40 | #undef ADD_BATTERY_DATA |
| 41 | |
| 42 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 43 | #include <media/IMediaPlayerService.h> |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 44 | #include <media/IMediaDeathNotifier.h> |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 45 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | |
| 47 | #include <private/media/AudioTrackShared.h> |
| 48 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 49 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 50 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 51 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | #include "AudioMixer.h" |
| 54 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 55 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 58 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 59 | #include <audio_effects/effect_ns.h> |
| 60 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 62 | #include <audio_utils/primitives.h> |
| 63 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 64 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 65 | |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 66 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 67 | #ifdef DEBUG_CPU_USAGE |
| 68 | #include <cpustats/CentralTendencyStatistics.h> |
| 69 | #include <cpustats/ThreadCpuUsage.h> |
| 70 | #endif |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 71 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 72 | #include <common_time/cc_helper.h> |
| 73 | #include <common_time/local_clock.h> |
| 74 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 75 | #include "FastMixer.h" |
| 76 | |
| 77 | // NBAIO implementations |
| 78 | #include "AudioStreamOutSink.h" |
| 79 | #include "MonoPipe.h" |
| 80 | #include "MonoPipeReader.h" |
| 81 | #include "SourceAudioBufferProvider.h" |
| 82 | |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 83 | #ifdef HAVE_REQUEST_PRIORITY |
| 84 | #include "SchedulingPolicyService.h" |
| 85 | #endif |
| 86 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 87 | #ifdef SOAKER |
| 88 | #include "Soaker.h" |
| 89 | #endif |
| 90 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 91 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 93 | // Note: the following macro is used for extremely verbose logging message. In |
| 94 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 95 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 96 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 97 | // turned on. Do not uncomment the #def below unless you really know what you |
| 98 | // are doing and want to see all of the extremely verbose messages. |
| 99 | //#define VERY_VERY_VERBOSE_LOGGING |
| 100 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 101 | #define ALOGVV ALOGV |
| 102 | #else |
| 103 | #define ALOGVV(a...) do { } while(0) |
| 104 | #endif |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 105 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 106 | namespace android { |
| 107 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 108 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 109 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 110 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 111 | static const float MAX_GAIN = 4096.0f; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 112 | static const uint32_t MAX_GAIN_INT = 0x1000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 113 | |
| 114 | // retry counts for buffer fill timeout |
| 115 | // 50 * ~20msecs = 1 second |
| 116 | static const int8_t kMaxTrackRetries = 50; |
| 117 | static const int8_t kMaxTrackStartupRetries = 50; |
| 118 | // allow less retry attempts on direct output thread. |
| 119 | // direct outputs can be a scarce resource in audio hardware and should |
| 120 | // be released as quickly as possible. |
| 121 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 122 | |
| 123 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 124 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 125 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 126 | // don't warn about blocked writes or record buffer overflows more often than this |
| 127 | static const nsecs_t kWarningThrottleNs = seconds(5); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 128 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 129 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 130 | static const int kRecordThreadSleepUs = 5000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 132 | // maximum time to wait for setParameters to complete |
| 133 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 134 | |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 135 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 136 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 137 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 138 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 139 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 140 | // minimum normal mix buffer size, expressed in milliseconds rather than frames |
| 141 | static const uint32_t kMinNormalMixBufferSizeMs = 20; |
| 142 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 143 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 144 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 145 | // Whether to use fast mixer |
| 146 | static const enum { |
| 147 | FastMixer_Never, // never initialize or use: for debugging only |
| 148 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 149 | // normal mixer multiplier is 1 |
| 150 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 151 | // multipler is calculated based on minimum normal mixer buffer size |
| 152 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 153 | // multipler is calculated based on minimum normal mixer buffer size |
| 154 | // FIXME for FastMixer_Dynamic: |
| 155 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 156 | // For example, one HAL implementation returns an error from a large write, |
| 157 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 158 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 159 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 160 | } kUseFastMixer = FastMixer_Static; |
| 161 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 162 | // ---------------------------------------------------------------------------- |
| 163 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 164 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 165 | // To collect the amplifier usage |
| 166 | static void addBatteryData(uint32_t params) { |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 167 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 168 | if (service == NULL) { |
| 169 | // it already logged |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | service->addBatteryData(params); |
| 174 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 175 | #endif |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 176 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 177 | static int load_audio_interface(const char *if_name, audio_hw_device_t **dev) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 178 | { |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 179 | const hw_module_t *mod; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 180 | int rc; |
| 181 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 182 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); |
| 183 | ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, |
| 184 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 185 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 186 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 187 | } |
| 188 | rc = audio_hw_device_open(mod, dev); |
| 189 | ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, |
| 190 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 191 | if (rc) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 192 | goto out; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 193 | } |
| 194 | if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { |
| 195 | ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); |
| 196 | rc = BAD_VALUE; |
| 197 | goto out; |
| 198 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | |
| 201 | out: |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 202 | *dev = NULL; |
| 203 | return rc; |
| 204 | } |
| 205 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 206 | // ---------------------------------------------------------------------------- |
| 207 | |
| 208 | AudioFlinger::AudioFlinger() |
| 209 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 210 | mPrimaryHardwareDev(NULL), |
| 211 | mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef() |
| 212 | mMasterVolume(1.0f), |
| 213 | mMasterVolumeSupportLvl(MVS_NONE), |
| 214 | mMasterMute(false), |
| 215 | mNextUniqueId(1), |
| 216 | mMode(AUDIO_MODE_INVALID), |
| 217 | mBtNrecIsOff(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 218 | { |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void AudioFlinger::onFirstRef() |
| 222 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 223 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 224 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 225 | Mutex::Autolock _l(mLock); |
| 226 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 227 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 228 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 229 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 230 | uint32_t int_val; |
| 231 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 232 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 233 | ALOGI("Using %u mSec as standby time.", int_val); |
| 234 | } else { |
| 235 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 236 | ALOGI("Using default %u mSec as standby time.", |
| 237 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 238 | } |
| 239 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 240 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 241 | mMode = AUDIO_MODE_NORMAL; |
| 242 | mMasterVolumeSW = 1.0; |
| 243 | mMasterVolume = 1.0; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 244 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | AudioFlinger::~AudioFlinger() |
| 248 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 249 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 250 | while (!mRecordThreads.isEmpty()) { |
| 251 | // closeInput() will remove first entry from mRecordThreads |
| 252 | closeInput(mRecordThreads.keyAt(0)); |
| 253 | } |
| 254 | while (!mPlaybackThreads.isEmpty()) { |
| 255 | // closeOutput() will remove first entry from mPlaybackThreads |
| 256 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 257 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 258 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 259 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 260 | // no mHardwareLock needed, as there are no other references to this |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 261 | audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); |
| 262 | delete mAudioHwDevs.valueAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 266 | static const char * const audio_interfaces[] = { |
| 267 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, |
| 268 | AUDIO_HARDWARE_MODULE_ID_A2DP, |
| 269 | AUDIO_HARDWARE_MODULE_ID_USB, |
| 270 | }; |
| 271 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 272 | |
| 273 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(audio_module_handle_t module, uint32_t devices) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 274 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 275 | // if module is 0, the request comes from an old policy manager and we should load |
| 276 | // well known modules |
| 277 | if (module == 0) { |
| 278 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); |
| 279 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 280 | loadHwModule_l(audio_interfaces[i]); |
| 281 | } |
| 282 | } else { |
| 283 | // check a match for the requested module handle |
| 284 | AudioHwDevice *audioHwdevice = mAudioHwDevs.valueFor(module); |
| 285 | if (audioHwdevice != NULL) { |
| 286 | return audioHwdevice->hwDevice(); |
| 287 | } |
| 288 | } |
| 289 | // then try to find a module supporting the requested device. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 290 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 291 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 292 | if ((dev->get_supported_devices(dev) & devices) == devices) |
| 293 | return dev; |
| 294 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 295 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 296 | return NULL; |
| 297 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 298 | |
| 299 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 300 | { |
| 301 | const size_t SIZE = 256; |
| 302 | char buffer[SIZE]; |
| 303 | String8 result; |
| 304 | |
| 305 | result.append("Clients:\n"); |
| 306 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 307 | sp<Client> client = mClients.valueAt(i).promote(); |
| 308 | if (client != 0) { |
| 309 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 310 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 313 | |
| 314 | result.append("Global session refs:\n"); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 315 | result.append(" session pid count\n"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 316 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 317 | AudioSessionRef *r = mAudioSessionRefs[i]; |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 318 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 319 | result.append(buffer); |
| 320 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 321 | write(fd, result.string(), result.size()); |
| 322 | return NO_ERROR; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 327 | { |
| 328 | const size_t SIZE = 256; |
| 329 | char buffer[SIZE]; |
| 330 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 331 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 332 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 333 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 334 | "Standby Time mSec: %u\n", |
| 335 | hardwareStatus, |
| 336 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 337 | result.append(buffer); |
| 338 | write(fd, result.string(), result.size()); |
| 339 | return NO_ERROR; |
| 340 | } |
| 341 | |
| 342 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 343 | { |
| 344 | const size_t SIZE = 256; |
| 345 | char buffer[SIZE]; |
| 346 | String8 result; |
| 347 | snprintf(buffer, SIZE, "Permission Denial: " |
| 348 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 349 | IPCThreadState::self()->getCallingPid(), |
| 350 | IPCThreadState::self()->getCallingUid()); |
| 351 | result.append(buffer); |
| 352 | write(fd, result.string(), result.size()); |
| 353 | return NO_ERROR; |
| 354 | } |
| 355 | |
| 356 | static bool tryLock(Mutex& mutex) |
| 357 | { |
| 358 | bool locked = false; |
| 359 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 360 | if (mutex.tryLock() == NO_ERROR) { |
| 361 | locked = true; |
| 362 | break; |
| 363 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 364 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 365 | } |
| 366 | return locked; |
| 367 | } |
| 368 | |
| 369 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 370 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 371 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 372 | dumpPermissionDenial(fd, args); |
| 373 | } else { |
| 374 | // get state of hardware lock |
| 375 | bool hardwareLocked = tryLock(mHardwareLock); |
| 376 | if (!hardwareLocked) { |
| 377 | String8 result(kHardwareLockedString); |
| 378 | write(fd, result.string(), result.size()); |
| 379 | } else { |
| 380 | mHardwareLock.unlock(); |
| 381 | } |
| 382 | |
| 383 | bool locked = tryLock(mLock); |
| 384 | |
| 385 | // failed to lock - AudioFlinger is probably deadlocked |
| 386 | if (!locked) { |
| 387 | String8 result(kDeadlockedString); |
| 388 | write(fd, result.string(), result.size()); |
| 389 | } |
| 390 | |
| 391 | dumpClients(fd, args); |
| 392 | dumpInternals(fd, args); |
| 393 | |
| 394 | // dump playback threads |
| 395 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 396 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 397 | } |
| 398 | |
| 399 | // dump record threads |
| 400 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 401 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 402 | } |
| 403 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 404 | // dump all hardware devs |
| 405 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 406 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 407 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 408 | } |
| 409 | if (locked) mLock.unlock(); |
| 410 | } |
| 411 | return NO_ERROR; |
| 412 | } |
| 413 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 414 | sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) |
| 415 | { |
| 416 | // If pid is already in the mClients wp<> map, then use that entry |
| 417 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 418 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 419 | if (client == 0) { |
| 420 | client = new Client(this, pid); |
| 421 | mClients.add(pid, client); |
| 422 | } |
| 423 | |
| 424 | return client; |
| 425 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 426 | |
| 427 | // IAudioFlinger interface |
| 428 | |
| 429 | |
| 430 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 431 | pid_t pid, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 432 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 433 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 434 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 435 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 436 | int frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 437 | IAudioFlinger::track_flags_t flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 438 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 439 | audio_io_handle_t output, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 440 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 441 | int *sessionId, |
| 442 | status_t *status) |
| 443 | { |
| 444 | sp<PlaybackThread::Track> track; |
| 445 | sp<TrackHandle> trackHandle; |
| 446 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 447 | status_t lStatus; |
| 448 | int lSessionId; |
| 449 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 450 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 451 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 452 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 453 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 454 | lStatus = BAD_VALUE; |
| 455 | goto Exit; |
| 456 | } |
| 457 | |
| 458 | { |
| 459 | Mutex::Autolock _l(mLock); |
| 460 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 461 | PlaybackThread *effectThread = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 462 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 463 | ALOGE("unknown output thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 464 | lStatus = BAD_VALUE; |
| 465 | goto Exit; |
| 466 | } |
| 467 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 468 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 469 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 470 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 471 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 472 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 473 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 474 | if (mPlaybackThreads.keyAt(i) != output) { |
| 475 | // prevent same audio session on different output threads |
| 476 | uint32_t sessions = t->hasAudioSession(*sessionId); |
| 477 | if (sessions & PlaybackThread::TRACK_SESSION) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 478 | ALOGE("createTrack() session ID %d already in use", *sessionId); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 479 | lStatus = BAD_VALUE; |
| 480 | goto Exit; |
| 481 | } |
| 482 | // check if an effect with same session ID is waiting for a track to be created |
| 483 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 484 | effectThread = t.get(); |
| 485 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 488 | lSessionId = *sessionId; |
| 489 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 490 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 491 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 492 | if (sessionId != NULL) { |
| 493 | *sessionId = lSessionId; |
| 494 | } |
| 495 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 496 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 497 | |
| 498 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 499 | channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 500 | |
| 501 | // move effect chain to this output thread if an effect on same session was waiting |
| 502 | // for a track to be created |
| 503 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 504 | Mutex::Autolock _dl(thread->mLock); |
| 505 | Mutex::Autolock _sl(effectThread->mLock); |
| 506 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 507 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 508 | |
| 509 | // Look for sync events awaiting for a session to be used. |
| 510 | for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) { |
| 511 | if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { |
| 512 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
| 513 | track->setSyncEvent(mPendingSyncEvents[i]); |
| 514 | mPendingSyncEvents.removeAt(i); |
| 515 | i--; |
| 516 | } |
| 517 | } |
| 518 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 519 | } |
| 520 | if (lStatus == NO_ERROR) { |
| 521 | trackHandle = new TrackHandle(track); |
| 522 | } else { |
| 523 | // remove local strong reference to Client before deleting the Track so that the Client |
| 524 | // destructor is called by the TrackBase destructor with mLock held |
| 525 | client.clear(); |
| 526 | track.clear(); |
| 527 | } |
| 528 | |
| 529 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 530 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | *status = lStatus; |
| 532 | } |
| 533 | return trackHandle; |
| 534 | } |
| 535 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 536 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 537 | { |
| 538 | Mutex::Autolock _l(mLock); |
| 539 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 540 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 541 | ALOGW("sampleRate() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | return thread->sampleRate(); |
| 545 | } |
| 546 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 547 | int AudioFlinger::channelCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 548 | { |
| 549 | Mutex::Autolock _l(mLock); |
| 550 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 551 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 552 | ALOGW("channelCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | return thread->channelCount(); |
| 556 | } |
| 557 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 558 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 559 | { |
| 560 | Mutex::Autolock _l(mLock); |
| 561 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 562 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 563 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 564 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 565 | } |
| 566 | return thread->format(); |
| 567 | } |
| 568 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 569 | size_t AudioFlinger::frameCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 570 | { |
| 571 | Mutex::Autolock _l(mLock); |
| 572 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 573 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 574 | ALOGW("frameCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 575 | return 0; |
| 576 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 577 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 578 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 579 | return thread->frameCount(); |
| 580 | } |
| 581 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 582 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 583 | { |
| 584 | Mutex::Autolock _l(mLock); |
| 585 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 586 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 587 | ALOGW("latency() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | return thread->latency(); |
| 591 | } |
| 592 | |
| 593 | status_t AudioFlinger::setMasterVolume(float value) |
| 594 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 595 | status_t ret = initCheck(); |
| 596 | if (ret != NO_ERROR) { |
| 597 | return ret; |
| 598 | } |
| 599 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 600 | // check calling permissions |
| 601 | if (!settingsAllowed()) { |
| 602 | return PERMISSION_DENIED; |
| 603 | } |
| 604 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 605 | float swmv = value; |
| 606 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 607 | Mutex::Autolock _l(mLock); |
| 608 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 609 | // when hw supports master volume, don't scale in sw mixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 610 | if (MVS_NONE != mMasterVolumeSupportLvl) { |
| 611 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 612 | AutoMutex lock(mHardwareLock); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 613 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 614 | |
| 615 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 616 | if (NULL != dev->set_master_volume) { |
| 617 | dev->set_master_volume(dev, value); |
| 618 | } |
| 619 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 620 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 621 | |
| 622 | swmv = 1.0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 623 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 624 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 625 | mMasterVolume = value; |
| 626 | mMasterVolumeSW = swmv; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 627 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 628 | mPlaybackThreads.valueAt(i)->setMasterVolume(swmv); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 629 | |
| 630 | return NO_ERROR; |
| 631 | } |
| 632 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 633 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 634 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 635 | status_t ret = initCheck(); |
| 636 | if (ret != NO_ERROR) { |
| 637 | return ret; |
| 638 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 639 | |
| 640 | // check calling permissions |
| 641 | if (!settingsAllowed()) { |
| 642 | return PERMISSION_DENIED; |
| 643 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 644 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 645 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 646 | return BAD_VALUE; |
| 647 | } |
| 648 | |
| 649 | { // scope for the lock |
| 650 | AutoMutex lock(mHardwareLock); |
| 651 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 652 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 653 | mHardwareStatus = AUDIO_HW_IDLE; |
| 654 | } |
| 655 | |
| 656 | if (NO_ERROR == ret) { |
| 657 | Mutex::Autolock _l(mLock); |
| 658 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 659 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 660 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | status_t AudioFlinger::setMicMute(bool state) |
| 667 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 668 | status_t ret = initCheck(); |
| 669 | if (ret != NO_ERROR) { |
| 670 | return ret; |
| 671 | } |
| 672 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 673 | // check calling permissions |
| 674 | if (!settingsAllowed()) { |
| 675 | return PERMISSION_DENIED; |
| 676 | } |
| 677 | |
| 678 | AutoMutex lock(mHardwareLock); |
| 679 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 680 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 681 | mHardwareStatus = AUDIO_HW_IDLE; |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | bool AudioFlinger::getMicMute() const |
| 686 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 687 | status_t ret = initCheck(); |
| 688 | if (ret != NO_ERROR) { |
| 689 | return false; |
| 690 | } |
| 691 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 692 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 693 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 694 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 695 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 696 | mHardwareStatus = AUDIO_HW_IDLE; |
| 697 | return state; |
| 698 | } |
| 699 | |
| 700 | status_t AudioFlinger::setMasterMute(bool muted) |
| 701 | { |
| 702 | // check calling permissions |
| 703 | if (!settingsAllowed()) { |
| 704 | return PERMISSION_DENIED; |
| 705 | } |
| 706 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 707 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 708 | // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 709 | mMasterMute = muted; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 710 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 711 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 712 | |
| 713 | return NO_ERROR; |
| 714 | } |
| 715 | |
| 716 | float AudioFlinger::masterVolume() const |
| 717 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 718 | Mutex::Autolock _l(mLock); |
| 719 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 720 | } |
| 721 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 722 | float AudioFlinger::masterVolumeSW() const |
| 723 | { |
| 724 | Mutex::Autolock _l(mLock); |
| 725 | return masterVolumeSW_l(); |
| 726 | } |
| 727 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 728 | bool AudioFlinger::masterMute() const |
| 729 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 730 | Mutex::Autolock _l(mLock); |
| 731 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 732 | } |
| 733 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 734 | float AudioFlinger::masterVolume_l() const |
| 735 | { |
| 736 | if (MVS_FULL == mMasterVolumeSupportLvl) { |
| 737 | float ret_val; |
| 738 | AutoMutex lock(mHardwareLock); |
| 739 | |
| 740 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 741 | ALOG_ASSERT((NULL != mPrimaryHardwareDev) && |
| 742 | (NULL != mPrimaryHardwareDev->get_master_volume), |
| 743 | "can't get master volume"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 744 | |
| 745 | mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val); |
| 746 | mHardwareStatus = AUDIO_HW_IDLE; |
| 747 | return ret_val; |
| 748 | } |
| 749 | |
| 750 | return mMasterVolume; |
| 751 | } |
| 752 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 753 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 754 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 755 | { |
| 756 | // check calling permissions |
| 757 | if (!settingsAllowed()) { |
| 758 | return PERMISSION_DENIED; |
| 759 | } |
| 760 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 761 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 762 | ALOGE("setStreamVolume() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 763 | return BAD_VALUE; |
| 764 | } |
| 765 | |
| 766 | AutoMutex lock(mLock); |
| 767 | PlaybackThread *thread = NULL; |
| 768 | if (output) { |
| 769 | thread = checkPlaybackThread_l(output); |
| 770 | if (thread == NULL) { |
| 771 | return BAD_VALUE; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | mStreamTypes[stream].volume = value; |
| 776 | |
| 777 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 778 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 779 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 780 | } |
| 781 | } else { |
| 782 | thread->setStreamVolume(stream, value); |
| 783 | } |
| 784 | |
| 785 | return NO_ERROR; |
| 786 | } |
| 787 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 788 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 789 | { |
| 790 | // check calling permissions |
| 791 | if (!settingsAllowed()) { |
| 792 | return PERMISSION_DENIED; |
| 793 | } |
| 794 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 795 | if (uint32_t(stream) >= AUDIO_STREAM_CNT || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 796 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 797 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 798 | return BAD_VALUE; |
| 799 | } |
| 800 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 801 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 802 | mStreamTypes[stream].mute = muted; |
| 803 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 804 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 805 | |
| 806 | return NO_ERROR; |
| 807 | } |
| 808 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 809 | float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 810 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 811 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 812 | return 0.0f; |
| 813 | } |
| 814 | |
| 815 | AutoMutex lock(mLock); |
| 816 | float volume; |
| 817 | if (output) { |
| 818 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 819 | if (thread == NULL) { |
| 820 | return 0.0f; |
| 821 | } |
| 822 | volume = thread->streamVolume(stream); |
| 823 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 824 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | return volume; |
| 828 | } |
| 829 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 830 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 831 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 832 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 833 | return true; |
| 834 | } |
| 835 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 836 | AutoMutex lock(mLock); |
| 837 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 840 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 841 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 842 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 843 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 844 | // check calling permissions |
| 845 | if (!settingsAllowed()) { |
| 846 | return PERMISSION_DENIED; |
| 847 | } |
| 848 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 849 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 850 | if (ioHandle == 0) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 851 | Mutex::Autolock _l(mLock); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 852 | status_t final_result = NO_ERROR; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 853 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 854 | AutoMutex lock(mHardwareLock); |
| 855 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 856 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 857 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 858 | status_t result = dev->set_parameters(dev, keyValuePairs.string()); |
| 859 | final_result = result ?: final_result; |
| 860 | } |
| 861 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 862 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 863 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 864 | AudioParameter param = AudioParameter(keyValuePairs); |
| 865 | String8 value; |
| 866 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 867 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 868 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 869 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 870 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
| 871 | RecordThread::RecordTrack *track = thread->track(); |
| 872 | if (track != NULL) { |
| 873 | audio_devices_t device = (audio_devices_t)( |
| 874 | thread->device() & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 875 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 876 | thread->setEffectSuspended(FX_IID_AEC, |
| 877 | suspend, |
| 878 | track->sessionId()); |
| 879 | thread->setEffectSuspended(FX_IID_NS, |
| 880 | suspend, |
| 881 | track->sessionId()); |
| 882 | } |
| 883 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 884 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 885 | } |
| 886 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 887 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 891 | // and the thread is exited once the lock is released |
| 892 | sp<ThreadBase> thread; |
| 893 | { |
| 894 | Mutex::Autolock _l(mLock); |
| 895 | thread = checkPlaybackThread_l(ioHandle); |
| 896 | if (thread == NULL) { |
| 897 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 898 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 899 | // indicate output device change to all input threads for pre processing |
| 900 | AudioParameter param = AudioParameter(keyValuePairs); |
| 901 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 902 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 903 | (value != 0)) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 904 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 905 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 906 | } |
| 907 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 910 | if (thread != 0) { |
| 911 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 912 | } |
| 913 | return BAD_VALUE; |
| 914 | } |
| 915 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 916 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 917 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 918 | // ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 919 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 920 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 921 | Mutex::Autolock _l(mLock); |
| 922 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 923 | if (ioHandle == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 924 | String8 out_s8; |
| 925 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 926 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 927 | char *s; |
| 928 | { |
| 929 | AutoMutex lock(mHardwareLock); |
| 930 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 931 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 932 | s = dev->get_parameters(dev, keys.string()); |
| 933 | mHardwareStatus = AUDIO_HW_IDLE; |
| 934 | } |
John Grossman | ef7740b | 2012-02-09 11:28:36 -0800 | [diff] [blame] | 935 | out_s8 += String8(s ? s : ""); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 936 | free(s); |
| 937 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 938 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 941 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 942 | if (playbackThread != NULL) { |
| 943 | return playbackThread->getParameters(keys); |
| 944 | } |
| 945 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 946 | if (recordThread != NULL) { |
| 947 | return recordThread->getParameters(keys); |
| 948 | } |
| 949 | return String8(""); |
| 950 | } |
| 951 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 952 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 953 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 954 | status_t ret = initCheck(); |
| 955 | if (ret != NO_ERROR) { |
| 956 | return 0; |
| 957 | } |
| 958 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 959 | AutoMutex lock(mHardwareLock); |
| 960 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 961 | struct audio_config config = { |
| 962 | sample_rate: sampleRate, |
| 963 | channel_mask: audio_channel_in_mask_from_count(channelCount), |
| 964 | format: format, |
| 965 | }; |
| 966 | size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, &config); |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 967 | mHardwareStatus = AUDIO_HW_IDLE; |
| 968 | return size; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 971 | unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 972 | { |
| 973 | if (ioHandle == 0) { |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | Mutex::Autolock _l(mLock); |
| 978 | |
| 979 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 980 | if (recordThread != NULL) { |
| 981 | return recordThread->getInputFramesLost(); |
| 982 | } |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | status_t AudioFlinger::setVoiceVolume(float value) |
| 987 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 988 | status_t ret = initCheck(); |
| 989 | if (ret != NO_ERROR) { |
| 990 | return ret; |
| 991 | } |
| 992 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | // check calling permissions |
| 994 | if (!settingsAllowed()) { |
| 995 | return PERMISSION_DENIED; |
| 996 | } |
| 997 | |
| 998 | AutoMutex lock(mHardwareLock); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 999 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1000 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1001 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1002 | |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1006 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
| 1007 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1008 | { |
| 1009 | status_t status; |
| 1010 | |
| 1011 | Mutex::Autolock _l(mLock); |
| 1012 | |
| 1013 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1014 | if (playbackThread != NULL) { |
| 1015 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1016 | } |
| 1017 | |
| 1018 | return BAD_VALUE; |
| 1019 | } |
| 1020 | |
| 1021 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1022 | { |
| 1023 | |
| 1024 | Mutex::Autolock _l(mLock); |
| 1025 | |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 1026 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1027 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1028 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1029 | client, |
| 1030 | pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1031 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1032 | |
| 1033 | mNotificationClients.add(pid, notificationClient); |
| 1034 | |
| 1035 | sp<IBinder> binder = client->asBinder(); |
| 1036 | binder->linkToDeath(notificationClient); |
| 1037 | |
| 1038 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1039 | // with AudioSystem::gLock |
| 1040 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1041 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 1042 | } |
| 1043 | |
| 1044 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1045 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1051 | { |
| 1052 | Mutex::Autolock _l(mLock); |
| 1053 | |
Glenn Kasten | a3b0925 | 2012-01-20 09:19:01 -0800 | [diff] [blame] | 1054 | mNotificationClients.removeItem(pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1055 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1056 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1057 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1058 | bool removed = false; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1059 | for (size_t i = 0; i< num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1060 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 1061 | ALOGV(" pid %d @ %d", ref->mPid, i); |
| 1062 | if (ref->mPid == pid) { |
| 1063 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1064 | mAudioSessionRefs.removeAt(i); |
| 1065 | delete ref; |
| 1066 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1067 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1068 | } else { |
| 1069 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | if (removed) { |
| 1073 | purgeStaleEffects_l(); |
| 1074 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 1078 | void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1079 | { |
| 1080 | size_t size = mNotificationClients.size(); |
| 1081 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1082 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, |
| 1083 | param2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 1088 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1089 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1090 | ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1091 | mClients.removeItem(pid); |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | // ---------------------------------------------------------------------------- |
| 1096 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1097 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 1098 | uint32_t device, type_t type) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1099 | : Thread(false), |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1100 | mType(type), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1101 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mNormalFrameCount(0), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1102 | // mChannelMask |
| 1103 | mChannelCount(0), |
| 1104 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), |
| 1105 | mParamStatus(NO_ERROR), |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1106 | mStandby(false), mId(id), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1107 | mDevice(device), |
| 1108 | mDeathRecipient(new PMDeathRecipient(this)) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1109 | { |
| 1110 | } |
| 1111 | |
| 1112 | AudioFlinger::ThreadBase::~ThreadBase() |
| 1113 | { |
| 1114 | mParamCond.broadcast(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1115 | // do not lock the mutex in destructor |
| 1116 | releaseWakeLock_l(); |
Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1117 | if (mPowerManager != 0) { |
| 1118 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 1119 | binder->unlinkToDeath(mDeathRecipient); |
| 1120 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void AudioFlinger::ThreadBase::exit() |
| 1124 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1125 | ALOGV("ThreadBase::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1126 | { |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1127 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 1128 | // if (!exitPending()) { |
| 1129 | // // context switch from here to exit() |
| 1130 | // // exit() calls requestExit(), what exitPending() observes |
| 1131 | // // exit() calls signal(), which is dropped since no waiters |
| 1132 | // // context switch back from exit() to here |
| 1133 | // mWaitWorkCV.wait(...); |
| 1134 | // // now thread is hung |
| 1135 | // } |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1136 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1137 | requestExit(); |
| 1138 | mWaitWorkCV.signal(); |
| 1139 | } |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1140 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 1141 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1142 | requestExitAndWait(); |
| 1143 | } |
| 1144 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1145 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1146 | { |
| 1147 | status_t status; |
| 1148 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1149 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1150 | Mutex::Autolock _l(mLock); |
| 1151 | |
| 1152 | mNewParameters.add(keyValuePairs); |
| 1153 | mWaitWorkCV.signal(); |
| 1154 | // wait condition with timeout in case the thread loop has exited |
| 1155 | // before the request could be processed |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1156 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1157 | status = mParamStatus; |
| 1158 | mWaitWorkCV.signal(); |
| 1159 | } else { |
| 1160 | status = TIMED_OUT; |
| 1161 | } |
| 1162 | return status; |
| 1163 | } |
| 1164 | |
| 1165 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1166 | { |
| 1167 | Mutex::Autolock _l(mLock); |
| 1168 | sendConfigEvent_l(event, param); |
| 1169 | } |
| 1170 | |
| 1171 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1172 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1173 | { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1174 | ConfigEvent configEvent; |
| 1175 | configEvent.mEvent = event; |
| 1176 | configEvent.mParam = param; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1177 | mConfigEvents.add(configEvent); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1178 | ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1179 | mWaitWorkCV.signal(); |
| 1180 | } |
| 1181 | |
| 1182 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1183 | { |
| 1184 | mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1185 | while (!mConfigEvents.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1186 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1187 | ConfigEvent configEvent = mConfigEvents[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1188 | mConfigEvents.removeAt(0); |
| 1189 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1190 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 1191 | mLock.unlock(); |
| 1192 | mAudioFlinger->mLock.lock(); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1193 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1194 | mAudioFlinger->mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1195 | mLock.lock(); |
| 1196 | } |
| 1197 | mLock.unlock(); |
| 1198 | } |
| 1199 | |
| 1200 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1201 | { |
| 1202 | const size_t SIZE = 256; |
| 1203 | char buffer[SIZE]; |
| 1204 | String8 result; |
| 1205 | |
| 1206 | bool locked = tryLock(mLock); |
| 1207 | if (!locked) { |
| 1208 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1209 | write(fd, buffer, strlen(buffer)); |
| 1210 | } |
| 1211 | |
Eric Laurent | 612bbb5 | 2012-03-14 15:03:26 -0700 | [diff] [blame] | 1212 | snprintf(buffer, SIZE, "io handle: %d\n", mId); |
| 1213 | result.append(buffer); |
| 1214 | snprintf(buffer, SIZE, "TID: %d\n", getTid()); |
| 1215 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1216 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1217 | result.append(buffer); |
| 1218 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1219 | result.append(buffer); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1220 | snprintf(buffer, SIZE, "HAL frame count: %d\n", mFrameCount); |
| 1221 | result.append(buffer); |
| 1222 | snprintf(buffer, SIZE, "Normal frame count: %d\n", mNormalFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1223 | result.append(buffer); |
| 1224 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1225 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1226 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1227 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1228 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1229 | result.append(buffer); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1230 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1231 | result.append(buffer); |
| 1232 | |
| 1233 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1234 | result.append(buffer); |
| 1235 | result.append(" Index Command"); |
| 1236 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1237 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1238 | result.append(buffer); |
| 1239 | result.append(mNewParameters[i]); |
| 1240 | } |
| 1241 | |
| 1242 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1243 | result.append(buffer); |
| 1244 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1245 | result.append(buffer); |
| 1246 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1247 | snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1248 | result.append(buffer); |
| 1249 | } |
| 1250 | result.append("\n"); |
| 1251 | |
| 1252 | write(fd, result.string(), result.size()); |
| 1253 | |
| 1254 | if (locked) { |
| 1255 | mLock.unlock(); |
| 1256 | } |
| 1257 | return NO_ERROR; |
| 1258 | } |
| 1259 | |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1260 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1261 | { |
| 1262 | const size_t SIZE = 256; |
| 1263 | char buffer[SIZE]; |
| 1264 | String8 result; |
| 1265 | |
| 1266 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1267 | write(fd, buffer, strlen(buffer)); |
| 1268 | |
| 1269 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1270 | sp<EffectChain> chain = mEffectChains[i]; |
| 1271 | if (chain != 0) { |
| 1272 | chain->dump(fd, args); |
| 1273 | } |
| 1274 | } |
| 1275 | return NO_ERROR; |
| 1276 | } |
| 1277 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1278 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1279 | { |
| 1280 | Mutex::Autolock _l(mLock); |
| 1281 | acquireWakeLock_l(); |
| 1282 | } |
| 1283 | |
| 1284 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1285 | { |
| 1286 | if (mPowerManager == 0) { |
| 1287 | // use checkService() to avoid blocking if power service is not up yet |
| 1288 | sp<IBinder> binder = |
| 1289 | defaultServiceManager()->checkService(String16("power")); |
| 1290 | if (binder == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1291 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1292 | } else { |
| 1293 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1294 | binder->linkToDeath(mDeathRecipient); |
| 1295 | } |
| 1296 | } |
| 1297 | if (mPowerManager != 0) { |
| 1298 | sp<IBinder> binder = new BBinder(); |
| 1299 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1300 | binder, |
| 1301 | String16(mName)); |
| 1302 | if (status == NO_ERROR) { |
| 1303 | mWakeLockToken = binder; |
| 1304 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1305 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1310 | { |
| 1311 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1312 | releaseWakeLock_l(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1316 | { |
| 1317 | if (mWakeLockToken != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1318 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1319 | if (mPowerManager != 0) { |
| 1320 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1321 | } |
| 1322 | mWakeLockToken.clear(); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1327 | { |
| 1328 | Mutex::Autolock _l(mLock); |
| 1329 | releaseWakeLock_l(); |
| 1330 | mPowerManager.clear(); |
| 1331 | } |
| 1332 | |
| 1333 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1334 | { |
| 1335 | sp<ThreadBase> thread = mThread.promote(); |
| 1336 | if (thread != 0) { |
| 1337 | thread->clearPowerManager(); |
| 1338 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1339 | ALOGW("power manager service died !!!"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1340 | } |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1341 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1342 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1343 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1344 | { |
| 1345 | Mutex::Autolock _l(mLock); |
| 1346 | setEffectSuspended_l(type, suspend, sessionId); |
| 1347 | } |
| 1348 | |
| 1349 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1350 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1351 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 1352 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1353 | if (chain != 0) { |
| 1354 | if (type != NULL) { |
| 1355 | chain->setEffectSuspended_l(type, suspend); |
| 1356 | } else { |
| 1357 | chain->setEffectSuspendedAll_l(suspend); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1362 | } |
| 1363 | |
| 1364 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1365 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1366 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1367 | if (index < 0) { |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1372 | mSuspendedSessions.editValueAt(index); |
| 1373 | |
| 1374 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1375 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1376 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1377 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1378 | chain->setEffectSuspendedAll_l(true); |
| 1379 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1380 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1381 | desc->mType.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1382 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1388 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1389 | bool suspend, |
| 1390 | int sessionId) |
| 1391 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1392 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1393 | |
| 1394 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1395 | |
| 1396 | if (suspend) { |
| 1397 | if (index >= 0) { |
| 1398 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1399 | } else { |
| 1400 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1401 | } |
| 1402 | } else { |
| 1403 | if (index < 0) { |
| 1404 | return; |
| 1405 | } |
| 1406 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | int key = EffectChain::kKeyForSuspendAll; |
| 1411 | if (type != NULL) { |
| 1412 | key = type->timeLow; |
| 1413 | } |
| 1414 | index = sessionEffects.indexOfKey(key); |
| 1415 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1416 | sp<SuspendedSessionDesc> desc; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1417 | if (suspend) { |
| 1418 | if (index >= 0) { |
| 1419 | desc = sessionEffects.valueAt(index); |
| 1420 | } else { |
| 1421 | desc = new SuspendedSessionDesc(); |
| 1422 | if (type != NULL) { |
| 1423 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1424 | } |
| 1425 | sessionEffects.add(key, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1426 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1427 | } |
| 1428 | desc->mRefCount++; |
| 1429 | } else { |
| 1430 | if (index < 0) { |
| 1431 | return; |
| 1432 | } |
| 1433 | desc = sessionEffects.valueAt(index); |
| 1434 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1435 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1436 | sessionEffects.removeItemsAt(index); |
| 1437 | if (sessionEffects.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1438 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1439 | sessionId); |
| 1440 | mSuspendedSessions.removeItem(sessionId); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | if (!sessionEffects.isEmpty()) { |
| 1445 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1450 | bool enabled, |
| 1451 | int sessionId) |
| 1452 | { |
| 1453 | Mutex::Autolock _l(mLock); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1454 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1455 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1456 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1457 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1458 | bool enabled, |
| 1459 | int sessionId) |
| 1460 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1461 | if (mType != RECORD) { |
| 1462 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1463 | // another session. This gives the priority to well behaved effect control panels |
| 1464 | // and applications not using global effects. |
| 1465 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
| 1466 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1467 | } |
| 1468 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1469 | |
| 1470 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1471 | if (chain != 0) { |
| 1472 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1473 | } |
| 1474 | } |
| 1475 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1476 | // ---------------------------------------------------------------------------- |
| 1477 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1478 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1479 | AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1480 | audio_io_handle_t id, |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1481 | uint32_t device, |
| 1482 | type_t type) |
| 1483 | : ThreadBase(audioFlinger, id, device, type), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1484 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), |
| 1485 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1486 | // but it would be safer to explicitly pass initial masterMute as parameter |
| 1487 | mMasterMute(audioFlinger->masterMute_l()), |
| 1488 | // mStreamTypes[] initialized in constructor body |
| 1489 | mOutput(output), |
| 1490 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1491 | // but it would be safer to explicitly pass initial masterVolume as parameter |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1492 | mMasterVolume(audioFlinger->masterVolumeSW_l()), |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 1493 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Glenn Kasten | aa4397f | 2012-03-12 18:13:59 -0700 | [diff] [blame] | 1494 | mMixerStatus(MIXER_IDLE), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1495 | standbyDelay(AudioFlinger::mStandbyTimeInNsecs), |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 1496 | // index 0 is reserved for normal mixer's submix |
| 1497 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1498 | { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 1499 | snprintf(mName, kNameLength, "AudioOut_%X", id); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1500 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1501 | readOutputParameters(); |
| 1502 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1503 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1504 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
| 1505 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; |
| 1506 | stream = (audio_stream_type_t) (stream + 1)) { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1507 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1508 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1509 | } |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1510 | // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, |
| 1511 | // because mAudioFlinger doesn't have one to copy from |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1515 | { |
| 1516 | delete [] mMixBuffer; |
| 1517 | } |
| 1518 | |
| 1519 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1520 | { |
| 1521 | dumpInternals(fd, args); |
| 1522 | dumpTracks(fd, args); |
| 1523 | dumpEffectChains(fd, args); |
| 1524 | return NO_ERROR; |
| 1525 | } |
| 1526 | |
| 1527 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1528 | { |
| 1529 | const size_t SIZE = 256; |
| 1530 | char buffer[SIZE]; |
| 1531 | String8 result; |
| 1532 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1533 | result.appendFormat("Output thread %p stream volumes in dB:\n ", this); |
| 1534 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1535 | const stream_type_t *st = &mStreamTypes[i]; |
| 1536 | if (i > 0) { |
| 1537 | result.appendFormat(", "); |
| 1538 | } |
| 1539 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1540 | if (st->mute) { |
| 1541 | result.append("M"); |
| 1542 | } |
| 1543 | } |
| 1544 | result.append("\n"); |
| 1545 | write(fd, result.string(), result.length()); |
| 1546 | result.clear(); |
| 1547 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1548 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1549 | result.append(buffer); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 1550 | Track::appendDumpHeader(result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1551 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1552 | sp<Track> track = mTracks[i]; |
| 1553 | if (track != 0) { |
| 1554 | track->dump(buffer, SIZE); |
| 1555 | result.append(buffer); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1560 | result.append(buffer); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 1561 | Track::appendDumpHeader(result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1562 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 1563 | sp<Track> track = mActiveTracks[i].promote(); |
| 1564 | if (track != 0) { |
| 1565 | track->dump(buffer, SIZE); |
| 1566 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | write(fd, result.string(), result.size()); |
| 1570 | return NO_ERROR; |
| 1571 | } |
| 1572 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1573 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1574 | { |
| 1575 | const size_t SIZE = 256; |
| 1576 | char buffer[SIZE]; |
| 1577 | String8 result; |
| 1578 | |
| 1579 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1580 | result.append(buffer); |
| 1581 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1582 | result.append(buffer); |
| 1583 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1584 | result.append(buffer); |
| 1585 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1586 | result.append(buffer); |
| 1587 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1588 | result.append(buffer); |
| 1589 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1590 | result.append(buffer); |
| 1591 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1592 | result.append(buffer); |
| 1593 | write(fd, result.string(), result.size()); |
| 1594 | |
| 1595 | dumpBase(fd, args); |
| 1596 | |
| 1597 | return NO_ERROR; |
| 1598 | } |
| 1599 | |
| 1600 | // Thread virtuals |
| 1601 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1602 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1603 | status_t status = initCheck(); |
| 1604 | if (status == NO_ERROR) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1605 | ALOGI("AudioFlinger's thread %p ready to run", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1606 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1607 | ALOGE("No working audio driver found."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1608 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1609 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1613 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1614 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 1618 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1619 | const sp<AudioFlinger::Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1620 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1621 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1622 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1623 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1624 | int frameCount, |
| 1625 | const sp<IMemory>& sharedBuffer, |
| 1626 | int sessionId, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1627 | IAudioFlinger::track_flags_t flags, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1628 | pid_t tid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1629 | status_t *status) |
| 1630 | { |
| 1631 | sp<Track> track; |
| 1632 | status_t lStatus; |
| 1633 | |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1634 | bool isTimed = (flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1635 | |
| 1636 | // client expresses a preference for FAST, but we get the final say |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1637 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 1638 | if ( |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1639 | // not timed |
| 1640 | (!isTimed) && |
| 1641 | // either of these use cases: |
| 1642 | ( |
| 1643 | // use case 1: shared buffer with any frame count |
| 1644 | ( |
| 1645 | (sharedBuffer != 0) |
| 1646 | ) || |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1647 | // use case 2: callback handler and frame count is default or at least as large as HAL |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1648 | ( |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1649 | (tid != -1) && |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1650 | ((frameCount == 0) || |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1651 | (frameCount >= (int) (mFrameCount * 2))) // * 2 is due to SRC jitter, see below |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1652 | ) |
| 1653 | ) && |
| 1654 | // PCM data |
| 1655 | audio_is_linear_pcm(format) && |
| 1656 | // mono or stereo |
| 1657 | ( (channelMask == AUDIO_CHANNEL_OUT_MONO) || |
| 1658 | (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) && |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1659 | #ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1660 | // hardware sample rate |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1661 | (sampleRate == mSampleRate) && |
| 1662 | #endif |
| 1663 | // normal mixer has an associated fast mixer |
| 1664 | hasFastMixer() && |
| 1665 | // there are sufficient fast track slots available |
| 1666 | (mFastTrackAvailMask != 0) |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1667 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1668 | // FIXME add a permission test also? |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1669 | ) { |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1670 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1671 | if (frameCount == 0) { |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1672 | frameCount = mFrameCount * 2; // FIXME * 2 is due to SRC jitter, should be computed |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1673 | } |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1674 | ALOGI("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
| 1675 | frameCount, mFrameCount); |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1676 | } else { |
| 1677 | ALOGW("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1678 | "mFrameCount=%d format=%d isLinear=%d channelMask=%d sampleRate=%d mSampleRate=%d " |
| 1679 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
| 1680 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, |
| 1681 | audio_is_linear_pcm(format), |
| 1682 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1683 | flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1684 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1685 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1686 | // This is probably too conservative, but legacy application code may depend on it. |
| 1687 | // If you change this calculation, also review the start threshold which is related. |
| 1688 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1689 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1690 | if (minBufCount < 2) { |
| 1691 | minBufCount = 2; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1692 | } |
Glenn Kasten | e0fa467 | 2012-04-24 14:35:14 -0700 | [diff] [blame] | 1693 | int minFrameCount = mNormalFrameCount * minBufCount; |
| 1694 | if (frameCount < minFrameCount) { |
| 1695 | frameCount = minFrameCount; |
| 1696 | } |
| 1697 | } |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1700 | if (mType == DIRECT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1701 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1702 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1703 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1704 | "for output %p with format %d", |
| 1705 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1706 | lStatus = BAD_VALUE; |
| 1707 | goto Exit; |
| 1708 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } else { |
| 1711 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1712 | if (sampleRate > mSampleRate*2) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1713 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1714 | lStatus = BAD_VALUE; |
| 1715 | goto Exit; |
| 1716 | } |
| 1717 | } |
| 1718 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1719 | lStatus = initCheck(); |
| 1720 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1721 | ALOGE("Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1722 | goto Exit; |
| 1723 | } |
| 1724 | |
| 1725 | { // scope for mLock |
| 1726 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1727 | |
| 1728 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1729 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1730 | // manager |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1731 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1732 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1733 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 639dbee | 2012-03-07 12:26:34 -0800 | [diff] [blame] | 1734 | if (t != 0 && !t->isOutputTrack()) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1735 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1736 | if (sessionId == t->sessionId() && strategy != actual) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1737 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1738 | strategy, actual); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1739 | lStatus = BAD_VALUE; |
| 1740 | goto Exit; |
| 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1745 | if (!isTimed) { |
| 1746 | track = new Track(this, client, streamType, sampleRate, format, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 1747 | channelMask, frameCount, sharedBuffer, sessionId, flags); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1748 | } else { |
| 1749 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
| 1750 | channelMask, frameCount, sharedBuffer, sessionId); |
| 1751 | } |
| 1752 | if (track == NULL || track->getCblk() == NULL || track->name() < 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1753 | lStatus = NO_MEMORY; |
| 1754 | goto Exit; |
| 1755 | } |
| 1756 | mTracks.add(track); |
| 1757 | |
| 1758 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1759 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1760 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1761 | track->setMainBuffer(chain->inBuffer()); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1762 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1763 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1764 | } |
| 1765 | } |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 1766 | |
| 1767 | #ifdef HAVE_REQUEST_PRIORITY |
| 1768 | if ((flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1769 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1770 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1771 | // so ask activity manager to do this on our behalf |
| 1772 | int err = requestPriority(callingPid, tid, 1); |
| 1773 | if (err != 0) { |
| 1774 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 1775 | 1, callingPid, tid, err); |
| 1776 | } |
| 1777 | } |
| 1778 | #endif |
| 1779 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1780 | lStatus = NO_ERROR; |
| 1781 | |
| 1782 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1783 | if (status) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1784 | *status = lStatus; |
| 1785 | } |
| 1786 | return track; |
| 1787 | } |
| 1788 | |
| 1789 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1790 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1791 | Mutex::Autolock _l(mLock); |
| 1792 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1793 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1794 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1795 | return 0; |
| 1796 | } |
| 1797 | } |
| 1798 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1799 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1800 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1801 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1802 | mMasterVolume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1805 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1806 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1807 | Mutex::Autolock _l(mLock); |
| 1808 | setMasterMute_l(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1811 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1812 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1813 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1814 | mStreamTypes[stream].volume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1817 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1818 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1819 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1820 | mStreamTypes[stream].mute = muted; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1823 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1824 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1825 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1826 | return mStreamTypes[stream].volume; |
| 1827 | } |
| 1828 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1829 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1830 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1831 | { |
| 1832 | status_t status = ALREADY_EXISTS; |
| 1833 | |
| 1834 | // set retry count for buffer fill |
| 1835 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1836 | if (mActiveTracks.indexOf(track) < 0) { |
| 1837 | // the track is newly added, make sure it fills up all its |
| 1838 | // buffers before playing. This is to ensure the client will |
| 1839 | // effectively get the latency it requested. |
| 1840 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1841 | track->mResetDone = false; |
| 1842 | mActiveTracks.add(track); |
| 1843 | if (track->mainBuffer() != mMixBuffer) { |
| 1844 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1845 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1846 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1847 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | status = NO_ERROR; |
| 1852 | } |
| 1853 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1854 | ALOGV("mWaitWorkCV.broadcast"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1855 | mWaitWorkCV.broadcast(); |
| 1856 | |
| 1857 | return status; |
| 1858 | } |
| 1859 | |
| 1860 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1861 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1862 | { |
| 1863 | track->mState = TrackBase::TERMINATED; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 1864 | // active tracks are removed by threadLoop() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1865 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1866 | removeTrack_l(track); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1871 | { |
| 1872 | mTracks.remove(track); |
| 1873 | deleteTrackName_l(track->name()); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 1874 | // redundant as track is about to be destroyed, for dumpsys only |
| 1875 | track->mName = -1; |
| 1876 | if (track->isFastTrack()) { |
| 1877 | int index = track->mFastIndex; |
| 1878 | ALOG_ASSERT(0 < index && index < FastMixerState::kMaxFastTracks); |
| 1879 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1880 | mFastTrackAvailMask |= 1 << index; |
| 1881 | // redundant as track is about to be destroyed, for dumpsys only |
| 1882 | track->mFastIndex = -1; |
| 1883 | } |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1884 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1885 | if (chain != 0) { |
| 1886 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1891 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1892 | String8 out_s8 = String8(""); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1893 | char *s; |
| 1894 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1895 | Mutex::Autolock _l(mLock); |
| 1896 | if (initCheck() != NO_ERROR) { |
| 1897 | return out_s8; |
| 1898 | } |
| 1899 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1900 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1901 | out_s8 = String8(s); |
| 1902 | free(s); |
| 1903 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1906 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1907 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1908 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1909 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1910 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1911 | ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1912 | |
| 1913 | switch (event) { |
| 1914 | case AudioSystem::OUTPUT_OPENED: |
| 1915 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1916 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1917 | desc.samplingRate = mSampleRate; |
| 1918 | desc.format = mFormat; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1919 | desc.frameCount = mNormalFrameCount; // FIXME see AudioFlinger::frameCount(audio_io_handle_t) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1920 | desc.latency = latency(); |
| 1921 | param2 = &desc; |
| 1922 | break; |
| 1923 | |
| 1924 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1925 | param2 = ¶m; |
| 1926 | case AudioSystem::OUTPUT_CLOSED: |
| 1927 | default: |
| 1928 | break; |
| 1929 | } |
| 1930 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1931 | } |
| 1932 | |
| 1933 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1934 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1935 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1936 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1937 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1938 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1939 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1940 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1941 | if (mFrameCount & 15) { |
| 1942 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 1943 | mFrameCount); |
| 1944 | } |
| 1945 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1946 | // Calculate size of normal mix buffer relative to the HAL output buffer size |
| 1947 | uint32_t multiple = 1; |
| 1948 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1949 | size_t minNormalFrameCount = (kMinNormalMixBufferSizeMs * mSampleRate) / 1000; |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1950 | multiple = (minNormalFrameCount + mFrameCount - 1) / mFrameCount; |
| 1951 | // force multiple to be even, for compatibility with doubling of fast tracks due to HAL SRC |
| 1952 | // (it would be unusual for the normal mix buffer size to not be a multiple of fast track) |
| 1953 | // FIXME this rounding up should not be done if no HAL SRC |
| 1954 | if ((multiple > 2) && (multiple & 1)) { |
| 1955 | ++multiple; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1956 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1957 | } |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 1958 | mNormalFrameCount = multiple * mFrameCount; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1959 | ALOGI("HAL output buffer size %u frames, normal mix buffer size %u frames", mFrameCount, mNormalFrameCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1960 | |
| 1961 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1962 | // Allocate a stereo buffer even if HW output is mono. |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 1963 | delete[] mMixBuffer; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1964 | mMixBuffer = new int16_t[mNormalFrameCount * 2]; |
| 1965 | memset(mMixBuffer, 0, mNormalFrameCount * 2 * sizeof(int16_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1966 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1967 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1968 | // parameters into account |
| 1969 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1970 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1971 | // matter. |
| 1972 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1973 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1974 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1975 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1976 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1980 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1981 | if (halFrames == NULL || dspFrames == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1982 | return BAD_VALUE; |
| 1983 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1984 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1985 | if (initCheck() != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1986 | return INVALID_OPERATION; |
| 1987 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1988 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1989 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1990 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1993 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1994 | { |
| 1995 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1996 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1997 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1998 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2002 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2003 | if (sessionId == track->sessionId() && |
| 2004 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2005 | result |= TRACK_SESSION; |
| 2006 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2010 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2013 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 2014 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2015 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2016 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2017 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2018 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2019 | } |
| 2020 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2021 | sp<Track> track = mTracks[i]; |
| 2022 | if (sessionId == track->sessionId() && |
| 2023 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2024 | return AudioSystem::getStrategyForStream(track->streamType()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2025 | } |
| 2026 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2027 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2030 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 2031 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2032 | { |
| 2033 | Mutex::Autolock _l(mLock); |
| 2034 | return mOutput; |
| 2035 | } |
| 2036 | |
| 2037 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 2038 | { |
| 2039 | Mutex::Autolock _l(mLock); |
| 2040 | AudioStreamOut *output = mOutput; |
| 2041 | mOutput = NULL; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2042 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2043 | // must push a NULL and wait for ack |
| 2044 | mOutputSink.clear(); |
| 2045 | mPipeSink.clear(); |
| 2046 | mNormalSink.clear(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2047 | return output; |
| 2048 | } |
| 2049 | |
| 2050 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 2051 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2052 | { |
| 2053 | if (mOutput == NULL) { |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | return &mOutput->stream->common; |
| 2057 | } |
| 2058 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 2059 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2060 | { |
| 2061 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 2062 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 2063 | // underruns |
| 2064 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2065 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2066 | } else { |
| 2067 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 2068 | } |
| 2069 | } |
| 2070 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2071 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2072 | { |
| 2073 | if (!isValidSyncEvent(event)) { |
| 2074 | return BAD_VALUE; |
| 2075 | } |
| 2076 | |
| 2077 | Mutex::Autolock _l(mLock); |
| 2078 | |
| 2079 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2080 | sp<Track> track = mTracks[i]; |
| 2081 | if (event->triggerSession() == track->sessionId()) { |
| 2082 | track->setSyncEvent(event); |
| 2083 | return NO_ERROR; |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | return NAME_NOT_FOUND; |
| 2088 | } |
| 2089 | |
| 2090 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) |
| 2091 | { |
| 2092 | switch (event->type()) { |
| 2093 | case AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE: |
| 2094 | return true; |
| 2095 | default: |
| 2096 | break; |
| 2097 | } |
| 2098 | return false; |
| 2099 | } |
| 2100 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2101 | // ---------------------------------------------------------------------------- |
| 2102 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 2103 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2104 | audio_io_handle_t id, uint32_t device, type_t type) |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2105 | : PlaybackThread(audioFlinger, output, id, device, type), |
| 2106 | // mAudioMixer below |
| 2107 | #ifdef SOAKER |
| 2108 | mSoaker(NULL), |
| 2109 | #endif |
| 2110 | // mFastMixer below |
| 2111 | mFastMixerFutex(0) |
| 2112 | // mOutputSink below |
| 2113 | // mPipeSink below |
| 2114 | // mNormalSink below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2115 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2116 | ALOGV("MixerThread() id=%d device=%d type=%d", id, device, type); |
| 2117 | ALOGV("mSampleRate=%d, mChannelMask=%d, mChannelCount=%d, mFormat=%d, mFrameSize=%d, " |
| 2118 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 2119 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 2120 | mNormalFrameCount); |
| 2121 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 2122 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2123 | // FIXME - Current mixer implementation only supports stereo output |
| 2124 | if (mChannelCount == 1) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2125 | ALOGE("Invalid audio hardware channel count"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2126 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2127 | |
| 2128 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 2129 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 2130 | size_t numCounterOffers = 0; |
| 2131 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount)}; |
| 2132 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2133 | ALOG_ASSERT(index == 0); |
| 2134 | |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2135 | // initialize fast mixer depending on configuration |
| 2136 | bool initFastMixer; |
| 2137 | switch (kUseFastMixer) { |
| 2138 | case FastMixer_Never: |
| 2139 | initFastMixer = false; |
| 2140 | break; |
| 2141 | case FastMixer_Always: |
| 2142 | initFastMixer = true; |
| 2143 | break; |
| 2144 | case FastMixer_Static: |
| 2145 | case FastMixer_Dynamic: |
| 2146 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 2147 | break; |
| 2148 | } |
| 2149 | if (initFastMixer) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2150 | |
| 2151 | // create a MonoPipe to connect our submix to FastMixer |
| 2152 | NBAIO_Format format = mOutputSink->format(); |
| 2153 | // frame count will be rounded up to a power of 2, so this formula should work well |
| 2154 | MonoPipe *monoPipe = new MonoPipe((mNormalFrameCount * 3) / 2, format, |
| 2155 | true /*writeCanBlock*/); |
| 2156 | const NBAIO_Format offers[1] = {format}; |
| 2157 | size_t numCounterOffers = 0; |
| 2158 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2159 | ALOG_ASSERT(index == 0); |
| 2160 | mPipeSink = monoPipe; |
| 2161 | |
| 2162 | #ifdef SOAKER |
| 2163 | // create a soaker as workaround for governor issues |
| 2164 | mSoaker = new Soaker(); |
| 2165 | // FIXME Soaker should only run when needed, i.e. when FastMixer is not in COLD_IDLE |
| 2166 | mSoaker->run("Soaker", PRIORITY_LOWEST); |
| 2167 | #endif |
| 2168 | |
| 2169 | // create fast mixer and configure it initially with just one fast track for our submix |
| 2170 | mFastMixer = new FastMixer(); |
| 2171 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2172 | FastMixerState *state = sq->begin(); |
| 2173 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2174 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 2175 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 2176 | fastTrack->mVolumeProvider = NULL; |
| 2177 | fastTrack->mGeneration++; |
| 2178 | state->mFastTracksGen++; |
| 2179 | state->mTrackMask = 1; |
| 2180 | // fast mixer will use the HAL output sink |
| 2181 | state->mOutputSink = mOutputSink.get(); |
| 2182 | state->mOutputSinkGen++; |
| 2183 | state->mFrameCount = mFrameCount; |
| 2184 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2185 | // already done in constructor initialization list |
| 2186 | //mFastMixerFutex = 0; |
| 2187 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2188 | state->mColdGen++; |
| 2189 | state->mDumpState = &mFastMixerDumpState; |
| 2190 | sq->end(); |
| 2191 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2192 | |
| 2193 | // start the fast mixer |
| 2194 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 2195 | #ifdef HAVE_REQUEST_PRIORITY |
| 2196 | pid_t tid = mFastMixer->getTid(); |
| 2197 | int err = requestPriority(getpid_cached, tid, 2); |
| 2198 | if (err != 0) { |
| 2199 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2200 | 2, getpid_cached, tid, err); |
| 2201 | } |
| 2202 | #endif |
| 2203 | |
| 2204 | } else { |
| 2205 | mFastMixer = NULL; |
| 2206 | } |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2207 | |
| 2208 | switch (kUseFastMixer) { |
| 2209 | case FastMixer_Never: |
| 2210 | case FastMixer_Dynamic: |
| 2211 | mNormalSink = mOutputSink; |
| 2212 | break; |
| 2213 | case FastMixer_Always: |
| 2214 | mNormalSink = mPipeSink; |
| 2215 | break; |
| 2216 | case FastMixer_Static: |
| 2217 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 2218 | break; |
| 2219 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | AudioFlinger::MixerThread::~MixerThread() |
| 2223 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2224 | if (mFastMixer != NULL) { |
| 2225 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2226 | FastMixerState *state = sq->begin(); |
| 2227 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2228 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2229 | if (old == -1) { |
| 2230 | __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
| 2231 | } |
| 2232 | } |
| 2233 | state->mCommand = FastMixerState::EXIT; |
| 2234 | sq->end(); |
| 2235 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2236 | mFastMixer->join(); |
| 2237 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 2238 | // We'll use that extract the final state which contains one remaining fast track |
| 2239 | // corresponding to our sub-mix. |
| 2240 | state = sq->begin(); |
| 2241 | ALOG_ASSERT(state->mTrackMask == 1); |
| 2242 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2243 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 2244 | delete fastTrack->mBufferProvider; |
| 2245 | sq->end(false /*didModify*/); |
| 2246 | delete mFastMixer; |
| 2247 | #ifdef SOAKER |
| 2248 | if (mSoaker != NULL) { |
| 2249 | mSoaker->requestExitAndWait(); |
| 2250 | } |
| 2251 | delete mSoaker; |
| 2252 | #endif |
| 2253 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2254 | delete mAudioMixer; |
| 2255 | } |
| 2256 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2257 | class CpuStats { |
| 2258 | public: |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2259 | CpuStats(); |
| 2260 | void sample(const String8 &title); |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2261 | #ifdef DEBUG_CPU_USAGE |
| 2262 | private: |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2263 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 2264 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 2265 | |
| 2266 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 2267 | |
| 2268 | int mCpuNum; // thread's current CPU number |
| 2269 | int mCpukHz; // frequency of thread's current CPU in kHz |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2270 | #endif |
| 2271 | }; |
| 2272 | |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2273 | CpuStats::CpuStats() |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2274 | #ifdef DEBUG_CPU_USAGE |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2275 | : mCpuNum(-1), mCpukHz(-1) |
| 2276 | #endif |
| 2277 | { |
| 2278 | } |
| 2279 | |
| 2280 | void CpuStats::sample(const String8 &title) { |
| 2281 | #ifdef DEBUG_CPU_USAGE |
| 2282 | // get current thread's delta CPU time in wall clock ns |
| 2283 | double wcNs; |
| 2284 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 2285 | |
| 2286 | // record sample for wall clock statistics |
| 2287 | if (valid) { |
| 2288 | mWcStats.sample(wcNs); |
| 2289 | } |
| 2290 | |
| 2291 | // get the current CPU number |
| 2292 | int cpuNum = sched_getcpu(); |
| 2293 | |
| 2294 | // get the current CPU frequency in kHz |
| 2295 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 2296 | |
| 2297 | // check if either CPU number or frequency changed |
| 2298 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 2299 | mCpuNum = cpuNum; |
| 2300 | mCpukHz = cpukHz; |
| 2301 | // ignore sample for purposes of cycles |
| 2302 | valid = false; |
| 2303 | } |
| 2304 | |
| 2305 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 2306 | if (valid && mCpukHz > 0) { |
| 2307 | double cycles = wcNs * cpukHz * 0.000001; |
| 2308 | mHzStats.sample(cycles); |
| 2309 | } |
| 2310 | |
| 2311 | unsigned n = mWcStats.n(); |
| 2312 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2313 | if ((n & 127) == 1) { |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2314 | long long elapsed = mCpuUsage.elapsed(); |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2315 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 2316 | double perLoop = elapsed / (double) n; |
| 2317 | double perLoop100 = perLoop * 0.01; |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2318 | double perLoop1k = perLoop * 0.001; |
| 2319 | double mean = mWcStats.mean(); |
| 2320 | double stddev = mWcStats.stddev(); |
| 2321 | double minimum = mWcStats.minimum(); |
| 2322 | double maximum = mWcStats.maximum(); |
| 2323 | double meanCycles = mHzStats.mean(); |
| 2324 | double stddevCycles = mHzStats.stddev(); |
| 2325 | double minCycles = mHzStats.minimum(); |
| 2326 | double maxCycles = mHzStats.maximum(); |
| 2327 | mCpuUsage.resetElapsed(); |
| 2328 | mWcStats.reset(); |
| 2329 | mHzStats.reset(); |
| 2330 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 2331 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 2332 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 2333 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 2334 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 2335 | title.string(), |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2336 | elapsed * .000000001, n, perLoop * .000001, |
| 2337 | mean * .001, |
| 2338 | stddev * .001, |
| 2339 | minimum * .001, |
| 2340 | maximum * .001, |
| 2341 | mean / perLoop100, |
| 2342 | stddev / perLoop100, |
| 2343 | minimum / perLoop100, |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2344 | maximum / perLoop100, |
| 2345 | meanCycles / perLoop1k, |
| 2346 | stddevCycles / perLoop1k, |
| 2347 | minCycles / perLoop1k, |
| 2348 | maxCycles / perLoop1k); |
| 2349 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | #endif |
| 2353 | }; |
| 2354 | |
Glenn Kasten | 37d825e | 2012-02-24 07:21:48 -0800 | [diff] [blame] | 2355 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2356 | { |
| 2357 | if (!mMasterMute) { |
| 2358 | char value[PROPERTY_VALUE_MAX]; |
| 2359 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2360 | char *endptr; |
| 2361 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2362 | if (*endptr == '\0' && ul != 0) { |
| 2363 | ALOGD("Silence is golden"); |
| 2364 | // The setprop command will not allow a property to be changed after |
| 2365 | // the first time it is set, so we don't have to worry about un-muting. |
| 2366 | setMasterMute_l(true); |
| 2367 | } |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2372 | bool AudioFlinger::PlaybackThread::threadLoop() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2373 | { |
| 2374 | Vector< sp<Track> > tracksToRemove; |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2375 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2376 | standbyTime = systemTime(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2377 | |
| 2378 | // MIXER |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2379 | nsecs_t lastWarning = 0; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2380 | if (mType == MIXER) { |
| 2381 | longStandbyExit = false; |
| 2382 | } |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2383 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2384 | // DUPLICATING |
| 2385 | // FIXME could this be made local to while loop? |
| 2386 | writeFrames = 0; |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2387 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2388 | cacheParameters_l(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2389 | sleepTime = idleSleepTime; |
| 2390 | |
| 2391 | if (mType == MIXER) { |
| 2392 | sleepTimeShift = 0; |
| 2393 | } |
| 2394 | |
Glenn Kasten | 83efdd0 | 2012-02-24 07:21:32 -0800 | [diff] [blame] | 2395 | CpuStats cpuStats; |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2396 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2397 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2398 | acquireWakeLock(); |
| 2399 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2400 | while (!exitPending()) |
| 2401 | { |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2402 | cpuStats.sample(myName); |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 2403 | |
Glenn Kasten | 73ca0f5 | 2012-02-29 07:56:15 -0800 | [diff] [blame] | 2404 | Vector< sp<EffectChain> > effectChains; |
| 2405 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2406 | processConfigEvents(); |
| 2407 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2408 | { // scope for mLock |
| 2409 | |
| 2410 | Mutex::Autolock _l(mLock); |
| 2411 | |
| 2412 | if (checkForNewParameters_l()) { |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2413 | cacheParameters_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2416 | saveOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2417 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2418 | // put audio hardware into standby after short delay |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2419 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
Glenn Kasten | c455fe9 | 2012-02-29 07:07:30 -0800 | [diff] [blame] | 2420 | mSuspended > 0)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2421 | if (!mStandby) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2422 | |
| 2423 | threadLoop_standby(); |
| 2424 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2425 | mStandby = true; |
| 2426 | mBytesWritten = 0; |
| 2427 | } |
| 2428 | |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2429 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2430 | // we're about to wait, flush the binder command buffer |
| 2431 | IPCThreadState::self()->flushCommands(); |
| 2432 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2433 | clearOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2434 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2435 | if (exitPending()) break; |
| 2436 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2437 | releaseWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2438 | // wait until we have something to do... |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2439 | ALOGV("%s going to sleep", myName.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2440 | mWaitWorkCV.wait(mLock); |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 2441 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2442 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2443 | |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2444 | mMixerStatus = MIXER_IDLE; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2445 | |
Glenn Kasten | 37d825e | 2012-02-24 07:21:48 -0800 | [diff] [blame] | 2446 | checkSilentMode_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2447 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2448 | standbyTime = systemTime() + standbyDelay; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2449 | sleepTime = idleSleepTime; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2450 | if (mType == MIXER) { |
| 2451 | sleepTimeShift = 0; |
| 2452 | } |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2453 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2454 | continue; |
| 2455 | } |
| 2456 | } |
| 2457 | |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2458 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2459 | |
| 2460 | // prevent any changes in effect chain list and in each effect chain |
| 2461 | // during mixing and effect process as the audio buffers could be deleted |
| 2462 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2463 | lockEffectChains_l(effectChains); |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2464 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2465 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2466 | if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2467 | threadLoop_mix(); |
| 2468 | } else { |
| 2469 | threadLoop_sleepTime(); |
| 2470 | } |
| 2471 | |
| 2472 | if (mSuspended > 0) { |
| 2473 | sleepTime = suspendSleepTimeUs(); |
| 2474 | } |
| 2475 | |
| 2476 | // only process effects if we're going to write |
| 2477 | if (sleepTime == 0) { |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2478 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2479 | effectChains[i]->process_l(); |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | // enable changes in effect chain |
| 2484 | unlockEffectChains(effectChains); |
| 2485 | |
| 2486 | // sleepTime == 0 means we must write to audio hardware |
| 2487 | if (sleepTime == 0) { |
| 2488 | |
| 2489 | threadLoop_write(); |
| 2490 | |
| 2491 | if (mType == MIXER) { |
| 2492 | // write blocked detection |
| 2493 | nsecs_t now = systemTime(); |
| 2494 | nsecs_t delta = now - mLastWriteTime; |
| 2495 | if (!mStandby && delta > maxPeriod) { |
| 2496 | mNumDelayedWrites++; |
| 2497 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 2498 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2499 | ns2ms(delta), mNumDelayedWrites, this); |
| 2500 | lastWarning = now; |
| 2501 | } |
| 2502 | // FIXME this is broken: longStandbyExit should be handled out of the if() and with |
| 2503 | // a different threshold. Or completely removed for what it is worth anyway... |
| 2504 | if (mStandby) { |
| 2505 | longStandbyExit = true; |
| 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | mStandby = false; |
| 2511 | } else { |
| 2512 | usleep(sleepTime); |
| 2513 | } |
| 2514 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2515 | // Finally let go of removed track(s), without the lock held |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2516 | // since we can't guarantee the destructors won't acquire that |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2517 | // same lock. This will also mutate and push a new fast mixer state. |
| 2518 | threadLoop_removeTracks(tracksToRemove); |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 2519 | tracksToRemove.clear(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2520 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 2521 | // FIXME I don't understand the need for this here; |
| 2522 | // it was in the original code but maybe the |
| 2523 | // assignment in saveOutputTracks() makes this unnecessary? |
| 2524 | clearOutputTracks(); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2525 | |
| 2526 | // Effect chains will be actually deleted here if they were removed from |
| 2527 | // mEffectChains list during mixing or effects processing |
| 2528 | effectChains.clear(); |
| 2529 | |
| 2530 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 2531 | // is now local to this block, but will keep it for now (at least until merge done). |
| 2532 | } |
| 2533 | |
| 2534 | if (mType == MIXER || mType == DIRECT) { |
| 2535 | // put output stream into standby mode |
| 2536 | if (!mStandby) { |
| 2537 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 2538 | } |
| 2539 | } |
| 2540 | if (mType == DUPLICATING) { |
| 2541 | // for DuplicatingThread, standby mode is handled by the outputTracks |
| 2542 | } |
| 2543 | |
| 2544 | releaseWakeLock(); |
| 2545 | |
| 2546 | ALOGV("Thread %p type %d exiting", this, mType); |
| 2547 | return false; |
| 2548 | } |
| 2549 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 2550 | // returns (via tracksToRemove) a set of tracks to remove. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2551 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 2552 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2553 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 2554 | } |
| 2555 | |
| 2556 | void AudioFlinger::MixerThread::threadLoop_write() |
| 2557 | { |
| 2558 | // FIXME we should only do one push per cycle; confirm this is true |
| 2559 | // Start the fast mixer if it's not already running |
| 2560 | if (mFastMixer != NULL) { |
| 2561 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2562 | FastMixerState *state = sq->begin(); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2563 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 2564 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2565 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2566 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2567 | if (old == -1) { |
| 2568 | __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
| 2569 | } |
| 2570 | } |
| 2571 | state->mCommand = FastMixerState::MIX_WRITE; |
| 2572 | sq->end(); |
| 2573 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2574 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2575 | mNormalSink = mPipeSink; |
| 2576 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2577 | } else { |
| 2578 | sq->end(false /*didModify*/); |
| 2579 | } |
| 2580 | } |
| 2581 | PlaybackThread::threadLoop_write(); |
| 2582 | } |
| 2583 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2584 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 2585 | void AudioFlinger::PlaybackThread::threadLoop_write() |
| 2586 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2587 | // FIXME rewrite to reduce number of system calls |
| 2588 | mLastWriteTime = systemTime(); |
| 2589 | mInWrite = true; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2590 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2591 | #define mBitShift 2 // FIXME |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2592 | size_t count = mixBufferSize >> mBitShift; |
| 2593 | ssize_t framesWritten = mNormalSink->write(mMixBuffer, count); |
| 2594 | if (framesWritten > 0) { |
| 2595 | size_t bytesWritten = framesWritten << mBitShift; |
| 2596 | mBytesWritten += bytesWritten; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2599 | mNumWrites++; |
| 2600 | mInWrite = false; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2601 | } |
| 2602 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2603 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 2604 | { |
| 2605 | // Idle the fast mixer if it's currently running |
| 2606 | if (mFastMixer != NULL) { |
| 2607 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2608 | FastMixerState *state = sq->begin(); |
| 2609 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 2610 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2611 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2612 | state->mColdGen++; |
| 2613 | mFastMixerFutex = 0; |
| 2614 | sq->end(); |
| 2615 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 2616 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
Glenn Kasten | 300a2ee | 2012-04-25 13:47:36 -0700 | [diff] [blame] | 2617 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2618 | mNormalSink = mOutputSink; |
| 2619 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2620 | } else { |
| 2621 | sq->end(false /*didModify*/); |
| 2622 | } |
| 2623 | } |
| 2624 | PlaybackThread::threadLoop_standby(); |
| 2625 | } |
| 2626 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2627 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 2628 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 2629 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2630 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %u", this, mSuspended); |
| 2631 | mOutput->stream->common.standby(&mOutput->stream->common); |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 2635 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2636 | // obtain the presentation timestamp of the next output buffer |
| 2637 | int64_t pts; |
| 2638 | status_t status = INVALID_OPERATION; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2639 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2640 | if (NULL != mOutput->stream->get_next_write_timestamp) { |
| 2641 | status = mOutput->stream->get_next_write_timestamp( |
| 2642 | mOutput->stream, &pts); |
| 2643 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2644 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2645 | if (status != NO_ERROR) { |
| 2646 | pts = AudioBufferProvider::kInvalidPTS; |
| 2647 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2648 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2649 | // mix buffers... |
| 2650 | mAudioMixer->process(pts); |
| 2651 | // increase sleep time progressively when application underrun condition clears. |
| 2652 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 2653 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 2654 | // such that we would underrun the audio HAL. |
| 2655 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
| 2656 | sleepTimeShift--; |
| 2657 | } |
| 2658 | sleepTime = 0; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 2659 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2660 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 2664 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2665 | // If no tracks are ready, sleep once for the duration of an output |
| 2666 | // buffer size, then write 0s to the output |
| 2667 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2668 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2669 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 2670 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 2671 | sleepTime = kMinThreadSleepTimeUs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2672 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2673 | // reduce sleep time in case of consecutive application underruns to avoid |
| 2674 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 2675 | // duration we would end up writing less data than needed by the audio HAL if |
| 2676 | // the condition persists. |
| 2677 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2678 | sleepTimeShift++; |
| 2679 | } |
| 2680 | } else { |
| 2681 | sleepTime = idleSleepTime; |
| 2682 | } |
| 2683 | } else if (mBytesWritten != 0 || |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2684 | (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2685 | memset (mMixBuffer, 0, mixBufferSize); |
| 2686 | sleepTime = 0; |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 2687 | ALOGV_IF((mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 2688 | } |
| 2689 | // TODO add standby time extension fct of effect tail |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | // prepareTracks_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2693 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2694 | Vector< sp<Track> > *tracksToRemove) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2695 | { |
| 2696 | |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2697 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2698 | // find out which tracks need to be processed |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2699 | size_t count = mActiveTracks.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2700 | size_t mixedTracks = 0; |
| 2701 | size_t tracksWithEffect = 0; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 2702 | // counts only _active_ fast tracks |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2703 | size_t fastTracks = 0; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 2704 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2705 | |
| 2706 | float masterVolume = mMasterVolume; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2707 | bool masterMute = mMasterMute; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2708 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2709 | if (masterMute) { |
| 2710 | masterVolume = 0; |
| 2711 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2712 | // Delegate master volume control to effect in output mix effect chain if needed |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2713 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2714 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2715 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2716 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2717 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2718 | chain.clear(); |
| 2719 | } |
| 2720 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 2721 | // prepare a new state to push |
| 2722 | FastMixerStateQueue *sq = NULL; |
| 2723 | FastMixerState *state = NULL; |
| 2724 | bool didModify = false; |
| 2725 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
| 2726 | if (mFastMixer != NULL) { |
| 2727 | sq = mFastMixer->sq(); |
| 2728 | state = sq->begin(); |
| 2729 | } |
| 2730 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2731 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 3e07470 | 2012-02-28 18:40:35 -0800 | [diff] [blame] | 2732 | sp<Track> t = mActiveTracks[i].promote(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2733 | if (t == 0) continue; |
| 2734 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2735 | // this const just means the local variable doesn't change |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2736 | Track* const track = t.get(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2737 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 2738 | // process fast tracks |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2739 | if (track->isFastTrack()) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 2740 | |
| 2741 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 2742 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 2743 | // the track never becomes active so it's fast mixer slot is never touched. |
| 2744 | // The converse, of removing an (active) track and then creating a new track |
| 2745 | // at the identical fast mixer slot within the same normal mix cycle, |
| 2746 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 2747 | int j = track->mFastIndex; |
| 2748 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 2749 | |
| 2750 | // Determine whether the track is currently in underrun condition, |
| 2751 | // and whether it had a recent underrun. |
| 2752 | uint32_t underruns = mFastMixerDumpState.mTracks[j].mUnderruns; |
| 2753 | uint32_t recentUnderruns = (underruns - (track->mObservedUnderruns & ~1)) >> 1; |
| 2754 | // don't count underruns that occur while stopping or pausing |
| 2755 | if (!(track->isStopped() || track->isPausing())) { |
| 2756 | track->mUnderrunCount += recentUnderruns; |
| 2757 | } |
| 2758 | track->mObservedUnderruns = underruns; |
| 2759 | |
| 2760 | // This is similar to the formula for normal tracks, |
| 2761 | // with a few modifications for fast tracks. |
| 2762 | bool isActive; |
| 2763 | if (track->isStopped()) { |
| 2764 | // track stays active after stop() until first underrun |
| 2765 | isActive = recentUnderruns == 0; |
| 2766 | } else if (track->isPaused() || track->isTerminated()) { |
| 2767 | isActive = false; |
| 2768 | } else if (track->isPausing()) { |
| 2769 | // ramp down is not yet implemented |
| 2770 | isActive = true; |
| 2771 | track->setPaused(); |
| 2772 | } else if (track->isResuming()) { |
| 2773 | // ramp up is not yet implemented |
| 2774 | isActive = true; |
| 2775 | track->mState = TrackBase::ACTIVE; |
| 2776 | } else { |
| 2777 | // no minimum frame count for fast tracks; continual underrun is allowed, |
| 2778 | // but later could implement automatic pause after several consecutive underruns, |
| 2779 | // or auto-mute yet still consider the track active and continue to service it |
| 2780 | isActive = true; |
| 2781 | } |
| 2782 | |
| 2783 | if (isActive) { |
| 2784 | // was it previously inactive? |
| 2785 | if (!(state->mTrackMask & (1 << j))) { |
| 2786 | ExtendedAudioBufferProvider *eabp = track; |
| 2787 | VolumeProvider *vp = track; |
| 2788 | fastTrack->mBufferProvider = eabp; |
| 2789 | fastTrack->mVolumeProvider = vp; |
| 2790 | fastTrack->mSampleRate = track->mSampleRate; |
| 2791 | fastTrack->mChannelMask = track->mChannelMask; |
| 2792 | fastTrack->mGeneration++; |
| 2793 | state->mTrackMask |= 1 << j; |
| 2794 | didModify = true; |
| 2795 | // no acknowledgement required for newly active tracks |
| 2796 | } |
| 2797 | // cache the combined master volume and stream type volume for fast mixer; this |
| 2798 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
| 2799 | track->mCachedVolume = track->isMuted() ? |
| 2800 | 0 : masterVolume * mStreamTypes[track->streamType()].volume; |
| 2801 | ++fastTracks; |
| 2802 | } else { |
| 2803 | // was it previously active? |
| 2804 | if (state->mTrackMask & (1 << j)) { |
| 2805 | fastTrack->mBufferProvider = NULL; |
| 2806 | fastTrack->mGeneration++; |
| 2807 | state->mTrackMask &= ~(1 << j); |
| 2808 | didModify = true; |
| 2809 | // If any fast tracks were removed, we must wait for acknowledgement |
| 2810 | // because we're about to decrement the last sp<> on those tracks. |
| 2811 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 2812 | } |
| 2813 | // Remainder of this block is copied from similar code for normal tracks |
| 2814 | if (track->isStopped()) { |
| 2815 | // Can't reset directly, as fast mixer is still polling this track |
| 2816 | // track->reset(); |
| 2817 | // So instead mark this track as needing to be reset after push with ack |
| 2818 | resetMask |= 1 << i; |
| 2819 | } |
| 2820 | // This would be incomplete if we auto-paused on underrun |
| 2821 | size_t audioHALFrames = |
| 2822 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 2823 | size_t framesWritten = |
| 2824 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 2825 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
| 2826 | tracksToRemove->add(track); |
| 2827 | } |
| 2828 | // Avoids a misleading display in dumpsys |
| 2829 | track->mObservedUnderruns &= ~1; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2830 | } |
| 2831 | continue; |
| 2832 | } |
| 2833 | |
| 2834 | { // local variable scope to avoid goto warning |
| 2835 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2836 | audio_track_cblk_t* cblk = track->cblk(); |
| 2837 | |
| 2838 | // The first time a track is added we wait |
| 2839 | // for all its buffers to be filled before processing it |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2840 | int name = track->name(); |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2841 | // make sure that we have enough frames to mix one full buffer. |
| 2842 | // enforce this condition only once to enable draining the buffer in case the client |
| 2843 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2844 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2845 | // during last round |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2846 | uint32_t minFrames = 1; |
Eric Laurent | 83faee0 | 2012-04-27 18:24:29 -0700 | [diff] [blame] | 2847 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2848 | (mMixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2849 | if (t->sampleRate() == (int)mSampleRate) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2850 | minFrames = mNormalFrameCount; |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2851 | } else { |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2852 | // +1 for rounding and +1 for additional sample needed for interpolation |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 2853 | minFrames = (mNormalFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2854 | // add frames already consumed but not yet released by the resampler |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2855 | // because cblk->framesReady() will include these frames |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2856 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 2857 | // the minimum track buffer size is normally twice the number of frames necessary |
| 2858 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 2859 | // of unreleased frames after each pass, but just in case... |
Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2860 | ALOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2861 | } |
| 2862 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2863 | if ((track->framesReady() >= minFrames) && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2864 | !track->isPaused() && !track->isTerminated()) |
| 2865 | { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2866 | //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2867 | |
| 2868 | mixedTracks++; |
| 2869 | |
| 2870 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2871 | // connected to the track |
| 2872 | chain.clear(); |
| 2873 | if (track->mainBuffer() != mMixBuffer) { |
| 2874 | chain = getEffectChain_l(track->sessionId()); |
| 2875 | // Delegate volume control to effect in track effect chain if needed |
| 2876 | if (chain != 0) { |
| 2877 | tracksWithEffect++; |
| 2878 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2879 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d", |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2880 | name, track->sessionId()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | |
| 2885 | int param = AudioMixer::VOLUME; |
| 2886 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2887 | // no ramp for the first volume setting |
| 2888 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2889 | if (track->mState == TrackBase::RESUMING) { |
| 2890 | track->mState = TrackBase::ACTIVE; |
| 2891 | param = AudioMixer::RAMP_VOLUME; |
| 2892 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2893 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2894 | } else if (cblk->server != 0) { |
| 2895 | // If the track is stopped before the first frame was mixed, |
| 2896 | // do not apply ramp |
| 2897 | param = AudioMixer::RAMP_VOLUME; |
| 2898 | } |
| 2899 | |
| 2900 | // compute volume for this track |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2901 | uint32_t vl, vr, va; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2902 | if (track->isMuted() || track->isPausing() || |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2903 | mStreamTypes[track->streamType()].mute) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2904 | vl = vr = va = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2905 | if (track->isPausing()) { |
| 2906 | track->setPaused(); |
| 2907 | } |
| 2908 | } else { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2909 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2910 | // read original volumes with volume control |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2911 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2912 | float v = masterVolume * typeVolume; |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 2913 | uint32_t vlr = cblk->getVolumeLR(); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2914 | vl = vlr & 0xFFFF; |
| 2915 | vr = vlr >> 16; |
| 2916 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 2917 | if (vl > MAX_GAIN_INT) { |
| 2918 | ALOGV("Track left volume out of range: %04X", vl); |
| 2919 | vl = MAX_GAIN_INT; |
| 2920 | } |
| 2921 | if (vr > MAX_GAIN_INT) { |
| 2922 | ALOGV("Track right volume out of range: %04X", vr); |
| 2923 | vr = MAX_GAIN_INT; |
| 2924 | } |
| 2925 | // now apply the master volume and stream type volume |
| 2926 | vl = (uint32_t)(v * vl) << 12; |
| 2927 | vr = (uint32_t)(v * vr) << 12; |
| 2928 | // assuming master volume and stream type volume each go up to 1.0, |
| 2929 | // vl and vr are now in 8.24 format |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2930 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2931 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); |
| 2932 | // send level comes from shared memory and so may be corrupt |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2933 | if (sendLevel > MAX_GAIN_INT) { |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2934 | ALOGV("Track send level out of range: %04X", sendLevel); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2935 | sendLevel = MAX_GAIN_INT; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2936 | } |
| 2937 | va = (uint32_t)(v * sendLevel); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2938 | } |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2939 | // Delegate volume control to effect in track effect chain if needed |
| 2940 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2941 | // Do not ramp volume if volume is controlled by effect |
| 2942 | param = AudioMixer::VOLUME; |
| 2943 | track->mHasVolumeController = true; |
| 2944 | } else { |
| 2945 | // force no volume ramp when volume controller was just disabled or removed |
| 2946 | // from effect chain to avoid volume spike |
| 2947 | if (track->mHasVolumeController) { |
| 2948 | param = AudioMixer::VOLUME; |
| 2949 | } |
| 2950 | track->mHasVolumeController = false; |
| 2951 | } |
| 2952 | |
| 2953 | // Convert volumes from 8.24 to 4.12 format |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2954 | // This additional clamping is needed in case chain->setVolume_l() overshot |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2955 | vl = (vl + (1 << 11)) >> 12; |
| 2956 | if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT; |
| 2957 | vr = (vr + (1 << 11)) >> 12; |
| 2958 | if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT; |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2959 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2960 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; // va is uint32_t, so no need to check for - |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2961 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2962 | // XXX: these things DON'T need to be done each time |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2963 | mAudioMixer->setBufferProvider(name, track); |
| 2964 | mAudioMixer->enable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2965 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2966 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl); |
| 2967 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr); |
| 2968 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2969 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2970 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2971 | AudioMixer::TRACK, |
| 2972 | AudioMixer::FORMAT, (void *)track->format()); |
| 2973 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2974 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2975 | AudioMixer::TRACK, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2976 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2977 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2978 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2979 | AudioMixer::RESAMPLE, |
| 2980 | AudioMixer::SAMPLE_RATE, |
| 2981 | (void *)(cblk->sampleRate)); |
| 2982 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2983 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2984 | AudioMixer::TRACK, |
| 2985 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2986 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2987 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2988 | AudioMixer::TRACK, |
| 2989 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 2990 | |
| 2991 | // reset retry count |
| 2992 | track->mRetryCount = kMaxTrackRetries; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 2993 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2994 | // If one track is ready, set the mixer ready if: |
| 2995 | // - the mixer was not ready during previous round OR |
| 2996 | // - no other track is not ready |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 2997 | if (mMixerStatus != MIXER_TRACKS_READY || |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2998 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 2999 | mixerStatus = MIXER_TRACKS_READY; |
| 3000 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3001 | } else { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3002 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3003 | if (track->isStopped()) { |
| 3004 | track->reset(); |
| 3005 | } |
Eric Laurent | 83faee0 | 2012-04-27 18:24:29 -0700 | [diff] [blame] | 3006 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 3007 | track->isStopped() || track->isPaused()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3008 | // We have consumed all the buffers of this track. |
| 3009 | // Remove it from the list of active tracks. |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3010 | // TODO: use actual buffer filling status instead of latency when available from |
| 3011 | // audio HAL |
| 3012 | size_t audioHALFrames = |
| 3013 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3014 | size_t framesWritten = |
| 3015 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 3016 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3017 | tracksToRemove->add(track); |
| 3018 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3019 | } else { |
| 3020 | // No buffers for this track. Give it a few chances to |
| 3021 | // fill a buffer, then remove it from active list. |
| 3022 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3023 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3024 | tracksToRemove->add(track); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 3025 | // indicate to client process that the track was disabled because of underrun; |
| 3026 | // it will then automatically call start() when data is available |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3027 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3028 | // If one track is not ready, mark the mixer also not ready if: |
| 3029 | // - the mixer was ready during previous round OR |
| 3030 | // - no other track is ready |
Eric Laurent | da74744 | 2012-04-25 18:53:13 -0700 | [diff] [blame] | 3031 | } else if (mMixerStatus == MIXER_TRACKS_READY || |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3032 | mixerStatus != MIXER_TRACKS_READY) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3033 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 3034 | } |
| 3035 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 3036 | mAudioMixer->disable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3037 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3038 | |
| 3039 | } // local variable scope to avoid goto warning |
| 3040 | track_is_ready: ; |
| 3041 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 3044 | // Push the new FastMixer state if necessary |
| 3045 | if (didModify) { |
| 3046 | state->mFastTracksGen++; |
| 3047 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 3048 | if (kUseFastMixer == FastMixer_Dynamic && |
| 3049 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 3050 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3051 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3052 | state->mColdGen++; |
| 3053 | mFastMixerFutex = 0; |
| 3054 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3055 | mNormalSink = mOutputSink; |
| 3056 | } |
| 3057 | // If we go into cold idle, need to wait for acknowledgement |
| 3058 | // so that fast mixer stops doing I/O. |
| 3059 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3060 | } |
| 3061 | sq->end(); |
| 3062 | } |
| 3063 | if (sq != NULL) { |
| 3064 | sq->end(didModify); |
| 3065 | sq->push(block); |
| 3066 | } |
| 3067 | |
| 3068 | // Now perform the deferred reset on fast tracks that have stopped |
| 3069 | while (resetMask != 0) { |
| 3070 | size_t i = __builtin_ctz(resetMask); |
| 3071 | ALOG_ASSERT(i < count); |
| 3072 | resetMask &= ~(1 << i); |
| 3073 | sp<Track> t = mActiveTracks[i].promote(); |
| 3074 | if (t == 0) continue; |
| 3075 | Track* track = t.get(); |
| 3076 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 3077 | track->reset(); |
| 3078 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3079 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3080 | // remove all the tracks that need to be... |
| 3081 | count = tracksToRemove->size(); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3082 | if (CC_UNLIKELY(count)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3083 | for (size_t i=0 ; i<count ; i++) { |
| 3084 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 3085 | mActiveTracks.remove(track); |
| 3086 | if (track->mainBuffer() != mMixBuffer) { |
| 3087 | chain = getEffectChain_l(track->sessionId()); |
| 3088 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3089 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3090 | chain->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | if (track->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3094 | removeTrack_l(track); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | // mix buffer must be cleared if all tracks are connected to an |
| 3100 | // effect chain as in this case the mixer will not write to |
| 3101 | // mix buffer and track effects will accumulate into it |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3102 | if ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || (mixedTracks == 0 && fastTracks > 0)) { |
| 3103 | // FIXME as a performance optimization, should remember previous zero status |
| 3104 | memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3107 | // if any fast tracks, then status is ready |
| 3108 | if (fastTracks > 0) { |
| 3109 | mixerStatus = MIXER_TRACKS_READY; |
| 3110 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3111 | return mixerStatus; |
| 3112 | } |
| 3113 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3114 | /* |
| 3115 | The derived values that are cached: |
| 3116 | - mixBufferSize from frame count * frame size |
| 3117 | - activeSleepTime from activeSleepTimeUs() |
| 3118 | - idleSleepTime from idleSleepTimeUs() |
| 3119 | - standbyDelay from mActiveSleepTimeUs (DIRECT only) |
| 3120 | - maxPeriod from frame count and sample rate (MIXER only) |
| 3121 | |
| 3122 | The parameters that affect these derived values are: |
| 3123 | - frame count |
| 3124 | - frame size |
| 3125 | - sample rate |
| 3126 | - device type: A2DP or not |
| 3127 | - device latency |
| 3128 | - format: PCM or not |
| 3129 | - active sleep time |
| 3130 | - idle sleep time |
| 3131 | */ |
| 3132 | |
| 3133 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 3134 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3135 | mixBufferSize = mNormalFrameCount * mFrameSize; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3136 | activeSleepTime = activeSleepTimeUs(); |
| 3137 | idleSleepTime = idleSleepTimeUs(); |
| 3138 | } |
| 3139 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3140 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3141 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3142 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3143 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3144 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3145 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3146 | size_t size = mTracks.size(); |
| 3147 | for (size_t i = 0; i < size; i++) { |
| 3148 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3149 | if (t->streamType() == streamType) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3150 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3151 | t->mCblk->cv.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3156 | // getTrackName_l() must be called with ThreadBase::mLock held |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3157 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3158 | { |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 3159 | return mAudioMixer->getTrackName(channelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3163 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 3164 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3165 | ALOGV("remove track (%d) and delete from mixer", name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3166 | mAudioMixer->deleteTrackName(name); |
| 3167 | } |
| 3168 | |
| 3169 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 3170 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 3171 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3172 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3173 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3174 | bool reconfig = false; |
| 3175 | |
| 3176 | while (!mNewParameters.isEmpty()) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3177 | |
| 3178 | if (mFastMixer != NULL) { |
| 3179 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3180 | FastMixerState *state = sq->begin(); |
| 3181 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3182 | previousCommand = state->mCommand; |
| 3183 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3184 | sq->end(); |
| 3185 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3186 | } else { |
| 3187 | sq->end(false /*didModify*/); |
| 3188 | } |
| 3189 | } |
| 3190 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3191 | status_t status = NO_ERROR; |
| 3192 | String8 keyValuePair = mNewParameters[0]; |
| 3193 | AudioParameter param = AudioParameter(keyValuePair); |
| 3194 | int value; |
| 3195 | |
| 3196 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 3197 | reconfig = true; |
| 3198 | } |
| 3199 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3200 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3201 | status = BAD_VALUE; |
| 3202 | } else { |
| 3203 | reconfig = true; |
| 3204 | } |
| 3205 | } |
| 3206 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3207 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3208 | status = BAD_VALUE; |
| 3209 | } else { |
| 3210 | reconfig = true; |
| 3211 | } |
| 3212 | } |
| 3213 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3214 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 3215 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3216 | // if frame count is changed after track creation |
| 3217 | if (!mTracks.isEmpty()) { |
| 3218 | status = INVALID_OPERATION; |
| 3219 | } else { |
| 3220 | reconfig = true; |
| 3221 | } |
| 3222 | } |
| 3223 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 3224 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3225 | // when changing the audio output device, call addBatteryData to notify |
| 3226 | // the change |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 3227 | if ((int)mDevice != value) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3228 | uint32_t params = 0; |
| 3229 | // check whether speaker is on |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3230 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3231 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 3232 | } |
| 3233 | |
| 3234 | int deviceWithoutSpeaker |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3235 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3236 | // check if any other device (except speaker) is on |
| 3237 | if (value & deviceWithoutSpeaker ) { |
| 3238 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3239 | } |
| 3240 | |
| 3241 | if (params != 0) { |
| 3242 | addBatteryData(params); |
| 3243 | } |
| 3244 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 3245 | #endif |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3246 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3247 | // forward device change to effects that have requested to be |
| 3248 | // aware of attached audio device. |
| 3249 | mDevice = (uint32_t)value; |
| 3250 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 3251 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3256 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3257 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3258 | if (!mStandby && status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3259 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3260 | mStandby = true; |
| 3261 | mBytesWritten = 0; |
| 3262 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3263 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3264 | } |
| 3265 | if (status == NO_ERROR && reconfig) { |
| 3266 | delete mAudioMixer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 3267 | // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't) |
| 3268 | mAudioMixer = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3269 | readOutputParameters(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3270 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3271 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3272 | int name = getTrackName_l((audio_channel_mask_t)mTracks[i]->mChannelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3273 | if (name < 0) break; |
| 3274 | mTracks[i]->mName = name; |
| 3275 | // limit track sample rate to 2 x new output sample rate |
| 3276 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 3277 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 3278 | } |
| 3279 | } |
| 3280 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | mNewParameters.removeAt(0); |
| 3285 | |
| 3286 | mParamStatus = status; |
| 3287 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3288 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3289 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 3290 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3291 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3292 | |
| 3293 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3294 | ALOG_ASSERT(mFastMixer != NULL); |
| 3295 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3296 | FastMixerState *state = sq->begin(); |
| 3297 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3298 | state->mCommand = previousCommand; |
| 3299 | sq->end(); |
| 3300 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3301 | } |
| 3302 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3303 | return reconfig; |
| 3304 | } |
| 3305 | |
| 3306 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 3307 | { |
| 3308 | const size_t SIZE = 256; |
| 3309 | char buffer[SIZE]; |
| 3310 | String8 result; |
| 3311 | |
| 3312 | PlaybackThread::dumpInternals(fd, args); |
| 3313 | |
| 3314 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 3315 | result.append(buffer); |
| 3316 | write(fd, result.string(), result.size()); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3317 | |
| 3318 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 3319 | FastMixerDumpState copy = mFastMixerDumpState; |
| 3320 | copy.dump(fd); |
| 3321 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3322 | return NO_ERROR; |
| 3323 | } |
| 3324 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3325 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3326 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3327 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3328 | } |
| 3329 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3330 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3331 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3332 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3333 | } |
| 3334 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3335 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 3336 | { |
| 3337 | PlaybackThread::cacheParameters_l(); |
| 3338 | |
| 3339 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 3340 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 3341 | // increase threshold again due to low power audio mode. The way this warning |
| 3342 | // threshold is calculated and its usefulness should be reconsidered anyway. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3343 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3344 | } |
| 3345 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3346 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3347 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3348 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3349 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3350 | // mLeftVolFloat, mRightVolFloat |
| 3351 | // mLeftVolShort, mRightVolShort |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3352 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 3356 | { |
| 3357 | } |
| 3358 | |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3359 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 3360 | Vector< sp<Track> > *tracksToRemove |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3361 | ) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3362 | { |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3363 | sp<Track> trackToRemove; |
| 3364 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3365 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3366 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3367 | // find out which tracks need to be processed |
| 3368 | if (mActiveTracks.size() != 0) { |
| 3369 | sp<Track> t = mActiveTracks[0].promote(); |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3370 | // The track died recently |
| 3371 | if (t == 0) return MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3372 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3373 | Track* const track = t.get(); |
| 3374 | audio_track_cblk_t* cblk = track->cblk(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3375 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3376 | // The first time a track is added we wait |
| 3377 | // for all its buffers to be filled before processing it |
| 3378 | if (cblk->framesReady() && track->isReady() && |
| 3379 | !track->isPaused() && !track->isTerminated()) |
| 3380 | { |
| 3381 | //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3382 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3383 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3384 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3385 | mLeftVolFloat = mRightVolFloat = 0; |
| 3386 | mLeftVolShort = mRightVolShort = 0; |
| 3387 | if (track->mState == TrackBase::RESUMING) { |
| 3388 | track->mState = TrackBase::ACTIVE; |
| 3389 | rampVolume = true; |
| 3390 | } |
| 3391 | } else if (cblk->server != 0) { |
| 3392 | // If the track is stopped before the first frame was mixed, |
| 3393 | // do not apply ramp |
| 3394 | rampVolume = true; |
| 3395 | } |
| 3396 | // compute volume for this track |
| 3397 | float left, right; |
| 3398 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 3399 | mStreamTypes[track->streamType()].mute) { |
| 3400 | left = right = 0; |
| 3401 | if (track->isPausing()) { |
| 3402 | track->setPaused(); |
| 3403 | } |
| 3404 | } else { |
| 3405 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3406 | float v = mMasterVolume * typeVolume; |
| 3407 | uint32_t vlr = cblk->getVolumeLR(); |
| 3408 | float v_clamped = v * (vlr & 0xFFFF); |
| 3409 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 3410 | left = v_clamped/MAX_GAIN; |
| 3411 | v_clamped = v * (vlr >> 16); |
| 3412 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 3413 | right = v_clamped/MAX_GAIN; |
| 3414 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3415 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3416 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 3417 | mLeftVolFloat = left; |
| 3418 | mRightVolFloat = right; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3419 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3420 | // If audio HAL implements volume control, |
| 3421 | // force software volume to nominal value |
| 3422 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
| 3423 | left = 1.0f; |
| 3424 | right = 1.0f; |
| 3425 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3426 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3427 | // Convert volumes from float to 8.24 |
| 3428 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 3429 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3430 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3431 | // Delegate volume control to effect in track effect chain if needed |
| 3432 | // only one effect chain can be present on DirectOutputThread, so if |
| 3433 | // there is one, the track is connected to it |
| 3434 | if (!mEffectChains.isEmpty()) { |
| 3435 | // Do not ramp volume if volume is controlled by effect |
| 3436 | if (mEffectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3437 | rampVolume = false; |
| 3438 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3439 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3440 | |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3441 | // Convert volumes from 8.24 to 4.12 format |
| 3442 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 3443 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 3444 | leftVol = (uint16_t)v_clamped; |
| 3445 | v_clamped = (vr + (1 << 11)) >> 12; |
| 3446 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 3447 | rightVol = (uint16_t)v_clamped; |
| 3448 | } else { |
| 3449 | leftVol = mLeftVolShort; |
| 3450 | rightVol = mRightVolShort; |
| 3451 | rampVolume = false; |
| 3452 | } |
| 3453 | |
| 3454 | // reset retry count |
| 3455 | track->mRetryCount = kMaxTrackRetriesDirect; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3456 | mActiveTrack = t; |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3457 | mixerStatus = MIXER_TRACKS_READY; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3458 | } else { |
| 3459 | //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
| 3460 | if (track->isStopped()) { |
| 3461 | track->reset(); |
| 3462 | } |
| 3463 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 3464 | // We have consumed all the buffers of this track. |
| 3465 | // Remove it from the list of active tracks. |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3466 | // TODO: implement behavior for compressed audio |
| 3467 | size_t audioHALFrames = |
| 3468 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3469 | size_t framesWritten = |
| 3470 | mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
| 3471 | if (track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3472 | trackToRemove = track; |
| 3473 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3474 | } else { |
| 3475 | // No buffers for this track. Give it a few chances to |
| 3476 | // fill a buffer, then remove it from active list. |
| 3477 | if (--(track->mRetryCount) <= 0) { |
| 3478 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
| 3479 | trackToRemove = track; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3480 | } else { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3481 | mixerStatus = MIXER_TRACKS_ENABLED; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3482 | } |
| 3483 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3484 | } |
| 3485 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3486 | |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3487 | // FIXME merge this with similar code for removing multiple tracks |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3488 | // remove all the tracks that need to be... |
| 3489 | if (CC_UNLIKELY(trackToRemove != 0)) { |
Glenn Kasten | 1465f0c | 2012-03-06 11:23:32 -0800 | [diff] [blame] | 3490 | tracksToRemove->add(trackToRemove); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3491 | mActiveTracks.remove(trackToRemove); |
| 3492 | if (!mEffectChains.isEmpty()) { |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 3493 | ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(), |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3494 | trackToRemove->sessionId()); |
| 3495 | mEffectChains[0]->decActiveTrackCnt(); |
| 3496 | } |
| 3497 | if (trackToRemove->isTerminated()) { |
| 3498 | removeTrack_l(trackToRemove); |
| 3499 | } |
| 3500 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3501 | |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3502 | return mixerStatus; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3503 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3504 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3505 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 3506 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3507 | AudioBufferProvider::Buffer buffer; |
| 3508 | size_t frameCount = mFrameCount; |
| 3509 | int8_t *curBuf = (int8_t *)mMixBuffer; |
| 3510 | // output audio to hardware |
| 3511 | while (frameCount) { |
| 3512 | buffer.frameCount = frameCount; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3513 | mActiveTrack->getNextBuffer(&buffer); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3514 | if (CC_UNLIKELY(buffer.raw == NULL)) { |
| 3515 | memset(curBuf, 0, frameCount * mFrameSize); |
| 3516 | break; |
| 3517 | } |
| 3518 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 3519 | frameCount -= buffer.frameCount; |
| 3520 | curBuf += buffer.frameCount * mFrameSize; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3521 | mActiveTrack->releaseBuffer(&buffer); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3522 | } |
| 3523 | sleepTime = 0; |
| 3524 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | b071e9b | 2012-03-07 17:05:59 -0800 | [diff] [blame] | 3525 | mActiveTrack.clear(); |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3526 | |
| 3527 | // apply volume |
| 3528 | |
| 3529 | // Do not apply volume on compressed audio |
| 3530 | if (!audio_is_linear_pcm(mFormat)) { |
| 3531 | return; |
| 3532 | } |
| 3533 | |
| 3534 | // convert to signed 16 bit before volume calculation |
| 3535 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
| 3536 | size_t count = mFrameCount * mChannelCount; |
| 3537 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 3538 | int16_t *dst = mMixBuffer + count-1; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3539 | while (count--) { |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3540 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | frameCount = mFrameCount; |
| 3545 | int16_t *out = mMixBuffer; |
| 3546 | if (rampVolume) { |
| 3547 | if (mChannelCount == 1) { |
| 3548 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 3549 | int32_t vlInc = d / (int32_t)frameCount; |
| 3550 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 3551 | do { |
| 3552 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 3553 | out++; |
| 3554 | vl += vlInc; |
| 3555 | } while (--frameCount); |
| 3556 | |
| 3557 | } else { |
| 3558 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 3559 | int32_t vlInc = d / (int32_t)frameCount; |
| 3560 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 3561 | int32_t vrInc = d / (int32_t)frameCount; |
| 3562 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 3563 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 3564 | do { |
| 3565 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 3566 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 3567 | out += 2; |
| 3568 | vl += vlInc; |
| 3569 | vr += vrInc; |
| 3570 | } while (--frameCount); |
| 3571 | } |
| 3572 | } else { |
| 3573 | if (mChannelCount == 1) { |
| 3574 | do { |
| 3575 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 3576 | out++; |
| 3577 | } while (--frameCount); |
| 3578 | } else { |
| 3579 | do { |
| 3580 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 3581 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 3582 | out += 2; |
| 3583 | } while (--frameCount); |
| 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | // convert back to unsigned 8 bit after volume calculation |
| 3588 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
| 3589 | size_t count = mFrameCount * mChannelCount; |
| 3590 | int16_t *src = mMixBuffer; |
| 3591 | uint8_t *dst = (uint8_t *)mMixBuffer; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3592 | while (count--) { |
Glenn Kasten | 73f4bc3 | 2012-03-09 12:08:48 -0800 | [diff] [blame] | 3593 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | mLeftVolShort = leftVol; |
| 3598 | mRightVolShort = rightVol; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 3602 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3603 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3604 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3605 | sleepTime = activeSleepTime; |
| 3606 | } else { |
| 3607 | sleepTime = idleSleepTime; |
| 3608 | } |
| 3609 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3610 | memset(mMixBuffer, 0, mFrameCount * mFrameSize); |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3611 | sleepTime = 0; |
| 3612 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | // getTrackName_l() must be called with ThreadBase::mLock held |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 3616 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3617 | { |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
| 3621 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3622 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 3623 | { |
| 3624 | } |
| 3625 | |
| 3626 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 3627 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 3628 | { |
| 3629 | bool reconfig = false; |
| 3630 | |
| 3631 | while (!mNewParameters.isEmpty()) { |
| 3632 | status_t status = NO_ERROR; |
| 3633 | String8 keyValuePair = mNewParameters[0]; |
| 3634 | AudioParameter param = AudioParameter(keyValuePair); |
| 3635 | int value; |
| 3636 | |
| 3637 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3638 | // do not accept frame count changes if tracks are open as the track buffer |
| 3639 | // size depends on frame count and correct behavior would not be garantied |
| 3640 | // if frame count is changed after track creation |
| 3641 | if (!mTracks.isEmpty()) { |
| 3642 | status = INVALID_OPERATION; |
| 3643 | } else { |
| 3644 | reconfig = true; |
| 3645 | } |
| 3646 | } |
| 3647 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3648 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3649 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3650 | if (!mStandby && status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3651 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3652 | mStandby = true; |
| 3653 | mBytesWritten = 0; |
| 3654 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3655 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3656 | } |
| 3657 | if (status == NO_ERROR && reconfig) { |
| 3658 | readOutputParameters(); |
| 3659 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | mNewParameters.removeAt(0); |
| 3664 | |
| 3665 | mParamStatus = status; |
| 3666 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3667 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3668 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 3669 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3670 | } |
| 3671 | return reconfig; |
| 3672 | } |
| 3673 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3674 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3675 | { |
| 3676 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3677 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 3678 | time = PlaybackThread::activeSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3679 | } else { |
| 3680 | time = 10000; |
| 3681 | } |
| 3682 | return time; |
| 3683 | } |
| 3684 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3685 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3686 | { |
| 3687 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3688 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 3689 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3690 | } else { |
| 3691 | time = 10000; |
| 3692 | } |
| 3693 | return time; |
| 3694 | } |
| 3695 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3696 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3697 | { |
| 3698 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3699 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3700 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 3701 | } else { |
| 3702 | time = 10000; |
| 3703 | } |
| 3704 | return time; |
| 3705 | } |
| 3706 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3707 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 3708 | { |
| 3709 | PlaybackThread::cacheParameters_l(); |
| 3710 | |
| 3711 | // use shorter standby delay as on normal output to release |
| 3712 | // hardware resources as soon as possible |
| 3713 | standbyDelay = microseconds(activeSleepTime*2); |
| 3714 | } |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3715 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3716 | // ---------------------------------------------------------------------------- |
| 3717 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3718 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3719 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3720 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING), |
| 3721 | mWaitTimeMs(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3722 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3723 | addOutputTrack(mainThread); |
| 3724 | } |
| 3725 | |
| 3726 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 3727 | { |
| 3728 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3729 | mOutputTracks[i]->destroy(); |
| 3730 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3733 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3734 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3735 | // mix buffers... |
| 3736 | if (outputsReady(outputTracks)) { |
| 3737 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 3738 | } else { |
| 3739 | memset(mMixBuffer, 0, mixBufferSize); |
| 3740 | } |
| 3741 | sleepTime = 0; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3742 | writeFrames = mNormalFrameCount; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 3746 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3747 | if (sleepTime == 0) { |
Glenn Kasten | fec279f | 2012-03-08 07:47:15 -0800 | [diff] [blame] | 3748 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3749 | sleepTime = activeSleepTime; |
| 3750 | } else { |
| 3751 | sleepTime = idleSleepTime; |
| 3752 | } |
| 3753 | } else if (mBytesWritten != 0) { |
| 3754 | // flush remaining overflow buffers in output tracks |
| 3755 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3756 | if (outputTracks[i]->isActive()) { |
| 3757 | sleepTime = 0; |
| 3758 | writeFrames = 0; |
| 3759 | memset(mMixBuffer, 0, mixBufferSize); |
| 3760 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3761 | } |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3762 | } |
| 3763 | } |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3764 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3765 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3766 | void AudioFlinger::DuplicatingThread::threadLoop_write() |
| 3767 | { |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3768 | standbyTime = systemTime() + standbyDelay; |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3769 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3770 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 3771 | } |
| 3772 | mBytesWritten += mixBufferSize; |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3773 | } |
Glenn Kasten | 688a640 | 2012-02-29 07:57:06 -0800 | [diff] [blame] | 3774 | |
Glenn Kasten | 000f0e3 | 2012-03-01 17:10:56 -0800 | [diff] [blame] | 3775 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 3776 | { |
Glenn Kasten | 952eeb2 | 2012-03-06 11:30:57 -0800 | [diff] [blame] | 3777 | // DuplicatingThread implements standby by stopping all tracks |
| 3778 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3779 | outputTracks[i]->stop(); |
| 3780 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3781 | } |
| 3782 | |
Glenn Kasten | fa26a85 | 2012-03-06 11:28:04 -0800 | [diff] [blame] | 3783 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 3784 | { |
| 3785 | outputTracks = mOutputTracks; |
| 3786 | } |
| 3787 | |
| 3788 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 3789 | { |
| 3790 | outputTracks.clear(); |
| 3791 | } |
| 3792 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3793 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3794 | { |
Glenn Kasten | b6b7406 | 2012-02-24 14:12:20 -0800 | [diff] [blame] | 3795 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 3796 | // FIXME explain this formula |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3797 | int frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3798 | OutputTrack *outputTrack = new OutputTrack(thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3799 | this, |
| 3800 | mSampleRate, |
| 3801 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3802 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3803 | frameCount); |
| 3804 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3805 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3806 | mOutputTracks.add(outputTrack); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3807 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 3808 | updateWaitTime_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3813 | { |
| 3814 | Mutex::Autolock _l(mLock); |
| 3815 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3816 | if (mOutputTracks[i]->thread() == thread) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3817 | mOutputTracks[i]->destroy(); |
| 3818 | mOutputTracks.removeAt(i); |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 3819 | updateWaitTime_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3820 | return; |
| 3821 | } |
| 3822 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3823 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3824 | } |
| 3825 | |
Glenn Kasten | 438b036 | 2012-03-06 11:24:48 -0800 | [diff] [blame] | 3826 | // caller must hold mLock |
| 3827 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3828 | { |
| 3829 | mWaitTimeMs = UINT_MAX; |
| 3830 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3831 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3832 | if (strong != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3833 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3834 | if (waitTimeMs < mWaitTimeMs) { |
| 3835 | mWaitTimeMs = waitTimeMs; |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 3842 | bool AudioFlinger::DuplicatingThread::outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3843 | { |
| 3844 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3845 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3846 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3847 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3848 | return false; |
| 3849 | } |
| 3850 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3851 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3852 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3853 | return false; |
| 3854 | } |
| 3855 | } |
| 3856 | return true; |
| 3857 | } |
| 3858 | |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 3859 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3860 | { |
| 3861 | return (mWaitTimeMs * 1000) / 2; |
| 3862 | } |
| 3863 | |
Glenn Kasten | 66fcab9 | 2012-02-24 14:59:21 -0800 | [diff] [blame] | 3864 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 3865 | { |
| 3866 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 3867 | updateWaitTime_l(); |
| 3868 | |
| 3869 | MixerThread::cacheParameters_l(); |
| 3870 | } |
| 3871 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3872 | // ---------------------------------------------------------------------------- |
| 3873 | |
| 3874 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 3875 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3876 | ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3877 | const sp<Client>& client, |
| 3878 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3879 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3880 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3881 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3882 | const sp<IMemory>& sharedBuffer, |
| 3883 | int sessionId) |
| 3884 | : RefBase(), |
| 3885 | mThread(thread), |
| 3886 | mClient(client), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3887 | mCblk(NULL), |
| 3888 | // mBuffer |
| 3889 | // mBufferEnd |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3890 | mFrameCount(0), |
| 3891 | mState(IDLE), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 3892 | mSampleRate(sampleRate), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3893 | mFormat(format), |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 3894 | mStepServerFailed(false), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3895 | mSessionId(sessionId) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3896 | // mChannelCount |
| 3897 | // mChannelMask |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3898 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3899 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3900 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3901 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3902 | size_t size = sizeof(audio_track_cblk_t); |
| 3903 | uint8_t channelCount = popcount(channelMask); |
| 3904 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3905 | if (sharedBuffer == 0) { |
| 3906 | size += bufferSize; |
| 3907 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3908 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3909 | if (client != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3910 | mCblkMemory = client->heap()->allocate(size); |
| 3911 | if (mCblkMemory != 0) { |
| 3912 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3913 | if (mCblk != NULL) { // construct the shared structure in-place. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3914 | new(mCblk) audio_track_cblk_t(); |
| 3915 | // clear all buffers |
| 3916 | mCblk->frameCount = frameCount; |
| 3917 | mCblk->sampleRate = sampleRate; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 3918 | // uncomment the following lines to quickly test 32-bit wraparound |
| 3919 | // mCblk->user = 0xffff0000; |
| 3920 | // mCblk->server = 0xffff0000; |
| 3921 | // mCblk->userBase = 0xffff0000; |
| 3922 | // mCblk->serverBase = 0xffff0000; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3923 | mChannelCount = channelCount; |
| 3924 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3925 | if (sharedBuffer == 0) { |
| 3926 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3927 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3928 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3929 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3930 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3931 | } else { |
| 3932 | mBuffer = sharedBuffer->pointer(); |
| 3933 | } |
| 3934 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3935 | } |
| 3936 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3937 | ALOGE("not enough memory for AudioTrack size=%u", size); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3938 | client->heap()->dump("AudioTrack"); |
| 3939 | return; |
| 3940 | } |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3941 | } else { |
| 3942 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 3943 | // construct the shared structure in-place. |
| 3944 | new(mCblk) audio_track_cblk_t(); |
| 3945 | // clear all buffers |
| 3946 | mCblk->frameCount = frameCount; |
| 3947 | mCblk->sampleRate = sampleRate; |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 3948 | // uncomment the following lines to quickly test 32-bit wraparound |
| 3949 | // mCblk->user = 0xffff0000; |
| 3950 | // mCblk->server = 0xffff0000; |
| 3951 | // mCblk->userBase = 0xffff0000; |
| 3952 | // mCblk->serverBase = 0xffff0000; |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 3953 | mChannelCount = channelCount; |
| 3954 | mChannelMask = channelMask; |
| 3955 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3956 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3957 | // Force underrun condition to avoid false underrun callback until first data is |
| 3958 | // written to buffer (other flags are cleared) |
| 3959 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3960 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3961 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 3965 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3966 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 3967 | if (mClient == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3968 | delete mCblk; |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 3969 | } else { |
| 3970 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3971 | } |
| 3972 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 3973 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3974 | if (mClient != 0) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3975 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3976 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3977 | // If the client's reference count drops to zero, the associated destructor |
| 3978 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than |
| 3979 | // relying on the automatic clear() at end of scope. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3980 | mClient.clear(); |
| 3981 | } |
| 3982 | } |
| 3983 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 3984 | // AudioBufferProvider interface |
| 3985 | // getNextBuffer() = 0; |
| 3986 | // This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3987 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 3988 | { |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3989 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3990 | mFrameCount = buffer->frameCount; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 3991 | // FIXME See note at getNextBuffer() |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 3992 | (void) step(); // ignore return value of step() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3993 | buffer->frameCount = 0; |
| 3994 | } |
| 3995 | |
| 3996 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 3997 | bool result; |
| 3998 | audio_track_cblk_t* cblk = this->cblk(); |
| 3999 | |
| 4000 | result = cblk->stepServer(mFrameCount); |
| 4001 | if (!result) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4002 | ALOGV("stepServer failed acquiring cblk mutex"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4003 | mStepServerFailed = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4004 | } |
| 4005 | return result; |
| 4006 | } |
| 4007 | |
| 4008 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 4009 | audio_track_cblk_t* cblk = this->cblk(); |
| 4010 | |
| 4011 | cblk->user = 0; |
| 4012 | cblk->server = 0; |
| 4013 | cblk->userBase = 0; |
| 4014 | cblk->serverBase = 0; |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4015 | mStepServerFailed = false; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4016 | ALOGV("TrackBase::reset"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4019 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 4020 | return (int)mCblk->sampleRate; |
| 4021 | } |
| 4022 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4023 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 4024 | audio_track_cblk_t* cblk = this->cblk(); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 4025 | size_t frameSize = cblk->frameSize; |
| 4026 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; |
| 4027 | int8_t *bufferEnd = bufferStart + frames * frameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4028 | |
| 4029 | // Check validity of returned pointer in case the track control block would have been corrupted. |
Jean-Michel Trivi | acb86cc | 2012-04-16 12:43:57 -0700 | [diff] [blame] | 4030 | ALOG_ASSERT(!(bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd), |
| 4031 | "TrackBase::getBuffer buffer out of range:\n" |
| 4032 | " start: %p, end %p , mBuffer %p mBufferEnd %p\n" |
| 4033 | " server %u, serverBase %u, user %u, userBase %u, frameSize %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4034 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | acb86cc | 2012-04-16 12:43:57 -0700 | [diff] [blame] | 4035 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase, frameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4036 | |
| 4037 | return bufferStart; |
| 4038 | } |
| 4039 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4040 | status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event) |
| 4041 | { |
| 4042 | mSyncEvents.add(event); |
| 4043 | return NO_ERROR; |
| 4044 | } |
| 4045 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4046 | // ---------------------------------------------------------------------------- |
| 4047 | |
| 4048 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 4049 | AudioFlinger::PlaybackThread::Track::Track( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4050 | PlaybackThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4051 | const sp<Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 4052 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4053 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4054 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4055 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4056 | int frameCount, |
| 4057 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4058 | int sessionId, |
| 4059 | IAudioFlinger::track_flags_t flags) |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 4060 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId), |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4061 | mMute(false), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4062 | mFillingUpStatus(FS_INVALID), |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4063 | // mRetryCount initialized later when needed |
| 4064 | mSharedBuffer(sharedBuffer), |
| 4065 | mStreamType(streamType), |
| 4066 | mName(-1), // see note below |
| 4067 | mMainBuffer(thread->mixBuffer()), |
| 4068 | mAuxBuffer(NULL), |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4069 | mAuxEffectId(0), mHasVolumeController(false), |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4070 | mPresentationCompleteFrames(0), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4071 | mFlags(flags), |
| 4072 | mFastIndex(-1), |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4073 | mObservedUnderruns(0), |
| 4074 | mUnderrunCount(0), |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4075 | mCachedVolume(1.0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4076 | { |
| 4077 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4078 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 4079 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack |
Eric Laurent | edc15ad | 2011-07-21 19:35:01 -0700 | [diff] [blame] | 4080 | mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4081 | if (flags & IAudioFlinger::TRACK_FAST) { |
| 4082 | mCblk->flags |= CBLK_FAST; // atomic op not needed yet |
| 4083 | ALOG_ASSERT(thread->mFastTrackAvailMask != 0); |
| 4084 | int i = __builtin_ctz(thread->mFastTrackAvailMask); |
| 4085 | ALOG_ASSERT(0 < i && i < FastMixerState::kMaxFastTracks); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4086 | // FIXME This is too eager. We allocate a fast track index before the |
| 4087 | // fast track becomes active. Since fast tracks are a scarce resource, |
| 4088 | // this means we are potentially denying other more important fast tracks from |
| 4089 | // being created. It would be better to allocate the index dynamically. |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4090 | mFastIndex = i; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4091 | // Read the initial underruns because this field is never cleared by the fast mixer |
| 4092 | mObservedUnderruns = thread->getFastTrackUnderruns(i) & ~1; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4093 | thread->mFastTrackAvailMask &= ~(1 << i); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4094 | } |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4095 | // to avoid leaking a track name, do not allocate one unless there is an mCblk |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 4096 | mName = thread->getTrackName_l((audio_channel_mask_t)channelMask); |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4097 | if (mName < 0) { |
| 4098 | ALOGE("no more track names available"); |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4099 | // FIXME bug - if sufficient fast track indices, but insufficient normal mixer names, |
| 4100 | // then we leak a fast track index. Should swap these two sections, or better yet |
| 4101 | // only allocate a normal mixer name for normal tracks. |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4102 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4103 | } |
Glenn Kasten | f995901 | 2012-03-19 11:14:37 -0700 | [diff] [blame] | 4104 | ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | AudioFlinger::PlaybackThread::Track::~Track() |
| 4108 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4109 | ALOGV("PlaybackThread::Track destructor"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4110 | sp<ThreadBase> thread = mThread.promote(); |
| 4111 | if (thread != 0) { |
| 4112 | Mutex::Autolock _l(thread->mLock); |
| 4113 | mState = TERMINATED; |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 4118 | { |
| 4119 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 4120 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 4121 | // destructor is called. As the destructor needs to lock mLock, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4122 | // we must acquire a strong reference on this Track before locking mLock |
| 4123 | // here so that the destructor is called only when exiting this function. |
| 4124 | // On the other hand, as long as Track::destroy() is only called by |
| 4125 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 4126 | // this Track with its member mTrack. |
| 4127 | sp<Track> keep(this); |
| 4128 | { // scope for mLock |
| 4129 | sp<ThreadBase> thread = mThread.promote(); |
| 4130 | if (thread != 0) { |
| 4131 | if (!isOutputTrack()) { |
| 4132 | if (mState == ACTIVE || mState == RESUMING) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4133 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4134 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4135 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4136 | // to track the speaker usage |
| 4137 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4138 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4139 | } |
| 4140 | AudioSystem::releaseOutput(thread->id()); |
| 4141 | } |
| 4142 | Mutex::Autolock _l(thread->mLock); |
| 4143 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4144 | playbackThread->destroyTrack_l(this); |
| 4145 | } |
| 4146 | } |
| 4147 | } |
| 4148 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4149 | /*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) |
| 4150 | { |
| 4151 | result.append(" Name Client Type Fmt Chn mask Session Frames S M F SRate L dB R dB " |
| 4152 | " Server User Main buf Aux Buf FastUnder\n"); |
| 4153 | |
| 4154 | } |
| 4155 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4156 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 4157 | { |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 4158 | uint32_t vlr = mCblk->getVolumeLR(); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4159 | if (isFastTrack()) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4160 | sprintf(buffer, " F %2d", mFastIndex); |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4161 | } else { |
| 4162 | sprintf(buffer, " %4d", mName - AudioMixer::TRACK0); |
| 4163 | } |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4164 | track_state state = mState; |
| 4165 | char stateChar; |
| 4166 | switch (state) { |
| 4167 | case IDLE: |
| 4168 | stateChar = 'I'; |
| 4169 | break; |
| 4170 | case TERMINATED: |
| 4171 | stateChar = 'T'; |
| 4172 | break; |
| 4173 | case STOPPED: |
| 4174 | stateChar = 'S'; |
| 4175 | break; |
| 4176 | case RESUMING: |
| 4177 | stateChar = 'R'; |
| 4178 | break; |
| 4179 | case ACTIVE: |
| 4180 | stateChar = 'A'; |
| 4181 | break; |
| 4182 | case PAUSING: |
| 4183 | stateChar = 'p'; |
| 4184 | break; |
| 4185 | case PAUSED: |
| 4186 | stateChar = 'P'; |
| 4187 | break; |
| 4188 | default: |
| 4189 | stateChar = '?'; |
| 4190 | break; |
| 4191 | } |
| 4192 | bool nowInUnderrun = mObservedUnderruns & 1; |
| 4193 | snprintf(&buffer[7], size-7, " %6d %4u %3u 0x%08x %7u %6u %1c %1d %1d %5u %5.2g %5.2g " |
| 4194 | "0x%08x 0x%08x 0x%08x 0x%08x %9u%c\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 4195 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4196 | mStreamType, |
| 4197 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4198 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4199 | mSessionId, |
| 4200 | mFrameCount, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4201 | stateChar, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4202 | mMute, |
| 4203 | mFillingUpStatus, |
| 4204 | mCblk->sampleRate, |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4205 | 20.0 * log10((vlr & 0xFFFF) / 4096.0), |
| 4206 | 20.0 * log10((vlr >> 16) / 4096.0), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4207 | mCblk->server, |
| 4208 | mCblk->user, |
| 4209 | (int)mMainBuffer, |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4210 | (int)mAuxBuffer, |
| 4211 | mUnderrunCount, |
| 4212 | nowInUnderrun ? '*' : ' '); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4213 | } |
| 4214 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4215 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4216 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4217 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4218 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4219 | audio_track_cblk_t* cblk = this->cblk(); |
| 4220 | uint32_t framesReady; |
| 4221 | uint32_t framesReq = buffer->frameCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4222 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4223 | // Check if last stepServer failed, try to step now |
| 4224 | if (mStepServerFailed) { |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4225 | // FIXME When called by fast mixer, this takes a mutex with tryLock(). |
| 4226 | // Since the fast mixer is higher priority than client callback thread, |
| 4227 | // it does not result in priority inversion for client. |
| 4228 | // But a non-blocking solution would be preferable to avoid |
| 4229 | // fast mixer being unable to tryLock(), and |
| 4230 | // to avoid the extra context switches if the client wakes up, |
| 4231 | // discovers the mutex is locked, then has to wait for fast mixer to unlock. |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4232 | if (!step()) goto getNextBuffer_exit; |
| 4233 | ALOGV("stepServer recovered"); |
| 4234 | mStepServerFailed = false; |
| 4235 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4236 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4237 | // FIXME Same as above |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4238 | framesReady = cblk->framesReady(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4239 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4240 | if (CC_LIKELY(framesReady)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4241 | uint32_t s = cblk->server; |
| 4242 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 4243 | |
| 4244 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 4245 | if (framesReq > framesReady) { |
| 4246 | framesReq = framesReady; |
| 4247 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 4248 | if (framesReq > bufferEnd - s) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4249 | framesReq = bufferEnd - s; |
| 4250 | } |
| 4251 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4252 | buffer->raw = getBuffer(s, framesReq); |
| 4253 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4254 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4255 | buffer->frameCount = framesReq; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4256 | return NO_ERROR; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4257 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4258 | |
| 4259 | getNextBuffer_exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4260 | buffer->raw = NULL; |
| 4261 | buffer->frameCount = 0; |
| 4262 | ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
| 4263 | return NOT_ENOUGH_DATA; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4264 | } |
| 4265 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4266 | // Note that framesReady() takes a mutex on the control block using tryLock(). |
| 4267 | // This could result in priority inversion if framesReady() is called by the normal mixer, |
| 4268 | // as the normal mixer thread runs at lower |
| 4269 | // priority than the client's callback thread: there is a short window within framesReady() |
| 4270 | // during which the normal mixer could be preempted, and the client callback would block. |
| 4271 | // Another problem can occur if framesReady() is called by the fast mixer: |
| 4272 | // the tryLock() could block for up to 1 ms, and a sequence of these could delay fast mixer. |
| 4273 | // FIXME Replace AudioTrackShared control block implementation by a non-blocking FIFO queue. |
| 4274 | size_t AudioFlinger::PlaybackThread::Track::framesReady() const { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4275 | return mCblk->framesReady(); |
| 4276 | } |
| 4277 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 4278 | // Don't call for fast tracks; the framesReady() could result in priority inversion |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4279 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 4280 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4281 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4282 | if (framesReady() >= mCblk->frameCount || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4283 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 4284 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4285 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4286 | return true; |
| 4287 | } |
| 4288 | return false; |
| 4289 | } |
| 4290 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 4291 | status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4292 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4293 | { |
| 4294 | status_t status = NO_ERROR; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4295 | ALOGV("start(%d), calling pid %d session %d", |
| 4296 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 4297 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4298 | sp<ThreadBase> thread = mThread.promote(); |
| 4299 | if (thread != 0) { |
| 4300 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 4301 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4302 | // here the track could be either new, or restarted |
| 4303 | // in both cases "unstop" the track |
| 4304 | if (mState == PAUSED) { |
| 4305 | mState = TrackBase::RESUMING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4306 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4307 | } else { |
| 4308 | mState = TrackBase::ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4309 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 4313 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4314 | status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4315 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4316 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4317 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4318 | // to track the speaker usage |
| 4319 | if (status == NO_ERROR) { |
| 4320 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 4321 | } |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4322 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4323 | } |
| 4324 | if (status == NO_ERROR) { |
| 4325 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4326 | playbackThread->addTrack_l(this); |
| 4327 | } else { |
| 4328 | mState = state; |
| 4329 | } |
| 4330 | } else { |
| 4331 | status = BAD_VALUE; |
| 4332 | } |
| 4333 | return status; |
| 4334 | } |
| 4335 | |
| 4336 | void AudioFlinger::PlaybackThread::Track::stop() |
| 4337 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 4338 | ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4339 | sp<ThreadBase> thread = mThread.promote(); |
| 4340 | if (thread != 0) { |
| 4341 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 4342 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4343 | if (mState > STOPPED) { |
| 4344 | mState = STOPPED; |
| 4345 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 4346 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4347 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 4348 | reset(); |
| 4349 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4350 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4351 | } |
| 4352 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 4353 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4354 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4355 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4356 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4357 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4358 | // to track the speaker usage |
| 4359 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4360 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | void AudioFlinger::PlaybackThread::Track::pause() |
| 4366 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 4367 | ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4368 | sp<ThreadBase> thread = mThread.promote(); |
| 4369 | if (thread != 0) { |
| 4370 | Mutex::Autolock _l(thread->mLock); |
| 4371 | if (mState == ACTIVE || mState == RESUMING) { |
| 4372 | mState = PAUSING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4373 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4374 | if (!isOutputTrack()) { |
| 4375 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 4376 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4377 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4378 | |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4379 | #ifdef ADD_BATTERY_DATA |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 4380 | // to track the speaker usage |
| 4381 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Glenn Kasten | d3cee2f | 2012-03-13 17:55:35 -0700 | [diff] [blame] | 4382 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4383 | } |
| 4384 | } |
| 4385 | } |
| 4386 | } |
| 4387 | |
| 4388 | void AudioFlinger::PlaybackThread::Track::flush() |
| 4389 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4390 | ALOGV("flush(%d)", mName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4391 | sp<ThreadBase> thread = mThread.promote(); |
| 4392 | if (thread != 0) { |
| 4393 | Mutex::Autolock _l(thread->mLock); |
| 4394 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 4395 | return; |
| 4396 | } |
| 4397 | // No point remaining in PAUSED state after a flush => go to |
| 4398 | // STOPPED state |
| 4399 | mState = STOPPED; |
| 4400 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4401 | // do not reset the track if it is still in the process of being stopped or paused. |
| 4402 | // this will be done by prepareTracks_l() when the track is stopped. |
| 4403 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4404 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 4405 | reset(); |
| 4406 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | void AudioFlinger::PlaybackThread::Track::reset() |
| 4411 | { |
| 4412 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 4413 | // the audioflinger thread detects the track is stopped. |
| 4414 | if (!mResetDone) { |
| 4415 | TrackBase::reset(); |
| 4416 | // Force underrun condition to avoid false underrun callback until first data is |
| 4417 | // written to buffer |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4418 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 4419 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4420 | mFillingUpStatus = FS_FILLING; |
| 4421 | mResetDone = true; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4422 | mPresentationCompleteFrames = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 4427 | { |
| 4428 | mMute = muted; |
| 4429 | } |
| 4430 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4431 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 4432 | { |
| 4433 | status_t status = DEAD_OBJECT; |
| 4434 | sp<ThreadBase> thread = mThread.promote(); |
| 4435 | if (thread != 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 4436 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4437 | status = playbackThread->attachAuxEffect(this, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4438 | } |
| 4439 | return status; |
| 4440 | } |
| 4441 | |
| 4442 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 4443 | { |
| 4444 | mAuxEffectId = EffectId; |
| 4445 | mAuxBuffer = buffer; |
| 4446 | } |
| 4447 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4448 | bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten, |
| 4449 | size_t audioHalFrames) |
| 4450 | { |
| 4451 | // a track is considered presented when the total number of frames written to audio HAL |
| 4452 | // corresponds to the number of frames written when presentationComplete() is called for the |
| 4453 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. |
| 4454 | if (mPresentationCompleteFrames == 0) { |
| 4455 | mPresentationCompleteFrames = framesWritten + audioHalFrames; |
| 4456 | ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d", |
| 4457 | mPresentationCompleteFrames, audioHalFrames); |
| 4458 | } |
| 4459 | if (framesWritten >= mPresentationCompleteFrames) { |
| 4460 | ALOGV("presentationComplete() session %d complete: framesWritten %d", |
| 4461 | mSessionId, framesWritten); |
| 4462 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 4463 | mPresentationCompleteFrames = 0; |
| 4464 | return true; |
| 4465 | } |
| 4466 | return false; |
| 4467 | } |
| 4468 | |
| 4469 | void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) |
| 4470 | { |
| 4471 | for (int i = 0; i < (int)mSyncEvents.size(); i++) { |
| 4472 | if (mSyncEvents[i]->type() == type) { |
| 4473 | mSyncEvents[i]->trigger(); |
| 4474 | mSyncEvents.removeAt(i); |
| 4475 | i--; |
| 4476 | } |
| 4477 | } |
| 4478 | } |
| 4479 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 4480 | // implement VolumeBufferProvider interface |
| 4481 | |
| 4482 | uint32_t AudioFlinger::PlaybackThread::Track::getVolumeLR() |
| 4483 | { |
| 4484 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs |
| 4485 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); |
| 4486 | uint32_t vlr = mCblk->getVolumeLR(); |
| 4487 | uint32_t vl = vlr & 0xFFFF; |
| 4488 | uint32_t vr = vlr >> 16; |
| 4489 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 4490 | if (vl > MAX_GAIN_INT) { |
| 4491 | vl = MAX_GAIN_INT; |
| 4492 | } |
| 4493 | if (vr > MAX_GAIN_INT) { |
| 4494 | vr = MAX_GAIN_INT; |
| 4495 | } |
| 4496 | // now apply the cached master volume and stream type volume; |
| 4497 | // this is trusted but lacks any synchronization or barrier so may be stale |
| 4498 | float v = mCachedVolume; |
| 4499 | vl *= v; |
| 4500 | vr *= v; |
| 4501 | // re-combine into U4.16 |
| 4502 | vlr = (vr << 16) | (vl & 0xFFFF); |
| 4503 | // FIXME look at mute, pause, and stop flags |
| 4504 | return vlr; |
| 4505 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 4506 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4507 | // timed audio tracks |
| 4508 | |
| 4509 | sp<AudioFlinger::PlaybackThread::TimedTrack> |
| 4510 | AudioFlinger::PlaybackThread::TimedTrack::create( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4511 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4512 | const sp<Client>& client, |
| 4513 | audio_stream_type_t streamType, |
| 4514 | uint32_t sampleRate, |
| 4515 | audio_format_t format, |
| 4516 | uint32_t channelMask, |
| 4517 | int frameCount, |
| 4518 | const sp<IMemory>& sharedBuffer, |
| 4519 | int sessionId) { |
| 4520 | if (!client->reserveTimedTrack()) |
| 4521 | return NULL; |
| 4522 | |
Glenn Kasten | a035676 | 2012-03-19 10:38:51 -0700 | [diff] [blame] | 4523 | return new TimedTrack( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4524 | thread, client, streamType, sampleRate, format, channelMask, frameCount, |
| 4525 | sharedBuffer, sessionId); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | AudioFlinger::PlaybackThread::TimedTrack::TimedTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4529 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4530 | const sp<Client>& client, |
| 4531 | audio_stream_type_t streamType, |
| 4532 | uint32_t sampleRate, |
| 4533 | audio_format_t format, |
| 4534 | uint32_t channelMask, |
| 4535 | int frameCount, |
| 4536 | const sp<IMemory>& sharedBuffer, |
| 4537 | int sessionId) |
| 4538 | : Track(thread, client, streamType, sampleRate, format, channelMask, |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 4539 | frameCount, sharedBuffer, sessionId, IAudioFlinger::TRACK_TIMED), |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4540 | mQueueHeadInFlight(false), |
| 4541 | mTrimQueueHeadOnRelease(false), |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4542 | mFramesPendingInQueue(0), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4543 | mTimedSilenceBuffer(NULL), |
| 4544 | mTimedSilenceBufferSize(0), |
| 4545 | mTimedAudioOutputOnTime(false), |
| 4546 | mMediaTimeTransformValid(false) |
| 4547 | { |
| 4548 | LocalClock lc; |
| 4549 | mLocalTimeFreq = lc.getLocalFreq(); |
| 4550 | |
| 4551 | mLocalTimeToSampleTransform.a_zero = 0; |
| 4552 | mLocalTimeToSampleTransform.b_zero = 0; |
| 4553 | mLocalTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 4554 | mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq; |
| 4555 | LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer, |
| 4556 | &mLocalTimeToSampleTransform.a_to_b_denom); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4557 | |
| 4558 | mMediaTimeToSampleTransform.a_zero = 0; |
| 4559 | mMediaTimeToSampleTransform.b_zero = 0; |
| 4560 | mMediaTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 4561 | mMediaTimeToSampleTransform.a_to_b_denom = 1000000; |
| 4562 | LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer, |
| 4563 | &mMediaTimeToSampleTransform.a_to_b_denom); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() { |
| 4567 | mClient->releaseTimedTrack(); |
| 4568 | delete [] mTimedSilenceBuffer; |
| 4569 | } |
| 4570 | |
| 4571 | status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer( |
| 4572 | size_t size, sp<IMemory>* buffer) { |
| 4573 | |
| 4574 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4575 | |
| 4576 | trimTimedBufferQueue_l(); |
| 4577 | |
| 4578 | // lazily initialize the shared memory heap for timed buffers |
| 4579 | if (mTimedMemoryDealer == NULL) { |
| 4580 | const int kTimedBufferHeapSize = 512 << 10; |
| 4581 | |
| 4582 | mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize, |
| 4583 | "AudioFlingerTimed"); |
| 4584 | if (mTimedMemoryDealer == NULL) |
| 4585 | return NO_MEMORY; |
| 4586 | } |
| 4587 | |
| 4588 | sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size); |
| 4589 | if (newBuffer == NULL) { |
| 4590 | newBuffer = mTimedMemoryDealer->allocate(size); |
| 4591 | if (newBuffer == NULL) |
| 4592 | return NO_MEMORY; |
| 4593 | } |
| 4594 | |
| 4595 | *buffer = newBuffer; |
| 4596 | return NO_ERROR; |
| 4597 | } |
| 4598 | |
| 4599 | // caller must hold mTimedBufferQueueLock |
| 4600 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() { |
| 4601 | int64_t mediaTimeNow; |
| 4602 | { |
| 4603 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4604 | if (!mMediaTimeTransformValid) |
| 4605 | return; |
| 4606 | |
| 4607 | int64_t targetTimeNow; |
| 4608 | status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) |
| 4609 | ? mCCHelper.getCommonTime(&targetTimeNow) |
| 4610 | : mCCHelper.getLocalTime(&targetTimeNow); |
| 4611 | |
| 4612 | if (OK != res) |
| 4613 | return; |
| 4614 | |
| 4615 | if (!mMediaTimeTransform.doReverseTransform(targetTimeNow, |
| 4616 | &mediaTimeNow)) { |
| 4617 | return; |
| 4618 | } |
| 4619 | } |
| 4620 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4621 | size_t trimEnd; |
| 4622 | for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) { |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4623 | int64_t bufEnd; |
| 4624 | |
John Grossman | c95cfbb | 2012-04-12 11:53:11 -0700 | [diff] [blame] | 4625 | if ((trimEnd + 1) < mTimedBufferQueue.size()) { |
| 4626 | // We have a next buffer. Just use its PTS as the PTS of the frame |
| 4627 | // following the last frame in this buffer. If the stream is sparse |
| 4628 | // (ie, there are deliberate gaps left in the stream which should be |
| 4629 | // filled with silence by the TimedAudioTrack), then this can result |
| 4630 | // in one extra buffer being left un-trimmed when it could have |
| 4631 | // been. In general, this is not typical, and we would rather |
| 4632 | // optimized away the TS calculation below for the more common case |
| 4633 | // where PTSes are contiguous. |
| 4634 | bufEnd = mTimedBufferQueue[trimEnd + 1].pts(); |
| 4635 | } else { |
| 4636 | // We have no next buffer. Compute the PTS of the frame following |
| 4637 | // the last frame in this buffer by computing the duration of of |
| 4638 | // this frame in media time units and adding it to the PTS of the |
| 4639 | // buffer. |
| 4640 | int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size() |
| 4641 | / mCblk->frameSize; |
| 4642 | |
| 4643 | if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount, |
| 4644 | &bufEnd)) { |
| 4645 | ALOGE("Failed to convert frame count of %lld to media time" |
| 4646 | " duration" " (scale factor %d/%u) in %s", |
| 4647 | frameCount, |
| 4648 | mMediaTimeToSampleTransform.a_to_b_numer, |
| 4649 | mMediaTimeToSampleTransform.a_to_b_denom, |
| 4650 | __PRETTY_FUNCTION__); |
| 4651 | break; |
| 4652 | } |
| 4653 | bufEnd += mTimedBufferQueue[trimEnd].pts(); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4654 | } |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4655 | |
| 4656 | if (bufEnd > mediaTimeNow) |
| 4657 | break; |
| 4658 | |
| 4659 | // Is the buffer we want to use in the middle of a mix operation right |
| 4660 | // now? If so, don't actually trim it. Just wait for the releaseBuffer |
| 4661 | // from the mixer which should be coming back shortly. |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4662 | if (!trimEnd && mQueueHeadInFlight) { |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4663 | mTrimQueueHeadOnRelease = true; |
| 4664 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4665 | } |
| 4666 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4667 | size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0; |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4668 | if (trimStart < trimEnd) { |
| 4669 | // Update the bookkeeping for framesReady() |
| 4670 | for (size_t i = trimStart; i < trimEnd; ++i) { |
| 4671 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim"); |
| 4672 | } |
| 4673 | |
| 4674 | // Now actually remove the buffers from the queue. |
| 4675 | mTimedBufferQueue.removeItemsAt(trimStart, trimEnd); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4676 | } |
| 4677 | } |
| 4678 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4679 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l( |
| 4680 | const char* logTag) { |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4681 | ALOG_ASSERT(mTimedBufferQueue.size() > 0, |
| 4682 | "%s called (reason \"%s\"), but timed buffer queue has no" |
| 4683 | " elements to trim.", __FUNCTION__, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4684 | |
| 4685 | updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag); |
| 4686 | mTimedBufferQueue.removeAt(0); |
| 4687 | } |
| 4688 | |
| 4689 | void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l( |
| 4690 | const TimedBuffer& buf, |
| 4691 | const char* logTag) { |
| 4692 | uint32_t bufBytes = buf.buffer()->size(); |
| 4693 | uint32_t consumedAlready = buf.position(); |
| 4694 | |
Eric Laurent | b388e53 | 2012-04-14 13:32:48 -0700 | [diff] [blame] | 4695 | ALOG_ASSERT(consumedAlready <= bufBytes, |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4696 | "Bad bookkeeping while updating frames pending. Timed buffer is" |
| 4697 | " only %u bytes long, but claims to have consumed %u" |
| 4698 | " bytes. (update reason: \"%s\")", |
Eric Laurent | b388e53 | 2012-04-14 13:32:48 -0700 | [diff] [blame] | 4699 | bufBytes, consumedAlready, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4700 | |
| 4701 | uint32_t bufFrames = (bufBytes - consumedAlready) / mCblk->frameSize; |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4702 | ALOG_ASSERT(mFramesPendingInQueue >= bufFrames, |
| 4703 | "Bad bookkeeping while updating frames pending. Should have at" |
| 4704 | " least %u queued frames, but we think we have only %u. (update" |
| 4705 | " reason: \"%s\")", |
| 4706 | bufFrames, mFramesPendingInQueue, logTag); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4707 | |
| 4708 | mFramesPendingInQueue -= bufFrames; |
| 4709 | } |
| 4710 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4711 | status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer( |
| 4712 | const sp<IMemory>& buffer, int64_t pts) { |
| 4713 | |
| 4714 | { |
| 4715 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4716 | if (!mMediaTimeTransformValid) |
| 4717 | return INVALID_OPERATION; |
| 4718 | } |
| 4719 | |
| 4720 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4721 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4722 | uint32_t bufFrames = buffer->size() / mCblk->frameSize; |
| 4723 | mFramesPendingInQueue += bufFrames; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4724 | mTimedBufferQueue.add(TimedBuffer(buffer, pts)); |
| 4725 | |
| 4726 | return NO_ERROR; |
| 4727 | } |
| 4728 | |
| 4729 | status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform( |
| 4730 | const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) { |
| 4731 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4732 | ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d", |
| 4733 | xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom, |
| 4734 | target); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4735 | |
| 4736 | if (!(target == TimedAudioTrack::LOCAL_TIME || |
| 4737 | target == TimedAudioTrack::COMMON_TIME)) { |
| 4738 | return BAD_VALUE; |
| 4739 | } |
| 4740 | |
| 4741 | Mutex::Autolock lock(mMediaTimeTransformLock); |
| 4742 | mMediaTimeTransform = xform; |
| 4743 | mMediaTimeTransformTarget = target; |
| 4744 | mMediaTimeTransformValid = true; |
| 4745 | |
| 4746 | return NO_ERROR; |
| 4747 | } |
| 4748 | |
| 4749 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 4750 | |
| 4751 | // implementation of getNextBuffer for tracks whose buffers have timestamps |
| 4752 | status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer( |
| 4753 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 4754 | { |
| 4755 | if (pts == AudioBufferProvider::kInvalidPTS) { |
| 4756 | buffer->raw = 0; |
| 4757 | buffer->frameCount = 0; |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4758 | mTimedAudioOutputOnTime = false; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4759 | return INVALID_OPERATION; |
| 4760 | } |
| 4761 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4762 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4763 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4764 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 4765 | "getNextBuffer called without releaseBuffer!"); |
| 4766 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4767 | while (true) { |
| 4768 | |
| 4769 | // if we have no timed buffers, then fail |
| 4770 | if (mTimedBufferQueue.isEmpty()) { |
| 4771 | buffer->raw = 0; |
| 4772 | buffer->frameCount = 0; |
| 4773 | return NOT_ENOUGH_DATA; |
| 4774 | } |
| 4775 | |
| 4776 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
| 4777 | |
| 4778 | // calculate the PTS of the head of the timed buffer queue expressed in |
| 4779 | // local time |
| 4780 | int64_t headLocalPTS; |
| 4781 | { |
| 4782 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 4783 | |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 4784 | ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4785 | |
| 4786 | if (mMediaTimeTransform.a_to_b_denom == 0) { |
| 4787 | // the transform represents a pause, so yield silence |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4788 | timedYieldSilence_l(buffer->frameCount, buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4789 | return NO_ERROR; |
| 4790 | } |
| 4791 | |
| 4792 | int64_t transformedPTS; |
| 4793 | if (!mMediaTimeTransform.doForwardTransform(head.pts(), |
| 4794 | &transformedPTS)) { |
| 4795 | // the transform failed. this shouldn't happen, but if it does |
| 4796 | // then just drop this buffer |
| 4797 | ALOGW("timedGetNextBuffer transform failed"); |
| 4798 | buffer->raw = 0; |
| 4799 | buffer->frameCount = 0; |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4800 | trimTimedBufferQueueHead_l("getNextBuffer; no transform"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4801 | return NO_ERROR; |
| 4802 | } |
| 4803 | |
| 4804 | if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) { |
| 4805 | if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS, |
| 4806 | &headLocalPTS)) { |
| 4807 | buffer->raw = 0; |
| 4808 | buffer->frameCount = 0; |
| 4809 | return INVALID_OPERATION; |
| 4810 | } |
| 4811 | } else { |
| 4812 | headLocalPTS = transformedPTS; |
| 4813 | } |
| 4814 | } |
| 4815 | |
| 4816 | // adjust the head buffer's PTS to reflect the portion of the head buffer |
| 4817 | // that has already been consumed |
| 4818 | int64_t effectivePTS = headLocalPTS + |
| 4819 | ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate()); |
| 4820 | |
| 4821 | // Calculate the delta in samples between the head of the input buffer |
| 4822 | // queue and the start of the next output buffer that will be written. |
| 4823 | // If the transformation fails because of over or underflow, it means |
| 4824 | // that the sample's position in the output stream is so far out of |
| 4825 | // whack that it should just be dropped. |
| 4826 | int64_t sampleDelta; |
| 4827 | if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) { |
| 4828 | ALOGV("*** head buffer is too far from PTS: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4829 | trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from" |
| 4830 | " mix"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4831 | continue; |
| 4832 | } |
| 4833 | if (!mLocalTimeToSampleTransform.doForwardTransform( |
| 4834 | (effectivePTS - pts) << 32, &sampleDelta)) { |
John Grossman | d3030da | 2012-04-12 11:56:36 -0700 | [diff] [blame] | 4835 | ALOGV("*** too late during sample rate transform: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4836 | trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4837 | continue; |
| 4838 | } |
| 4839 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4840 | ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld" |
| 4841 | " sampleDelta=[%d.%08x]", |
| 4842 | head.pts(), head.position(), pts, |
| 4843 | static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) |
| 4844 | + (sampleDelta >> 32)), |
| 4845 | static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF)); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4846 | |
| 4847 | // if the delta between the ideal placement for the next input sample and |
| 4848 | // the current output position is within this threshold, then we will |
| 4849 | // concatenate the next input samples to the previous output |
| 4850 | const int64_t kSampleContinuityThreshold = |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4851 | (static_cast<int64_t>(sampleRate()) << 32) / 250; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4852 | |
| 4853 | // if this is the first buffer of audio that we're emitting from this track |
| 4854 | // then it should be almost exactly on time. |
| 4855 | const int64_t kSampleStartupThreshold = 1LL << 32; |
| 4856 | |
| 4857 | if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) || |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4858 | (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4859 | // the next input is close enough to being on time, so concatenate it |
| 4860 | // with the last output |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4861 | timedYieldSamples_l(buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4862 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4863 | ALOGVV("*** on time: head.pos=%d frameCount=%u", |
| 4864 | head.position(), buffer->frameCount); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4865 | return NO_ERROR; |
John Grossman | 8d314b7 | 2012-04-19 12:08:17 -0700 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | // Looks like our output is not on time. Reset our on timed status. |
| 4869 | // Next time we mix samples from our input queue, then should be within |
| 4870 | // the StartupThreshold. |
| 4871 | mTimedAudioOutputOnTime = false; |
| 4872 | if (sampleDelta > 0) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4873 | // the gap between the current output position and the proper start of |
| 4874 | // the next input sample is too big, so fill it with silence |
| 4875 | uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32; |
| 4876 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4877 | timedYieldSilence_l(framesUntilNextInput, buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4878 | ALOGV("*** silence: frameCount=%u", buffer->frameCount); |
| 4879 | return NO_ERROR; |
| 4880 | } else { |
| 4881 | // the next input sample is late |
| 4882 | uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32)); |
| 4883 | size_t onTimeSamplePosition = |
| 4884 | head.position() + lateFrames * mCblk->frameSize; |
| 4885 | |
| 4886 | if (onTimeSamplePosition > head.buffer()->size()) { |
| 4887 | // all the remaining samples in the head are too late, so |
| 4888 | // drop it and move on |
| 4889 | ALOGV("*** too late: dropped buffer"); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4890 | trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4891 | continue; |
| 4892 | } else { |
| 4893 | // skip over the late samples |
| 4894 | head.setPosition(onTimeSamplePosition); |
| 4895 | |
| 4896 | // yield the available samples |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4897 | timedYieldSamples_l(buffer); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4898 | |
| 4899 | ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); |
| 4900 | return NO_ERROR; |
| 4901 | } |
| 4902 | } |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | // Yield samples from the timed buffer queue head up to the given output |
| 4907 | // buffer's capacity. |
| 4908 | // |
| 4909 | // Caller must hold mTimedBufferQueueLock |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4910 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4911 | AudioBufferProvider::Buffer* buffer) { |
| 4912 | |
| 4913 | const TimedBuffer& head = mTimedBufferQueue[0]; |
| 4914 | |
| 4915 | buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) + |
| 4916 | head.position()); |
| 4917 | |
| 4918 | uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) / |
| 4919 | mCblk->frameSize); |
| 4920 | size_t framesRequested = buffer->frameCount; |
| 4921 | buffer->frameCount = min(framesLeftInHead, framesRequested); |
| 4922 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4923 | mQueueHeadInFlight = true; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4924 | mTimedAudioOutputOnTime = true; |
| 4925 | } |
| 4926 | |
| 4927 | // Yield samples of silence up to the given output buffer's capacity |
| 4928 | // |
| 4929 | // Caller must hold mTimedBufferQueueLock |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4930 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l( |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4931 | uint32_t numFrames, AudioBufferProvider::Buffer* buffer) { |
| 4932 | |
| 4933 | // lazily allocate a buffer filled with silence |
| 4934 | if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) { |
| 4935 | delete [] mTimedSilenceBuffer; |
| 4936 | mTimedSilenceBufferSize = numFrames * mCblk->frameSize; |
| 4937 | mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize]; |
| 4938 | memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize); |
| 4939 | } |
| 4940 | |
| 4941 | buffer->raw = mTimedSilenceBuffer; |
| 4942 | size_t framesRequested = buffer->frameCount; |
| 4943 | buffer->frameCount = min(numFrames, framesRequested); |
| 4944 | |
| 4945 | mTimedAudioOutputOnTime = false; |
| 4946 | } |
| 4947 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 4948 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4949 | void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer( |
| 4950 | AudioBufferProvider::Buffer* buffer) { |
| 4951 | |
| 4952 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4953 | |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4954 | // If the buffer which was just released is part of the buffer at the head |
| 4955 | // of the queue, be sure to update the amt of the buffer which has been |
| 4956 | // consumed. If the buffer being returned is not part of the head of the |
| 4957 | // queue, its either because the buffer is part of the silence buffer, or |
| 4958 | // because the head of the timed queue was trimmed after the mixer called |
| 4959 | // getNextBuffer but before the mixer called releaseBuffer. |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4960 | if (buffer->raw == mTimedSilenceBuffer) { |
| 4961 | ALOG_ASSERT(!mQueueHeadInFlight, |
| 4962 | "Queue head in flight during release of silence buffer!"); |
| 4963 | goto done; |
| 4964 | } |
| 4965 | |
| 4966 | ALOG_ASSERT(mQueueHeadInFlight, |
| 4967 | "TimedTrack::releaseBuffer of non-silence buffer, but no queue" |
| 4968 | " head in flight."); |
| 4969 | |
| 4970 | if (mTimedBufferQueue.size()) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4971 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4972 | |
| 4973 | void* start = head.buffer()->pointer(); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4974 | void* end = reinterpret_cast<void*>( |
| 4975 | reinterpret_cast<uint8_t*>(head.buffer()->pointer()) |
| 4976 | + head.buffer()->size()); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4977 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4978 | ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end), |
| 4979 | "released buffer not within the head of the timed buffer" |
| 4980 | " queue; qHead = [%p, %p], released buffer = %p", |
| 4981 | start, end, buffer->raw); |
| 4982 | |
| 4983 | head.setPosition(head.position() + |
| 4984 | (buffer->frameCount * mCblk->frameSize)); |
| 4985 | mQueueHeadInFlight = false; |
| 4986 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4987 | ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount, |
| 4988 | "Bad bookkeeping during releaseBuffer! Should have at" |
| 4989 | " least %u queued frames, but we think we have only %u", |
| 4990 | buffer->frameCount, mFramesPendingInQueue); |
| 4991 | |
| 4992 | mFramesPendingInQueue -= buffer->frameCount; |
| 4993 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4994 | if ((static_cast<size_t>(head.position()) >= head.buffer()->size()) |
| 4995 | || mTrimQueueHeadOnRelease) { |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 4996 | trimTimedBufferQueueHead_l("releaseBuffer"); |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4997 | mTrimQueueHeadOnRelease = false; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4998 | } |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 4999 | } else { |
| 5000 | LOG_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no" |
| 5001 | " buffers in the timed buffer queue"); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5002 | } |
| 5003 | |
John Grossman | 9fbdee1 | 2012-03-26 17:51:46 -0700 | [diff] [blame] | 5004 | done: |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5005 | buffer->raw = 0; |
| 5006 | buffer->frameCount = 0; |
| 5007 | } |
| 5008 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame^] | 5009 | size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5010 | Mutex::Autolock _l(mTimedBufferQueueLock); |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 5011 | return mFramesPendingInQueue; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer() |
| 5015 | : mPTS(0), mPosition(0) {} |
| 5016 | |
| 5017 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer( |
| 5018 | const sp<IMemory>& buffer, int64_t pts) |
| 5019 | : mBuffer(buffer), mPTS(pts), mPosition(0) {} |
| 5020 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5021 | // ---------------------------------------------------------------------------- |
| 5022 | |
| 5023 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 5024 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 5025 | RecordThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5026 | const sp<Client>& client, |
| 5027 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5028 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5029 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5030 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5031 | int sessionId) |
| 5032 | : TrackBase(thread, client, sampleRate, format, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5033 | channelMask, frameCount, 0 /*sharedBuffer*/, sessionId), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5034 | mOverflow(false) |
| 5035 | { |
| 5036 | if (mCblk != NULL) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5037 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
| 5038 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
| 5039 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
| 5040 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
| 5041 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
| 5042 | } else { |
| 5043 | mCblk->frameSize = sizeof(int8_t); |
| 5044 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 5049 | { |
| 5050 | sp<ThreadBase> thread = mThread.promote(); |
| 5051 | if (thread != 0) { |
| 5052 | AudioSystem::releaseInput(thread->id()); |
| 5053 | } |
| 5054 | } |
| 5055 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 5056 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5057 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5058 | { |
| 5059 | audio_track_cblk_t* cblk = this->cblk(); |
| 5060 | uint32_t framesAvail; |
| 5061 | uint32_t framesReq = buffer->frameCount; |
| 5062 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5063 | // Check if last stepServer failed, try to step now |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5064 | if (mStepServerFailed) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5065 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5066 | ALOGV("stepServer recovered"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5067 | mStepServerFailed = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | framesAvail = cblk->framesAvailable_l(); |
| 5071 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 5072 | if (CC_LIKELY(framesAvail)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5073 | uint32_t s = cblk->server; |
| 5074 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 5075 | |
| 5076 | if (framesReq > framesAvail) { |
| 5077 | framesReq = framesAvail; |
| 5078 | } |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 5079 | if (framesReq > bufferEnd - s) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5080 | framesReq = bufferEnd - s; |
| 5081 | } |
| 5082 | |
| 5083 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5084 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5085 | |
| 5086 | buffer->frameCount = framesReq; |
| 5087 | return NO_ERROR; |
| 5088 | } |
| 5089 | |
| 5090 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5091 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5092 | buffer->frameCount = 0; |
| 5093 | return NOT_ENOUGH_DATA; |
| 5094 | } |
| 5095 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5096 | status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5097 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5098 | { |
| 5099 | sp<ThreadBase> thread = mThread.promote(); |
| 5100 | if (thread != 0) { |
| 5101 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5102 | return recordThread->start(this, event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5103 | } else { |
| 5104 | return BAD_VALUE; |
| 5105 | } |
| 5106 | } |
| 5107 | |
| 5108 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 5109 | { |
| 5110 | sp<ThreadBase> thread = mThread.promote(); |
| 5111 | if (thread != 0) { |
| 5112 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 5113 | recordThread->stop(this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 5114 | TrackBase::reset(); |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 5115 | // Force overrun condition to avoid false overrun callback until first data is |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 5116 | // read from buffer |
| 5117 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 5122 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5123 | snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 5124 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5125 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5126 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5127 | mSessionId, |
| 5128 | mFrameCount, |
| 5129 | mState, |
| 5130 | mCblk->sampleRate, |
| 5131 | mCblk->server, |
| 5132 | mCblk->user); |
| 5133 | } |
| 5134 | |
| 5135 | |
| 5136 | // ---------------------------------------------------------------------------- |
| 5137 | |
| 5138 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 5139 | PlaybackThread *playbackThread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5140 | DuplicatingThread *sourceThread, |
| 5141 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5142 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5143 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5144 | int frameCount) |
Glenn Kasten | 73d2275 | 2012-03-19 13:38:30 -0700 | [diff] [blame] | 5145 | : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, |
| 5146 | NULL, 0, IAudioFlinger::TRACK_DEFAULT), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5147 | mActive(false), mSourceThread(sourceThread) |
| 5148 | { |
| 5149 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5150 | if (mCblk != NULL) { |
| 5151 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 5152 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5153 | mOutBuffer.frameCount = 0; |
| 5154 | playbackThread->mTracks.add(this); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5155 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5156 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 5157 | mCblk, mBuffer, mCblk->buffers, |
| 5158 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5159 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5160 | ALOGW("Error creating output track on thread %p", playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 5165 | { |
| 5166 | clearBufferQueue(); |
| 5167 | } |
| 5168 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5169 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5170 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5171 | { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5172 | status_t status = Track::start(event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5173 | if (status != NO_ERROR) { |
| 5174 | return status; |
| 5175 | } |
| 5176 | |
| 5177 | mActive = true; |
| 5178 | mRetryCount = 127; |
| 5179 | return status; |
| 5180 | } |
| 5181 | |
| 5182 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 5183 | { |
| 5184 | Track::stop(); |
| 5185 | clearBufferQueue(); |
| 5186 | mOutBuffer.frameCount = 0; |
| 5187 | mActive = false; |
| 5188 | } |
| 5189 | |
| 5190 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 5191 | { |
| 5192 | Buffer *pInBuffer; |
| 5193 | Buffer inBuffer; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5194 | uint32_t channelCount = mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5195 | bool outputBufferFull = false; |
| 5196 | inBuffer.frameCount = frames; |
| 5197 | inBuffer.i16 = data; |
| 5198 | |
| 5199 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 5200 | |
| 5201 | if (!mActive && frames != 0) { |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5202 | start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5203 | sp<ThreadBase> thread = mThread.promote(); |
| 5204 | if (thread != 0) { |
| 5205 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 5206 | if (mCblk->frameCount > frames){ |
| 5207 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 5208 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 5209 | pInBuffer = new Buffer; |
| 5210 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 5211 | pInBuffer->frameCount = startFrames; |
| 5212 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5213 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 5214 | mBufferQueue.add(pInBuffer); |
| 5215 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5216 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5217 | } |
| 5218 | } |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | while (waitTimeLeftMs) { |
| 5223 | // First write pending buffers, then new data |
| 5224 | if (mBufferQueue.size()) { |
| 5225 | pInBuffer = mBufferQueue.itemAt(0); |
| 5226 | } else { |
| 5227 | pInBuffer = &inBuffer; |
| 5228 | } |
| 5229 | |
| 5230 | if (pInBuffer->frameCount == 0) { |
| 5231 | break; |
| 5232 | } |
| 5233 | |
| 5234 | if (mOutBuffer.frameCount == 0) { |
| 5235 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 5236 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5237 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5238 | ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5239 | outputBufferFull = true; |
| 5240 | break; |
| 5241 | } |
| 5242 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 5243 | if (waitTimeLeftMs >= waitTimeMs) { |
| 5244 | waitTimeLeftMs -= waitTimeMs; |
| 5245 | } else { |
| 5246 | waitTimeLeftMs = 0; |
| 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 5251 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 5252 | mCblk->stepUser(outFrames); |
| 5253 | pInBuffer->frameCount -= outFrames; |
| 5254 | pInBuffer->i16 += outFrames * channelCount; |
| 5255 | mOutBuffer.frameCount -= outFrames; |
| 5256 | mOutBuffer.i16 += outFrames * channelCount; |
| 5257 | |
| 5258 | if (pInBuffer->frameCount == 0) { |
| 5259 | if (mBufferQueue.size()) { |
| 5260 | mBufferQueue.removeAt(0); |
| 5261 | delete [] pInBuffer->mBuffer; |
| 5262 | delete pInBuffer; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5263 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5264 | } else { |
| 5265 | break; |
| 5266 | } |
| 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 5271 | if (inBuffer.frameCount) { |
| 5272 | sp<ThreadBase> thread = mThread.promote(); |
| 5273 | if (thread != 0 && !thread->standby()) { |
| 5274 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 5275 | pInBuffer = new Buffer; |
| 5276 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 5277 | pInBuffer->frameCount = inBuffer.frameCount; |
| 5278 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5279 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 5280 | mBufferQueue.add(pInBuffer); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5281 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5282 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5283 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5284 | } |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 5289 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 5290 | // by output mixer. |
| 5291 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 5292 | if (mCblk->user < mCblk->frameCount) { |
| 5293 | frames = mCblk->frameCount - mCblk->user; |
| 5294 | pInBuffer = new Buffer; |
| 5295 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 5296 | pInBuffer->frameCount = frames; |
| 5297 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 5298 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 5299 | mBufferQueue.add(pInBuffer); |
| 5300 | } else if (mActive) { |
| 5301 | stop(); |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | return outputBufferFull; |
| 5306 | } |
| 5307 | |
| 5308 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 5309 | { |
| 5310 | int active; |
| 5311 | status_t result; |
| 5312 | audio_track_cblk_t* cblk = mCblk; |
| 5313 | uint32_t framesReq = buffer->frameCount; |
| 5314 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5315 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5316 | buffer->frameCount = 0; |
| 5317 | |
| 5318 | uint32_t framesAvail = cblk->framesAvailable(); |
| 5319 | |
| 5320 | |
| 5321 | if (framesAvail == 0) { |
| 5322 | Mutex::Autolock _l(cblk->lock); |
| 5323 | goto start_loop_here; |
| 5324 | while (framesAvail == 0) { |
| 5325 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 5326 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5327 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5328 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5329 | } |
| 5330 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 5331 | if (result != NO_ERROR) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5332 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5333 | } |
| 5334 | // read the server count again |
| 5335 | start_loop_here: |
| 5336 | framesAvail = cblk->framesAvailable_l(); |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | // if (framesAvail < framesReq) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 5341 | // return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5342 | // } |
| 5343 | |
| 5344 | if (framesReq > framesAvail) { |
| 5345 | framesReq = framesAvail; |
| 5346 | } |
| 5347 | |
| 5348 | uint32_t u = cblk->user; |
| 5349 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 5350 | |
Marco Nelissen | a1472d9 | 2012-03-30 14:36:54 -0700 | [diff] [blame] | 5351 | if (framesReq > bufferEnd - u) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5352 | framesReq = bufferEnd - u; |
| 5353 | } |
| 5354 | |
| 5355 | buffer->frameCount = framesReq; |
| 5356 | buffer->raw = (void *)cblk->buffer(u); |
| 5357 | return NO_ERROR; |
| 5358 | } |
| 5359 | |
| 5360 | |
| 5361 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 5362 | { |
| 5363 | size_t size = mBufferQueue.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5364 | |
| 5365 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5366 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5367 | delete [] pBuffer->mBuffer; |
| 5368 | delete pBuffer; |
| 5369 | } |
| 5370 | mBufferQueue.clear(); |
| 5371 | } |
| 5372 | |
| 5373 | // ---------------------------------------------------------------------------- |
| 5374 | |
| 5375 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 5376 | : RefBase(), |
| 5377 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 5378 | // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5379 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5380 | mPid(pid), |
| 5381 | mTimedTrackCount(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5382 | { |
| 5383 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 5384 | } |
| 5385 | |
| 5386 | // Client destructor must be called with AudioFlinger::mLock held |
| 5387 | AudioFlinger::Client::~Client() |
| 5388 | { |
| 5389 | mAudioFlinger->removeClient_l(mPid); |
| 5390 | } |
| 5391 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 5392 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5393 | { |
| 5394 | return mMemoryDealer; |
| 5395 | } |
| 5396 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5397 | // Reserve one of the limited slots for a timed audio track associated |
| 5398 | // with this client |
| 5399 | bool AudioFlinger::Client::reserveTimedTrack() |
| 5400 | { |
| 5401 | const int kMaxTimedTracksPerClient = 4; |
| 5402 | |
| 5403 | Mutex::Autolock _l(mTimedTrackLock); |
| 5404 | |
| 5405 | if (mTimedTrackCount >= kMaxTimedTracksPerClient) { |
| 5406 | ALOGW("can not create timed track - pid %d has exceeded the limit", |
| 5407 | mPid); |
| 5408 | return false; |
| 5409 | } |
| 5410 | |
| 5411 | mTimedTrackCount++; |
| 5412 | return true; |
| 5413 | } |
| 5414 | |
| 5415 | // Release a slot for a timed audio track |
| 5416 | void AudioFlinger::Client::releaseTimedTrack() |
| 5417 | { |
| 5418 | Mutex::Autolock _l(mTimedTrackLock); |
| 5419 | mTimedTrackCount--; |
| 5420 | } |
| 5421 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5422 | // ---------------------------------------------------------------------------- |
| 5423 | |
| 5424 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 5425 | const sp<IAudioFlingerClient>& client, |
| 5426 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5427 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5428 | { |
| 5429 | } |
| 5430 | |
| 5431 | AudioFlinger::NotificationClient::~NotificationClient() |
| 5432 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 5436 | { |
| 5437 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5438 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | // ---------------------------------------------------------------------------- |
| 5442 | |
| 5443 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 5444 | : BnAudioTrack(), |
| 5445 | mTrack(track) |
| 5446 | { |
| 5447 | } |
| 5448 | |
| 5449 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 5450 | // just stop the track on deletion, associated resources |
| 5451 | // will be freed from the main thread once all pending buffers have |
| 5452 | // been played. Unless it's not in the active track list, in which |
| 5453 | // case we free everything now... |
| 5454 | mTrack->destroy(); |
| 5455 | } |
| 5456 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 5457 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 5458 | return mTrack->getCblk(); |
| 5459 | } |
| 5460 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5461 | status_t AudioFlinger::TrackHandle::start() { |
| 5462 | return mTrack->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | void AudioFlinger::TrackHandle::stop() { |
| 5466 | mTrack->stop(); |
| 5467 | } |
| 5468 | |
| 5469 | void AudioFlinger::TrackHandle::flush() { |
| 5470 | mTrack->flush(); |
| 5471 | } |
| 5472 | |
| 5473 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 5474 | mTrack->mute(e); |
| 5475 | } |
| 5476 | |
| 5477 | void AudioFlinger::TrackHandle::pause() { |
| 5478 | mTrack->pause(); |
| 5479 | } |
| 5480 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5481 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 5482 | { |
| 5483 | return mTrack->attachAuxEffect(EffectId); |
| 5484 | } |
| 5485 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5486 | status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size, |
| 5487 | sp<IMemory>* buffer) { |
| 5488 | if (!mTrack->isTimedTrack()) |
| 5489 | return INVALID_OPERATION; |
| 5490 | |
| 5491 | PlaybackThread::TimedTrack* tt = |
| 5492 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5493 | return tt->allocateTimedBuffer(size, buffer); |
| 5494 | } |
| 5495 | |
| 5496 | status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer, |
| 5497 | int64_t pts) { |
| 5498 | if (!mTrack->isTimedTrack()) |
| 5499 | return INVALID_OPERATION; |
| 5500 | |
| 5501 | PlaybackThread::TimedTrack* tt = |
| 5502 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5503 | return tt->queueTimedBuffer(buffer, pts); |
| 5504 | } |
| 5505 | |
| 5506 | status_t AudioFlinger::TrackHandle::setMediaTimeTransform( |
| 5507 | const LinearTransform& xform, int target) { |
| 5508 | |
| 5509 | if (!mTrack->isTimedTrack()) |
| 5510 | return INVALID_OPERATION; |
| 5511 | |
| 5512 | PlaybackThread::TimedTrack* tt = |
| 5513 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 5514 | return tt->setMediaTimeTransform( |
| 5515 | xform, static_cast<TimedAudioTrack::TargetTimeline>(target)); |
| 5516 | } |
| 5517 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5518 | status_t AudioFlinger::TrackHandle::onTransact( |
| 5519 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 5520 | { |
| 5521 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 5522 | } |
| 5523 | |
| 5524 | // ---------------------------------------------------------------------------- |
| 5525 | |
| 5526 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 5527 | pid_t pid, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5528 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5529 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5530 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5531 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5532 | int frameCount, |
Glenn Kasten | a075db4 | 2012-03-06 11:22:44 -0800 | [diff] [blame] | 5533 | IAudioFlinger::track_flags_t flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5534 | int *sessionId, |
| 5535 | status_t *status) |
| 5536 | { |
| 5537 | sp<RecordThread::RecordTrack> recordTrack; |
| 5538 | sp<RecordHandle> recordHandle; |
| 5539 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5540 | status_t lStatus; |
| 5541 | RecordThread *thread; |
| 5542 | size_t inFrameCount; |
| 5543 | int lSessionId; |
| 5544 | |
| 5545 | // check calling permissions |
| 5546 | if (!recordingAllowed()) { |
| 5547 | lStatus = PERMISSION_DENIED; |
| 5548 | goto Exit; |
| 5549 | } |
| 5550 | |
| 5551 | // add client to list |
| 5552 | { // scope for mLock |
| 5553 | Mutex::Autolock _l(mLock); |
| 5554 | thread = checkRecordThread_l(input); |
| 5555 | if (thread == NULL) { |
| 5556 | lStatus = BAD_VALUE; |
| 5557 | goto Exit; |
| 5558 | } |
| 5559 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 5560 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5561 | |
| 5562 | // If no audio session id is provided, create one here |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5563 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5564 | lSessionId = *sessionId; |
| 5565 | } else { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5566 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5567 | if (sessionId != NULL) { |
| 5568 | *sessionId = lSessionId; |
| 5569 | } |
| 5570 | } |
| 5571 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5572 | recordTrack = thread->createRecordTrack_l(client, |
| 5573 | sampleRate, |
| 5574 | format, |
| 5575 | channelMask, |
| 5576 | frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5577 | lSessionId, |
| 5578 | &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5579 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5580 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5581 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 5582 | // destructor is called by the TrackBase destructor with mLock held |
| 5583 | client.clear(); |
| 5584 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5585 | goto Exit; |
| 5586 | } |
| 5587 | |
| 5588 | // return to handle to client |
| 5589 | recordHandle = new RecordHandle(recordTrack); |
| 5590 | lStatus = NO_ERROR; |
| 5591 | |
| 5592 | Exit: |
| 5593 | if (status) { |
| 5594 | *status = lStatus; |
| 5595 | } |
| 5596 | return recordHandle; |
| 5597 | } |
| 5598 | |
| 5599 | // ---------------------------------------------------------------------------- |
| 5600 | |
| 5601 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 5602 | : BnAudioRecord(), |
| 5603 | mRecordTrack(recordTrack) |
| 5604 | { |
| 5605 | } |
| 5606 | |
| 5607 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 5608 | stop(); |
| 5609 | } |
| 5610 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 5611 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 5612 | return mRecordTrack->getCblk(); |
| 5613 | } |
| 5614 | |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5615 | status_t AudioFlinger::RecordHandle::start(int event, int triggerSession) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5616 | ALOGV("RecordHandle::start()"); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5617 | return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5621 | ALOGV("RecordHandle::stop()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5622 | mRecordTrack->stop(); |
| 5623 | } |
| 5624 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5625 | status_t AudioFlinger::RecordHandle::onTransact( |
| 5626 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 5627 | { |
| 5628 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 5629 | } |
| 5630 | |
| 5631 | // ---------------------------------------------------------------------------- |
| 5632 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5633 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5634 | AudioStreamIn *input, |
| 5635 | uint32_t sampleRate, |
| 5636 | uint32_t channels, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5637 | audio_io_handle_t id, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5638 | uint32_t device) : |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 5639 | ThreadBase(audioFlinger, id, device, RECORD), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5640 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL), |
| 5641 | // mRsmpInIndex and mInputBytes set by readInputParameters() |
| 5642 | mReqChannelCount(popcount(channels)), |
| 5643 | mReqSampleRate(sampleRate) |
| 5644 | // mBytesRead is only meaningful while active, and so is cleared in start() |
| 5645 | // (but might be better to also clear here for dump?) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5646 | { |
Glenn Kasten | 480b468 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 5647 | snprintf(mName, kNameLength, "AudioIn_%X", id); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5648 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5649 | readInputParameters(); |
| 5650 | } |
| 5651 | |
| 5652 | |
| 5653 | AudioFlinger::RecordThread::~RecordThread() |
| 5654 | { |
| 5655 | delete[] mRsmpInBuffer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 5656 | delete mResampler; |
| 5657 | delete[] mRsmpOutBuffer; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | void AudioFlinger::RecordThread::onFirstRef() |
| 5661 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5662 | run(mName, PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5663 | } |
| 5664 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5665 | status_t AudioFlinger::RecordThread::readyToRun() |
| 5666 | { |
| 5667 | status_t status = initCheck(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5668 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5669 | return status; |
| 5670 | } |
| 5671 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5672 | bool AudioFlinger::RecordThread::threadLoop() |
| 5673 | { |
| 5674 | AudioBufferProvider::Buffer buffer; |
| 5675 | sp<RecordTrack> activeTrack; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5676 | Vector< sp<EffectChain> > effectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5677 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5678 | nsecs_t lastWarning = 0; |
| 5679 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5680 | acquireWakeLock(); |
| 5681 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5682 | // start recording |
| 5683 | while (!exitPending()) { |
| 5684 | |
| 5685 | processConfigEvents(); |
| 5686 | |
| 5687 | { // scope for mLock |
| 5688 | Mutex::Autolock _l(mLock); |
| 5689 | checkForNewParameters_l(); |
| 5690 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 5691 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5692 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5693 | mStandby = true; |
| 5694 | } |
| 5695 | |
| 5696 | if (exitPending()) break; |
| 5697 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5698 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5699 | ALOGV("RecordThread: loop stopping"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5700 | // go to sleep |
| 5701 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5702 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5703 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5704 | continue; |
| 5705 | } |
| 5706 | if (mActiveTrack != 0) { |
| 5707 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 5708 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5709 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5710 | mStandby = true; |
| 5711 | } |
| 5712 | mActiveTrack.clear(); |
| 5713 | mStartStopCond.broadcast(); |
| 5714 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 5715 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 5716 | mActiveTrack.clear(); |
| 5717 | mStartStopCond.broadcast(); |
| 5718 | } else if (mBytesRead != 0) { |
| 5719 | // record start succeeds only if first read from audio input |
| 5720 | // succeeds |
| 5721 | if (mBytesRead > 0) { |
| 5722 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 5723 | } else { |
| 5724 | mActiveTrack.clear(); |
| 5725 | } |
| 5726 | mStartStopCond.broadcast(); |
| 5727 | } |
| 5728 | mStandby = false; |
| 5729 | } |
| 5730 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5731 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5732 | } |
| 5733 | |
| 5734 | if (mActiveTrack != 0) { |
| 5735 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 5736 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5737 | unlockEffectChains(effectChains); |
| 5738 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5739 | continue; |
| 5740 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5741 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 5742 | effectChains[i]->process_l(); |
| 5743 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5744 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5745 | buffer.frameCount = mFrameCount; |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 5746 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5747 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5748 | if (mResampler == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5749 | // no resampling |
| 5750 | while (framesOut) { |
| 5751 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 5752 | if (framesIn) { |
| 5753 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 5754 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 5755 | if (framesIn > framesOut) |
| 5756 | framesIn = framesOut; |
| 5757 | mRsmpInIndex += framesIn; |
| 5758 | framesOut -= framesIn; |
| 5759 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5760 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5761 | memcpy(dst, src, framesIn * mFrameSize); |
| 5762 | } else { |
| 5763 | int16_t *src16 = (int16_t *)src; |
| 5764 | int16_t *dst16 = (int16_t *)dst; |
| 5765 | if (mChannelCount == 1) { |
| 5766 | while (framesIn--) { |
| 5767 | *dst16++ = *src16; |
| 5768 | *dst16++ = *src16++; |
| 5769 | } |
| 5770 | } else { |
| 5771 | while (framesIn--) { |
| 5772 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 5773 | src16 += 2; |
| 5774 | } |
| 5775 | } |
| 5776 | } |
| 5777 | } |
| 5778 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 5779 | if (framesOut == mFrameCount && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5780 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5781 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5782 | framesOut = 0; |
| 5783 | } else { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5784 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5785 | mRsmpInIndex = 0; |
| 5786 | } |
| 5787 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5788 | ALOGE("Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5789 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 5790 | // Force input into standby so that it tries to |
| 5791 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5792 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5793 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5794 | } |
| 5795 | mRsmpInIndex = mFrameCount; |
| 5796 | framesOut = 0; |
| 5797 | buffer.frameCount = 0; |
| 5798 | } |
| 5799 | } |
| 5800 | } |
| 5801 | } else { |
| 5802 | // resampling |
| 5803 | |
| 5804 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 5805 | // alter output frame count as if we were expecting stereo samples |
| 5806 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 5807 | framesOut >>= 1; |
| 5808 | } |
| 5809 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 5810 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 5811 | // are 32 bit aligned which should be always true. |
| 5812 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 5813 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5814 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 5815 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 5816 | int16_t *dst = buffer.i16; |
| 5817 | while (framesOut--) { |
| 5818 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 5819 | src += 2; |
| 5820 | } |
| 5821 | } else { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 5822 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5823 | } |
| 5824 | |
| 5825 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5826 | if (mFramestoDrop == 0) { |
| 5827 | mActiveTrack->releaseBuffer(&buffer); |
| 5828 | } else { |
| 5829 | if (mFramestoDrop > 0) { |
| 5830 | mFramestoDrop -= buffer.frameCount; |
| 5831 | if (mFramestoDrop < 0) { |
| 5832 | mFramestoDrop = 0; |
| 5833 | } |
| 5834 | } |
| 5835 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5836 | mActiveTrack->overflow(); |
| 5837 | } |
| 5838 | // client isn't retrieving buffers fast enough |
| 5839 | else { |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5840 | if (!mActiveTrack->setOverflow()) { |
| 5841 | nsecs_t now = systemTime(); |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 5842 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5843 | ALOGW("RecordThread: buffer overflow"); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 5844 | lastWarning = now; |
| 5845 | } |
| 5846 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5847 | // Release the processor for a while before asking for a new buffer. |
| 5848 | // This will give the application more chance to read from the buffer and |
| 5849 | // clear the overflow. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5850 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5851 | } |
| 5852 | } |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 5853 | // enable changes in effect chain |
| 5854 | unlockEffectChains(effectChains); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5855 | effectChains.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5859 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5860 | } |
| 5861 | mActiveTrack.clear(); |
| 5862 | |
| 5863 | mStartStopCond.broadcast(); |
| 5864 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 5865 | releaseWakeLock(); |
| 5866 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5867 | ALOGV("RecordThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5868 | return false; |
| 5869 | } |
| 5870 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5871 | |
| 5872 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 5873 | const sp<AudioFlinger::Client>& client, |
| 5874 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5875 | audio_format_t format, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5876 | int channelMask, |
| 5877 | int frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5878 | int sessionId, |
| 5879 | status_t *status) |
| 5880 | { |
| 5881 | sp<RecordTrack> track; |
| 5882 | status_t lStatus; |
| 5883 | |
| 5884 | lStatus = initCheck(); |
| 5885 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5886 | ALOGE("Audio driver not initialized."); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5887 | goto Exit; |
| 5888 | } |
| 5889 | |
| 5890 | { // scope for mLock |
| 5891 | Mutex::Autolock _l(mLock); |
| 5892 | |
| 5893 | track = new RecordTrack(this, client, sampleRate, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame] | 5894 | format, channelMask, frameCount, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5895 | |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 5896 | if (track->getCblk() == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5897 | lStatus = NO_MEMORY; |
| 5898 | goto Exit; |
| 5899 | } |
| 5900 | |
| 5901 | mTrack = track.get(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5902 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5903 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 5904 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5905 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 5906 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5907 | } |
| 5908 | lStatus = NO_ERROR; |
| 5909 | |
| 5910 | Exit: |
| 5911 | if (status) { |
| 5912 | *status = lStatus; |
| 5913 | } |
| 5914 | return track; |
| 5915 | } |
| 5916 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5917 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 5918 | AudioSystem::sync_event_t event, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5919 | int triggerSession) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5920 | { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 5921 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 5922 | sp<ThreadBase> strongMe = this; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5923 | status_t status = NO_ERROR; |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5924 | |
| 5925 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
| 5926 | mSyncStartEvent.clear(); |
| 5927 | mFramestoDrop = 0; |
| 5928 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
| 5929 | mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
| 5930 | triggerSession, |
| 5931 | recordTrack->sessionId(), |
| 5932 | syncStartEventCallback, |
| 5933 | this); |
| 5934 | mFramestoDrop = -1; |
| 5935 | } |
| 5936 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5937 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 5938 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5939 | if (mActiveTrack != 0) { |
| 5940 | if (recordTrack != mActiveTrack.get()) { |
| 5941 | status = -EBUSY; |
| 5942 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 5943 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 5944 | } |
| 5945 | return status; |
| 5946 | } |
| 5947 | |
| 5948 | recordTrack->mState = TrackBase::IDLE; |
| 5949 | mActiveTrack = recordTrack; |
| 5950 | mLock.unlock(); |
| 5951 | status_t status = AudioSystem::startInput(mId); |
| 5952 | mLock.lock(); |
| 5953 | if (status != NO_ERROR) { |
| 5954 | mActiveTrack.clear(); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5955 | clearSyncStartEvent(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5956 | return status; |
| 5957 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5958 | mRsmpInIndex = mFrameCount; |
| 5959 | mBytesRead = 0; |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 5960 | if (mResampler != NULL) { |
| 5961 | mResampler->reset(); |
| 5962 | } |
| 5963 | mActiveTrack->mState = TrackBase::RESUMING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5964 | // signal thread to start |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5965 | ALOGV("Signal record thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5966 | mWaitWorkCV.signal(); |
| 5967 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 5968 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5969 | mActiveTrack.clear(); |
| 5970 | status = INVALID_OPERATION; |
| 5971 | goto startError; |
| 5972 | } |
| 5973 | mStartStopCond.wait(mLock); |
| 5974 | if (mActiveTrack == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5975 | ALOGV("Record failed to start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5976 | status = BAD_VALUE; |
| 5977 | goto startError; |
| 5978 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5979 | ALOGV("Record started OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5980 | return status; |
| 5981 | } |
| 5982 | startError: |
| 5983 | AudioSystem::stopInput(mId); |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5984 | clearSyncStartEvent(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5985 | return status; |
| 5986 | } |
| 5987 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 5988 | void AudioFlinger::RecordThread::clearSyncStartEvent() |
| 5989 | { |
| 5990 | if (mSyncStartEvent != 0) { |
| 5991 | mSyncStartEvent->cancel(); |
| 5992 | } |
| 5993 | mSyncStartEvent.clear(); |
| 5994 | } |
| 5995 | |
| 5996 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 5997 | { |
| 5998 | sp<SyncEvent> strongEvent = event.promote(); |
| 5999 | |
| 6000 | if (strongEvent != 0) { |
| 6001 | RecordThread *me = (RecordThread *)strongEvent->cookie(); |
| 6002 | me->handleSyncStartEvent(strongEvent); |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | void AudioFlinger::RecordThread::handleSyncStartEvent(const sp<SyncEvent>& event) |
| 6007 | { |
| 6008 | ALOGV("handleSyncStartEvent() mActiveTrack %p session %d event->listenerSession() %d", |
| 6009 | mActiveTrack.get(), |
| 6010 | mActiveTrack.get() ? mActiveTrack->sessionId() : 0, |
| 6011 | event->listenerSession()); |
| 6012 | |
| 6013 | if (mActiveTrack != 0 && |
| 6014 | event == mSyncStartEvent) { |
| 6015 | // TODO: use actual buffer filling status instead of 2 buffers when info is available |
| 6016 | // from audio HAL |
| 6017 | mFramestoDrop = mFrameCount * 2; |
| 6018 | mSyncStartEvent.clear(); |
| 6019 | } |
| 6020 | } |
| 6021 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6022 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6023 | ALOGV("RecordThread::stop"); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6024 | sp<ThreadBase> strongMe = this; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6025 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 6026 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6027 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 6028 | mActiveTrack->mState = TrackBase::PAUSING; |
| 6029 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6030 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6031 | return; |
| 6032 | } |
| 6033 | mStartStopCond.wait(mLock); |
| 6034 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 6035 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 6036 | mLock.unlock(); |
| 6037 | AudioSystem::stopInput(mId); |
| 6038 | mLock.lock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6039 | ALOGV("Record stopped OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6040 | } |
| 6041 | } |
| 6042 | } |
| 6043 | } |
| 6044 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6045 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event) |
| 6046 | { |
| 6047 | return false; |
| 6048 | } |
| 6049 | |
| 6050 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event) |
| 6051 | { |
| 6052 | if (!isValidSyncEvent(event)) { |
| 6053 | return BAD_VALUE; |
| 6054 | } |
| 6055 | |
| 6056 | Mutex::Autolock _l(mLock); |
| 6057 | |
| 6058 | if (mTrack != NULL && event->triggerSession() == mTrack->sessionId()) { |
| 6059 | mTrack->setSyncEvent(event); |
| 6060 | return NO_ERROR; |
| 6061 | } |
| 6062 | return NAME_NOT_FOUND; |
| 6063 | } |
| 6064 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6065 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6066 | { |
| 6067 | const size_t SIZE = 256; |
| 6068 | char buffer[SIZE]; |
| 6069 | String8 result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6070 | |
| 6071 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 6072 | result.append(buffer); |
| 6073 | |
| 6074 | if (mActiveTrack != 0) { |
| 6075 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6076 | result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6077 | mActiveTrack->dump(buffer, SIZE); |
| 6078 | result.append(buffer); |
| 6079 | |
| 6080 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 6081 | result.append(buffer); |
| 6082 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 6083 | result.append(buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6084 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6085 | result.append(buffer); |
| 6086 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 6087 | result.append(buffer); |
| 6088 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 6089 | result.append(buffer); |
| 6090 | |
| 6091 | |
| 6092 | } else { |
| 6093 | result.append("No record client\n"); |
| 6094 | } |
| 6095 | write(fd, result.string(), result.size()); |
| 6096 | |
| 6097 | dumpBase(fd, args); |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 6098 | dumpEffectChains(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6099 | |
| 6100 | return NO_ERROR; |
| 6101 | } |
| 6102 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6103 | // AudioBufferProvider interface |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 6104 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6105 | { |
| 6106 | size_t framesReq = buffer->frameCount; |
| 6107 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 6108 | int channelCount; |
| 6109 | |
| 6110 | if (framesReady == 0) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6111 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6112 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6113 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6114 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 6115 | // Force input into standby so that it tries to |
| 6116 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6117 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6118 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6119 | } |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6120 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6121 | buffer->frameCount = 0; |
| 6122 | return NOT_ENOUGH_DATA; |
| 6123 | } |
| 6124 | mRsmpInIndex = 0; |
| 6125 | framesReady = mFrameCount; |
| 6126 | } |
| 6127 | |
| 6128 | if (framesReq > framesReady) { |
| 6129 | framesReq = framesReady; |
| 6130 | } |
| 6131 | |
| 6132 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 6133 | channelCount = 1; |
| 6134 | } else { |
| 6135 | channelCount = 2; |
| 6136 | } |
| 6137 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 6138 | buffer->frameCount = framesReq; |
| 6139 | return NO_ERROR; |
| 6140 | } |
| 6141 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 6142 | // AudioBufferProvider interface |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6143 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 6144 | { |
| 6145 | mRsmpInIndex += buffer->frameCount; |
| 6146 | buffer->frameCount = 0; |
| 6147 | } |
| 6148 | |
| 6149 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 6150 | { |
| 6151 | bool reconfig = false; |
| 6152 | |
| 6153 | while (!mNewParameters.isEmpty()) { |
| 6154 | status_t status = NO_ERROR; |
| 6155 | String8 keyValuePair = mNewParameters[0]; |
| 6156 | AudioParameter param = AudioParameter(keyValuePair); |
| 6157 | int value; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6158 | audio_format_t reqFormat = mFormat; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6159 | int reqSamplingRate = mReqSampleRate; |
| 6160 | int reqChannelCount = mReqChannelCount; |
| 6161 | |
| 6162 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6163 | reqSamplingRate = value; |
| 6164 | reconfig = true; |
| 6165 | } |
| 6166 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 6167 | reqFormat = (audio_format_t) value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6168 | reconfig = true; |
| 6169 | } |
| 6170 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6171 | reqChannelCount = popcount(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6172 | reconfig = true; |
| 6173 | } |
| 6174 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6175 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 6176 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6177 | // if frame count is changed after track creation |
| 6178 | if (mActiveTrack != 0) { |
| 6179 | status = INVALID_OPERATION; |
| 6180 | } else { |
| 6181 | reconfig = true; |
| 6182 | } |
| 6183 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6184 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 6185 | // forward device change to effects that have requested to be |
| 6186 | // aware of attached audio device. |
| 6187 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6188 | mEffectChains[i]->setDevice_l(value); |
| 6189 | } |
| 6190 | // store input device and output device but do not forward output device to audio HAL. |
| 6191 | // Note that status is ignored by the caller for output device |
| 6192 | // (see AudioFlinger::setParameters() |
| 6193 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 6194 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 6195 | status = BAD_VALUE; |
| 6196 | } else { |
| 6197 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6198 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6199 | if (mTrack != NULL) { |
| 6200 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 6201 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6202 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 6203 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 6204 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6205 | } |
| 6206 | mDevice |= (uint32_t)value; |
| 6207 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6208 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6209 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6210 | if (status == INVALID_OPERATION) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6211 | mInput->stream->common.standby(&mInput->stream->common); |
| 6212 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6213 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6214 | } |
| 6215 | if (reconfig) { |
| 6216 | if (status == BAD_VALUE && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6217 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6218 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6219 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
Glenn Kasten | 53d76db | 2012-03-08 12:32:47 -0800 | [diff] [blame] | 6220 | popcount(mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && |
| 6221 | (reqChannelCount <= FCC_2)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6222 | status = NO_ERROR; |
| 6223 | } |
| 6224 | if (status == NO_ERROR) { |
| 6225 | readInputParameters(); |
| 6226 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 6227 | } |
| 6228 | } |
| 6229 | } |
| 6230 | |
| 6231 | mNewParameters.removeAt(0); |
| 6232 | |
| 6233 | mParamStatus = status; |
| 6234 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 6235 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 6236 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 6237 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6238 | } |
| 6239 | return reconfig; |
| 6240 | } |
| 6241 | |
| 6242 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6243 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6244 | char *s; |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6245 | String8 out_s8 = String8(); |
| 6246 | |
| 6247 | Mutex::Autolock _l(mLock); |
| 6248 | if (initCheck() != NO_ERROR) { |
| 6249 | return out_s8; |
| 6250 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6251 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6252 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6253 | out_s8 = String8(s); |
| 6254 | free(s); |
| 6255 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6256 | } |
| 6257 | |
| 6258 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 6259 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6260 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6261 | |
| 6262 | switch (event) { |
| 6263 | case AudioSystem::INPUT_OPENED: |
| 6264 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6265 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6266 | desc.samplingRate = mSampleRate; |
| 6267 | desc.format = mFormat; |
| 6268 | desc.frameCount = mFrameCount; |
| 6269 | desc.latency = 0; |
| 6270 | param2 = &desc; |
| 6271 | break; |
| 6272 | |
| 6273 | case AudioSystem::INPUT_CLOSED: |
| 6274 | default: |
| 6275 | break; |
| 6276 | } |
| 6277 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 6278 | } |
| 6279 | |
| 6280 | void AudioFlinger::RecordThread::readInputParameters() |
| 6281 | { |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 6282 | delete mRsmpInBuffer; |
| 6283 | // mRsmpInBuffer is always assigned a new[] below |
| 6284 | delete mRsmpOutBuffer; |
| 6285 | mRsmpOutBuffer = NULL; |
| 6286 | delete mResampler; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 6287 | mResampler = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6288 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6289 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 6290 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 6291 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6292 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 6293 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6294 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6295 | mFrameCount = mInputBytes / mFrameSize; |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 6296 | mNormalFrameCount = mFrameCount; // not used by record, but used by input effects |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6297 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 6298 | |
Glenn Kasten | 53d76db | 2012-03-08 12:32:47 -0800 | [diff] [blame] | 6299 | if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6300 | { |
| 6301 | int channelCount; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6302 | // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 6303 | // stereo to mono post process as the resampler always outputs stereo. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6304 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 6305 | channelCount = 1; |
| 6306 | } else { |
| 6307 | channelCount = 2; |
| 6308 | } |
| 6309 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 6310 | mResampler->setSampleRate(mSampleRate); |
| 6311 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 6312 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 6313 | |
| 6314 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 6315 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 6316 | mFrameCount >>= 1; |
| 6317 | } |
| 6318 | |
| 6319 | } |
| 6320 | mRsmpInIndex = mFrameCount; |
| 6321 | } |
| 6322 | |
| 6323 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 6324 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6325 | Mutex::Autolock _l(mLock); |
| 6326 | if (initCheck() != NO_ERROR) { |
| 6327 | return 0; |
| 6328 | } |
| 6329 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6330 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6331 | } |
| 6332 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6333 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 6334 | { |
| 6335 | Mutex::Autolock _l(mLock); |
| 6336 | uint32_t result = 0; |
| 6337 | if (getEffectChain_l(sessionId) != 0) { |
| 6338 | result = EFFECT_SESSION; |
| 6339 | } |
| 6340 | |
| 6341 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 6342 | result |= TRACK_SESSION; |
| 6343 | } |
| 6344 | |
| 6345 | return result; |
| 6346 | } |
| 6347 | |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 6348 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 6349 | { |
| 6350 | Mutex::Autolock _l(mLock); |
| 6351 | return mTrack; |
| 6352 | } |
| 6353 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 6354 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6355 | { |
| 6356 | Mutex::Autolock _l(mLock); |
| 6357 | return mInput; |
| 6358 | } |
| 6359 | |
| 6360 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6361 | { |
| 6362 | Mutex::Autolock _l(mLock); |
| 6363 | AudioStreamIn *input = mInput; |
| 6364 | mInput = NULL; |
| 6365 | return input; |
| 6366 | } |
| 6367 | |
| 6368 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Glenn Kasten | 0bf65bd | 2012-02-28 18:32:53 -0800 | [diff] [blame] | 6369 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6370 | { |
| 6371 | if (mInput == NULL) { |
| 6372 | return NULL; |
| 6373 | } |
| 6374 | return &mInput->stream->common; |
| 6375 | } |
| 6376 | |
| 6377 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6378 | // ---------------------------------------------------------------------------- |
| 6379 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6380 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 6381 | { |
| 6382 | if (!settingsAllowed()) { |
| 6383 | return 0; |
| 6384 | } |
| 6385 | Mutex::Autolock _l(mLock); |
| 6386 | return loadHwModule_l(name); |
| 6387 | } |
| 6388 | |
| 6389 | // loadHwModule_l() must be called with AudioFlinger::mLock held |
| 6390 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 6391 | { |
| 6392 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 6393 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 6394 | ALOGW("loadHwModule() module %s already loaded", name); |
| 6395 | return mAudioHwDevs.keyAt(i); |
| 6396 | } |
| 6397 | } |
| 6398 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6399 | audio_hw_device_t *dev; |
| 6400 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6401 | int rc = load_audio_interface(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6402 | if (rc) { |
| 6403 | ALOGI("loadHwModule() error %d loading module %s ", rc, name); |
| 6404 | return 0; |
| 6405 | } |
| 6406 | |
| 6407 | mHardwareStatus = AUDIO_HW_INIT; |
| 6408 | rc = dev->init_check(dev); |
| 6409 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6410 | if (rc) { |
| 6411 | ALOGI("loadHwModule() init check error %d for module %s ", rc, name); |
| 6412 | return 0; |
| 6413 | } |
| 6414 | |
| 6415 | if ((mMasterVolumeSupportLvl != MVS_NONE) && |
| 6416 | (NULL != dev->set_master_volume)) { |
| 6417 | AutoMutex lock(mHardwareLock); |
| 6418 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 6419 | dev->set_master_volume(dev, mMasterVolume); |
| 6420 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6421 | } |
| 6422 | |
| 6423 | audio_module_handle_t handle = nextUniqueId(); |
| 6424 | mAudioHwDevs.add(handle, new AudioHwDevice(name, dev)); |
| 6425 | |
| 6426 | ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6427 | name, dev->common.module->name, dev->common.module->id, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6428 | |
| 6429 | return handle; |
| 6430 | |
| 6431 | } |
| 6432 | |
| 6433 | audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 6434 | audio_devices_t *pDevices, |
| 6435 | uint32_t *pSamplingRate, |
| 6436 | audio_format_t *pFormat, |
| 6437 | audio_channel_mask_t *pChannelMask, |
| 6438 | uint32_t *pLatencyMs, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6439 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6440 | { |
| 6441 | status_t status; |
| 6442 | PlaybackThread *thread = NULL; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6443 | struct audio_config config = { |
| 6444 | sample_rate: pSamplingRate ? *pSamplingRate : 0, |
| 6445 | channel_mask: pChannelMask ? *pChannelMask : 0, |
| 6446 | format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, |
| 6447 | }; |
| 6448 | audio_stream_out_t *outStream = NULL; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6449 | audio_hw_device_t *outHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6450 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6451 | ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
| 6452 | module, |
Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 6453 | (pDevices != NULL) ? (int)*pDevices : 0, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6454 | config.sample_rate, |
| 6455 | config.format, |
| 6456 | config.channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6457 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6458 | |
| 6459 | if (pDevices == NULL || *pDevices == 0) { |
| 6460 | return 0; |
| 6461 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6462 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6463 | Mutex::Autolock _l(mLock); |
| 6464 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6465 | outHwDev = findSuitableHwDev_l(module, *pDevices); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6466 | if (outHwDev == NULL) |
| 6467 | return 0; |
| 6468 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6469 | audio_io_handle_t id = nextUniqueId(); |
| 6470 | |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 6471 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6472 | |
| 6473 | status = outHwDev->open_output_stream(outHwDev, |
| 6474 | id, |
| 6475 | *pDevices, |
| 6476 | (audio_output_flags_t)flags, |
| 6477 | &config, |
| 6478 | &outStream); |
| 6479 | |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 6480 | mHardwareStatus = AUDIO_HW_IDLE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6481 | ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6482 | outStream, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6483 | config.sample_rate, |
| 6484 | config.format, |
| 6485 | config.channel_mask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6486 | status); |
| 6487 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6488 | if (status == NO_ERROR && outStream != NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6489 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6490 | |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6491 | if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) || |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6492 | (config.format != AUDIO_FORMAT_PCM_16_BIT) || |
| 6493 | (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6494 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6495 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6496 | } else { |
| 6497 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6498 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6499 | } |
| 6500 | mPlaybackThreads.add(id, thread); |
| 6501 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6502 | if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate; |
| 6503 | if (pFormat != NULL) *pFormat = config.format; |
| 6504 | if (pChannelMask != NULL) *pChannelMask = config.channel_mask; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6505 | if (pLatencyMs != NULL) *pLatencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6506 | |
| 6507 | // notify client processes of the new output creation |
| 6508 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6509 | |
| 6510 | // the first primary output opened designates the primary hw device |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 6511 | if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6512 | ALOGI("Using module %d has the primary audio interface", module); |
| 6513 | mPrimaryHardwareDev = outHwDev; |
| 6514 | |
| 6515 | AutoMutex lock(mHardwareLock); |
| 6516 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 6517 | outHwDev->set_mode(outHwDev, mMode); |
| 6518 | |
| 6519 | // Determine the level of master volume support the primary audio HAL has, |
| 6520 | // and set the initial master volume at the same time. |
| 6521 | float initialVolume = 1.0; |
| 6522 | mMasterVolumeSupportLvl = MVS_NONE; |
| 6523 | |
| 6524 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 6525 | if ((NULL != outHwDev->get_master_volume) && |
| 6526 | (NO_ERROR == outHwDev->get_master_volume(outHwDev, &initialVolume))) { |
| 6527 | mMasterVolumeSupportLvl = MVS_FULL; |
| 6528 | } else { |
| 6529 | mMasterVolumeSupportLvl = MVS_SETONLY; |
| 6530 | initialVolume = 1.0; |
| 6531 | } |
| 6532 | |
| 6533 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 6534 | if ((NULL == outHwDev->set_master_volume) || |
| 6535 | (NO_ERROR != outHwDev->set_master_volume(outHwDev, initialVolume))) { |
| 6536 | mMasterVolumeSupportLvl = MVS_NONE; |
| 6537 | } |
| 6538 | // now that we have a primary device, initialize master volume on other devices |
| 6539 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 6540 | audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 6541 | |
| 6542 | if ((dev != mPrimaryHardwareDev) && |
| 6543 | (NULL != dev->set_master_volume)) { |
| 6544 | dev->set_master_volume(dev, initialVolume); |
| 6545 | } |
| 6546 | } |
| 6547 | mHardwareStatus = AUDIO_HW_IDLE; |
| 6548 | mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl) |
| 6549 | ? initialVolume |
| 6550 | : 1.0; |
| 6551 | mMasterVolume = initialVolume; |
| 6552 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6553 | return id; |
| 6554 | } |
| 6555 | |
| 6556 | return 0; |
| 6557 | } |
| 6558 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6559 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 6560 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6561 | { |
| 6562 | Mutex::Autolock _l(mLock); |
| 6563 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 6564 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 6565 | |
| 6566 | if (thread1 == NULL || thread2 == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6567 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6568 | return 0; |
| 6569 | } |
| 6570 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6571 | audio_io_handle_t id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6572 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 6573 | thread->addOutputTrack(thread2); |
| 6574 | mPlaybackThreads.add(id, thread); |
| 6575 | // notify client processes of the new output creation |
| 6576 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 6577 | return id; |
| 6578 | } |
| 6579 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6580 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6581 | { |
| 6582 | // keep strong reference on the playback thread so that |
| 6583 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6584 | sp<PlaybackThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6585 | { |
| 6586 | Mutex::Autolock _l(mLock); |
| 6587 | thread = checkPlaybackThread_l(output); |
| 6588 | if (thread == NULL) { |
| 6589 | return BAD_VALUE; |
| 6590 | } |
| 6591 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6592 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6593 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6594 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6595 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6596 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6597 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 6598 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 6599 | } |
| 6600 | } |
| 6601 | } |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6602 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6603 | mPlaybackThreads.removeItem(output); |
| 6604 | } |
| 6605 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6606 | // The thread entity (active unit of execution) is no longer running here, |
| 6607 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6608 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6609 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6610 | AudioStreamOut *out = thread->clearOutput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 6611 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6612 | // from now on thread->mOutput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6613 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 6614 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6615 | } |
| 6616 | return NO_ERROR; |
| 6617 | } |
| 6618 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6619 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6620 | { |
| 6621 | Mutex::Autolock _l(mLock); |
| 6622 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 6623 | |
| 6624 | if (thread == NULL) { |
| 6625 | return BAD_VALUE; |
| 6626 | } |
| 6627 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6628 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6629 | thread->suspend(); |
| 6630 | |
| 6631 | return NO_ERROR; |
| 6632 | } |
| 6633 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6634 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6635 | { |
| 6636 | Mutex::Autolock _l(mLock); |
| 6637 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 6638 | |
| 6639 | if (thread == NULL) { |
| 6640 | return BAD_VALUE; |
| 6641 | } |
| 6642 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6643 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6644 | |
| 6645 | thread->restore(); |
| 6646 | |
| 6647 | return NO_ERROR; |
| 6648 | } |
| 6649 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6650 | audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module, |
| 6651 | audio_devices_t *pDevices, |
| 6652 | uint32_t *pSamplingRate, |
| 6653 | audio_format_t *pFormat, |
| 6654 | uint32_t *pChannelMask) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6655 | { |
| 6656 | status_t status; |
| 6657 | RecordThread *thread = NULL; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6658 | struct audio_config config = { |
| 6659 | sample_rate: pSamplingRate ? *pSamplingRate : 0, |
| 6660 | channel_mask: pChannelMask ? *pChannelMask : 0, |
| 6661 | format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT, |
| 6662 | }; |
| 6663 | uint32_t reqSamplingRate = config.sample_rate; |
| 6664 | audio_format_t reqFormat = config.format; |
| 6665 | audio_channel_mask_t reqChannels = config.channel_mask; |
| 6666 | audio_stream_in_t *inStream = NULL; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6667 | audio_hw_device_t *inHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6668 | |
| 6669 | if (pDevices == NULL || *pDevices == 0) { |
| 6670 | return 0; |
| 6671 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6672 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6673 | Mutex::Autolock _l(mLock); |
| 6674 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6675 | inHwDev = findSuitableHwDev_l(module, *pDevices); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6676 | if (inHwDev == NULL) |
| 6677 | return 0; |
| 6678 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6679 | audio_io_handle_t id = nextUniqueId(); |
| 6680 | |
| 6681 | status = inHwDev->open_input_stream(inHwDev, id, *pDevices, &config, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6682 | &inStream); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6683 | ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6684 | inStream, |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6685 | config.sample_rate, |
| 6686 | config.format, |
| 6687 | config.channel_mask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6688 | status); |
| 6689 | |
| 6690 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 6691 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 6692 | // or stereo to mono conversions on 16 bit PCM inputs. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6693 | if (status == BAD_VALUE && |
| 6694 | reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT && |
| 6695 | (config.sample_rate <= 2 * reqSamplingRate) && |
| 6696 | (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6697 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6698 | inStream = NULL; |
| 6699 | status = inHwDev->open_input_stream(inHwDev, id, *pDevices, &config, &inStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6700 | } |
| 6701 | |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6702 | if (status == NO_ERROR && inStream != NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6703 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 6704 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6705 | // Start record thread |
| 6706 | // RecorThread require both input and output device indication to forward to audio |
| 6707 | // pre processing modules |
| 6708 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 6709 | thread = new RecordThread(this, |
| 6710 | input, |
| 6711 | reqSamplingRate, |
| 6712 | reqChannels, |
| 6713 | id, |
| 6714 | device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6715 | mRecordThreads.add(id, thread); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6716 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6717 | if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate; |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 6718 | if (pFormat != NULL) *pFormat = config.format; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 6719 | if (pChannelMask != NULL) *pChannelMask = reqChannels; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6720 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6721 | input->stream->common.standby(&input->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6722 | |
| 6723 | // notify client processes of the new input creation |
| 6724 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 6725 | return id; |
| 6726 | } |
| 6727 | |
| 6728 | return 0; |
| 6729 | } |
| 6730 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6731 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6732 | { |
| 6733 | // keep strong reference on the record thread so that |
| 6734 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6735 | sp<RecordThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6736 | { |
| 6737 | Mutex::Autolock _l(mLock); |
| 6738 | thread = checkRecordThread_l(input); |
| 6739 | if (thread == NULL) { |
| 6740 | return BAD_VALUE; |
| 6741 | } |
| 6742 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6743 | ALOGV("closeInput() %d", input); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6744 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6745 | mRecordThreads.removeItem(input); |
| 6746 | } |
| 6747 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 6748 | // The thread entity (active unit of execution) is no longer running here, |
| 6749 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6750 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6751 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 6752 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6753 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 6754 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 6755 | delete in; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6756 | |
| 6757 | return NO_ERROR; |
| 6758 | } |
| 6759 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6760 | status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6761 | { |
| 6762 | Mutex::Autolock _l(mLock); |
| 6763 | MixerThread *dstThread = checkMixerThread_l(output); |
| 6764 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6765 | ALOGW("setStreamOutput() bad output id %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6766 | return BAD_VALUE; |
| 6767 | } |
| 6768 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6769 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6770 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 6771 | |
| 6772 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6773 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6774 | if (thread != dstThread && thread->type() != ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6775 | MixerThread *srcThread = (MixerThread *)thread; |
| 6776 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6777 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6778 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6779 | |
| 6780 | return NO_ERROR; |
| 6781 | } |
| 6782 | |
| 6783 | |
| 6784 | int AudioFlinger::newAudioSessionId() |
| 6785 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6786 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6787 | } |
| 6788 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6789 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 6790 | { |
| 6791 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 6792 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6793 | ALOGV("acquiring %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 6794 | size_t num = mAudioSessionRefs.size(); |
| 6795 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6796 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 6797 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 6798 | ref->mCnt++; |
| 6799 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6800 | return; |
| 6801 | } |
| 6802 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 6803 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 6804 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 6808 | { |
| 6809 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 6810 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6811 | ALOGV("releasing %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 6812 | size_t num = mAudioSessionRefs.size(); |
| 6813 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6814 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 6815 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 6816 | ref->mCnt--; |
| 6817 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 6818 | if (ref->mCnt == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6819 | mAudioSessionRefs.removeAt(i); |
| 6820 | delete ref; |
| 6821 | purgeStaleEffects_l(); |
| 6822 | } |
| 6823 | return; |
| 6824 | } |
| 6825 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6826 | ALOGW("session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6827 | } |
| 6828 | |
| 6829 | void AudioFlinger::purgeStaleEffects_l() { |
| 6830 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6831 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6832 | |
| 6833 | Vector< sp<EffectChain> > chains; |
| 6834 | |
| 6835 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6836 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 6837 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 6838 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 6839 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 6840 | chains.push(ec); |
| 6841 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6842 | } |
| 6843 | } |
| 6844 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 6845 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 6846 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 6847 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 6848 | chains.push(ec); |
| 6849 | } |
| 6850 | } |
| 6851 | |
| 6852 | for (size_t i = 0; i < chains.size(); i++) { |
| 6853 | sp<EffectChain> ec = chains[i]; |
| 6854 | int sessionid = ec->sessionId(); |
| 6855 | sp<ThreadBase> t = ec->mThread.promote(); |
| 6856 | if (t == 0) { |
| 6857 | continue; |
| 6858 | } |
| 6859 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 6860 | bool found = false; |
| 6861 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 6862 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 6863 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6864 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 6865 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6866 | found = true; |
| 6867 | break; |
| 6868 | } |
| 6869 | } |
| 6870 | if (!found) { |
| 6871 | // remove all effects from the chain |
| 6872 | while (ec->mEffects.size()) { |
| 6873 | sp<EffectModule> effect = ec->mEffects[0]; |
| 6874 | effect->unPin(); |
| 6875 | Mutex::Autolock _l (t->mLock); |
| 6876 | t->removeEffect_l(effect); |
| 6877 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 6878 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 6879 | if (handle != 0) { |
| 6880 | handle->mEffect.clear(); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6881 | if (handle->mHasControl && handle->mEnabled) { |
| 6882 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 6883 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6884 | } |
| 6885 | } |
| 6886 | AudioSystem::unregisterEffect(effect->id()); |
| 6887 | } |
| 6888 | } |
| 6889 | } |
| 6890 | return; |
| 6891 | } |
| 6892 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6893 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6894 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6895 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6896 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6897 | } |
| 6898 | |
| 6899 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6900 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6901 | { |
| 6902 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6903 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6907 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6908 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6909 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6910 | } |
| 6911 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6912 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6913 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6914 | return android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6915 | } |
| 6916 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 6917 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6918 | { |
| 6919 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6920 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6921 | AudioStreamOut *output = thread->getOutput(); |
| 6922 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6923 | return thread; |
| 6924 | } |
| 6925 | } |
| 6926 | return NULL; |
| 6927 | } |
| 6928 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 6929 | uint32_t AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6930 | { |
| 6931 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 6932 | |
| 6933 | if (thread == NULL) { |
| 6934 | return 0; |
| 6935 | } |
| 6936 | |
| 6937 | return thread->device(); |
| 6938 | } |
| 6939 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 6940 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
| 6941 | int triggerSession, |
| 6942 | int listenerSession, |
| 6943 | sync_event_callback_t callBack, |
| 6944 | void *cookie) |
| 6945 | { |
| 6946 | Mutex::Autolock _l(mLock); |
| 6947 | |
| 6948 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 6949 | status_t playStatus = NAME_NOT_FOUND; |
| 6950 | status_t recStatus = NAME_NOT_FOUND; |
| 6951 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6952 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 6953 | if (playStatus == NO_ERROR) { |
| 6954 | return event; |
| 6955 | } |
| 6956 | } |
| 6957 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 6958 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 6959 | if (recStatus == NO_ERROR) { |
| 6960 | return event; |
| 6961 | } |
| 6962 | } |
| 6963 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 6964 | mPendingSyncEvents.add(event); |
| 6965 | } else { |
| 6966 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 6967 | event.clear(); |
| 6968 | } |
| 6969 | return event; |
| 6970 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6971 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6972 | // ---------------------------------------------------------------------------- |
| 6973 | // Effect management |
| 6974 | // ---------------------------------------------------------------------------- |
| 6975 | |
| 6976 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 6977 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6978 | { |
| 6979 | Mutex::Autolock _l(mLock); |
| 6980 | return EffectQueryNumberEffects(numEffects); |
| 6981 | } |
| 6982 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 6983 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6984 | { |
| 6985 | Mutex::Autolock _l(mLock); |
| 6986 | return EffectQueryEffect(index, descriptor); |
| 6987 | } |
| 6988 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 6989 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 6990 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6991 | { |
| 6992 | Mutex::Autolock _l(mLock); |
| 6993 | return EffectGetDescriptor(pUuid, descriptor); |
| 6994 | } |
| 6995 | |
| 6996 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6997 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 6998 | effect_descriptor_t *pDesc, |
| 6999 | const sp<IEffectClient>& effectClient, |
| 7000 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7001 | audio_io_handle_t io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7002 | int sessionId, |
| 7003 | status_t *status, |
| 7004 | int *id, |
| 7005 | int *enabled) |
| 7006 | { |
| 7007 | status_t lStatus = NO_ERROR; |
| 7008 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7009 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7010 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7011 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7012 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7013 | |
| 7014 | if (pDesc == NULL) { |
| 7015 | lStatus = BAD_VALUE; |
| 7016 | goto Exit; |
| 7017 | } |
| 7018 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7019 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7020 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7021 | lStatus = PERMISSION_DENIED; |
| 7022 | goto Exit; |
| 7023 | } |
| 7024 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7025 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7026 | // that can only be created by audio policy manager (running in same process) |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 7027 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7028 | lStatus = PERMISSION_DENIED; |
| 7029 | goto Exit; |
| 7030 | } |
| 7031 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7032 | if (io == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7033 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7034 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7035 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7036 | lStatus = BAD_VALUE; |
| 7037 | goto Exit; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7038 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7039 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7040 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7041 | // and we will exit safely |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7042 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7043 | } |
| 7044 | } |
| 7045 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7046 | { |
| 7047 | Mutex::Autolock _l(mLock); |
| 7048 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7049 | |
| 7050 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 7051 | // if uuid is specified, request effect descriptor |
| 7052 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 7053 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7054 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7055 | goto Exit; |
| 7056 | } |
| 7057 | } else { |
| 7058 | // if uuid is not specified, look for an available implementation |
| 7059 | // of the required type in effect factory |
| 7060 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7061 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7062 | lStatus = BAD_VALUE; |
| 7063 | goto Exit; |
| 7064 | } |
| 7065 | uint32_t numEffects = 0; |
| 7066 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7067 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7068 | bool found = false; |
| 7069 | |
| 7070 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 7071 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7072 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7073 | goto Exit; |
| 7074 | } |
| 7075 | for (uint32_t i = 0; i < numEffects; i++) { |
| 7076 | lStatus = EffectQueryEffect(i, &desc); |
| 7077 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7078 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7079 | continue; |
| 7080 | } |
| 7081 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 7082 | // If matching type found save effect descriptor. If the session is |
| 7083 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 7084 | // an auxiliary version of this effect type is available |
| 7085 | found = true; |
| 7086 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7087 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7088 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7089 | break; |
| 7090 | } |
| 7091 | } |
| 7092 | } |
| 7093 | if (!found) { |
| 7094 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7095 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7096 | goto Exit; |
| 7097 | } |
| 7098 | // For same effect type, chose auxiliary version over insert version if |
| 7099 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7100 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7101 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7102 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 7103 | } |
| 7104 | } |
| 7105 | |
| 7106 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7107 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7108 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7109 | lStatus = INVALID_OPERATION; |
| 7110 | goto Exit; |
| 7111 | } |
| 7112 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7113 | // check recording permission for visualizer |
| 7114 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 7115 | !recordingAllowed()) { |
| 7116 | lStatus = PERMISSION_DENIED; |
| 7117 | goto Exit; |
| 7118 | } |
| 7119 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7120 | // return effect descriptor |
| 7121 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 7122 | |
| 7123 | // If output is not specified try to find a matching audio session ID in one of the |
| 7124 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7125 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 7126 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7127 | // Note: io is never 0 when creating an effect on an input |
| 7128 | if (io == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7129 | // look for the thread where the specified audio session is present |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7130 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 7131 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7132 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7133 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7134 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7135 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7136 | if (io == 0) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7137 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 7138 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 7139 | io = mRecordThreads.keyAt(i); |
| 7140 | break; |
| 7141 | } |
| 7142 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7143 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7144 | // If no output thread contains the requested session ID, default to |
| 7145 | // first output. The effect chain will be moved to the correct output |
| 7146 | // thread when a track with the same session ID is created |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7147 | if (io == 0 && mPlaybackThreads.size()) { |
| 7148 | io = mPlaybackThreads.keyAt(0); |
| 7149 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7150 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7151 | } |
| 7152 | ThreadBase *thread = checkRecordThread_l(io); |
| 7153 | if (thread == NULL) { |
| 7154 | thread = checkPlaybackThread_l(io); |
| 7155 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 7156 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7157 | lStatus = BAD_VALUE; |
| 7158 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7159 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7160 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 7161 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7162 | sp<Client> client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7163 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7164 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7165 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 7166 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7167 | if (handle != 0 && id != NULL) { |
| 7168 | *id = handle->id(); |
| 7169 | } |
| 7170 | } |
| 7171 | |
| 7172 | Exit: |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7173 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7174 | *status = lStatus; |
| 7175 | } |
| 7176 | return handle; |
| 7177 | } |
| 7178 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7179 | status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, |
| 7180 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7181 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7182 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7183 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7184 | Mutex::Autolock _l(mLock); |
| 7185 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7186 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7187 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7188 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7189 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 7190 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7191 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7192 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7193 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7194 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 7195 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7196 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7197 | return BAD_VALUE; |
| 7198 | } |
| 7199 | |
| 7200 | Mutex::Autolock _dl(dstThread->mLock); |
| 7201 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7202 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7203 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7204 | return NO_ERROR; |
| 7205 | } |
| 7206 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7207 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7208 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7209 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7210 | AudioFlinger::PlaybackThread *dstThread, |
| 7211 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7212 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7213 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7214 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7215 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7216 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7217 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7218 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7219 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7220 | return INVALID_OPERATION; |
| 7221 | } |
| 7222 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7223 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7224 | // so that a new chain is created with correct parameters when first effect is added. This is |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7225 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7226 | // removed. |
| 7227 | srcThread->removeEffectChain_l(chain); |
| 7228 | |
| 7229 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 7230 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 7231 | audio_io_handle_t dstOutput = dstThread->id(); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7232 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7233 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7234 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 7235 | while (effect != 0) { |
| 7236 | srcThread->removeEffect_l(effect); |
| 7237 | dstThread->addEffect_l(effect); |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7238 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 7239 | if (effect->state() == EffectModule::ACTIVE || |
| 7240 | effect->state() == EffectModule::STOPPING) { |
| 7241 | effect->start(); |
| 7242 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7243 | // if the move request is not received from audio policy manager, the effect must be |
| 7244 | // re-registered with the new strategy and output |
| 7245 | if (dstChain == 0) { |
| 7246 | dstChain = effect->chain().promote(); |
| 7247 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7248 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7249 | srcThread->addEffect_l(effect); |
| 7250 | return NO_INIT; |
| 7251 | } |
| 7252 | strategy = dstChain->strategy(); |
| 7253 | } |
| 7254 | if (reRegister) { |
| 7255 | AudioSystem::unregisterEffect(effect->id()); |
| 7256 | AudioSystem::registerEffect(&effect->desc(), |
| 7257 | dstOutput, |
| 7258 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7259 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 7260 | effect->id()); |
| 7261 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7262 | effect = chain->getEffectFromId_l(0); |
| 7263 | } |
| 7264 | |
| 7265 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7266 | } |
| 7267 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7268 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7269 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7270 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7271 | const sp<AudioFlinger::Client>& client, |
| 7272 | const sp<IEffectClient>& effectClient, |
| 7273 | int32_t priority, |
| 7274 | int sessionId, |
| 7275 | effect_descriptor_t *desc, |
| 7276 | int *enabled, |
| 7277 | status_t *status |
| 7278 | ) |
| 7279 | { |
| 7280 | sp<EffectModule> effect; |
| 7281 | sp<EffectHandle> handle; |
| 7282 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7283 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7284 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7285 | bool effectCreated = false; |
| 7286 | bool effectRegistered = false; |
| 7287 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7288 | lStatus = initCheck(); |
| 7289 | if (lStatus != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7290 | ALOGW("createEffect_l() Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7291 | goto Exit; |
| 7292 | } |
| 7293 | |
| 7294 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 7295 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7296 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7297 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7298 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7299 | lStatus = BAD_VALUE; |
| 7300 | goto Exit; |
| 7301 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7302 | // Only Pre processor effects are allowed on input threads and only on input threads |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 7303 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7304 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7305 | desc->name, desc->flags, mType); |
| 7306 | lStatus = BAD_VALUE; |
| 7307 | goto Exit; |
| 7308 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7309 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7310 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7311 | |
| 7312 | { // scope for mLock |
| 7313 | Mutex::Autolock _l(mLock); |
| 7314 | |
| 7315 | // check for existing effect chain with the requested audio session |
| 7316 | chain = getEffectChain_l(sessionId); |
| 7317 | if (chain == 0) { |
| 7318 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7319 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7320 | chain = new EffectChain(this, sessionId); |
| 7321 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7322 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 7323 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7324 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7325 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7326 | } |
| 7327 | |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 7328 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7329 | |
| 7330 | if (effect == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7331 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7332 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7333 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7334 | if (lStatus != NO_ERROR) { |
| 7335 | goto Exit; |
| 7336 | } |
| 7337 | effectRegistered = true; |
| 7338 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7339 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7340 | lStatus = effect->status(); |
| 7341 | if (lStatus != NO_ERROR) { |
| 7342 | goto Exit; |
| 7343 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7344 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7345 | if (lStatus != NO_ERROR) { |
| 7346 | goto Exit; |
| 7347 | } |
| 7348 | effectCreated = true; |
| 7349 | |
| 7350 | effect->setDevice(mDevice); |
| 7351 | effect->setMode(mAudioFlinger->getMode()); |
| 7352 | } |
| 7353 | // create effect handle and connect it to effect module |
| 7354 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 7355 | lStatus = effect->addHandle(handle); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7356 | if (enabled != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7357 | *enabled = (int)effect->isEnabled(); |
| 7358 | } |
| 7359 | } |
| 7360 | |
| 7361 | Exit: |
| 7362 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7363 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7364 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7365 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7366 | } |
| 7367 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7368 | AudioSystem::unregisterEffect(effect->id()); |
| 7369 | } |
| 7370 | if (chainCreated) { |
| 7371 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7372 | } |
| 7373 | handle.clear(); |
| 7374 | } |
| 7375 | |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7376 | if (status != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7377 | *status = lStatus; |
| 7378 | } |
| 7379 | return handle; |
| 7380 | } |
| 7381 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7382 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 7383 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7384 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7385 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7386 | } |
| 7387 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7388 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 7389 | // PlaybackThread::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7390 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7391 | { |
| 7392 | // check for existing effect chain with the requested audio session |
| 7393 | int sessionId = effect->sessionId(); |
| 7394 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 7395 | bool chainCreated = false; |
| 7396 | |
| 7397 | if (chain == 0) { |
| 7398 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7399 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7400 | chain = new EffectChain(this, sessionId); |
| 7401 | addEffectChain_l(chain); |
| 7402 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 7403 | chainCreated = true; |
| 7404 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7405 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7406 | |
| 7407 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7408 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7409 | this, effect->desc().name, chain.get()); |
| 7410 | return BAD_VALUE; |
| 7411 | } |
| 7412 | |
| 7413 | status_t status = chain->addEffect_l(effect); |
| 7414 | if (status != NO_ERROR) { |
| 7415 | if (chainCreated) { |
| 7416 | removeEffectChain_l(chain); |
| 7417 | } |
| 7418 | return status; |
| 7419 | } |
| 7420 | |
| 7421 | effect->setDevice(mDevice); |
| 7422 | effect->setMode(mAudioFlinger->getMode()); |
| 7423 | return NO_ERROR; |
| 7424 | } |
| 7425 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7426 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7427 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7428 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7429 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7430 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7431 | detachAuxEffect_l(effect->id()); |
| 7432 | } |
| 7433 | |
| 7434 | sp<EffectChain> chain = effect->chain().promote(); |
| 7435 | if (chain != 0) { |
| 7436 | // remove effect chain if removing last effect |
| 7437 | if (chain->removeEffect_l(effect) == 0) { |
| 7438 | removeEffectChain_l(chain); |
| 7439 | } |
| 7440 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7441 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7442 | } |
| 7443 | } |
| 7444 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7445 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7446 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7447 | { |
| 7448 | effectChains = mEffectChains; |
| 7449 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7450 | mEffectChains[i]->lock(); |
| 7451 | } |
| 7452 | } |
| 7453 | |
| 7454 | void AudioFlinger::ThreadBase::unlockEffectChains( |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7455 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7456 | { |
| 7457 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 7458 | effectChains[i]->unlock(); |
| 7459 | } |
| 7460 | } |
| 7461 | |
| 7462 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 7463 | { |
| 7464 | Mutex::Autolock _l(mLock); |
| 7465 | return getEffectChain_l(sessionId); |
| 7466 | } |
| 7467 | |
| 7468 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 7469 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7470 | size_t size = mEffectChains.size(); |
| 7471 | for (size_t i = 0; i < size; i++) { |
| 7472 | if (mEffectChains[i]->sessionId() == sessionId) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7473 | return mEffectChains[i]; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7474 | } |
| 7475 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7476 | return 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7477 | } |
| 7478 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7479 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7480 | { |
| 7481 | Mutex::Autolock _l(mLock); |
| 7482 | size_t size = mEffectChains.size(); |
| 7483 | for (size_t i = 0; i < size; i++) { |
| 7484 | mEffectChains[i]->setMode_l(mode); |
| 7485 | } |
| 7486 | } |
| 7487 | |
| 7488 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7489 | const wp<EffectHandle>& handle, |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7490 | bool unpinIfLast) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7491 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7492 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7493 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7494 | // delete the effect module if removing last handle on it |
| 7495 | if (effect->removeHandle(handle) == 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7496 | if (!effect->isPinned() || unpinIfLast) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7497 | removeEffect_l(effect); |
| 7498 | AudioSystem::unregisterEffect(effect->id()); |
| 7499 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7500 | } |
| 7501 | } |
| 7502 | |
| 7503 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7504 | { |
| 7505 | int session = chain->sessionId(); |
| 7506 | int16_t *buffer = mMixBuffer; |
| 7507 | bool ownsBuffer = false; |
| 7508 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7509 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7510 | if (session > 0) { |
| 7511 | // Only one effect chain can be present in direct output thread and it uses |
| 7512 | // the mix buffer as input |
| 7513 | if (mType != DIRECT) { |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 7514 | size_t numSamples = mNormalFrameCount * mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7515 | buffer = new int16_t[numSamples]; |
| 7516 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7517 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7518 | ownsBuffer = true; |
| 7519 | } |
| 7520 | |
| 7521 | // Attach all tracks with same session ID to this chain. |
| 7522 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7523 | sp<Track> track = mTracks[i]; |
| 7524 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7525 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7526 | track->setMainBuffer(buffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7527 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7528 | } |
| 7529 | } |
| 7530 | |
| 7531 | // indicate all active tracks in the chain |
| 7532 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 7533 | sp<Track> track = mActiveTracks[i].promote(); |
| 7534 | if (track == 0) continue; |
| 7535 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7536 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7537 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7538 | } |
| 7539 | } |
| 7540 | } |
| 7541 | |
| 7542 | chain->setInBuffer(buffer, ownsBuffer); |
| 7543 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7544 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7545 | // chains list in order to be processed last as it contains output stage effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7546 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 7547 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7548 | // after track specific effects and before output stage |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7549 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 7550 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7551 | // Effect chain for other sessions are inserted at beginning of effect |
| 7552 | // chains list to be processed before output mix effects. Relative order between other |
| 7553 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7554 | size_t size = mEffectChains.size(); |
| 7555 | size_t i = 0; |
| 7556 | for (i = 0; i < size; i++) { |
| 7557 | if (mEffectChains[i]->sessionId() < session) break; |
| 7558 | } |
| 7559 | mEffectChains.insertAt(chain, i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7560 | checkSuspendOnAddEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7561 | |
| 7562 | return NO_ERROR; |
| 7563 | } |
| 7564 | |
| 7565 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7566 | { |
| 7567 | int session = chain->sessionId(); |
| 7568 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7569 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7570 | |
| 7571 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7572 | if (chain == mEffectChains[i]) { |
| 7573 | mEffectChains.removeAt(i); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7574 | // detach all active tracks from the chain |
| 7575 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 7576 | sp<Track> track = mActiveTracks[i].promote(); |
| 7577 | if (track == 0) continue; |
| 7578 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7579 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7580 | chain.get(), session); |
| 7581 | chain->decActiveTrackCnt(); |
| 7582 | } |
| 7583 | } |
| 7584 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7585 | // detach all tracks with same session ID from this chain |
| 7586 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7587 | sp<Track> track = mTracks[i]; |
| 7588 | if (session == track->sessionId()) { |
| 7589 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7590 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7591 | } |
| 7592 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7593 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7594 | } |
| 7595 | } |
| 7596 | return mEffectChains.size(); |
| 7597 | } |
| 7598 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7599 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 7600 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7601 | { |
| 7602 | Mutex::Autolock _l(mLock); |
| 7603 | return attachAuxEffect_l(track, EffectId); |
| 7604 | } |
| 7605 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7606 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 7607 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7608 | { |
| 7609 | status_t status = NO_ERROR; |
| 7610 | |
| 7611 | if (EffectId == 0) { |
| 7612 | track->setAuxBuffer(0, NULL); |
| 7613 | } else { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7614 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 7615 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7616 | if (effect != 0) { |
| 7617 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7618 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 7619 | } else { |
| 7620 | status = INVALID_OPERATION; |
| 7621 | } |
| 7622 | } else { |
| 7623 | status = BAD_VALUE; |
| 7624 | } |
| 7625 | } |
| 7626 | return status; |
| 7627 | } |
| 7628 | |
| 7629 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 7630 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 7631 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7632 | sp<Track> track = mTracks[i]; |
| 7633 | if (track->auxEffectId() == effectId) { |
| 7634 | attachAuxEffect_l(track, 0); |
| 7635 | } |
| 7636 | } |
| 7637 | } |
| 7638 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7639 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7640 | { |
| 7641 | // only one chain per input thread |
| 7642 | if (mEffectChains.size() != 0) { |
| 7643 | return INVALID_OPERATION; |
| 7644 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7645 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7646 | |
| 7647 | chain->setInBuffer(NULL); |
| 7648 | chain->setOutBuffer(NULL); |
| 7649 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7650 | checkSuspendOnAddEffectChain_l(chain); |
| 7651 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7652 | mEffectChains.add(chain); |
| 7653 | |
| 7654 | return NO_ERROR; |
| 7655 | } |
| 7656 | |
| 7657 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7658 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7659 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7660 | ALOGW_IF(mEffectChains.size() != 1, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7661 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 7662 | chain.get(), mEffectChains.size(), this); |
| 7663 | if (mEffectChains.size() == 1) { |
| 7664 | mEffectChains.removeAt(0); |
| 7665 | } |
| 7666 | return 0; |
| 7667 | } |
| 7668 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7669 | // ---------------------------------------------------------------------------- |
| 7670 | // EffectModule implementation |
| 7671 | // ---------------------------------------------------------------------------- |
| 7672 | |
| 7673 | #undef LOG_TAG |
| 7674 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 7675 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7676 | AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7677 | const wp<AudioFlinger::EffectChain>& chain, |
| 7678 | effect_descriptor_t *desc, |
| 7679 | int id, |
| 7680 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7681 | : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7682 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7683 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7684 | ALOGV("Constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7685 | int lStatus; |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7686 | if (thread == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7687 | return; |
| 7688 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7689 | |
| 7690 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 7691 | |
| 7692 | // create effect engine from effect factory |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7693 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7694 | |
| 7695 | if (mStatus != NO_ERROR) { |
| 7696 | return; |
| 7697 | } |
| 7698 | lStatus = init(); |
| 7699 | if (lStatus < 0) { |
| 7700 | mStatus = lStatus; |
| 7701 | goto Error; |
| 7702 | } |
| 7703 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7704 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 7705 | mPinned = true; |
| 7706 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7707 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7708 | return; |
| 7709 | Error: |
| 7710 | EffectRelease(mEffectInterface); |
| 7711 | mEffectInterface = NULL; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7712 | ALOGV("Constructor Error %d", mStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7713 | } |
| 7714 | |
| 7715 | AudioFlinger::EffectModule::~EffectModule() |
| 7716 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7717 | ALOGV("Destructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7718 | if (mEffectInterface != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7719 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 7720 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 7721 | sp<ThreadBase> thread = mThread.promote(); |
| 7722 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 7723 | audio_stream_t *stream = thread->stream(); |
| 7724 | if (stream != NULL) { |
| 7725 | stream->remove_audio_effect(stream, mEffectInterface); |
| 7726 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7727 | } |
| 7728 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7729 | // release effect engine |
| 7730 | EffectRelease(mEffectInterface); |
| 7731 | } |
| 7732 | } |
| 7733 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 7734 | status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7735 | { |
| 7736 | status_t status; |
| 7737 | |
| 7738 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7739 | int priority = handle->priority(); |
| 7740 | size_t size = mHandles.size(); |
| 7741 | sp<EffectHandle> h; |
| 7742 | size_t i; |
| 7743 | for (i = 0; i < size; i++) { |
| 7744 | h = mHandles[i].promote(); |
| 7745 | if (h == 0) continue; |
| 7746 | if (h->priority() <= priority) break; |
| 7747 | } |
| 7748 | // if inserted in first place, move effect control from previous owner to this handle |
| 7749 | if (i == 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7750 | bool enabled = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7751 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7752 | enabled = h->enabled(); |
| 7753 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7754 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7755 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7756 | status = NO_ERROR; |
| 7757 | } else { |
| 7758 | status = ALREADY_EXISTS; |
| 7759 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7760 | ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7761 | mHandles.insertAt(handle, i); |
| 7762 | return status; |
| 7763 | } |
| 7764 | |
| 7765 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 7766 | { |
| 7767 | Mutex::Autolock _l(mLock); |
| 7768 | size_t size = mHandles.size(); |
| 7769 | size_t i; |
| 7770 | for (i = 0; i < size; i++) { |
| 7771 | if (mHandles[i] == handle) break; |
| 7772 | } |
| 7773 | if (i == size) { |
| 7774 | return size; |
| 7775 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7776 | ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7777 | |
| 7778 | bool enabled = false; |
| 7779 | EffectHandle *hdl = handle.unsafe_get(); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7780 | if (hdl != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7781 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7782 | enabled = hdl->enabled(); |
| 7783 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7784 | mHandles.removeAt(i); |
| 7785 | size = mHandles.size(); |
| 7786 | // if removed from first place, move effect control from this handle to next in line |
| 7787 | if (i == 0 && size != 0) { |
| 7788 | sp<EffectHandle> h = mHandles[0].promote(); |
| 7789 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7790 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7791 | } |
| 7792 | } |
| 7793 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7794 | // Prevent calls to process() and other functions on effect interface from now on. |
| 7795 | // The effect engine will be released by the destructor when the last strong reference on |
| 7796 | // this object is released which can happen after next process is called. |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7797 | if (size == 0 && !mPinned) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7798 | mState = DESTROYED; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7799 | } |
| 7800 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7801 | return size; |
| 7802 | } |
| 7803 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7804 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 7805 | { |
| 7806 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7807 | return mHandles.size() != 0 ? mHandles[0].promote() : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7808 | } |
| 7809 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7810 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7811 | { |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 7812 | ALOGV("disconnect() %p handle %p", this, handle.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7813 | // keep a strong reference on this EffectModule to avoid calling the |
| 7814 | // destructor before we exit |
| 7815 | sp<EffectModule> keep(this); |
| 7816 | { |
| 7817 | sp<ThreadBase> thread = mThread.promote(); |
| 7818 | if (thread != 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7819 | thread->disconnectEffect(keep, handle, unpinIfLast); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7820 | } |
| 7821 | } |
| 7822 | } |
| 7823 | |
| 7824 | void AudioFlinger::EffectModule::updateState() { |
| 7825 | Mutex::Autolock _l(mLock); |
| 7826 | |
| 7827 | switch (mState) { |
| 7828 | case RESTART: |
| 7829 | reset_l(); |
| 7830 | // FALL THROUGH |
| 7831 | |
| 7832 | case STARTING: |
| 7833 | // clear auxiliary effect input buffer for next accumulation |
| 7834 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7835 | memset(mConfig.inputCfg.buffer.raw, |
| 7836 | 0, |
| 7837 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 7838 | } |
| 7839 | start_l(); |
| 7840 | mState = ACTIVE; |
| 7841 | break; |
| 7842 | case STOPPING: |
| 7843 | stop_l(); |
| 7844 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 7845 | mState = STOPPED; |
| 7846 | break; |
| 7847 | case STOPPED: |
| 7848 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 7849 | // turn off sequence. |
| 7850 | if (--mDisableWaitCnt == 0) { |
| 7851 | reset_l(); |
| 7852 | mState = IDLE; |
| 7853 | } |
| 7854 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7855 | default: //IDLE , ACTIVE, DESTROYED |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7856 | break; |
| 7857 | } |
| 7858 | } |
| 7859 | |
| 7860 | void AudioFlinger::EffectModule::process() |
| 7861 | { |
| 7862 | Mutex::Autolock _l(mLock); |
| 7863 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7864 | if (mState == DESTROYED || mEffectInterface == NULL || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7865 | mConfig.inputCfg.buffer.raw == NULL || |
| 7866 | mConfig.outputCfg.buffer.raw == NULL) { |
| 7867 | return; |
| 7868 | } |
| 7869 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7870 | if (isProcessEnabled()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7871 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 7872 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 7873 | ditherAndClamp(mConfig.inputCfg.buffer.s32, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7874 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7875 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7876 | } |
| 7877 | |
| 7878 | // do the actual processing in the effect engine |
| 7879 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 7880 | &mConfig.inputCfg.buffer, |
| 7881 | &mConfig.outputCfg.buffer); |
| 7882 | |
| 7883 | // force transition to IDLE state when engine is ready |
| 7884 | if (mState == STOPPED && ret == -ENODATA) { |
| 7885 | mDisableWaitCnt = 1; |
| 7886 | } |
| 7887 | |
| 7888 | // clear auxiliary effect input buffer for next accumulation |
| 7889 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 7890 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 7891 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7892 | } |
| 7893 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 7894 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 7895 | // If an insert effect is idle and input buffer is different from output buffer, |
| 7896 | // accumulate input onto output |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7897 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7898 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 7899 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 7900 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 7901 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 7902 | for (size_t i = 0; i < frameCnt; i++) { |
| 7903 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7904 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7905 | } |
| 7906 | } |
| 7907 | } |
| 7908 | |
| 7909 | void AudioFlinger::EffectModule::reset_l() |
| 7910 | { |
| 7911 | if (mEffectInterface == NULL) { |
| 7912 | return; |
| 7913 | } |
| 7914 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 7915 | } |
| 7916 | |
| 7917 | status_t AudioFlinger::EffectModule::configure() |
| 7918 | { |
| 7919 | uint32_t channels; |
| 7920 | if (mEffectInterface == NULL) { |
| 7921 | return NO_INIT; |
| 7922 | } |
| 7923 | |
| 7924 | sp<ThreadBase> thread = mThread.promote(); |
| 7925 | if (thread == 0) { |
| 7926 | return DEAD_OBJECT; |
| 7927 | } |
| 7928 | |
| 7929 | // TODO: handle configuration of effects replacing track process |
| 7930 | if (thread->channelCount() == 1) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7931 | channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7932 | } else { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7933 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7934 | } |
| 7935 | |
| 7936 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7937 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7938 | } else { |
| 7939 | mConfig.inputCfg.channels = channels; |
| 7940 | } |
| 7941 | mConfig.outputCfg.channels = channels; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7942 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 7943 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7944 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 7945 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 7946 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 7947 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 7948 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 7949 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 7950 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 7951 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 7952 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 7953 | // Insert effect: |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7954 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7955 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7956 | // - in other sessions: |
| 7957 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 7958 | // other effect: overwrites output buffer: input buffer == output buffer |
| 7959 | // Auxiliary effect: |
| 7960 | // accumulates in output buffer: input buffer != output buffer |
| 7961 | // Therefore: accumulate <=> input buffer != output buffer |
| 7962 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 7963 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 7964 | } else { |
| 7965 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 7966 | } |
| 7967 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 7968 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 7969 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 7970 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 7971 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7972 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7973 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 7974 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7975 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7976 | uint32_t size = sizeof(int); |
| 7977 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 7978 | EFFECT_CMD_SET_CONFIG, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7979 | sizeof(effect_config_t), |
| 7980 | &mConfig, |
| 7981 | &size, |
| 7982 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7983 | if (status == 0) { |
| 7984 | status = cmdStatus; |
| 7985 | } |
| 7986 | |
| 7987 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 7988 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 7989 | |
| 7990 | return status; |
| 7991 | } |
| 7992 | |
| 7993 | status_t AudioFlinger::EffectModule::init() |
| 7994 | { |
| 7995 | Mutex::Autolock _l(mLock); |
| 7996 | if (mEffectInterface == NULL) { |
| 7997 | return NO_INIT; |
| 7998 | } |
| 7999 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8000 | uint32_t size = sizeof(status_t); |
| 8001 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8002 | EFFECT_CMD_INIT, |
| 8003 | 0, |
| 8004 | NULL, |
| 8005 | &size, |
| 8006 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8007 | if (status == 0) { |
| 8008 | status = cmdStatus; |
| 8009 | } |
| 8010 | return status; |
| 8011 | } |
| 8012 | |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 8013 | status_t AudioFlinger::EffectModule::start() |
| 8014 | { |
| 8015 | Mutex::Autolock _l(mLock); |
| 8016 | return start_l(); |
| 8017 | } |
| 8018 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8019 | status_t AudioFlinger::EffectModule::start_l() |
| 8020 | { |
| 8021 | if (mEffectInterface == NULL) { |
| 8022 | return NO_INIT; |
| 8023 | } |
| 8024 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8025 | uint32_t size = sizeof(status_t); |
| 8026 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8027 | EFFECT_CMD_ENABLE, |
| 8028 | 0, |
| 8029 | NULL, |
| 8030 | &size, |
| 8031 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8032 | if (status == 0) { |
| 8033 | status = cmdStatus; |
| 8034 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8035 | if (status == 0 && |
| 8036 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 8037 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 8038 | sp<ThreadBase> thread = mThread.promote(); |
| 8039 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 8040 | audio_stream_t *stream = thread->stream(); |
| 8041 | if (stream != NULL) { |
| 8042 | stream->add_audio_effect(stream, mEffectInterface); |
| 8043 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8044 | } |
| 8045 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8046 | return status; |
| 8047 | } |
| 8048 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8049 | status_t AudioFlinger::EffectModule::stop() |
| 8050 | { |
| 8051 | Mutex::Autolock _l(mLock); |
| 8052 | return stop_l(); |
| 8053 | } |
| 8054 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8055 | status_t AudioFlinger::EffectModule::stop_l() |
| 8056 | { |
| 8057 | if (mEffectInterface == NULL) { |
| 8058 | return NO_INIT; |
| 8059 | } |
| 8060 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8061 | uint32_t size = sizeof(status_t); |
| 8062 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8063 | EFFECT_CMD_DISABLE, |
| 8064 | 0, |
| 8065 | NULL, |
| 8066 | &size, |
| 8067 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8068 | if (status == 0) { |
| 8069 | status = cmdStatus; |
| 8070 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8071 | if (status == 0 && |
| 8072 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 8073 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 8074 | sp<ThreadBase> thread = mThread.promote(); |
| 8075 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 8076 | audio_stream_t *stream = thread->stream(); |
| 8077 | if (stream != NULL) { |
| 8078 | stream->remove_audio_effect(stream, mEffectInterface); |
| 8079 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8080 | } |
| 8081 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8082 | return status; |
| 8083 | } |
| 8084 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8085 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 8086 | uint32_t cmdSize, |
| 8087 | void *pCmdData, |
| 8088 | uint32_t *replySize, |
| 8089 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8090 | { |
| 8091 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8092 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8093 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8094 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8095 | return NO_INIT; |
| 8096 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8097 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 8098 | cmdCode, |
| 8099 | cmdSize, |
| 8100 | pCmdData, |
| 8101 | replySize, |
| 8102 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8103 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8104 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8105 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 8106 | sp<EffectHandle> h = mHandles[i].promote(); |
| 8107 | if (h != 0) { |
| 8108 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 8109 | } |
| 8110 | } |
| 8111 | } |
| 8112 | return status; |
| 8113 | } |
| 8114 | |
| 8115 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 8116 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8117 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8118 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8119 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8120 | |
| 8121 | if (enabled != isEnabled()) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8122 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 8123 | if (enabled && status != NO_ERROR) { |
| 8124 | return status; |
| 8125 | } |
| 8126 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8127 | switch (mState) { |
| 8128 | // going from disabled to enabled |
| 8129 | case IDLE: |
| 8130 | mState = STARTING; |
| 8131 | break; |
| 8132 | case STOPPED: |
| 8133 | mState = RESTART; |
| 8134 | break; |
| 8135 | case STOPPING: |
| 8136 | mState = ACTIVE; |
| 8137 | break; |
| 8138 | |
| 8139 | // going from enabled to disabled |
| 8140 | case RESTART: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8141 | mState = STOPPED; |
| 8142 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8143 | case STARTING: |
| 8144 | mState = IDLE; |
| 8145 | break; |
| 8146 | case ACTIVE: |
| 8147 | mState = STOPPING; |
| 8148 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8149 | case DESTROYED: |
| 8150 | return NO_ERROR; // simply ignore as we are being destroyed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8151 | } |
| 8152 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 8153 | sp<EffectHandle> h = mHandles[i].promote(); |
| 8154 | if (h != 0) { |
| 8155 | h->setEnabled(enabled); |
| 8156 | } |
| 8157 | } |
| 8158 | } |
| 8159 | return NO_ERROR; |
| 8160 | } |
| 8161 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 8162 | bool AudioFlinger::EffectModule::isEnabled() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8163 | { |
| 8164 | switch (mState) { |
| 8165 | case RESTART: |
| 8166 | case STARTING: |
| 8167 | case ACTIVE: |
| 8168 | return true; |
| 8169 | case IDLE: |
| 8170 | case STOPPING: |
| 8171 | case STOPPED: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8172 | case DESTROYED: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8173 | default: |
| 8174 | return false; |
| 8175 | } |
| 8176 | } |
| 8177 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 8178 | bool AudioFlinger::EffectModule::isProcessEnabled() const |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8179 | { |
| 8180 | switch (mState) { |
| 8181 | case RESTART: |
| 8182 | case ACTIVE: |
| 8183 | case STOPPING: |
| 8184 | case STOPPED: |
| 8185 | return true; |
| 8186 | case IDLE: |
| 8187 | case STARTING: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8188 | case DESTROYED: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8189 | default: |
| 8190 | return false; |
| 8191 | } |
| 8192 | } |
| 8193 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8194 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 8195 | { |
| 8196 | Mutex::Autolock _l(mLock); |
| 8197 | status_t status = NO_ERROR; |
| 8198 | |
| 8199 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 8200 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8201 | if (isProcessEnabled() && |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8202 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 8203 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8204 | status_t cmdStatus; |
| 8205 | uint32_t volume[2]; |
| 8206 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8207 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8208 | volume[0] = *left; |
| 8209 | volume[1] = *right; |
| 8210 | if (controller) { |
| 8211 | pVolume = volume; |
| 8212 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8213 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8214 | EFFECT_CMD_SET_VOLUME, |
| 8215 | size, |
| 8216 | volume, |
| 8217 | &size, |
| 8218 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8219 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 8220 | *left = volume[0]; |
| 8221 | *right = volume[1]; |
| 8222 | } |
| 8223 | } |
| 8224 | return status; |
| 8225 | } |
| 8226 | |
| 8227 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 8228 | { |
| 8229 | Mutex::Autolock _l(mLock); |
| 8230 | status_t status = NO_ERROR; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 8231 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 8232 | // audio pre processing modules on RecordThread can receive both output and |
| 8233 | // input device indication in the same call |
| 8234 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 8235 | if (dev) { |
| 8236 | status_t cmdStatus; |
| 8237 | uint32_t size = sizeof(status_t); |
| 8238 | |
| 8239 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8240 | EFFECT_CMD_SET_DEVICE, |
| 8241 | sizeof(uint32_t), |
| 8242 | &dev, |
| 8243 | &size, |
| 8244 | &cmdStatus); |
| 8245 | if (status == NO_ERROR) { |
| 8246 | status = cmdStatus; |
| 8247 | } |
| 8248 | } |
| 8249 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 8250 | if (dev) { |
| 8251 | status_t cmdStatus; |
| 8252 | uint32_t size = sizeof(status_t); |
| 8253 | |
| 8254 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 8255 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 8256 | sizeof(uint32_t), |
| 8257 | &dev, |
| 8258 | &size, |
| 8259 | &cmdStatus); |
| 8260 | if (status2 == NO_ERROR) { |
| 8261 | status2 = cmdStatus; |
| 8262 | } |
| 8263 | if (status == NO_ERROR) { |
| 8264 | status = status2; |
| 8265 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8266 | } |
| 8267 | } |
| 8268 | return status; |
| 8269 | } |
| 8270 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8271 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8272 | { |
| 8273 | Mutex::Autolock _l(mLock); |
| 8274 | status_t status = NO_ERROR; |
| 8275 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8276 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8277 | uint32_t size = sizeof(status_t); |
| 8278 | status = (*mEffectInterface)->command(mEffectInterface, |
| 8279 | EFFECT_CMD_SET_AUDIO_MODE, |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8280 | sizeof(audio_mode_t), |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8281 | &mode, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8282 | &size, |
| 8283 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8284 | if (status == NO_ERROR) { |
| 8285 | status = cmdStatus; |
| 8286 | } |
| 8287 | } |
| 8288 | return status; |
| 8289 | } |
| 8290 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8291 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 8292 | { |
| 8293 | Mutex::Autolock _l(mLock); |
| 8294 | mSuspended = suspended; |
| 8295 | } |
Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 8296 | |
| 8297 | bool AudioFlinger::EffectModule::suspended() const |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8298 | { |
| 8299 | Mutex::Autolock _l(mLock); |
| 8300 | return mSuspended; |
| 8301 | } |
| 8302 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8303 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 8304 | { |
| 8305 | const size_t SIZE = 256; |
| 8306 | char buffer[SIZE]; |
| 8307 | String8 result; |
| 8308 | |
| 8309 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 8310 | result.append(buffer); |
| 8311 | |
| 8312 | bool locked = tryLock(mLock); |
| 8313 | // failed to lock - AudioFlinger is probably deadlocked |
| 8314 | if (!locked) { |
| 8315 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 8316 | } |
| 8317 | |
| 8318 | result.append("\t\tSession Status State Engine:\n"); |
| 8319 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 8320 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 8321 | result.append(buffer); |
| 8322 | |
| 8323 | result.append("\t\tDescriptor:\n"); |
| 8324 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 8325 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 8326 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 8327 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 8328 | result.append(buffer); |
| 8329 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 8330 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 8331 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 8332 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 8333 | result.append(buffer); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 8334 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8335 | mDescriptor.apiVersion, |
| 8336 | mDescriptor.flags); |
| 8337 | result.append(buffer); |
| 8338 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 8339 | mDescriptor.name); |
| 8340 | result.append(buffer); |
| 8341 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 8342 | mDescriptor.implementor); |
| 8343 | result.append(buffer); |
| 8344 | |
| 8345 | result.append("\t\t- Input configuration:\n"); |
| 8346 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 8347 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 8348 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 8349 | mConfig.inputCfg.buffer.frameCount, |
| 8350 | mConfig.inputCfg.samplingRate, |
| 8351 | mConfig.inputCfg.channels, |
| 8352 | mConfig.inputCfg.format); |
| 8353 | result.append(buffer); |
| 8354 | |
| 8355 | result.append("\t\t- Output configuration:\n"); |
| 8356 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 8357 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 8358 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 8359 | mConfig.outputCfg.buffer.frameCount, |
| 8360 | mConfig.outputCfg.samplingRate, |
| 8361 | mConfig.outputCfg.channels, |
| 8362 | mConfig.outputCfg.format); |
| 8363 | result.append(buffer); |
| 8364 | |
| 8365 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 8366 | result.append(buffer); |
| 8367 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 8368 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 8369 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 8370 | if (handle != 0) { |
| 8371 | handle->dump(buffer, SIZE); |
| 8372 | result.append(buffer); |
| 8373 | } |
| 8374 | } |
| 8375 | |
| 8376 | result.append("\n"); |
| 8377 | |
| 8378 | write(fd, result.string(), result.length()); |
| 8379 | |
| 8380 | if (locked) { |
| 8381 | mLock.unlock(); |
| 8382 | } |
| 8383 | |
| 8384 | return NO_ERROR; |
| 8385 | } |
| 8386 | |
| 8387 | // ---------------------------------------------------------------------------- |
| 8388 | // EffectHandle implementation |
| 8389 | // ---------------------------------------------------------------------------- |
| 8390 | |
| 8391 | #undef LOG_TAG |
| 8392 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 8393 | |
| 8394 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 8395 | const sp<AudioFlinger::Client>& client, |
| 8396 | const sp<IEffectClient>& effectClient, |
| 8397 | int32_t priority) |
| 8398 | : BnEffect(), |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8399 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8400 | mPriority(priority), mHasControl(false), mEnabled(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8401 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8402 | ALOGV("constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8403 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8404 | if (client == 0) { |
| 8405 | return; |
| 8406 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8407 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 8408 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 8409 | if (mCblkMemory != 0) { |
| 8410 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 8411 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8412 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8413 | new(mCblk) effect_param_cblk_t(); |
| 8414 | mBuffer = (uint8_t *)mCblk + bufOffset; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 8415 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8416 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 8417 | ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8418 | return; |
| 8419 | } |
| 8420 | } |
| 8421 | |
| 8422 | AudioFlinger::EffectHandle::~EffectHandle() |
| 8423 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8424 | ALOGV("Destructor %p", this); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8425 | disconnect(false); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8426 | ALOGV("Destructor DONE %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8427 | } |
| 8428 | |
| 8429 | status_t AudioFlinger::EffectHandle::enable() |
| 8430 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8431 | ALOGV("enable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8432 | if (!mHasControl) return INVALID_OPERATION; |
| 8433 | if (mEffect == 0) return DEAD_OBJECT; |
| 8434 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8435 | if (mEnabled) { |
| 8436 | return NO_ERROR; |
| 8437 | } |
| 8438 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8439 | mEnabled = true; |
| 8440 | |
| 8441 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8442 | if (thread != 0) { |
| 8443 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 8444 | } |
| 8445 | |
| 8446 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 8447 | if (mEffect->suspended()) { |
| 8448 | return NO_ERROR; |
| 8449 | } |
| 8450 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8451 | status_t status = mEffect->setEnabled(true); |
| 8452 | if (status != NO_ERROR) { |
| 8453 | if (thread != 0) { |
| 8454 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8455 | } |
| 8456 | mEnabled = false; |
| 8457 | } |
| 8458 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8459 | } |
| 8460 | |
| 8461 | status_t AudioFlinger::EffectHandle::disable() |
| 8462 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8463 | ALOGV("disable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8464 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8465 | if (mEffect == 0) return DEAD_OBJECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8466 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8467 | if (!mEnabled) { |
| 8468 | return NO_ERROR; |
| 8469 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8470 | mEnabled = false; |
| 8471 | |
| 8472 | if (mEffect->suspended()) { |
| 8473 | return NO_ERROR; |
| 8474 | } |
| 8475 | |
| 8476 | status_t status = mEffect->setEnabled(false); |
| 8477 | |
| 8478 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8479 | if (thread != 0) { |
| 8480 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8481 | } |
| 8482 | |
| 8483 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8484 | } |
| 8485 | |
| 8486 | void AudioFlinger::EffectHandle::disconnect() |
| 8487 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8488 | disconnect(true); |
| 8489 | } |
| 8490 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8491 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8492 | { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8493 | ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8494 | if (mEffect == 0) { |
| 8495 | return; |
| 8496 | } |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 8497 | mEffect->disconnect(this, unpinIfLast); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8498 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 8499 | if (mHasControl && mEnabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8500 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 8501 | if (thread != 0) { |
| 8502 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 8503 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8504 | } |
| 8505 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8506 | // release sp on module => module destructor can be called now |
| 8507 | mEffect.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8508 | if (mClient != 0) { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8509 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 8510 | // unlike ~TrackBase(), mCblk is never a local new, so don't delete |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8511 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 8512 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 8513 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 8514 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8515 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 8516 | mClient.clear(); |
| 8517 | } |
| 8518 | } |
| 8519 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8520 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 8521 | uint32_t cmdSize, |
| 8522 | void *pCmdData, |
| 8523 | uint32_t *replySize, |
| 8524 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8525 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8526 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8527 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8528 | |
| 8529 | // only get parameter command is permitted for applications not controlling the effect |
| 8530 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 8531 | return INVALID_OPERATION; |
| 8532 | } |
| 8533 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8534 | if (mClient == 0) return INVALID_OPERATION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8535 | |
| 8536 | // handle commands that are not forwarded transparently to effect engine |
| 8537 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 8538 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 8539 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 8540 | Mutex::Autolock _l(mCblk->lock); |
| 8541 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 8542 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 8543 | mCblk->serverIndex = 0; |
| 8544 | mCblk->clientIndex = 0; |
| 8545 | return BAD_VALUE; |
| 8546 | } |
| 8547 | status_t status = NO_ERROR; |
| 8548 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 8549 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8550 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8551 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 8552 | int size = *p++; |
| 8553 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8554 | ALOGW("command(): invalid parameter block size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8555 | break; |
| 8556 | } |
| 8557 | effect_param_t *param = (effect_param_t *)p; |
| 8558 | if (param->psize == 0 || param->vsize == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8559 | ALOGW("command(): null parameter or value size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8560 | mCblk->serverIndex += size; |
| 8561 | continue; |
| 8562 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8563 | uint32_t psize = sizeof(effect_param_t) + |
| 8564 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 8565 | param->vsize; |
| 8566 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 8567 | psize, |
| 8568 | p, |
| 8569 | &rsize, |
| 8570 | &reply); |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8571 | // stop at first error encountered |
| 8572 | if (ret != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8573 | status = ret; |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8574 | *(int *)pReplyData = reply; |
| 8575 | break; |
| 8576 | } else if (reply != NO_ERROR) { |
| 8577 | *(int *)pReplyData = reply; |
| 8578 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8579 | } |
| 8580 | mCblk->serverIndex += size; |
| 8581 | } |
| 8582 | mCblk->serverIndex = 0; |
| 8583 | mCblk->clientIndex = 0; |
| 8584 | return status; |
| 8585 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8586 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8587 | return enable(); |
| 8588 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 8589 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8590 | return disable(); |
| 8591 | } |
| 8592 | |
| 8593 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 8594 | } |
| 8595 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8596 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8597 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8598 | ALOGV("setControl %p control %d", this, hasControl); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8599 | |
| 8600 | mHasControl = hasControl; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8601 | mEnabled = enabled; |
| 8602 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8603 | if (signal && mEffectClient != 0) { |
| 8604 | mEffectClient->controlStatusChanged(hasControl); |
| 8605 | } |
| 8606 | } |
| 8607 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 8608 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 8609 | uint32_t cmdSize, |
| 8610 | void *pCmdData, |
| 8611 | uint32_t replySize, |
| 8612 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8613 | { |
| 8614 | if (mEffectClient != 0) { |
| 8615 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 8616 | } |
| 8617 | } |
| 8618 | |
| 8619 | |
| 8620 | |
| 8621 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 8622 | { |
| 8623 | if (mEffectClient != 0) { |
| 8624 | mEffectClient->enableStatusChanged(enabled); |
| 8625 | } |
| 8626 | } |
| 8627 | |
| 8628 | status_t AudioFlinger::EffectHandle::onTransact( |
| 8629 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 8630 | { |
| 8631 | return BnEffect::onTransact(code, data, reply, flags); |
| 8632 | } |
| 8633 | |
| 8634 | |
| 8635 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 8636 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 8637 | bool locked = mCblk != NULL && tryLock(mCblk->lock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8638 | |
| 8639 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 8640 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8641 | mPriority, |
| 8642 | mHasControl, |
| 8643 | !locked, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 8644 | mCblk ? mCblk->clientIndex : 0, |
| 8645 | mCblk ? mCblk->serverIndex : 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8646 | ); |
| 8647 | |
| 8648 | if (locked) { |
| 8649 | mCblk->lock.unlock(); |
| 8650 | } |
| 8651 | } |
| 8652 | |
| 8653 | #undef LOG_TAG |
| 8654 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 8655 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8656 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8657 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8658 | : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 8659 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 8660 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8661 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 8662 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 8663 | if (thread == NULL) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8664 | return; |
| 8665 | } |
| 8666 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 8667 | thread->frameCount(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8668 | } |
| 8669 | |
| 8670 | AudioFlinger::EffectChain::~EffectChain() |
| 8671 | { |
| 8672 | if (mOwnInBuffer) { |
| 8673 | delete mInBuffer; |
| 8674 | } |
| 8675 | |
| 8676 | } |
| 8677 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8678 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8679 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8680 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8681 | size_t size = mEffects.size(); |
| 8682 | |
| 8683 | for (size_t i = 0; i < size; i++) { |
| 8684 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8685 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8686 | } |
| 8687 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8688 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8689 | } |
| 8690 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8691 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8692 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8693 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8694 | size_t size = mEffects.size(); |
| 8695 | |
| 8696 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8697 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 8698 | if (id == 0 || mEffects[i]->id() == id) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8699 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8700 | } |
| 8701 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8702 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8703 | } |
| 8704 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8705 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 8706 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 8707 | const effect_uuid_t *type) |
| 8708 | { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8709 | size_t size = mEffects.size(); |
| 8710 | |
| 8711 | for (size_t i = 0; i < size; i++) { |
| 8712 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8713 | return mEffects[i]; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8714 | } |
| 8715 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8716 | return 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8717 | } |
| 8718 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8719 | // Must be called with EffectChain::mLock locked |
| 8720 | void AudioFlinger::EffectChain::process_l() |
| 8721 | { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8722 | sp<ThreadBase> thread = mThread.promote(); |
| 8723 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8724 | ALOGW("process_l(): cannot promote mixer thread"); |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8725 | return; |
| 8726 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 8727 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 8728 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8729 | // always process effects unless no more tracks are on the session and the effect tail |
| 8730 | // has been rendered |
| 8731 | bool doProcess = true; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8732 | if (!isGlobalSession) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8733 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 8734 | |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8735 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 8736 | doProcess = false; |
| 8737 | } |
| 8738 | |
| 8739 | if (activeTrackCnt() == 0) { |
| 8740 | // if no track is active and the effect tail has not been rendered, |
| 8741 | // the input buffer must be cleared here as the mixer process will not do it |
| 8742 | if (tracksOnSession || mTailBufferCount > 0) { |
| 8743 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 8744 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 8745 | if (mTailBufferCount > 0) { |
| 8746 | mTailBufferCount--; |
| 8747 | } |
| 8748 | } |
| 8749 | } |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8750 | } |
| 8751 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8752 | size_t size = mEffects.size(); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 8753 | if (doProcess) { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 8754 | for (size_t i = 0; i < size; i++) { |
| 8755 | mEffects[i]->process(); |
| 8756 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8757 | } |
| 8758 | for (size_t i = 0; i < size; i++) { |
| 8759 | mEffects[i]->updateState(); |
| 8760 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8761 | } |
| 8762 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8763 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8764 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8765 | { |
| 8766 | effect_descriptor_t desc = effect->desc(); |
| 8767 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 8768 | |
| 8769 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8770 | effect->setChain(this); |
| 8771 | sp<ThreadBase> thread = mThread.promote(); |
| 8772 | if (thread == 0) { |
| 8773 | return NO_INIT; |
| 8774 | } |
| 8775 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8776 | |
| 8777 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 8778 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 8779 | // they are processed first and accumulated in chain input buffer |
| 8780 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 8781 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8782 | // the input buffer for auxiliary effect contains mono samples in |
| 8783 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 8784 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 8785 | // calling the process in effect engine |
| 8786 | size_t numSamples = thread->frameCount(); |
| 8787 | int32_t *buffer = new int32_t[numSamples]; |
| 8788 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 8789 | effect->setInBuffer((int16_t *)buffer); |
| 8790 | // auxiliary effects output samples to chain input buffer for further processing |
| 8791 | // by insert effects |
| 8792 | effect->setOutBuffer(mInBuffer); |
| 8793 | } else { |
| 8794 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 8795 | // after track and auxiliary effects. |
| 8796 | // Insert effect order as a function of indicated preference: |
| 8797 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 8798 | // another effect is present |
| 8799 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 8800 | // last effect claiming first position |
| 8801 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 8802 | // first effect claiming last position |
| 8803 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 8804 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 8805 | // already present |
| 8806 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8807 | size_t size = mEffects.size(); |
| 8808 | size_t idx_insert = size; |
| 8809 | ssize_t idx_insert_first = -1; |
| 8810 | ssize_t idx_insert_last = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8811 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8812 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8813 | effect_descriptor_t d = mEffects[i]->desc(); |
| 8814 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 8815 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 8816 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 8817 | // check invalid effect chaining combinations |
| 8818 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 8819 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8820 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8821 | return INVALID_OPERATION; |
| 8822 | } |
| 8823 | // remember position of first insert effect and by default |
| 8824 | // select this as insert position for new effect |
| 8825 | if (idx_insert == size) { |
| 8826 | idx_insert = i; |
| 8827 | } |
| 8828 | // remember position of last insert effect claiming |
| 8829 | // first position |
| 8830 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 8831 | idx_insert_first = i; |
| 8832 | } |
| 8833 | // remember position of first insert effect claiming |
| 8834 | // last position |
| 8835 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 8836 | idx_insert_last == -1) { |
| 8837 | idx_insert_last = i; |
| 8838 | } |
| 8839 | } |
| 8840 | } |
| 8841 | |
| 8842 | // modify idx_insert from first position if needed |
| 8843 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 8844 | if (idx_insert_last != -1) { |
| 8845 | idx_insert = idx_insert_last; |
| 8846 | } else { |
| 8847 | idx_insert = size; |
| 8848 | } |
| 8849 | } else { |
| 8850 | if (idx_insert_first != -1) { |
| 8851 | idx_insert = idx_insert_first + 1; |
| 8852 | } |
| 8853 | } |
| 8854 | |
| 8855 | // always read samples from chain input buffer |
| 8856 | effect->setInBuffer(mInBuffer); |
| 8857 | |
| 8858 | // if last effect in the chain, output samples to chain |
| 8859 | // output buffer, otherwise to chain input buffer |
| 8860 | if (idx_insert == size) { |
| 8861 | if (idx_insert != 0) { |
| 8862 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 8863 | mEffects[idx_insert-1]->configure(); |
| 8864 | } |
| 8865 | effect->setOutBuffer(mOutBuffer); |
| 8866 | } else { |
| 8867 | effect->setOutBuffer(mInBuffer); |
| 8868 | } |
| 8869 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8870 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8871 | ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8872 | } |
| 8873 | effect->configure(); |
| 8874 | return NO_ERROR; |
| 8875 | } |
| 8876 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8877 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 8878 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8879 | { |
| 8880 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8881 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8882 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 8883 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8884 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8885 | if (effect == mEffects[i]) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 8886 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 8887 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 8888 | // the middle of a read from audio HAL |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 8889 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 8890 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 8891 | mEffects[i]->stop(); |
| 8892 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8893 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 8894 | delete[] effect->inBuffer(); |
| 8895 | } else { |
| 8896 | if (i == size - 1 && i != 0) { |
| 8897 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 8898 | mEffects[i - 1]->configure(); |
| 8899 | } |
| 8900 | } |
| 8901 | mEffects.removeAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8902 | ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8903 | break; |
| 8904 | } |
| 8905 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8906 | |
| 8907 | return mEffects.size(); |
| 8908 | } |
| 8909 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8910 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 8911 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8912 | { |
| 8913 | size_t size = mEffects.size(); |
| 8914 | for (size_t i = 0; i < size; i++) { |
| 8915 | mEffects[i]->setDevice(device); |
| 8916 | } |
| 8917 | } |
| 8918 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8919 | // setMode_l() must be called with PlaybackThread::mLock held |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 8920 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8921 | { |
| 8922 | size_t size = mEffects.size(); |
| 8923 | for (size_t i = 0; i < size; i++) { |
| 8924 | mEffects[i]->setMode(mode); |
| 8925 | } |
| 8926 | } |
| 8927 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8928 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 8929 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8930 | { |
| 8931 | uint32_t newLeft = *left; |
| 8932 | uint32_t newRight = *right; |
| 8933 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8934 | int ctrlIdx = -1; |
| 8935 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8936 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8937 | // first update volume controller |
| 8938 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 8939 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8940 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 8941 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8942 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8943 | break; |
| 8944 | } |
| 8945 | } |
| 8946 | |
| 8947 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8948 | if (hasControl) { |
| 8949 | *left = mNewLeftVolume; |
| 8950 | *right = mNewRightVolume; |
| 8951 | } |
| 8952 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8953 | } |
| 8954 | |
| 8955 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8956 | mLeftVolume = newLeft; |
| 8957 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8958 | |
| 8959 | // second get volume update from volume controller |
| 8960 | if (ctrlIdx >= 0) { |
| 8961 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 8962 | mNewLeftVolume = newLeft; |
| 8963 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8964 | } |
| 8965 | // then indicate volume to all other effects in chain. |
| 8966 | // Pass altered volume to effects before volume controller |
| 8967 | // and requested volume to effects after controller |
| 8968 | uint32_t lVol = newLeft; |
| 8969 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8970 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8971 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 8972 | if ((int)i == ctrlIdx) continue; |
| 8973 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 8974 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8975 | lVol = *left; |
| 8976 | rVol = *right; |
| 8977 | } |
| 8978 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 8979 | } |
| 8980 | *left = newLeft; |
| 8981 | *right = newRight; |
| 8982 | |
| 8983 | return hasControl; |
| 8984 | } |
| 8985 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8986 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 8987 | { |
| 8988 | const size_t SIZE = 256; |
| 8989 | char buffer[SIZE]; |
| 8990 | String8 result; |
| 8991 | |
| 8992 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 8993 | result.append(buffer); |
| 8994 | |
| 8995 | bool locked = tryLock(mLock); |
| 8996 | // failed to lock - AudioFlinger is probably deadlocked |
| 8997 | if (!locked) { |
| 8998 | result.append("\tCould not lock mutex:\n"); |
| 8999 | } |
| 9000 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 9001 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 9002 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9003 | mEffects.size(), |
| 9004 | (uint32_t)mInBuffer, |
| 9005 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9006 | mActiveTrackCnt); |
| 9007 | result.append(buffer); |
| 9008 | write(fd, result.string(), result.size()); |
| 9009 | |
| 9010 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 9011 | sp<EffectModule> effect = mEffects[i]; |
| 9012 | if (effect != 0) { |
| 9013 | effect->dump(fd, args); |
| 9014 | } |
| 9015 | } |
| 9016 | |
| 9017 | if (locked) { |
| 9018 | mLock.unlock(); |
| 9019 | } |
| 9020 | |
| 9021 | return NO_ERROR; |
| 9022 | } |
| 9023 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9024 | // must be called with ThreadBase::mLock held |
| 9025 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 9026 | const effect_uuid_t *type, bool suspend) |
| 9027 | { |
| 9028 | sp<SuspendedEffectDesc> desc; |
| 9029 | // use effect type UUID timelow as key as there is no real risk of identical |
| 9030 | // timeLow fields among effect type UUIDs. |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9031 | ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9032 | if (suspend) { |
| 9033 | if (index >= 0) { |
| 9034 | desc = mSuspendedEffects.valueAt(index); |
| 9035 | } else { |
| 9036 | desc = new SuspendedEffectDesc(); |
| 9037 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 9038 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9039 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9040 | } |
| 9041 | if (desc->mRefCount++ == 0) { |
| 9042 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 9043 | if (effect != 0) { |
| 9044 | desc->mEffect = effect; |
| 9045 | effect->setSuspended(true); |
| 9046 | effect->setEnabled(false); |
| 9047 | } |
| 9048 | } |
| 9049 | } else { |
| 9050 | if (index < 0) { |
| 9051 | return; |
| 9052 | } |
| 9053 | desc = mSuspendedEffects.valueAt(index); |
| 9054 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9055 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9056 | desc->mRefCount = 1; |
| 9057 | } |
| 9058 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9059 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9060 | if (desc->mEffect != 0) { |
| 9061 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 9062 | if (effect != 0) { |
| 9063 | effect->setSuspended(false); |
| 9064 | sp<EffectHandle> handle = effect->controlHandle(); |
| 9065 | if (handle != 0) { |
| 9066 | effect->setEnabled(handle->enabled()); |
| 9067 | } |
| 9068 | } |
| 9069 | desc->mEffect.clear(); |
| 9070 | } |
| 9071 | mSuspendedEffects.removeItemsAt(index); |
| 9072 | } |
| 9073 | } |
| 9074 | } |
| 9075 | |
| 9076 | // must be called with ThreadBase::mLock held |
| 9077 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 9078 | { |
| 9079 | sp<SuspendedEffectDesc> desc; |
| 9080 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9081 | ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9082 | if (suspend) { |
| 9083 | if (index >= 0) { |
| 9084 | desc = mSuspendedEffects.valueAt(index); |
| 9085 | } else { |
| 9086 | desc = new SuspendedEffectDesc(); |
| 9087 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9088 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9089 | } |
| 9090 | if (desc->mRefCount++ == 0) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9091 | Vector< sp<EffectModule> > effects; |
| 9092 | getSuspendEligibleEffects(effects); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9093 | for (size_t i = 0; i < effects.size(); i++) { |
| 9094 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 9095 | } |
| 9096 | } |
| 9097 | } else { |
| 9098 | if (index < 0) { |
| 9099 | return; |
| 9100 | } |
| 9101 | desc = mSuspendedEffects.valueAt(index); |
| 9102 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9103 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9104 | desc->mRefCount = 1; |
| 9105 | } |
| 9106 | if (--desc->mRefCount == 0) { |
| 9107 | Vector<const effect_uuid_t *> types; |
| 9108 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 9109 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 9110 | continue; |
| 9111 | } |
| 9112 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 9113 | } |
| 9114 | for (size_t i = 0; i < types.size(); i++) { |
| 9115 | setEffectSuspended_l(types[i], false); |
| 9116 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9117 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9118 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 9119 | } |
| 9120 | } |
| 9121 | } |
| 9122 | |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 9123 | |
| 9124 | // The volume effect is used for automated tests only |
| 9125 | #ifndef OPENSL_ES_H_ |
| 9126 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 9127 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 9128 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 9129 | #endif //OPENSL_ES_H_ |
| 9130 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9131 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 9132 | { |
| 9133 | // auxiliary effects and visualizer are never suspended on output mix |
| 9134 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 9135 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 9136 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 9137 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9138 | return false; |
| 9139 | } |
| 9140 | return true; |
| 9141 | } |
| 9142 | |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9143 | void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects) |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9144 | { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9145 | effects.clear(); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9146 | for (size_t i = 0; i < mEffects.size(); i++) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 9147 | if (isEffectEligibleForSuspend(mEffects[i]->desc())) { |
| 9148 | effects.add(mEffects[i]); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9149 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9150 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9151 | } |
| 9152 | |
| 9153 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 9154 | const effect_uuid_t *type) |
| 9155 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 9156 | sp<EffectModule> effect = getEffectFromType_l(type); |
| 9157 | return effect != 0 && effect->isEnabled() ? effect : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9158 | } |
| 9159 | |
| 9160 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 9161 | bool enabled) |
| 9162 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 9163 | ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9164 | if (enabled) { |
| 9165 | if (index < 0) { |
| 9166 | // if the effect is not suspend check if all effects are suspended |
| 9167 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 9168 | if (index < 0) { |
| 9169 | return; |
| 9170 | } |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9171 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 9172 | return; |
| 9173 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9174 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 9175 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9176 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 9177 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 9178 | return; |
| 9179 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9180 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9181 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9182 | effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9183 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 9184 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 9185 | if (desc->mEffect == 0) { |
| 9186 | desc->mEffect = effect; |
| 9187 | effect->setEnabled(false); |
| 9188 | effect->setSuspended(true); |
| 9189 | } |
| 9190 | } else { |
| 9191 | if (index < 0) { |
| 9192 | return; |
| 9193 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 9194 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 9195 | effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 9196 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 9197 | desc->mEffect.clear(); |
| 9198 | effect->setSuspended(false); |
| 9199 | } |
| 9200 | } |
| 9201 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9202 | #undef LOG_TAG |
| 9203 | #define LOG_TAG "AudioFlinger" |
| 9204 | |
| 9205 | // ---------------------------------------------------------------------------- |
| 9206 | |
| 9207 | status_t AudioFlinger::onTransact( |
| 9208 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 9209 | { |
| 9210 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 9211 | } |
| 9212 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 9213 | }; // namespace android |