Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2012, 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 |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 22 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 23 | #include "Configuration.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 24 | #include <math.h> |
| 25 | #include <fcntl.h> |
Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 26 | #include <linux/futex.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/AudioParameter.h> |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 31 | #include <media/AudioResamplerPublic.h> |
Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 32 | #include <media/RecordBufferConverter.h> |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 33 | #include <media/TypeConverter.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 34 | #include <utils/Log.h> |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 35 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 36 | |
| 37 | #include <private/media/AudioTrackShared.h> |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
Glenn Kasten | 6bf707f | 2017-02-23 16:53:58 -0800 | [diff] [blame] | 39 | #include <audio_utils/mono_blend.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | #include <audio_utils/primitives.h> |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 41 | #include <audio_utils/format.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 42 | #include <audio_utils/minifloat.h> |
Mikhail Naganov | 9fe9401 | 2016-10-14 14:57:40 -0700 | [diff] [blame] | 43 | #include <system/audio_effects/effect_ns.h> |
| 44 | #include <system/audio_effects/effect_aec.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 45 | #include <system/audio.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 46 | |
| 47 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 48 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 49 | #include <media/nbaio/AudioStreamOutSink.h> |
| 50 | #include <media/nbaio/MonoPipe.h> |
| 51 | #include <media/nbaio/MonoPipeReader.h> |
| 52 | #include <media/nbaio/Pipe.h> |
| 53 | #include <media/nbaio/PipeReader.h> |
| 54 | #include <media/nbaio/SourceAudioBufferProvider.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 55 | #include <mediautils/BatteryNotifier.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 56 | |
| 57 | #include <powermanager/PowerManager.h> |
| 58 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | #include "AudioFlinger.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 60 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 61 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "ServiceUtilities.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 63 | #include "mediautils/SchedulingPolicyService.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 65 | #ifdef ADD_BATTERY_DATA |
| 66 | #include <media/IMediaPlayerService.h> |
| 67 | #include <media/IMediaDeathNotifier.h> |
| 68 | #endif |
| 69 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 70 | #ifdef DEBUG_CPU_USAGE |
| 71 | #include <cpustats/CentralTendencyStatistics.h> |
| 72 | #include <cpustats/ThreadCpuUsage.h> |
| 73 | #endif |
| 74 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 75 | #include "AutoPark.h" |
| 76 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 77 | #include <pthread.h> |
| 78 | #include "TypedLogger.h" |
| 79 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 80 | // ---------------------------------------------------------------------------- |
| 81 | |
| 82 | // Note: the following macro is used for extremely verbose logging message. In |
| 83 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 84 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 85 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 86 | // turned on. Do not uncomment the #def below unless you really know what you |
| 87 | // are doing and want to see all of the extremely verbose messages. |
| 88 | //#define VERY_VERY_VERBOSE_LOGGING |
| 89 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 90 | #define ALOGVV ALOGV |
| 91 | #else |
| 92 | #define ALOGVV(a...) do { } while(0) |
| 93 | #endif |
| 94 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 95 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 96 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 97 | template <typename T> |
| 98 | static inline T min(const T& a, const T& b) |
| 99 | { |
| 100 | return a < b ? a : b; |
| 101 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 102 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 103 | namespace android { |
| 104 | |
| 105 | // retry counts for buffer fill timeout |
| 106 | // 50 * ~20msecs = 1 second |
| 107 | static const int8_t kMaxTrackRetries = 50; |
| 108 | static const int8_t kMaxTrackStartupRetries = 50; |
| 109 | // allow less retry attempts on direct output thread. |
| 110 | // direct outputs can be a scarce resource in audio hardware and should |
| 111 | // be released as quickly as possible. |
| 112 | static const int8_t kMaxTrackRetriesDirect = 2; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 113 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 114 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 115 | |
| 116 | // don't warn about blocked writes or record buffer overflows more often than this |
| 117 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 118 | |
| 119 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 120 | static const int kRecordThreadSleepUs = 5000; |
| 121 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 122 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 123 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 124 | |
| 125 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 126 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 127 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 128 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 129 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 130 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 131 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 132 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 133 | // maximum normal sink buffer size |
| 134 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 135 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 136 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 137 | // FIXME This should be based on experimentally observed scheduling jitter |
| 138 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 139 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 140 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 141 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 142 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 143 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 144 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 145 | |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 146 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good |
| 147 | // balance between power consumption and latency, and allows threads to be scheduled reliably |
| 148 | // by the CFS scheduler. |
| 149 | // FIXME Express other hardcoded references to 20ms with references to this constant and move |
| 150 | // it appropriately. |
| 151 | #define FMS_20 20 |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 152 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 153 | // Whether to use fast mixer |
| 154 | static const enum { |
| 155 | FastMixer_Never, // never initialize or use: for debugging only |
| 156 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 157 | // normal mixer multiplier is 1 |
| 158 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 159 | // multiplier is calculated based on min & max normal mixer buffer size |
| 160 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 161 | // multiplier is calculated based on min & max normal mixer buffer size |
| 162 | // FIXME for FastMixer_Dynamic: |
| 163 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 164 | // For example, one HAL implementation returns an error from a large write, |
| 165 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 166 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 167 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 168 | } kUseFastMixer = FastMixer_Static; |
| 169 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 170 | // Whether to use fast capture |
| 171 | static const enum { |
| 172 | FastCapture_Never, // never initialize or use: for debugging only |
| 173 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 174 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 175 | } kUseFastCapture = FastCapture_Static; |
| 176 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 177 | // Priorities for requestPriority |
| 178 | static const int kPriorityAudioApp = 2; |
| 179 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 180 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 181 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 182 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 183 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 184 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 185 | |
| 186 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 187 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 188 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 189 | // The minimum and maximum allowed values |
| 190 | static const int kFastTrackMultiplierMin = 1; |
| 191 | static const int kFastTrackMultiplierMax = 2; |
| 192 | |
| 193 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 194 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 195 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 196 | // See Thread::readOnlyHeap(). |
| 197 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 198 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 199 | // and that all "fast" AudioRecord clients read from. In either case, the size can be small. |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 200 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 201 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 202 | // ---------------------------------------------------------------------------- |
| 203 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 204 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 205 | |
| 206 | static void sFastTrackMultiplierInit() |
| 207 | { |
| 208 | char value[PROPERTY_VALUE_MAX]; |
| 209 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 210 | char *endptr; |
| 211 | unsigned long ul = strtoul(value, &endptr, 0); |
| 212 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 213 | sFastTrackMultiplier = (int) ul; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // ---------------------------------------------------------------------------- |
| 219 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 220 | #ifdef ADD_BATTERY_DATA |
| 221 | // To collect the amplifier usage |
| 222 | static void addBatteryData(uint32_t params) { |
| 223 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 224 | if (service == NULL) { |
| 225 | // it already logged |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | service->addBatteryData(params); |
| 230 | } |
| 231 | #endif |
| 232 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 233 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 234 | struct { |
| 235 | // call when you acquire a partial wakelock |
| 236 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 237 | pthread_mutex_lock(&mLock); |
| 238 | if (wakeLockToken.get() == nullptr) { |
| 239 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 240 | } else { |
| 241 | if (mCount == 0) { |
| 242 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 243 | } |
| 244 | ++mCount; |
| 245 | } |
| 246 | pthread_mutex_unlock(&mLock); |
| 247 | } |
| 248 | |
| 249 | // call when you release a partial wakelock. |
| 250 | void release(const sp<IBinder> &wakeLockToken) { |
| 251 | if (wakeLockToken.get() == nullptr) { |
| 252 | return; |
| 253 | } |
| 254 | pthread_mutex_lock(&mLock); |
| 255 | if (--mCount < 0) { |
| 256 | ALOGE("negative wakelock count"); |
| 257 | mCount = 0; |
| 258 | } |
| 259 | pthread_mutex_unlock(&mLock); |
| 260 | } |
| 261 | |
| 262 | // retrieves the boottime timebase offset from monotonic. |
| 263 | int64_t getBoottimeOffset() { |
| 264 | pthread_mutex_lock(&mLock); |
| 265 | int64_t boottimeOffset = mBoottimeOffset; |
| 266 | pthread_mutex_unlock(&mLock); |
| 267 | return boottimeOffset; |
| 268 | } |
| 269 | |
| 270 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 271 | // and the selected timebase. |
| 272 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 273 | // |
| 274 | // This only needs to be called upon acquiring the first partial wakelock |
| 275 | // after all other partial wakelocks are released. |
| 276 | // |
| 277 | // We do an empirical measurement of the offset rather than parsing |
| 278 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 279 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 280 | int clockbase; |
| 281 | switch (timebase) { |
| 282 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 283 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 284 | break; |
| 285 | default: |
| 286 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 287 | break; |
| 288 | } |
| 289 | // try three times to get the clock offset, choose the one |
| 290 | // with the minimum gap in measurements. |
| 291 | const int tries = 3; |
| 292 | nsecs_t bestGap, measured; |
| 293 | for (int i = 0; i < tries; ++i) { |
| 294 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 295 | const nsecs_t tbase = systemTime(clockbase); |
| 296 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 297 | const nsecs_t gap = tmono2 - tmono; |
| 298 | if (i == 0 || gap < bestGap) { |
| 299 | bestGap = gap; |
| 300 | measured = tbase - ((tmono + tmono2) >> 1); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // to avoid micro-adjusting, we don't change the timebase |
| 305 | // unless it is significantly different. |
| 306 | // |
| 307 | // Assumption: It probably takes more than toleranceNs to |
| 308 | // suspend and resume the device. |
| 309 | static int64_t toleranceNs = 10000; // 10 us |
| 310 | if (llabs(*offset - measured) > toleranceNs) { |
| 311 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 312 | (long long)*offset, (long long)measured); |
| 313 | *offset = measured; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | pthread_mutex_t mLock; |
| 318 | int32_t mCount; |
| 319 | int64_t mBoottimeOffset; |
| 320 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 321 | |
| 322 | // ---------------------------------------------------------------------------- |
| 323 | // CPU Stats |
| 324 | // ---------------------------------------------------------------------------- |
| 325 | |
| 326 | class CpuStats { |
| 327 | public: |
| 328 | CpuStats(); |
| 329 | void sample(const String8 &title); |
| 330 | #ifdef DEBUG_CPU_USAGE |
| 331 | private: |
| 332 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 333 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 334 | |
| 335 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 336 | |
| 337 | int mCpuNum; // thread's current CPU number |
| 338 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 339 | #endif |
| 340 | }; |
| 341 | |
| 342 | CpuStats::CpuStats() |
| 343 | #ifdef DEBUG_CPU_USAGE |
| 344 | : mCpuNum(-1), mCpukHz(-1) |
| 345 | #endif |
| 346 | { |
| 347 | } |
| 348 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 349 | void CpuStats::sample(const String8 &title |
| 350 | #ifndef DEBUG_CPU_USAGE |
| 351 | __unused |
| 352 | #endif |
| 353 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 354 | #ifdef DEBUG_CPU_USAGE |
| 355 | // get current thread's delta CPU time in wall clock ns |
| 356 | double wcNs; |
| 357 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 358 | |
| 359 | // record sample for wall clock statistics |
| 360 | if (valid) { |
| 361 | mWcStats.sample(wcNs); |
| 362 | } |
| 363 | |
| 364 | // get the current CPU number |
| 365 | int cpuNum = sched_getcpu(); |
| 366 | |
| 367 | // get the current CPU frequency in kHz |
| 368 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 369 | |
| 370 | // check if either CPU number or frequency changed |
| 371 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 372 | mCpuNum = cpuNum; |
| 373 | mCpukHz = cpukHz; |
| 374 | // ignore sample for purposes of cycles |
| 375 | valid = false; |
| 376 | } |
| 377 | |
| 378 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 379 | if (valid && mCpukHz > 0) { |
| 380 | double cycles = wcNs * cpukHz * 0.000001; |
| 381 | mHzStats.sample(cycles); |
| 382 | } |
| 383 | |
| 384 | unsigned n = mWcStats.n(); |
| 385 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 386 | if ((n & 127) == 1) { |
| 387 | long long elapsed = mCpuUsage.elapsed(); |
| 388 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 389 | double perLoop = elapsed / (double) n; |
| 390 | double perLoop100 = perLoop * 0.01; |
| 391 | double perLoop1k = perLoop * 0.001; |
| 392 | double mean = mWcStats.mean(); |
| 393 | double stddev = mWcStats.stddev(); |
| 394 | double minimum = mWcStats.minimum(); |
| 395 | double maximum = mWcStats.maximum(); |
| 396 | double meanCycles = mHzStats.mean(); |
| 397 | double stddevCycles = mHzStats.stddev(); |
| 398 | double minCycles = mHzStats.minimum(); |
| 399 | double maxCycles = mHzStats.maximum(); |
| 400 | mCpuUsage.resetElapsed(); |
| 401 | mWcStats.reset(); |
| 402 | mHzStats.reset(); |
| 403 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 404 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 405 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 406 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 407 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 408 | title.string(), |
| 409 | elapsed * .000000001, n, perLoop * .000001, |
| 410 | mean * .001, |
| 411 | stddev * .001, |
| 412 | minimum * .001, |
| 413 | maximum * .001, |
| 414 | mean / perLoop100, |
| 415 | stddev / perLoop100, |
| 416 | minimum / perLoop100, |
| 417 | maximum / perLoop100, |
| 418 | meanCycles / perLoop1k, |
| 419 | stddevCycles / perLoop1k, |
| 420 | minCycles / perLoop1k, |
| 421 | maxCycles / perLoop1k); |
| 422 | |
| 423 | } |
| 424 | } |
| 425 | #endif |
| 426 | }; |
| 427 | |
| 428 | // ---------------------------------------------------------------------------- |
| 429 | // ThreadBase |
| 430 | // ---------------------------------------------------------------------------- |
| 431 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 432 | // static |
| 433 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 434 | { |
| 435 | switch (type) { |
| 436 | case MIXER: |
| 437 | return "MIXER"; |
| 438 | case DIRECT: |
| 439 | return "DIRECT"; |
| 440 | case DUPLICATING: |
| 441 | return "DUPLICATING"; |
| 442 | case RECORD: |
| 443 | return "RECORD"; |
| 444 | case OFFLOAD: |
| 445 | return "OFFLOAD"; |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 446 | case MMAP: |
| 447 | return "MMAP"; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 448 | default: |
| 449 | return "unknown"; |
| 450 | } |
| 451 | } |
| 452 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 453 | std::string devicesToString(audio_devices_t devices) |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 454 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 455 | std::string result; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 456 | if (devices & AUDIO_DEVICE_BIT_IN) { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 457 | InputDeviceConverter::maskToString(devices, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 458 | } else { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 459 | OutputDeviceConverter::maskToString(devices, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 460 | } |
| 461 | return result; |
| 462 | } |
| 463 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 464 | std::string inputFlagsToString(audio_input_flags_t flags) |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 465 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 466 | std::string result; |
| 467 | InputFlagConverter::maskToString(flags, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 468 | return result; |
| 469 | } |
| 470 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 471 | std::string outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 472 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 473 | std::string result; |
| 474 | OutputFlagConverter::maskToString(flags, result); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 475 | return result; |
| 476 | } |
| 477 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 478 | const char *sourceToString(audio_source_t source) |
| 479 | { |
| 480 | switch (source) { |
| 481 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 482 | case AUDIO_SOURCE_MIC: return "mic"; |
| 483 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 484 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 485 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 486 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 487 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 488 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 489 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 490 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 491 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 492 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 493 | default: return "unknown"; |
| 494 | } |
| 495 | } |
| 496 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 497 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 498 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 499 | : Thread(false /*canCallJava*/), |
| 500 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 501 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 502 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 503 | // are set by PlaybackThread::readOutputParameters_l() or |
| 504 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 505 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 506 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 507 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 508 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 509 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 510 | mDeathRecipient(new PMDeathRecipient(this)), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 511 | mSystemReady(systemReady), |
| 512 | mSignalPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 513 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 514 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | AudioFlinger::ThreadBase::~ThreadBase() |
| 518 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 519 | // mConfigEvents should be empty, but just in case it isn't, free the memory it owns |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 520 | mConfigEvents.clear(); |
| 521 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 | // do not lock the mutex in destructor |
| 523 | releaseWakeLock_l(); |
| 524 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 525 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 526 | binder->unlinkToDeath(mDeathRecipient); |
| 527 | } |
| 528 | } |
| 529 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 530 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 531 | { |
| 532 | status_t status = initCheck(); |
| 533 | if (status == NO_ERROR) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 534 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 535 | } else { |
| 536 | ALOGE("No working audio driver found."); |
| 537 | } |
| 538 | return status; |
| 539 | } |
| 540 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 541 | void AudioFlinger::ThreadBase::exit() |
| 542 | { |
| 543 | ALOGV("ThreadBase::exit"); |
| 544 | // do any cleanup required for exit to succeed |
| 545 | preExit(); |
| 546 | { |
| 547 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 548 | // if (!exitPending()) { |
| 549 | // // context switch from here to exit() |
| 550 | // // exit() calls requestExit(), what exitPending() observes |
| 551 | // // exit() calls signal(), which is dropped since no waiters |
| 552 | // // context switch back from exit() to here |
| 553 | // mWaitWorkCV.wait(...); |
| 554 | // // now thread is hung |
| 555 | // } |
| 556 | AutoMutex lock(mLock); |
| 557 | requestExit(); |
| 558 | mWaitWorkCV.broadcast(); |
| 559 | } |
| 560 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 561 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 562 | requestExitAndWait(); |
| 563 | } |
| 564 | |
| 565 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 566 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 567 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 568 | Mutex::Autolock _l(mLock); |
| 569 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 570 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 571 | } |
| 572 | |
| 573 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 574 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 575 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 576 | { |
| 577 | status_t status = NO_ERROR; |
| 578 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 579 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 580 | event->mWaitStatus = false; |
| 581 | mPendingConfigEvents.add(event); |
| 582 | return status; |
| 583 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 584 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 585 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 586 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 587 | mLock.unlock(); |
| 588 | { |
| 589 | Mutex::Autolock _l(event->mLock); |
| 590 | while (event->mWaitStatus) { |
| 591 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 592 | event->mStatus = TIMED_OUT; |
| 593 | event->mWaitStatus = false; |
| 594 | } |
| 595 | } |
| 596 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 597 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 598 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 599 | return status; |
| 600 | } |
| 601 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 602 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 603 | { |
| 604 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 605 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 609 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 610 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 611 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 612 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 615 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 616 | { |
| 617 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 618 | sendPrioConfigEvent_l(pid, tid, prio, forApp); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 621 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 622 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l( |
| 623 | pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 624 | { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 625 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 626 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 629 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 630 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 631 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 632 | sp<ConfigEvent> configEvent; |
| 633 | AudioParameter param(keyValuePair); |
| 634 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 635 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 636 | setMasterMono_l(value != 0); |
| 637 | if (param.size() == 1) { |
| 638 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 639 | } |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 640 | param.remove(String8(AudioParameter::keyMonoOutput)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 641 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 642 | } else { |
| 643 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 644 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 645 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 648 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 649 | const struct audio_patch *patch, |
| 650 | audio_patch_handle_t *handle) |
| 651 | { |
| 652 | Mutex::Autolock _l(mLock); |
| 653 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 654 | status_t status = sendConfigEvent_l(configEvent); |
| 655 | if (status == NO_ERROR) { |
| 656 | CreateAudioPatchConfigEventData *data = |
| 657 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 658 | *handle = data->mHandle; |
| 659 | } |
| 660 | return status; |
| 661 | } |
| 662 | |
| 663 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 664 | const audio_patch_handle_t handle) |
| 665 | { |
| 666 | Mutex::Autolock _l(mLock); |
| 667 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 668 | return sendConfigEvent_l(configEvent); |
| 669 | } |
| 670 | |
| 671 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 672 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 673 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 674 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 675 | bool configChanged = false; |
| 676 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 677 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 678 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 679 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 680 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 681 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 682 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 683 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 684 | // FIXME Need to understand why this has to be done asynchronously |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 685 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 686 | true /*asynchronous*/); |
| 687 | if (err != 0) { |
| 688 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 689 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 690 | } |
| 691 | } break; |
| 692 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 693 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 694 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 695 | } break; |
| 696 | case CFG_EVENT_SET_PARAMETER: { |
| 697 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 698 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 699 | configChanged = true; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 700 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", |
| 701 | data->mKeyValuePairs.string()); |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 702 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 703 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 704 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 705 | const audio_devices_t oldDevice = getDevice(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 706 | CreateAudioPatchConfigEventData *data = |
| 707 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 708 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 709 | const audio_devices_t newDevice = getDevice(); |
| 710 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", |
| 711 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), |
| 712 | (unsigned)newDevice, devicesToString(newDevice).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 713 | } break; |
| 714 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 715 | const audio_devices_t oldDevice = getDevice(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 716 | ReleaseAudioPatchConfigEventData *data = |
| 717 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 718 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 719 | const audio_devices_t newDevice = getDevice(); |
| 720 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", |
| 721 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), |
| 722 | (unsigned)newDevice, devicesToString(newDevice).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 723 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 724 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 725 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 726 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 727 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 728 | { |
| 729 | Mutex::Autolock _l(event->mLock); |
| 730 | if (event->mWaitStatus) { |
| 731 | event->mWaitStatus = false; |
| 732 | event->mCond.signal(); |
| 733 | } |
| 734 | } |
| 735 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 736 | } |
| 737 | |
| 738 | if (configChanged) { |
| 739 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 740 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 743 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 744 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 745 | const audio_channel_representation_t representation = |
| 746 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 747 | |
| 748 | switch (representation) { |
| 749 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 750 | if (output) { |
| 751 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 752 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 753 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 754 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 755 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 756 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 757 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 758 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 759 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 760 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 761 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 762 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 763 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 764 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 765 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 766 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 767 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 768 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 769 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 770 | } else { |
| 771 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 772 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 773 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 774 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 775 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 776 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 777 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 778 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 779 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 780 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 781 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 782 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 783 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 784 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 785 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 786 | } |
| 787 | const int len = s.length(); |
| 788 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 789 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 790 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 791 | } |
| 792 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 793 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 794 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 795 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 796 | return s; |
| 797 | default: |
| 798 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 799 | representation, audio_channel_mask_get_bits(mask)); |
| 800 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 801 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 804 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 805 | { |
| 806 | const size_t SIZE = 256; |
| 807 | char buffer[SIZE]; |
| 808 | String8 result; |
| 809 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 810 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", |
| 811 | this, mThreadName, getTid(), type(), threadTypeToString(type())); |
| 812 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 813 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 814 | if (!locked) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 815 | dprintf(fd, " Thread may be deadlocked\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 818 | dprintf(fd, " I/O handle: %d\n", mId); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 819 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 820 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 821 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 822 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat).c_str()); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 823 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 824 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 825 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 826 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 827 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat).c_str()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 828 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 829 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 830 | size_t numConfig = mConfigEvents.size(); |
| 831 | if (numConfig) { |
| 832 | for (size_t i = 0; i < numConfig; i++) { |
| 833 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 834 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 835 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 836 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 837 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 838 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 839 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 840 | // Note: output device may be used by capture threads for effects such as AEC. |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 841 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).c_str()); |
| 842 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).c_str()); |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 843 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 844 | |
| 845 | if (locked) { |
| 846 | mLock.unlock(); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 851 | { |
| 852 | const size_t SIZE = 256; |
| 853 | char buffer[SIZE]; |
| 854 | String8 result; |
| 855 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 856 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 857 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 858 | write(fd, buffer, strlen(buffer)); |
| 859 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 860 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 861 | sp<EffectChain> chain = mEffectChains[i]; |
| 862 | if (chain != 0) { |
| 863 | chain->dump(fd, args); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 868 | void AudioFlinger::ThreadBase::acquireWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 869 | { |
| 870 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 871 | acquireWakeLock_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 874 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 875 | { |
| 876 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 877 | case MIXER: |
| 878 | return String16("AudioMix"); |
| 879 | case DIRECT: |
| 880 | return String16("AudioDirectOut"); |
| 881 | case DUPLICATING: |
| 882 | return String16("AudioDup"); |
| 883 | case RECORD: |
| 884 | return String16("AudioIn"); |
| 885 | case OFFLOAD: |
| 886 | return String16("AudioOffload"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 887 | case MMAP: |
| 888 | return String16("Mmap"); |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 889 | default: |
| 890 | ALOG_ASSERT(false); |
| 891 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 895 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 896 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 897 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 898 | if (mPowerManager != 0) { |
| 899 | sp<IBinder> binder = new BBinder(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 900 | // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids. |
| 901 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 902 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 903 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 904 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 905 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 906 | if (status == NO_ERROR) { |
| 907 | mWakeLockToken = binder; |
| 908 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 909 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 910 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 911 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 912 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 913 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 914 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 918 | { |
| 919 | Mutex::Autolock _l(mLock); |
| 920 | releaseWakeLock_l(); |
| 921 | } |
| 922 | |
| 923 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 924 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 925 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 926 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 927 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 928 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 929 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 930 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 931 | } |
| 932 | mWakeLockToken.clear(); |
| 933 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 937 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 938 | // use checkService() to avoid blocking if power service is not up yet |
| 939 | sp<IBinder> binder = |
| 940 | defaultServiceManager()->checkService(String16("power")); |
| 941 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 942 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 943 | } else { |
| 944 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 945 | binder->linkToDeath(mDeathRecipient); |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 950 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 951 | getPowerManager_l(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 952 | |
| 953 | #if !LOG_NDEBUG |
| 954 | std::stringstream s; |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 955 | for (uid_t uid : uids) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 956 | s << uid << " "; |
| 957 | } |
| 958 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); |
| 959 | #endif |
| 960 | |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 961 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 962 | if (mSystemReady) { |
| 963 | ALOGE("no wake lock to update, but system ready!"); |
| 964 | } else { |
| 965 | ALOGW("no wake lock to update, system not ready yet"); |
| 966 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | if (mPowerManager != 0) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 970 | std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints |
| 971 | status_t status = mPowerManager->updateWakeLockUids( |
| 972 | mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(), |
| 973 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 974 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 978 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 979 | { |
| 980 | Mutex::Autolock _l(mLock); |
| 981 | releaseWakeLock_l(); |
| 982 | mPowerManager.clear(); |
| 983 | } |
| 984 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 985 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 986 | { |
| 987 | sp<ThreadBase> thread = mThread.promote(); |
| 988 | if (thread != 0) { |
| 989 | thread->clearPowerManager(); |
| 990 | } |
| 991 | ALOGW("power manager service died !!!"); |
| 992 | } |
| 993 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 994 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 995 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 996 | { |
| 997 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 998 | if (chain != 0) { |
| 999 | if (type != NULL) { |
| 1000 | chain->setEffectSuspended_l(type, suspend); |
| 1001 | } else { |
| 1002 | chain->setEffectSuspendedAll_l(suspend); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1007 | } |
| 1008 | |
| 1009 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1010 | { |
| 1011 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1012 | if (index < 0) { |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1017 | mSuspendedSessions.valueAt(index); |
| 1018 | |
| 1019 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1020 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1021 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1022 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1023 | chain->setEffectSuspendedAll_l(true); |
| 1024 | } else { |
| 1025 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1026 | desc->mType.timeLow); |
| 1027 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1034 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1035 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1036 | { |
| 1037 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1038 | |
| 1039 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1040 | |
| 1041 | if (suspend) { |
| 1042 | if (index >= 0) { |
| 1043 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1044 | } else { |
| 1045 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1046 | } |
| 1047 | } else { |
| 1048 | if (index < 0) { |
| 1049 | return; |
| 1050 | } |
| 1051 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | int key = EffectChain::kKeyForSuspendAll; |
| 1056 | if (type != NULL) { |
| 1057 | key = type->timeLow; |
| 1058 | } |
| 1059 | index = sessionEffects.indexOfKey(key); |
| 1060 | |
| 1061 | sp<SuspendedSessionDesc> desc; |
| 1062 | if (suspend) { |
| 1063 | if (index >= 0) { |
| 1064 | desc = sessionEffects.valueAt(index); |
| 1065 | } else { |
| 1066 | desc = new SuspendedSessionDesc(); |
| 1067 | if (type != NULL) { |
| 1068 | desc->mType = *type; |
| 1069 | } |
| 1070 | sessionEffects.add(key, desc); |
| 1071 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1072 | } |
| 1073 | desc->mRefCount++; |
| 1074 | } else { |
| 1075 | if (index < 0) { |
| 1076 | return; |
| 1077 | } |
| 1078 | desc = sessionEffects.valueAt(index); |
| 1079 | if (--desc->mRefCount == 0) { |
| 1080 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1081 | sessionEffects.removeItemsAt(index); |
| 1082 | if (sessionEffects.isEmpty()) { |
| 1083 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1084 | sessionId); |
| 1085 | mSuspendedSessions.removeItem(sessionId); |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | if (!sessionEffects.isEmpty()) { |
| 1090 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1095 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1096 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1097 | { |
| 1098 | Mutex::Autolock _l(mLock); |
| 1099 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1100 | } |
| 1101 | |
| 1102 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1103 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1104 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1105 | { |
| 1106 | if (mType != RECORD) { |
| 1107 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1108 | // another session. This gives the priority to well behaved effect control panels |
| 1109 | // and applications not using global effects. |
| 1110 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1111 | // global effects |
| 1112 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1113 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1118 | if (chain != 0) { |
| 1119 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1123 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1124 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( |
| 1125 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1126 | { |
| 1127 | // No global effect sessions on record threads |
| 1128 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1129 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1130 | desc->name, mThreadName); |
| 1131 | return BAD_VALUE; |
| 1132 | } |
| 1133 | // only pre processing effects on record thread |
| 1134 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1135 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1136 | desc->name, mThreadName); |
| 1137 | return BAD_VALUE; |
| 1138 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1139 | |
| 1140 | // always allow effects without processing load or latency |
| 1141 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1142 | return NO_ERROR; |
| 1143 | } |
| 1144 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1145 | audio_input_flags_t flags = mInput->flags; |
| 1146 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1147 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1148 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1149 | desc->name, mThreadName); |
| 1150 | return BAD_VALUE; |
| 1151 | } |
| 1152 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1153 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1154 | desc->name, mThreadName); |
| 1155 | return BAD_VALUE; |
| 1156 | } |
| 1157 | } |
| 1158 | return NO_ERROR; |
| 1159 | } |
| 1160 | |
| 1161 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1162 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( |
| 1163 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1164 | { |
| 1165 | // no preprocessing on playback threads |
| 1166 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1167 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" |
| 1168 | " thread %s", desc->name, mThreadName); |
| 1169 | return BAD_VALUE; |
| 1170 | } |
| 1171 | |
Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1172 | // always allow effects without processing load or latency |
| 1173 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1174 | return NO_ERROR; |
| 1175 | } |
| 1176 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1177 | switch (mType) { |
| 1178 | case MIXER: { |
| 1179 | // Reject any effect on mixer multichannel sinks. |
| 1180 | // TODO: fix both format and multichannel issues with effects. |
| 1181 | if (mChannelCount != FCC_2) { |
| 1182 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" |
| 1183 | " thread %s", desc->name, mChannelCount, mThreadName); |
| 1184 | return BAD_VALUE; |
| 1185 | } |
| 1186 | audio_output_flags_t flags = mOutput->flags; |
| 1187 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1188 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1189 | // global effects are applied only to non fast tracks if they are SW |
| 1190 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1191 | break; |
| 1192 | } |
| 1193 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1194 | // only post processing on output stage session |
| 1195 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1196 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1197 | " on output stage session", desc->name); |
| 1198 | return BAD_VALUE; |
| 1199 | } |
| 1200 | } else { |
| 1201 | // no restriction on effects applied on non fast tracks |
| 1202 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1203 | break; |
| 1204 | } |
| 1205 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1206 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1207 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
| 1208 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", |
| 1209 | desc->name); |
| 1210 | return BAD_VALUE; |
| 1211 | } |
| 1212 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1213 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" |
| 1214 | " in fast mode", desc->name); |
| 1215 | return BAD_VALUE; |
| 1216 | } |
| 1217 | } |
| 1218 | } break; |
| 1219 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1220 | // nothing actionable on offload threads, if the effect: |
| 1221 | // - is offloadable: the effect can be created |
| 1222 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1223 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1224 | break; |
| 1225 | case DIRECT: |
| 1226 | // Reject any effect on Direct output threads for now, since the format of |
| 1227 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1228 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", |
| 1229 | desc->name, mThreadName); |
| 1230 | return BAD_VALUE; |
| 1231 | case DUPLICATING: |
| 1232 | // Reject any effect on mixer multichannel sinks. |
| 1233 | // TODO: fix both format and multichannel issues with effects. |
| 1234 | if (mChannelCount != FCC_2) { |
| 1235 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" |
| 1236 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); |
| 1237 | return BAD_VALUE; |
| 1238 | } |
| 1239 | if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) { |
| 1240 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" |
| 1241 | " thread %s", desc->name, mThreadName); |
| 1242 | return BAD_VALUE; |
| 1243 | } |
| 1244 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 1245 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" |
| 1246 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1247 | return BAD_VALUE; |
| 1248 | } |
| 1249 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1250 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" |
| 1251 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1252 | return BAD_VALUE; |
| 1253 | } |
| 1254 | break; |
| 1255 | default: |
| 1256 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1257 | } |
| 1258 | |
| 1259 | return NO_ERROR; |
| 1260 | } |
| 1261 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1262 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1263 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1264 | const sp<AudioFlinger::Client>& client, |
| 1265 | const sp<IEffectClient>& effectClient, |
| 1266 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1267 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1268 | effect_descriptor_t *desc, |
| 1269 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1270 | status_t *status, |
| 1271 | bool pinned) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1272 | { |
| 1273 | sp<EffectModule> effect; |
| 1274 | sp<EffectHandle> handle; |
| 1275 | status_t lStatus; |
| 1276 | sp<EffectChain> chain; |
| 1277 | bool chainCreated = false; |
| 1278 | bool effectCreated = false; |
| 1279 | bool effectRegistered = false; |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1280 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1281 | |
| 1282 | lStatus = initCheck(); |
| 1283 | if (lStatus != NO_ERROR) { |
| 1284 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1285 | goto Exit; |
| 1286 | } |
| 1287 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1288 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1289 | |
| 1290 | { // scope for mLock |
| 1291 | Mutex::Autolock _l(mLock); |
| 1292 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1293 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
| 1294 | if (lStatus != NO_ERROR) { |
| 1295 | goto Exit; |
| 1296 | } |
| 1297 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1298 | // check for existing effect chain with the requested audio session |
| 1299 | chain = getEffectChain_l(sessionId); |
| 1300 | if (chain == 0) { |
| 1301 | // create a new chain for this session |
| 1302 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1303 | chain = new EffectChain(this, sessionId); |
| 1304 | addEffectChain_l(chain); |
| 1305 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1306 | chainCreated = true; |
| 1307 | } else { |
| 1308 | effect = chain->getEffectFromDesc_l(desc); |
| 1309 | } |
| 1310 | |
| 1311 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1312 | |
| 1313 | if (effect == 0) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1314 | effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1315 | // Check CPU and memory usage |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1316 | lStatus = AudioSystem::registerEffect( |
| 1317 | desc, mId, chain->strategy(), sessionId, effectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1318 | if (lStatus != NO_ERROR) { |
| 1319 | goto Exit; |
| 1320 | } |
| 1321 | effectRegistered = true; |
| 1322 | // create a new effect module if none present in the chain |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1323 | lStatus = chain->createEffect_l(effect, this, desc, effectId, sessionId, pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 | if (lStatus != NO_ERROR) { |
| 1325 | goto Exit; |
| 1326 | } |
| 1327 | effectCreated = true; |
| 1328 | |
| 1329 | effect->setDevice(mOutDevice); |
| 1330 | effect->setDevice(mInDevice); |
| 1331 | effect->setMode(mAudioFlinger->getMode()); |
| 1332 | effect->setAudioSource(mAudioSource); |
| 1333 | } |
| 1334 | // create effect handle and connect it to effect module |
| 1335 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1336 | lStatus = handle->initCheck(); |
| 1337 | if (lStatus == OK) { |
| 1338 | lStatus = effect->addHandle(handle.get()); |
| 1339 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1340 | if (enabled != NULL) { |
| 1341 | *enabled = (int)effect->isEnabled(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | Exit: |
| 1346 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1347 | Mutex::Autolock _l(mLock); |
| 1348 | if (effectCreated) { |
| 1349 | chain->removeEffect_l(effect); |
| 1350 | } |
| 1351 | if (effectRegistered) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1352 | AudioSystem::unregisterEffect(effectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1353 | } |
| 1354 | if (chainCreated) { |
| 1355 | removeEffectChain_l(chain); |
| 1356 | } |
| 1357 | handle.clear(); |
| 1358 | } |
| 1359 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1360 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1361 | return handle; |
| 1362 | } |
| 1363 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1364 | void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle, |
| 1365 | bool unpinIfLast) |
| 1366 | { |
| 1367 | bool remove = false; |
| 1368 | sp<EffectModule> effect; |
| 1369 | { |
| 1370 | Mutex::Autolock _l(mLock); |
| 1371 | |
| 1372 | effect = handle->effect().promote(); |
| 1373 | if (effect == 0) { |
| 1374 | return; |
| 1375 | } |
| 1376 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 1377 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 1378 | if (remove) { |
| 1379 | removeEffect_l(effect, true); |
| 1380 | } |
| 1381 | } |
| 1382 | if (remove) { |
| 1383 | mAudioFlinger->updateOrphanEffectChains(effect); |
| 1384 | AudioSystem::unregisterEffect(effect->id()); |
| 1385 | if (handle->enabled()) { |
| 1386 | checkSuspendOnEffectEnabled(effect, false, effect->sessionId()); |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1391 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1392 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1393 | { |
| 1394 | Mutex::Autolock _l(mLock); |
| 1395 | return getEffect_l(sessionId, effectId); |
| 1396 | } |
| 1397 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1398 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1399 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1400 | { |
| 1401 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1402 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1403 | } |
| 1404 | |
| 1405 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1406 | // PlaybackThread::mLock held |
| 1407 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1408 | { |
| 1409 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1410 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1411 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1412 | bool chainCreated = false; |
| 1413 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1414 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1415 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1416 | this, effect->desc().name, effect->desc().flags); |
| 1417 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1418 | if (chain == 0) { |
| 1419 | // create a new chain for this session |
| 1420 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1421 | chain = new EffectChain(this, sessionId); |
| 1422 | addEffectChain_l(chain); |
| 1423 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1424 | chainCreated = true; |
| 1425 | } |
| 1426 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1427 | |
| 1428 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1429 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1430 | this, effect->desc().name, chain.get()); |
| 1431 | return BAD_VALUE; |
| 1432 | } |
| 1433 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1434 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1435 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1436 | status_t status = chain->addEffect_l(effect); |
| 1437 | if (status != NO_ERROR) { |
| 1438 | if (chainCreated) { |
| 1439 | removeEffectChain_l(chain); |
| 1440 | } |
| 1441 | return status; |
| 1442 | } |
| 1443 | |
| 1444 | effect->setDevice(mOutDevice); |
| 1445 | effect->setDevice(mInDevice); |
| 1446 | effect->setMode(mAudioFlinger->getMode()); |
| 1447 | effect->setAudioSource(mAudioSource); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1448 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1449 | return NO_ERROR; |
| 1450 | } |
| 1451 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1452 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1453 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1454 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1455 | effect_descriptor_t desc = effect->desc(); |
| 1456 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1457 | detachAuxEffect_l(effect->id()); |
| 1458 | } |
| 1459 | |
| 1460 | sp<EffectChain> chain = effect->chain().promote(); |
| 1461 | if (chain != 0) { |
| 1462 | // remove effect chain if removing last effect |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1463 | if (chain->removeEffect_l(effect, release) == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1464 | removeEffectChain_l(chain); |
| 1465 | } |
| 1466 | } else { |
| 1467 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1472 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1473 | { |
| 1474 | effectChains = mEffectChains; |
| 1475 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1476 | mEffectChains[i]->lock(); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1481 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1482 | { |
| 1483 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1484 | effectChains[i]->unlock(); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1488 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1489 | { |
| 1490 | Mutex::Autolock _l(mLock); |
| 1491 | return getEffectChain_l(sessionId); |
| 1492 | } |
| 1493 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1494 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1495 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1496 | { |
| 1497 | size_t size = mEffectChains.size(); |
| 1498 | for (size_t i = 0; i < size; i++) { |
| 1499 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1500 | return mEffectChains[i]; |
| 1501 | } |
| 1502 | } |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1507 | { |
| 1508 | Mutex::Autolock _l(mLock); |
| 1509 | size_t size = mEffectChains.size(); |
| 1510 | for (size_t i = 0; i < size; i++) { |
| 1511 | mEffectChains[i]->setMode_l(mode); |
| 1512 | } |
| 1513 | } |
| 1514 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1515 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1516 | { |
| 1517 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1518 | config->ext.mix.handle = mId; |
| 1519 | config->sample_rate = mSampleRate; |
| 1520 | config->format = mFormat; |
| 1521 | config->channel_mask = mChannelMask; |
| 1522 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1523 | AUDIO_PORT_CONFIG_FORMAT; |
| 1524 | } |
| 1525 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1526 | void AudioFlinger::ThreadBase::systemReady() |
| 1527 | { |
| 1528 | Mutex::Autolock _l(mLock); |
| 1529 | if (mSystemReady) { |
| 1530 | return; |
| 1531 | } |
| 1532 | mSystemReady = true; |
| 1533 | |
| 1534 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1535 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1536 | } |
| 1537 | mPendingConfigEvents.clear(); |
| 1538 | } |
| 1539 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1540 | template <typename T> |
| 1541 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) { |
| 1542 | ssize_t index = mActiveTracks.indexOf(track); |
| 1543 | if (index >= 0) { |
| 1544 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); |
| 1545 | return index; |
| 1546 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1547 | logTrack("add", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1548 | mActiveTracksGeneration++; |
| 1549 | mLatestActiveTrack = track; |
| 1550 | ++mBatteryCounter[track->uid()].second; |
| 1551 | return mActiveTracks.add(track); |
| 1552 | } |
| 1553 | |
| 1554 | template <typename T> |
| 1555 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) { |
| 1556 | ssize_t index = mActiveTracks.remove(track); |
| 1557 | if (index < 0) { |
| 1558 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); |
| 1559 | return index; |
| 1560 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1561 | logTrack("remove", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1562 | mActiveTracksGeneration++; |
| 1563 | --mBatteryCounter[track->uid()].second; |
| 1564 | // mLatestActiveTrack is not cleared even if is the same as track. |
| 1565 | return index; |
| 1566 | } |
| 1567 | |
| 1568 | template <typename T> |
| 1569 | void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() { |
| 1570 | for (const sp<T> &track : mActiveTracks) { |
| 1571 | BatteryNotifier::getInstance().noteStopAudio(track->uid()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1572 | logTrack("clear", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1573 | } |
| 1574 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1575 | mActiveTracks.clear(); |
| 1576 | mLatestActiveTrack.clear(); |
| 1577 | mBatteryCounter.clear(); |
| 1578 | } |
| 1579 | |
| 1580 | template <typename T> |
| 1581 | void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState( |
| 1582 | sp<ThreadBase> thread, bool force) { |
| 1583 | // Updates ActiveTracks client uids to the thread wakelock. |
| 1584 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { |
| 1585 | thread->updateWakeLockUids_l(getWakeLockUids()); |
| 1586 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1587 | } |
| 1588 | |
| 1589 | // Updates BatteryNotifier uids |
| 1590 | for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) { |
| 1591 | const uid_t uid = it->first; |
| 1592 | ssize_t &previous = it->second.first; |
| 1593 | ssize_t ¤t = it->second.second; |
| 1594 | if (current > 0) { |
| 1595 | if (previous == 0) { |
| 1596 | BatteryNotifier::getInstance().noteStartAudio(uid); |
| 1597 | } |
| 1598 | previous = current; |
| 1599 | ++it; |
| 1600 | } else if (current == 0) { |
| 1601 | if (previous > 0) { |
| 1602 | BatteryNotifier::getInstance().noteStopAudio(uid); |
| 1603 | } |
| 1604 | it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase. |
| 1605 | } else /* (current < 0) */ { |
| 1606 | LOG_ALWAYS_FATAL("negative battery count %zd", current); |
| 1607 | } |
| 1608 | } |
| 1609 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1610 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1611 | template <typename T> |
| 1612 | void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack( |
| 1613 | const char *funcName, const sp<T> &track) const { |
| 1614 | if (mLocalLog != nullptr) { |
| 1615 | String8 result; |
| 1616 | track->appendDump(result, false /* active */); |
| 1617 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string()); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1621 | void AudioFlinger::ThreadBase::broadcast_l() |
| 1622 | { |
| 1623 | // Thread could be blocked waiting for async |
| 1624 | // so signal it to handle state changes immediately |
| 1625 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1626 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1627 | mSignalPending = true; |
| 1628 | mWaitWorkCV.broadcast(); |
| 1629 | } |
| 1630 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1631 | // ---------------------------------------------------------------------------- |
| 1632 | // Playback |
| 1633 | // ---------------------------------------------------------------------------- |
| 1634 | |
| 1635 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1636 | AudioStreamOut* output, |
| 1637 | audio_io_handle_t id, |
| 1638 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1639 | type_t type, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1640 | bool systemReady) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1641 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1642 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1643 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1644 | mMixerBuffer(NULL), |
| 1645 | mMixerBufferSize(0), |
| 1646 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1647 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1648 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1649 | mEffectBuffer(NULL), |
| 1650 | mEffectBufferSize(0), |
| 1651 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1652 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1653 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1654 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1655 | mSuspendedFrames(0), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1656 | mActiveTracks(&this->mLocalLog), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1657 | // mStreamTypes[] initialized in constructor body |
| 1658 | mOutput(output), |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1659 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1660 | mMixerStatus(MIXER_IDLE), |
| 1661 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1662 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1663 | mBytesRemaining(0), |
| 1664 | mCurrentWriteLength(0), |
| 1665 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1666 | mWriteAckSequence(0), |
| 1667 | mDrainSequence(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1668 | mScreenState(AudioFlinger::mScreenState), |
| 1669 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1670 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1671 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1672 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1673 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1674 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1675 | |
| 1676 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1677 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1678 | // parameter. |
| 1679 | // |
| 1680 | // If the HAL we are using has support for master volume or master mute, |
| 1681 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1682 | // and the mute set to false). |
| 1683 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1684 | mMasterMute = audioFlinger->masterMute_l(); |
| 1685 | if (mOutput && mOutput->audioHwDev) { |
| 1686 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1687 | mMasterVolume = 1.0; |
| 1688 | } |
| 1689 | |
| 1690 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1691 | mMasterMute = false; |
| 1692 | } |
| 1693 | } |
| 1694 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1695 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1696 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1697 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1698 | for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1699 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1700 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1701 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1702 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1706 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1707 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1708 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1709 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1710 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1714 | { |
| 1715 | dumpInternals(fd, args); |
| 1716 | dumpTracks(fd, args); |
| 1717 | dumpEffectChains(fd, args); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1718 | dprintf(fd, " Local log:\n"); |
| 1719 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1720 | } |
| 1721 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1722 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1723 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1724 | String8 result; |
| 1725 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1726 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1727 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1728 | const stream_type_t *st = &mStreamTypes[i]; |
| 1729 | if (i > 0) { |
| 1730 | result.appendFormat(", "); |
| 1731 | } |
| 1732 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1733 | if (st->mute) { |
| 1734 | result.append("M"); |
| 1735 | } |
| 1736 | } |
| 1737 | result.append("\n"); |
| 1738 | write(fd, result.string(), result.length()); |
| 1739 | result.clear(); |
| 1740 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1741 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1742 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1743 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1744 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1745 | |
| 1746 | size_t numtracks = mTracks.size(); |
| 1747 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1748 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1749 | size_t numactiveseen = 0; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1750 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1751 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1752 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1753 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1754 | Track::appendDumpHeader(result); |
| 1755 | for (size_t i = 0; i < numtracks; ++i) { |
| 1756 | sp<Track> track = mTracks[i]; |
| 1757 | if (track != 0) { |
| 1758 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1759 | if (active) { |
| 1760 | numactiveseen++; |
| 1761 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1762 | result.append(prefix); |
| 1763 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | } else { |
| 1767 | result.append("\n"); |
| 1768 | } |
| 1769 | if (numactiveseen != numactive) { |
| 1770 | // some tracks in the active list were not in the tracks list |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1771 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1772 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1773 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1774 | Track::appendDumpHeader(result); |
| 1775 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1776 | sp<Track> track = mActiveTracks[i]; |
| 1777 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1778 | result.append(prefix); |
| 1779 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1788 | { |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1789 | dumpBase(fd, args); |
| 1790 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1791 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1792 | dprintf(fd, " Last write occurred (msecs): %llu\n", |
| 1793 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1794 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1795 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1796 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1797 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1798 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1799 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1800 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1801 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1802 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1803 | AudioStreamOut *output = mOutput; |
| 1804 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 1805 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", |
| 1806 | output, flags, outputFlagsToString(flags).c_str()); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 1807 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 1808 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 1809 | if (mPipeSink.get() != nullptr) { |
| 1810 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 1811 | } |
| 1812 | if (output != nullptr) { |
| 1813 | dprintf(fd, " Hal stream dump:\n"); |
| 1814 | (void)output->stream->dump(fd); |
| 1815 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1819 | |
| 1820 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1821 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1822 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | // ThreadBase virtuals |
| 1826 | void AudioFlinger::PlaybackThread::preExit() |
| 1827 | { |
| 1828 | ALOGV(" preExit()"); |
| 1829 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1830 | // converted to use audio HAL extensions required to support tunneling |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1831 | status_t result = mOutput->stream->setParameters(String8("exiting=1")); |
| 1832 | ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1836 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1837 | const sp<AudioFlinger::Client>& client, |
| 1838 | audio_stream_type_t streamType, |
| 1839 | uint32_t sampleRate, |
| 1840 | audio_format_t format, |
| 1841 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1842 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1843 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1844 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1845 | audio_output_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1846 | pid_t tid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1847 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1848 | status_t *status, |
| 1849 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1850 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1851 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1852 | sp<Track> track; |
| 1853 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1854 | audio_output_flags_t outputFlags = mOutput->flags; |
| 1855 | |
| 1856 | // special case for FAST flag considered OK if fast mixer is present |
| 1857 | if (hasFastMixer()) { |
| 1858 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 1859 | } |
| 1860 | |
| 1861 | // Check if requested flags are compatible with output stream flags |
| 1862 | if ((*flags & outputFlags) != *flags) { |
| 1863 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 1864 | *flags, outputFlags); |
| 1865 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 1866 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1868 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1869 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1870 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1871 | // PCM data |
| 1872 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1873 | // TODO: extract as a data library function that checks that a computationally |
| 1874 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1875 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1876 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1877 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1878 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1879 | // hardware sample rate |
| 1880 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1881 | // normal mixer has an associated fast mixer |
| 1882 | hasFastMixer() && |
| 1883 | // there are sufficient fast track slots available |
| 1884 | (mFastTrackAvailMask != 0) |
| 1885 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1886 | // FIXME add a permission test also? |
| 1887 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1888 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 1889 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1890 | // read the fast track multiplier property the first time it is needed |
| 1891 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1892 | if (ok != 0) { |
| 1893 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1894 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1895 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1896 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1897 | |
| 1898 | // check compatibility with audio effects. |
| 1899 | { // scope for mLock |
| 1900 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 1901 | for (audio_session_t session : { |
| 1902 | AUDIO_SESSION_OUTPUT_STAGE, |
| 1903 | AUDIO_SESSION_OUTPUT_MIX, |
| 1904 | sessionId, |
| 1905 | }) { |
| 1906 | sp<EffectChain> chain = getEffectChain_l(session); |
| 1907 | if (chain.get() != nullptr) { |
| 1908 | audio_output_flags_t old = *flags; |
| 1909 | chain->checkOutputFlagCompatibility(flags); |
| 1910 | if (old != *flags) { |
| 1911 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 1912 | (int)session, (int)old, (int)*flags); |
| 1913 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1917 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1918 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 1919 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1920 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1921 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 1922 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1923 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1924 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1925 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1926 | audio_is_linear_pcm(format), |
| 1927 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1928 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | // For normal PCM streaming tracks, update minimum frame count. |
| 1932 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1933 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1934 | // This is probably too conservative, but legacy application code may depend on it. |
| 1935 | // If you change this calculation, also review the start threshold which is related. |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1936 | if (!(*flags & AUDIO_OUTPUT_FLAG_FAST) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1937 | && audio_has_proportional_frames(format) && sharedBuffer == 0) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1938 | // this must match AudioTrack.cpp calculateMinFrameCount(). |
| 1939 | // TODO: Move to a common library |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1940 | uint32_t latencyMs = 0; |
| 1941 | lStatus = mOutput->stream->getLatency(&latencyMs); |
| 1942 | if (lStatus != OK) { |
| 1943 | ALOGE("Error when retrieving output stream latency: %d", lStatus); |
| 1944 | goto Exit; |
| 1945 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1946 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1947 | if (minBufCount < 2) { |
| 1948 | minBufCount = 2; |
| 1949 | } |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1950 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack |
| 1951 | // or the client should compute and pass in a larger buffer request. |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1952 | size_t minFrameCount = |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1953 | minBufCount * sourceFramesNeededWithTimestretch( |
| 1954 | sampleRate, mNormalFrameCount, |
| 1955 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1956 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1957 | frameCount = minFrameCount; |
| 1958 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1959 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1960 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1961 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1962 | switch (mType) { |
| 1963 | |
| 1964 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1965 | if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1966 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1967 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1968 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1969 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1970 | lStatus = BAD_VALUE; |
| 1971 | goto Exit; |
| 1972 | } |
| 1973 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1974 | break; |
| 1975 | |
| 1976 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1977 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1978 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1979 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1980 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1981 | lStatus = BAD_VALUE; |
| 1982 | goto Exit; |
| 1983 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1984 | break; |
| 1985 | |
| 1986 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1987 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1988 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1989 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1990 | format, mOutput, mFormat); |
| 1991 | lStatus = BAD_VALUE; |
| 1992 | goto Exit; |
| 1993 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1994 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1995 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1996 | lStatus = BAD_VALUE; |
| 1997 | goto Exit; |
| 1998 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1999 | break; |
| 2000 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | lStatus = initCheck(); |
| 2004 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2005 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2006 | goto Exit; |
| 2007 | } |
| 2008 | |
| 2009 | { // scope for mLock |
| 2010 | Mutex::Autolock _l(mLock); |
| 2011 | |
| 2012 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2013 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2014 | // manager |
| 2015 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 2016 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2017 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2018 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2019 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 2020 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2021 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2022 | strategy, actual); |
| 2023 | lStatus = BAD_VALUE; |
| 2024 | goto Exit; |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2029 | track = new Track(this, client, streamType, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2030 | channelMask, frameCount, |
| 2031 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2032 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT, portId); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2033 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2034 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2035 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2036 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2037 | // track must be cleared from the caller as the caller has the AF lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2038 | goto Exit; |
| 2039 | } |
| 2040 | mTracks.add(track); |
| 2041 | |
| 2042 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 2043 | if (chain != 0) { |
| 2044 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2045 | track->setMainBuffer(chain->inBuffer()); |
| 2046 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 2047 | chain->incTrackCnt(); |
| 2048 | } |
| 2049 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2050 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2051 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2052 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2053 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2054 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | lStatus = NO_ERROR; |
| 2059 | |
| 2060 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2061 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2062 | return track; |
| 2063 | } |
| 2064 | |
| 2065 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 2066 | { |
| 2067 | return latency; |
| 2068 | } |
| 2069 | |
| 2070 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 2071 | { |
| 2072 | Mutex::Autolock _l(mLock); |
| 2073 | return latency_l(); |
| 2074 | } |
| 2075 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 2076 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2077 | uint32_t latency; |
| 2078 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { |
| 2079 | return correctLatency_l(latency); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2080 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2081 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 2085 | { |
| 2086 | Mutex::Autolock _l(mLock); |
| 2087 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2088 | if (mOutput && mOutput->audioHwDev && |
| 2089 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2090 | mMasterVolume = 1.0; |
| 2091 | } else { |
| 2092 | mMasterVolume = value; |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 2097 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2098 | if (isDuplicating()) { |
| 2099 | return; |
| 2100 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2101 | Mutex::Autolock _l(mLock); |
| 2102 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2103 | if (mOutput && mOutput->audioHwDev && |
| 2104 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2105 | mMasterMute = false; |
| 2106 | } else { |
| 2107 | mMasterMute = muted; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2112 | { |
| 2113 | Mutex::Autolock _l(mLock); |
| 2114 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2115 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2119 | { |
| 2120 | Mutex::Autolock _l(mLock); |
| 2121 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2122 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2126 | { |
| 2127 | Mutex::Autolock _l(mLock); |
| 2128 | return mStreamTypes[stream].volume; |
| 2129 | } |
| 2130 | |
| 2131 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2132 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2133 | { |
| 2134 | status_t status = ALREADY_EXISTS; |
| 2135 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2136 | if (mActiveTracks.indexOf(track) < 0) { |
| 2137 | // the track is newly added, make sure it fills up all its |
| 2138 | // buffers before playing. This is to ensure the client will |
| 2139 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2140 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2141 | TrackBase::track_state state = track->mState; |
| 2142 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2143 | status = AudioSystem::startOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2144 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2145 | mLock.lock(); |
| 2146 | // abort track was stopped/paused while we released the lock |
| 2147 | if (state != track->mState) { |
| 2148 | if (status == NO_ERROR) { |
| 2149 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2150 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2151 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2152 | mLock.lock(); |
| 2153 | } |
| 2154 | return INVALID_OPERATION; |
| 2155 | } |
| 2156 | // abort if start is rejected by audio policy manager |
| 2157 | if (status != NO_ERROR) { |
| 2158 | return PERMISSION_DENIED; |
| 2159 | } |
| 2160 | #ifdef ADD_BATTERY_DATA |
| 2161 | // to track the speaker usage |
| 2162 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2163 | #endif |
| 2164 | } |
| 2165 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2166 | // set retry count for buffer fill |
| 2167 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2168 | if (track->isStopping_1()) { |
| 2169 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 2170 | } else { |
| 2171 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2172 | } |
| 2173 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2174 | } else { |
| 2175 | track->mRetryCount = kMaxTrackStartupRetries; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2176 | track->mFillingUpStatus = |
| 2177 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2178 | } |
| 2179 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2180 | track->mResetDone = false; |
| 2181 | track->mPresentationCompleteFrames = 0; |
| 2182 | mActiveTracks.add(track); |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2183 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2184 | if (chain != 0) { |
| 2185 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2186 | track->sessionId()); |
| 2187 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | status = NO_ERROR; |
| 2191 | } |
| 2192 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2193 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2194 | return status; |
| 2195 | } |
| 2196 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2197 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2198 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2199 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2200 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2201 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2202 | track->mState = TrackBase::STOPPED; |
| 2203 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2204 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2205 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2206 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2207 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2208 | |
| 2209 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2213 | { |
| 2214 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2215 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2216 | String8 result; |
| 2217 | track->appendDump(result, false /* active */); |
| 2218 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2219 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2220 | mTracks.remove(track); |
| 2221 | deleteTrackName_l(track->name()); |
| 2222 | // redundant as track is about to be destroyed, for dumpsys only |
| 2223 | track->mName = -1; |
| 2224 | if (track->isFastTrack()) { |
| 2225 | int index = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2226 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2227 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2228 | mFastTrackAvailMask |= 1 << index; |
| 2229 | // redundant as track is about to be destroyed, for dumpsys only |
| 2230 | track->mFastIndex = -1; |
| 2231 | } |
| 2232 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2233 | if (chain != 0) { |
| 2234 | chain->decTrackCnt(); |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2239 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2240 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2241 | String8 out_s8; |
| 2242 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { |
| 2243 | return out_s8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2244 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2245 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2246 | } |
| 2247 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2248 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2249 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2250 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2251 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2252 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2253 | |
| 2254 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2255 | case AUDIO_OUTPUT_OPENED: |
| 2256 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2257 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2258 | desc->mChannelMask = mChannelMask; |
| 2259 | desc->mSamplingRate = mSampleRate; |
| 2260 | desc->mFormat = mFormat; |
| 2261 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2262 | // AudioFlinger::frameCount(audio_io_handle_t) |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2263 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2264 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2265 | break; |
| 2266 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2267 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2268 | default: |
| 2269 | break; |
| 2270 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2271 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2274 | void AudioFlinger::PlaybackThread::onWriteReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2275 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2276 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2279 | void AudioFlinger::PlaybackThread::onDrainReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2280 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2281 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2282 | } |
| 2283 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2284 | void AudioFlinger::PlaybackThread::onError() |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2285 | { |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2286 | mCallbackThread->setAsyncError(); |
| 2287 | } |
| 2288 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2289 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2290 | { |
| 2291 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2292 | // reject out of sequence requests |
| 2293 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2294 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2295 | mWaitWorkCV.signal(); |
| 2296 | } |
| 2297 | } |
| 2298 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2299 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2300 | { |
| 2301 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2302 | // reject out of sequence requests |
| 2303 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2304 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2305 | mWaitWorkCV.signal(); |
| 2306 | } |
| 2307 | } |
| 2308 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2309 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2310 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2311 | // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2312 | mSampleRate = mOutput->getSampleRate(); |
| 2313 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2314 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2315 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2316 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2317 | if ((mType == MIXER || mType == DUPLICATING) |
| 2318 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2319 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2320 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2321 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2322 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2323 | |
| 2324 | // Get actual HAL format. |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2325 | status_t result = mOutput->stream->getFormat(&mHALFormat); |
| 2326 | LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2327 | // Get format from the shim, which will be different than the HAL format |
| 2328 | // if playing compressed audio over HDMI passthrough. |
| 2329 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2330 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2331 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2332 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2333 | if ((mType == MIXER || mType == DUPLICATING) |
| 2334 | && !isValidPcmSinkFormat(mFormat)) { |
| 2335 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2336 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2337 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2338 | mFrameSize = mOutput->getFrameSize(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2339 | result = mOutput->stream->getBufferSize(&mBufferSize); |
| 2340 | LOG_ALWAYS_FATAL_IF(result != OK, |
| 2341 | "Error when retrieving output stream buffer size: %d", result); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2342 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2343 | if (mFrameCount & 15) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2344 | ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2345 | mFrameCount); |
| 2346 | } |
| 2347 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2348 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) { |
| 2349 | if (mOutput->stream->setCallback(this) == OK) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2350 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2351 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2355 | mHwSupportsPause = false; |
| 2356 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2357 | bool supportsPause = false, supportsResume = false; |
| 2358 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { |
| 2359 | if (supportsPause && supportsResume) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2360 | mHwSupportsPause = true; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2361 | } else if (supportsPause) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2362 | ALOGW("direct output implements pause but not resume"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2363 | } else if (supportsResume) { |
| 2364 | ALOGW("direct output implements resume but not pause"); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2365 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2366 | } |
| 2367 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2368 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2369 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2370 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2371 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2372 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2373 | // For best precision, we use float instead of the associated output |
| 2374 | // device format (typically PCM 16 bit). |
| 2375 | |
| 2376 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2377 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2378 | mBufferSize = mFrameSize * mFrameCount; |
| 2379 | |
| 2380 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2381 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2382 | // (if a valid mask) to avoid premature downmix. |
| 2383 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2384 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2385 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2386 | } |
| 2387 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2388 | // Calculate size of normal sink buffer relative to the HAL output buffer size |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | double multiplier = 1.0; |
| 2390 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2391 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2392 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2393 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2394 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2395 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2396 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2397 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2398 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2399 | maxNormalFrameCount = minNormalFrameCount; |
| 2400 | } |
| 2401 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2402 | if (multiplier <= 1.0) { |
| 2403 | multiplier = 1.0; |
| 2404 | } else if (multiplier <= 2.0) { |
| 2405 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2406 | multiplier = 2.0; |
| 2407 | } else { |
| 2408 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2409 | } |
| 2410 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2411 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | mNormalFrameCount = multiplier * mFrameCount; |
| 2415 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2416 | if (mType == MIXER || mType == DUPLICATING) { |
| 2417 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2418 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2419 | ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", mFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2420 | mNormalFrameCount); |
| 2421 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2422 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2423 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2424 | mThreadThrottleTimeMs = 0; |
| 2425 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2426 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2427 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2428 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2429 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2430 | free(mSinkBuffer); |
| 2431 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2432 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2433 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2434 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2435 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2436 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2437 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2438 | // drives the output. |
| 2439 | free(mMixerBuffer); |
| 2440 | mMixerBuffer = NULL; |
| 2441 | if (mMixerBufferEnabled) { |
| 2442 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2443 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2444 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2445 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2446 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2447 | free(mEffectBuffer); |
| 2448 | mEffectBuffer = NULL; |
| 2449 | if (mEffectBufferEnabled) { |
| 2450 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2451 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2452 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2453 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2454 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2455 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2456 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2457 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2458 | // Note that mLock is not held when readOutputParameters_l() is called from the constructor |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2459 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2460 | // matter. |
| 2461 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2462 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2463 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2464 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2469 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2470 | { |
| 2471 | if (halFrames == NULL || dspFrames == NULL) { |
| 2472 | return BAD_VALUE; |
| 2473 | } |
| 2474 | Mutex::Autolock _l(mLock); |
| 2475 | if (initCheck() != NO_ERROR) { |
| 2476 | return INVALID_OPERATION; |
| 2477 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2478 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2479 | *halFrames = framesWritten; |
| 2480 | |
| 2481 | if (isSuspended()) { |
| 2482 | // return an estimation of rendered frames when the output is suspended |
| 2483 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2484 | *dspFrames = (uint32_t) |
| 2485 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2486 | return NO_ERROR; |
| 2487 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2488 | status_t status; |
| 2489 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2490 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2491 | *dspFrames = (size_t)frames; |
| 2492 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2493 | } |
| 2494 | } |
| 2495 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2496 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 2497 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2498 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2499 | uint32_t result = 0; |
| 2500 | if (getEffectChain_l(sessionId) != 0) { |
| 2501 | result = EFFECT_SESSION; |
| 2502 | } |
| 2503 | |
| 2504 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2505 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2506 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2507 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2508 | if (track->isFastTrack()) { |
| 2509 | result |= FAST_SESSION; |
| 2510 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2511 | break; |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | return result; |
| 2516 | } |
| 2517 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2518 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2519 | { |
| 2520 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2521 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2522 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2523 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2524 | } |
| 2525 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2526 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2527 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2528 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2529 | } |
| 2530 | } |
| 2531 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2532 | } |
| 2533 | |
| 2534 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2535 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2536 | { |
| 2537 | Mutex::Autolock _l(mLock); |
| 2538 | return mOutput; |
| 2539 | } |
| 2540 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2541 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2542 | { |
| 2543 | Mutex::Autolock _l(mLock); |
| 2544 | AudioStreamOut *output = mOutput; |
| 2545 | mOutput = NULL; |
| 2546 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2547 | // must push a NULL and wait for ack |
| 2548 | mOutputSink.clear(); |
| 2549 | mPipeSink.clear(); |
| 2550 | mNormalSink.clear(); |
| 2551 | return output; |
| 2552 | } |
| 2553 | |
| 2554 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2555 | sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2556 | { |
| 2557 | if (mOutput == NULL) { |
| 2558 | return NULL; |
| 2559 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2560 | return mOutput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2564 | { |
| 2565 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2566 | } |
| 2567 | |
| 2568 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2569 | { |
| 2570 | if (!isValidSyncEvent(event)) { |
| 2571 | return BAD_VALUE; |
| 2572 | } |
| 2573 | |
| 2574 | Mutex::Autolock _l(mLock); |
| 2575 | |
| 2576 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2577 | sp<Track> track = mTracks[i]; |
| 2578 | if (event->triggerSession() == track->sessionId()) { |
| 2579 | (void) track->setSyncEvent(event); |
| 2580 | return NO_ERROR; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | return NAME_NOT_FOUND; |
| 2585 | } |
| 2586 | |
| 2587 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2588 | { |
| 2589 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2590 | } |
| 2591 | |
| 2592 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2593 | const Vector< sp<Track> >& tracksToRemove) |
| 2594 | { |
| 2595 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2596 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2597 | for (size_t i = 0 ; i < count ; i++) { |
| 2598 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2599 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2600 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2601 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2602 | #ifdef ADD_BATTERY_DATA |
| 2603 | // to track the speaker usage |
| 2604 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2605 | #endif |
| 2606 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2607 | AudioSystem::releaseOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2608 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2609 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2610 | } |
| 2611 | } |
| 2612 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2616 | { |
| 2617 | if (!mMasterMute) { |
| 2618 | char value[PROPERTY_VALUE_MAX]; |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2619 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 2620 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 2621 | return; |
| 2622 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2623 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2624 | char *endptr; |
| 2625 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2626 | if (*endptr == '\0' && ul != 0) { |
| 2627 | ALOGD("Silence is golden"); |
| 2628 | // The setprop command will not allow a property to be changed after |
| 2629 | // the first time it is set, so we don't have to worry about un-muting. |
| 2630 | setMasterMute_l(true); |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2637 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2638 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2639 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2640 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2641 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2642 | |
| 2643 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2644 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2645 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2646 | const size_t count = mBytesRemaining / mFrameSize; |
| 2647 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2648 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2649 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2650 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2651 | if (screenState != mScreenState) { |
| 2652 | mScreenState = screenState; |
| 2653 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2654 | if (pipe != NULL) { |
| 2655 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2656 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2657 | } |
| 2658 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2659 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2660 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2661 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2662 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2663 | } else { |
| 2664 | bytesWritten = framesWritten; |
| 2665 | } |
| 2666 | // otherwise use the HAL / AudioStreamOut directly |
| 2667 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2668 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2669 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2670 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2671 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2672 | mWriteAckSequence += 2; |
| 2673 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2674 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2675 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2676 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2677 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2678 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2679 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2680 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2681 | if (mUseAsyncWrite && |
| 2682 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2683 | // do not wait for async callback in case of error of full write |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2684 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2685 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2686 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2687 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2690 | mNumWrites++; |
| 2691 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2692 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2693 | return bytesWritten; |
| 2694 | } |
| 2695 | |
| 2696 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2697 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2698 | bool supportsDrain = false; |
| 2699 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2700 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2701 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2702 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2703 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2704 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2705 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2706 | } |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 2707 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2708 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2713 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2714 | { |
| 2715 | Mutex::Autolock _l(mLock); |
| 2716 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2717 | sp<Track> track = mTracks[i]; |
| 2718 | track->invalidate(); |
| 2719 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2720 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. |
| 2721 | // After we exit there are no more track changes sent to BatteryNotifier |
| 2722 | // because that requires an active threadLoop. |
| 2723 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? |
| 2724 | mActiveTracks.clear(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2725 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | /* |
| 2729 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2730 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2731 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2732 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2733 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2734 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2735 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2736 | |
| 2737 | The parameters that affect these derived values are: |
| 2738 | - frame count |
| 2739 | - frame size |
| 2740 | - sample rate |
| 2741 | - device type: A2DP or not |
| 2742 | - device latency |
| 2743 | - format: PCM or not |
| 2744 | - active sleep time |
| 2745 | - idle sleep time |
| 2746 | */ |
| 2747 | |
| 2748 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2749 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2750 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2751 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2752 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2753 | |
| 2754 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2755 | // truncating audio when going to standby. |
| 2756 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2757 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2758 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2759 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2760 | } |
| 2761 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2764 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2765 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2766 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2767 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2768 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2769 | size_t size = mTracks.size(); |
| 2770 | for (size_t i = 0; i < size; i++) { |
| 2771 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2772 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2773 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2774 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2775 | } |
| 2776 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2777 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2778 | } |
| 2779 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2780 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2781 | { |
| 2782 | Mutex::Autolock _l(mLock); |
| 2783 | invalidateTracks_l(streamType); |
| 2784 | } |
| 2785 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2786 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2787 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2788 | audio_session_t session = chain->sessionId(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2789 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; |
| 2790 | status_t result = EffectBufferHalInterface::mirror( |
| 2791 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, |
| 2792 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, |
| 2793 | &halInBuffer); |
| 2794 | if (result != OK) return result; |
| 2795 | halOutBuffer = halInBuffer; |
| 2796 | int16_t *buffer = reinterpret_cast<int16_t*>(halInBuffer->externalData()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2797 | |
| 2798 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2799 | if (session > AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2800 | // Only one effect chain can be present in direct output thread and it uses |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2801 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2802 | if (mType != DIRECT) { |
| 2803 | size_t numSamples = mNormalFrameCount * mChannelCount; |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2804 | status_t result = EffectBufferHalInterface::allocate( |
| 2805 | numSamples * sizeof(int16_t), |
| 2806 | &halInBuffer); |
| 2807 | if (result != OK) return result; |
| 2808 | buffer = halInBuffer->audioBuffer()->s16; |
| 2809 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 2810 | buffer, session); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | // Attach all tracks with same session ID to this chain. |
| 2814 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2815 | sp<Track> track = mTracks[i]; |
| 2816 | if (session == track->sessionId()) { |
| 2817 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2818 | buffer); |
| 2819 | track->setMainBuffer(buffer); |
| 2820 | chain->incTrackCnt(); |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | // indicate all active tracks in the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2825 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2826 | if (session == track->sessionId()) { |
| 2827 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2828 | chain->incActiveTrackCnt(); |
| 2829 | } |
| 2830 | } |
| 2831 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2832 | chain->setThread(this); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2833 | chain->setInBuffer(halInBuffer); |
| 2834 | chain->setOutBuffer(halOutBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2835 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2836 | // chains list in order to be processed last as it contains output stage effects. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2837 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2838 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2839 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2840 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2841 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2842 | // Effect chain for other sessions are inserted at beginning of effect |
| 2843 | // chains list to be processed before output mix effects. Relative order between other |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2844 | // sessions is not important. |
| 2845 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
| 2846 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, |
| 2847 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2848 | size_t size = mEffectChains.size(); |
| 2849 | size_t i = 0; |
| 2850 | for (i = 0; i < size; i++) { |
| 2851 | if (mEffectChains[i]->sessionId() < session) { |
| 2852 | break; |
| 2853 | } |
| 2854 | } |
| 2855 | mEffectChains.insertAt(chain, i); |
| 2856 | checkSuspendOnAddEffectChain_l(chain); |
| 2857 | |
| 2858 | return NO_ERROR; |
| 2859 | } |
| 2860 | |
| 2861 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2862 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2863 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2864 | |
| 2865 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2866 | |
| 2867 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2868 | if (chain == mEffectChains[i]) { |
| 2869 | mEffectChains.removeAt(i); |
| 2870 | // detach all active tracks from the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2871 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2872 | if (session == track->sessionId()) { |
| 2873 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2874 | chain.get(), session); |
| 2875 | chain->decActiveTrackCnt(); |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | // detach all tracks with same session ID from this chain |
| 2880 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2881 | sp<Track> track = mTracks[i]; |
| 2882 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2883 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2884 | chain->decTrackCnt(); |
| 2885 | } |
| 2886 | } |
| 2887 | break; |
| 2888 | } |
| 2889 | } |
| 2890 | return mEffectChains.size(); |
| 2891 | } |
| 2892 | |
| 2893 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2894 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2895 | { |
| 2896 | Mutex::Autolock _l(mLock); |
| 2897 | return attachAuxEffect_l(track, EffectId); |
| 2898 | } |
| 2899 | |
| 2900 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2901 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2902 | { |
| 2903 | status_t status = NO_ERROR; |
| 2904 | |
| 2905 | if (EffectId == 0) { |
| 2906 | track->setAuxBuffer(0, NULL); |
| 2907 | } else { |
| 2908 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2909 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2910 | if (effect != 0) { |
| 2911 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2912 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2913 | } else { |
| 2914 | status = INVALID_OPERATION; |
| 2915 | } |
| 2916 | } else { |
| 2917 | status = BAD_VALUE; |
| 2918 | } |
| 2919 | } |
| 2920 | return status; |
| 2921 | } |
| 2922 | |
| 2923 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2924 | { |
| 2925 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2926 | sp<Track> track = mTracks[i]; |
| 2927 | if (track->auxEffectId() == effectId) { |
| 2928 | attachAuxEffect_l(track, 0); |
| 2929 | } |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2934 | { |
Glenn Kasten | 388d571 | 2017-04-07 14:38:41 -0700 | [diff] [blame] | 2935 | tlNBLogWriter = mNBLogWriter.get(); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 2936 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2937 | Vector< sp<Track> > tracksToRemove; |
| 2938 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2939 | mStandbyTimeNs = systemTime(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2940 | nsecs_t lastWriteFinished = -1; // time last server write completed |
| 2941 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2942 | |
| 2943 | // MIXER |
| 2944 | nsecs_t lastWarning = 0; |
| 2945 | |
| 2946 | // DUPLICATING |
| 2947 | // FIXME could this be made local to while loop? |
| 2948 | writeFrames = 0; |
| 2949 | |
| 2950 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2951 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2952 | |
| 2953 | if (mType == MIXER) { |
| 2954 | sleepTimeShift = 0; |
| 2955 | } |
| 2956 | |
| 2957 | CpuStats cpuStats; |
| 2958 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2959 | |
| 2960 | acquireWakeLock(); |
| 2961 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2962 | // mNBLogWriter logging APIs can only be called by a single thread, typically the |
| 2963 | // thread associated with this PlaybackThread. |
| 2964 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) |
| 2965 | // then all such threads must agree to hold a common mutex before logging. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2966 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2967 | // and then that string will be logged at the next convenient opportunity. |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2968 | // See reference to logString below. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2969 | const char *logString = NULL; |
| 2970 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 2971 | // Estimated time for next buffer to be written to hal. This is used only on |
| 2972 | // suspended mode (for now) to help schedule the wait time until next iteration. |
| 2973 | nsecs_t timeLoopNextNs = 0; |
| 2974 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2975 | checkSilentMode_l(); |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2976 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2977 | while (!exitPending()) |
| 2978 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 2979 | // Log merge requests are performed during AudioFlinger binder transactions, but |
| 2980 | // that does not cover audio playback. It's requested here for that reason. |
| 2981 | mAudioFlinger->requestLogMerge(); |
| 2982 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2983 | cpuStats.sample(myName); |
| 2984 | |
| 2985 | Vector< sp<EffectChain> > effectChains; |
| 2986 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2987 | { // scope for mLock |
| 2988 | |
| 2989 | Mutex::Autolock _l(mLock); |
| 2990 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2991 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2992 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2993 | // See comment at declaration of logString for why this is done under mLock |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2994 | if (logString != NULL) { |
| 2995 | mNBLogWriter->logTimestamp(); |
| 2996 | mNBLogWriter->log(logString); |
| 2997 | logString = NULL; |
| 2998 | } |
| 2999 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3000 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3001 | // and associate with the sink frames written out. We need |
| 3002 | // this to convert the sink timestamp to the track timestamp. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3003 | bool kernelLocationUpdate = false; |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3004 | if (mNormalSink != 0) { |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3005 | // Note: The DuplicatingThread may not have a mNormalSink. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3006 | // We always fetch the timestamp here because often the downstream |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3007 | // sink will block while writing. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3008 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 3009 | (void) mNormalSink->getTimestamp(timestamp); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3010 | |
| 3011 | // We keep track of the last valid kernel position in case we are in underrun |
| 3012 | // and the normal mixer period is the same as the fast mixer period, or there |
| 3013 | // is some error from the HAL. |
| 3014 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3015 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3016 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3017 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3018 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3019 | |
| 3020 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3021 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 3022 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3023 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3027 | kernelLocationUpdate = true; |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3028 | } else { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3029 | ALOGVV("getTimestamp error - no valid kernel position"); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3032 | // copy over kernel info |
| 3033 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3034 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 3035 | + mSuspendedFrames; // add frames discarded when suspended |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3036 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 3037 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3038 | } |
| 3039 | // mFramesWritten for non-offloaded tracks are contiguous |
| 3040 | // even after standby() is called. This is useful for the track frame |
| 3041 | // to sink frame mapping. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3042 | bool serverLocationUpdate = false; |
| 3043 | if (mFramesWritten != lastFramesWritten) { |
| 3044 | serverLocationUpdate = true; |
| 3045 | lastFramesWritten = mFramesWritten; |
| 3046 | } |
| 3047 | // Only update timestamps if there is a meaningful change. |
| 3048 | // Either the kernel timestamp must be valid or we have written something. |
| 3049 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 3050 | if (serverLocationUpdate) { |
| 3051 | // use the time before we called the HAL write - it is a bit more accurate |
| 3052 | // to when the server last read data than the current time here. |
| 3053 | // |
| 3054 | // If we haven't written anything, mLastWriteTime will be -1 |
| 3055 | // and we use systemTime(). |
| 3056 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 3057 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 |
| 3058 | ? systemTime() : mLastWriteTime; |
| 3059 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3060 | |
| 3061 | for (const sp<Track> &t : mActiveTracks) { |
| 3062 | if (!t->isFastTrack()) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3063 | t->updateTrackFrameInfo( |
| 3064 | t->mAudioTrackServerProxy->framesReleased(), |
| 3065 | mFramesWritten, |
| 3066 | mTimestamp); |
| 3067 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3068 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3069 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3070 | #if 0 |
| 3071 | // logFormat example |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 3072 | if (z % 100 == 0) { |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3073 | timespec ts; |
| 3074 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3075 | LOGT("This is an integer %d, this is a float %f, this is my " |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3076 | "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts); |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3077 | LOGT("A deceptive null-terminated string %\0"); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3078 | } |
| 3079 | ++z; |
| 3080 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3081 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3082 | if (mSignalPending) { |
| 3083 | // A signal was raised while we were unlocked |
| 3084 | mSignalPending = false; |
| 3085 | } else if (waitingAsyncCallback_l()) { |
| 3086 | if (exitPending()) { |
| 3087 | break; |
| 3088 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3089 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3090 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3091 | releaseWakeLock_l(); |
| 3092 | released = true; |
| 3093 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 3094 | |
| 3095 | const int64_t waitNs = computeWaitTimeNs_l(); |
| 3096 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); |
| 3097 | status_t status = mWaitWorkCV.waitRelative(mLock, waitNs); |
| 3098 | if (status == TIMED_OUT) { |
| 3099 | mSignalPending = true; // if timeout recheck everything |
| 3100 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3101 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3102 | if (released) { |
| 3103 | acquireWakeLock_l(); |
| 3104 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3105 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3106 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3107 | |
| 3108 | continue; |
| 3109 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3110 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3111 | isSuspended()) { |
| 3112 | // put audio hardware into standby after short delay |
| 3113 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3114 | |
| 3115 | threadLoop_standby(); |
| 3116 | |
| 3117 | mStandby = true; |
| 3118 | } |
| 3119 | |
| 3120 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 3121 | // we're about to wait, flush the binder command buffer |
| 3122 | IPCThreadState::self()->flushCommands(); |
| 3123 | |
| 3124 | clearOutputTracks(); |
| 3125 | |
| 3126 | if (exitPending()) { |
| 3127 | break; |
| 3128 | } |
| 3129 | |
| 3130 | releaseWakeLock_l(); |
| 3131 | // wait until we have something to do... |
| 3132 | ALOGV("%s going to sleep", myName.string()); |
| 3133 | mWaitWorkCV.wait(mLock); |
| 3134 | ALOGV("%s waking up", myName.string()); |
| 3135 | acquireWakeLock_l(); |
| 3136 | |
| 3137 | mMixerStatus = MIXER_IDLE; |
| 3138 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 3139 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3140 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3141 | checkSilentMode_l(); |
| 3142 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3143 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3144 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3145 | if (mType == MIXER) { |
| 3146 | sleepTimeShift = 0; |
| 3147 | } |
| 3148 | |
| 3149 | continue; |
| 3150 | } |
| 3151 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3152 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 3153 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 3154 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3155 | mActiveTracks.updatePowerState(this); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3156 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3157 | // prevent any changes in effect chain list and in each effect chain |
| 3158 | // during mixing and effect process as the audio buffers could be deleted |
| 3159 | // or modified if an effect is created or deleted |
| 3160 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3161 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3162 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3163 | if (mBytesRemaining == 0) { |
| 3164 | mCurrentWriteLength = 0; |
| 3165 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3166 | // threadLoop_mix() sets mCurrentWriteLength |
| 3167 | threadLoop_mix(); |
| 3168 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3169 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3170 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3171 | // must be written to HAL |
| 3172 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3173 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3174 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3175 | } |
| 3176 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3177 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3178 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3179 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3180 | // or mSinkBuffer (if there are no effects). |
| 3181 | // |
| 3182 | // This is done pre-effects computation; if effects change to |
| 3183 | // support higher precision, this needs to move. |
| 3184 | // |
| 3185 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3186 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3187 | if (mMixerBufferValid) { |
| 3188 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3189 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3190 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3191 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3192 | // and is handled here if we're going directly to the sink. |
| 3193 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3194 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3195 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3196 | } |
| 3197 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3198 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3199 | mNormalFrameCount * mChannelCount); |
| 3200 | } |
| 3201 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3202 | mBytesRemaining = mCurrentWriteLength; |
| 3203 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3204 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 3205 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 3206 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 3207 | mBytesWritten += mBytesRemaining; |
| 3208 | mFramesWritten += framesRemaining; |
| 3209 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3210 | mBytesRemaining = 0; |
| 3211 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3212 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3213 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3214 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3215 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3216 | effectChains[i]->process_l(); |
| 3217 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3218 | } |
| 3219 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3220 | // Process effect chains for offloaded thread even if no audio |
| 3221 | // was read from audio track: process only updates effect state |
| 3222 | // and thus does have to be synchronized with audio writes but may have |
| 3223 | // to be called while waiting for async write callback |
| 3224 | if (mType == OFFLOAD) { |
| 3225 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3226 | effectChains[i]->process_l(); |
| 3227 | } |
| 3228 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3229 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3230 | // Only if the Effects buffer is enabled and there is data in the |
| 3231 | // Effects buffer (buffer valid), we need to |
| 3232 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3233 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3234 | if (mEffectBufferValid) { |
| 3235 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3236 | |
| 3237 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3238 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3239 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3242 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3243 | mNormalFrameCount * mChannelCount); |
| 3244 | } |
| 3245 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3246 | // enable changes in effect chain |
| 3247 | unlockEffectChains(effectChains); |
| 3248 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3249 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3250 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3251 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3252 | ssize_t ret = 0; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3253 | // We save lastWriteFinished here, as previousLastWriteFinished, |
| 3254 | // for throttling. On thread start, previousLastWriteFinished will be |
| 3255 | // set to -1, which properly results in no throttling after the first write. |
| 3256 | nsecs_t previousLastWriteFinished = lastWriteFinished; |
| 3257 | nsecs_t delta = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3258 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3259 | // FIXME rewrite to reduce number of system calls |
| 3260 | mLastWriteTime = systemTime(); // also used for dumpsys |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3261 | ret = threadLoop_write(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3262 | lastWriteFinished = systemTime(); |
| 3263 | delta = lastWriteFinished - mLastWriteTime; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3264 | if (ret < 0) { |
| 3265 | mBytesRemaining = 0; |
| 3266 | } else { |
| 3267 | mBytesWritten += ret; |
| 3268 | mBytesRemaining -= ret; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3269 | mFramesWritten += ret / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3270 | } |
| 3271 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3272 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3273 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3274 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3275 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3276 | // write blocked detection |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3277 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3278 | mNumDelayedWrites++; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3279 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3280 | ATRACE_NAME("underrun"); |
| 3281 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3282 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3283 | lastWarning = lastWriteFinished; |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3284 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3285 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3286 | |
| 3287 | if (mThreadThrottle |
| 3288 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3289 | && ret > 0) { // we wrote something |
| 3290 | // Limit MixerThread data processing to no more than twice the |
| 3291 | // expected processing rate. |
| 3292 | // |
| 3293 | // This helps prevent underruns with NuPlayer and other applications |
| 3294 | // which may set up buffers that are close to the minimum size, or use |
| 3295 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3296 | // |
| 3297 | // The throttle smooths out sudden large data drains from the device, |
| 3298 | // e.g. when it comes out of standby, which often causes problems with |
| 3299 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3300 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3301 | // the app won't fill fast enough to handle the sudden draw). |
Haynes Mathew George | f92b217 | 2016-05-09 11:34:15 -0700 | [diff] [blame] | 3302 | // |
| 3303 | // Total time spent in last processing cycle equals time spent in |
| 3304 | // 1. threadLoop_write, as well as time spent in |
| 3305 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 3306 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3307 | |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3308 | // it's OK if deltaMs is an overestimate. |
| 3309 | const int32_t deltaMs = |
| 3310 | (lastWriteFinished - previousLastWriteFinished) / 1000000; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3311 | const int32_t throttleMs = mHalfBufferMs - deltaMs; |
| 3312 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3313 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3314 | // notify of throttle start on verbose log |
| 3315 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3316 | "mixer(%p) throttle begin:" |
| 3317 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3318 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3319 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3320 | // Throttle must be attributed to the previous mixer loop's write time |
| 3321 | // to allow back-to-back throttling. |
| 3322 | lastWriteFinished += throttleMs * 1000000; |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3323 | } else { |
| 3324 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3325 | if (diff > 0) { |
| 3326 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3327 | // but prevent spamming for bluetooth |
| 3328 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()), |
| 3329 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3330 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3331 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3332 | } |
| 3333 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3334 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3335 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3336 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3337 | ATRACE_BEGIN("sleep"); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3338 | Mutex::Autolock _l(mLock); |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3339 | // suspended requires accurate metering of sleep time. |
| 3340 | if (isSuspended()) { |
| 3341 | // advance by expected sleepTime |
| 3342 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); |
| 3343 | const nsecs_t nowNs = systemTime(); |
| 3344 | |
| 3345 | // compute expected next time vs current time. |
| 3346 | // (negative deltas are treated as delays). |
| 3347 | nsecs_t deltaNs = timeLoopNextNs - nowNs; |
| 3348 | if (deltaNs < -kMaxNextBufferDelayNs) { |
| 3349 | // Delays longer than the max allowed trigger a reset. |
| 3350 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); |
| 3351 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); |
| 3352 | timeLoopNextNs = nowNs + deltaNs; |
| 3353 | } else if (deltaNs < 0) { |
| 3354 | // Delays within the max delay allowed: zero the delta/sleepTime |
| 3355 | // to help the system catch up in the next iteration(s) |
| 3356 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); |
| 3357 | deltaNs = 0; |
| 3358 | } |
| 3359 | // update sleep time (which is >= 0) |
| 3360 | mSleepTimeUs = deltaNs / 1000; |
| 3361 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3362 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
| 3363 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3364 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3365 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3366 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | // Finally let go of removed track(s), without the lock held |
| 3370 | // since we can't guarantee the destructors won't acquire that |
| 3371 | // same lock. This will also mutate and push a new fast mixer state. |
| 3372 | threadLoop_removeTracks(tracksToRemove); |
| 3373 | tracksToRemove.clear(); |
| 3374 | |
| 3375 | // FIXME I don't understand the need for this here; |
| 3376 | // it was in the original code but maybe the |
| 3377 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3378 | clearOutputTracks(); |
| 3379 | |
| 3380 | // Effect chains will be actually deleted here if they were removed from |
| 3381 | // mEffectChains list during mixing or effects processing |
| 3382 | effectChains.clear(); |
| 3383 | |
| 3384 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3385 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3386 | } |
| 3387 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3388 | threadLoop_exit(); |
| 3389 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3390 | if (!mStandby) { |
| 3391 | threadLoop_standby(); |
| 3392 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | releaseWakeLock(); |
| 3396 | |
| 3397 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3398 | return false; |
| 3399 | } |
| 3400 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3401 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3402 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3403 | { |
| 3404 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3405 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3406 | for (size_t i=0 ; i<count ; i++) { |
| 3407 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3408 | mActiveTracks.remove(track); |
| 3409 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3410 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3411 | if (chain != 0) { |
| 3412 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3413 | track->sessionId()); |
| 3414 | chain->decActiveTrackCnt(); |
| 3415 | } |
| 3416 | if (track->isTerminated()) { |
| 3417 | removeTrack_l(track); |
| 3418 | } |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3423 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3424 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3425 | { |
| 3426 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3427 | ExtendedTimestamp ets; |
| 3428 | status_t status = mNormalSink->getTimestamp(ets); |
| 3429 | if (status == NO_ERROR) { |
| 3430 | status = ets.getBestTimestamp(×tamp); |
| 3431 | } |
| 3432 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3433 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3434 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3435 | uint64_t position64; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3436 | if (mOutput->getPresentationPosition(&position64, ×tamp.mTime) == OK) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3437 | timestamp.mPosition = (uint32_t)position64; |
| 3438 | return NO_ERROR; |
| 3439 | } |
| 3440 | } |
| 3441 | return INVALID_OPERATION; |
| 3442 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3443 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3444 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3445 | audio_patch_handle_t *handle) |
| 3446 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3447 | status_t status; |
| 3448 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3449 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3450 | // or if HAL does not properly lock against access. |
| 3451 | AutoPark<FastMixer> park(mFastMixer); |
| 3452 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3453 | } else { |
| 3454 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3455 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3456 | return status; |
| 3457 | } |
| 3458 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3459 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3460 | audio_patch_handle_t *handle) |
| 3461 | { |
| 3462 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3463 | |
| 3464 | // store new device and send to effects |
| 3465 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3466 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3467 | type |= patch->sinks[i].ext.device.type; |
| 3468 | } |
| 3469 | |
| 3470 | #ifdef ADD_BATTERY_DATA |
| 3471 | // when changing the audio output device, call addBatteryData to notify |
| 3472 | // the change |
| 3473 | if (mOutDevice != type) { |
| 3474 | uint32_t params = 0; |
| 3475 | // check whether speaker is on |
| 3476 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3477 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3480 | audio_devices_t deviceWithoutSpeaker |
| 3481 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3482 | // check if any other device (except speaker) is on |
| 3483 | if (type & deviceWithoutSpeaker) { |
| 3484 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3485 | } |
| 3486 | |
| 3487 | if (params != 0) { |
| 3488 | addBatteryData(params); |
| 3489 | } |
| 3490 | } |
| 3491 | #endif |
| 3492 | |
| 3493 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3494 | mEffectChains[i]->setDevice_l(type); |
| 3495 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3496 | |
| 3497 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3498 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3499 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3500 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3501 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3502 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3503 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3504 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3505 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 3506 | patch->sources, |
| 3507 | patch->num_sinks, |
| 3508 | patch->sinks, |
| 3509 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3510 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3511 | char *address; |
| 3512 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3513 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3514 | address = audio_device_address_to_parameter( |
| 3515 | patch->sinks[0].ext.device.type, |
| 3516 | patch->sinks[0].ext.device.address); |
| 3517 | } else { |
| 3518 | address = (char *)calloc(1, 1); |
| 3519 | } |
| 3520 | AudioParameter param = AudioParameter(String8(address)); |
| 3521 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3522 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3523 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3524 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3525 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3526 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3527 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3528 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3529 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3530 | return status; |
| 3531 | } |
| 3532 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3533 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3534 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3535 | status_t status; |
| 3536 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3537 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3538 | // or if HAL does not properly lock against access. |
| 3539 | AutoPark<FastMixer> park(mFastMixer); |
| 3540 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3541 | } else { |
| 3542 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3543 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3544 | return status; |
| 3545 | } |
| 3546 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3547 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3548 | { |
| 3549 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3550 | |
| 3551 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3552 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3553 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3554 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3555 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3556 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3557 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3558 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3559 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3560 | } |
| 3561 | return status; |
| 3562 | } |
| 3563 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3564 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3565 | { |
| 3566 | Mutex::Autolock _l(mLock); |
| 3567 | mTracks.add(track); |
| 3568 | } |
| 3569 | |
| 3570 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3571 | { |
| 3572 | Mutex::Autolock _l(mLock); |
| 3573 | destroyTrack_l(track); |
| 3574 | } |
| 3575 | |
| 3576 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3577 | { |
| 3578 | ThreadBase::getAudioPortConfig(config); |
| 3579 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3580 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3581 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3582 | } |
| 3583 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3584 | // ---------------------------------------------------------------------------- |
| 3585 | |
| 3586 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3587 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3588 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3589 | // mAudioMixer below |
| 3590 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3591 | mFastMixerFutex(0), |
| 3592 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3593 | // mOutputSink below |
| 3594 | // mPipeSink below |
| 3595 | // mNormalSink below |
| 3596 | { |
| 3597 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3598 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%zu, " |
| 3599 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3600 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3601 | mNormalFrameCount); |
| 3602 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3603 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3604 | if (type == DUPLICATING) { |
| 3605 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3606 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3607 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3608 | return; |
| 3609 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3610 | // create an NBAIO sink for the HAL output stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 3611 | mOutputSink = new AudioStreamOutSink(output->stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3613 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3614 | #if !LOG_NDEBUG |
| 3615 | ssize_t index = |
| 3616 | #else |
| 3617 | (void) |
| 3618 | #endif |
| 3619 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3620 | ALOG_ASSERT(index == 0); |
| 3621 | |
| 3622 | // initialize fast mixer depending on configuration |
| 3623 | bool initFastMixer; |
| 3624 | switch (kUseFastMixer) { |
| 3625 | case FastMixer_Never: |
| 3626 | initFastMixer = false; |
| 3627 | break; |
| 3628 | case FastMixer_Always: |
| 3629 | initFastMixer = true; |
| 3630 | break; |
| 3631 | case FastMixer_Static: |
| 3632 | case FastMixer_Dynamic: |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3633 | // FastMixer was designed to operate with a HAL that pulls at a regular rate, |
| 3634 | // where the period is less than an experimentally determined threshold that can be |
| 3635 | // scheduled reliably with CFS. However, the BT A2DP HAL is |
| 3636 | // bursty (does not pull at a regular rate) and so cannot operate with FastMixer. |
| 3637 | initFastMixer = mFrameCount < mNormalFrameCount |
| 3638 | && (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) == 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3639 | break; |
| 3640 | } |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3641 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, |
| 3642 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", |
| 3643 | mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3644 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3645 | audio_format_t fastMixerFormat; |
| 3646 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3647 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3648 | } else { |
| 3649 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3650 | } |
| 3651 | if (mFormat != fastMixerFormat) { |
| 3652 | // change our Sink format to accept our intermediate precision |
| 3653 | mFormat = fastMixerFormat; |
| 3654 | free(mSinkBuffer); |
| 3655 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3656 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3657 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3658 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3659 | |
| 3660 | // create a MonoPipe to connect our submix to FastMixer |
| 3661 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3662 | #ifdef TEE_SINK |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3663 | NBAIO_Format origformat = format; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3664 | #endif |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3665 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3666 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3667 | format.mFormat = fastMixerFormat; |
| 3668 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3669 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3670 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3671 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3672 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3673 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3674 | const NBAIO_Format offers[1] = {format}; |
| 3675 | size_t numCounterOffers = 0; |
Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3676 | #if !LOG_NDEBUG || defined(TEE_SINK) |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3677 | ssize_t index = |
| 3678 | #else |
| 3679 | (void) |
| 3680 | #endif |
| 3681 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3682 | ALOG_ASSERT(index == 0); |
| 3683 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3684 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3685 | mPipeSink = monoPipe; |
| 3686 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3687 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3688 | if (mTeeSinkOutputEnabled) { |
| 3689 | // create a Pipe to archive a copy of FastMixer's output for dumpsys |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3690 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3691 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3692 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3693 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3694 | ALOG_ASSERT(index == 0); |
| 3695 | mTeeSink = teeSink; |
| 3696 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3697 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3698 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3699 | ALOG_ASSERT(index == 0); |
| 3700 | mTeeSource = teeSource; |
| 3701 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3702 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3703 | |
| 3704 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3705 | mFastMixer = new FastMixer(); |
| 3706 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3707 | #ifdef STATE_QUEUE_DUMP |
| 3708 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3709 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3710 | #endif |
| 3711 | FastMixerState *state = sq->begin(); |
| 3712 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3713 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3714 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3715 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3716 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3717 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3718 | fastTrack->mGeneration++; |
| 3719 | state->mFastTracksGen++; |
| 3720 | state->mTrackMask = 1; |
| 3721 | // fast mixer will use the HAL output sink |
| 3722 | state->mOutputSink = mOutputSink.get(); |
| 3723 | state->mOutputSinkGen++; |
| 3724 | state->mFrameCount = mFrameCount; |
| 3725 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3726 | // already done in constructor initialization list |
| 3727 | //mFastMixerFutex = 0; |
| 3728 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3729 | state->mColdGen++; |
| 3730 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3731 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3732 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3733 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3734 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3735 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3736 | sq->end(); |
| 3737 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3738 | |
| 3739 | // start the fast mixer |
| 3740 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3741 | pid_t tid = mFastMixer->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3742 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 3743 | stream()->setHalThreadPriority(kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3744 | |
| 3745 | #ifdef AUDIO_WATCHDOG |
| 3746 | // create and start the watchdog |
| 3747 | mAudioWatchdog = new AudioWatchdog(); |
| 3748 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3749 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3750 | tid = mAudioWatchdog->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3751 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3752 | #endif |
| 3753 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | switch (kUseFastMixer) { |
| 3757 | case FastMixer_Never: |
| 3758 | case FastMixer_Dynamic: |
| 3759 | mNormalSink = mOutputSink; |
| 3760 | break; |
| 3761 | case FastMixer_Always: |
| 3762 | mNormalSink = mPipeSink; |
| 3763 | break; |
| 3764 | case FastMixer_Static: |
| 3765 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3766 | break; |
| 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | AudioFlinger::MixerThread::~MixerThread() |
| 3771 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3772 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3773 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3774 | FastMixerState *state = sq->begin(); |
| 3775 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3776 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3777 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3778 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3779 | } |
| 3780 | } |
| 3781 | state->mCommand = FastMixerState::EXIT; |
| 3782 | sq->end(); |
| 3783 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3784 | mFastMixer->join(); |
| 3785 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3786 | // We'll use that extract the final state which contains one remaining fast track |
| 3787 | // corresponding to our sub-mix. |
| 3788 | state = sq->begin(); |
| 3789 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3790 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3791 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3792 | delete fastTrack->mBufferProvider; |
| 3793 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3794 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3795 | #ifdef AUDIO_WATCHDOG |
| 3796 | if (mAudioWatchdog != 0) { |
| 3797 | mAudioWatchdog->requestExit(); |
| 3798 | mAudioWatchdog->requestExitAndWait(); |
| 3799 | mAudioWatchdog.clear(); |
| 3800 | } |
| 3801 | #endif |
| 3802 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3803 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3804 | delete mAudioMixer; |
| 3805 | } |
| 3806 | |
| 3807 | |
| 3808 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3809 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3810 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3811 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3812 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3813 | } |
| 3814 | return latency; |
| 3815 | } |
| 3816 | |
| 3817 | |
| 3818 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3819 | { |
| 3820 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3821 | } |
| 3822 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3823 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3824 | { |
| 3825 | // FIXME we should only do one push per cycle; confirm this is true |
| 3826 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3827 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3828 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3829 | FastMixerState *state = sq->begin(); |
| 3830 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3831 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3832 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3833 | |
| 3834 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 3835 | ATRACE_BEGIN("write"); |
| 3836 | mOutput->write((char *)mSinkBuffer, 0); |
| 3837 | ATRACE_END(); |
| 3838 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3839 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3840 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3841 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3842 | } |
| 3843 | #ifdef AUDIO_WATCHDOG |
| 3844 | if (mAudioWatchdog != 0) { |
| 3845 | mAudioWatchdog->resume(); |
| 3846 | } |
| 3847 | #endif |
| 3848 | } |
| 3849 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3850 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3851 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3852 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3853 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3854 | sq->end(); |
| 3855 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3856 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3857 | mNormalSink = mPipeSink; |
| 3858 | } |
| 3859 | } else { |
| 3860 | sq->end(false /*didModify*/); |
| 3861 | } |
| 3862 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3863 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3867 | { |
| 3868 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3869 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3870 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3871 | FastMixerState *state = sq->begin(); |
| 3872 | if (!(state->mCommand & FastMixerState::IDLE)) { |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3873 | // Report any frames trapped in the Monopipe |
| 3874 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); |
| 3875 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); |
| 3876 | mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld " |
| 3877 | "monoPipeWritten:%lld monoPipeLeft:%lld", |
| 3878 | (long long)mFramesWritten, (long long)mSuspendedFrames, |
| 3879 | (long long)mPipeSink->framesWritten(), pipeFrames); |
| 3880 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); |
| 3881 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3882 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3883 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3884 | state->mColdGen++; |
| 3885 | mFastMixerFutex = 0; |
| 3886 | sq->end(); |
| 3887 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3888 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3889 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3890 | mNormalSink = mOutputSink; |
| 3891 | } |
| 3892 | #ifdef AUDIO_WATCHDOG |
| 3893 | if (mAudioWatchdog != 0) { |
| 3894 | mAudioWatchdog->pause(); |
| 3895 | } |
| 3896 | #endif |
| 3897 | } else { |
| 3898 | sq->end(false /*didModify*/); |
| 3899 | } |
| 3900 | } |
| 3901 | PlaybackThread::threadLoop_standby(); |
| 3902 | } |
| 3903 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3904 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3905 | { |
| 3906 | return false; |
| 3907 | } |
| 3908 | |
| 3909 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3910 | { |
| 3911 | return !mStandby; |
| 3912 | } |
| 3913 | |
| 3914 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3915 | { |
| 3916 | Mutex::Autolock _l(mLock); |
| 3917 | return waitingAsyncCallback_l(); |
| 3918 | } |
| 3919 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3920 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3921 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3922 | { |
| 3923 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3924 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3925 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3926 | // discard any pending drain or write ack by incrementing sequence |
| 3927 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3928 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3929 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3930 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3931 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3932 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3933 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3934 | } |
| 3935 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3936 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3937 | { |
| 3938 | ALOGV("signal playback thread"); |
| 3939 | broadcast_l(); |
| 3940 | } |
| 3941 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3942 | void AudioFlinger::PlaybackThread::onAsyncError() |
| 3943 | { |
| 3944 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 3945 | invalidateTracks((audio_stream_type_t)i); |
| 3946 | } |
| 3947 | } |
| 3948 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3949 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3950 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3951 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3952 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3953 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3954 | // increase sleep time progressively when application underrun condition clears. |
| 3955 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3956 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3957 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3958 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3959 | sleepTimeShift--; |
| 3960 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3961 | mSleepTimeUs = 0; |
| 3962 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3963 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3964 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3968 | { |
| 3969 | // If no tracks are ready, sleep once for the duration of an output |
| 3970 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3971 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3972 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3973 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 3974 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 3975 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3976 | } |
| 3977 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3978 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3979 | // duration we would end up writing less data than needed by the audio HAL if |
| 3980 | // the condition persists. |
| 3981 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3982 | sleepTimeShift++; |
| 3983 | } |
| 3984 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3985 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3986 | } |
| 3987 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3988 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3989 | // before effects processing or output. |
| 3990 | if (mMixerBufferValid) { |
| 3991 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3992 | } else { |
| 3993 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3994 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3995 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3996 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3997 | "anticipated start"); |
| 3998 | } |
| 3999 | // TODO add standby time extension fct of effect tail |
| 4000 | } |
| 4001 | |
| 4002 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 4003 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 4004 | Vector< sp<Track> > *tracksToRemove) |
| 4005 | { |
| 4006 | |
| 4007 | mixer_state mixerStatus = MIXER_IDLE; |
| 4008 | // find out which tracks need to be processed |
| 4009 | size_t count = mActiveTracks.size(); |
| 4010 | size_t mixedTracks = 0; |
| 4011 | size_t tracksWithEffect = 0; |
| 4012 | // counts only _active_ fast tracks |
| 4013 | size_t fastTracks = 0; |
| 4014 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 4015 | |
| 4016 | float masterVolume = mMasterVolume; |
| 4017 | bool masterMute = mMasterMute; |
| 4018 | |
| 4019 | if (masterMute) { |
| 4020 | masterVolume = 0; |
| 4021 | } |
| 4022 | // Delegate master volume control to effect in output mix effect chain if needed |
| 4023 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 4024 | if (chain != 0) { |
| 4025 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 4026 | chain->setVolume_l(&v, &v); |
| 4027 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 4028 | chain.clear(); |
| 4029 | } |
| 4030 | |
| 4031 | // prepare a new state to push |
| 4032 | FastMixerStateQueue *sq = NULL; |
| 4033 | FastMixerState *state = NULL; |
| 4034 | bool didModify = false; |
| 4035 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4036 | bool coldIdle = false; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4037 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4038 | sq = mFastMixer->sq(); |
| 4039 | state = sq->begin(); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4040 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4041 | } |
| 4042 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4043 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4044 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4045 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4046 | for (size_t i=0 ; i<count ; i++) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4047 | const sp<Track> t = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4048 | |
| 4049 | // this const just means the local variable doesn't change |
| 4050 | Track* const track = t.get(); |
| 4051 | |
| 4052 | // process fast tracks |
| 4053 | if (track->isFastTrack()) { |
| 4054 | |
| 4055 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 4056 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 4057 | // the track never becomes active so it's fast mixer slot is never touched. |
| 4058 | // The converse, of removing an (active) track and then creating a new track |
| 4059 | // at the identical fast mixer slot within the same normal mix cycle, |
| 4060 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 4061 | int j = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4062 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4063 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 4064 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 4065 | |
| 4066 | // Determine whether the track is currently in underrun condition, |
| 4067 | // and whether it had a recent underrun. |
| 4068 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 4069 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 4070 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 4071 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 4072 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 4073 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 4074 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 4075 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 4076 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 4077 | track->mObservedUnderruns = underruns; |
| 4078 | // don't count underruns that occur while stopping or pausing |
| 4079 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4080 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 4081 | recentUnderruns > 0) { |
| 4082 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 4083 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4084 | } else { |
| 4085 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | // This is similar to the state machine for normal tracks, |
| 4089 | // with a few modifications for fast tracks. |
| 4090 | bool isActive = true; |
| 4091 | switch (track->mState) { |
| 4092 | case TrackBase::STOPPING_1: |
| 4093 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4094 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4095 | track->mState = TrackBase::STOPPING_2; |
| 4096 | } |
| 4097 | break; |
| 4098 | case TrackBase::PAUSING: |
| 4099 | // ramp down is not yet implemented |
| 4100 | track->setPaused(); |
| 4101 | break; |
| 4102 | case TrackBase::RESUMING: |
| 4103 | // ramp up is not yet implemented |
| 4104 | track->mState = TrackBase::ACTIVE; |
| 4105 | break; |
| 4106 | case TrackBase::ACTIVE: |
| 4107 | if (recentFull > 0 || recentPartial > 0) { |
| 4108 | // track has provided at least some frames recently: reset retry count |
| 4109 | track->mRetryCount = kMaxTrackRetries; |
| 4110 | } |
| 4111 | if (recentUnderruns == 0) { |
| 4112 | // no recent underruns: stay active |
| 4113 | break; |
| 4114 | } |
| 4115 | // there has recently been an underrun of some kind |
| 4116 | if (track->sharedBuffer() == 0) { |
| 4117 | // were any of the recent underruns "empty" (no frames available)? |
| 4118 | if (recentEmpty == 0) { |
| 4119 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 4120 | break; |
| 4121 | } |
| 4122 | // there has recently been an "empty" underrun: decrement the retry counter |
| 4123 | if (--(track->mRetryCount) > 0) { |
| 4124 | break; |
| 4125 | } |
| 4126 | // indicate to client process that the track was disabled because of underrun; |
| 4127 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4128 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4129 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 4130 | isActive = false; |
| 4131 | break; |
| 4132 | } |
| 4133 | // fall through |
| 4134 | case TrackBase::STOPPING_2: |
| 4135 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4136 | case TrackBase::STOPPED: |
| 4137 | case TrackBase::FLUSHED: // flush() while active |
| 4138 | // Check for presentation complete if track is inactive |
| 4139 | // We have consumed all the buffers of this track. |
| 4140 | // This would be incomplete if we auto-paused on underrun |
| 4141 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4142 | uint32_t latency = 0; |
| 4143 | status_t result = mOutput->stream->getLatency(&latency); |
| 4144 | ALOGE_IF(result != OK, |
| 4145 | "Error when retrieving output stream latency: %d", result); |
| 4146 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4147 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4148 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 4149 | // track stays in active list until presentation is complete |
| 4150 | break; |
| 4151 | } |
| 4152 | } |
| 4153 | if (track->isStopping_2()) { |
| 4154 | track->mState = TrackBase::STOPPED; |
| 4155 | } |
| 4156 | if (track->isStopped()) { |
| 4157 | // Can't reset directly, as fast mixer is still polling this track |
| 4158 | // track->reset(); |
| 4159 | // So instead mark this track as needing to be reset after push with ack |
| 4160 | resetMask |= 1 << i; |
| 4161 | } |
| 4162 | isActive = false; |
| 4163 | break; |
| 4164 | case TrackBase::IDLE: |
| 4165 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4166 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | if (isActive) { |
| 4170 | // was it previously inactive? |
| 4171 | if (!(state->mTrackMask & (1 << j))) { |
| 4172 | ExtendedAudioBufferProvider *eabp = track; |
| 4173 | VolumeProvider *vp = track; |
| 4174 | fastTrack->mBufferProvider = eabp; |
| 4175 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4176 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4177 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4178 | fastTrack->mGeneration++; |
| 4179 | state->mTrackMask |= 1 << j; |
| 4180 | didModify = true; |
| 4181 | // no acknowledgement required for newly active tracks |
| 4182 | } |
| 4183 | // cache the combined master volume and stream type volume for fast mixer; this |
| 4184 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4185 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4186 | track->mAudioTrackServerProxy->framesReleased()).first; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4187 | track->mCachedVolume = masterVolume |
| 4188 | * mStreamTypes[track->streamType()].volume |
| 4189 | * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4190 | ++fastTracks; |
| 4191 | } else { |
| 4192 | // was it previously active? |
| 4193 | if (state->mTrackMask & (1 << j)) { |
| 4194 | fastTrack->mBufferProvider = NULL; |
| 4195 | fastTrack->mGeneration++; |
| 4196 | state->mTrackMask &= ~(1 << j); |
| 4197 | didModify = true; |
| 4198 | // If any fast tracks were removed, we must wait for acknowledgement |
| 4199 | // because we're about to decrement the last sp<> on those tracks. |
| 4200 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4201 | } else { |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4202 | LOG_ALWAYS_FATAL("fast track %d should have been active; " |
| 4203 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 4204 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 4205 | track->sharedBuffer() != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4206 | } |
| 4207 | tracksToRemove->add(track); |
| 4208 | // Avoids a misleading display in dumpsys |
| 4209 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 4210 | } |
| 4211 | continue; |
| 4212 | } |
| 4213 | |
| 4214 | { // local variable scope to avoid goto warning |
| 4215 | |
| 4216 | audio_track_cblk_t* cblk = track->cblk(); |
| 4217 | |
| 4218 | // The first time a track is added we wait |
| 4219 | // for all its buffers to be filled before processing it |
| 4220 | int name = track->name(); |
| 4221 | // make sure that we have enough frames to mix one full buffer. |
| 4222 | // enforce this condition only once to enable draining the buffer in case the client |
| 4223 | // app does not call stop() and relies on underrun to stop: |
| 4224 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4225 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4226 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4227 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4228 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4229 | |
| 4230 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4231 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4232 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4233 | // add frames already consumed but not yet released by the resampler |
| 4234 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4235 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4236 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4237 | uint32_t minFrames = 1; |
| 4238 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4239 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4240 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4241 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4242 | |
| 4243 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4244 | if (ATRACE_ENABLED()) { |
| 4245 | // I wish we had formatted trace names |
| 4246 | char traceName[16]; |
| 4247 | strcpy(traceName, "nRdy"); |
| 4248 | int name = track->name(); |
| 4249 | if (AudioMixer::TRACK0 <= name && |
| 4250 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 4251 | name -= AudioMixer::TRACK0; |
| 4252 | traceName[4] = (name / 10) + '0'; |
| 4253 | traceName[5] = (name % 10) + '0'; |
| 4254 | } else { |
| 4255 | traceName[4] = '?'; |
| 4256 | traceName[5] = '?'; |
| 4257 | } |
| 4258 | traceName[6] = '\0'; |
| 4259 | ATRACE_INT(traceName, framesReady); |
| 4260 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4261 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4262 | !track->isPaused() && !track->isTerminated()) |
| 4263 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4264 | ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4265 | |
| 4266 | mixedTracks++; |
| 4267 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4268 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4269 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4270 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4271 | if (track->mainBuffer() != mSinkBuffer && |
| 4272 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4273 | if (mEffectBufferEnabled) { |
| 4274 | mEffectBufferValid = true; // Later can set directly. |
| 4275 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4276 | chain = getEffectChain_l(track->sessionId()); |
| 4277 | // Delegate volume control to effect in track effect chain if needed |
| 4278 | if (chain != 0) { |
| 4279 | tracksWithEffect++; |
| 4280 | } else { |
| 4281 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4282 | "session %d", |
| 4283 | name, track->sessionId()); |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | |
| 4288 | int param = AudioMixer::VOLUME; |
| 4289 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4290 | // no ramp for the first volume setting |
| 4291 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4292 | if (track->mState == TrackBase::RESUMING) { |
| 4293 | track->mState = TrackBase::ACTIVE; |
| 4294 | param = AudioMixer::RAMP_VOLUME; |
| 4295 | } |
| 4296 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4297 | // FIXME should not make a decision based on mServer |
| 4298 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4299 | // If the track is stopped before the first frame was mixed, |
| 4300 | // do not apply ramp |
| 4301 | param = AudioMixer::RAMP_VOLUME; |
| 4302 | } |
| 4303 | |
| 4304 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4305 | uint32_t vl, vr; // in U8.24 integer format |
| 4306 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4307 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4308 | vl = vr = 0; |
| 4309 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4310 | if (track->isPausing()) { |
| 4311 | track->setPaused(); |
| 4312 | } |
| 4313 | } else { |
| 4314 | |
| 4315 | // read original volumes with volume control |
| 4316 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4317 | float v = masterVolume * typeVolume; |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4318 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4319 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4320 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4321 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4322 | // track volumes come from shared memory, so can't be trusted and must be clamped |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4323 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4324 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4325 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4326 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4327 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4328 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4329 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4330 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4331 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4332 | track->mAudioTrackServerProxy->framesReleased()).first; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4333 | // now apply the master volume and stream type volume and shaper volume |
| 4334 | vlf *= v * vh; |
| 4335 | vrf *= v * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4336 | // assuming master volume and stream type volume each go up to 1.0, |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4337 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4338 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4339 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4340 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4341 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4342 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4343 | // send level comes from shared memory and so may be corrupt |
| 4344 | if (sendLevel > MAX_GAIN_INT) { |
| 4345 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4346 | sendLevel = MAX_GAIN_INT; |
| 4347 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4348 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4349 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4350 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4351 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4352 | // Delegate volume control to effect in track effect chain if needed |
| 4353 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4354 | // Do not ramp volume if volume is controlled by effect |
| 4355 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4356 | // Update remaining floating point volume levels |
| 4357 | vlf = (float)vl / (1 << 24); |
| 4358 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4359 | track->mHasVolumeController = true; |
| 4360 | } else { |
| 4361 | // force no volume ramp when volume controller was just disabled or removed |
| 4362 | // from effect chain to avoid volume spike |
| 4363 | if (track->mHasVolumeController) { |
| 4364 | param = AudioMixer::VOLUME; |
| 4365 | } |
| 4366 | track->mHasVolumeController = false; |
| 4367 | } |
| 4368 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4369 | // XXX: these things DON'T need to be done each time |
| 4370 | mAudioMixer->setBufferProvider(name, track); |
| 4371 | mAudioMixer->enable(name); |
| 4372 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4373 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4374 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4375 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4376 | mAudioMixer->setParameter( |
| 4377 | name, |
| 4378 | AudioMixer::TRACK, |
| 4379 | AudioMixer::FORMAT, (void *)track->format()); |
| 4380 | mAudioMixer->setParameter( |
| 4381 | name, |
| 4382 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4383 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4384 | mAudioMixer->setParameter( |
| 4385 | name, |
| 4386 | AudioMixer::TRACK, |
| 4387 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4388 | // limit track sample rate to 2 x output sample rate, which changes at re-configuration |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 4389 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4390 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4391 | if (reqSampleRate == 0) { |
| 4392 | reqSampleRate = mSampleRate; |
| 4393 | } else if (reqSampleRate > maxSampleRate) { |
| 4394 | reqSampleRate = maxSampleRate; |
| 4395 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4396 | mAudioMixer->setParameter( |
| 4397 | name, |
| 4398 | AudioMixer::RESAMPLE, |
| 4399 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4400 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4401 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4402 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4403 | mAudioMixer->setParameter( |
| 4404 | name, |
| 4405 | AudioMixer::TIMESTRETCH, |
| 4406 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4407 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4408 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4409 | /* |
| 4410 | * Select the appropriate output buffer for the track. |
| 4411 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4412 | * Tracks with effects go into their own effects chain buffer |
| 4413 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4414 | * |
| 4415 | * Other tracks can use mMixerBuffer for higher precision |
| 4416 | * channel accumulation. If this buffer is enabled |
| 4417 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4418 | * into it. |
| 4419 | * |
| 4420 | */ |
| 4421 | if (mMixerBufferEnabled |
| 4422 | && (track->mainBuffer() == mSinkBuffer |
| 4423 | || track->mainBuffer() == mMixerBuffer)) { |
| 4424 | mAudioMixer->setParameter( |
| 4425 | name, |
| 4426 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4427 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4428 | mAudioMixer->setParameter( |
| 4429 | name, |
| 4430 | AudioMixer::TRACK, |
| 4431 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4432 | // TODO: override track->mainBuffer()? |
| 4433 | mMixerBufferValid = true; |
| 4434 | } else { |
| 4435 | mAudioMixer->setParameter( |
| 4436 | name, |
| 4437 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4438 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4439 | mAudioMixer->setParameter( |
| 4440 | name, |
| 4441 | AudioMixer::TRACK, |
| 4442 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4443 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4444 | mAudioMixer->setParameter( |
| 4445 | name, |
| 4446 | AudioMixer::TRACK, |
| 4447 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4448 | |
| 4449 | // reset retry count |
| 4450 | track->mRetryCount = kMaxTrackRetries; |
| 4451 | |
| 4452 | // If one track is ready, set the mixer ready if: |
| 4453 | // - the mixer was not ready during previous round OR |
| 4454 | // - no other track is not ready |
| 4455 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4456 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4457 | mixerStatus = MIXER_TRACKS_READY; |
| 4458 | } |
| 4459 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4460 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4461 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4462 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4463 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4464 | } else { |
| 4465 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4466 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4467 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4468 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4469 | // previous audio buffer again to effects |
| 4470 | chain = getEffectChain_l(track->sessionId()); |
| 4471 | if (chain != 0) { |
| 4472 | chain->clearInputBuffer(); |
| 4473 | } |
| 4474 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4475 | ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4476 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4477 | track->isStopped() || track->isPaused()) { |
| 4478 | // We have consumed all the buffers of this track. |
| 4479 | // Remove it from the list of active tracks. |
| 4480 | // TODO: use actual buffer filling status instead of latency when available from |
| 4481 | // audio HAL |
| 4482 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4483 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4484 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4485 | if (track->isStopped()) { |
| 4486 | track->reset(); |
| 4487 | } |
| 4488 | tracksToRemove->add(track); |
| 4489 | } |
| 4490 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4491 | // No buffers for this track. Give it a few chances to |
| 4492 | // fill a buffer, then remove it from active list. |
| 4493 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4494 | ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4495 | tracksToRemove->add(track); |
| 4496 | // indicate to client process that the track was disabled because of underrun; |
| 4497 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4498 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4499 | // If one track is not ready, mark the mixer also not ready if: |
| 4500 | // - the mixer was ready during previous round OR |
| 4501 | // - no other track is ready |
| 4502 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4503 | mixerStatus != MIXER_TRACKS_READY) { |
| 4504 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4505 | } |
| 4506 | } |
| 4507 | mAudioMixer->disable(name); |
| 4508 | } |
| 4509 | |
| 4510 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4511 | |
| 4512 | } |
| 4513 | |
| 4514 | // Push the new FastMixer state if necessary |
| 4515 | bool pauseAudioWatchdog = false; |
| 4516 | if (didModify) { |
| 4517 | state->mFastTracksGen++; |
| 4518 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4519 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4520 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4521 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4522 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4523 | state->mColdGen++; |
| 4524 | mFastMixerFutex = 0; |
| 4525 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4526 | mNormalSink = mOutputSink; |
| 4527 | } |
| 4528 | // If we go into cold idle, need to wait for acknowledgement |
| 4529 | // so that fast mixer stops doing I/O. |
| 4530 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4531 | pauseAudioWatchdog = true; |
| 4532 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4533 | } |
| 4534 | if (sq != NULL) { |
| 4535 | sq->end(didModify); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4536 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread |
| 4537 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE |
| 4538 | // when bringing the output sink into standby.) |
| 4539 | // |
| 4540 | // We will get the latest FastMixer state when we come out of COLD_IDLE. |
| 4541 | // |
| 4542 | // This occurs with BT suspend when we idle the FastMixer with |
| 4543 | // active tracks, which may be added or removed. |
| 4544 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4545 | } |
| 4546 | #ifdef AUDIO_WATCHDOG |
| 4547 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4548 | mAudioWatchdog->pause(); |
| 4549 | } |
| 4550 | #endif |
| 4551 | |
| 4552 | // Now perform the deferred reset on fast tracks that have stopped |
| 4553 | while (resetMask != 0) { |
| 4554 | size_t i = __builtin_ctz(resetMask); |
| 4555 | ALOG_ASSERT(i < count); |
| 4556 | resetMask &= ~(1 << i); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4557 | sp<Track> track = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4558 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4559 | track->reset(); |
| 4560 | } |
| 4561 | |
| 4562 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4563 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4564 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4565 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4566 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4570 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4571 | // passing a non-clean buffer to the effect chain |
| 4572 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4573 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4574 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4575 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4576 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4577 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4578 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4579 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4580 | if (mMixerBufferValid) { |
| 4581 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4582 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4583 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4584 | // after mixing. |
| 4585 | // |
| 4586 | // To enforce this guarantee: |
| 4587 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4588 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4589 | // must imply MIXER_TRACKS_READY. |
| 4590 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4591 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4592 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4593 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | // if any fast tracks, then status is ready |
| 4597 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4598 | if (fastTracks > 0) { |
| 4599 | mixerStatus = MIXER_TRACKS_READY; |
| 4600 | } |
| 4601 | return mixerStatus; |
| 4602 | } |
| 4603 | |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4604 | // trackCountForUid_l() must be called with ThreadBase::mLock held |
| 4605 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) |
| 4606 | { |
| 4607 | uint32_t trackCount = 0; |
| 4608 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 4609 | if (mTracks[i]->uid() == uid) { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4610 | trackCount++; |
| 4611 | } |
| 4612 | } |
| 4613 | return trackCount; |
| 4614 | } |
| 4615 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4616 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4617 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4618 | audio_format_t format, audio_session_t sessionId, uid_t uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4619 | { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4620 | if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { |
| 4621 | return -1; |
| 4622 | } |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4623 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 4627 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 4628 | { |
| 4629 | ALOGV("remove track (%d) and delete from mixer", name); |
| 4630 | mAudioMixer->deleteTrackName(name); |
| 4631 | } |
| 4632 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4633 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4634 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4635 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4636 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4637 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4638 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4639 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4640 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4641 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4642 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4643 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4644 | AudioParameter param = AudioParameter(keyValuePair); |
| 4645 | int value; |
| 4646 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4647 | reconfig = true; |
| 4648 | } |
| 4649 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4650 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4651 | status = BAD_VALUE; |
| 4652 | } else { |
| 4653 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4654 | reconfig = true; |
| 4655 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4656 | } |
| 4657 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4658 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4659 | status = BAD_VALUE; |
| 4660 | } else { |
| 4661 | // no need to save value, since it's constant |
| 4662 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4663 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4664 | } |
| 4665 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4666 | // do not accept frame count changes if tracks are open as the track buffer |
| 4667 | // size depends on frame count and correct behavior would not be guaranteed |
| 4668 | // if frame count is changed after track creation |
| 4669 | if (!mTracks.isEmpty()) { |
| 4670 | status = INVALID_OPERATION; |
| 4671 | } else { |
| 4672 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4673 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4674 | } |
| 4675 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4676 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4677 | // when changing the audio output device, call addBatteryData to notify |
| 4678 | // the change |
| 4679 | if (mOutDevice != value) { |
| 4680 | uint32_t params = 0; |
| 4681 | // check whether speaker is on |
| 4682 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4683 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4684 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4685 | |
| 4686 | audio_devices_t deviceWithoutSpeaker |
| 4687 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4688 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4689 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4690 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4691 | } |
| 4692 | |
| 4693 | if (params != 0) { |
| 4694 | addBatteryData(params); |
| 4695 | } |
| 4696 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4697 | #endif |
| 4698 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4699 | // forward device change to effects that have requested to be |
| 4700 | // aware of attached audio device. |
| 4701 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4702 | a2dpDeviceChanged = |
| 4703 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4704 | mOutDevice = value; |
| 4705 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4706 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4707 | } |
| 4708 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4709 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4710 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4711 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4712 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4713 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4714 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4715 | mStandby = true; |
| 4716 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4717 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4718 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4719 | if (status == NO_ERROR && reconfig) { |
| 4720 | readOutputParameters_l(); |
| 4721 | delete mAudioMixer; |
| 4722 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4723 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4724 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4725 | mTracks[i]->mFormat, mTracks[i]->mSessionId, mTracks[i]->uid()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4726 | if (name < 0) { |
| 4727 | break; |
| 4728 | } |
| 4729 | mTracks[i]->mName = name; |
| 4730 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4731 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4732 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4733 | } |
| 4734 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4735 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | |
| 4739 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4740 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4741 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4742 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4743 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4744 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4745 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4746 | if (hasFastMixer()) { |
| 4747 | dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); |
| 4748 | |
| 4749 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 4750 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 4751 | // This is a large object so we place it on the heap. |
| 4752 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 4753 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 4754 | copy->dump(fd); |
| 4755 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4756 | |
| 4757 | #ifdef STATE_QUEUE_DUMP |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4758 | // Similar for state queue |
| 4759 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4760 | observerCopy.dump(fd); |
| 4761 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4762 | mutatorCopy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4763 | #endif |
| 4764 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4765 | #ifdef AUDIO_WATCHDOG |
| 4766 | if (mAudioWatchdog != 0) { |
| 4767 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4768 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4769 | wdCopy.dump(fd); |
| 4770 | } |
| 4771 | #endif |
| 4772 | |
| 4773 | } else { |
| 4774 | dprintf(fd, " No FastMixer\n"); |
| 4775 | } |
| 4776 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4777 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4778 | // Write the tee output to a .wav file |
Glenn Kasten | 5b2191a | 2016-08-19 11:44:47 -0700 | [diff] [blame] | 4779 | dumpTee(fd, mTeeSource, mId, 'M'); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4780 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4781 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4785 | { |
| 4786 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4787 | } |
| 4788 | |
| 4789 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4790 | { |
| 4791 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4792 | } |
| 4793 | |
| 4794 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4795 | { |
| 4796 | PlaybackThread::cacheParameters_l(); |
| 4797 | |
| 4798 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4799 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4800 | // increase threshold again due to low power audio mode. The way this warning |
| 4801 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4802 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4803 | } |
| 4804 | |
| 4805 | // ---------------------------------------------------------------------------- |
| 4806 | |
| 4807 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4808 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 4809 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4810 | // mLeftVolFloat, mRightVolFloat |
| 4811 | { |
| 4812 | } |
| 4813 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4814 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4815 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4816 | ThreadBase::type_t type, bool systemReady) |
| 4817 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4818 | // mLeftVolFloat, mRightVolFloat |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4819 | , mVolumeShaperActive(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4820 | { |
| 4821 | } |
| 4822 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4823 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4824 | { |
| 4825 | } |
| 4826 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4827 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4828 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4829 | float left, right; |
| 4830 | |
| 4831 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4832 | left = right = 0; |
| 4833 | } else { |
| 4834 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4835 | float v = mMasterVolume * typeVolume; |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4836 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4837 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4838 | // Get volumeshaper scaling |
| 4839 | std::pair<float /* volume */, bool /* active */> |
| 4840 | vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4841 | track->mAudioTrackServerProxy->framesReleased()); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4842 | v *= vh.first; |
| 4843 | mVolumeShaperActive = vh.second; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4844 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4845 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4846 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4847 | if (left > GAIN_FLOAT_UNITY) { |
| 4848 | left = GAIN_FLOAT_UNITY; |
| 4849 | } |
| 4850 | left *= v; |
| 4851 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4852 | if (right > GAIN_FLOAT_UNITY) { |
| 4853 | right = GAIN_FLOAT_UNITY; |
| 4854 | } |
| 4855 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | if (lastTrack) { |
| 4859 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4860 | mLeftVolFloat = left; |
| 4861 | mRightVolFloat = right; |
| 4862 | |
| 4863 | // Convert volumes from float to 8.24 |
| 4864 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4865 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4866 | |
| 4867 | // Delegate volume control to effect in track effect chain if needed |
| 4868 | // only one effect chain can be present on DirectOutputThread, so if |
| 4869 | // there is one, the track is connected to it |
| 4870 | if (!mEffectChains.isEmpty()) { |
| 4871 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4872 | left = (float)vl / (1 << 24); |
| 4873 | right = (float)vr / (1 << 24); |
| 4874 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4875 | status_t result = mOutput->stream->setVolume(left, right); |
| 4876 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | } |
| 4880 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4881 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 4882 | { |
| 4883 | sp<Track> previousTrack = mPreviousTrack.promote(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4884 | sp<Track> latestTrack = mActiveTracks.getLatest(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4885 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4886 | if (previousTrack != 0 && latestTrack != 0) { |
| 4887 | if (mType == DIRECT) { |
| 4888 | if (previousTrack.get() != latestTrack.get()) { |
| 4889 | mFlushPending = true; |
| 4890 | } |
| 4891 | } else /* mType == OFFLOAD */ { |
| 4892 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 4893 | mFlushPending = true; |
| 4894 | } |
| 4895 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4896 | } |
| 4897 | PlaybackThread::onAddNewTrack_l(); |
| 4898 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4899 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4900 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4901 | Vector< sp<Track> > *tracksToRemove |
| 4902 | ) |
| 4903 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4904 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4905 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4906 | bool doHwPause = false; |
| 4907 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4908 | |
| 4909 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4910 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4911 | if (t->isInvalid()) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4912 | ALOGW("An invalidated track shouldn't be in active list"); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4913 | tracksToRemove->add(t); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4914 | continue; |
| 4915 | } |
| 4916 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4917 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4918 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4919 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4920 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4921 | // Only consider last track started for volume and mixer state control. |
| 4922 | // In theory an older track could underrun and restart after the new one starts |
| 4923 | // but as we only care about the transition phase between two tracks on a |
| 4924 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4925 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4926 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4927 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4928 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4929 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4930 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4931 | doHwPause = true; |
| 4932 | mHwPaused = true; |
| 4933 | } |
| 4934 | tracksToRemove->add(track); |
| 4935 | } else if (track->isFlushPending()) { |
| 4936 | track->flushAck(); |
| 4937 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4938 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4939 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4940 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4941 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 4942 | if (last) { |
| 4943 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 4944 | if (mHwPaused) { |
| 4945 | doHwResume = true; |
| 4946 | mHwPaused = false; |
| 4947 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4951 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4952 | // for all its buffers to be filled before processing it. |
| 4953 | // Allow draining the buffer in case the client |
| 4954 | // app does not call stop() and relies on underrun to stop: |
| 4955 | // hence the test on (track->mRetryCount > 1). |
| 4956 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4957 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4958 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4959 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4960 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4961 | minFrames = mNormalFrameCount; |
| 4962 | } else { |
| 4963 | minFrames = 1; |
| 4964 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4965 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4966 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4967 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4968 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4969 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4970 | |
| 4971 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4972 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 4973 | if (last) { |
| 4974 | // make sure processVolume_l() will apply new volume even if 0 |
| 4975 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 4976 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4977 | if (!mHwSupportsPause) { |
| 4978 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4979 | } |
| 4980 | } |
| 4981 | |
| 4982 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4983 | processVolume_l(track, last); |
| 4984 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4985 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4986 | if (previousTrack != 0) { |
| 4987 | if (track != previousTrack.get()) { |
| 4988 | // Flush any data still being written from last track |
| 4989 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4990 | // Invalidate previous track to force a seek when resuming. |
| 4991 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4992 | } |
| 4993 | } |
| 4994 | mPreviousTrack = track; |
| 4995 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4996 | // reset retry count |
| 4997 | track->mRetryCount = kMaxTrackRetriesDirect; |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4998 | mActiveTrack = t; |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4999 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5000 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5001 | doHwResume = true; |
| 5002 | mHwPaused = false; |
| 5003 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5004 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5005 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5006 | // clear effect chain input buffer if the last active track started underruns |
| 5007 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5008 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5009 | mEffectChains[0]->clearInputBuffer(); |
| 5010 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5011 | if (track->isStopping_1()) { |
| 5012 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5013 | if (last && mHwPaused) { |
| 5014 | doHwResume = true; |
| 5015 | mHwPaused = false; |
| 5016 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5017 | } |
| 5018 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 5019 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5020 | // We have consumed all the buffers of this track. |
| 5021 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5022 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5023 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5024 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 5025 | } else { |
| 5026 | audioHALFrames = 0; |
| 5027 | } |
| 5028 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5029 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5030 | if (mStandby || !last || |
| 5031 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5032 | if (track->isStopping_2()) { |
| 5033 | track->mState = TrackBase::STOPPED; |
| 5034 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5035 | if (track->isStopped()) { |
| 5036 | track->reset(); |
| 5037 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5038 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5039 | } |
| 5040 | } else { |
| 5041 | // No buffers for this track. Give it a few chances to |
| 5042 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5043 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5044 | if (--(track->mRetryCount) <= 0) { |
| 5045 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5046 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5047 | // indicate to client process that the track was disabled because of underrun; |
| 5048 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5049 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5050 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5051 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 5052 | "minFrames = %u, mFormat = %#x", |
| 5053 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5054 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5055 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5056 | doHwPause = true; |
| 5057 | mHwPaused = true; |
| 5058 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5059 | } |
| 5060 | } |
| 5061 | } |
| 5062 | } |
| 5063 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5064 | // if an active track did not command a flush, check for pending flush on stopped tracks |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5065 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5066 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5067 | if (mTracks[i]->isFlushPending()) { |
| 5068 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5069 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5070 | } |
| 5071 | } |
| 5072 | } |
| 5073 | |
| 5074 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5075 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5076 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5077 | // if resume is received before pause is executed. |
| 5078 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5079 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5080 | status_t result = mOutput->stream->pause(); |
| 5081 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5082 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5083 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5084 | flushHw_l(); |
| 5085 | } |
| 5086 | if (mHwSupportsPause && !mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5087 | status_t result = mOutput->stream->resume(); |
| 5088 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5089 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5090 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5091 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5092 | |
| 5093 | return mixerStatus; |
| 5094 | } |
| 5095 | |
| 5096 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 5097 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5098 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5099 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5100 | // output audio to hardware |
| 5101 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5102 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5103 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5104 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 5105 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5106 | // no need to pad with 0 for compressed audio |
| 5107 | if (audio_has_proportional_frames(mFormat)) { |
| 5108 | memset(curBuf, 0, frameCount * mFrameSize); |
| 5109 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5110 | break; |
| 5111 | } |
| 5112 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 5113 | frameCount -= buffer.frameCount; |
| 5114 | curBuf += buffer.frameCount * mFrameSize; |
| 5115 | mActiveTrack->releaseBuffer(&buffer); |
| 5116 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5117 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5118 | mSleepTimeUs = 0; |
| 5119 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5120 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5121 | } |
| 5122 | |
| 5123 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 5124 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5125 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5126 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5127 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5128 | return; |
| 5129 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5130 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5131 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5132 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5133 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5134 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5135 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5136 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5137 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5138 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5139 | } |
| 5140 | } |
| 5141 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5142 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 5143 | { |
| 5144 | { |
| 5145 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5146 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5147 | if (mTracks[i]->isFlushPending()) { |
| 5148 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5149 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5150 | } |
| 5151 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5152 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5153 | flushHw_l(); |
| 5154 | } |
| 5155 | } |
| 5156 | PlaybackThread::threadLoop_exit(); |
| 5157 | } |
| 5158 | |
| 5159 | // must be called with thread mutex locked |
| 5160 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 5161 | { |
| 5162 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5163 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5164 | |
vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5165 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { |
| 5166 | return !mStandby; |
| 5167 | } |
| 5168 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5169 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 5170 | // after a timeout and we will enter standby then. |
| 5171 | if (mTracks.size() > 0) { |
| 5172 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5173 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 5174 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5175 | } |
| 5176 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5177 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5178 | } |
| 5179 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5180 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5181 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5182 | audio_format_t format __unused, audio_session_t sessionId __unused, uid_t uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5183 | { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5184 | if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { |
| 5185 | return -1; |
| 5186 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5191 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5192 | { |
| 5193 | } |
| 5194 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5195 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 5196 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5197 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5198 | { |
| 5199 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5200 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5201 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5202 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5203 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5204 | AudioParameter param = AudioParameter(keyValuePair); |
| 5205 | int value; |
| 5206 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5207 | // forward device change to effects that have requested to be |
| 5208 | // aware of attached audio device. |
| 5209 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5210 | a2dpDeviceChanged = |
| 5211 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5212 | mOutDevice = value; |
| 5213 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5214 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5215 | } |
| 5216 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5217 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5218 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5219 | // do not accept frame count changes if tracks are open as the track buffer |
| 5220 | // size depends on frame count and correct behavior would not be garantied |
| 5221 | // if frame count is changed after track creation |
| 5222 | if (!mTracks.isEmpty()) { |
| 5223 | status = INVALID_OPERATION; |
| 5224 | } else { |
| 5225 | reconfig = true; |
| 5226 | } |
| 5227 | } |
| 5228 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5229 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5230 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5231 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5232 | mStandby = true; |
| 5233 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5234 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5235 | } |
| 5236 | if (status == NO_ERROR && reconfig) { |
| 5237 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5238 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5242 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5246 | { |
| 5247 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5248 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5249 | time = PlaybackThread::activeSleepTimeUs(); |
| 5250 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5251 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5252 | } |
| 5253 | return time; |
| 5254 | } |
| 5255 | |
| 5256 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5257 | { |
| 5258 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5259 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5260 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5261 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5262 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | } |
| 5264 | return time; |
| 5265 | } |
| 5266 | |
| 5267 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5268 | { |
| 5269 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5270 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5271 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5272 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5273 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5274 | } |
| 5275 | return time; |
| 5276 | } |
| 5277 | |
| 5278 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5279 | { |
| 5280 | PlaybackThread::cacheParameters_l(); |
| 5281 | |
| 5282 | // use shorter standby delay as on normal output to release |
| 5283 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5284 | // no delay on outputs with HW A/V sync |
| 5285 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5286 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5287 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5288 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5289 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5290 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5291 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5292 | } |
| 5293 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5294 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5295 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5296 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5297 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5298 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5299 | } |
| 5300 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5301 | int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const { |
| 5302 | // If a VolumeShaper is active, we must wake up periodically to update volume. |
| 5303 | const int64_t NS_PER_MS = 1000000; |
| 5304 | return mVolumeShaperActive ? |
| 5305 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); |
| 5306 | } |
| 5307 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5308 | // ---------------------------------------------------------------------------- |
| 5309 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5310 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5311 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5312 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5313 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5314 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5315 | mDrainSequence(0), |
| 5316 | mAsyncError(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5317 | { |
| 5318 | } |
| 5319 | |
| 5320 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5321 | { |
| 5322 | } |
| 5323 | |
| 5324 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5325 | { |
| 5326 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5327 | } |
| 5328 | |
| 5329 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5330 | { |
| 5331 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5332 | uint32_t writeAckSequence; |
| 5333 | uint32_t drainSequence; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5334 | bool asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5335 | |
| 5336 | { |
| 5337 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5338 | while (!((mWriteAckSequence & 1) || |
| 5339 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5340 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5341 | exitPending())) { |
| 5342 | mWaitWorkCV.wait(mLock); |
| 5343 | } |
| 5344 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5345 | if (exitPending()) { |
| 5346 | break; |
| 5347 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5348 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5349 | mWriteAckSequence, mDrainSequence); |
| 5350 | writeAckSequence = mWriteAckSequence; |
| 5351 | mWriteAckSequence &= ~1; |
| 5352 | drainSequence = mDrainSequence; |
| 5353 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5354 | asyncError = mAsyncError; |
| 5355 | mAsyncError = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5356 | } |
| 5357 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5358 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5359 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5360 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5361 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5362 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5363 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5364 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5365 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5366 | if (asyncError) { |
| 5367 | playbackThread->onAsyncError(); |
| 5368 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5369 | } |
| 5370 | } |
| 5371 | } |
| 5372 | return false; |
| 5373 | } |
| 5374 | |
| 5375 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5376 | { |
| 5377 | ALOGV("AsyncCallbackThread::exit"); |
| 5378 | Mutex::Autolock _l(mLock); |
| 5379 | requestExit(); |
| 5380 | mWaitWorkCV.broadcast(); |
| 5381 | } |
| 5382 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5383 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5384 | { |
| 5385 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5386 | // bit 0 is cleared |
| 5387 | mWriteAckSequence = sequence << 1; |
| 5388 | } |
| 5389 | |
| 5390 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5391 | { |
| 5392 | Mutex::Autolock _l(mLock); |
| 5393 | // ignore unexpected callbacks |
| 5394 | if (mWriteAckSequence & 2) { |
| 5395 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5396 | mWaitWorkCV.signal(); |
| 5397 | } |
| 5398 | } |
| 5399 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5400 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5401 | { |
| 5402 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5403 | // bit 0 is cleared |
| 5404 | mDrainSequence = sequence << 1; |
| 5405 | } |
| 5406 | |
| 5407 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5408 | { |
| 5409 | Mutex::Autolock _l(mLock); |
| 5410 | // ignore unexpected callbacks |
| 5411 | if (mDrainSequence & 2) { |
| 5412 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5413 | mWaitWorkCV.signal(); |
| 5414 | } |
| 5415 | } |
| 5416 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5417 | void AudioFlinger::AsyncCallbackThread::setAsyncError() |
| 5418 | { |
| 5419 | Mutex::Autolock _l(mLock); |
| 5420 | mAsyncError = true; |
| 5421 | mWaitWorkCV.signal(); |
| 5422 | } |
| 5423 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5424 | |
| 5425 | // ---------------------------------------------------------------------------- |
| 5426 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5427 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5428 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5429 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), |
| 5430 | mOffloadUnderrunPosition(~0LL) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5431 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5432 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 5433 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5434 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5435 | } |
| 5436 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5437 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5438 | { |
| 5439 | if (mFlushPending || mHwPaused) { |
| 5440 | // If a flush is pending or track was paused, just discard buffered data |
| 5441 | flushHw_l(); |
| 5442 | } else { |
| 5443 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5444 | threadLoop_drain(); |
| 5445 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5446 | if (mUseAsyncWrite) { |
| 5447 | ALOG_ASSERT(mCallbackThread != 0); |
| 5448 | mCallbackThread->exit(); |
| 5449 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5450 | PlaybackThread::threadLoop_exit(); |
| 5451 | } |
| 5452 | |
| 5453 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5454 | Vector< sp<Track> > *tracksToRemove |
| 5455 | ) |
| 5456 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5457 | size_t count = mActiveTracks.size(); |
| 5458 | |
| 5459 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5460 | bool doHwPause = false; |
| 5461 | bool doHwResume = false; |
| 5462 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5463 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5464 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5465 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5466 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5467 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5468 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5469 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5470 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5471 | // Only consider last track started for volume and mixer state control. |
| 5472 | // In theory an older track could underrun and restart after the new one starts |
| 5473 | // but as we only care about the transition phase between two tracks on a |
| 5474 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5475 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5476 | bool last = l.get() == track; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5477 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5478 | if (track->isInvalid()) { |
| 5479 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5480 | tracksToRemove->add(track); |
| 5481 | continue; |
| 5482 | } |
| 5483 | |
| 5484 | if (track->mState == TrackBase::IDLE) { |
| 5485 | ALOGW("An idle track shouldn't be in active list"); |
| 5486 | continue; |
| 5487 | } |
| 5488 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5489 | if (track->isPausing()) { |
| 5490 | track->setPaused(); |
| 5491 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5492 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5493 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5494 | mHwPaused = true; |
| 5495 | } |
| 5496 | // If we were part way through writing the mixbuffer to |
| 5497 | // the HAL we must save this until we resume |
| 5498 | // BUG - this will be wrong if a different track is made active, |
| 5499 | // in that case we want to discard the pending data in the |
| 5500 | // mixbuffer and tell the client to present it again when the |
| 5501 | // track is resumed |
| 5502 | mPausedWriteLength = mCurrentWriteLength; |
| 5503 | mPausedBytesRemaining = mBytesRemaining; |
| 5504 | mBytesRemaining = 0; // stop writing |
| 5505 | } |
| 5506 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5507 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5508 | if (track->isStopping_1()) { |
| 5509 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5510 | } else { |
| 5511 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5512 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5513 | track->flushAck(); |
| 5514 | if (last) { |
| 5515 | mFlushPending = true; |
| 5516 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5517 | } else if (track->isResumePending()){ |
| 5518 | track->resumeAck(); |
| 5519 | if (last) { |
| 5520 | if (mPausedBytesRemaining) { |
| 5521 | // Need to continue write that was interrupted |
| 5522 | mCurrentWriteLength = mPausedWriteLength; |
| 5523 | mBytesRemaining = mPausedBytesRemaining; |
| 5524 | mPausedBytesRemaining = 0; |
| 5525 | } |
| 5526 | if (mHwPaused) { |
| 5527 | doHwResume = true; |
| 5528 | mHwPaused = false; |
| 5529 | // threadLoop_mix() will handle the case that we need to |
| 5530 | // resume an interrupted write |
| 5531 | } |
| 5532 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5533 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5534 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5535 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5536 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5537 | // Do not handle new data in this iteration even if track->framesReady() |
| 5538 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5539 | } |
| 5540 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5541 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5542 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5543 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5544 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5545 | if (last) { |
| 5546 | // make sure processVolume_l() will apply new volume even if 0 |
| 5547 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5548 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5552 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5553 | if (previousTrack != 0) { |
| 5554 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5555 | // Flush any data still being written from last track |
| 5556 | mBytesRemaining = 0; |
| 5557 | if (mPausedBytesRemaining) { |
| 5558 | // Last track was paused so we also need to flush saved |
| 5559 | // mixbuffer state and invalidate track so that it will |
| 5560 | // re-submit that unwritten data when it is next resumed |
| 5561 | mPausedBytesRemaining = 0; |
| 5562 | // Invalidate is a bit drastic - would be more efficient |
| 5563 | // to have a flag to tell client that some of the |
| 5564 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5565 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5566 | } |
| 5567 | // flush data already sent to the DSP if changing audio session as audio |
| 5568 | // comes from a different source. Also invalidate previous track to force a |
| 5569 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5570 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5571 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | } |
| 5575 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5576 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5577 | if (track->isStopping_1()) { |
| 5578 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5579 | } else { |
| 5580 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5581 | } |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5582 | mActiveTrack = t; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5583 | mixerStatus = MIXER_TRACKS_READY; |
| 5584 | } |
| 5585 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5586 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5587 | if (track->isStopping_1()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5588 | if (--(track->mRetryCount) <= 0) { |
| 5589 | // Hardware buffer can hold a large amount of audio so we must |
| 5590 | // wait for all current track's data to drain before we say |
| 5591 | // that the track is stopped. |
| 5592 | if (mBytesRemaining == 0) { |
| 5593 | // Only start draining when all data in mixbuffer |
| 5594 | // has been written |
| 5595 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5596 | track->mState = TrackBase::STOPPING_2; // so presentation completes after |
| 5597 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 5598 | if (last && !mStandby) { |
| 5599 | // do not modify drain sequence if we are already draining. This happens |
| 5600 | // when resuming from pause after drain. |
| 5601 | if ((mDrainSequence & 1) == 0) { |
| 5602 | mSleepTimeUs = 0; |
| 5603 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 5604 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5605 | mDrainSequence += 2; |
| 5606 | } |
| 5607 | if (mHwPaused) { |
| 5608 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5609 | // a resume so we must ensure hardware is running |
| 5610 | doHwResume = true; |
| 5611 | mHwPaused = false; |
| 5612 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5613 | } |
| 5614 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5615 | } else if (last) { |
| 5616 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); |
| 5617 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5618 | } |
| 5619 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5620 | // Drain has completed or we are in standby, signal presentation complete |
| 5621 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5622 | track->mState = TrackBase::STOPPED; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5623 | uint32_t latency = 0; |
| 5624 | status_t result = mOutput->stream->getLatency(&latency); |
| 5625 | ALOGE_IF(result != OK, |
| 5626 | "Error when retrieving output stream latency: %d", result); |
| 5627 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5628 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5629 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5630 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5631 | track->reset(); |
| 5632 | tracksToRemove->add(track); |
| 5633 | } |
| 5634 | } else { |
| 5635 | // No buffers for this track. Give it a few chances to |
| 5636 | // fill a buffer, then remove it from active list. |
| 5637 | if (--(track->mRetryCount) <= 0) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5638 | bool running = false; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5639 | uint64_t position = 0; |
| 5640 | struct timespec unused; |
| 5641 | // The running check restarts the retry counter at least once. |
| 5642 | status_t ret = mOutput->stream->getPresentationPosition(&position, &unused); |
| 5643 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { |
| 5644 | running = true; |
| 5645 | mOffloadUnderrunPosition = position; |
| 5646 | } |
| 5647 | if (ret == NO_ERROR) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5648 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, |
| 5649 | (long long)position, (long long)mOffloadUnderrunPosition); |
| 5650 | } |
| 5651 | if (running) { // still running, give us more time. |
| 5652 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5653 | } else { |
| 5654 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5655 | track->name()); |
| 5656 | tracksToRemove->add(track); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 5657 | // tell client process that the track was disabled because of underrun; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5658 | // it will then automatically call start() when data is available |
| 5659 | track->disable(); |
| 5660 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5661 | } else if (last){ |
| 5662 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5663 | } |
| 5664 | } |
| 5665 | } |
| 5666 | // compute volume for this track |
| 5667 | processVolume_l(track, last); |
| 5668 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5669 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5670 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5671 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5672 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5673 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5674 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5675 | status_t result = mOutput->stream->pause(); |
| 5676 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5677 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5678 | if (mFlushPending) { |
| 5679 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5680 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5681 | if (!mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5682 | status_t result = mOutput->stream->resume(); |
| 5683 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5684 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5685 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5686 | // remove all the tracks that need to be... |
| 5687 | removeTracks_l(*tracksToRemove); |
| 5688 | |
| 5689 | return mixerStatus; |
| 5690 | } |
| 5691 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5692 | // must be called with thread mutex locked |
| 5693 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5694 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5695 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5696 | mWriteAckSequence, mDrainSequence); |
| 5697 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5698 | return true; |
| 5699 | } |
| 5700 | return false; |
| 5701 | } |
| 5702 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5703 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5704 | { |
| 5705 | Mutex::Autolock _l(mLock); |
| 5706 | return waitingAsyncCallback_l(); |
| 5707 | } |
| 5708 | |
| 5709 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5710 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5711 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5712 | // Flush anything still waiting in the mixbuffer |
| 5713 | mCurrentWriteLength = 0; |
| 5714 | mBytesRemaining = 0; |
| 5715 | mPausedWriteLength = 0; |
| 5716 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5717 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 5718 | mBytesWritten = 0; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5719 | mOffloadUnderrunPosition = ~0LL; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5720 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5721 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5722 | // discard any pending drain or write ack by incrementing sequence |
| 5723 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5724 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5725 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5726 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5727 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5728 | } |
| 5729 | } |
| 5730 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5731 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
| 5732 | { |
| 5733 | Mutex::Autolock _l(mLock); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5734 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 5735 | mFlushPending = true; |
| 5736 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5737 | } |
| 5738 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5739 | // ---------------------------------------------------------------------------- |
| 5740 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5741 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5742 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5743 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5744 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5745 | mWaitTimeMs(UINT_MAX) |
| 5746 | { |
| 5747 | addOutputTrack(mainThread); |
| 5748 | } |
| 5749 | |
| 5750 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5751 | { |
| 5752 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5753 | mOutputTracks[i]->destroy(); |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5758 | { |
| 5759 | // mix buffers... |
| 5760 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5761 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5762 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5763 | if (mMixerBufferValid) { |
| 5764 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5765 | } else { |
| 5766 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5767 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5768 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5769 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5770 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5771 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5772 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5776 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5777 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5778 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5779 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5780 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5781 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5782 | } |
| 5783 | } else if (mBytesWritten != 0) { |
| 5784 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5785 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5786 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5787 | } else { |
| 5788 | // flush remaining overflow buffers in output tracks |
| 5789 | writeFrames = 0; |
| 5790 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5791 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5792 | } |
| 5793 | } |
| 5794 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5795 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5796 | { |
| 5797 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5798 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5799 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5800 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5801 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5805 | { |
| 5806 | // DuplicatingThread implements standby by stopping all tracks |
| 5807 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5808 | outputTracks[i]->stop(); |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5813 | { |
| 5814 | outputTracks = mOutputTracks; |
| 5815 | } |
| 5816 | |
| 5817 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5818 | { |
| 5819 | outputTracks.clear(); |
| 5820 | } |
| 5821 | |
| 5822 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5823 | { |
| 5824 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5825 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5826 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5827 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5828 | const size_t frameCount = |
| 5829 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5830 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5831 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5832 | // nearly empty and the other may be dropping data). |
| 5833 | |
| 5834 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5835 | this, |
| 5836 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5837 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5838 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5839 | frameCount, |
| 5840 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5841 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 5842 | if (status != NO_ERROR) { |
| 5843 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 5844 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5845 | } |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5846 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
| 5847 | mOutputTracks.add(outputTrack); |
| 5848 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 5849 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5853 | { |
| 5854 | Mutex::Autolock _l(mLock); |
| 5855 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5856 | if (mOutputTracks[i]->thread() == thread) { |
| 5857 | mOutputTracks[i]->destroy(); |
| 5858 | mOutputTracks.removeAt(i); |
| 5859 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5860 | if (thread->getOutput() == mOutput) { |
| 5861 | mOutput = NULL; |
| 5862 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5863 | return; |
| 5864 | } |
| 5865 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5866 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | // caller must hold mLock |
| 5870 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5871 | { |
| 5872 | mWaitTimeMs = UINT_MAX; |
| 5873 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5874 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5875 | if (strong != 0) { |
| 5876 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5877 | if (waitTimeMs < mWaitTimeMs) { |
| 5878 | mWaitTimeMs = waitTimeMs; |
| 5879 | } |
| 5880 | } |
| 5881 | } |
| 5882 | } |
| 5883 | |
| 5884 | |
| 5885 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5886 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5887 | { |
| 5888 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5889 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5890 | if (thread == 0) { |
| 5891 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5892 | outputTracks[i].get()); |
| 5893 | return false; |
| 5894 | } |
| 5895 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5896 | // see note at standby() declaration |
| 5897 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5898 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5899 | thread.get()); |
| 5900 | return false; |
| 5901 | } |
| 5902 | } |
| 5903 | return true; |
| 5904 | } |
| 5905 | |
| 5906 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5907 | { |
| 5908 | return (mWaitTimeMs * 1000) / 2; |
| 5909 | } |
| 5910 | |
| 5911 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5912 | { |
| 5913 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5914 | updateWaitTime_l(); |
| 5915 | |
| 5916 | MixerThread::cacheParameters_l(); |
| 5917 | } |
| 5918 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 5919 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5920 | // ---------------------------------------------------------------------------- |
| 5921 | // Record |
| 5922 | // ---------------------------------------------------------------------------- |
| 5923 | |
| 5924 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5925 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5926 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5927 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5928 | audio_devices_t inDevice, |
| 5929 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5930 | #ifdef TEE_SINK |
| 5931 | , const sp<NBAIO_Sink>& teeSink |
| 5932 | #endif |
| 5933 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5934 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 5935 | mInput(input), |
| 5936 | mActiveTracks(&this->mLocalLog), |
| 5937 | mRsmpInBuffer(NULL), |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 5938 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5939 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5940 | #ifdef TEE_SINK |
| 5941 | , mTeeSink(teeSink) |
| 5942 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5943 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5944 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5945 | // mFastCapture below |
| 5946 | , mFastCaptureFutex(0) |
| 5947 | // mInputSource |
| 5948 | // mPipeSink |
| 5949 | // mPipeSource |
| 5950 | , mPipeFramesP2(0) |
| 5951 | // mPipeMemory |
| 5952 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5953 | , mFastTrackAvail(false) |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 5954 | , mBtNrecSuspended(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5955 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5956 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5957 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5958 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5959 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5960 | |
| 5961 | // create an NBAIO source for the HAL input stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 5962 | mInputSource = new AudioStreamInSource(input->stream); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5963 | size_t numCounterOffers = 0; |
| 5964 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5965 | #if !LOG_NDEBUG |
| 5966 | ssize_t index = |
| 5967 | #else |
| 5968 | (void) |
| 5969 | #endif |
| 5970 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5971 | ALOG_ASSERT(index == 0); |
| 5972 | |
| 5973 | // initialize fast capture depending on configuration |
| 5974 | bool initFastCapture; |
| 5975 | switch (kUseFastCapture) { |
| 5976 | case FastCapture_Never: |
| 5977 | initFastCapture = false; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 5978 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5979 | break; |
| 5980 | case FastCapture_Always: |
| 5981 | initFastCapture = true; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 5982 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5983 | break; |
| 5984 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5985 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 5986 | ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d", |
| 5987 | this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs, |
| 5988 | initFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5989 | break; |
| 5990 | // case FastCapture_Dynamic: |
| 5991 | } |
| 5992 | |
| 5993 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5994 | // create a Pipe for FastCapture to write to, and for us and fast tracks to read from |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5995 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 5996 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread |
| 5997 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5998 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 5999 | void *pipeBuffer = nullptr; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6000 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 6001 | sp<IMemory> pipeMemory; |
| 6002 | if ((roHeap == 0) || |
| 6003 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6004 | (pipeBuffer = pipeMemory->pointer()) == nullptr) { |
| 6005 | ALOGE("not enough memory for pipe buffer size=%zu; " |
| 6006 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", |
| 6007 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, |
| 6008 | (long long)kRecordThreadReadOnlyHeapSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6009 | goto failed; |
| 6010 | } |
| 6011 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 6012 | memset(pipeBuffer, 0, pipeSize); |
| 6013 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 6014 | const NBAIO_Format offers[1] = {format}; |
| 6015 | size_t numCounterOffers = 0; |
| 6016 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 6017 | ALOG_ASSERT(index == 0); |
| 6018 | mPipeSink = pipe; |
| 6019 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 6020 | numCounterOffers = 0; |
| 6021 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 6022 | ALOG_ASSERT(index == 0); |
| 6023 | mPipeSource = pipeReader; |
| 6024 | mPipeFramesP2 = pipeFramesP2; |
| 6025 | mPipeMemory = pipeMemory; |
| 6026 | |
| 6027 | // create fast capture |
| 6028 | mFastCapture = new FastCapture(); |
| 6029 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6030 | #ifdef STATE_QUEUE_DUMP |
| 6031 | // FIXME |
| 6032 | #endif |
| 6033 | FastCaptureState *state = sq->begin(); |
| 6034 | state->mCblk = NULL; |
| 6035 | state->mInputSource = mInputSource.get(); |
| 6036 | state->mInputSourceGen++; |
| 6037 | state->mPipeSink = pipe; |
| 6038 | state->mPipeSinkGen++; |
| 6039 | state->mFrameCount = mFrameCount; |
| 6040 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6041 | // already done in constructor initialization list |
| 6042 | //mFastCaptureFutex = 0; |
| 6043 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6044 | state->mColdGen++; |
| 6045 | state->mDumpState = &mFastCaptureDumpState; |
| 6046 | #ifdef TEE_SINK |
| 6047 | // FIXME |
| 6048 | #endif |
| 6049 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 6050 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 6051 | sq->end(); |
| 6052 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6053 | |
| 6054 | // start the fast capture |
| 6055 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 6056 | pid_t tid = mFastCapture->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6057 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 6058 | stream()->setHalThreadPriority(kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6059 | #ifdef AUDIO_WATCHDOG |
| 6060 | // FIXME |
| 6061 | #endif |
| 6062 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6063 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6064 | } |
| 6065 | failed: ; |
| 6066 | |
| 6067 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6068 | } |
| 6069 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6070 | AudioFlinger::RecordThread::~RecordThread() |
| 6071 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6072 | if (mFastCapture != 0) { |
| 6073 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6074 | FastCaptureState *state = sq->begin(); |
| 6075 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6076 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6077 | if (old == -1) { |
| 6078 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6079 | } |
| 6080 | } |
| 6081 | state->mCommand = FastCaptureState::EXIT; |
| 6082 | sq->end(); |
| 6083 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6084 | mFastCapture->join(); |
| 6085 | mFastCapture.clear(); |
| 6086 | } |
| 6087 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 6088 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6089 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | void AudioFlinger::RecordThread::onFirstRef() |
| 6093 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6094 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6095 | } |
| 6096 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 6097 | void AudioFlinger::RecordThread::preExit() |
| 6098 | { |
| 6099 | ALOGV(" preExit()"); |
| 6100 | Mutex::Autolock _l(mLock); |
| 6101 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6102 | sp<RecordTrack> track = mTracks[i]; |
| 6103 | track->invalidate(); |
| 6104 | } |
| 6105 | mActiveTracks.clear(); |
| 6106 | mStartStopCond.broadcast(); |
| 6107 | } |
| 6108 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6109 | bool AudioFlinger::RecordThread::threadLoop() |
| 6110 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6111 | nsecs_t lastWarning = 0; |
| 6112 | |
| 6113 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6114 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6115 | reacquire_wakelock: |
| 6116 | sp<RecordTrack> activeTrack; |
| 6117 | { |
| 6118 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6119 | acquireWakeLock_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6120 | } |
| 6121 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6122 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 6123 | uint32_t sleepUs = 0; |
| 6124 | |
| 6125 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6126 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6127 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6128 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6129 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 6130 | Vector< sp<RecordTrack> > activeTracks; |
| 6131 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6132 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6133 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6134 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6135 | // reference to a fast track which is about to be removed |
| 6136 | sp<RecordTrack> fastTrackToRemove; |
| 6137 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6138 | { // scope for mLock |
| 6139 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6140 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6141 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6142 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6143 | // check exitPending here because checkForNewParameters_l() and |
| 6144 | // checkForNewParameters_l() can temporarily release mLock |
| 6145 | if (exitPending()) { |
| 6146 | break; |
| 6147 | } |
| 6148 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6149 | // sleep with mutex unlocked |
| 6150 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 6151 | ATRACE_BEGIN("sleepC"); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6152 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); |
| 6153 | ATRACE_END(); |
| 6154 | sleepUs = 0; |
| 6155 | continue; |
| 6156 | } |
| 6157 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6158 | // if no active track(s), then standby and release wakelock |
| 6159 | size_t size = mActiveTracks.size(); |
| 6160 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6161 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6162 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6163 | releaseWakeLock_l(); |
| 6164 | ALOGV("RecordThread: loop stopping"); |
| 6165 | // go to sleep |
| 6166 | mWaitWorkCV.wait(mLock); |
| 6167 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6168 | goto reacquire_wakelock; |
| 6169 | } |
| 6170 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6171 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6172 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6173 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6174 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6175 | activeTrack = mActiveTracks[i]; |
| 6176 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6177 | if (activeTrack->isFastTrack()) { |
| 6178 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 6179 | fastTrackToRemove = activeTrack; |
| 6180 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6181 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6182 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6183 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6184 | continue; |
| 6185 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6186 | |
| 6187 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 6188 | switch (activeTrackState) { |
| 6189 | |
| 6190 | case TrackBase::PAUSING: |
| 6191 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6192 | doBroadcast = true; |
| 6193 | size--; |
| 6194 | continue; |
| 6195 | |
| 6196 | case TrackBase::STARTING_1: |
| 6197 | sleepUs = 10000; |
| 6198 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6199 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6200 | continue; |
| 6201 | |
| 6202 | case TrackBase::STARTING_2: |
| 6203 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6204 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6205 | activeTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6206 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6207 | break; |
| 6208 | |
| 6209 | case TrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6210 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6211 | break; |
| 6212 | |
| 6213 | case TrackBase::IDLE: |
| 6214 | i++; |
| 6215 | continue; |
| 6216 | |
| 6217 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6218 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6219 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6220 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6221 | activeTracks.add(activeTrack); |
| 6222 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6223 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6224 | if (activeTrack->isFastTrack()) { |
| 6225 | ALOG_ASSERT(!mFastTrackAvail); |
| 6226 | ALOG_ASSERT(fastTrack == 0); |
| 6227 | fastTrack = activeTrack; |
| 6228 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6229 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6230 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6231 | mActiveTracks.updatePowerState(this); |
| 6232 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6233 | if (allStopped) { |
| 6234 | standbyIfNotAlreadyInStandby(); |
| 6235 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6236 | if (doBroadcast) { |
| 6237 | mStartStopCond.broadcast(); |
| 6238 | } |
| 6239 | |
| 6240 | // sleep if there are no active tracks to process |
| 6241 | if (activeTracks.size() == 0) { |
| 6242 | if (sleepUs == 0) { |
| 6243 | sleepUs = kRecordThreadSleepUs; |
| 6244 | } |
| 6245 | continue; |
| 6246 | } |
| 6247 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6248 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6249 | lockEffectChains_l(effectChains); |
| 6250 | } |
| 6251 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6252 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6253 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6254 | size_t size = effectChains.size(); |
| 6255 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6256 | // thread mutex is not locked, but effect chain is locked |
| 6257 | effectChains[i]->process_l(); |
| 6258 | } |
| 6259 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6260 | // Push a new fast capture state if fast capture is not already running, or cblk change |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6261 | if (mFastCapture != 0) { |
| 6262 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6263 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6264 | bool didModify = false; |
| 6265 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6266 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 6267 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 6268 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6269 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6270 | if (old == -1) { |
| 6271 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6272 | } |
| 6273 | } |
| 6274 | state->mCommand = FastCaptureState::READ_WRITE; |
| 6275 | #if 0 // FIXME |
| 6276 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6277 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 6278 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6279 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6280 | didModify = true; |
| 6281 | } |
| 6282 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 6283 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 6284 | if (cblkNew != cblkOld) { |
| 6285 | state->mCblk = cblkNew; |
| 6286 | // block until acked if removing a fast track |
| 6287 | if (cblkOld != NULL) { |
| 6288 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 6289 | } |
| 6290 | didModify = true; |
| 6291 | } |
| 6292 | sq->end(didModify); |
| 6293 | if (didModify) { |
| 6294 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6295 | #if 0 |
| 6296 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6297 | mNormalSource = mPipeSource; |
| 6298 | } |
| 6299 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6300 | } |
| 6301 | } |
| 6302 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6303 | // now run the fast track destructor with thread mutex unlocked |
| 6304 | fastTrackToRemove.clear(); |
| 6305 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6306 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 6307 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 6308 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 6309 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 6310 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6311 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6312 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6313 | ssize_t framesRead; |
| 6314 | |
| 6315 | // If an NBAIO source is present, use it to read the normal capture's data |
| 6316 | if (mPipeSource != 0) { |
| 6317 | size_t framesToRead = mBufferSize / mFrameSize; |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6318 | framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6319 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6320 | framesToRead); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6321 | // since pipe is non-blocking, simulate blocking input by waiting for 1/2 of |
| 6322 | // buffer size or at least for 20ms. |
| 6323 | size_t sleepFrames = max( |
| 6324 | min(mPipeFramesP2, mRsmpInFramesP2) / 2, FMS_20 * mSampleRate / 1000); |
| 6325 | if (framesRead <= (ssize_t) sleepFrames) { |
| 6326 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; |
| 6327 | } |
| 6328 | if (framesRead < 0) { |
| 6329 | status_t status = (status_t) framesRead; |
| 6330 | switch (status) { |
| 6331 | case OVERRUN: |
| 6332 | ALOGW("overrun on read from pipe"); |
| 6333 | framesRead = 0; |
| 6334 | break; |
| 6335 | case NEGOTIATE: |
| 6336 | ALOGE("re-negotiation is needed"); |
| 6337 | framesRead = -1; // Will cause an attempt to recover. |
| 6338 | break; |
| 6339 | default: |
| 6340 | ALOGE("unknown error %d on read from pipe", status); |
| 6341 | break; |
| 6342 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6343 | } |
| 6344 | // otherwise use the HAL / AudioStreamIn directly |
| 6345 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6346 | ATRACE_BEGIN("read"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6347 | size_t bytesRead; |
| 6348 | status_t result = mInput->stream->read( |
| 6349 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6350 | ATRACE_END(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6351 | if (result < 0) { |
| 6352 | framesRead = result; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6353 | } else { |
| 6354 | framesRead = bytesRead / mFrameSize; |
| 6355 | } |
| 6356 | } |
| 6357 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6358 | // Update server timestamp with server stats |
| 6359 | // systemTime() is optional if the hardware supports timestamps. |
| 6360 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6361 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6362 | |
| 6363 | // Update server timestamp with kernel stats |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6364 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6365 | int64_t position, time; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6366 | int ret = mInput->stream->getCapturePosition(&position, &time); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6367 | if (ret == NO_ERROR) { |
| 6368 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6369 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6370 | // Note: In general record buffers should tend to be empty in |
| 6371 | // a properly running pipeline. |
| 6372 | // |
| 6373 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6374 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6375 | } |
| 6376 | } |
| 6377 | // Use this to track timestamp information |
| 6378 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6379 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6380 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6381 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6382 | // Force input into standby so that it tries to recover at next read attempt |
| 6383 | inputStandBy(); |
| 6384 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6385 | } |
| 6386 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6387 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6388 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6389 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6390 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6391 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6392 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6393 | } |
| 6394 | // If destination is non-contiguous, we now correct for reading past end of buffer. |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6395 | { |
| 6396 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6397 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6398 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6399 | (framesRead - part1) * mFrameSize); |
| 6400 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6401 | } |
| 6402 | rear = mRsmpInRear += framesRead; |
| 6403 | |
| 6404 | size = activeTracks.size(); |
| 6405 | // loop over each active track |
| 6406 | for (size_t i = 0; i < size; i++) { |
| 6407 | activeTrack = activeTracks[i]; |
| 6408 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6409 | // skip fast tracks, as those are handled directly by FastCapture |
| 6410 | if (activeTrack->isFastTrack()) { |
| 6411 | continue; |
| 6412 | } |
| 6413 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6414 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6415 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6416 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6417 | enum { |
| 6418 | OVERRUN_UNKNOWN, |
| 6419 | OVERRUN_TRUE, |
| 6420 | OVERRUN_FALSE |
| 6421 | } overrun = OVERRUN_UNKNOWN; |
| 6422 | |
| 6423 | // loop over getNextBuffer to handle circular sink |
| 6424 | for (;;) { |
| 6425 | |
| 6426 | activeTrack->mSink.frameCount = ~0; |
| 6427 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6428 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6429 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6430 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6431 | // check available frames and handle overrun conditions |
| 6432 | // if the record track isn't draining fast enough. |
| 6433 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6434 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6435 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6436 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6437 | overrun = OVERRUN_TRUE; |
| 6438 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6439 | if (framesOut == 0 || framesIn == 0) { |
| 6440 | break; |
| 6441 | } |
| 6442 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6443 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6444 | // from framesIn. |
| 6445 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6446 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6447 | framesOut = min(framesOut, |
| 6448 | destinationFramesPossible( |
| 6449 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6450 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6451 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6452 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6453 | |
| 6454 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6455 | overrun = OVERRUN_FALSE; |
| 6456 | } |
| 6457 | |
| 6458 | if (activeTrack->mFramesToDrop == 0) { |
| 6459 | if (framesOut > 0) { |
| 6460 | activeTrack->mSink.frameCount = framesOut; |
| 6461 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6462 | } |
| 6463 | } else { |
| 6464 | // FIXME could do a partial drop of framesOut |
| 6465 | if (activeTrack->mFramesToDrop > 0) { |
| 6466 | activeTrack->mFramesToDrop -= framesOut; |
| 6467 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6468 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6469 | } |
| 6470 | } else { |
| 6471 | activeTrack->mFramesToDrop += framesOut; |
| 6472 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6473 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6474 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6475 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6476 | activeTrack->sessionId(), |
| 6477 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6478 | activeTrack->mSyncStartEvent->triggerSession() : |
| 6479 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6480 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6486 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6487 | } |
| 6488 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6489 | |
| 6490 | switch (overrun) { |
| 6491 | case OVERRUN_TRUE: |
| 6492 | // client isn't retrieving buffers fast enough |
| 6493 | if (!activeTrack->setOverflow()) { |
| 6494 | nsecs_t now = systemTime(); |
| 6495 | // FIXME should lastWarning per track? |
| 6496 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6497 | ALOGW("RecordThread: buffer overflow"); |
| 6498 | lastWarning = now; |
| 6499 | } |
| 6500 | } |
| 6501 | break; |
| 6502 | case OVERRUN_FALSE: |
| 6503 | activeTrack->clearOverflow(); |
| 6504 | break; |
| 6505 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6506 | break; |
| 6507 | } |
| 6508 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6509 | // update frame information and push timestamp out |
| 6510 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6511 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6512 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6513 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6514 | } |
| 6515 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6516 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6517 | // enable changes in effect chain |
| 6518 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6519 | // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6520 | } |
| 6521 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6522 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6523 | |
| 6524 | { |
| 6525 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6526 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6527 | sp<RecordTrack> track = mTracks[i]; |
| 6528 | track->invalidate(); |
| 6529 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6530 | mActiveTracks.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6531 | mStartStopCond.broadcast(); |
| 6532 | } |
| 6533 | |
| 6534 | releaseWakeLock(); |
| 6535 | |
| 6536 | ALOGV("RecordThread %p exiting", this); |
| 6537 | return false; |
| 6538 | } |
| 6539 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6540 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6541 | { |
| 6542 | if (!mStandby) { |
| 6543 | inputStandBy(); |
| 6544 | mStandby = true; |
| 6545 | } |
| 6546 | } |
| 6547 | |
| 6548 | void AudioFlinger::RecordThread::inputStandBy() |
| 6549 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6550 | // Idle the fast capture if it's currently running |
| 6551 | if (mFastCapture != 0) { |
| 6552 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6553 | FastCaptureState *state = sq->begin(); |
| 6554 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6555 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6556 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6557 | state->mColdGen++; |
| 6558 | mFastCaptureFutex = 0; |
| 6559 | sq->end(); |
| 6560 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6561 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6562 | #if 0 |
| 6563 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6564 | // FIXME |
| 6565 | } |
| 6566 | #endif |
| 6567 | #ifdef AUDIO_WATCHDOG |
| 6568 | // FIXME |
| 6569 | #endif |
| 6570 | } else { |
| 6571 | sq->end(false /*didModify*/); |
| 6572 | } |
| 6573 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6574 | status_t result = mInput->stream->standby(); |
| 6575 | ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result); |
Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 6576 | |
| 6577 | // If going into standby, flush the pipe source. |
| 6578 | if (mPipeSource.get() != nullptr) { |
| 6579 | const ssize_t flushed = mPipeSource->flush(); |
| 6580 | if (flushed > 0) { |
| 6581 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); |
| 6582 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; |
| 6583 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6584 | } |
| 6585 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6586 | } |
| 6587 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6588 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6589 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6590 | const sp<AudioFlinger::Client>& client, |
| 6591 | uint32_t sampleRate, |
| 6592 | audio_format_t format, |
| 6593 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6594 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6595 | audio_session_t sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6596 | size_t *notificationFrames, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6597 | uid_t uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6598 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6599 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6600 | status_t *status, |
| 6601 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6602 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6603 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6604 | sp<RecordTrack> track; |
| 6605 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6606 | audio_input_flags_t inputFlags = mInput->flags; |
| 6607 | |
| 6608 | // special case for FAST flag considered OK if fast capture is present |
| 6609 | if (hasFastCapture()) { |
| 6610 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 6611 | } |
| 6612 | |
| 6613 | // Check if requested flags are compatible with output stream flags |
| 6614 | if ((*flags & inputFlags) != *flags) { |
| 6615 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 6616 | " input flags (%08x)", |
| 6617 | *flags, inputFlags); |
| 6618 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 6619 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6620 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6621 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6622 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6623 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6624 | // we formerly checked for a callback handler (non-0 tid), |
| 6625 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6626 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6627 | // frame count is not specified, or is exactly the pipe depth |
| 6628 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6629 | // PCM data |
| 6630 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6631 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6632 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6633 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6634 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6635 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6636 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6637 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6638 | hasFastCapture() && |
| 6639 | // there are sufficient fast track slots available |
| 6640 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6641 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6642 | // check compatibility with audio effects. |
| 6643 | Mutex::Autolock _l(mLock); |
| 6644 | // Do not accept FAST flag if the session has software effects |
| 6645 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 6646 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 6647 | audio_input_flags_t old = *flags; |
| 6648 | chain->checkInputFlagCompatibility(flags); |
| 6649 | if (old != *flags) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6650 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 6651 | this, (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6652 | } |
| 6653 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6654 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6655 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 6656 | this, frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6657 | } else { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6658 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
| 6659 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6660 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6661 | this, frameCount, mFrameCount, mPipeFramesP2, |
| 6662 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6663 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6664 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6665 | } |
| 6666 | } |
| 6667 | |
| 6668 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6669 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6670 | // fast track: frame count is exactly the pipe depth |
| 6671 | frameCount = mPipeFramesP2; |
| 6672 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 6673 | *notificationFrames = mFrameCount; |
| 6674 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6675 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6676 | // or 20 ms if there is a fast capture |
| 6677 | // TODO This could be a roundupRatio inline, and const |
| 6678 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6679 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6680 | // minimum number of notification periods is at least kMinNotifications, |
| 6681 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6682 | static const size_t kMinNotifications = 3; |
| 6683 | static const uint32_t kMinMs = 30; |
| 6684 | // TODO This could be a roundupRatio inline |
| 6685 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6686 | // TODO This could be a roundupRatio inline |
| 6687 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6688 | maxNotificationFrames; |
| 6689 | const size_t minFrameCount = maxNotificationFrames * |
| 6690 | max(kMinNotifications, minNotificationsByMs); |
| 6691 | frameCount = max(frameCount, minFrameCount); |
| 6692 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 6693 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6694 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6695 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6696 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6697 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6698 | lStatus = initCheck(); |
| 6699 | if (lStatus != NO_ERROR) { |
| 6700 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6701 | goto Exit; |
| 6702 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6703 | |
| 6704 | { // scope for mLock |
| 6705 | Mutex::Autolock _l(mLock); |
| 6706 | |
| 6707 | track = new RecordTrack(this, client, sampleRate, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 6708 | format, channelMask, frameCount, |
| 6709 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6710 | *flags, TrackBase::TYPE_DEFAULT, portId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6711 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6712 | lStatus = track->initCheck(); |
| 6713 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6714 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6715 | // track must be cleared from the caller as the caller has the AF lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6716 | goto Exit; |
| 6717 | } |
| 6718 | mTracks.add(track); |
| 6719 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6720 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6721 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6722 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6723 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6724 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6725 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6726 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6727 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6728 | lStatus = NO_ERROR; |
| 6729 | |
| 6730 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6731 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6732 | return track; |
| 6733 | } |
| 6734 | |
| 6735 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6736 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6737 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6738 | { |
| 6739 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6740 | sp<ThreadBase> strongMe = this; |
| 6741 | status_t status = NO_ERROR; |
| 6742 | |
| 6743 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6744 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6745 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6746 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6747 | triggerSession, |
| 6748 | recordTrack->sessionId(), |
| 6749 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6750 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6751 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6752 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6753 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6754 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6755 | } else { |
| 6756 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6757 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6758 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6759 | } |
| 6760 | } |
| 6761 | |
| 6762 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6763 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6764 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6765 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6766 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6767 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6768 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6769 | } else { |
| 6770 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6771 | } |
| 6772 | return status; |
| 6773 | } |
| 6774 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6775 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6776 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6777 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6778 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6779 | mActiveTracks.add(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6780 | status_t status = NO_ERROR; |
| 6781 | if (recordTrack->isExternalTrack()) { |
| 6782 | mLock.unlock(); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6783 | status = AudioSystem::startInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6784 | mLock.lock(); |
| 6785 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6786 | if (status != NO_ERROR) { |
| 6787 | mActiveTracks.remove(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6788 | recordTrack->clearSyncStartEvent(); |
| 6789 | ALOGV("RecordThread::start error %d", status); |
| 6790 | return status; |
| 6791 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6792 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6793 | // Catch up with current buffer indices if thread is already running. |
| 6794 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6795 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6796 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6797 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6798 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6799 | // clear any converter state as new data will be discontinuous |
| 6800 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6801 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6802 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6803 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6804 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6805 | ALOGV("Record failed to start"); |
| 6806 | status = BAD_VALUE; |
| 6807 | goto startError; |
| 6808 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6809 | return status; |
| 6810 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6811 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6812 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6813 | if (recordTrack->isExternalTrack()) { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6814 | AudioSystem::stopInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6815 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6816 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6817 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6818 | return status; |
| 6819 | } |
| 6820 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6821 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6822 | { |
| 6823 | sp<SyncEvent> strongEvent = event.promote(); |
| 6824 | |
| 6825 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6826 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6827 | if (ptr != 0) { |
| 6828 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6829 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6830 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6831 | } |
| 6832 | } |
| 6833 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6834 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6835 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6836 | AutoMutex _l(mLock); |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 6837 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6838 | return false; |
| 6839 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6840 | // note that threadLoop may still be processing the track at this point [without lock] |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6841 | recordTrack->mState = TrackBase::PAUSING; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6842 | // signal thread to stop |
| 6843 | mWaitWorkCV.broadcast(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6844 | // do not wait for mStartStopCond if exiting |
| 6845 | if (exitPending()) { |
| 6846 | return true; |
| 6847 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6848 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6849 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6850 | // if we have been restarted, recordTrack is in mActiveTracks here |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 6851 | if (exitPending() || mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6852 | ALOGV("Record stopped OK"); |
| 6853 | return true; |
| 6854 | } |
| 6855 | return false; |
| 6856 | } |
| 6857 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6858 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6859 | { |
| 6860 | return false; |
| 6861 | } |
| 6862 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6863 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6864 | { |
| 6865 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6866 | if (!isValidSyncEvent(event)) { |
| 6867 | return BAD_VALUE; |
| 6868 | } |
| 6869 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6870 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6871 | status_t ret = NAME_NOT_FOUND; |
| 6872 | |
| 6873 | Mutex::Autolock _l(mLock); |
| 6874 | |
| 6875 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6876 | sp<RecordTrack> track = mTracks[i]; |
| 6877 | if (eventSession == track->sessionId()) { |
| 6878 | (void) track->setSyncEvent(event); |
| 6879 | ret = NO_ERROR; |
| 6880 | } |
| 6881 | } |
| 6882 | return ret; |
| 6883 | #else |
| 6884 | return BAD_VALUE; |
| 6885 | #endif |
| 6886 | } |
| 6887 | |
| 6888 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6889 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6890 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6891 | track->terminate(); |
| 6892 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6893 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6894 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6895 | removeTrack_l(track); |
| 6896 | } |
| 6897 | } |
| 6898 | |
| 6899 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6900 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6901 | String8 result; |
| 6902 | track->appendDump(result, false /* active */); |
| 6903 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
| 6904 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6905 | mTracks.remove(track); |
| 6906 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6907 | if (track->isFastTrack()) { |
| 6908 | ALOG_ASSERT(!mFastTrackAvail); |
| 6909 | mFastTrackAvail = true; |
| 6910 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6914 | { |
| 6915 | dumpInternals(fd, args); |
| 6916 | dumpTracks(fd, args); |
| 6917 | dumpEffectChains(fd, args); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6918 | dprintf(fd, " Local log:\n"); |
| 6919 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6923 | { |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6924 | dumpBase(fd, args); |
| 6925 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 6926 | AudioStreamIn *input = mInput; |
| 6927 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; |
| 6928 | dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n", |
| 6929 | input, flags, inputFlagsToString(flags).c_str()); |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6930 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6931 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6932 | } |
Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 6933 | |
| 6934 | if (input != nullptr) { |
| 6935 | dprintf(fd, " Hal stream dump:\n"); |
| 6936 | (void)input->stream->dump(fd); |
| 6937 | } |
| 6938 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6939 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6940 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6941 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6942 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 6943 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6944 | // This is a large object so we place it on the heap. |
| 6945 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 6946 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 6947 | copy->dump(fd); |
| 6948 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6949 | } |
| 6950 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6951 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6952 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6953 | String8 result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6954 | size_t numtracks = mTracks.size(); |
| 6955 | size_t numactive = mActiveTracks.size(); |
| 6956 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6957 | dprintf(fd, " %zu Tracks", numtracks); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6958 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6959 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6960 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6961 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6962 | RecordTrack::appendDumpHeader(result); |
| 6963 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6964 | sp<RecordTrack> track = mTracks[i]; |
| 6965 | if (track != 0) { |
| 6966 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6967 | if (active) { |
| 6968 | numactiveseen++; |
| 6969 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6970 | result.append(prefix); |
| 6971 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6972 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6973 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6974 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6975 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6976 | } |
| 6977 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6978 | if (numactiveseen != numactive) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6979 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6980 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6981 | result.append(prefix); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6982 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6983 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6984 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6985 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6986 | result.append(prefix); |
| 6987 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6988 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6989 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6990 | |
| 6991 | } |
| 6992 | write(fd, result.string(), result.size()); |
| 6993 | } |
| 6994 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6995 | |
| 6996 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 6997 | { |
| 6998 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6999 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7000 | mRsmpInFront = recordThread->mRsmpInRear; |
| 7001 | mRsmpInUnrel = 0; |
| 7002 | } |
| 7003 | |
| 7004 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 7005 | size_t *framesAvailable, bool *hasOverrun) |
| 7006 | { |
| 7007 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 7008 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7009 | const int32_t rear = recordThread->mRsmpInRear; |
| 7010 | const int32_t front = mRsmpInFront; |
| 7011 | const ssize_t filled = rear - front; |
| 7012 | |
| 7013 | size_t framesIn; |
| 7014 | bool overrun = false; |
| 7015 | if (filled < 0) { |
| 7016 | // should not happen, but treat like a massive overrun and re-sync |
| 7017 | framesIn = 0; |
| 7018 | mRsmpInFront = rear; |
| 7019 | overrun = true; |
| 7020 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 7021 | framesIn = (size_t) filled; |
| 7022 | } else { |
| 7023 | // client is not keeping up with server, but give it latest data |
| 7024 | framesIn = recordThread->mRsmpInFrames; |
| 7025 | mRsmpInFront = /* front = */ rear - framesIn; |
| 7026 | overrun = true; |
| 7027 | } |
| 7028 | if (framesAvailable != NULL) { |
| 7029 | *framesAvailable = framesIn; |
| 7030 | } |
| 7031 | if (hasOverrun != NULL) { |
| 7032 | *hasOverrun = overrun; |
| 7033 | } |
| 7034 | } |
| 7035 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7036 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7037 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7038 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7039 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7040 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7041 | if (threadBase == 0) { |
| 7042 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7043 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7044 | return NOT_ENOUGH_DATA; |
| 7045 | } |
| 7046 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7047 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7048 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7049 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7050 | // FIXME should not be P2 (don't want to increase latency) |
| 7051 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7052 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7053 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7054 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 7055 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7056 | if (part1 > (size_t) filled) { |
| 7057 | part1 = filled; |
| 7058 | } |
| 7059 | size_t ask = buffer->frameCount; |
| 7060 | ALOG_ASSERT(ask > 0); |
| 7061 | if (part1 > ask) { |
| 7062 | part1 = ask; |
| 7063 | } |
| 7064 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7065 | // out of data is fine since the resampler will return a short-count. |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7066 | buffer->raw = NULL; |
| 7067 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7068 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7069 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7070 | } |
| 7071 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7072 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7073 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7074 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7075 | return NO_ERROR; |
| 7076 | } |
| 7077 | |
| 7078 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7079 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 7080 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7081 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7082 | size_t stepCount = buffer->frameCount; |
| 7083 | if (stepCount == 0) { |
| 7084 | return; |
| 7085 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7086 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 7087 | mRsmpInUnrel -= stepCount; |
| 7088 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7089 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7090 | buffer->frameCount = 0; |
| 7091 | } |
| 7092 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7093 | void AudioFlinger::RecordThread::checkBtNrec() |
| 7094 | { |
| 7095 | Mutex::Autolock _l(mLock); |
| 7096 | checkBtNrec_l(); |
| 7097 | } |
| 7098 | |
| 7099 | void AudioFlinger::RecordThread::checkBtNrec_l() |
| 7100 | { |
| 7101 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7102 | // pre processings |
| 7103 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7104 | mAudioFlinger->btNrecIsOff(); |
| 7105 | if (mBtNrecSuspended.exchange(suspend) != suspend) { |
| 7106 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7107 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); |
| 7108 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); |
| 7109 | } |
| 7110 | } |
| 7111 | } |
| 7112 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7113 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7114 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 7115 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7116 | { |
| 7117 | bool reconfig = false; |
| 7118 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7119 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7120 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7121 | audio_format_t reqFormat = mFormat; |
| 7122 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7123 | // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs). |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7124 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 7125 | |
| 7126 | AudioParameter param = AudioParameter(keyValuePair); |
| 7127 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7128 | |
| 7129 | // scope for AutoPark extends to end of method |
| 7130 | AutoPark<FastCapture> park(mFastCapture); |
| 7131 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7132 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 7133 | // channel count change can be requested. Do we mandate the first client defines the |
| 7134 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 7135 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 7136 | samplingRate = value; |
| 7137 | reconfig = true; |
| 7138 | } |
| 7139 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7140 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7141 | status = BAD_VALUE; |
| 7142 | } else { |
| 7143 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7144 | reconfig = true; |
| 7145 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7146 | } |
| 7147 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 7148 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7149 | if (!audio_is_input_channel(mask) || |
| 7150 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7151 | status = BAD_VALUE; |
| 7152 | } else { |
| 7153 | channelMask = mask; |
| 7154 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7155 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7156 | } |
| 7157 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7158 | // do not accept frame count changes if tracks are open as the track buffer |
| 7159 | // size depends on frame count and correct behavior would not be guaranteed |
| 7160 | // if frame count is changed after track creation |
| 7161 | if (mActiveTracks.size() > 0) { |
| 7162 | status = INVALID_OPERATION; |
| 7163 | } else { |
| 7164 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7165 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7166 | } |
| 7167 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 7168 | // forward device change to effects that have requested to be |
| 7169 | // aware of attached audio device. |
| 7170 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7171 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7172 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7173 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7174 | // store input device and output device but do not forward output device to audio HAL. |
| 7175 | // Note that status is ignored by the caller for output device |
| 7176 | // (see AudioFlinger::setParameters() |
| 7177 | if (audio_is_output_devices(value)) { |
| 7178 | mOutDevice = value; |
| 7179 | status = BAD_VALUE; |
| 7180 | } else { |
| 7181 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7182 | if (value != AUDIO_DEVICE_NONE) { |
| 7183 | mPrevInDevice = value; |
| 7184 | } |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7185 | checkBtNrec_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7186 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7187 | } |
| 7188 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7189 | mAudioSource != (audio_source_t)value) { |
| 7190 | // forward device change to effects that have requested to be |
| 7191 | // aware of attached audio device. |
| 7192 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7193 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7194 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7195 | mAudioSource = (audio_source_t)value; |
| 7196 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7197 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7198 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7199 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7200 | if (status == INVALID_OPERATION) { |
| 7201 | inputStandBy(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7202 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7203 | } |
| 7204 | if (reconfig) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7205 | if (status == BAD_VALUE) { |
| 7206 | uint32_t sRate; |
| 7207 | audio_channel_mask_t channelMask; |
| 7208 | audio_format_t format; |
| 7209 | if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK && |
| 7210 | audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) && |
| 7211 | sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && |
| 7212 | audio_channel_count_from_in_mask(channelMask) <= FCC_8) { |
| 7213 | status = NO_ERROR; |
| 7214 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7215 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7216 | if (status == NO_ERROR) { |
| 7217 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7218 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7219 | } |
| 7220 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7221 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7222 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7223 | return reconfig; |
| 7224 | } |
| 7225 | |
| 7226 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7227 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7228 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7229 | if (initCheck() == NO_ERROR) { |
| 7230 | String8 out_s8; |
| 7231 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { |
| 7232 | return out_s8; |
| 7233 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7234 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7235 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7236 | } |
| 7237 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7238 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7239 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7240 | |
| 7241 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7242 | |
| 7243 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7244 | case AUDIO_INPUT_OPENED: |
| 7245 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7246 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7247 | desc->mChannelMask = mChannelMask; |
| 7248 | desc->mSamplingRate = mSampleRate; |
| 7249 | desc->mFormat = mFormat; |
| 7250 | desc->mFrameCount = mFrameCount; |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7251 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7252 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7253 | break; |
| 7254 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7255 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7256 | default: |
| 7257 | break; |
| 7258 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7259 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7260 | } |
| 7261 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7262 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7263 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7264 | status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 7265 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7266 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7267 | LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_8, "HAL channel count %d > %d", mChannelCount, FCC_8); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7268 | mFormat = mHALFormat; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7269 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 7270 | result = mInput->stream->getFrameSize(&mFrameSize); |
| 7271 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
| 7272 | result = mInput->stream->getBufferSize(&mBufferSize); |
| 7273 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7274 | mFrameCount = mBufferSize / mFrameSize; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7275 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, " |
| 7276 | "mBufferSize=%lld, mFrameCount=%lld", |
| 7277 | this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize, |
| 7278 | (long long)mFrameCount); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7279 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7280 | // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7281 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7282 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7283 | // A larger value should allow more old data to be read after a track calls start(), |
| 7284 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7285 | // |
| 7286 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7287 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7288 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7289 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7290 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7291 | |
| 7292 | // TODO optimize audio capture buffer sizes ... |
| 7293 | // Here we calculate the size of the sliding buffer used as a source |
| 7294 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7295 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7296 | // be better to have it derived from the pipe depth in the long term. |
| 7297 | // The current value is higher than necessary. However it should not add to latency. |
| 7298 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7299 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 7300 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; |
| 7301 | (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7302 | // if posix_memalign fails, will segv here. |
| 7303 | memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7304 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7305 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7306 | // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7307 | } |
| 7308 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7309 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7310 | { |
| 7311 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7312 | uint32_t result; |
| 7313 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { |
| 7314 | return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7315 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7316 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7317 | } |
| 7318 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7319 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 7320 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7321 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7322 | uint32_t result = 0; |
| 7323 | if (getEffectChain_l(sessionId) != 0) { |
| 7324 | result = EFFECT_SESSION; |
| 7325 | } |
| 7326 | |
| 7327 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7328 | if (sessionId == mTracks[i]->sessionId()) { |
| 7329 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7330 | if (mTracks[i]->isFastTrack()) { |
| 7331 | result |= FAST_SESSION; |
| 7332 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7333 | break; |
| 7334 | } |
| 7335 | } |
| 7336 | |
| 7337 | return result; |
| 7338 | } |
| 7339 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7340 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7341 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7342 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7343 | Mutex::Autolock _l(mLock); |
| 7344 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7345 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7346 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7347 | if (ids.indexOfKey(sessionId) < 0) { |
| 7348 | ids.add(sessionId, true); |
| 7349 | } |
| 7350 | } |
| 7351 | return ids; |
| 7352 | } |
| 7353 | |
| 7354 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7355 | { |
| 7356 | Mutex::Autolock _l(mLock); |
| 7357 | AudioStreamIn *input = mInput; |
| 7358 | mInput = NULL; |
| 7359 | return input; |
| 7360 | } |
| 7361 | |
| 7362 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7363 | sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7364 | { |
| 7365 | if (mInput == NULL) { |
| 7366 | return NULL; |
| 7367 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7368 | return mInput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7369 | } |
| 7370 | |
| 7371 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7372 | { |
| 7373 | // only one chain per input thread |
| 7374 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7375 | ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7376 | return INVALID_OPERATION; |
| 7377 | } |
| 7378 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7379 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7380 | chain->setInBuffer(NULL); |
| 7381 | chain->setOutBuffer(NULL); |
| 7382 | |
| 7383 | checkSuspendOnAddEffectChain_l(chain); |
| 7384 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7385 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7386 | // just moved them to a new input stream. |
| 7387 | chain->syncHalEffectsState(); |
| 7388 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7389 | mEffectChains.add(chain); |
| 7390 | |
| 7391 | return NO_ERROR; |
| 7392 | } |
| 7393 | |
| 7394 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7395 | { |
| 7396 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7397 | ALOGW_IF(mEffectChains.size() != 1, |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7398 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7399 | chain.get(), mEffectChains.size(), this); |
| 7400 | if (mEffectChains.size() == 1) { |
| 7401 | mEffectChains.removeAt(0); |
| 7402 | } |
| 7403 | return 0; |
| 7404 | } |
| 7405 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7406 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7407 | audio_patch_handle_t *handle) |
| 7408 | { |
| 7409 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7410 | |
| 7411 | // store new device and send to effects |
| 7412 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7413 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7414 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7415 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7416 | } |
| 7417 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7418 | checkBtNrec_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7419 | |
| 7420 | // store new source and send to effects |
| 7421 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7422 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7423 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7424 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7425 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7426 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7427 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7428 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7429 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 7430 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 7431 | patch->sources, |
| 7432 | patch->num_sinks, |
| 7433 | patch->sinks, |
| 7434 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7435 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7436 | char *address; |
| 7437 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7438 | address = audio_device_address_to_parameter( |
| 7439 | patch->sources[0].ext.device.type, |
| 7440 | patch->sources[0].ext.device.address); |
| 7441 | } else { |
| 7442 | address = (char *)calloc(1, 1); |
| 7443 | } |
| 7444 | AudioParameter param = AudioParameter(String8(address)); |
| 7445 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7446 | param.addInt(String8(AudioParameter::keyRouting), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7447 | (int)patch->sources[0].ext.device.type); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7448 | param.addInt(String8(AudioParameter::keyInputSource), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7449 | (int)patch->sinks[0].ext.mix.usecase.source); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7450 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7451 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7452 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7453 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7454 | if (mInDevice != mPrevInDevice) { |
| 7455 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7456 | mPrevInDevice = mInDevice; |
| 7457 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7458 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7459 | return status; |
| 7460 | } |
| 7461 | |
| 7462 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7463 | { |
| 7464 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7465 | |
| 7466 | mInDevice = AUDIO_DEVICE_NONE; |
| 7467 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7468 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7469 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 7470 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7471 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7472 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7473 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7474 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7475 | } |
| 7476 | return status; |
| 7477 | } |
| 7478 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7479 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7480 | { |
| 7481 | Mutex::Autolock _l(mLock); |
| 7482 | mTracks.add(record); |
| 7483 | } |
| 7484 | |
| 7485 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7486 | { |
| 7487 | Mutex::Autolock _l(mLock); |
| 7488 | destroyTrack_l(record); |
| 7489 | } |
| 7490 | |
| 7491 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7492 | { |
| 7493 | ThreadBase::getAudioPortConfig(config); |
| 7494 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7495 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7496 | config->ext.mix.usecase.source = mAudioSource; |
| 7497 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7498 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7499 | // ---------------------------------------------------------------------------- |
| 7500 | // Mmap |
| 7501 | // ---------------------------------------------------------------------------- |
| 7502 | |
| 7503 | AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread) |
| 7504 | : mThread(thread) |
| 7505 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7506 | assert(thread != 0); // thread must start non-null and stay non-null |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7507 | } |
| 7508 | |
| 7509 | AudioFlinger::MmapThreadHandle::~MmapThreadHandle() |
| 7510 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7511 | mThread->disconnect(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7512 | } |
| 7513 | |
| 7514 | status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, |
| 7515 | struct audio_mmap_buffer_info *info) |
| 7516 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7517 | return mThread->createMmapBuffer(minSizeFrames, info); |
| 7518 | } |
| 7519 | |
| 7520 | status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position) |
| 7521 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7522 | return mThread->getMmapPosition(position); |
| 7523 | } |
| 7524 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7525 | status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client, |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7526 | audio_port_handle_t *handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7527 | |
| 7528 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7529 | return mThread->start(client, handle); |
| 7530 | } |
| 7531 | |
| 7532 | status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle) |
| 7533 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7534 | return mThread->stop(handle); |
| 7535 | } |
| 7536 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7537 | status_t AudioFlinger::MmapThreadHandle::standby() |
| 7538 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7539 | return mThread->standby(); |
| 7540 | } |
| 7541 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7542 | |
| 7543 | AudioFlinger::MmapThread::MmapThread( |
| 7544 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 7545 | AudioHwDevice *hwDev, sp<StreamHalInterface> stream, |
| 7546 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 7547 | : ThreadBase(audioFlinger, id, outDevice, inDevice, MMAP, systemReady), |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7548 | mSessionId(AUDIO_SESSION_NONE), |
| 7549 | mDeviceId(AUDIO_PORT_HANDLE_NONE), mPortId(AUDIO_PORT_HANDLE_NONE), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7550 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), |
| 7551 | mActiveTracks(&this->mLocalLog) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7552 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7553 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7554 | readHalParameters_l(); |
| 7555 | } |
| 7556 | |
| 7557 | AudioFlinger::MmapThread::~MmapThread() |
| 7558 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7559 | releaseWakeLock_l(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7560 | } |
| 7561 | |
| 7562 | void AudioFlinger::MmapThread::onFirstRef() |
| 7563 | { |
| 7564 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 7565 | } |
| 7566 | |
| 7567 | void AudioFlinger::MmapThread::disconnect() |
| 7568 | { |
| 7569 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 7570 | stop(t->portId()); |
| 7571 | } |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7572 | // This will decrement references and may cause the destruction of this thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7573 | if (isOutput()) { |
| 7574 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); |
| 7575 | } else { |
| 7576 | AudioSystem::releaseInput(mId, mSessionId); |
| 7577 | } |
| 7578 | } |
| 7579 | |
| 7580 | |
| 7581 | void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr, |
| 7582 | audio_stream_type_t streamType __unused, |
| 7583 | audio_session_t sessionId, |
| 7584 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7585 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7586 | audio_port_handle_t portId) |
| 7587 | { |
| 7588 | mAttr = *attr; |
| 7589 | mSessionId = sessionId; |
| 7590 | mCallback = callback; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7591 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7592 | mPortId = portId; |
| 7593 | } |
| 7594 | |
| 7595 | status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames, |
| 7596 | struct audio_mmap_buffer_info *info) |
| 7597 | { |
| 7598 | if (mHalStream == 0) { |
| 7599 | return NO_INIT; |
| 7600 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7601 | mStandby = true; |
| 7602 | acquireWakeLock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7603 | return mHalStream->createMmapBuffer(minSizeFrames, info); |
| 7604 | } |
| 7605 | |
| 7606 | status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position) |
| 7607 | { |
| 7608 | if (mHalStream == 0) { |
| 7609 | return NO_INIT; |
| 7610 | } |
| 7611 | return mHalStream->getMmapPosition(position); |
| 7612 | } |
| 7613 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7614 | status_t AudioFlinger::MmapThread::start(const AudioClient& client, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7615 | audio_port_handle_t *handle) |
| 7616 | { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7617 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, |
| 7618 | client.clientUid, mStandby, mPortId, *handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7619 | if (mHalStream == 0) { |
| 7620 | return NO_INIT; |
| 7621 | } |
| 7622 | |
| 7623 | status_t ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7624 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7625 | if (*handle == mPortId) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7626 | // for the first track, reuse portId and session allocated when the stream was opened |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7627 | ret = mHalStream->start(); |
| 7628 | if (ret != NO_ERROR) { |
| 7629 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); |
| 7630 | return ret; |
| 7631 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7632 | mStandby = false; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7633 | return NO_ERROR; |
| 7634 | } |
| 7635 | |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame^] | 7636 | if (!isOutput() && !recordingAllowed(client.packageName, client.clientPid, client.clientUid)) { |
| 7637 | return PERMISSION_DENIED; |
| 7638 | } |
| 7639 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7640 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 7641 | |
| 7642 | audio_io_handle_t io = mId; |
| 7643 | if (isOutput()) { |
| 7644 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 7645 | config.sample_rate = mSampleRate; |
| 7646 | config.channel_mask = mChannelMask; |
| 7647 | config.format = mFormat; |
| 7648 | audio_stream_type_t stream = streamType(); |
| 7649 | audio_output_flags_t flags = |
| 7650 | (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7651 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7652 | ret = AudioSystem::getOutputForAttr(&mAttr, &io, |
| 7653 | mSessionId, |
| 7654 | &stream, |
| 7655 | client.clientUid, |
| 7656 | &config, |
| 7657 | flags, |
| 7658 | &deviceId, |
| 7659 | &portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7660 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7661 | audio_config_base_t config; |
| 7662 | config.sample_rate = mSampleRate; |
| 7663 | config.channel_mask = mChannelMask; |
| 7664 | config.format = mFormat; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7665 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7666 | ret = AudioSystem::getInputForAttr(&mAttr, &io, |
| 7667 | mSessionId, |
| 7668 | client.clientPid, |
| 7669 | client.clientUid, |
| 7670 | &config, |
| 7671 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, |
| 7672 | &deviceId, |
| 7673 | &portId); |
| 7674 | } |
| 7675 | // APM should not chose a different input or output stream for the same set of attributes |
| 7676 | // and audo configuration |
| 7677 | if (ret != NO_ERROR || io != mId) { |
| 7678 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", |
| 7679 | __FUNCTION__, ret, io, mId); |
| 7680 | return BAD_VALUE; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7681 | } |
| 7682 | |
| 7683 | if (isOutput()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7684 | ret = AudioSystem::startOutput(mId, streamType(), mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7685 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7686 | ret = AudioSystem::startInput(mId, mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7687 | } |
| 7688 | |
| 7689 | // abort if start is rejected by audio policy manager |
| 7690 | if (ret != NO_ERROR) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7691 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7692 | if (mActiveTracks.size() != 0) { |
| 7693 | if (isOutput()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7694 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7695 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7696 | AudioSystem::releaseInput(mId, mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7697 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7698 | } else { |
| 7699 | mHalStream->stop(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7700 | } |
| 7701 | return PERMISSION_DENIED; |
| 7702 | } |
| 7703 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7704 | sp<MmapTrack> track = new MmapTrack(this, mSampleRate, mFormat, mChannelMask, mSessionId, |
| 7705 | client.clientUid, client.clientPid, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7706 | |
| 7707 | mActiveTracks.add(track); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7708 | sp<EffectChain> chain = getEffectChain_l(mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7709 | if (chain != 0) { |
| 7710 | chain->setStrategy(AudioSystem::getStrategyForStream(streamType())); |
| 7711 | chain->incTrackCnt(); |
| 7712 | chain->incActiveTrackCnt(); |
| 7713 | } |
| 7714 | |
| 7715 | *handle = portId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7716 | broadcast_l(); |
| 7717 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7718 | ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7719 | |
| 7720 | return NO_ERROR; |
| 7721 | } |
| 7722 | |
| 7723 | status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle) |
| 7724 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7725 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 7726 | |
| 7727 | if (mHalStream == 0) { |
| 7728 | return NO_INIT; |
| 7729 | } |
| 7730 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7731 | if (handle == mPortId) { |
| 7732 | mHalStream->stop(); |
| 7733 | return NO_ERROR; |
| 7734 | } |
| 7735 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7736 | sp<MmapTrack> track; |
| 7737 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 7738 | if (handle == t->portId()) { |
| 7739 | track = t; |
| 7740 | break; |
| 7741 | } |
| 7742 | } |
| 7743 | if (track == 0) { |
| 7744 | return BAD_VALUE; |
| 7745 | } |
| 7746 | |
| 7747 | mActiveTracks.remove(track); |
| 7748 | |
| 7749 | if (isOutput()) { |
| 7750 | AudioSystem::stopOutput(mId, streamType(), track->sessionId()); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7751 | AudioSystem::releaseOutput(mId, streamType(), track->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7752 | } else { |
| 7753 | AudioSystem::stopInput(mId, track->sessionId()); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7754 | AudioSystem::releaseInput(mId, track->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 7758 | if (chain != 0) { |
| 7759 | chain->decActiveTrackCnt(); |
| 7760 | chain->decTrackCnt(); |
| 7761 | } |
| 7762 | |
| 7763 | broadcast_l(); |
| 7764 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7765 | return NO_ERROR; |
| 7766 | } |
| 7767 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7768 | status_t AudioFlinger::MmapThread::standby() |
| 7769 | { |
| 7770 | ALOGV("%s", __FUNCTION__); |
| 7771 | |
| 7772 | if (mHalStream == 0) { |
| 7773 | return NO_INIT; |
| 7774 | } |
| 7775 | if (mActiveTracks.size() != 0) { |
| 7776 | return INVALID_OPERATION; |
| 7777 | } |
| 7778 | mHalStream->standby(); |
| 7779 | mStandby = true; |
| 7780 | releaseWakeLock(); |
| 7781 | return NO_ERROR; |
| 7782 | } |
| 7783 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7784 | |
| 7785 | void AudioFlinger::MmapThread::readHalParameters_l() |
| 7786 | { |
| 7787 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 7788 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
| 7789 | mFormat = mHALFormat; |
| 7790 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 7791 | result = mHalStream->getFrameSize(&mFrameSize); |
| 7792 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
| 7793 | result = mHalStream->getBufferSize(&mBufferSize); |
| 7794 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
| 7795 | mFrameCount = mBufferSize / mFrameSize; |
| 7796 | } |
| 7797 | |
| 7798 | bool AudioFlinger::MmapThread::threadLoop() |
| 7799 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7800 | checkSilentMode_l(); |
| 7801 | |
| 7802 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 7803 | |
| 7804 | while (!exitPending()) |
| 7805 | { |
| 7806 | Mutex::Autolock _l(mLock); |
| 7807 | Vector< sp<EffectChain> > effectChains; |
| 7808 | |
| 7809 | if (mSignalPending) { |
| 7810 | // A signal was raised while we were unlocked |
| 7811 | mSignalPending = false; |
| 7812 | } else { |
| 7813 | if (mConfigEvents.isEmpty()) { |
| 7814 | // we're about to wait, flush the binder command buffer |
| 7815 | IPCThreadState::self()->flushCommands(); |
| 7816 | |
| 7817 | if (exitPending()) { |
| 7818 | break; |
| 7819 | } |
| 7820 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7821 | // wait until we have something to do... |
| 7822 | ALOGV("%s going to sleep", myName.string()); |
| 7823 | mWaitWorkCV.wait(mLock); |
| 7824 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7825 | |
| 7826 | checkSilentMode_l(); |
| 7827 | |
| 7828 | continue; |
| 7829 | } |
| 7830 | } |
| 7831 | |
| 7832 | processConfigEvents_l(); |
| 7833 | |
| 7834 | processVolume_l(); |
| 7835 | |
| 7836 | checkInvalidTracks_l(); |
| 7837 | |
| 7838 | mActiveTracks.updatePowerState(this); |
| 7839 | |
| 7840 | lockEffectChains_l(effectChains); |
| 7841 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 7842 | effectChains[i]->process_l(); |
| 7843 | } |
| 7844 | // enable changes in effect chain |
| 7845 | unlockEffectChains(effectChains); |
| 7846 | // Effect chains will be actually deleted here if they were removed from |
| 7847 | // mEffectChains list during mixing or effects processing |
| 7848 | } |
| 7849 | |
| 7850 | threadLoop_exit(); |
| 7851 | |
| 7852 | if (!mStandby) { |
| 7853 | threadLoop_standby(); |
| 7854 | mStandby = true; |
| 7855 | } |
| 7856 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7857 | ALOGV("Thread %p type %d exiting", this, mType); |
| 7858 | return false; |
| 7859 | } |
| 7860 | |
| 7861 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 7862 | bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair, |
| 7863 | status_t& status) |
| 7864 | { |
| 7865 | AudioParameter param = AudioParameter(keyValuePair); |
| 7866 | int value; |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 7867 | bool sendToHal = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7868 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 7869 | audio_devices_t device = (audio_devices_t)value; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7870 | // forward device change to effects that have requested to be |
| 7871 | // aware of attached audio device. |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 7872 | if (device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7873 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 7874 | mEffectChains[i]->setDevice_l(device); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7875 | } |
| 7876 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 7877 | if (audio_is_output_devices(device)) { |
| 7878 | mOutDevice = device; |
| 7879 | if (!isOutput()) { |
| 7880 | sendToHal = false; |
| 7881 | } |
| 7882 | } else { |
| 7883 | mInDevice = device; |
| 7884 | if (device != AUDIO_DEVICE_NONE) { |
| 7885 | mPrevInDevice = value; |
| 7886 | } |
| 7887 | // TODO: implement and call checkBtNrec_l(); |
| 7888 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7889 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 7890 | if (sendToHal) { |
| 7891 | status = mHalStream->setParameters(keyValuePair); |
| 7892 | } else { |
| 7893 | status = NO_ERROR; |
| 7894 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7895 | |
| 7896 | return false; |
| 7897 | } |
| 7898 | |
| 7899 | String8 AudioFlinger::MmapThread::getParameters(const String8& keys) |
| 7900 | { |
| 7901 | Mutex::Autolock _l(mLock); |
| 7902 | String8 out_s8; |
| 7903 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { |
| 7904 | return out_s8; |
| 7905 | } |
| 7906 | return String8(); |
| 7907 | } |
| 7908 | |
| 7909 | void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
| 7910 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7911 | |
| 7912 | desc->mIoHandle = mId; |
| 7913 | |
| 7914 | switch (event) { |
| 7915 | case AUDIO_INPUT_OPENED: |
| 7916 | case AUDIO_INPUT_CONFIG_CHANGED: |
| 7917 | case AUDIO_OUTPUT_OPENED: |
| 7918 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 7919 | desc->mPatch = mPatch; |
| 7920 | desc->mChannelMask = mChannelMask; |
| 7921 | desc->mSamplingRate = mSampleRate; |
| 7922 | desc->mFormat = mFormat; |
| 7923 | desc->mFrameCount = mFrameCount; |
| 7924 | desc->mFrameCountHAL = mFrameCount; |
| 7925 | desc->mLatency = 0; |
| 7926 | break; |
| 7927 | |
| 7928 | case AUDIO_INPUT_CLOSED: |
| 7929 | case AUDIO_OUTPUT_CLOSED: |
| 7930 | default: |
| 7931 | break; |
| 7932 | } |
| 7933 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
| 7934 | } |
| 7935 | |
| 7936 | status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7937 | audio_patch_handle_t *handle) |
| 7938 | { |
| 7939 | status_t status = NO_ERROR; |
| 7940 | |
| 7941 | // store new device and send to effects |
| 7942 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 7943 | audio_port_handle_t deviceId; |
| 7944 | if (isOutput()) { |
| 7945 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 7946 | type |= patch->sinks[i].ext.device.type; |
| 7947 | } |
| 7948 | deviceId = patch->sinks[0].id; |
| 7949 | } else { |
| 7950 | type = patch->sources[0].ext.device.type; |
| 7951 | deviceId = patch->sources[0].id; |
| 7952 | } |
| 7953 | |
| 7954 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7955 | mEffectChains[i]->setDevice_l(type); |
| 7956 | } |
| 7957 | |
| 7958 | if (isOutput()) { |
| 7959 | mOutDevice = type; |
| 7960 | } else { |
| 7961 | mInDevice = type; |
| 7962 | // store new source and send to effects |
| 7963 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7964 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 7965 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7966 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 7967 | } |
| 7968 | } |
| 7969 | } |
| 7970 | |
| 7971 | if (mAudioHwDev->supportsAudioPatches()) { |
| 7972 | status = mHalDevice->createAudioPatch(patch->num_sources, |
| 7973 | patch->sources, |
| 7974 | patch->num_sinks, |
| 7975 | patch->sinks, |
| 7976 | handle); |
| 7977 | } else { |
| 7978 | char *address; |
| 7979 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 7980 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 7981 | address = audio_device_address_to_parameter( |
| 7982 | patch->sinks[0].ext.device.type, |
| 7983 | patch->sinks[0].ext.device.address); |
| 7984 | } else { |
| 7985 | address = (char *)calloc(1, 1); |
| 7986 | } |
| 7987 | AudioParameter param = AudioParameter(String8(address)); |
| 7988 | free(address); |
| 7989 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
| 7990 | if (!isOutput()) { |
| 7991 | param.addInt(String8(AudioParameter::keyInputSource), |
| 7992 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 7993 | } |
| 7994 | status = mHalStream->setParameters(param.toString()); |
| 7995 | *handle = AUDIO_PATCH_HANDLE_NONE; |
| 7996 | } |
| 7997 | |
| 7998 | if (isOutput() && mPrevOutDevice != mOutDevice) { |
| 7999 | mPrevOutDevice = type; |
| 8000 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8001 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8002 | if (mDeviceId != deviceId && callback != 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8003 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8004 | } |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8005 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8006 | } |
| 8007 | if (!isOutput() && mPrevInDevice != mInDevice) { |
| 8008 | mPrevInDevice = type; |
| 8009 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8010 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8011 | if (mDeviceId != deviceId && callback != 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8012 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8013 | } |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8014 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8015 | } |
| 8016 | return status; |
| 8017 | } |
| 8018 | |
| 8019 | status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 8020 | { |
| 8021 | status_t status = NO_ERROR; |
| 8022 | |
| 8023 | mInDevice = AUDIO_DEVICE_NONE; |
| 8024 | |
| 8025 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? |
| 8026 | supportsAudioPatches : false; |
| 8027 | |
| 8028 | if (supportsAudioPatches) { |
| 8029 | status = mHalDevice->releaseAudioPatch(handle); |
| 8030 | } else { |
| 8031 | AudioParameter param; |
| 8032 | param.addInt(String8(AudioParameter::keyRouting), 0); |
| 8033 | status = mHalStream->setParameters(param.toString()); |
| 8034 | } |
| 8035 | return status; |
| 8036 | } |
| 8037 | |
| 8038 | void AudioFlinger::MmapThread::getAudioPortConfig(struct audio_port_config *config) |
| 8039 | { |
| 8040 | ThreadBase::getAudioPortConfig(config); |
| 8041 | if (isOutput()) { |
| 8042 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 8043 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 8044 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 8045 | } else { |
| 8046 | config->role = AUDIO_PORT_ROLE_SINK; |
| 8047 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 8048 | config->ext.mix.usecase.source = mAudioSource; |
| 8049 | } |
| 8050 | } |
| 8051 | |
| 8052 | status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 8053 | { |
| 8054 | audio_session_t session = chain->sessionId(); |
| 8055 | |
| 8056 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 8057 | // Attach all tracks with same session ID to this chain. |
| 8058 | // indicate all active tracks in the chain |
| 8059 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8060 | if (session == track->sessionId()) { |
| 8061 | chain->incTrackCnt(); |
| 8062 | chain->incActiveTrackCnt(); |
| 8063 | } |
| 8064 | } |
| 8065 | |
| 8066 | chain->setThread(this); |
| 8067 | chain->setInBuffer(nullptr); |
| 8068 | chain->setOutBuffer(nullptr); |
| 8069 | chain->syncHalEffectsState(); |
| 8070 | |
| 8071 | mEffectChains.add(chain); |
| 8072 | checkSuspendOnAddEffectChain_l(chain); |
| 8073 | return NO_ERROR; |
| 8074 | } |
| 8075 | |
| 8076 | size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 8077 | { |
| 8078 | audio_session_t session = chain->sessionId(); |
| 8079 | |
| 8080 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 8081 | |
| 8082 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8083 | if (chain == mEffectChains[i]) { |
| 8084 | mEffectChains.removeAt(i); |
| 8085 | // detach all active tracks from the chain |
| 8086 | // detach all tracks with same session ID from this chain |
| 8087 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8088 | if (session == track->sessionId()) { |
| 8089 | chain->decActiveTrackCnt(); |
| 8090 | chain->decTrackCnt(); |
| 8091 | } |
| 8092 | } |
| 8093 | break; |
| 8094 | } |
| 8095 | } |
| 8096 | return mEffectChains.size(); |
| 8097 | } |
| 8098 | |
| 8099 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 8100 | uint32_t AudioFlinger::MmapThread::hasAudioSession_l(audio_session_t sessionId) const |
| 8101 | { |
| 8102 | uint32_t result = 0; |
| 8103 | if (getEffectChain_l(sessionId) != 0) { |
| 8104 | result = EFFECT_SESSION; |
| 8105 | } |
| 8106 | |
| 8107 | for (size_t i = 0; i < mActiveTracks.size(); i++) { |
| 8108 | sp<MmapTrack> track = mActiveTracks[i]; |
| 8109 | if (sessionId == track->sessionId()) { |
| 8110 | result |= TRACK_SESSION; |
| 8111 | if (track->isFastTrack()) { |
| 8112 | result |= FAST_SESSION; |
| 8113 | } |
| 8114 | break; |
| 8115 | } |
| 8116 | } |
| 8117 | |
| 8118 | return result; |
| 8119 | } |
| 8120 | |
| 8121 | void AudioFlinger::MmapThread::threadLoop_standby() |
| 8122 | { |
| 8123 | mHalStream->standby(); |
| 8124 | } |
| 8125 | |
| 8126 | void AudioFlinger::MmapThread::threadLoop_exit() |
| 8127 | { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8128 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8129 | if (callback != 0) { |
| 8130 | callback->onTearDown(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8131 | } |
| 8132 | } |
| 8133 | |
| 8134 | status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
| 8135 | { |
| 8136 | return BAD_VALUE; |
| 8137 | } |
| 8138 | |
| 8139 | bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
| 8140 | { |
| 8141 | return false; |
| 8142 | } |
| 8143 | |
| 8144 | status_t AudioFlinger::MmapThread::checkEffectCompatibility_l( |
| 8145 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 8146 | { |
| 8147 | // No global effect sessions on mmap threads |
| 8148 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 8149 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 8150 | desc->name, mThreadName); |
| 8151 | return BAD_VALUE; |
| 8152 | } |
| 8153 | |
| 8154 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 8155 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", |
| 8156 | desc->name); |
| 8157 | return BAD_VALUE; |
| 8158 | } |
| 8159 | if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8160 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " |
| 8161 | "thread", desc->name); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8162 | return BAD_VALUE; |
| 8163 | } |
| 8164 | |
| 8165 | // Only allow effects without processing load or latency |
| 8166 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { |
| 8167 | return BAD_VALUE; |
| 8168 | } |
| 8169 | |
| 8170 | return NO_ERROR; |
| 8171 | |
| 8172 | } |
| 8173 | |
| 8174 | void AudioFlinger::MmapThread::checkInvalidTracks_l() |
| 8175 | { |
| 8176 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8177 | if (track->isInvalid()) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8178 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8179 | if (callback != 0) { |
| 8180 | callback->onTearDown(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8181 | } |
| 8182 | break; |
| 8183 | } |
| 8184 | } |
| 8185 | } |
| 8186 | |
| 8187 | void AudioFlinger::MmapThread::dump(int fd, const Vector<String16>& args) |
| 8188 | { |
| 8189 | dumpInternals(fd, args); |
| 8190 | dumpTracks(fd, args); |
| 8191 | dumpEffectChains(fd, args); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8192 | dprintf(fd, " Local log:\n"); |
| 8193 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8194 | } |
| 8195 | |
| 8196 | void AudioFlinger::MmapThread::dumpInternals(int fd, const Vector<String16>& args) |
| 8197 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8198 | dumpBase(fd, args); |
| 8199 | |
| 8200 | dprintf(fd, " Attributes: content type %d usage %d source %d\n", |
| 8201 | mAttr.content_type, mAttr.usage, mAttr.source); |
| 8202 | dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId); |
| 8203 | if (mActiveTracks.size() == 0) { |
| 8204 | dprintf(fd, " No active clients\n"); |
| 8205 | } |
| 8206 | } |
| 8207 | |
| 8208 | void AudioFlinger::MmapThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
| 8209 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8210 | String8 result; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8211 | size_t numtracks = mActiveTracks.size(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8212 | dprintf(fd, " %zu Tracks\n", numtracks); |
| 8213 | const char *prefix = " "; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8214 | if (numtracks) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8215 | result.append(prefix); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8216 | MmapTrack::appendDumpHeader(result); |
| 8217 | for (size_t i = 0; i < numtracks ; ++i) { |
| 8218 | sp<MmapTrack> track = mActiveTracks[i]; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8219 | result.append(prefix); |
| 8220 | track->appendDump(result, true /* active */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8221 | } |
| 8222 | } else { |
| 8223 | dprintf(fd, "\n"); |
| 8224 | } |
| 8225 | write(fd, result.string(), result.size()); |
| 8226 | } |
| 8227 | |
| 8228 | AudioFlinger::MmapPlaybackThread::MmapPlaybackThread( |
| 8229 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 8230 | AudioHwDevice *hwDev, AudioStreamOut *output, |
| 8231 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 8232 | : MmapThread(audioFlinger, id, hwDev, output->stream, outDevice, inDevice, systemReady), |
| 8233 | mStreamType(AUDIO_STREAM_MUSIC), |
| 8234 | mStreamVolume(1.0), mStreamMute(false), mOutput(output) |
| 8235 | { |
| 8236 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); |
| 8237 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 8238 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 8239 | mMasterMute = audioFlinger->masterMute_l(); |
| 8240 | if (mAudioHwDev) { |
| 8241 | if (mAudioHwDev->canSetMasterVolume()) { |
| 8242 | mMasterVolume = 1.0; |
| 8243 | } |
| 8244 | |
| 8245 | if (mAudioHwDev->canSetMasterMute()) { |
| 8246 | mMasterMute = false; |
| 8247 | } |
| 8248 | } |
| 8249 | } |
| 8250 | |
| 8251 | void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr, |
| 8252 | audio_stream_type_t streamType, |
| 8253 | audio_session_t sessionId, |
| 8254 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8255 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8256 | audio_port_handle_t portId) |
| 8257 | { |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8258 | MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8259 | mStreamType = streamType; |
| 8260 | } |
| 8261 | |
| 8262 | AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput() |
| 8263 | { |
| 8264 | Mutex::Autolock _l(mLock); |
| 8265 | AudioStreamOut *output = mOutput; |
| 8266 | mOutput = NULL; |
| 8267 | return output; |
| 8268 | } |
| 8269 | |
| 8270 | void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value) |
| 8271 | { |
| 8272 | Mutex::Autolock _l(mLock); |
| 8273 | // Don't apply master volume in SW if our HAL can do it for us. |
| 8274 | if (mAudioHwDev && |
| 8275 | mAudioHwDev->canSetMasterVolume()) { |
| 8276 | mMasterVolume = 1.0; |
| 8277 | } else { |
| 8278 | mMasterVolume = value; |
| 8279 | } |
| 8280 | } |
| 8281 | |
| 8282 | void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted) |
| 8283 | { |
| 8284 | Mutex::Autolock _l(mLock); |
| 8285 | // Don't apply master mute in SW if our HAL can do it for us. |
| 8286 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { |
| 8287 | mMasterMute = false; |
| 8288 | } else { |
| 8289 | mMasterMute = muted; |
| 8290 | } |
| 8291 | } |
| 8292 | |
| 8293 | void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 8294 | { |
| 8295 | Mutex::Autolock _l(mLock); |
| 8296 | if (stream == mStreamType) { |
| 8297 | mStreamVolume = value; |
| 8298 | broadcast_l(); |
| 8299 | } |
| 8300 | } |
| 8301 | |
| 8302 | float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 8303 | { |
| 8304 | Mutex::Autolock _l(mLock); |
| 8305 | if (stream == mStreamType) { |
| 8306 | return mStreamVolume; |
| 8307 | } |
| 8308 | return 0.0f; |
| 8309 | } |
| 8310 | |
| 8311 | void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 8312 | { |
| 8313 | Mutex::Autolock _l(mLock); |
| 8314 | if (stream == mStreamType) { |
| 8315 | mStreamMute= muted; |
| 8316 | broadcast_l(); |
| 8317 | } |
| 8318 | } |
| 8319 | |
| 8320 | void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 8321 | { |
| 8322 | Mutex::Autolock _l(mLock); |
| 8323 | if (streamType == mStreamType) { |
| 8324 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8325 | track->invalidate(); |
| 8326 | } |
| 8327 | broadcast_l(); |
| 8328 | } |
| 8329 | } |
| 8330 | |
| 8331 | void AudioFlinger::MmapPlaybackThread::processVolume_l() |
| 8332 | { |
| 8333 | float volume; |
| 8334 | |
| 8335 | if (mMasterMute || mStreamMute) { |
| 8336 | volume = 0; |
| 8337 | } else { |
| 8338 | volume = mMasterVolume * mStreamVolume; |
| 8339 | } |
| 8340 | |
| 8341 | if (volume != mHalVolFloat) { |
| 8342 | mHalVolFloat = volume; |
| 8343 | |
| 8344 | // Convert volumes from float to 8.24 |
| 8345 | uint32_t vol = (uint32_t)(volume * (1 << 24)); |
| 8346 | |
| 8347 | // Delegate volume control to effect in track effect chain if needed |
| 8348 | // only one effect chain can be present on DirectOutputThread, so if |
| 8349 | // there is one, the track is connected to it |
| 8350 | if (!mEffectChains.isEmpty()) { |
| 8351 | mEffectChains[0]->setVolume_l(&vol, &vol); |
| 8352 | volume = (float)vol / (1 << 24); |
| 8353 | } |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8354 | // Try to use HW volume control and fall back to SW control if not implemented |
| 8355 | if (mOutput->stream->setVolume(volume, volume) != NO_ERROR) { |
| 8356 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8357 | if (callback != 0) { |
| 8358 | int channelCount; |
| 8359 | if (isOutput()) { |
| 8360 | channelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 8361 | } else { |
| 8362 | channelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8363 | } |
| 8364 | Vector<float> values; |
| 8365 | for (int i = 0; i < channelCount; i++) { |
| 8366 | values.add(volume); |
| 8367 | } |
| 8368 | callback->onVolumeChanged(mChannelMask, values); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8369 | } else { |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8370 | ALOGW("Could not set MMAP stream volume: no volume callback!"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8371 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8372 | } |
| 8373 | } |
| 8374 | } |
| 8375 | |
| 8376 | void AudioFlinger::MmapPlaybackThread::checkSilentMode_l() |
| 8377 | { |
| 8378 | if (!mMasterMute) { |
| 8379 | char value[PROPERTY_VALUE_MAX]; |
| 8380 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 8381 | char *endptr; |
| 8382 | unsigned long ul = strtoul(value, &endptr, 0); |
| 8383 | if (*endptr == '\0' && ul != 0) { |
| 8384 | ALOGD("Silence is golden"); |
| 8385 | // The setprop command will not allow a property to be changed after |
| 8386 | // the first time it is set, so we don't have to worry about un-muting. |
| 8387 | setMasterMute_l(true); |
| 8388 | } |
| 8389 | } |
| 8390 | } |
| 8391 | } |
| 8392 | |
| 8393 | void AudioFlinger::MmapPlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 8394 | { |
| 8395 | MmapThread::dumpInternals(fd, args); |
| 8396 | |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8397 | dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", |
| 8398 | mStreamType, mStreamVolume, mHalVolFloat, mStreamMute); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8399 | dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); |
| 8400 | } |
| 8401 | |
| 8402 | AudioFlinger::MmapCaptureThread::MmapCaptureThread( |
| 8403 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 8404 | AudioHwDevice *hwDev, AudioStreamIn *input, |
| 8405 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 8406 | : MmapThread(audioFlinger, id, hwDev, input->stream, outDevice, inDevice, systemReady), |
| 8407 | mInput(input) |
| 8408 | { |
| 8409 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); |
| 8410 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8411 | } |
| 8412 | |
| 8413 | AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput() |
| 8414 | { |
| 8415 | Mutex::Autolock _l(mLock); |
| 8416 | AudioStreamIn *input = mInput; |
| 8417 | mInput = NULL; |
| 8418 | return input; |
| 8419 | } |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 8420 | } // namespace android |