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 | |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 59 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 60 | #include <media/audiohal/StreamHalInterface.h> |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 61 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "AudioFlinger.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 63 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 64 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 65 | #include "ServiceUtilities.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 66 | #include "mediautils/SchedulingPolicyService.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 67 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 68 | #ifdef ADD_BATTERY_DATA |
| 69 | #include <media/IMediaPlayerService.h> |
| 70 | #include <media/IMediaDeathNotifier.h> |
| 71 | #endif |
| 72 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 73 | #ifdef DEBUG_CPU_USAGE |
| 74 | #include <cpustats/CentralTendencyStatistics.h> |
| 75 | #include <cpustats/ThreadCpuUsage.h> |
| 76 | #endif |
| 77 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 78 | #include "AutoPark.h" |
| 79 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 80 | #include <pthread.h> |
| 81 | #include "TypedLogger.h" |
| 82 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 83 | // ---------------------------------------------------------------------------- |
| 84 | |
| 85 | // Note: the following macro is used for extremely verbose logging message. In |
| 86 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 87 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 88 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 89 | // turned on. Do not uncomment the #def below unless you really know what you |
| 90 | // are doing and want to see all of the extremely verbose messages. |
| 91 | //#define VERY_VERY_VERBOSE_LOGGING |
| 92 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 93 | #define ALOGVV ALOGV |
| 94 | #else |
| 95 | #define ALOGVV(a...) do { } while(0) |
| 96 | #endif |
| 97 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 98 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 99 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 100 | template <typename T> |
| 101 | static inline T min(const T& a, const T& b) |
| 102 | { |
| 103 | return a < b ? a : b; |
| 104 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 105 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 106 | namespace android { |
| 107 | |
| 108 | // retry counts for buffer fill timeout |
| 109 | // 50 * ~20msecs = 1 second |
| 110 | static const int8_t kMaxTrackRetries = 50; |
| 111 | static const int8_t kMaxTrackStartupRetries = 50; |
| 112 | // allow less retry attempts on direct output thread. |
| 113 | // direct outputs can be a scarce resource in audio hardware and should |
| 114 | // be released as quickly as possible. |
| 115 | static const int8_t kMaxTrackRetriesDirect = 2; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 116 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 117 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 118 | |
| 119 | // don't warn about blocked writes or record buffer overflows more often than this |
| 120 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 121 | |
| 122 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 123 | static const int kRecordThreadSleepUs = 5000; |
| 124 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 125 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 126 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 127 | |
| 128 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 129 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 130 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 131 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 132 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 133 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 134 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 135 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 136 | // maximum normal sink buffer size |
| 137 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 138 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 139 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 140 | // FIXME This should be based on experimentally observed scheduling jitter |
| 141 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 142 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 143 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 144 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 145 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 146 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 147 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 148 | |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 149 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good |
| 150 | // balance between power consumption and latency, and allows threads to be scheduled reliably |
| 151 | // by the CFS scheduler. |
| 152 | // FIXME Express other hardcoded references to 20ms with references to this constant and move |
| 153 | // it appropriately. |
| 154 | #define FMS_20 20 |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 155 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 156 | // Whether to use fast mixer |
| 157 | static const enum { |
| 158 | FastMixer_Never, // never initialize or use: for debugging only |
| 159 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 160 | // normal mixer multiplier is 1 |
| 161 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 162 | // multiplier is calculated based on min & max normal mixer buffer size |
| 163 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 164 | // multiplier is calculated based on min & max normal mixer buffer size |
| 165 | // FIXME for FastMixer_Dynamic: |
| 166 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 167 | // For example, one HAL implementation returns an error from a large write, |
| 168 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 169 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 170 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 171 | } kUseFastMixer = FastMixer_Static; |
| 172 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 173 | // Whether to use fast capture |
| 174 | static const enum { |
| 175 | FastCapture_Never, // never initialize or use: for debugging only |
| 176 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 177 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 178 | } kUseFastCapture = FastCapture_Static; |
| 179 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 180 | // Priorities for requestPriority |
| 181 | static const int kPriorityAudioApp = 2; |
| 182 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 183 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 184 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 185 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 186 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 187 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 188 | |
| 189 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 190 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 191 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 192 | // The minimum and maximum allowed values |
| 193 | static const int kFastTrackMultiplierMin = 1; |
| 194 | static const int kFastTrackMultiplierMax = 2; |
| 195 | |
| 196 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 197 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 198 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 199 | // See Thread::readOnlyHeap(). |
| 200 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 201 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 202 | // and that all "fast" AudioRecord clients read from. In either case, the size can be small. |
Glenn Kasten | 691b02a | 2017-10-03 10:12:20 -0700 | [diff] [blame] | 203 | static const size_t kRecordThreadReadOnlyHeapSize = 0x4000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 204 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 205 | // ---------------------------------------------------------------------------- |
| 206 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 207 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 208 | |
| 209 | static void sFastTrackMultiplierInit() |
| 210 | { |
| 211 | char value[PROPERTY_VALUE_MAX]; |
| 212 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 213 | char *endptr; |
| 214 | unsigned long ul = strtoul(value, &endptr, 0); |
| 215 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 216 | sFastTrackMultiplier = (int) ul; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // ---------------------------------------------------------------------------- |
| 222 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 223 | #ifdef ADD_BATTERY_DATA |
| 224 | // To collect the amplifier usage |
| 225 | static void addBatteryData(uint32_t params) { |
| 226 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 227 | if (service == NULL) { |
| 228 | // it already logged |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | service->addBatteryData(params); |
| 233 | } |
| 234 | #endif |
| 235 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 236 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 237 | struct { |
| 238 | // call when you acquire a partial wakelock |
| 239 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 240 | pthread_mutex_lock(&mLock); |
| 241 | if (wakeLockToken.get() == nullptr) { |
| 242 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 243 | } else { |
| 244 | if (mCount == 0) { |
| 245 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 246 | } |
| 247 | ++mCount; |
| 248 | } |
| 249 | pthread_mutex_unlock(&mLock); |
| 250 | } |
| 251 | |
| 252 | // call when you release a partial wakelock. |
| 253 | void release(const sp<IBinder> &wakeLockToken) { |
| 254 | if (wakeLockToken.get() == nullptr) { |
| 255 | return; |
| 256 | } |
| 257 | pthread_mutex_lock(&mLock); |
| 258 | if (--mCount < 0) { |
| 259 | ALOGE("negative wakelock count"); |
| 260 | mCount = 0; |
| 261 | } |
| 262 | pthread_mutex_unlock(&mLock); |
| 263 | } |
| 264 | |
| 265 | // retrieves the boottime timebase offset from monotonic. |
| 266 | int64_t getBoottimeOffset() { |
| 267 | pthread_mutex_lock(&mLock); |
| 268 | int64_t boottimeOffset = mBoottimeOffset; |
| 269 | pthread_mutex_unlock(&mLock); |
| 270 | return boottimeOffset; |
| 271 | } |
| 272 | |
| 273 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 274 | // and the selected timebase. |
| 275 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 276 | // |
| 277 | // This only needs to be called upon acquiring the first partial wakelock |
| 278 | // after all other partial wakelocks are released. |
| 279 | // |
| 280 | // We do an empirical measurement of the offset rather than parsing |
| 281 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 282 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 283 | int clockbase; |
| 284 | switch (timebase) { |
| 285 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 286 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 287 | break; |
| 288 | default: |
| 289 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 290 | break; |
| 291 | } |
| 292 | // try three times to get the clock offset, choose the one |
| 293 | // with the minimum gap in measurements. |
| 294 | const int tries = 3; |
| 295 | nsecs_t bestGap, measured; |
| 296 | for (int i = 0; i < tries; ++i) { |
| 297 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 298 | const nsecs_t tbase = systemTime(clockbase); |
| 299 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 300 | const nsecs_t gap = tmono2 - tmono; |
| 301 | if (i == 0 || gap < bestGap) { |
| 302 | bestGap = gap; |
| 303 | measured = tbase - ((tmono + tmono2) >> 1); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // to avoid micro-adjusting, we don't change the timebase |
| 308 | // unless it is significantly different. |
| 309 | // |
| 310 | // Assumption: It probably takes more than toleranceNs to |
| 311 | // suspend and resume the device. |
| 312 | static int64_t toleranceNs = 10000; // 10 us |
| 313 | if (llabs(*offset - measured) > toleranceNs) { |
| 314 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 315 | (long long)*offset, (long long)measured); |
| 316 | *offset = measured; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | pthread_mutex_t mLock; |
| 321 | int32_t mCount; |
| 322 | int64_t mBoottimeOffset; |
| 323 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 324 | |
| 325 | // ---------------------------------------------------------------------------- |
| 326 | // CPU Stats |
| 327 | // ---------------------------------------------------------------------------- |
| 328 | |
| 329 | class CpuStats { |
| 330 | public: |
| 331 | CpuStats(); |
| 332 | void sample(const String8 &title); |
| 333 | #ifdef DEBUG_CPU_USAGE |
| 334 | private: |
| 335 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 336 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 337 | |
| 338 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 339 | |
| 340 | int mCpuNum; // thread's current CPU number |
| 341 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 342 | #endif |
| 343 | }; |
| 344 | |
| 345 | CpuStats::CpuStats() |
| 346 | #ifdef DEBUG_CPU_USAGE |
| 347 | : mCpuNum(-1), mCpukHz(-1) |
| 348 | #endif |
| 349 | { |
| 350 | } |
| 351 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 352 | void CpuStats::sample(const String8 &title |
| 353 | #ifndef DEBUG_CPU_USAGE |
| 354 | __unused |
| 355 | #endif |
| 356 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 357 | #ifdef DEBUG_CPU_USAGE |
| 358 | // get current thread's delta CPU time in wall clock ns |
| 359 | double wcNs; |
| 360 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 361 | |
| 362 | // record sample for wall clock statistics |
| 363 | if (valid) { |
| 364 | mWcStats.sample(wcNs); |
| 365 | } |
| 366 | |
| 367 | // get the current CPU number |
| 368 | int cpuNum = sched_getcpu(); |
| 369 | |
| 370 | // get the current CPU frequency in kHz |
| 371 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 372 | |
| 373 | // check if either CPU number or frequency changed |
| 374 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 375 | mCpuNum = cpuNum; |
| 376 | mCpukHz = cpukHz; |
| 377 | // ignore sample for purposes of cycles |
| 378 | valid = false; |
| 379 | } |
| 380 | |
| 381 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 382 | if (valid && mCpukHz > 0) { |
| 383 | double cycles = wcNs * cpukHz * 0.000001; |
| 384 | mHzStats.sample(cycles); |
| 385 | } |
| 386 | |
| 387 | unsigned n = mWcStats.n(); |
| 388 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 389 | if ((n & 127) == 1) { |
| 390 | long long elapsed = mCpuUsage.elapsed(); |
| 391 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 392 | double perLoop = elapsed / (double) n; |
| 393 | double perLoop100 = perLoop * 0.01; |
| 394 | double perLoop1k = perLoop * 0.001; |
| 395 | double mean = mWcStats.mean(); |
| 396 | double stddev = mWcStats.stddev(); |
| 397 | double minimum = mWcStats.minimum(); |
| 398 | double maximum = mWcStats.maximum(); |
| 399 | double meanCycles = mHzStats.mean(); |
| 400 | double stddevCycles = mHzStats.stddev(); |
| 401 | double minCycles = mHzStats.minimum(); |
| 402 | double maxCycles = mHzStats.maximum(); |
| 403 | mCpuUsage.resetElapsed(); |
| 404 | mWcStats.reset(); |
| 405 | mHzStats.reset(); |
| 406 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 407 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 408 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 409 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 410 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 411 | title.string(), |
| 412 | elapsed * .000000001, n, perLoop * .000001, |
| 413 | mean * .001, |
| 414 | stddev * .001, |
| 415 | minimum * .001, |
| 416 | maximum * .001, |
| 417 | mean / perLoop100, |
| 418 | stddev / perLoop100, |
| 419 | minimum / perLoop100, |
| 420 | maximum / perLoop100, |
| 421 | meanCycles / perLoop1k, |
| 422 | stddevCycles / perLoop1k, |
| 423 | minCycles / perLoop1k, |
| 424 | maxCycles / perLoop1k); |
| 425 | |
| 426 | } |
| 427 | } |
| 428 | #endif |
| 429 | }; |
| 430 | |
| 431 | // ---------------------------------------------------------------------------- |
| 432 | // ThreadBase |
| 433 | // ---------------------------------------------------------------------------- |
| 434 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 435 | // static |
| 436 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 437 | { |
| 438 | switch (type) { |
| 439 | case MIXER: |
| 440 | return "MIXER"; |
| 441 | case DIRECT: |
| 442 | return "DIRECT"; |
| 443 | case DUPLICATING: |
| 444 | return "DUPLICATING"; |
| 445 | case RECORD: |
| 446 | return "RECORD"; |
| 447 | case OFFLOAD: |
| 448 | return "OFFLOAD"; |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 449 | case MMAP: |
| 450 | return "MMAP"; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 451 | default: |
| 452 | return "unknown"; |
| 453 | } |
| 454 | } |
| 455 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 456 | std::string devicesToString(audio_devices_t devices) |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 457 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 458 | std::string result; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 459 | if (devices & AUDIO_DEVICE_BIT_IN) { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 460 | InputDeviceConverter::maskToString(devices, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 461 | } else { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 462 | OutputDeviceConverter::maskToString(devices, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 467 | std::string inputFlagsToString(audio_input_flags_t flags) |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 468 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 469 | std::string result; |
| 470 | InputFlagConverter::maskToString(flags, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 471 | return result; |
| 472 | } |
| 473 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 474 | std::string outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 475 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 476 | std::string result; |
| 477 | OutputFlagConverter::maskToString(flags, result); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 478 | return result; |
| 479 | } |
| 480 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 481 | const char *sourceToString(audio_source_t source) |
| 482 | { |
| 483 | switch (source) { |
| 484 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 485 | case AUDIO_SOURCE_MIC: return "mic"; |
| 486 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 487 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 488 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 489 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 490 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 491 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 492 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 493 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 494 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 495 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 496 | default: return "unknown"; |
| 497 | } |
| 498 | } |
| 499 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 500 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 501 | 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] | 502 | : Thread(false /*canCallJava*/), |
| 503 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 504 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 505 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 506 | // are set by PlaybackThread::readOutputParameters_l() or |
| 507 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 508 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 509 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 510 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 511 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 512 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 513 | mDeathRecipient(new PMDeathRecipient(this)), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 514 | mSystemReady(systemReady), |
| 515 | mSignalPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 516 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 517 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | AudioFlinger::ThreadBase::~ThreadBase() |
| 521 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 522 | // 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] | 523 | mConfigEvents.clear(); |
| 524 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 525 | // do not lock the mutex in destructor |
| 526 | releaseWakeLock_l(); |
| 527 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 528 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 529 | binder->unlinkToDeath(mDeathRecipient); |
| 530 | } |
| 531 | } |
| 532 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 533 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 534 | { |
| 535 | status_t status = initCheck(); |
| 536 | if (status == NO_ERROR) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 537 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 538 | } else { |
| 539 | ALOGE("No working audio driver found."); |
| 540 | } |
| 541 | return status; |
| 542 | } |
| 543 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 544 | void AudioFlinger::ThreadBase::exit() |
| 545 | { |
| 546 | ALOGV("ThreadBase::exit"); |
| 547 | // do any cleanup required for exit to succeed |
| 548 | preExit(); |
| 549 | { |
| 550 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 551 | // if (!exitPending()) { |
| 552 | // // context switch from here to exit() |
| 553 | // // exit() calls requestExit(), what exitPending() observes |
| 554 | // // exit() calls signal(), which is dropped since no waiters |
| 555 | // // context switch back from exit() to here |
| 556 | // mWaitWorkCV.wait(...); |
| 557 | // // now thread is hung |
| 558 | // } |
| 559 | AutoMutex lock(mLock); |
| 560 | requestExit(); |
| 561 | mWaitWorkCV.broadcast(); |
| 562 | } |
| 563 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 564 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 565 | requestExitAndWait(); |
| 566 | } |
| 567 | |
| 568 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 569 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 570 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 571 | Mutex::Autolock _l(mLock); |
| 572 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 573 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 574 | } |
| 575 | |
| 576 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 577 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 578 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 579 | { |
| 580 | status_t status = NO_ERROR; |
| 581 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 582 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 583 | event->mWaitStatus = false; |
| 584 | mPendingConfigEvents.add(event); |
| 585 | return status; |
| 586 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 587 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 588 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 589 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 590 | mLock.unlock(); |
| 591 | { |
| 592 | Mutex::Autolock _l(event->mLock); |
| 593 | while (event->mWaitStatus) { |
| 594 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 595 | event->mStatus = TIMED_OUT; |
| 596 | event->mWaitStatus = false; |
| 597 | } |
| 598 | } |
| 599 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 600 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 601 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 602 | return status; |
| 603 | } |
| 604 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 605 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 606 | { |
| 607 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 608 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 612 | 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] | 613 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 614 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 615 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 618 | 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] | 619 | { |
| 620 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 621 | sendPrioConfigEvent_l(pid, tid, prio, forApp); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 624 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 625 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l( |
| 626 | pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 627 | { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 628 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 629 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 632 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 633 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 634 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 635 | sp<ConfigEvent> configEvent; |
| 636 | AudioParameter param(keyValuePair); |
| 637 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 638 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 639 | setMasterMono_l(value != 0); |
| 640 | if (param.size() == 1) { |
| 641 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 642 | } |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 643 | param.remove(String8(AudioParameter::keyMonoOutput)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 644 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 645 | } else { |
| 646 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 647 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 648 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 651 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 652 | const struct audio_patch *patch, |
| 653 | audio_patch_handle_t *handle) |
| 654 | { |
| 655 | Mutex::Autolock _l(mLock); |
| 656 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 657 | status_t status = sendConfigEvent_l(configEvent); |
| 658 | if (status == NO_ERROR) { |
| 659 | CreateAudioPatchConfigEventData *data = |
| 660 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 661 | *handle = data->mHandle; |
| 662 | } |
| 663 | return status; |
| 664 | } |
| 665 | |
| 666 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 667 | const audio_patch_handle_t handle) |
| 668 | { |
| 669 | Mutex::Autolock _l(mLock); |
| 670 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 671 | return sendConfigEvent_l(configEvent); |
| 672 | } |
| 673 | |
| 674 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 675 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 676 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 677 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 678 | bool configChanged = false; |
| 679 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 680 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 681 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 682 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 683 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 684 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 685 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 686 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 687 | // FIXME Need to understand why this has to be done asynchronously |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 688 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 689 | true /*asynchronous*/); |
| 690 | if (err != 0) { |
| 691 | 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] | 692 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 693 | } |
| 694 | } break; |
| 695 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 696 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 697 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 698 | } break; |
| 699 | case CFG_EVENT_SET_PARAMETER: { |
| 700 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 701 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 702 | configChanged = true; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 703 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", |
| 704 | data->mKeyValuePairs.string()); |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 705 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 706 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 707 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 708 | const audio_devices_t oldDevice = getDevice(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 709 | CreateAudioPatchConfigEventData *data = |
| 710 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 711 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 712 | const audio_devices_t newDevice = getDevice(); |
| 713 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", |
| 714 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), |
| 715 | (unsigned)newDevice, devicesToString(newDevice).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 716 | } break; |
| 717 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 718 | const audio_devices_t oldDevice = getDevice(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 719 | ReleaseAudioPatchConfigEventData *data = |
| 720 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 721 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 722 | const audio_devices_t newDevice = getDevice(); |
| 723 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", |
| 724 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), |
| 725 | (unsigned)newDevice, devicesToString(newDevice).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 726 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 727 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 728 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 729 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 730 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 731 | { |
| 732 | Mutex::Autolock _l(event->mLock); |
| 733 | if (event->mWaitStatus) { |
| 734 | event->mWaitStatus = false; |
| 735 | event->mCond.signal(); |
| 736 | } |
| 737 | } |
| 738 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 739 | } |
| 740 | |
| 741 | if (configChanged) { |
| 742 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 746 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 747 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 748 | const audio_channel_representation_t representation = |
| 749 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 750 | |
| 751 | switch (representation) { |
| 752 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 753 | if (output) { |
| 754 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 755 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 756 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 757 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 758 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 759 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 760 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 761 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 762 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 763 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 764 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 765 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 766 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 767 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 768 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 769 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 770 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 771 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 772 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 773 | } else { |
| 774 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 775 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 776 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 777 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 778 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 779 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 780 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 781 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 782 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 783 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 784 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 785 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 786 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 787 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 788 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 789 | } |
| 790 | const int len = s.length(); |
| 791 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 792 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 793 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 794 | } |
| 795 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 796 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 797 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 798 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 799 | return s; |
| 800 | default: |
| 801 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 802 | representation, audio_channel_mask_get_bits(mask)); |
| 803 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 804 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 805 | } |
| 806 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 807 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 808 | { |
| 809 | const size_t SIZE = 256; |
| 810 | char buffer[SIZE]; |
| 811 | String8 result; |
| 812 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 813 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", |
| 814 | this, mThreadName, getTid(), type(), threadTypeToString(type())); |
| 815 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 816 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 817 | if (!locked) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 818 | dprintf(fd, " Thread may be deadlocked\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 821 | dprintf(fd, " I/O handle: %d\n", mId); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 822 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 823 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 824 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 825 | 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] | 826 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 827 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 828 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 829 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 830 | 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] | 831 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 832 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 833 | size_t numConfig = mConfigEvents.size(); |
| 834 | if (numConfig) { |
| 835 | for (size_t i = 0; i < numConfig; i++) { |
| 836 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 837 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 838 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 839 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 840 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 841 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 842 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 843 | // 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] | 844 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).c_str()); |
| 845 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).c_str()); |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 846 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 847 | |
| 848 | if (locked) { |
| 849 | mLock.unlock(); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 854 | { |
| 855 | const size_t SIZE = 256; |
| 856 | char buffer[SIZE]; |
| 857 | String8 result; |
| 858 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 859 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 860 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 861 | write(fd, buffer, strlen(buffer)); |
| 862 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 863 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 864 | sp<EffectChain> chain = mEffectChains[i]; |
| 865 | if (chain != 0) { |
| 866 | chain->dump(fd, args); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 871 | void AudioFlinger::ThreadBase::acquireWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 | { |
| 873 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 874 | acquireWakeLock_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 877 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 878 | { |
| 879 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 880 | case MIXER: |
| 881 | return String16("AudioMix"); |
| 882 | case DIRECT: |
| 883 | return String16("AudioDirectOut"); |
| 884 | case DUPLICATING: |
| 885 | return String16("AudioDup"); |
| 886 | case RECORD: |
| 887 | return String16("AudioIn"); |
| 888 | case OFFLOAD: |
| 889 | return String16("AudioOffload"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 890 | case MMAP: |
| 891 | return String16("Mmap"); |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 892 | default: |
| 893 | ALOG_ASSERT(false); |
| 894 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 898 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 899 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 900 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 901 | if (mPowerManager != 0) { |
| 902 | sp<IBinder> binder = new BBinder(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 903 | // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids. |
| 904 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 905 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 906 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 907 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 908 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 909 | if (status == NO_ERROR) { |
| 910 | mWakeLockToken = binder; |
| 911 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 912 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 913 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 914 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 915 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 916 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 917 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 921 | { |
| 922 | Mutex::Autolock _l(mLock); |
| 923 | releaseWakeLock_l(); |
| 924 | } |
| 925 | |
| 926 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 927 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 928 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 929 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 930 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 931 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 932 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 933 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 934 | } |
| 935 | mWakeLockToken.clear(); |
| 936 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 940 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 941 | // use checkService() to avoid blocking if power service is not up yet |
| 942 | sp<IBinder> binder = |
| 943 | defaultServiceManager()->checkService(String16("power")); |
| 944 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 945 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 946 | } else { |
| 947 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 948 | binder->linkToDeath(mDeathRecipient); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 953 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 954 | getPowerManager_l(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 955 | |
| 956 | #if !LOG_NDEBUG |
| 957 | std::stringstream s; |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 958 | for (uid_t uid : uids) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 959 | s << uid << " "; |
| 960 | } |
| 961 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); |
| 962 | #endif |
| 963 | |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 964 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 965 | if (mSystemReady) { |
| 966 | ALOGE("no wake lock to update, but system ready!"); |
| 967 | } else { |
| 968 | ALOGW("no wake lock to update, system not ready yet"); |
| 969 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 970 | return; |
| 971 | } |
| 972 | if (mPowerManager != 0) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 973 | std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints |
| 974 | status_t status = mPowerManager->updateWakeLockUids( |
| 975 | mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(), |
| 976 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 977 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 981 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 982 | { |
| 983 | Mutex::Autolock _l(mLock); |
| 984 | releaseWakeLock_l(); |
| 985 | mPowerManager.clear(); |
| 986 | } |
| 987 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 988 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 989 | { |
| 990 | sp<ThreadBase> thread = mThread.promote(); |
| 991 | if (thread != 0) { |
| 992 | thread->clearPowerManager(); |
| 993 | } |
| 994 | ALOGW("power manager service died !!!"); |
| 995 | } |
| 996 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 997 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 998 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 999 | { |
| 1000 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1001 | if (chain != 0) { |
| 1002 | if (type != NULL) { |
| 1003 | chain->setEffectSuspended_l(type, suspend); |
| 1004 | } else { |
| 1005 | chain->setEffectSuspendedAll_l(suspend); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1010 | } |
| 1011 | |
| 1012 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1013 | { |
| 1014 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1015 | if (index < 0) { |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1020 | mSuspendedSessions.valueAt(index); |
| 1021 | |
| 1022 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1023 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1024 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1025 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1026 | chain->setEffectSuspendedAll_l(true); |
| 1027 | } else { |
| 1028 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1029 | desc->mType.timeLow); |
| 1030 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1037 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1038 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1039 | { |
| 1040 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1041 | |
| 1042 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1043 | |
| 1044 | if (suspend) { |
| 1045 | if (index >= 0) { |
| 1046 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1047 | } else { |
| 1048 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1049 | } |
| 1050 | } else { |
| 1051 | if (index < 0) { |
| 1052 | return; |
| 1053 | } |
| 1054 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | int key = EffectChain::kKeyForSuspendAll; |
| 1059 | if (type != NULL) { |
| 1060 | key = type->timeLow; |
| 1061 | } |
| 1062 | index = sessionEffects.indexOfKey(key); |
| 1063 | |
| 1064 | sp<SuspendedSessionDesc> desc; |
| 1065 | if (suspend) { |
| 1066 | if (index >= 0) { |
| 1067 | desc = sessionEffects.valueAt(index); |
| 1068 | } else { |
| 1069 | desc = new SuspendedSessionDesc(); |
| 1070 | if (type != NULL) { |
| 1071 | desc->mType = *type; |
| 1072 | } |
| 1073 | sessionEffects.add(key, desc); |
| 1074 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1075 | } |
| 1076 | desc->mRefCount++; |
| 1077 | } else { |
| 1078 | if (index < 0) { |
| 1079 | return; |
| 1080 | } |
| 1081 | desc = sessionEffects.valueAt(index); |
| 1082 | if (--desc->mRefCount == 0) { |
| 1083 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1084 | sessionEffects.removeItemsAt(index); |
| 1085 | if (sessionEffects.isEmpty()) { |
| 1086 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1087 | sessionId); |
| 1088 | mSuspendedSessions.removeItem(sessionId); |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | if (!sessionEffects.isEmpty()) { |
| 1093 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1098 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1099 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1100 | { |
| 1101 | Mutex::Autolock _l(mLock); |
| 1102 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1103 | } |
| 1104 | |
| 1105 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1106 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1107 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1108 | { |
| 1109 | if (mType != RECORD) { |
| 1110 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1111 | // another session. This gives the priority to well behaved effect control panels |
| 1112 | // and applications not using global effects. |
| 1113 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1114 | // global effects |
| 1115 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1116 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1121 | if (chain != 0) { |
| 1122 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1123 | } |
| 1124 | } |
| 1125 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1126 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1127 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( |
| 1128 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1129 | { |
| 1130 | // No global effect sessions on record threads |
| 1131 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1132 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1133 | desc->name, mThreadName); |
| 1134 | return BAD_VALUE; |
| 1135 | } |
| 1136 | // only pre processing effects on record thread |
| 1137 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1138 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1139 | desc->name, mThreadName); |
| 1140 | return BAD_VALUE; |
| 1141 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1142 | |
| 1143 | // always allow effects without processing load or latency |
| 1144 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1145 | return NO_ERROR; |
| 1146 | } |
| 1147 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1148 | audio_input_flags_t flags = mInput->flags; |
| 1149 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1150 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1151 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1152 | desc->name, mThreadName); |
| 1153 | return BAD_VALUE; |
| 1154 | } |
| 1155 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1156 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1157 | desc->name, mThreadName); |
| 1158 | return BAD_VALUE; |
| 1159 | } |
| 1160 | } |
| 1161 | return NO_ERROR; |
| 1162 | } |
| 1163 | |
| 1164 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1165 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( |
| 1166 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1167 | { |
| 1168 | // no preprocessing on playback threads |
| 1169 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1170 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" |
| 1171 | " thread %s", desc->name, mThreadName); |
| 1172 | return BAD_VALUE; |
| 1173 | } |
| 1174 | |
Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1175 | // always allow effects without processing load or latency |
| 1176 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1177 | return NO_ERROR; |
| 1178 | } |
| 1179 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1180 | switch (mType) { |
| 1181 | case MIXER: { |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1182 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1183 | // Reject any effect on mixer multichannel sinks. |
| 1184 | // TODO: fix both format and multichannel issues with effects. |
| 1185 | if (mChannelCount != FCC_2) { |
| 1186 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" |
| 1187 | " thread %s", desc->name, mChannelCount, mThreadName); |
| 1188 | return BAD_VALUE; |
| 1189 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1190 | #endif |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1191 | audio_output_flags_t flags = mOutput->flags; |
| 1192 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1193 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1194 | // global effects are applied only to non fast tracks if they are SW |
| 1195 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1196 | break; |
| 1197 | } |
| 1198 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1199 | // only post processing on output stage session |
| 1200 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1201 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1202 | " on output stage session", desc->name); |
| 1203 | return BAD_VALUE; |
| 1204 | } |
| 1205 | } else { |
| 1206 | // no restriction on effects applied on non fast tracks |
| 1207 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1211 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1212 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
| 1213 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", |
| 1214 | desc->name); |
| 1215 | return BAD_VALUE; |
| 1216 | } |
| 1217 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1218 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" |
| 1219 | " in fast mode", desc->name); |
| 1220 | return BAD_VALUE; |
| 1221 | } |
| 1222 | } |
| 1223 | } break; |
| 1224 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1225 | // nothing actionable on offload threads, if the effect: |
| 1226 | // - is offloadable: the effect can be created |
| 1227 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1228 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1229 | break; |
| 1230 | case DIRECT: |
| 1231 | // Reject any effect on Direct output threads for now, since the format of |
| 1232 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1233 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", |
| 1234 | desc->name, mThreadName); |
| 1235 | return BAD_VALUE; |
| 1236 | case DUPLICATING: |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1237 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1238 | // Reject any effect on mixer multichannel sinks. |
| 1239 | // TODO: fix both format and multichannel issues with effects. |
| 1240 | if (mChannelCount != FCC_2) { |
| 1241 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" |
| 1242 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); |
| 1243 | return BAD_VALUE; |
| 1244 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1245 | #endif |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1246 | if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) { |
| 1247 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" |
| 1248 | " thread %s", desc->name, mThreadName); |
| 1249 | return BAD_VALUE; |
| 1250 | } |
| 1251 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 1252 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" |
| 1253 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1254 | return BAD_VALUE; |
| 1255 | } |
| 1256 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1257 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" |
| 1258 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1259 | return BAD_VALUE; |
| 1260 | } |
| 1261 | break; |
| 1262 | default: |
| 1263 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1264 | } |
| 1265 | |
| 1266 | return NO_ERROR; |
| 1267 | } |
| 1268 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1269 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1270 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1271 | const sp<AudioFlinger::Client>& client, |
| 1272 | const sp<IEffectClient>& effectClient, |
| 1273 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1274 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1275 | effect_descriptor_t *desc, |
| 1276 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1277 | status_t *status, |
| 1278 | bool pinned) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1279 | { |
| 1280 | sp<EffectModule> effect; |
| 1281 | sp<EffectHandle> handle; |
| 1282 | status_t lStatus; |
| 1283 | sp<EffectChain> chain; |
| 1284 | bool chainCreated = false; |
| 1285 | bool effectCreated = false; |
| 1286 | bool effectRegistered = false; |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1287 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1288 | |
| 1289 | lStatus = initCheck(); |
| 1290 | if (lStatus != NO_ERROR) { |
| 1291 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1292 | goto Exit; |
| 1293 | } |
| 1294 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1295 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1296 | |
| 1297 | { // scope for mLock |
| 1298 | Mutex::Autolock _l(mLock); |
| 1299 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1300 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
| 1301 | if (lStatus != NO_ERROR) { |
| 1302 | goto Exit; |
| 1303 | } |
| 1304 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1305 | // check for existing effect chain with the requested audio session |
| 1306 | chain = getEffectChain_l(sessionId); |
| 1307 | if (chain == 0) { |
| 1308 | // create a new chain for this session |
| 1309 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1310 | chain = new EffectChain(this, sessionId); |
| 1311 | addEffectChain_l(chain); |
| 1312 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1313 | chainCreated = true; |
| 1314 | } else { |
| 1315 | effect = chain->getEffectFromDesc_l(desc); |
| 1316 | } |
| 1317 | |
| 1318 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1319 | |
| 1320 | if (effect == 0) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1321 | effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1322 | // Check CPU and memory usage |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1323 | lStatus = AudioSystem::registerEffect( |
| 1324 | desc, mId, chain->strategy(), sessionId, effectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1325 | if (lStatus != NO_ERROR) { |
| 1326 | goto Exit; |
| 1327 | } |
| 1328 | effectRegistered = true; |
| 1329 | // create a new effect module if none present in the chain |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1330 | lStatus = chain->createEffect_l(effect, this, desc, effectId, sessionId, pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1331 | if (lStatus != NO_ERROR) { |
| 1332 | goto Exit; |
| 1333 | } |
| 1334 | effectCreated = true; |
| 1335 | |
| 1336 | effect->setDevice(mOutDevice); |
| 1337 | effect->setDevice(mInDevice); |
| 1338 | effect->setMode(mAudioFlinger->getMode()); |
| 1339 | effect->setAudioSource(mAudioSource); |
| 1340 | } |
| 1341 | // create effect handle and connect it to effect module |
| 1342 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1343 | lStatus = handle->initCheck(); |
| 1344 | if (lStatus == OK) { |
| 1345 | lStatus = effect->addHandle(handle.get()); |
| 1346 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1347 | if (enabled != NULL) { |
| 1348 | *enabled = (int)effect->isEnabled(); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | Exit: |
| 1353 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1354 | Mutex::Autolock _l(mLock); |
| 1355 | if (effectCreated) { |
| 1356 | chain->removeEffect_l(effect); |
| 1357 | } |
| 1358 | if (effectRegistered) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1359 | AudioSystem::unregisterEffect(effectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1360 | } |
| 1361 | if (chainCreated) { |
| 1362 | removeEffectChain_l(chain); |
| 1363 | } |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 1364 | // handle must be cleared by caller to avoid deadlock. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1367 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1368 | return handle; |
| 1369 | } |
| 1370 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1371 | void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle, |
| 1372 | bool unpinIfLast) |
| 1373 | { |
| 1374 | bool remove = false; |
| 1375 | sp<EffectModule> effect; |
| 1376 | { |
| 1377 | Mutex::Autolock _l(mLock); |
| 1378 | |
| 1379 | effect = handle->effect().promote(); |
| 1380 | if (effect == 0) { |
| 1381 | return; |
| 1382 | } |
| 1383 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 1384 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 1385 | if (remove) { |
| 1386 | removeEffect_l(effect, true); |
| 1387 | } |
| 1388 | } |
| 1389 | if (remove) { |
| 1390 | mAudioFlinger->updateOrphanEffectChains(effect); |
| 1391 | AudioSystem::unregisterEffect(effect->id()); |
| 1392 | if (handle->enabled()) { |
| 1393 | checkSuspendOnEffectEnabled(effect, false, effect->sessionId()); |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1398 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1399 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1400 | { |
| 1401 | Mutex::Autolock _l(mLock); |
| 1402 | return getEffect_l(sessionId, effectId); |
| 1403 | } |
| 1404 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1405 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1406 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1407 | { |
| 1408 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1409 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1410 | } |
| 1411 | |
| 1412 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1413 | // PlaybackThread::mLock held |
| 1414 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1415 | { |
| 1416 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1417 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1418 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1419 | bool chainCreated = false; |
| 1420 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1421 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1422 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %#x", |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1423 | this, effect->desc().name, effect->desc().flags); |
| 1424 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1425 | if (chain == 0) { |
| 1426 | // create a new chain for this session |
| 1427 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1428 | chain = new EffectChain(this, sessionId); |
| 1429 | addEffectChain_l(chain); |
| 1430 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1431 | chainCreated = true; |
| 1432 | } |
| 1433 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1434 | |
| 1435 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1436 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1437 | this, effect->desc().name, chain.get()); |
| 1438 | return BAD_VALUE; |
| 1439 | } |
| 1440 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1441 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1442 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1443 | status_t status = chain->addEffect_l(effect); |
| 1444 | if (status != NO_ERROR) { |
| 1445 | if (chainCreated) { |
| 1446 | removeEffectChain_l(chain); |
| 1447 | } |
| 1448 | return status; |
| 1449 | } |
| 1450 | |
| 1451 | effect->setDevice(mOutDevice); |
| 1452 | effect->setDevice(mInDevice); |
| 1453 | effect->setMode(mAudioFlinger->getMode()); |
| 1454 | effect->setAudioSource(mAudioSource); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1455 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1456 | return NO_ERROR; |
| 1457 | } |
| 1458 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1459 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1460 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1461 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1462 | effect_descriptor_t desc = effect->desc(); |
| 1463 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1464 | detachAuxEffect_l(effect->id()); |
| 1465 | } |
| 1466 | |
| 1467 | sp<EffectChain> chain = effect->chain().promote(); |
| 1468 | if (chain != 0) { |
| 1469 | // remove effect chain if removing last effect |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1470 | if (chain->removeEffect_l(effect, release) == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1471 | removeEffectChain_l(chain); |
| 1472 | } |
| 1473 | } else { |
| 1474 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1479 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1480 | { |
| 1481 | effectChains = mEffectChains; |
| 1482 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1483 | mEffectChains[i]->lock(); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1488 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1489 | { |
| 1490 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1491 | effectChains[i]->unlock(); |
| 1492 | } |
| 1493 | } |
| 1494 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1495 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1496 | { |
| 1497 | Mutex::Autolock _l(mLock); |
| 1498 | return getEffectChain_l(sessionId); |
| 1499 | } |
| 1500 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1501 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1502 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1503 | { |
| 1504 | size_t size = mEffectChains.size(); |
| 1505 | for (size_t i = 0; i < size; i++) { |
| 1506 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1507 | return mEffectChains[i]; |
| 1508 | } |
| 1509 | } |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1514 | { |
| 1515 | Mutex::Autolock _l(mLock); |
| 1516 | size_t size = mEffectChains.size(); |
| 1517 | for (size_t i = 0; i < size; i++) { |
| 1518 | mEffectChains[i]->setMode_l(mode); |
| 1519 | } |
| 1520 | } |
| 1521 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1522 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1523 | { |
| 1524 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1525 | config->ext.mix.handle = mId; |
| 1526 | config->sample_rate = mSampleRate; |
| 1527 | config->format = mFormat; |
| 1528 | config->channel_mask = mChannelMask; |
| 1529 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1530 | AUDIO_PORT_CONFIG_FORMAT; |
| 1531 | } |
| 1532 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1533 | void AudioFlinger::ThreadBase::systemReady() |
| 1534 | { |
| 1535 | Mutex::Autolock _l(mLock); |
| 1536 | if (mSystemReady) { |
| 1537 | return; |
| 1538 | } |
| 1539 | mSystemReady = true; |
| 1540 | |
| 1541 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1542 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1543 | } |
| 1544 | mPendingConfigEvents.clear(); |
| 1545 | } |
| 1546 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1547 | template <typename T> |
| 1548 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) { |
| 1549 | ssize_t index = mActiveTracks.indexOf(track); |
| 1550 | if (index >= 0) { |
| 1551 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); |
| 1552 | return index; |
| 1553 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1554 | logTrack("add", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1555 | mActiveTracksGeneration++; |
| 1556 | mLatestActiveTrack = track; |
| 1557 | ++mBatteryCounter[track->uid()].second; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1558 | mHasChanged = true; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1559 | return mActiveTracks.add(track); |
| 1560 | } |
| 1561 | |
| 1562 | template <typename T> |
| 1563 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) { |
| 1564 | ssize_t index = mActiveTracks.remove(track); |
| 1565 | if (index < 0) { |
| 1566 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); |
| 1567 | return index; |
| 1568 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1569 | logTrack("remove", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1570 | mActiveTracksGeneration++; |
| 1571 | --mBatteryCounter[track->uid()].second; |
| 1572 | // mLatestActiveTrack is not cleared even if is the same as track. |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1573 | mHasChanged = true; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1574 | return index; |
| 1575 | } |
| 1576 | |
| 1577 | template <typename T> |
| 1578 | void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() { |
| 1579 | for (const sp<T> &track : mActiveTracks) { |
| 1580 | BatteryNotifier::getInstance().noteStopAudio(track->uid()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1581 | logTrack("clear", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1582 | } |
| 1583 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1584 | if (!mActiveTracks.empty()) { mHasChanged = true; } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1585 | mActiveTracks.clear(); |
| 1586 | mLatestActiveTrack.clear(); |
| 1587 | mBatteryCounter.clear(); |
| 1588 | } |
| 1589 | |
| 1590 | template <typename T> |
| 1591 | void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState( |
| 1592 | sp<ThreadBase> thread, bool force) { |
| 1593 | // Updates ActiveTracks client uids to the thread wakelock. |
| 1594 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { |
| 1595 | thread->updateWakeLockUids_l(getWakeLockUids()); |
| 1596 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1597 | } |
| 1598 | |
| 1599 | // Updates BatteryNotifier uids |
| 1600 | for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) { |
| 1601 | const uid_t uid = it->first; |
| 1602 | ssize_t &previous = it->second.first; |
| 1603 | ssize_t ¤t = it->second.second; |
| 1604 | if (current > 0) { |
| 1605 | if (previous == 0) { |
| 1606 | BatteryNotifier::getInstance().noteStartAudio(uid); |
| 1607 | } |
| 1608 | previous = current; |
| 1609 | ++it; |
| 1610 | } else if (current == 0) { |
| 1611 | if (previous > 0) { |
| 1612 | BatteryNotifier::getInstance().noteStopAudio(uid); |
| 1613 | } |
| 1614 | it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase. |
| 1615 | } else /* (current < 0) */ { |
| 1616 | LOG_ALWAYS_FATAL("negative battery count %zd", current); |
| 1617 | } |
| 1618 | } |
| 1619 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1620 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1621 | template <typename T> |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1622 | bool AudioFlinger::ThreadBase::ActiveTracks<T>::readAndClearHasChanged() { |
| 1623 | const bool hasChanged = mHasChanged; |
| 1624 | mHasChanged = false; |
| 1625 | return hasChanged; |
| 1626 | } |
| 1627 | |
| 1628 | template <typename T> |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1629 | void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack( |
| 1630 | const char *funcName, const sp<T> &track) const { |
| 1631 | if (mLocalLog != nullptr) { |
| 1632 | String8 result; |
| 1633 | track->appendDump(result, false /* active */); |
| 1634 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string()); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1638 | void AudioFlinger::ThreadBase::broadcast_l() |
| 1639 | { |
| 1640 | // Thread could be blocked waiting for async |
| 1641 | // so signal it to handle state changes immediately |
| 1642 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1643 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1644 | mSignalPending = true; |
| 1645 | mWaitWorkCV.broadcast(); |
| 1646 | } |
| 1647 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1648 | // ---------------------------------------------------------------------------- |
| 1649 | // Playback |
| 1650 | // ---------------------------------------------------------------------------- |
| 1651 | |
| 1652 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1653 | AudioStreamOut* output, |
| 1654 | audio_io_handle_t id, |
| 1655 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1656 | type_t type, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1657 | bool systemReady) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1658 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1659 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1660 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1661 | mMixerBuffer(NULL), |
| 1662 | mMixerBufferSize(0), |
| 1663 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1664 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1665 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1666 | mEffectBuffer(NULL), |
| 1667 | mEffectBufferSize(0), |
| 1668 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1669 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1670 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1671 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1672 | mSuspendedFrames(0), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1673 | mActiveTracks(&this->mLocalLog), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1674 | // mStreamTypes[] initialized in constructor body |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1675 | mTracks(type == MIXER), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1676 | mOutput(output), |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1677 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1678 | mMixerStatus(MIXER_IDLE), |
| 1679 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1680 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1681 | mBytesRemaining(0), |
| 1682 | mCurrentWriteLength(0), |
| 1683 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1684 | mWriteAckSequence(0), |
| 1685 | mDrainSequence(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1686 | mScreenState(AudioFlinger::mScreenState), |
| 1687 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1688 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 1689 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
| 1690 | mLeftVolFloat(-1.0), mRightVolFloat(-1.0) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1691 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1692 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1693 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1694 | |
| 1695 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1696 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1697 | // parameter. |
| 1698 | // |
| 1699 | // If the HAL we are using has support for master volume or master mute, |
| 1700 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1701 | // and the mute set to false). |
| 1702 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1703 | mMasterMute = audioFlinger->masterMute_l(); |
| 1704 | if (mOutput && mOutput->audioHwDev) { |
| 1705 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1706 | mMasterVolume = 1.0; |
| 1707 | } |
| 1708 | |
| 1709 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1710 | mMasterMute = false; |
| 1711 | } |
| 1712 | } |
| 1713 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1714 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1715 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1716 | // ++ operator does not compile |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1717 | for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_FOR_POLICY_CNT; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1718 | stream = (audio_stream_type_t) (stream + 1)) { |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1719 | mStreamTypes[stream].volume = 0.0f; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1720 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1721 | } |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1722 | // Audio patch volume is always max |
| 1723 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 1724 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1728 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1729 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1730 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1731 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1732 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1736 | { |
| 1737 | dumpInternals(fd, args); |
| 1738 | dumpTracks(fd, args); |
| 1739 | dumpEffectChains(fd, args); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1740 | dprintf(fd, " Local log:\n"); |
| 1741 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1742 | } |
| 1743 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1744 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1745 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1746 | String8 result; |
| 1747 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1748 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1749 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1750 | const stream_type_t *st = &mStreamTypes[i]; |
| 1751 | if (i > 0) { |
| 1752 | result.appendFormat(", "); |
| 1753 | } |
| 1754 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1755 | if (st->mute) { |
| 1756 | result.append("M"); |
| 1757 | } |
| 1758 | } |
| 1759 | result.append("\n"); |
| 1760 | write(fd, result.string(), result.length()); |
| 1761 | result.clear(); |
| 1762 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1763 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1764 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1765 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1766 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1767 | |
| 1768 | size_t numtracks = mTracks.size(); |
| 1769 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1770 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1771 | size_t numactiveseen = 0; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1772 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1773 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1774 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1775 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1776 | Track::appendDumpHeader(result); |
| 1777 | for (size_t i = 0; i < numtracks; ++i) { |
| 1778 | sp<Track> track = mTracks[i]; |
| 1779 | if (track != 0) { |
| 1780 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1781 | if (active) { |
| 1782 | numactiveseen++; |
| 1783 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1784 | result.append(prefix); |
| 1785 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | } else { |
| 1789 | result.append("\n"); |
| 1790 | } |
| 1791 | if (numactiveseen != numactive) { |
| 1792 | // some tracks in the active list were not in the tracks list |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1793 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1794 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1795 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1796 | Track::appendDumpHeader(result); |
| 1797 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1798 | sp<Track> track = mActiveTracks[i]; |
| 1799 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1800 | result.append(prefix); |
| 1801 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1810 | { |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1811 | dumpBase(fd, args); |
| 1812 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1813 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1814 | dprintf(fd, " Last write occurred (msecs): %llu\n", |
| 1815 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1816 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1817 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1818 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1819 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1820 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1821 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1822 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1823 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1824 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1825 | AudioStreamOut *output = mOutput; |
| 1826 | 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] | 1827 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", |
| 1828 | output, flags, outputFlagsToString(flags).c_str()); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 1829 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 1830 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 1831 | if (mPipeSink.get() != nullptr) { |
| 1832 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 1833 | } |
| 1834 | if (output != nullptr) { |
| 1835 | dprintf(fd, " Hal stream dump:\n"); |
| 1836 | (void)output->stream->dump(fd); |
| 1837 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1841 | |
| 1842 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1843 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1844 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // ThreadBase virtuals |
| 1848 | void AudioFlinger::PlaybackThread::preExit() |
| 1849 | { |
| 1850 | ALOGV(" preExit()"); |
Mikhail Naganov | ad9c7e4 | 2018-03-05 12:25:58 -0800 | [diff] [blame] | 1851 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1852 | // converted to use audio HAL extensions required to support tunneling |
| 1853 | status_t result = mOutput->stream->setParameters(String8("exiting=1")); |
| 1854 | ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1858 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1859 | const sp<AudioFlinger::Client>& client, |
| 1860 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 1861 | const audio_attributes_t& attr, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1862 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1863 | audio_format_t format, |
| 1864 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1865 | size_t *pFrameCount, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1866 | size_t *pNotificationFrameCount, |
| 1867 | uint32_t notificationsPerBuffer, |
| 1868 | float speed, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1869 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1870 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1871 | audio_output_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1872 | pid_t tid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1873 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1874 | status_t *status, |
| 1875 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1876 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1877 | size_t frameCount = *pFrameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1878 | size_t notificationFrameCount = *pNotificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1879 | sp<Track> track; |
| 1880 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1881 | audio_output_flags_t outputFlags = mOutput->flags; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1882 | audio_output_flags_t requestedFlags = *flags; |
| 1883 | |
| 1884 | if (*pSampleRate == 0) { |
| 1885 | *pSampleRate = mSampleRate; |
| 1886 | } |
| 1887 | uint32_t sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1888 | |
| 1889 | // special case for FAST flag considered OK if fast mixer is present |
| 1890 | if (hasFastMixer()) { |
| 1891 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 1892 | } |
| 1893 | |
| 1894 | // Check if requested flags are compatible with output stream flags |
| 1895 | if ((*flags & outputFlags) != *flags) { |
| 1896 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 1897 | *flags, outputFlags); |
| 1898 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 1899 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1900 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1901 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1902 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1903 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1904 | // PCM data |
| 1905 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1906 | // TODO: extract as a data library function that checks that a computationally |
| 1907 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1908 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1909 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1910 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1911 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1912 | // hardware sample rate |
| 1913 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1914 | // normal mixer has an associated fast mixer |
| 1915 | hasFastMixer() && |
| 1916 | // there are sufficient fast track slots available |
| 1917 | (mFastTrackAvailMask != 0) |
| 1918 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1919 | // FIXME add a permission test also? |
| 1920 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1921 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 1922 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1923 | // read the fast track multiplier property the first time it is needed |
| 1924 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1925 | if (ok != 0) { |
| 1926 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1927 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1928 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1929 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1930 | |
| 1931 | // check compatibility with audio effects. |
| 1932 | { // scope for mLock |
| 1933 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 1934 | for (audio_session_t session : { |
| 1935 | AUDIO_SESSION_OUTPUT_STAGE, |
| 1936 | AUDIO_SESSION_OUTPUT_MIX, |
| 1937 | sessionId, |
| 1938 | }) { |
| 1939 | sp<EffectChain> chain = getEffectChain_l(session); |
| 1940 | if (chain.get() != nullptr) { |
| 1941 | audio_output_flags_t old = *flags; |
| 1942 | chain->checkOutputFlagCompatibility(flags); |
| 1943 | if (old != *flags) { |
| 1944 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 1945 | (int)session, (int)old, (int)*flags); |
| 1946 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1950 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1951 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 1952 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1953 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1954 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 1955 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1956 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1957 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1958 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1959 | audio_is_linear_pcm(format), |
| 1960 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1961 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1962 | } |
| 1963 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1964 | |
| 1965 | if (!audio_has_proportional_frames(format)) { |
| 1966 | if (sharedBuffer != 0) { |
| 1967 | // Same comment as below about ignoring frameCount parameter for set() |
| 1968 | frameCount = sharedBuffer->size(); |
| 1969 | } else if (frameCount == 0) { |
| 1970 | frameCount = mNormalFrameCount; |
| 1971 | } |
| 1972 | if (notificationFrameCount != frameCount) { |
| 1973 | notificationFrameCount = frameCount; |
| 1974 | } |
| 1975 | } else if (sharedBuffer != 0) { |
| 1976 | // FIXME: Ensure client side memory buffers need |
| 1977 | // not have additional alignment beyond sample |
| 1978 | // (e.g. 16 bit stereo accessed as 32 bit frame). |
| 1979 | size_t alignment = audio_bytes_per_sample(format); |
| 1980 | if (alignment & 1) { |
| 1981 | // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). |
| 1982 | alignment = 1; |
| 1983 | } |
| 1984 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1985 | size_t frameSize = channelCount * audio_bytes_per_sample(format); |
| 1986 | if (channelCount > 1) { |
| 1987 | // More than 2 channels does not require stronger alignment than stereo |
| 1988 | alignment <<= 1; |
| 1989 | } |
| 1990 | if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) { |
| 1991 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
| 1992 | sharedBuffer->pointer(), channelCount); |
| 1993 | lStatus = BAD_VALUE; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1994 | goto Exit; |
| 1995 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1996 | |
| 1997 | // When initializing a shared buffer AudioTrack via constructors, |
| 1998 | // there's no frameCount parameter. |
| 1999 | // But when initializing a shared buffer AudioTrack via set(), |
| 2000 | // there _is_ a frameCount parameter. We silently ignore it. |
| 2001 | frameCount = sharedBuffer->size() / frameSize; |
| 2002 | } else { |
| 2003 | size_t minFrameCount = 0; |
| 2004 | // For fast tracks we try to respect the application's request for notifications per buffer. |
| 2005 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2006 | if (notificationsPerBuffer > 0) { |
| 2007 | // Avoid possible arithmetic overflow during multiplication. |
| 2008 | if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { |
| 2009 | ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", |
| 2010 | notificationsPerBuffer, mFrameCount); |
| 2011 | } else { |
| 2012 | minFrameCount = mFrameCount * notificationsPerBuffer; |
| 2013 | } |
| 2014 | } |
| 2015 | } else { |
| 2016 | // For normal PCM streaming tracks, update minimum frame count. |
| 2017 | // Buffer depth is forced to be at least 2 x the normal mixer frame count and |
| 2018 | // cover audio hardware latency. |
| 2019 | // This is probably too conservative, but legacy application code may depend on it. |
| 2020 | // If you change this calculation, also review the start threshold which is related. |
| 2021 | uint32_t latencyMs = latency_l(); |
| 2022 | if (latencyMs == 0) { |
| 2023 | ALOGE("Error when retrieving output stream latency"); |
| 2024 | lStatus = UNKNOWN_ERROR; |
| 2025 | goto Exit; |
| 2026 | } |
| 2027 | |
| 2028 | minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, |
| 2029 | mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); |
| 2030 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2031 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2032 | if (frameCount < minFrameCount) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2033 | frameCount = minFrameCount; |
| 2034 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2035 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2036 | |
| 2037 | // Make sure that application is notified with sufficient margin before underrun. |
| 2038 | // The client can divide the AudioTrack buffer into sub-buffers, |
| 2039 | // and expresses its desire to server as the notification frame count. |
| 2040 | if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { |
| 2041 | size_t maxNotificationFrames; |
| 2042 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2043 | // notify every HAL buffer, regardless of the size of the track buffer |
| 2044 | maxNotificationFrames = mFrameCount; |
| 2045 | } else { |
| 2046 | // For normal tracks, use at least double-buffering if no sample rate conversion, |
| 2047 | // or at least triple-buffering if there is sample rate conversion |
| 2048 | const int nBuffering = sampleRate == mSampleRate ? 2 : 3; |
| 2049 | maxNotificationFrames = frameCount / nBuffering; |
| 2050 | // If client requested a fast track but this was denied, then use the smaller maximum. |
| 2051 | if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2052 | size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; |
| 2053 | if (maxNotificationFrames > maxNotificationFramesFastDenied) { |
| 2054 | maxNotificationFrames = maxNotificationFramesFastDenied; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 2059 | if (notificationFrameCount == 0) { |
| 2060 | ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", |
| 2061 | maxNotificationFrames, frameCount); |
| 2062 | } else { |
| 2063 | ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", |
| 2064 | notificationFrameCount, maxNotificationFrames, frameCount); |
| 2065 | } |
| 2066 | notificationFrameCount = maxNotificationFrames; |
| 2067 | } |
| 2068 | } |
| 2069 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2070 | *pFrameCount = frameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2071 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2072 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2073 | switch (mType) { |
| 2074 | |
| 2075 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2076 | 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] | 2077 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2078 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 2079 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2080 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2081 | lStatus = BAD_VALUE; |
| 2082 | goto Exit; |
| 2083 | } |
| 2084 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2085 | break; |
| 2086 | |
| 2087 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2088 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2089 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 2090 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2091 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2092 | lStatus = BAD_VALUE; |
| 2093 | goto Exit; |
| 2094 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2095 | break; |
| 2096 | |
| 2097 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2098 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2099 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 2100 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2101 | format, mOutput, mFormat); |
| 2102 | lStatus = BAD_VALUE; |
| 2103 | goto Exit; |
| 2104 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2105 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2106 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2107 | lStatus = BAD_VALUE; |
| 2108 | goto Exit; |
| 2109 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2110 | break; |
| 2111 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | lStatus = initCheck(); |
| 2115 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2116 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2117 | goto Exit; |
| 2118 | } |
| 2119 | |
| 2120 | { // scope for mLock |
| 2121 | Mutex::Autolock _l(mLock); |
| 2122 | |
| 2123 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2124 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2125 | // manager |
| 2126 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 2127 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2128 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2129 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2130 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 2131 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2132 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2133 | strategy, actual); |
| 2134 | lStatus = BAD_VALUE; |
| 2135 | goto Exit; |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2140 | track = new Track(this, client, streamType, attr, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2141 | channelMask, frameCount, |
| 2142 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2143 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT, portId); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2144 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2145 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2146 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2147 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2148 | // 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] | 2149 | goto Exit; |
| 2150 | } |
| 2151 | mTracks.add(track); |
| 2152 | |
| 2153 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 2154 | if (chain != 0) { |
| 2155 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2156 | track->setMainBuffer(chain->inBuffer()); |
| 2157 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 2158 | chain->incTrackCnt(); |
| 2159 | } |
| 2160 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2161 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2162 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2163 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2164 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2165 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | lStatus = NO_ERROR; |
| 2170 | |
| 2171 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2172 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2173 | return track; |
| 2174 | } |
| 2175 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2176 | template<typename T> |
| 2177 | ssize_t AudioFlinger::PlaybackThread::Tracks<T>::add(const sp<T> &track) |
| 2178 | { |
| 2179 | const ssize_t index = mTracks.add(track); |
| 2180 | if (index >= 0) { |
| 2181 | // set name for track when adding. |
| 2182 | int name; |
| 2183 | if (mUnusedTrackNames.empty()) { |
| 2184 | name = mTracks.size() - 1; // new name {0 ... size-1}. |
| 2185 | } else { |
| 2186 | // reuse smallest name for deleted track. |
| 2187 | auto it = mUnusedTrackNames.begin(); |
| 2188 | name = *it; |
| 2189 | (void)mUnusedTrackNames.erase(it); |
| 2190 | } |
| 2191 | track->setName(name); |
| 2192 | } else { |
| 2193 | LOG_ALWAYS_FATAL("cannot add track"); |
| 2194 | } |
| 2195 | return index; |
| 2196 | } |
| 2197 | |
| 2198 | template<typename T> |
| 2199 | ssize_t AudioFlinger::PlaybackThread::Tracks<T>::remove(const sp<T> &track) |
| 2200 | { |
| 2201 | const int name = track->name(); |
| 2202 | const ssize_t index = mTracks.remove(track); |
| 2203 | if (index >= 0) { |
| 2204 | // invalidate name when removing from mTracks. |
| 2205 | LOG_ALWAYS_FATAL_IF(name < 0, "invalid name %d for track on mTracks", name); |
| 2206 | |
| 2207 | if (mSaveDeletedTrackNames) { |
| 2208 | // We can't directly access mAudioMixer since the caller may be outside of threadLoop. |
| 2209 | // Instead, we add to mDeletedTrackNames which is solely used for mAudioMixer update, |
| 2210 | // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer. |
| 2211 | mDeletedTrackNames.emplace(name); |
| 2212 | } |
| 2213 | |
| 2214 | mUnusedTrackNames.emplace(name); |
| 2215 | track->setName(T::TRACK_NAME_PENDING); |
| 2216 | } else { |
| 2217 | LOG_ALWAYS_FATAL_IF(name >= 0, |
| 2218 | "valid name %d for track not in mTracks (returned %zd)", name, index); |
| 2219 | } |
| 2220 | return index; |
| 2221 | } |
| 2222 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2223 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 2224 | { |
| 2225 | return latency; |
| 2226 | } |
| 2227 | |
| 2228 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 2229 | { |
| 2230 | Mutex::Autolock _l(mLock); |
| 2231 | return latency_l(); |
| 2232 | } |
| 2233 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 2234 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2235 | uint32_t latency; |
| 2236 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { |
| 2237 | return correctLatency_l(latency); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2238 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2239 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 2243 | { |
| 2244 | Mutex::Autolock _l(mLock); |
| 2245 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2246 | if (mOutput && mOutput->audioHwDev && |
| 2247 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2248 | mMasterVolume = 1.0; |
| 2249 | } else { |
| 2250 | mMasterVolume = value; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 2255 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2256 | if (isDuplicating()) { |
| 2257 | return; |
| 2258 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2259 | Mutex::Autolock _l(mLock); |
| 2260 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2261 | if (mOutput && mOutput->audioHwDev && |
| 2262 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2263 | mMasterMute = false; |
| 2264 | } else { |
| 2265 | mMasterMute = muted; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2270 | { |
| 2271 | Mutex::Autolock _l(mLock); |
| 2272 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2273 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2277 | { |
| 2278 | Mutex::Autolock _l(mLock); |
| 2279 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2280 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2284 | { |
| 2285 | Mutex::Autolock _l(mLock); |
| 2286 | return mStreamTypes[stream].volume; |
| 2287 | } |
| 2288 | |
| 2289 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2290 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2291 | { |
| 2292 | status_t status = ALREADY_EXISTS; |
| 2293 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2294 | if (mActiveTracks.indexOf(track) < 0) { |
| 2295 | // the track is newly added, make sure it fills up all its |
| 2296 | // buffers before playing. This is to ensure the client will |
| 2297 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2298 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2299 | TrackBase::track_state state = track->mState; |
| 2300 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2301 | status = AudioSystem::startOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2302 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2303 | mLock.lock(); |
| 2304 | // abort track was stopped/paused while we released the lock |
| 2305 | if (state != track->mState) { |
| 2306 | if (status == NO_ERROR) { |
| 2307 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2308 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2309 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2310 | mLock.lock(); |
| 2311 | } |
| 2312 | return INVALID_OPERATION; |
| 2313 | } |
| 2314 | // abort if start is rejected by audio policy manager |
| 2315 | if (status != NO_ERROR) { |
| 2316 | return PERMISSION_DENIED; |
| 2317 | } |
| 2318 | #ifdef ADD_BATTERY_DATA |
| 2319 | // to track the speaker usage |
| 2320 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2321 | #endif |
| 2322 | } |
| 2323 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2324 | // set retry count for buffer fill |
| 2325 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2326 | if (track->isStopping_1()) { |
| 2327 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 2328 | } else { |
| 2329 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2330 | } |
| 2331 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2332 | } else { |
| 2333 | track->mRetryCount = kMaxTrackStartupRetries; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2334 | track->mFillingUpStatus = |
| 2335 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2338 | track->mResetDone = false; |
| 2339 | track->mPresentationCompleteFrames = 0; |
| 2340 | mActiveTracks.add(track); |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2341 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2342 | if (chain != 0) { |
| 2343 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2344 | track->sessionId()); |
| 2345 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | status = NO_ERROR; |
| 2349 | } |
| 2350 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2351 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2352 | return status; |
| 2353 | } |
| 2354 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2355 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2356 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2357 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2358 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2359 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2360 | track->mState = TrackBase::STOPPED; |
| 2361 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2362 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2363 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2364 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2365 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2366 | |
| 2367 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2371 | { |
| 2372 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2373 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2374 | String8 result; |
| 2375 | track->appendDump(result, false /* active */); |
| 2376 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2377 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2378 | mTracks.remove(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2379 | if (track->isFastTrack()) { |
| 2380 | int index = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2381 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2382 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2383 | mFastTrackAvailMask |= 1 << index; |
| 2384 | // redundant as track is about to be destroyed, for dumpsys only |
| 2385 | track->mFastIndex = -1; |
| 2386 | } |
| 2387 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2388 | if (chain != 0) { |
| 2389 | chain->decTrackCnt(); |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2394 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2395 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2396 | String8 out_s8; |
| 2397 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { |
| 2398 | return out_s8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2399 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2400 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2403 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2404 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2405 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2406 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2407 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2408 | |
| 2409 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2410 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2411 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2412 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2413 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2414 | desc->mChannelMask = mChannelMask; |
| 2415 | desc->mSamplingRate = mSampleRate; |
| 2416 | desc->mFormat = mFormat; |
| 2417 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2418 | // AudioFlinger::frameCount(audio_io_handle_t) |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2419 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2420 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2421 | break; |
| 2422 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2423 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2424 | default: |
| 2425 | break; |
| 2426 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2427 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2428 | } |
| 2429 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2430 | void AudioFlinger::PlaybackThread::onWriteReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2431 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2432 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2433 | } |
| 2434 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2435 | void AudioFlinger::PlaybackThread::onDrainReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2436 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2437 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2438 | } |
| 2439 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2440 | void AudioFlinger::PlaybackThread::onError() |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2441 | { |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2442 | mCallbackThread->setAsyncError(); |
| 2443 | } |
| 2444 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2445 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2446 | { |
| 2447 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2448 | // reject out of sequence requests |
| 2449 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2450 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2451 | mWaitWorkCV.signal(); |
| 2452 | } |
| 2453 | } |
| 2454 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2455 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2456 | { |
| 2457 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2458 | // reject out of sequence requests |
| 2459 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2460 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2461 | mWaitWorkCV.signal(); |
| 2462 | } |
| 2463 | } |
| 2464 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2465 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2466 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2467 | // 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] | 2468 | mSampleRate = mOutput->getSampleRate(); |
| 2469 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2470 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2471 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2472 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2473 | if ((mType == MIXER || mType == DUPLICATING) |
| 2474 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2475 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2476 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2477 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2478 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2479 | |
| 2480 | // Get actual HAL format. |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2481 | status_t result = mOutput->stream->getFormat(&mHALFormat); |
| 2482 | 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] | 2483 | // Get format from the shim, which will be different than the HAL format |
| 2484 | // if playing compressed audio over HDMI passthrough. |
| 2485 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2486 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2487 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2488 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2489 | if ((mType == MIXER || mType == DUPLICATING) |
| 2490 | && !isValidPcmSinkFormat(mFormat)) { |
| 2491 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2492 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2493 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2494 | mFrameSize = mOutput->getFrameSize(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2495 | result = mOutput->stream->getBufferSize(&mBufferSize); |
| 2496 | LOG_ALWAYS_FATAL_IF(result != OK, |
| 2497 | "Error when retrieving output stream buffer size: %d", result); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2498 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2499 | if (mFrameCount & 15) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2500 | 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] | 2501 | mFrameCount); |
| 2502 | } |
| 2503 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2504 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) { |
| 2505 | if (mOutput->stream->setCallback(this) == OK) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2506 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2507 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2511 | mHwSupportsPause = false; |
| 2512 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2513 | bool supportsPause = false, supportsResume = false; |
| 2514 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { |
| 2515 | if (supportsPause && supportsResume) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2516 | mHwSupportsPause = true; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2517 | } else if (supportsPause) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2518 | ALOGW("direct output implements pause but not resume"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2519 | } else if (supportsResume) { |
| 2520 | ALOGW("direct output implements resume but not pause"); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2521 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2522 | } |
| 2523 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2524 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2525 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2526 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2527 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2528 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2529 | // For best precision, we use float instead of the associated output |
| 2530 | // device format (typically PCM 16 bit). |
| 2531 | |
| 2532 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2533 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2534 | mBufferSize = mFrameSize * mFrameCount; |
| 2535 | |
| 2536 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2537 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2538 | // (if a valid mask) to avoid premature downmix. |
| 2539 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2540 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2541 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2542 | } |
| 2543 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2544 | // 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] | 2545 | double multiplier = 1.0; |
| 2546 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2547 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2548 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2549 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2550 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2551 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2552 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2553 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2554 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2555 | maxNormalFrameCount = minNormalFrameCount; |
| 2556 | } |
| 2557 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2558 | if (multiplier <= 1.0) { |
| 2559 | multiplier = 1.0; |
| 2560 | } else if (multiplier <= 2.0) { |
| 2561 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2562 | multiplier = 2.0; |
| 2563 | } else { |
| 2564 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2565 | } |
| 2566 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2567 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | mNormalFrameCount = multiplier * mFrameCount; |
| 2571 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2572 | if (mType == MIXER || mType == DUPLICATING) { |
| 2573 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2574 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2575 | 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] | 2576 | mNormalFrameCount); |
| 2577 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2578 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2579 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2580 | mThreadThrottleTimeMs = 0; |
| 2581 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2582 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2583 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2584 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2585 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2586 | free(mSinkBuffer); |
| 2587 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2588 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2589 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2590 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2591 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2592 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2593 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2594 | // drives the output. |
| 2595 | free(mMixerBuffer); |
| 2596 | mMixerBuffer = NULL; |
| 2597 | if (mMixerBufferEnabled) { |
| 2598 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2599 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2600 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2601 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2602 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2603 | free(mEffectBuffer); |
| 2604 | mEffectBuffer = NULL; |
| 2605 | if (mEffectBufferEnabled) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2606 | mEffectBufferFormat = EFFECT_BUFFER_FORMAT; |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2607 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2608 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2609 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2610 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2611 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2612 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2613 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2614 | // 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] | 2615 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2616 | // matter. |
| 2617 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2618 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2619 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2620 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2621 | } |
| 2622 | } |
| 2623 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2624 | void AudioFlinger::PlaybackThread::updateMetadata_l() |
| 2625 | { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2626 | if (mOutput == nullptr || mOutput->stream == nullptr ) { |
| 2627 | return; // That should not happen |
| 2628 | } |
| 2629 | bool hasChanged = mActiveTracks.readAndClearHasChanged(); |
| 2630 | for (const sp<Track> &track : mActiveTracks) { |
| 2631 | // Do not short-circuit as all hasChanged states must be reset |
| 2632 | // as all the metadata are going to be sent |
| 2633 | hasChanged |= track->readAndClearHasChanged(); |
| 2634 | } |
| 2635 | if (!hasChanged) { |
| 2636 | return; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2637 | } |
| 2638 | StreamOutHalInterface::SourceMetadata metadata; |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2639 | auto backInserter = std::back_inserter(metadata.tracks); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2640 | for (const sp<Track> &track : mActiveTracks) { |
| 2641 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2642 | track->copyMetadataTo(backInserter); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2643 | } |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2644 | sendMetadataToBackend_l(metadata); |
Kevin Rocard | 80ee272 | 2018-04-11 15:53:48 +0000 | [diff] [blame] | 2645 | } |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 2646 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2647 | void AudioFlinger::PlaybackThread::sendMetadataToBackend_l( |
| 2648 | const StreamOutHalInterface::SourceMetadata& metadata) |
| 2649 | { |
| 2650 | mOutput->stream->updateSourceMetadata(metadata); |
| 2651 | }; |
| 2652 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2653 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2654 | { |
| 2655 | if (halFrames == NULL || dspFrames == NULL) { |
| 2656 | return BAD_VALUE; |
| 2657 | } |
| 2658 | Mutex::Autolock _l(mLock); |
| 2659 | if (initCheck() != NO_ERROR) { |
| 2660 | return INVALID_OPERATION; |
| 2661 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2662 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2663 | *halFrames = framesWritten; |
| 2664 | |
| 2665 | if (isSuspended()) { |
| 2666 | // return an estimation of rendered frames when the output is suspended |
| 2667 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2668 | *dspFrames = (uint32_t) |
| 2669 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 | return NO_ERROR; |
| 2671 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2672 | status_t status; |
| 2673 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2674 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2675 | *dspFrames = (size_t)frames; |
| 2676 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2680 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 2681 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2682 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2683 | uint32_t result = 0; |
| 2684 | if (getEffectChain_l(sessionId) != 0) { |
| 2685 | result = EFFECT_SESSION; |
| 2686 | } |
| 2687 | |
| 2688 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2689 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2690 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2691 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2692 | if (track->isFastTrack()) { |
| 2693 | result |= FAST_SESSION; |
| 2694 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2695 | break; |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | return result; |
| 2700 | } |
| 2701 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2702 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2703 | { |
| 2704 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2705 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2706 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2707 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2708 | } |
| 2709 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2710 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2711 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2712 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2713 | } |
| 2714 | } |
| 2715 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2716 | } |
| 2717 | |
| 2718 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2719 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2720 | { |
| 2721 | Mutex::Autolock _l(mLock); |
| 2722 | return mOutput; |
| 2723 | } |
| 2724 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2725 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2726 | { |
| 2727 | Mutex::Autolock _l(mLock); |
| 2728 | AudioStreamOut *output = mOutput; |
| 2729 | mOutput = NULL; |
| 2730 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2731 | // must push a NULL and wait for ack |
| 2732 | mOutputSink.clear(); |
| 2733 | mPipeSink.clear(); |
| 2734 | mNormalSink.clear(); |
| 2735 | return output; |
| 2736 | } |
| 2737 | |
| 2738 | // 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] | 2739 | sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2740 | { |
| 2741 | if (mOutput == NULL) { |
| 2742 | return NULL; |
| 2743 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2744 | return mOutput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2748 | { |
| 2749 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2750 | } |
| 2751 | |
| 2752 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2753 | { |
| 2754 | if (!isValidSyncEvent(event)) { |
| 2755 | return BAD_VALUE; |
| 2756 | } |
| 2757 | |
| 2758 | Mutex::Autolock _l(mLock); |
| 2759 | |
| 2760 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2761 | sp<Track> track = mTracks[i]; |
| 2762 | if (event->triggerSession() == track->sessionId()) { |
| 2763 | (void) track->setSyncEvent(event); |
| 2764 | return NO_ERROR; |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | return NAME_NOT_FOUND; |
| 2769 | } |
| 2770 | |
| 2771 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2772 | { |
| 2773 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2774 | } |
| 2775 | |
| 2776 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2777 | const Vector< sp<Track> >& tracksToRemove) |
| 2778 | { |
| 2779 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2780 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2781 | for (size_t i = 0 ; i < count ; i++) { |
| 2782 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2783 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2784 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2785 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2786 | #ifdef ADD_BATTERY_DATA |
| 2787 | // to track the speaker usage |
| 2788 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2789 | #endif |
| 2790 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2791 | AudioSystem::releaseOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2792 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2793 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2800 | { |
| 2801 | if (!mMasterMute) { |
| 2802 | char value[PROPERTY_VALUE_MAX]; |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2803 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 2804 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 2805 | return; |
| 2806 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2807 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2808 | char *endptr; |
| 2809 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2810 | if (*endptr == '\0' && ul != 0) { |
| 2811 | ALOGD("Silence is golden"); |
| 2812 | // The setprop command will not allow a property to be changed after |
| 2813 | // the first time it is set, so we don't have to worry about un-muting. |
| 2814 | setMasterMute_l(true); |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2821 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2822 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 2823 | LOG_HIST_TS(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2824 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2825 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2826 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2827 | |
| 2828 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2829 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2830 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2831 | const size_t count = mBytesRemaining / mFrameSize; |
| 2832 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2833 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2834 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2835 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2836 | if (screenState != mScreenState) { |
| 2837 | mScreenState = screenState; |
| 2838 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2839 | if (pipe != NULL) { |
| 2840 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2841 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2842 | } |
| 2843 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2844 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2845 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2846 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2847 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2848 | } else { |
| 2849 | bytesWritten = framesWritten; |
| 2850 | } |
| 2851 | // otherwise use the HAL / AudioStreamOut directly |
| 2852 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2853 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2854 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2855 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2856 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2857 | mWriteAckSequence += 2; |
| 2858 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2859 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2860 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2861 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2862 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2863 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2864 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2865 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2866 | if (mUseAsyncWrite && |
| 2867 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2868 | // 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] | 2869 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2870 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2871 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2872 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2875 | mNumWrites++; |
| 2876 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2877 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2878 | return bytesWritten; |
| 2879 | } |
| 2880 | |
| 2881 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2882 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2883 | bool supportsDrain = false; |
| 2884 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2885 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2886 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2887 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2888 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2889 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2890 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2891 | } |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 2892 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2893 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2898 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2899 | { |
| 2900 | Mutex::Autolock _l(mLock); |
| 2901 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2902 | sp<Track> track = mTracks[i]; |
| 2903 | track->invalidate(); |
| 2904 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2905 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. |
| 2906 | // After we exit there are no more track changes sent to BatteryNotifier |
| 2907 | // because that requires an active threadLoop. |
| 2908 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? |
| 2909 | mActiveTracks.clear(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2910 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | /* |
| 2914 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2915 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2916 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2917 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2918 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2919 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2920 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2921 | |
| 2922 | The parameters that affect these derived values are: |
| 2923 | - frame count |
| 2924 | - frame size |
| 2925 | - sample rate |
| 2926 | - device type: A2DP or not |
| 2927 | - device latency |
| 2928 | - format: PCM or not |
| 2929 | - active sleep time |
| 2930 | - idle sleep time |
| 2931 | */ |
| 2932 | |
| 2933 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2934 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2935 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2936 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2937 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2938 | |
| 2939 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2940 | // truncating audio when going to standby. |
| 2941 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2942 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2943 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2944 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2945 | } |
| 2946 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2947 | } |
| 2948 | |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2949 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2950 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2951 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2952 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2953 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2954 | size_t size = mTracks.size(); |
| 2955 | for (size_t i = 0; i < size; i++) { |
| 2956 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2957 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2958 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2959 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2960 | } |
| 2961 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2962 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2963 | } |
| 2964 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2965 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2966 | { |
| 2967 | Mutex::Autolock _l(mLock); |
| 2968 | invalidateTracks_l(streamType); |
| 2969 | } |
| 2970 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2971 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2972 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2973 | audio_session_t session = chain->sessionId(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2974 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 2975 | status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer( |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2976 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, |
| 2977 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, |
| 2978 | &halInBuffer); |
| 2979 | if (result != OK) return result; |
| 2980 | halOutBuffer = halInBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2981 | effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2982 | 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] | 2983 | if (session > AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2984 | // 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] | 2985 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2986 | if (mType != DIRECT) { |
| 2987 | size_t numSamples = mNormalFrameCount * mChannelCount; |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 2988 | status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer( |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2989 | numSamples * sizeof(effect_buffer_t), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2990 | &halInBuffer); |
| 2991 | if (result != OK) return result; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2992 | #ifdef FLOAT_EFFECT_CHAIN |
| 2993 | buffer = halInBuffer->audioBuffer()->f32; |
| 2994 | #else |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2995 | buffer = halInBuffer->audioBuffer()->s16; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2996 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2997 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 2998 | buffer, session); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | // Attach all tracks with same session ID to this chain. |
| 3002 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3003 | sp<Track> track = mTracks[i]; |
| 3004 | if (session == track->sessionId()) { |
| 3005 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 3006 | buffer); |
| 3007 | track->setMainBuffer(buffer); |
| 3008 | chain->incTrackCnt(); |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | // indicate all active tracks in the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3013 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3014 | if (session == track->sessionId()) { |
| 3015 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 3016 | chain->incActiveTrackCnt(); |
| 3017 | } |
| 3018 | } |
| 3019 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3020 | chain->setThread(this); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3021 | chain->setInBuffer(halInBuffer); |
| 3022 | chain->setOutBuffer(halOutBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3023 | // 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] | 3024 | // 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] | 3025 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 3026 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3027 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3028 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3029 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3030 | // Effect chain for other sessions are inserted at beginning of effect |
| 3031 | // 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] | 3032 | // sessions is not important. |
| 3033 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
| 3034 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, |
| 3035 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3036 | size_t size = mEffectChains.size(); |
| 3037 | size_t i = 0; |
| 3038 | for (i = 0; i < size; i++) { |
| 3039 | if (mEffectChains[i]->sessionId() < session) { |
| 3040 | break; |
| 3041 | } |
| 3042 | } |
| 3043 | mEffectChains.insertAt(chain, i); |
| 3044 | checkSuspendOnAddEffectChain_l(chain); |
| 3045 | |
| 3046 | return NO_ERROR; |
| 3047 | } |
| 3048 | |
| 3049 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 3050 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3051 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3052 | |
| 3053 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 3054 | |
| 3055 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3056 | if (chain == mEffectChains[i]) { |
| 3057 | mEffectChains.removeAt(i); |
| 3058 | // detach all active tracks from the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3059 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3060 | if (session == track->sessionId()) { |
| 3061 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 3062 | chain.get(), session); |
| 3063 | chain->decActiveTrackCnt(); |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | // detach all tracks with same session ID from this chain |
| 3068 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3069 | sp<Track> track = mTracks[i]; |
| 3070 | if (session == track->sessionId()) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3071 | track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3072 | chain->decTrackCnt(); |
| 3073 | } |
| 3074 | } |
| 3075 | break; |
| 3076 | } |
| 3077 | } |
| 3078 | return mEffectChains.size(); |
| 3079 | } |
| 3080 | |
| 3081 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3082 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3083 | { |
| 3084 | Mutex::Autolock _l(mLock); |
| 3085 | return attachAuxEffect_l(track, EffectId); |
| 3086 | } |
| 3087 | |
| 3088 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3089 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3090 | { |
| 3091 | status_t status = NO_ERROR; |
| 3092 | |
| 3093 | if (EffectId == 0) { |
| 3094 | track->setAuxBuffer(0, NULL); |
| 3095 | } else { |
| 3096 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 3097 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 3098 | if (effect != 0) { |
| 3099 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 3100 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 3101 | } else { |
| 3102 | status = INVALID_OPERATION; |
| 3103 | } |
| 3104 | } else { |
| 3105 | status = BAD_VALUE; |
| 3106 | } |
| 3107 | } |
| 3108 | return status; |
| 3109 | } |
| 3110 | |
| 3111 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 3112 | { |
| 3113 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3114 | sp<Track> track = mTracks[i]; |
| 3115 | if (track->auxEffectId() == effectId) { |
| 3116 | attachAuxEffect_l(track, 0); |
| 3117 | } |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 3122 | { |
Glenn Kasten | 388d571 | 2017-04-07 14:38:41 -0700 | [diff] [blame] | 3123 | tlNBLogWriter = mNBLogWriter.get(); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3124 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3125 | Vector< sp<Track> > tracksToRemove; |
| 3126 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3127 | mStandbyTimeNs = systemTime(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3128 | nsecs_t lastWriteFinished = -1; // time last server write completed |
| 3129 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3130 | |
| 3131 | // MIXER |
| 3132 | nsecs_t lastWarning = 0; |
| 3133 | |
| 3134 | // DUPLICATING |
| 3135 | // FIXME could this be made local to while loop? |
| 3136 | writeFrames = 0; |
| 3137 | |
| 3138 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3139 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3140 | |
| 3141 | if (mType == MIXER) { |
| 3142 | sleepTimeShift = 0; |
| 3143 | } |
| 3144 | |
| 3145 | CpuStats cpuStats; |
| 3146 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 3147 | |
| 3148 | acquireWakeLock(); |
| 3149 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3150 | // mNBLogWriter logging APIs can only be called by a single thread, typically the |
| 3151 | // thread associated with this PlaybackThread. |
| 3152 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) |
| 3153 | // 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] | 3154 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 3155 | // and then that string will be logged at the next convenient opportunity. |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3156 | // See reference to logString below. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3157 | const char *logString = NULL; |
| 3158 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3159 | // Estimated time for next buffer to be written to hal. This is used only on |
| 3160 | // suspended mode (for now) to help schedule the wait time until next iteration. |
| 3161 | nsecs_t timeLoopNextNs = 0; |
| 3162 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 3163 | checkSilentMode_l(); |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3164 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3165 | while (!exitPending()) |
| 3166 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 3167 | // Log merge requests are performed during AudioFlinger binder transactions, but |
| 3168 | // that does not cover audio playback. It's requested here for that reason. |
| 3169 | mAudioFlinger->requestLogMerge(); |
| 3170 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3171 | cpuStats.sample(myName); |
| 3172 | |
| 3173 | Vector< sp<EffectChain> > effectChains; |
| 3174 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3175 | { // scope for mLock |
| 3176 | |
| 3177 | Mutex::Autolock _l(mLock); |
| 3178 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3179 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3180 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3181 | // 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] | 3182 | if (logString != NULL) { |
| 3183 | mNBLogWriter->logTimestamp(); |
| 3184 | mNBLogWriter->log(logString); |
| 3185 | logString = NULL; |
| 3186 | } |
| 3187 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3188 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3189 | // and associate with the sink frames written out. We need |
| 3190 | // this to convert the sink timestamp to the track timestamp. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3191 | bool kernelLocationUpdate = false; |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3192 | if (mNormalSink != 0) { |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3193 | // Note: The DuplicatingThread may not have a mNormalSink. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3194 | // We always fetch the timestamp here because often the downstream |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3195 | // sink will block while writing. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3196 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 3197 | (void) mNormalSink->getTimestamp(timestamp); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3198 | |
| 3199 | // We keep track of the last valid kernel position in case we are in underrun |
| 3200 | // and the normal mixer period is the same as the fast mixer period, or there |
| 3201 | // is some error from the HAL. |
| 3202 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3203 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3204 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3205 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3206 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3207 | |
| 3208 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3209 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 3210 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3211 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3215 | kernelLocationUpdate = true; |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3216 | } else { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3217 | ALOGVV("getTimestamp error - no valid kernel position"); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3220 | // copy over kernel info |
| 3221 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3222 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 3223 | + mSuspendedFrames; // add frames discarded when suspended |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3224 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 3225 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3226 | } |
| 3227 | // mFramesWritten for non-offloaded tracks are contiguous |
| 3228 | // even after standby() is called. This is useful for the track frame |
| 3229 | // to sink frame mapping. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3230 | bool serverLocationUpdate = false; |
| 3231 | if (mFramesWritten != lastFramesWritten) { |
| 3232 | serverLocationUpdate = true; |
| 3233 | lastFramesWritten = mFramesWritten; |
| 3234 | } |
| 3235 | // Only update timestamps if there is a meaningful change. |
| 3236 | // Either the kernel timestamp must be valid or we have written something. |
| 3237 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 3238 | if (serverLocationUpdate) { |
| 3239 | // use the time before we called the HAL write - it is a bit more accurate |
| 3240 | // to when the server last read data than the current time here. |
| 3241 | // |
| 3242 | // If we haven't written anything, mLastWriteTime will be -1 |
| 3243 | // and we use systemTime(). |
| 3244 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 3245 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 |
| 3246 | ? systemTime() : mLastWriteTime; |
| 3247 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3248 | |
| 3249 | for (const sp<Track> &t : mActiveTracks) { |
| 3250 | if (!t->isFastTrack()) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3251 | t->updateTrackFrameInfo( |
| 3252 | t->mAudioTrackServerProxy->framesReleased(), |
| 3253 | mFramesWritten, |
| 3254 | mTimestamp); |
| 3255 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3256 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3257 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3258 | #if 0 |
| 3259 | // logFormat example |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 3260 | if (z % 100 == 0) { |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3261 | timespec ts; |
| 3262 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3263 | 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] | 3264 | "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] | 3265 | LOGT("A deceptive null-terminated string %\0"); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3266 | } |
| 3267 | ++z; |
| 3268 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3269 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3270 | if (mSignalPending) { |
| 3271 | // A signal was raised while we were unlocked |
| 3272 | mSignalPending = false; |
| 3273 | } else if (waitingAsyncCallback_l()) { |
| 3274 | if (exitPending()) { |
| 3275 | break; |
| 3276 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3277 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3278 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3279 | releaseWakeLock_l(); |
| 3280 | released = true; |
| 3281 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 3282 | |
| 3283 | const int64_t waitNs = computeWaitTimeNs_l(); |
| 3284 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); |
| 3285 | status_t status = mWaitWorkCV.waitRelative(mLock, waitNs); |
| 3286 | if (status == TIMED_OUT) { |
| 3287 | mSignalPending = true; // if timeout recheck everything |
| 3288 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3289 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3290 | if (released) { |
| 3291 | acquireWakeLock_l(); |
| 3292 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3293 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3294 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3295 | |
| 3296 | continue; |
| 3297 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3298 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3299 | isSuspended()) { |
| 3300 | // put audio hardware into standby after short delay |
| 3301 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3302 | |
| 3303 | threadLoop_standby(); |
| 3304 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3305 | // This is where we go into standby |
| 3306 | if (!mStandby) { |
| 3307 | LOG_AUDIO_STATE(); |
| 3308 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3309 | mStandby = true; |
| 3310 | } |
| 3311 | |
| 3312 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 3313 | // we're about to wait, flush the binder command buffer |
| 3314 | IPCThreadState::self()->flushCommands(); |
| 3315 | |
| 3316 | clearOutputTracks(); |
| 3317 | |
| 3318 | if (exitPending()) { |
| 3319 | break; |
| 3320 | } |
| 3321 | |
| 3322 | releaseWakeLock_l(); |
| 3323 | // wait until we have something to do... |
| 3324 | ALOGV("%s going to sleep", myName.string()); |
| 3325 | mWaitWorkCV.wait(mLock); |
| 3326 | ALOGV("%s waking up", myName.string()); |
| 3327 | acquireWakeLock_l(); |
| 3328 | |
| 3329 | mMixerStatus = MIXER_IDLE; |
| 3330 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 3331 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3332 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3333 | checkSilentMode_l(); |
| 3334 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3335 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3336 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3337 | if (mType == MIXER) { |
| 3338 | sleepTimeShift = 0; |
| 3339 | } |
| 3340 | |
| 3341 | continue; |
| 3342 | } |
| 3343 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3344 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 3345 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 3346 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3347 | mActiveTracks.updatePowerState(this); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3348 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3349 | updateMetadata_l(); |
| 3350 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3351 | // prevent any changes in effect chain list and in each effect chain |
| 3352 | // during mixing and effect process as the audio buffers could be deleted |
| 3353 | // or modified if an effect is created or deleted |
| 3354 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3355 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3356 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3357 | if (mBytesRemaining == 0) { |
| 3358 | mCurrentWriteLength = 0; |
| 3359 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3360 | // threadLoop_mix() sets mCurrentWriteLength |
| 3361 | threadLoop_mix(); |
| 3362 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3363 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3364 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3365 | // must be written to HAL |
| 3366 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3367 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3368 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3369 | } |
| 3370 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3371 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3372 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3373 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3374 | // or mSinkBuffer (if there are no effects). |
| 3375 | // |
| 3376 | // This is done pre-effects computation; if effects change to |
| 3377 | // support higher precision, this needs to move. |
| 3378 | // |
| 3379 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3380 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3381 | if (mMixerBufferValid) { |
| 3382 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3383 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3384 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3385 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3386 | // and is handled here if we're going directly to the sink. |
| 3387 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3388 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3389 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3390 | } |
| 3391 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3392 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3393 | mNormalFrameCount * mChannelCount); |
| 3394 | } |
| 3395 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3396 | mBytesRemaining = mCurrentWriteLength; |
| 3397 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3398 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 3399 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 3400 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 3401 | mBytesWritten += mBytesRemaining; |
| 3402 | mFramesWritten += framesRemaining; |
| 3403 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3404 | mBytesRemaining = 0; |
| 3405 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3406 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3407 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3408 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3409 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3410 | effectChains[i]->process_l(); |
| 3411 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3412 | } |
| 3413 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3414 | // Process effect chains for offloaded thread even if no audio |
| 3415 | // was read from audio track: process only updates effect state |
| 3416 | // and thus does have to be synchronized with audio writes but may have |
| 3417 | // to be called while waiting for async write callback |
| 3418 | if (mType == OFFLOAD) { |
| 3419 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3420 | effectChains[i]->process_l(); |
| 3421 | } |
| 3422 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3423 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3424 | // Only if the Effects buffer is enabled and there is data in the |
| 3425 | // Effects buffer (buffer valid), we need to |
| 3426 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3427 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3428 | if (mEffectBufferValid) { |
| 3429 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3430 | |
| 3431 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3432 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3433 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3434 | } |
| 3435 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3436 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3437 | mNormalFrameCount * mChannelCount); |
| 3438 | } |
| 3439 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3440 | // enable changes in effect chain |
| 3441 | unlockEffectChains(effectChains); |
| 3442 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3443 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3444 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3445 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3446 | ssize_t ret = 0; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3447 | // We save lastWriteFinished here, as previousLastWriteFinished, |
| 3448 | // for throttling. On thread start, previousLastWriteFinished will be |
| 3449 | // set to -1, which properly results in no throttling after the first write. |
| 3450 | nsecs_t previousLastWriteFinished = lastWriteFinished; |
| 3451 | nsecs_t delta = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3452 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3453 | // FIXME rewrite to reduce number of system calls |
| 3454 | mLastWriteTime = systemTime(); // also used for dumpsys |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3455 | ret = threadLoop_write(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3456 | lastWriteFinished = systemTime(); |
| 3457 | delta = lastWriteFinished - mLastWriteTime; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3458 | if (ret < 0) { |
| 3459 | mBytesRemaining = 0; |
| 3460 | } else { |
| 3461 | mBytesWritten += ret; |
| 3462 | mBytesRemaining -= ret; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3463 | mFramesWritten += ret / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3464 | } |
| 3465 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3466 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3467 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3468 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3469 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3470 | // write blocked detection |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3471 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3472 | mNumDelayedWrites++; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3473 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3474 | ATRACE_NAME("underrun"); |
| 3475 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3476 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3477 | lastWarning = lastWriteFinished; |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3478 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3479 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3480 | |
| 3481 | if (mThreadThrottle |
| 3482 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3483 | && ret > 0) { // we wrote something |
| 3484 | // Limit MixerThread data processing to no more than twice the |
| 3485 | // expected processing rate. |
| 3486 | // |
| 3487 | // This helps prevent underruns with NuPlayer and other applications |
| 3488 | // which may set up buffers that are close to the minimum size, or use |
| 3489 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3490 | // |
| 3491 | // The throttle smooths out sudden large data drains from the device, |
| 3492 | // e.g. when it comes out of standby, which often causes problems with |
| 3493 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3494 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3495 | // 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] | 3496 | // |
| 3497 | // Total time spent in last processing cycle equals time spent in |
| 3498 | // 1. threadLoop_write, as well as time spent in |
| 3499 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 3500 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3501 | |
Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 3502 | // it's OK if deltaMs (and deltaNs) is an overestimate. |
| 3503 | nsecs_t deltaNs; |
| 3504 | // deltaNs = lastWriteFinished - previousLastWriteFinished; |
| 3505 | __builtin_sub_overflow( |
| 3506 | lastWriteFinished,previousLastWriteFinished, &deltaNs); |
| 3507 | const int32_t deltaMs = deltaNs / 1000000; |
| 3508 | |
Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 3509 | const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3510 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3511 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3512 | // notify of throttle start on verbose log |
| 3513 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3514 | "mixer(%p) throttle begin:" |
| 3515 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3516 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3517 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3518 | // Throttle must be attributed to the previous mixer loop's write time |
| 3519 | // to allow back-to-back throttling. |
| 3520 | lastWriteFinished += throttleMs * 1000000; |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3521 | } else { |
| 3522 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3523 | if (diff > 0) { |
| 3524 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3525 | // but prevent spamming for bluetooth |
Jakub Pawlowski | 0568ded | 2018-03-14 11:20:05 -0700 | [diff] [blame] | 3526 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()) && |
| 3527 | !audio_is_hearing_aid_out_device(outDevice()), |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3528 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3529 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3530 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3531 | } |
| 3532 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3533 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3534 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3535 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3536 | ATRACE_BEGIN("sleep"); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3537 | Mutex::Autolock _l(mLock); |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3538 | // suspended requires accurate metering of sleep time. |
| 3539 | if (isSuspended()) { |
| 3540 | // advance by expected sleepTime |
| 3541 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); |
| 3542 | const nsecs_t nowNs = systemTime(); |
| 3543 | |
| 3544 | // compute expected next time vs current time. |
| 3545 | // (negative deltas are treated as delays). |
| 3546 | nsecs_t deltaNs = timeLoopNextNs - nowNs; |
| 3547 | if (deltaNs < -kMaxNextBufferDelayNs) { |
| 3548 | // Delays longer than the max allowed trigger a reset. |
| 3549 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); |
| 3550 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); |
| 3551 | timeLoopNextNs = nowNs + deltaNs; |
| 3552 | } else if (deltaNs < 0) { |
| 3553 | // Delays within the max delay allowed: zero the delta/sleepTime |
| 3554 | // to help the system catch up in the next iteration(s) |
| 3555 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); |
| 3556 | deltaNs = 0; |
| 3557 | } |
| 3558 | // update sleep time (which is >= 0) |
| 3559 | mSleepTimeUs = deltaNs / 1000; |
| 3560 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3561 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
| 3562 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3563 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3564 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3565 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | // Finally let go of removed track(s), without the lock held |
| 3569 | // since we can't guarantee the destructors won't acquire that |
| 3570 | // same lock. This will also mutate and push a new fast mixer state. |
| 3571 | threadLoop_removeTracks(tracksToRemove); |
| 3572 | tracksToRemove.clear(); |
| 3573 | |
| 3574 | // FIXME I don't understand the need for this here; |
| 3575 | // it was in the original code but maybe the |
| 3576 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3577 | clearOutputTracks(); |
| 3578 | |
| 3579 | // Effect chains will be actually deleted here if they were removed from |
| 3580 | // mEffectChains list during mixing or effects processing |
| 3581 | effectChains.clear(); |
| 3582 | |
| 3583 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3584 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3585 | } |
| 3586 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3587 | threadLoop_exit(); |
| 3588 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3589 | if (!mStandby) { |
| 3590 | threadLoop_standby(); |
| 3591 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | releaseWakeLock(); |
| 3595 | |
| 3596 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3597 | return false; |
| 3598 | } |
| 3599 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3600 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3601 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3602 | { |
| 3603 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3604 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3605 | for (size_t i=0 ; i<count ; i++) { |
| 3606 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3607 | mActiveTracks.remove(track); |
| 3608 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3609 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3610 | if (chain != 0) { |
| 3611 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3612 | track->sessionId()); |
| 3613 | chain->decActiveTrackCnt(); |
| 3614 | } |
| 3615 | if (track->isTerminated()) { |
| 3616 | removeTrack_l(track); |
| 3617 | } |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3622 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3623 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3624 | { |
| 3625 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3626 | ExtendedTimestamp ets; |
| 3627 | status_t status = mNormalSink->getTimestamp(ets); |
| 3628 | if (status == NO_ERROR) { |
| 3629 | status = ets.getBestTimestamp(×tamp); |
| 3630 | } |
| 3631 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3632 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3633 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3634 | uint64_t position64; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3635 | if (mOutput->getPresentationPosition(&position64, ×tamp.mTime) == OK) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3636 | timestamp.mPosition = (uint32_t)position64; |
| 3637 | return NO_ERROR; |
| 3638 | } |
| 3639 | } |
| 3640 | return INVALID_OPERATION; |
| 3641 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3642 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3643 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3644 | audio_patch_handle_t *handle) |
| 3645 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3646 | status_t status; |
| 3647 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3648 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3649 | // or if HAL does not properly lock against access. |
| 3650 | AutoPark<FastMixer> park(mFastMixer); |
| 3651 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3652 | } else { |
| 3653 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3654 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3655 | return status; |
| 3656 | } |
| 3657 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3658 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3659 | audio_patch_handle_t *handle) |
| 3660 | { |
| 3661 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3662 | |
| 3663 | // store new device and send to effects |
| 3664 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3665 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3666 | type |= patch->sinks[i].ext.device.type; |
| 3667 | } |
| 3668 | |
| 3669 | #ifdef ADD_BATTERY_DATA |
| 3670 | // when changing the audio output device, call addBatteryData to notify |
| 3671 | // the change |
| 3672 | if (mOutDevice != type) { |
| 3673 | uint32_t params = 0; |
| 3674 | // check whether speaker is on |
| 3675 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3676 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3679 | audio_devices_t deviceWithoutSpeaker |
| 3680 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3681 | // check if any other device (except speaker) is on |
| 3682 | if (type & deviceWithoutSpeaker) { |
| 3683 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3684 | } |
| 3685 | |
| 3686 | if (params != 0) { |
| 3687 | addBatteryData(params); |
| 3688 | } |
| 3689 | } |
| 3690 | #endif |
| 3691 | |
| 3692 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3693 | mEffectChains[i]->setDevice_l(type); |
| 3694 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3695 | |
| 3696 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3697 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3698 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3699 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3700 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3701 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3702 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3703 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3704 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 3705 | patch->sources, |
| 3706 | patch->num_sinks, |
| 3707 | patch->sinks, |
| 3708 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3709 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3710 | char *address; |
| 3711 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3712 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3713 | address = audio_device_address_to_parameter( |
| 3714 | patch->sinks[0].ext.device.type, |
| 3715 | patch->sinks[0].ext.device.address); |
| 3716 | } else { |
| 3717 | address = (char *)calloc(1, 1); |
| 3718 | } |
| 3719 | AudioParameter param = AudioParameter(String8(address)); |
| 3720 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3721 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3722 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3723 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3724 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3725 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3726 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3727 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3728 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3729 | return status; |
| 3730 | } |
| 3731 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3732 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3733 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3734 | status_t status; |
| 3735 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3736 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3737 | // or if HAL does not properly lock against access. |
| 3738 | AutoPark<FastMixer> park(mFastMixer); |
| 3739 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3740 | } else { |
| 3741 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3742 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3743 | return status; |
| 3744 | } |
| 3745 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3746 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3747 | { |
| 3748 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3749 | |
| 3750 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3751 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3752 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3753 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3754 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3755 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3756 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3757 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3758 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3759 | } |
| 3760 | return status; |
| 3761 | } |
| 3762 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3763 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3764 | { |
| 3765 | Mutex::Autolock _l(mLock); |
| 3766 | mTracks.add(track); |
| 3767 | } |
| 3768 | |
| 3769 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3770 | { |
| 3771 | Mutex::Autolock _l(mLock); |
| 3772 | destroyTrack_l(track); |
| 3773 | } |
| 3774 | |
| 3775 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3776 | { |
| 3777 | ThreadBase::getAudioPortConfig(config); |
| 3778 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3779 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3780 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3781 | } |
| 3782 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3783 | // ---------------------------------------------------------------------------- |
| 3784 | |
| 3785 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3786 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3787 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3788 | // mAudioMixer below |
| 3789 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3790 | mFastMixerFutex(0), |
| 3791 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3792 | // mOutputSink below |
| 3793 | // mPipeSink below |
| 3794 | // mNormalSink below |
| 3795 | { |
| 3796 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3797 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3798 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3799 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3800 | mNormalFrameCount); |
| 3801 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3802 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3803 | if (type == DUPLICATING) { |
| 3804 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3805 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3806 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3807 | return; |
| 3808 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3809 | // create an NBAIO sink for the HAL output stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 3810 | mOutputSink = new AudioStreamOutSink(output->stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3811 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3812 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3813 | #if !LOG_NDEBUG |
| 3814 | ssize_t index = |
| 3815 | #else |
| 3816 | (void) |
| 3817 | #endif |
| 3818 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3819 | ALOG_ASSERT(index == 0); |
| 3820 | |
| 3821 | // initialize fast mixer depending on configuration |
| 3822 | bool initFastMixer; |
| 3823 | switch (kUseFastMixer) { |
| 3824 | case FastMixer_Never: |
| 3825 | initFastMixer = false; |
| 3826 | break; |
| 3827 | case FastMixer_Always: |
| 3828 | initFastMixer = true; |
| 3829 | break; |
| 3830 | case FastMixer_Static: |
| 3831 | case FastMixer_Dynamic: |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3832 | // FastMixer was designed to operate with a HAL that pulls at a regular rate, |
| 3833 | // where the period is less than an experimentally determined threshold that can be |
| 3834 | // scheduled reliably with CFS. However, the BT A2DP HAL is |
| 3835 | // bursty (does not pull at a regular rate) and so cannot operate with FastMixer. |
| 3836 | initFastMixer = mFrameCount < mNormalFrameCount |
| 3837 | && (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) == 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3838 | break; |
| 3839 | } |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3840 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, |
| 3841 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", |
| 3842 | mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3843 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3844 | audio_format_t fastMixerFormat; |
| 3845 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3846 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3847 | } else { |
| 3848 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3849 | } |
| 3850 | if (mFormat != fastMixerFormat) { |
| 3851 | // change our Sink format to accept our intermediate precision |
| 3852 | mFormat = fastMixerFormat; |
| 3853 | free(mSinkBuffer); |
| 3854 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3855 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3856 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3857 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3858 | |
| 3859 | // create a MonoPipe to connect our submix to FastMixer |
| 3860 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3861 | #ifdef TEE_SINK |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3862 | NBAIO_Format origformat = format; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3863 | #endif |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3864 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3865 | ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3866 | format.mFormat = fastMixerFormat; |
| 3867 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3868 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3869 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3870 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3871 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3872 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3873 | const NBAIO_Format offers[1] = {format}; |
| 3874 | size_t numCounterOffers = 0; |
Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3875 | #if !LOG_NDEBUG || defined(TEE_SINK) |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3876 | ssize_t index = |
| 3877 | #else |
| 3878 | (void) |
| 3879 | #endif |
| 3880 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3881 | ALOG_ASSERT(index == 0); |
| 3882 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3883 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3884 | mPipeSink = monoPipe; |
| 3885 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3886 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3887 | if (mTeeSinkOutputEnabled) { |
| 3888 | // 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] | 3889 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3890 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3891 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3892 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3893 | ALOG_ASSERT(index == 0); |
| 3894 | mTeeSink = teeSink; |
| 3895 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3896 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3897 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3898 | ALOG_ASSERT(index == 0); |
| 3899 | mTeeSource = teeSource; |
| 3900 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3901 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3902 | |
| 3903 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3904 | mFastMixer = new FastMixer(); |
| 3905 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3906 | #ifdef STATE_QUEUE_DUMP |
| 3907 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3908 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3909 | #endif |
| 3910 | FastMixerState *state = sq->begin(); |
| 3911 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3912 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3913 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3914 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3915 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3916 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3917 | fastTrack->mGeneration++; |
| 3918 | state->mFastTracksGen++; |
| 3919 | state->mTrackMask = 1; |
| 3920 | // fast mixer will use the HAL output sink |
| 3921 | state->mOutputSink = mOutputSink.get(); |
| 3922 | state->mOutputSinkGen++; |
| 3923 | state->mFrameCount = mFrameCount; |
| 3924 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3925 | // already done in constructor initialization list |
| 3926 | //mFastMixerFutex = 0; |
| 3927 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3928 | state->mColdGen++; |
| 3929 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3930 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3931 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3932 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3933 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3934 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3935 | sq->end(); |
| 3936 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3937 | |
| 3938 | // start the fast mixer |
| 3939 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3940 | pid_t tid = mFastMixer->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3941 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 3942 | stream()->setHalThreadPriority(kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3943 | |
| 3944 | #ifdef AUDIO_WATCHDOG |
| 3945 | // create and start the watchdog |
| 3946 | mAudioWatchdog = new AudioWatchdog(); |
| 3947 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3948 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3949 | tid = mAudioWatchdog->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3950 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3951 | #endif |
| 3952 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | switch (kUseFastMixer) { |
| 3956 | case FastMixer_Never: |
| 3957 | case FastMixer_Dynamic: |
| 3958 | mNormalSink = mOutputSink; |
| 3959 | break; |
| 3960 | case FastMixer_Always: |
| 3961 | mNormalSink = mPipeSink; |
| 3962 | break; |
| 3963 | case FastMixer_Static: |
| 3964 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3965 | break; |
| 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | AudioFlinger::MixerThread::~MixerThread() |
| 3970 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3971 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3972 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3973 | FastMixerState *state = sq->begin(); |
| 3974 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3975 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3976 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3977 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | state->mCommand = FastMixerState::EXIT; |
| 3981 | sq->end(); |
| 3982 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3983 | mFastMixer->join(); |
| 3984 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3985 | // We'll use that extract the final state which contains one remaining fast track |
| 3986 | // corresponding to our sub-mix. |
| 3987 | state = sq->begin(); |
| 3988 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3989 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3990 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3991 | delete fastTrack->mBufferProvider; |
| 3992 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3993 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3994 | #ifdef AUDIO_WATCHDOG |
| 3995 | if (mAudioWatchdog != 0) { |
| 3996 | mAudioWatchdog->requestExit(); |
| 3997 | mAudioWatchdog->requestExitAndWait(); |
| 3998 | mAudioWatchdog.clear(); |
| 3999 | } |
| 4000 | #endif |
| 4001 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4002 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4003 | delete mAudioMixer; |
| 4004 | } |
| 4005 | |
| 4006 | |
| 4007 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 4008 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4009 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4010 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 4011 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 4012 | } |
| 4013 | return latency; |
| 4014 | } |
| 4015 | |
| 4016 | |
| 4017 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 4018 | { |
| 4019 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 4020 | } |
| 4021 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4022 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4023 | { |
| 4024 | // FIXME we should only do one push per cycle; confirm this is true |
| 4025 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4026 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4027 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4028 | FastMixerState *state = sq->begin(); |
| 4029 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 4030 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 4031 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 4032 | |
| 4033 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 4034 | ATRACE_BEGIN("write"); |
| 4035 | mOutput->write((char *)mSinkBuffer, 0); |
| 4036 | ATRACE_END(); |
| 4037 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4038 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 4039 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 4040 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4041 | } |
| 4042 | #ifdef AUDIO_WATCHDOG |
| 4043 | if (mAudioWatchdog != 0) { |
| 4044 | mAudioWatchdog->resume(); |
| 4045 | } |
| 4046 | #endif |
| 4047 | } |
| 4048 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 4049 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 4050 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 4051 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 4052 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4053 | sq->end(); |
| 4054 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4055 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4056 | mNormalSink = mPipeSink; |
| 4057 | } |
| 4058 | } else { |
| 4059 | sq->end(false /*didModify*/); |
| 4060 | } |
| 4061 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4062 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 4066 | { |
| 4067 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4068 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4069 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4070 | FastMixerState *state = sq->begin(); |
| 4071 | if (!(state->mCommand & FastMixerState::IDLE)) { |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 4072 | // Report any frames trapped in the Monopipe |
| 4073 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); |
| 4074 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); |
| 4075 | mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld " |
| 4076 | "monoPipeWritten:%lld monoPipeLeft:%lld", |
| 4077 | (long long)mFramesWritten, (long long)mSuspendedFrames, |
| 4078 | (long long)mPipeSink->framesWritten(), pipeFrames); |
| 4079 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); |
| 4080 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4081 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4082 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4083 | state->mColdGen++; |
| 4084 | mFastMixerFutex = 0; |
| 4085 | sq->end(); |
| 4086 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 4087 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 4088 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4089 | mNormalSink = mOutputSink; |
| 4090 | } |
| 4091 | #ifdef AUDIO_WATCHDOG |
| 4092 | if (mAudioWatchdog != 0) { |
| 4093 | mAudioWatchdog->pause(); |
| 4094 | } |
| 4095 | #endif |
| 4096 | } else { |
| 4097 | sq->end(false /*didModify*/); |
| 4098 | } |
| 4099 | } |
| 4100 | PlaybackThread::threadLoop_standby(); |
| 4101 | } |
| 4102 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4103 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 4104 | { |
| 4105 | return false; |
| 4106 | } |
| 4107 | |
| 4108 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 4109 | { |
| 4110 | return !mStandby; |
| 4111 | } |
| 4112 | |
| 4113 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 4114 | { |
| 4115 | Mutex::Autolock _l(mLock); |
| 4116 | return waitingAsyncCallback_l(); |
| 4117 | } |
| 4118 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4119 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 4120 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 4121 | { |
| 4122 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4123 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4124 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4125 | // discard any pending drain or write ack by incrementing sequence |
| 4126 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 4127 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4128 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4129 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 4130 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4131 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4132 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4133 | } |
| 4134 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4135 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 4136 | { |
| 4137 | ALOGV("signal playback thread"); |
| 4138 | broadcast_l(); |
| 4139 | } |
| 4140 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 4141 | void AudioFlinger::PlaybackThread::onAsyncError() |
| 4142 | { |
| 4143 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 4144 | invalidateTracks((audio_stream_type_t)i); |
| 4145 | } |
| 4146 | } |
| 4147 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4148 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 4149 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4150 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 4151 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4152 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4153 | // increase sleep time progressively when application underrun condition clears. |
| 4154 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 4155 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 4156 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4157 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4158 | sleepTimeShift--; |
| 4159 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4160 | mSleepTimeUs = 0; |
| 4161 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4162 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 4163 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 4167 | { |
| 4168 | // If no tracks are ready, sleep once for the duration of an output |
| 4169 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4170 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4171 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4172 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 4173 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 4174 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4175 | } |
| 4176 | // reduce sleep time in case of consecutive application underruns to avoid |
| 4177 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 4178 | // duration we would end up writing less data than needed by the audio HAL if |
| 4179 | // the condition persists. |
| 4180 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 4181 | sleepTimeShift++; |
| 4182 | } |
| 4183 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4184 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4185 | } |
| 4186 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4187 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 4188 | // before effects processing or output. |
| 4189 | if (mMixerBufferValid) { |
| 4190 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4191 | } else { |
| 4192 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 4193 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4194 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4195 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 4196 | "anticipated start"); |
| 4197 | } |
| 4198 | // TODO add standby time extension fct of effect tail |
| 4199 | } |
| 4200 | |
| 4201 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 4202 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 4203 | Vector< sp<Track> > *tracksToRemove) |
| 4204 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4205 | // clean up deleted track names in AudioMixer before allocating new tracks |
| 4206 | (void)mTracks.processDeletedTrackNames([this](int name) { |
| 4207 | // for each name, destroy it in the AudioMixer |
| 4208 | if (mAudioMixer->exists(name)) { |
| 4209 | mAudioMixer->destroy(name); |
| 4210 | } |
| 4211 | }); |
| 4212 | mTracks.clearDeletedTrackNames(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4213 | |
| 4214 | mixer_state mixerStatus = MIXER_IDLE; |
| 4215 | // find out which tracks need to be processed |
| 4216 | size_t count = mActiveTracks.size(); |
| 4217 | size_t mixedTracks = 0; |
| 4218 | size_t tracksWithEffect = 0; |
| 4219 | // counts only _active_ fast tracks |
| 4220 | size_t fastTracks = 0; |
| 4221 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 4222 | |
| 4223 | float masterVolume = mMasterVolume; |
| 4224 | bool masterMute = mMasterMute; |
| 4225 | |
| 4226 | if (masterMute) { |
| 4227 | masterVolume = 0; |
| 4228 | } |
| 4229 | // Delegate master volume control to effect in output mix effect chain if needed |
| 4230 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 4231 | if (chain != 0) { |
| 4232 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 4233 | chain->setVolume_l(&v, &v); |
| 4234 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 4235 | chain.clear(); |
| 4236 | } |
| 4237 | |
| 4238 | // prepare a new state to push |
| 4239 | FastMixerStateQueue *sq = NULL; |
| 4240 | FastMixerState *state = NULL; |
| 4241 | bool didModify = false; |
| 4242 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4243 | bool coldIdle = false; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4244 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4245 | sq = mFastMixer->sq(); |
| 4246 | state = sq->begin(); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4247 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4248 | } |
| 4249 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4250 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4251 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4252 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4253 | for (size_t i=0 ; i<count ; i++) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4254 | const sp<Track> t = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4255 | |
| 4256 | // this const just means the local variable doesn't change |
| 4257 | Track* const track = t.get(); |
| 4258 | |
| 4259 | // process fast tracks |
| 4260 | if (track->isFastTrack()) { |
| 4261 | |
| 4262 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 4263 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 4264 | // the track never becomes active so it's fast mixer slot is never touched. |
| 4265 | // The converse, of removing an (active) track and then creating a new track |
| 4266 | // at the identical fast mixer slot within the same normal mix cycle, |
| 4267 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 4268 | int j = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4269 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4270 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 4271 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 4272 | |
| 4273 | // Determine whether the track is currently in underrun condition, |
| 4274 | // and whether it had a recent underrun. |
| 4275 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 4276 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 4277 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 4278 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 4279 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 4280 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 4281 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 4282 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 4283 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 4284 | track->mObservedUnderruns = underruns; |
| 4285 | // don't count underruns that occur while stopping or pausing |
| 4286 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4287 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 4288 | recentUnderruns > 0) { |
| 4289 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 4290 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4291 | } else { |
| 4292 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | // This is similar to the state machine for normal tracks, |
| 4296 | // with a few modifications for fast tracks. |
| 4297 | bool isActive = true; |
| 4298 | switch (track->mState) { |
| 4299 | case TrackBase::STOPPING_1: |
| 4300 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4301 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4302 | track->mState = TrackBase::STOPPING_2; |
| 4303 | } |
| 4304 | break; |
| 4305 | case TrackBase::PAUSING: |
| 4306 | // ramp down is not yet implemented |
| 4307 | track->setPaused(); |
| 4308 | break; |
| 4309 | case TrackBase::RESUMING: |
| 4310 | // ramp up is not yet implemented |
| 4311 | track->mState = TrackBase::ACTIVE; |
| 4312 | break; |
| 4313 | case TrackBase::ACTIVE: |
| 4314 | if (recentFull > 0 || recentPartial > 0) { |
| 4315 | // track has provided at least some frames recently: reset retry count |
| 4316 | track->mRetryCount = kMaxTrackRetries; |
| 4317 | } |
| 4318 | if (recentUnderruns == 0) { |
| 4319 | // no recent underruns: stay active |
| 4320 | break; |
| 4321 | } |
| 4322 | // there has recently been an underrun of some kind |
| 4323 | if (track->sharedBuffer() == 0) { |
| 4324 | // were any of the recent underruns "empty" (no frames available)? |
| 4325 | if (recentEmpty == 0) { |
| 4326 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 4327 | break; |
| 4328 | } |
| 4329 | // there has recently been an "empty" underrun: decrement the retry counter |
| 4330 | if (--(track->mRetryCount) > 0) { |
| 4331 | break; |
| 4332 | } |
| 4333 | // indicate to client process that the track was disabled because of underrun; |
| 4334 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4335 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4336 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 4337 | isActive = false; |
| 4338 | break; |
| 4339 | } |
| 4340 | // fall through |
| 4341 | case TrackBase::STOPPING_2: |
| 4342 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4343 | case TrackBase::STOPPED: |
| 4344 | case TrackBase::FLUSHED: // flush() while active |
| 4345 | // Check for presentation complete if track is inactive |
| 4346 | // We have consumed all the buffers of this track. |
| 4347 | // This would be incomplete if we auto-paused on underrun |
| 4348 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4349 | uint32_t latency = 0; |
| 4350 | status_t result = mOutput->stream->getLatency(&latency); |
| 4351 | ALOGE_IF(result != OK, |
| 4352 | "Error when retrieving output stream latency: %d", result); |
| 4353 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4354 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4355 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 4356 | // track stays in active list until presentation is complete |
| 4357 | break; |
| 4358 | } |
| 4359 | } |
| 4360 | if (track->isStopping_2()) { |
| 4361 | track->mState = TrackBase::STOPPED; |
| 4362 | } |
| 4363 | if (track->isStopped()) { |
| 4364 | // Can't reset directly, as fast mixer is still polling this track |
| 4365 | // track->reset(); |
| 4366 | // So instead mark this track as needing to be reset after push with ack |
| 4367 | resetMask |= 1 << i; |
| 4368 | } |
| 4369 | isActive = false; |
| 4370 | break; |
| 4371 | case TrackBase::IDLE: |
| 4372 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4373 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | if (isActive) { |
| 4377 | // was it previously inactive? |
| 4378 | if (!(state->mTrackMask & (1 << j))) { |
| 4379 | ExtendedAudioBufferProvider *eabp = track; |
| 4380 | VolumeProvider *vp = track; |
| 4381 | fastTrack->mBufferProvider = eabp; |
| 4382 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4383 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4384 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4385 | fastTrack->mGeneration++; |
| 4386 | state->mTrackMask |= 1 << j; |
| 4387 | didModify = true; |
| 4388 | // no acknowledgement required for newly active tracks |
| 4389 | } |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 4390 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4391 | // cache the combined master volume and stream type volume for fast mixer; this |
| 4392 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4393 | const float vh = track->getVolumeHandler()->getVolume( |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 4394 | proxy->framesReleased()).first; |
| 4395 | float volume = masterVolume |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4396 | * mStreamTypes[track->streamType()].volume |
| 4397 | * vh; |
Kevin Rocard | b0eeaf12 | 2018-04-11 08:30:16 -0700 | [diff] [blame] | 4398 | track->mCachedVolume = volume; |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 4399 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4400 | float vlf = volume * float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4401 | float vrf = volume * float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4402 | track->setFinalVolume((vlf + vrf) / 2.f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4403 | ++fastTracks; |
| 4404 | } else { |
| 4405 | // was it previously active? |
| 4406 | if (state->mTrackMask & (1 << j)) { |
| 4407 | fastTrack->mBufferProvider = NULL; |
| 4408 | fastTrack->mGeneration++; |
| 4409 | state->mTrackMask &= ~(1 << j); |
| 4410 | didModify = true; |
| 4411 | // If any fast tracks were removed, we must wait for acknowledgement |
| 4412 | // because we're about to decrement the last sp<> on those tracks. |
| 4413 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4414 | } else { |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 4415 | // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an |
| 4416 | // AudioTrack may start (which may not be with a start() but with a write() |
| 4417 | // after underrun) and immediately paused or released. In that case the |
| 4418 | // FastTrack state hasn't had time to update. |
| 4419 | // TODO Remove the ALOGW when this theory is confirmed. |
| 4420 | ALOGW("fast track %d should have been active; " |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4421 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 4422 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 4423 | track->sharedBuffer() != 0); |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 4424 | // Since the FastMixer state already has the track inactive, do nothing here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4425 | } |
| 4426 | tracksToRemove->add(track); |
| 4427 | // Avoids a misleading display in dumpsys |
| 4428 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 4429 | } |
| 4430 | continue; |
| 4431 | } |
| 4432 | |
| 4433 | { // local variable scope to avoid goto warning |
| 4434 | |
| 4435 | audio_track_cblk_t* cblk = track->cblk(); |
| 4436 | |
| 4437 | // The first time a track is added we wait |
| 4438 | // for all its buffers to be filled before processing it |
| 4439 | int name = track->name(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4440 | |
| 4441 | // if an active track doesn't exist in the AudioMixer, create it. |
| 4442 | if (!mAudioMixer->exists(name)) { |
| 4443 | status_t status = mAudioMixer->create( |
| 4444 | name, |
| 4445 | track->mChannelMask, |
| 4446 | track->mFormat, |
| 4447 | track->mSessionId); |
| 4448 | if (status != OK) { |
| 4449 | ALOGW("%s: cannot create track name" |
| 4450 | " %d, mask %#x, format %#x, sessionId %d in AudioMixer", |
| 4451 | __func__, name, track->mChannelMask, track->mFormat, track->mSessionId); |
| 4452 | tracksToRemove->add(track); |
| 4453 | track->invalidate(); // consider it dead. |
| 4454 | continue; |
| 4455 | } |
| 4456 | } |
| 4457 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4458 | // make sure that we have enough frames to mix one full buffer. |
| 4459 | // enforce this condition only once to enable draining the buffer in case the client |
| 4460 | // app does not call stop() and relies on underrun to stop: |
| 4461 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4462 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4463 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4464 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4465 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4466 | |
| 4467 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4468 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4469 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4470 | // add frames already consumed but not yet released by the resampler |
| 4471 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4472 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4473 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4474 | uint32_t minFrames = 1; |
| 4475 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4476 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4477 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4478 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4479 | |
| 4480 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4481 | if (ATRACE_ENABLED()) { |
| 4482 | // I wish we had formatted trace names |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4483 | std::string traceName("nRdy"); |
| 4484 | traceName += std::to_string(track->name()); |
| 4485 | ATRACE_INT(traceName.c_str(), framesReady); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4486 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4487 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4488 | !track->isPaused() && !track->isTerminated()) |
| 4489 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4490 | 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] | 4491 | |
| 4492 | mixedTracks++; |
| 4493 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4494 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4495 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4496 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4497 | if (track->mainBuffer() != mSinkBuffer && |
| 4498 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4499 | if (mEffectBufferEnabled) { |
| 4500 | mEffectBufferValid = true; // Later can set directly. |
| 4501 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4502 | chain = getEffectChain_l(track->sessionId()); |
| 4503 | // Delegate volume control to effect in track effect chain if needed |
| 4504 | if (chain != 0) { |
| 4505 | tracksWithEffect++; |
| 4506 | } else { |
| 4507 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4508 | "session %d", |
| 4509 | name, track->sessionId()); |
| 4510 | } |
| 4511 | } |
| 4512 | |
| 4513 | |
| 4514 | int param = AudioMixer::VOLUME; |
| 4515 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4516 | // no ramp for the first volume setting |
| 4517 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4518 | if (track->mState == TrackBase::RESUMING) { |
| 4519 | track->mState = TrackBase::ACTIVE; |
| 4520 | param = AudioMixer::RAMP_VOLUME; |
| 4521 | } |
| 4522 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4523 | mLeftVolFloat = -1.0; |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4524 | // FIXME should not make a decision based on mServer |
| 4525 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4526 | // If the track is stopped before the first frame was mixed, |
| 4527 | // do not apply ramp |
| 4528 | param = AudioMixer::RAMP_VOLUME; |
| 4529 | } |
| 4530 | |
| 4531 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4532 | uint32_t vl, vr; // in U8.24 integer format |
| 4533 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4534 | // read original volumes with volume control |
| 4535 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4536 | float v = masterVolume * typeVolume; |
| 4537 | |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4538 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4539 | vl = vr = 0; |
| 4540 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4541 | if (track->isPausing()) { |
| 4542 | track->setPaused(); |
| 4543 | } |
| 4544 | } else { |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4545 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4546 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4547 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4548 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4549 | // 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] | 4550 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4551 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4552 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4553 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4554 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4555 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4556 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4557 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4558 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4559 | track->mAudioTrackServerProxy->framesReleased()).first; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4560 | // now apply the master volume and stream type volume and shaper volume |
| 4561 | vlf *= v * vh; |
| 4562 | vrf *= v * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4563 | // 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] | 4564 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4565 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4566 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4567 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4568 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4569 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4570 | // send level comes from shared memory and so may be corrupt |
| 4571 | if (sendLevel > MAX_GAIN_INT) { |
| 4572 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4573 | sendLevel = MAX_GAIN_INT; |
| 4574 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4575 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4576 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4577 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4578 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 4579 | track->setFinalVolume((vrf + vlf) / 2.f); |
| 4580 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4581 | // Delegate volume control to effect in track effect chain if needed |
| 4582 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4583 | // Do not ramp volume if volume is controlled by effect |
| 4584 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4585 | // Update remaining floating point volume levels |
| 4586 | vlf = (float)vl / (1 << 24); |
| 4587 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4588 | track->mHasVolumeController = true; |
| 4589 | } else { |
| 4590 | // force no volume ramp when volume controller was just disabled or removed |
| 4591 | // from effect chain to avoid volume spike |
| 4592 | if (track->mHasVolumeController) { |
| 4593 | param = AudioMixer::VOLUME; |
| 4594 | } |
| 4595 | track->mHasVolumeController = false; |
| 4596 | } |
| 4597 | |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4598 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is |
| 4599 | // still applied by the mixer. |
| 4600 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { |
| 4601 | v = mStreamTypes[track->streamType()].mute ? 0.0f : v; |
| 4602 | if (v != mLeftVolFloat) { |
| 4603 | status_t result = mOutput->stream->setVolume(v, v); |
| 4604 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); |
| 4605 | if (result == OK) { |
| 4606 | mLeftVolFloat = v; |
| 4607 | } |
| 4608 | } |
| 4609 | // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we |
| 4610 | // remove stream volume contribution from software volume. |
| 4611 | if (v != 0.0f && mLeftVolFloat == v) { |
| 4612 | vlf = min(1.0f, vlf / v); |
| 4613 | vrf = min(1.0f, vrf / v); |
| 4614 | vaf = min(1.0f, vaf / v); |
| 4615 | } |
| 4616 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4617 | // XXX: these things DON'T need to be done each time |
| 4618 | mAudioMixer->setBufferProvider(name, track); |
| 4619 | mAudioMixer->enable(name); |
| 4620 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4621 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4622 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4623 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4624 | mAudioMixer->setParameter( |
| 4625 | name, |
| 4626 | AudioMixer::TRACK, |
| 4627 | AudioMixer::FORMAT, (void *)track->format()); |
| 4628 | mAudioMixer->setParameter( |
| 4629 | name, |
| 4630 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4631 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4632 | mAudioMixer->setParameter( |
| 4633 | name, |
| 4634 | AudioMixer::TRACK, |
| 4635 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4636 | // 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] | 4637 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4638 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4639 | if (reqSampleRate == 0) { |
| 4640 | reqSampleRate = mSampleRate; |
| 4641 | } else if (reqSampleRate > maxSampleRate) { |
| 4642 | reqSampleRate = maxSampleRate; |
| 4643 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4644 | mAudioMixer->setParameter( |
| 4645 | name, |
| 4646 | AudioMixer::RESAMPLE, |
| 4647 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4648 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4649 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4650 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4651 | mAudioMixer->setParameter( |
| 4652 | name, |
| 4653 | AudioMixer::TIMESTRETCH, |
| 4654 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4655 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4656 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4657 | /* |
| 4658 | * Select the appropriate output buffer for the track. |
| 4659 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4660 | * Tracks with effects go into their own effects chain buffer |
| 4661 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4662 | * |
| 4663 | * Other tracks can use mMixerBuffer for higher precision |
| 4664 | * channel accumulation. If this buffer is enabled |
| 4665 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4666 | * into it. |
| 4667 | * |
| 4668 | */ |
| 4669 | if (mMixerBufferEnabled |
| 4670 | && (track->mainBuffer() == mSinkBuffer |
| 4671 | || track->mainBuffer() == mMixerBuffer)) { |
| 4672 | mAudioMixer->setParameter( |
| 4673 | name, |
| 4674 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4675 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4676 | mAudioMixer->setParameter( |
| 4677 | name, |
| 4678 | AudioMixer::TRACK, |
| 4679 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4680 | // TODO: override track->mainBuffer()? |
| 4681 | mMixerBufferValid = true; |
| 4682 | } else { |
| 4683 | mAudioMixer->setParameter( |
| 4684 | name, |
| 4685 | AudioMixer::TRACK, |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 4686 | AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4687 | mAudioMixer->setParameter( |
| 4688 | name, |
| 4689 | AudioMixer::TRACK, |
| 4690 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4691 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4692 | mAudioMixer->setParameter( |
| 4693 | name, |
| 4694 | AudioMixer::TRACK, |
| 4695 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4696 | |
| 4697 | // reset retry count |
| 4698 | track->mRetryCount = kMaxTrackRetries; |
| 4699 | |
| 4700 | // If one track is ready, set the mixer ready if: |
| 4701 | // - the mixer was not ready during previous round OR |
| 4702 | // - no other track is not ready |
| 4703 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4704 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4705 | mixerStatus = MIXER_TRACKS_READY; |
| 4706 | } |
| 4707 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4708 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4709 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4710 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4711 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4712 | } else { |
| 4713 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4714 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4715 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4716 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4717 | // previous audio buffer again to effects |
| 4718 | chain = getEffectChain_l(track->sessionId()); |
| 4719 | if (chain != 0) { |
| 4720 | chain->clearInputBuffer(); |
| 4721 | } |
| 4722 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4723 | 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] | 4724 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4725 | track->isStopped() || track->isPaused()) { |
| 4726 | // We have consumed all the buffers of this track. |
| 4727 | // Remove it from the list of active tracks. |
| 4728 | // TODO: use actual buffer filling status instead of latency when available from |
| 4729 | // audio HAL |
| 4730 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4731 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4732 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4733 | if (track->isStopped()) { |
| 4734 | track->reset(); |
| 4735 | } |
| 4736 | tracksToRemove->add(track); |
| 4737 | } |
| 4738 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4739 | // No buffers for this track. Give it a few chances to |
| 4740 | // fill a buffer, then remove it from active list. |
| 4741 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4742 | 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] | 4743 | tracksToRemove->add(track); |
| 4744 | // indicate to client process that the track was disabled because of underrun; |
| 4745 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4746 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4747 | // If one track is not ready, mark the mixer also not ready if: |
| 4748 | // - the mixer was ready during previous round OR |
| 4749 | // - no other track is ready |
| 4750 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4751 | mixerStatus != MIXER_TRACKS_READY) { |
| 4752 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4753 | } |
| 4754 | } |
| 4755 | mAudioMixer->disable(name); |
| 4756 | } |
| 4757 | |
| 4758 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4759 | |
| 4760 | } |
| 4761 | |
| 4762 | // Push the new FastMixer state if necessary |
| 4763 | bool pauseAudioWatchdog = false; |
| 4764 | if (didModify) { |
| 4765 | state->mFastTracksGen++; |
| 4766 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4767 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4768 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4769 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4770 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4771 | state->mColdGen++; |
| 4772 | mFastMixerFutex = 0; |
| 4773 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4774 | mNormalSink = mOutputSink; |
| 4775 | } |
| 4776 | // If we go into cold idle, need to wait for acknowledgement |
| 4777 | // so that fast mixer stops doing I/O. |
| 4778 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4779 | pauseAudioWatchdog = true; |
| 4780 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4781 | } |
| 4782 | if (sq != NULL) { |
| 4783 | sq->end(didModify); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4784 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread |
| 4785 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE |
| 4786 | // when bringing the output sink into standby.) |
| 4787 | // |
| 4788 | // We will get the latest FastMixer state when we come out of COLD_IDLE. |
| 4789 | // |
| 4790 | // This occurs with BT suspend when we idle the FastMixer with |
| 4791 | // active tracks, which may be added or removed. |
| 4792 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4793 | } |
| 4794 | #ifdef AUDIO_WATCHDOG |
| 4795 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4796 | mAudioWatchdog->pause(); |
| 4797 | } |
| 4798 | #endif |
| 4799 | |
| 4800 | // Now perform the deferred reset on fast tracks that have stopped |
| 4801 | while (resetMask != 0) { |
| 4802 | size_t i = __builtin_ctz(resetMask); |
| 4803 | ALOG_ASSERT(i < count); |
| 4804 | resetMask &= ~(1 << i); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4805 | sp<Track> track = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4806 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4807 | track->reset(); |
| 4808 | } |
| 4809 | |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 4810 | // Track destruction may occur outside of threadLoop once it is removed from active tracks. |
| 4811 | // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if |
| 4812 | // it ceases to be active, to allow safe removal from the AudioMixer at the start |
| 4813 | // of prepareTracks_l(); this releases any outstanding buffer back to the track. |
| 4814 | // See also the implementation of destroyTrack_l(). |
| 4815 | for (const auto &track : *tracksToRemove) { |
| 4816 | const int name = track->name(); |
| 4817 | if (mAudioMixer->exists(name)) { // Normal tracks here, fast tracks in FastMixer. |
| 4818 | mAudioMixer->setBufferProvider(name, nullptr /* bufferProvider */); |
| 4819 | } |
| 4820 | } |
| 4821 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4822 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4823 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4824 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4825 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4826 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4830 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4831 | // passing a non-clean buffer to the effect chain |
| 4832 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4833 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4834 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4835 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4836 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4837 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4838 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4839 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4840 | if (mMixerBufferValid) { |
| 4841 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4842 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4843 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4844 | // after mixing. |
| 4845 | // |
| 4846 | // To enforce this guarantee: |
| 4847 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4848 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4849 | // must imply MIXER_TRACKS_READY. |
| 4850 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4851 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4852 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4853 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | // if any fast tracks, then status is ready |
| 4857 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4858 | if (fastTracks > 0) { |
| 4859 | mixerStatus = MIXER_TRACKS_READY; |
| 4860 | } |
| 4861 | return mixerStatus; |
| 4862 | } |
| 4863 | |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4864 | // trackCountForUid_l() must be called with ThreadBase::mLock held |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4865 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) const |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4866 | { |
| 4867 | uint32_t trackCount = 0; |
| 4868 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 4869 | if (mTracks[i]->uid() == uid) { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4870 | trackCount++; |
| 4871 | } |
| 4872 | } |
| 4873 | return trackCount; |
| 4874 | } |
| 4875 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4876 | // isTrackAllowed_l() must be called with ThreadBase::mLock held |
| 4877 | bool AudioFlinger::MixerThread::isTrackAllowed_l( |
| 4878 | audio_channel_mask_t channelMask, audio_format_t format, |
| 4879 | audio_session_t sessionId, uid_t uid) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4880 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4881 | if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 4882 | return false; |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4883 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4884 | // Check validity as we don't call AudioMixer::create() here. |
| 4885 | if (!AudioMixer::isValidFormat(format)) { |
| 4886 | ALOGW("%s: invalid format: %#x", __func__, format); |
| 4887 | return false; |
| 4888 | } |
| 4889 | if (!AudioMixer::isValidChannelMask(channelMask)) { |
| 4890 | ALOGW("%s: invalid channelMask: %#x", __func__, channelMask); |
| 4891 | return false; |
| 4892 | } |
| 4893 | return true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4894 | } |
| 4895 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4896 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4897 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4898 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4899 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4900 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4901 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4902 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4903 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4904 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4905 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4906 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4907 | AudioParameter param = AudioParameter(keyValuePair); |
| 4908 | int value; |
| 4909 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4910 | reconfig = true; |
| 4911 | } |
| 4912 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4913 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4914 | status = BAD_VALUE; |
| 4915 | } else { |
| 4916 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4917 | reconfig = true; |
| 4918 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4919 | } |
| 4920 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4921 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4922 | status = BAD_VALUE; |
| 4923 | } else { |
| 4924 | // no need to save value, since it's constant |
| 4925 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4926 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4927 | } |
| 4928 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4929 | // do not accept frame count changes if tracks are open as the track buffer |
| 4930 | // size depends on frame count and correct behavior would not be guaranteed |
| 4931 | // if frame count is changed after track creation |
| 4932 | if (!mTracks.isEmpty()) { |
| 4933 | status = INVALID_OPERATION; |
| 4934 | } else { |
| 4935 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4936 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4937 | } |
| 4938 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4939 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4940 | // when changing the audio output device, call addBatteryData to notify |
| 4941 | // the change |
| 4942 | if (mOutDevice != value) { |
| 4943 | uint32_t params = 0; |
| 4944 | // check whether speaker is on |
| 4945 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4946 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4947 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4948 | |
| 4949 | audio_devices_t deviceWithoutSpeaker |
| 4950 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4951 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4952 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4953 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4954 | } |
| 4955 | |
| 4956 | if (params != 0) { |
| 4957 | addBatteryData(params); |
| 4958 | } |
| 4959 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4960 | #endif |
| 4961 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4962 | // forward device change to effects that have requested to be |
| 4963 | // aware of attached audio device. |
| 4964 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4965 | a2dpDeviceChanged = |
| 4966 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4967 | mOutDevice = value; |
| 4968 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4969 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4970 | } |
| 4971 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4972 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4973 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4974 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4975 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4976 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4977 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4978 | mStandby = true; |
| 4979 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4980 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4981 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4982 | if (status == NO_ERROR && reconfig) { |
| 4983 | readOutputParameters_l(); |
| 4984 | delete mAudioMixer; |
| 4985 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4986 | for (const auto &track : mTracks) { |
| 4987 | const int name = track->name(); |
| 4988 | status_t status = mAudioMixer->create( |
| 4989 | name, |
| 4990 | track->mChannelMask, |
| 4991 | track->mFormat, |
| 4992 | track->mSessionId); |
| 4993 | ALOGW_IF(status != NO_ERROR, |
| 4994 | "%s: cannot create track name" |
| 4995 | " %d, mask %#x, format %#x, sessionId %d in AudioMixer", |
| 4996 | __func__, |
| 4997 | name, track->mChannelMask, track->mFormat, track->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4998 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4999 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5000 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5001 | } |
| 5002 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5003 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | |
| 5007 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 5008 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5009 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 5010 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 5011 | dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5012 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5013 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5014 | if (hasFastMixer()) { |
| 5015 | dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); |
| 5016 | |
| 5017 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 5018 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 5019 | // This is a large object so we place it on the heap. |
| 5020 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 5021 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 5022 | copy->dump(fd); |
| 5023 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5024 | |
| 5025 | #ifdef STATE_QUEUE_DUMP |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5026 | // Similar for state queue |
| 5027 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 5028 | observerCopy.dump(fd); |
| 5029 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 5030 | mutatorCopy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5031 | #endif |
| 5032 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5033 | #ifdef AUDIO_WATCHDOG |
| 5034 | if (mAudioWatchdog != 0) { |
| 5035 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 5036 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 5037 | wdCopy.dump(fd); |
| 5038 | } |
| 5039 | #endif |
| 5040 | |
| 5041 | } else { |
| 5042 | dprintf(fd, " No FastMixer\n"); |
| 5043 | } |
| 5044 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5045 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5046 | // Write the tee output to a .wav file |
Glenn Kasten | 5b2191a | 2016-08-19 11:44:47 -0700 | [diff] [blame] | 5047 | dumpTee(fd, mTeeSource, mId, 'M'); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5048 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5049 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 5053 | { |
| 5054 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5055 | } |
| 5056 | |
| 5057 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 5058 | { |
| 5059 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 5060 | } |
| 5061 | |
| 5062 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 5063 | { |
| 5064 | PlaybackThread::cacheParameters_l(); |
| 5065 | |
| 5066 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 5067 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 5068 | // increase threshold again due to low power audio mode. The way this warning |
| 5069 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 5070 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 5071 | } |
| 5072 | |
| 5073 | // ---------------------------------------------------------------------------- |
| 5074 | |
| 5075 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5076 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 5077 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5078 | { |
| 5079 | } |
| 5080 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5081 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 5082 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5083 | ThreadBase::type_t type, bool systemReady) |
| 5084 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5085 | , mVolumeShaperActive(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5086 | { |
| 5087 | } |
| 5088 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5089 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 5090 | { |
| 5091 | } |
| 5092 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5093 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5094 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5095 | float left, right; |
| 5096 | |
| 5097 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 5098 | left = right = 0; |
| 5099 | } else { |
| 5100 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 5101 | float v = mMasterVolume * typeVolume; |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 5102 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5103 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5104 | // Get volumeshaper scaling |
| 5105 | std::pair<float /* volume */, bool /* active */> |
| 5106 | vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5107 | track->mAudioTrackServerProxy->framesReleased()); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5108 | v *= vh.first; |
| 5109 | mVolumeShaperActive = vh.second; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5110 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5111 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 5112 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5113 | if (left > GAIN_FLOAT_UNITY) { |
| 5114 | left = GAIN_FLOAT_UNITY; |
| 5115 | } |
| 5116 | left *= v; |
| 5117 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 5118 | if (right > GAIN_FLOAT_UNITY) { |
| 5119 | right = GAIN_FLOAT_UNITY; |
| 5120 | } |
| 5121 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | if (lastTrack) { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5125 | track->setFinalVolume((left + right) / 2.f); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5126 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 5127 | mLeftVolFloat = left; |
| 5128 | mRightVolFloat = right; |
| 5129 | |
| 5130 | // Convert volumes from float to 8.24 |
| 5131 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 5132 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 5133 | |
| 5134 | // Delegate volume control to effect in track effect chain if needed |
| 5135 | // only one effect chain can be present on DirectOutputThread, so if |
| 5136 | // there is one, the track is connected to it |
| 5137 | if (!mEffectChains.isEmpty()) { |
| 5138 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 5139 | left = (float)vl / (1 << 24); |
| 5140 | right = (float)vr / (1 << 24); |
| 5141 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5142 | status_t result = mOutput->stream->setVolume(left, right); |
| 5143 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | } |
| 5147 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5148 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 5149 | { |
| 5150 | sp<Track> previousTrack = mPreviousTrack.promote(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5151 | sp<Track> latestTrack = mActiveTracks.getLatest(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5152 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5153 | if (previousTrack != 0 && latestTrack != 0) { |
| 5154 | if (mType == DIRECT) { |
| 5155 | if (previousTrack.get() != latestTrack.get()) { |
| 5156 | mFlushPending = true; |
| 5157 | } |
| 5158 | } else /* mType == OFFLOAD */ { |
| 5159 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 5160 | mFlushPending = true; |
| 5161 | } |
| 5162 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5163 | } |
| 5164 | PlaybackThread::onAddNewTrack_l(); |
| 5165 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5166 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5167 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 5168 | Vector< sp<Track> > *tracksToRemove |
| 5169 | ) |
| 5170 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5171 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5172 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5173 | bool doHwPause = false; |
| 5174 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5175 | |
| 5176 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5177 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5178 | if (t->isInvalid()) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5179 | ALOGW("An invalidated track shouldn't be in active list"); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5180 | tracksToRemove->add(t); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5181 | continue; |
| 5182 | } |
| 5183 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5184 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5185 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5186 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5187 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5188 | // Only consider last track started for volume and mixer state control. |
| 5189 | // In theory an older track could underrun and restart after the new one starts |
| 5190 | // but as we only care about the transition phase between two tracks on a |
| 5191 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5192 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5193 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5194 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5195 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5196 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5197 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5198 | doHwPause = true; |
| 5199 | mHwPaused = true; |
| 5200 | } |
| 5201 | tracksToRemove->add(track); |
| 5202 | } else if (track->isFlushPending()) { |
| 5203 | track->flushAck(); |
| 5204 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5205 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5206 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5207 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5208 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5209 | if (last) { |
| 5210 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5211 | if (mHwPaused) { |
| 5212 | doHwResume = true; |
| 5213 | mHwPaused = false; |
| 5214 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5215 | } |
| 5216 | } |
| 5217 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5218 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5219 | // for all its buffers to be filled before processing it. |
| 5220 | // Allow draining the buffer in case the client |
| 5221 | // app does not call stop() and relies on underrun to stop: |
| 5222 | // hence the test on (track->mRetryCount > 1). |
| 5223 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5224 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5225 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5226 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5227 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5228 | minFrames = mNormalFrameCount; |
| 5229 | } else { |
| 5230 | minFrames = 1; |
| 5231 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5232 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5233 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 5234 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5235 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5236 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5237 | |
| 5238 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5239 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5240 | if (last) { |
| 5241 | // make sure processVolume_l() will apply new volume even if 0 |
| 5242 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5243 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5244 | if (!mHwSupportsPause) { |
| 5245 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
| 5249 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5250 | processVolume_l(track, last); |
| 5251 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5252 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5253 | if (previousTrack != 0) { |
| 5254 | if (track != previousTrack.get()) { |
| 5255 | // Flush any data still being written from last track |
| 5256 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5257 | // Invalidate previous track to force a seek when resuming. |
| 5258 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5259 | } |
| 5260 | } |
| 5261 | mPreviousTrack = track; |
| 5262 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5263 | // reset retry count |
| 5264 | track->mRetryCount = kMaxTrackRetriesDirect; |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5265 | mActiveTrack = t; |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5266 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5267 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5268 | doHwResume = true; |
| 5269 | mHwPaused = false; |
| 5270 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5271 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5272 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5273 | // clear effect chain input buffer if the last active track started underruns |
| 5274 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5275 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5276 | mEffectChains[0]->clearInputBuffer(); |
| 5277 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5278 | if (track->isStopping_1()) { |
| 5279 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5280 | if (last && mHwPaused) { |
| 5281 | doHwResume = true; |
| 5282 | mHwPaused = false; |
| 5283 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5284 | } |
| 5285 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 5286 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5287 | // We have consumed all the buffers of this track. |
| 5288 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5289 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5290 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5291 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 5292 | } else { |
| 5293 | audioHALFrames = 0; |
| 5294 | } |
| 5295 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5296 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5297 | if (mStandby || !last || |
| 5298 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5299 | if (track->isStopping_2()) { |
| 5300 | track->mState = TrackBase::STOPPED; |
| 5301 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5302 | if (track->isStopped()) { |
| 5303 | track->reset(); |
| 5304 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5305 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5306 | } |
| 5307 | } else { |
| 5308 | // No buffers for this track. Give it a few chances to |
| 5309 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5310 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5311 | if (--(track->mRetryCount) <= 0) { |
| 5312 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5313 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5314 | // indicate to client process that the track was disabled because of underrun; |
| 5315 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5316 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5317 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5318 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 5319 | "minFrames = %u, mFormat = %#x", |
| 5320 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5321 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5322 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5323 | doHwPause = true; |
| 5324 | mHwPaused = true; |
| 5325 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5326 | } |
| 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5331 | // 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] | 5332 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5333 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5334 | if (mTracks[i]->isFlushPending()) { |
| 5335 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5336 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5337 | } |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5342 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5343 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5344 | // if resume is received before pause is executed. |
| 5345 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5346 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5347 | status_t result = mOutput->stream->pause(); |
| 5348 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5349 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5350 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5351 | flushHw_l(); |
| 5352 | } |
| 5353 | if (mHwSupportsPause && !mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5354 | status_t result = mOutput->stream->resume(); |
| 5355 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5356 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5357 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5358 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5359 | |
| 5360 | return mixerStatus; |
| 5361 | } |
| 5362 | |
| 5363 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 5364 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5365 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5366 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5367 | // output audio to hardware |
| 5368 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5369 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5370 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5371 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 5372 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5373 | // no need to pad with 0 for compressed audio |
| 5374 | if (audio_has_proportional_frames(mFormat)) { |
| 5375 | memset(curBuf, 0, frameCount * mFrameSize); |
| 5376 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5377 | break; |
| 5378 | } |
| 5379 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 5380 | frameCount -= buffer.frameCount; |
| 5381 | curBuf += buffer.frameCount * mFrameSize; |
| 5382 | mActiveTrack->releaseBuffer(&buffer); |
| 5383 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5384 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5385 | mSleepTimeUs = 0; |
| 5386 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5387 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 5391 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5392 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5393 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5394 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5395 | return; |
| 5396 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5397 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5398 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5399 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5400 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5401 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5402 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5403 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5404 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5405 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5406 | } |
| 5407 | } |
| 5408 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5409 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 5410 | { |
| 5411 | { |
| 5412 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5413 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5414 | if (mTracks[i]->isFlushPending()) { |
| 5415 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5416 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5417 | } |
| 5418 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5419 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5420 | flushHw_l(); |
| 5421 | } |
| 5422 | } |
| 5423 | PlaybackThread::threadLoop_exit(); |
| 5424 | } |
| 5425 | |
| 5426 | // must be called with thread mutex locked |
| 5427 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 5428 | { |
| 5429 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5430 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5431 | |
vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5432 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { |
| 5433 | return !mStandby; |
| 5434 | } |
| 5435 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5436 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 5437 | // after a timeout and we will enter standby then. |
| 5438 | if (mTracks.size() > 0) { |
| 5439 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5440 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 5441 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5442 | } |
| 5443 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5444 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5445 | } |
| 5446 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5447 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 5448 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5449 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5450 | { |
| 5451 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5452 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5453 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5454 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5455 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5456 | AudioParameter param = AudioParameter(keyValuePair); |
| 5457 | int value; |
| 5458 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5459 | // forward device change to effects that have requested to be |
| 5460 | // aware of attached audio device. |
| 5461 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5462 | a2dpDeviceChanged = |
| 5463 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5464 | mOutDevice = value; |
| 5465 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5466 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5467 | } |
| 5468 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5469 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5470 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5471 | // do not accept frame count changes if tracks are open as the track buffer |
| 5472 | // size depends on frame count and correct behavior would not be garantied |
| 5473 | // if frame count is changed after track creation |
| 5474 | if (!mTracks.isEmpty()) { |
| 5475 | status = INVALID_OPERATION; |
| 5476 | } else { |
| 5477 | reconfig = true; |
| 5478 | } |
| 5479 | } |
| 5480 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5481 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5482 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5483 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5484 | mStandby = true; |
| 5485 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5486 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5487 | } |
| 5488 | if (status == NO_ERROR && reconfig) { |
| 5489 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5490 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5491 | } |
| 5492 | } |
| 5493 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5494 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5498 | { |
| 5499 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5500 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5501 | time = PlaybackThread::activeSleepTimeUs(); |
| 5502 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5503 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5504 | } |
| 5505 | return time; |
| 5506 | } |
| 5507 | |
| 5508 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5509 | { |
| 5510 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5511 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5512 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5513 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5514 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5515 | } |
| 5516 | return time; |
| 5517 | } |
| 5518 | |
| 5519 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5520 | { |
| 5521 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5522 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5523 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5524 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5525 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5526 | } |
| 5527 | return time; |
| 5528 | } |
| 5529 | |
| 5530 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5531 | { |
| 5532 | PlaybackThread::cacheParameters_l(); |
| 5533 | |
| 5534 | // use shorter standby delay as on normal output to release |
| 5535 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5536 | // no delay on outputs with HW A/V sync |
| 5537 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5538 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5539 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5540 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5541 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5542 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5543 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5544 | } |
| 5545 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5546 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5547 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5548 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5549 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5550 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5551 | } |
| 5552 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5553 | int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const { |
| 5554 | // If a VolumeShaper is active, we must wake up periodically to update volume. |
| 5555 | const int64_t NS_PER_MS = 1000000; |
| 5556 | return mVolumeShaperActive ? |
| 5557 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); |
| 5558 | } |
| 5559 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5560 | // ---------------------------------------------------------------------------- |
| 5561 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5562 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5563 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5564 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5565 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5566 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5567 | mDrainSequence(0), |
| 5568 | mAsyncError(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5569 | { |
| 5570 | } |
| 5571 | |
| 5572 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5573 | { |
| 5574 | } |
| 5575 | |
| 5576 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5577 | { |
| 5578 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5579 | } |
| 5580 | |
| 5581 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5582 | { |
| 5583 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5584 | uint32_t writeAckSequence; |
| 5585 | uint32_t drainSequence; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5586 | bool asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5587 | |
| 5588 | { |
| 5589 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5590 | while (!((mWriteAckSequence & 1) || |
| 5591 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5592 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5593 | exitPending())) { |
| 5594 | mWaitWorkCV.wait(mLock); |
| 5595 | } |
| 5596 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5597 | if (exitPending()) { |
| 5598 | break; |
| 5599 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5600 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5601 | mWriteAckSequence, mDrainSequence); |
| 5602 | writeAckSequence = mWriteAckSequence; |
| 5603 | mWriteAckSequence &= ~1; |
| 5604 | drainSequence = mDrainSequence; |
| 5605 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5606 | asyncError = mAsyncError; |
| 5607 | mAsyncError = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5608 | } |
| 5609 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5610 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5611 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5612 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5613 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5614 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5615 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5616 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5617 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5618 | if (asyncError) { |
| 5619 | playbackThread->onAsyncError(); |
| 5620 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5621 | } |
| 5622 | } |
| 5623 | } |
| 5624 | return false; |
| 5625 | } |
| 5626 | |
| 5627 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5628 | { |
| 5629 | ALOGV("AsyncCallbackThread::exit"); |
| 5630 | Mutex::Autolock _l(mLock); |
| 5631 | requestExit(); |
| 5632 | mWaitWorkCV.broadcast(); |
| 5633 | } |
| 5634 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5635 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5636 | { |
| 5637 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5638 | // bit 0 is cleared |
| 5639 | mWriteAckSequence = sequence << 1; |
| 5640 | } |
| 5641 | |
| 5642 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5643 | { |
| 5644 | Mutex::Autolock _l(mLock); |
| 5645 | // ignore unexpected callbacks |
| 5646 | if (mWriteAckSequence & 2) { |
| 5647 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5648 | mWaitWorkCV.signal(); |
| 5649 | } |
| 5650 | } |
| 5651 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5652 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5653 | { |
| 5654 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5655 | // bit 0 is cleared |
| 5656 | mDrainSequence = sequence << 1; |
| 5657 | } |
| 5658 | |
| 5659 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5660 | { |
| 5661 | Mutex::Autolock _l(mLock); |
| 5662 | // ignore unexpected callbacks |
| 5663 | if (mDrainSequence & 2) { |
| 5664 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5665 | mWaitWorkCV.signal(); |
| 5666 | } |
| 5667 | } |
| 5668 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5669 | void AudioFlinger::AsyncCallbackThread::setAsyncError() |
| 5670 | { |
| 5671 | Mutex::Autolock _l(mLock); |
| 5672 | mAsyncError = true; |
| 5673 | mWaitWorkCV.signal(); |
| 5674 | } |
| 5675 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5676 | |
| 5677 | // ---------------------------------------------------------------------------- |
| 5678 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5679 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5680 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5681 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), |
| 5682 | mOffloadUnderrunPosition(~0LL) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5683 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 5684 | //FIXME: mStandby should be set to true by ThreadBase constructo |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5685 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5686 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5687 | } |
| 5688 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5689 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5690 | { |
| 5691 | if (mFlushPending || mHwPaused) { |
| 5692 | // If a flush is pending or track was paused, just discard buffered data |
| 5693 | flushHw_l(); |
| 5694 | } else { |
| 5695 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5696 | threadLoop_drain(); |
| 5697 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5698 | if (mUseAsyncWrite) { |
| 5699 | ALOG_ASSERT(mCallbackThread != 0); |
| 5700 | mCallbackThread->exit(); |
| 5701 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5702 | PlaybackThread::threadLoop_exit(); |
| 5703 | } |
| 5704 | |
| 5705 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5706 | Vector< sp<Track> > *tracksToRemove |
| 5707 | ) |
| 5708 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5709 | size_t count = mActiveTracks.size(); |
| 5710 | |
| 5711 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5712 | bool doHwPause = false; |
| 5713 | bool doHwResume = false; |
| 5714 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5715 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5716 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5717 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5718 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5719 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5720 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5721 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5722 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5723 | // Only consider last track started for volume and mixer state control. |
| 5724 | // In theory an older track could underrun and restart after the new one starts |
| 5725 | // but as we only care about the transition phase between two tracks on a |
| 5726 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5727 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5728 | bool last = l.get() == track; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5729 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5730 | if (track->isInvalid()) { |
| 5731 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5732 | tracksToRemove->add(track); |
| 5733 | continue; |
| 5734 | } |
| 5735 | |
| 5736 | if (track->mState == TrackBase::IDLE) { |
| 5737 | ALOGW("An idle track shouldn't be in active list"); |
| 5738 | continue; |
| 5739 | } |
| 5740 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5741 | if (track->isPausing()) { |
| 5742 | track->setPaused(); |
| 5743 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5744 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5745 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5746 | mHwPaused = true; |
| 5747 | } |
| 5748 | // If we were part way through writing the mixbuffer to |
| 5749 | // the HAL we must save this until we resume |
| 5750 | // BUG - this will be wrong if a different track is made active, |
| 5751 | // in that case we want to discard the pending data in the |
| 5752 | // mixbuffer and tell the client to present it again when the |
| 5753 | // track is resumed |
| 5754 | mPausedWriteLength = mCurrentWriteLength; |
| 5755 | mPausedBytesRemaining = mBytesRemaining; |
| 5756 | mBytesRemaining = 0; // stop writing |
| 5757 | } |
| 5758 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5759 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5760 | if (track->isStopping_1()) { |
| 5761 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5762 | } else { |
| 5763 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5764 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5765 | track->flushAck(); |
| 5766 | if (last) { |
| 5767 | mFlushPending = true; |
| 5768 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5769 | } else if (track->isResumePending()){ |
| 5770 | track->resumeAck(); |
| 5771 | if (last) { |
| 5772 | if (mPausedBytesRemaining) { |
| 5773 | // Need to continue write that was interrupted |
| 5774 | mCurrentWriteLength = mPausedWriteLength; |
| 5775 | mBytesRemaining = mPausedBytesRemaining; |
| 5776 | mPausedBytesRemaining = 0; |
| 5777 | } |
| 5778 | if (mHwPaused) { |
| 5779 | doHwResume = true; |
| 5780 | mHwPaused = false; |
| 5781 | // threadLoop_mix() will handle the case that we need to |
| 5782 | // resume an interrupted write |
| 5783 | } |
| 5784 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5785 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5786 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5787 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5788 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5789 | // Do not handle new data in this iteration even if track->framesReady() |
| 5790 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5791 | } |
| 5792 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5793 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5794 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5795 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5796 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5797 | if (last) { |
| 5798 | // make sure processVolume_l() will apply new volume even if 0 |
| 5799 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5800 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5804 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5805 | if (previousTrack != 0) { |
| 5806 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5807 | // Flush any data still being written from last track |
| 5808 | mBytesRemaining = 0; |
| 5809 | if (mPausedBytesRemaining) { |
| 5810 | // Last track was paused so we also need to flush saved |
| 5811 | // mixbuffer state and invalidate track so that it will |
| 5812 | // re-submit that unwritten data when it is next resumed |
| 5813 | mPausedBytesRemaining = 0; |
| 5814 | // Invalidate is a bit drastic - would be more efficient |
| 5815 | // to have a flag to tell client that some of the |
| 5816 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5817 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5818 | } |
| 5819 | // flush data already sent to the DSP if changing audio session as audio |
| 5820 | // comes from a different source. Also invalidate previous track to force a |
| 5821 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5822 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5823 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5824 | } |
| 5825 | } |
| 5826 | } |
| 5827 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5828 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5829 | if (track->isStopping_1()) { |
| 5830 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5831 | } else { |
| 5832 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5833 | } |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5834 | mActiveTrack = t; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5835 | mixerStatus = MIXER_TRACKS_READY; |
| 5836 | } |
| 5837 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5838 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5839 | if (track->isStopping_1()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5840 | if (--(track->mRetryCount) <= 0) { |
| 5841 | // Hardware buffer can hold a large amount of audio so we must |
| 5842 | // wait for all current track's data to drain before we say |
| 5843 | // that the track is stopped. |
| 5844 | if (mBytesRemaining == 0) { |
| 5845 | // Only start draining when all data in mixbuffer |
| 5846 | // has been written |
| 5847 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5848 | track->mState = TrackBase::STOPPING_2; // so presentation completes after |
| 5849 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 5850 | if (last && !mStandby) { |
| 5851 | // do not modify drain sequence if we are already draining. This happens |
| 5852 | // when resuming from pause after drain. |
| 5853 | if ((mDrainSequence & 1) == 0) { |
| 5854 | mSleepTimeUs = 0; |
| 5855 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 5856 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5857 | mDrainSequence += 2; |
| 5858 | } |
| 5859 | if (mHwPaused) { |
| 5860 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5861 | // a resume so we must ensure hardware is running |
| 5862 | doHwResume = true; |
| 5863 | mHwPaused = false; |
| 5864 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5865 | } |
| 5866 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5867 | } else if (last) { |
| 5868 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); |
| 5869 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5870 | } |
| 5871 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5872 | // Drain has completed or we are in standby, signal presentation complete |
| 5873 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5874 | track->mState = TrackBase::STOPPED; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5875 | uint32_t latency = 0; |
| 5876 | status_t result = mOutput->stream->getLatency(&latency); |
| 5877 | ALOGE_IF(result != OK, |
| 5878 | "Error when retrieving output stream latency: %d", result); |
| 5879 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5880 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5881 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5882 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5883 | track->reset(); |
| 5884 | tracksToRemove->add(track); |
| 5885 | } |
| 5886 | } else { |
| 5887 | // No buffers for this track. Give it a few chances to |
| 5888 | // fill a buffer, then remove it from active list. |
| 5889 | if (--(track->mRetryCount) <= 0) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5890 | bool running = false; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5891 | uint64_t position = 0; |
| 5892 | struct timespec unused; |
| 5893 | // The running check restarts the retry counter at least once. |
| 5894 | status_t ret = mOutput->stream->getPresentationPosition(&position, &unused); |
| 5895 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { |
| 5896 | running = true; |
| 5897 | mOffloadUnderrunPosition = position; |
| 5898 | } |
| 5899 | if (ret == NO_ERROR) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5900 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, |
| 5901 | (long long)position, (long long)mOffloadUnderrunPosition); |
| 5902 | } |
| 5903 | if (running) { // still running, give us more time. |
| 5904 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5905 | } else { |
| 5906 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5907 | track->name()); |
| 5908 | tracksToRemove->add(track); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 5909 | // tell client process that the track was disabled because of underrun; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5910 | // it will then automatically call start() when data is available |
| 5911 | track->disable(); |
| 5912 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5913 | } else if (last){ |
| 5914 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5915 | } |
| 5916 | } |
| 5917 | } |
| 5918 | // compute volume for this track |
| 5919 | processVolume_l(track, last); |
| 5920 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5921 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5922 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5923 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5924 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5925 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5926 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5927 | status_t result = mOutput->stream->pause(); |
| 5928 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5929 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5930 | if (mFlushPending) { |
| 5931 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5932 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5933 | if (!mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5934 | status_t result = mOutput->stream->resume(); |
| 5935 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5936 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5937 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5938 | // remove all the tracks that need to be... |
| 5939 | removeTracks_l(*tracksToRemove); |
| 5940 | |
| 5941 | return mixerStatus; |
| 5942 | } |
| 5943 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5944 | // must be called with thread mutex locked |
| 5945 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5946 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5947 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5948 | mWriteAckSequence, mDrainSequence); |
| 5949 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5950 | return true; |
| 5951 | } |
| 5952 | return false; |
| 5953 | } |
| 5954 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5955 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5956 | { |
| 5957 | Mutex::Autolock _l(mLock); |
| 5958 | return waitingAsyncCallback_l(); |
| 5959 | } |
| 5960 | |
| 5961 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5962 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5963 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5964 | // Flush anything still waiting in the mixbuffer |
| 5965 | mCurrentWriteLength = 0; |
| 5966 | mBytesRemaining = 0; |
| 5967 | mPausedWriteLength = 0; |
| 5968 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5969 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 5970 | mBytesWritten = 0; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5971 | mOffloadUnderrunPosition = ~0LL; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5972 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5973 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5974 | // discard any pending drain or write ack by incrementing sequence |
| 5975 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5976 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5977 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5978 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5979 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5980 | } |
| 5981 | } |
| 5982 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5983 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
| 5984 | { |
| 5985 | Mutex::Autolock _l(mLock); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5986 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 5987 | mFlushPending = true; |
| 5988 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5989 | } |
| 5990 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5991 | // ---------------------------------------------------------------------------- |
| 5992 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5993 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5994 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5995 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5996 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5997 | mWaitTimeMs(UINT_MAX) |
| 5998 | { |
| 5999 | addOutputTrack(mainThread); |
| 6000 | } |
| 6001 | |
| 6002 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 6003 | { |
| 6004 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6005 | mOutputTracks[i]->destroy(); |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 6010 | { |
| 6011 | // mix buffers... |
| 6012 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6013 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6014 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 6015 | if (mMixerBufferValid) { |
| 6016 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 6017 | } else { |
| 6018 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 6019 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6020 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6021 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6022 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 6023 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6024 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 6028 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6029 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6030 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6031 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6032 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6033 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6034 | } |
| 6035 | } else if (mBytesWritten != 0) { |
| 6036 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 6037 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 6038 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6039 | } else { |
| 6040 | // flush remaining overflow buffers in output tracks |
| 6041 | writeFrames = 0; |
| 6042 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6043 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6044 | } |
| 6045 | } |
| 6046 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6047 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6048 | { |
| 6049 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6050 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6051 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 6052 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 6053 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 6057 | { |
| 6058 | // DuplicatingThread implements standby by stopping all tracks |
| 6059 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 6060 | outputTracks[i]->stop(); |
| 6061 | } |
| 6062 | } |
| 6063 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6064 | void AudioFlinger::DuplicatingThread::dumpInternals(int fd, const Vector<String16>& args __unused) |
| 6065 | { |
| 6066 | MixerThread::dumpInternals(fd, args); |
| 6067 | |
| 6068 | std::stringstream ss; |
| 6069 | const size_t numTracks = mOutputTracks.size(); |
| 6070 | ss << " " << numTracks << " OutputTracks"; |
| 6071 | if (numTracks > 0) { |
| 6072 | ss << ":"; |
| 6073 | for (const auto &track : mOutputTracks) { |
| 6074 | const sp<ThreadBase> thread = track->thread().promote(); |
| 6075 | ss << " (" << track->name() << " : "; |
| 6076 | if (thread.get() != nullptr) { |
| 6077 | ss << thread.get() << ", " << thread->id(); |
| 6078 | } else { |
| 6079 | ss << "null"; |
| 6080 | } |
| 6081 | ss << ")"; |
| 6082 | } |
| 6083 | } |
| 6084 | ss << "\n"; |
| 6085 | std::string result = ss.str(); |
| 6086 | write(fd, result.c_str(), result.size()); |
| 6087 | } |
| 6088 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6089 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 6090 | { |
| 6091 | outputTracks = mOutputTracks; |
| 6092 | } |
| 6093 | |
| 6094 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 6095 | { |
| 6096 | outputTracks.clear(); |
| 6097 | } |
| 6098 | |
| 6099 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 6100 | { |
| 6101 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6102 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 6103 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 6104 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 6105 | const size_t frameCount = |
| 6106 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 6107 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 6108 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 6109 | // nearly empty and the other may be dropping data). |
| 6110 | |
| 6111 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6112 | this, |
| 6113 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6114 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6115 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6116 | frameCount, |
| 6117 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 6118 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 6119 | if (status != NO_ERROR) { |
| 6120 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 6121 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6122 | } |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 6123 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
| 6124 | mOutputTracks.add(outputTrack); |
| 6125 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 6126 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 6130 | { |
| 6131 | Mutex::Autolock _l(mLock); |
| 6132 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6133 | if (mOutputTracks[i]->thread() == thread) { |
| 6134 | mOutputTracks[i]->destroy(); |
| 6135 | mOutputTracks.removeAt(i); |
| 6136 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 6137 | if (thread->getOutput() == mOutput) { |
| 6138 | mOutput = NULL; |
| 6139 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6140 | return; |
| 6141 | } |
| 6142 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 6143 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | // caller must hold mLock |
| 6147 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 6148 | { |
| 6149 | mWaitTimeMs = UINT_MAX; |
| 6150 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6151 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 6152 | if (strong != 0) { |
| 6153 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 6154 | if (waitTimeMs < mWaitTimeMs) { |
| 6155 | mWaitTimeMs = waitTimeMs; |
| 6156 | } |
| 6157 | } |
| 6158 | } |
| 6159 | } |
| 6160 | |
| 6161 | |
| 6162 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 6163 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 6164 | { |
| 6165 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 6166 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 6167 | if (thread == 0) { |
| 6168 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 6169 | outputTracks[i].get()); |
| 6170 | return false; |
| 6171 | } |
| 6172 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 6173 | // see note at standby() declaration |
| 6174 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 6175 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 6176 | thread.get()); |
| 6177 | return false; |
| 6178 | } |
| 6179 | } |
| 6180 | return true; |
| 6181 | } |
| 6182 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6183 | void AudioFlinger::DuplicatingThread::sendMetadataToBackend_l( |
| 6184 | const StreamOutHalInterface::SourceMetadata& metadata) |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 6185 | { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6186 | for (auto& outputTrack : outputTracks) { // not mOutputTracks |
| 6187 | outputTrack->setMetadatas(metadata.tracks); |
| 6188 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 6189 | } |
| 6190 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6191 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 6192 | { |
| 6193 | return (mWaitTimeMs * 1000) / 2; |
| 6194 | } |
| 6195 | |
| 6196 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 6197 | { |
| 6198 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 6199 | updateWaitTime_l(); |
| 6200 | |
| 6201 | MixerThread::cacheParameters_l(); |
| 6202 | } |
| 6203 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 6204 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6205 | // ---------------------------------------------------------------------------- |
| 6206 | // Record |
| 6207 | // ---------------------------------------------------------------------------- |
| 6208 | |
| 6209 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 6210 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6211 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 6212 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6213 | audio_devices_t inDevice, |
| 6214 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6215 | #ifdef TEE_SINK |
| 6216 | , const sp<NBAIO_Sink>& teeSink |
| 6217 | #endif |
| 6218 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6219 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6220 | mInput(input), |
| 6221 | mActiveTracks(&this->mLocalLog), |
| 6222 | mRsmpInBuffer(NULL), |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6223 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6224 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6225 | #ifdef TEE_SINK |
| 6226 | , mTeeSink(teeSink) |
| 6227 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 6228 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 6229 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6230 | // mFastCapture below |
| 6231 | , mFastCaptureFutex(0) |
| 6232 | // mInputSource |
| 6233 | // mPipeSink |
| 6234 | // mPipeSource |
| 6235 | , mPipeFramesP2(0) |
| 6236 | // mPipeMemory |
| 6237 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6238 | , mFastTrackAvail(false) |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 6239 | , mBtNrecSuspended(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6240 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6241 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 6242 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6243 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6244 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6245 | |
| 6246 | // create an NBAIO source for the HAL input stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 6247 | mInputSource = new AudioStreamInSource(input->stream); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6248 | size_t numCounterOffers = 0; |
| 6249 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6250 | #if !LOG_NDEBUG |
| 6251 | ssize_t index = |
| 6252 | #else |
| 6253 | (void) |
| 6254 | #endif |
| 6255 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6256 | ALOG_ASSERT(index == 0); |
| 6257 | |
| 6258 | // initialize fast capture depending on configuration |
| 6259 | bool initFastCapture; |
| 6260 | switch (kUseFastCapture) { |
| 6261 | case FastCapture_Never: |
| 6262 | initFastCapture = false; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6263 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6264 | break; |
| 6265 | case FastCapture_Always: |
| 6266 | initFastCapture = true; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6267 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6268 | break; |
| 6269 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 6270 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6271 | ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d", |
| 6272 | this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs, |
| 6273 | initFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6274 | break; |
| 6275 | // case FastCapture_Dynamic: |
| 6276 | } |
| 6277 | |
| 6278 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 6279 | // 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] | 6280 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6281 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread |
| 6282 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6283 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6284 | void *pipeBuffer = nullptr; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6285 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 6286 | sp<IMemory> pipeMemory; |
| 6287 | if ((roHeap == 0) || |
| 6288 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6289 | (pipeBuffer = pipeMemory->pointer()) == nullptr) { |
| 6290 | ALOGE("not enough memory for pipe buffer size=%zu; " |
| 6291 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", |
| 6292 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, |
| 6293 | (long long)kRecordThreadReadOnlyHeapSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6294 | goto failed; |
| 6295 | } |
| 6296 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 6297 | memset(pipeBuffer, 0, pipeSize); |
| 6298 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 6299 | const NBAIO_Format offers[1] = {format}; |
| 6300 | size_t numCounterOffers = 0; |
| 6301 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 6302 | ALOG_ASSERT(index == 0); |
| 6303 | mPipeSink = pipe; |
| 6304 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 6305 | numCounterOffers = 0; |
| 6306 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 6307 | ALOG_ASSERT(index == 0); |
| 6308 | mPipeSource = pipeReader; |
| 6309 | mPipeFramesP2 = pipeFramesP2; |
| 6310 | mPipeMemory = pipeMemory; |
| 6311 | |
| 6312 | // create fast capture |
| 6313 | mFastCapture = new FastCapture(); |
| 6314 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6315 | #ifdef STATE_QUEUE_DUMP |
| 6316 | // FIXME |
| 6317 | #endif |
| 6318 | FastCaptureState *state = sq->begin(); |
| 6319 | state->mCblk = NULL; |
| 6320 | state->mInputSource = mInputSource.get(); |
| 6321 | state->mInputSourceGen++; |
| 6322 | state->mPipeSink = pipe; |
| 6323 | state->mPipeSinkGen++; |
| 6324 | state->mFrameCount = mFrameCount; |
| 6325 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6326 | // already done in constructor initialization list |
| 6327 | //mFastCaptureFutex = 0; |
| 6328 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6329 | state->mColdGen++; |
| 6330 | state->mDumpState = &mFastCaptureDumpState; |
| 6331 | #ifdef TEE_SINK |
| 6332 | // FIXME |
| 6333 | #endif |
| 6334 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 6335 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 6336 | sq->end(); |
| 6337 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6338 | |
| 6339 | // start the fast capture |
| 6340 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 6341 | pid_t tid = mFastCapture->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6342 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 6343 | stream()->setHalThreadPriority(kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6344 | #ifdef AUDIO_WATCHDOG |
| 6345 | // FIXME |
| 6346 | #endif |
| 6347 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6348 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6349 | } |
| 6350 | failed: ; |
| 6351 | |
| 6352 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6353 | } |
| 6354 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6355 | AudioFlinger::RecordThread::~RecordThread() |
| 6356 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6357 | if (mFastCapture != 0) { |
| 6358 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6359 | FastCaptureState *state = sq->begin(); |
| 6360 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6361 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6362 | if (old == -1) { |
| 6363 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6364 | } |
| 6365 | } |
| 6366 | state->mCommand = FastCaptureState::EXIT; |
| 6367 | sq->end(); |
| 6368 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6369 | mFastCapture->join(); |
| 6370 | mFastCapture.clear(); |
| 6371 | } |
| 6372 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 6373 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6374 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | void AudioFlinger::RecordThread::onFirstRef() |
| 6378 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6379 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6380 | } |
| 6381 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 6382 | void AudioFlinger::RecordThread::preExit() |
| 6383 | { |
| 6384 | ALOGV(" preExit()"); |
| 6385 | Mutex::Autolock _l(mLock); |
| 6386 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6387 | sp<RecordTrack> track = mTracks[i]; |
| 6388 | track->invalidate(); |
| 6389 | } |
| 6390 | mActiveTracks.clear(); |
| 6391 | mStartStopCond.broadcast(); |
| 6392 | } |
| 6393 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6394 | bool AudioFlinger::RecordThread::threadLoop() |
| 6395 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6396 | nsecs_t lastWarning = 0; |
| 6397 | |
| 6398 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6399 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6400 | reacquire_wakelock: |
| 6401 | sp<RecordTrack> activeTrack; |
| 6402 | { |
| 6403 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6404 | acquireWakeLock_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6405 | } |
| 6406 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6407 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 6408 | uint32_t sleepUs = 0; |
| 6409 | |
| 6410 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6411 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6412 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6413 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6414 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 6415 | Vector< sp<RecordTrack> > activeTracks; |
| 6416 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6417 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6418 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6419 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6420 | // reference to a fast track which is about to be removed |
| 6421 | sp<RecordTrack> fastTrackToRemove; |
| 6422 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6423 | { // scope for mLock |
| 6424 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6425 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6426 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6427 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6428 | // check exitPending here because checkForNewParameters_l() and |
| 6429 | // checkForNewParameters_l() can temporarily release mLock |
| 6430 | if (exitPending()) { |
| 6431 | break; |
| 6432 | } |
| 6433 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6434 | // sleep with mutex unlocked |
| 6435 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 6436 | ATRACE_BEGIN("sleepC"); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6437 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); |
| 6438 | ATRACE_END(); |
| 6439 | sleepUs = 0; |
| 6440 | continue; |
| 6441 | } |
| 6442 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6443 | // if no active track(s), then standby and release wakelock |
| 6444 | size_t size = mActiveTracks.size(); |
| 6445 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6446 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6447 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6448 | releaseWakeLock_l(); |
| 6449 | ALOGV("RecordThread: loop stopping"); |
| 6450 | // go to sleep |
| 6451 | mWaitWorkCV.wait(mLock); |
| 6452 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6453 | goto reacquire_wakelock; |
| 6454 | } |
| 6455 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6456 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6457 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6458 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6459 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6460 | activeTrack = mActiveTracks[i]; |
| 6461 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6462 | if (activeTrack->isFastTrack()) { |
| 6463 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 6464 | fastTrackToRemove = activeTrack; |
| 6465 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6466 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6467 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6468 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6469 | continue; |
| 6470 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6471 | |
| 6472 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 6473 | switch (activeTrackState) { |
| 6474 | |
| 6475 | case TrackBase::PAUSING: |
| 6476 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6477 | doBroadcast = true; |
| 6478 | size--; |
| 6479 | continue; |
| 6480 | |
| 6481 | case TrackBase::STARTING_1: |
| 6482 | sleepUs = 10000; |
| 6483 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6484 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6485 | continue; |
| 6486 | |
| 6487 | case TrackBase::STARTING_2: |
| 6488 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6489 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6490 | activeTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6491 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6492 | break; |
| 6493 | |
| 6494 | case TrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6495 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6496 | break; |
| 6497 | |
| 6498 | case TrackBase::IDLE: |
| 6499 | i++; |
| 6500 | continue; |
| 6501 | |
| 6502 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6503 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6504 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6505 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6506 | activeTracks.add(activeTrack); |
| 6507 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6508 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6509 | if (activeTrack->isFastTrack()) { |
| 6510 | ALOG_ASSERT(!mFastTrackAvail); |
| 6511 | ALOG_ASSERT(fastTrack == 0); |
| 6512 | fastTrack = activeTrack; |
| 6513 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6514 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6515 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6516 | mActiveTracks.updatePowerState(this); |
| 6517 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 6518 | updateMetadata_l(); |
| 6519 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6520 | if (allStopped) { |
| 6521 | standbyIfNotAlreadyInStandby(); |
| 6522 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6523 | if (doBroadcast) { |
| 6524 | mStartStopCond.broadcast(); |
| 6525 | } |
| 6526 | |
| 6527 | // sleep if there are no active tracks to process |
| 6528 | if (activeTracks.size() == 0) { |
| 6529 | if (sleepUs == 0) { |
| 6530 | sleepUs = kRecordThreadSleepUs; |
| 6531 | } |
| 6532 | continue; |
| 6533 | } |
| 6534 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6535 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6536 | lockEffectChains_l(effectChains); |
| 6537 | } |
| 6538 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6539 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6540 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6541 | size_t size = effectChains.size(); |
| 6542 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6543 | // thread mutex is not locked, but effect chain is locked |
| 6544 | effectChains[i]->process_l(); |
| 6545 | } |
| 6546 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6547 | // 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] | 6548 | if (mFastCapture != 0) { |
| 6549 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6550 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6551 | bool didModify = false; |
| 6552 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6553 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 6554 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 6555 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6556 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6557 | if (old == -1) { |
| 6558 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6559 | } |
| 6560 | } |
| 6561 | state->mCommand = FastCaptureState::READ_WRITE; |
| 6562 | #if 0 // FIXME |
| 6563 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6564 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 6565 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6566 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6567 | didModify = true; |
| 6568 | } |
| 6569 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 6570 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 6571 | if (cblkNew != cblkOld) { |
| 6572 | state->mCblk = cblkNew; |
| 6573 | // block until acked if removing a fast track |
| 6574 | if (cblkOld != NULL) { |
| 6575 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 6576 | } |
| 6577 | didModify = true; |
| 6578 | } |
| 6579 | sq->end(didModify); |
| 6580 | if (didModify) { |
| 6581 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6582 | #if 0 |
| 6583 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6584 | mNormalSource = mPipeSource; |
| 6585 | } |
| 6586 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6587 | } |
| 6588 | } |
| 6589 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6590 | // now run the fast track destructor with thread mutex unlocked |
| 6591 | fastTrackToRemove.clear(); |
| 6592 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6593 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 6594 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 6595 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 6596 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 6597 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6598 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6599 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6600 | ssize_t framesRead; |
| 6601 | |
| 6602 | // If an NBAIO source is present, use it to read the normal capture's data |
| 6603 | if (mPipeSource != 0) { |
| 6604 | size_t framesToRead = mBufferSize / mFrameSize; |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6605 | framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6606 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6607 | framesToRead); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6608 | // since pipe is non-blocking, simulate blocking input by waiting for 1/2 of |
| 6609 | // buffer size or at least for 20ms. |
| 6610 | size_t sleepFrames = max( |
| 6611 | min(mPipeFramesP2, mRsmpInFramesP2) / 2, FMS_20 * mSampleRate / 1000); |
| 6612 | if (framesRead <= (ssize_t) sleepFrames) { |
| 6613 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; |
| 6614 | } |
| 6615 | if (framesRead < 0) { |
| 6616 | status_t status = (status_t) framesRead; |
| 6617 | switch (status) { |
| 6618 | case OVERRUN: |
| 6619 | ALOGW("overrun on read from pipe"); |
| 6620 | framesRead = 0; |
| 6621 | break; |
| 6622 | case NEGOTIATE: |
| 6623 | ALOGE("re-negotiation is needed"); |
| 6624 | framesRead = -1; // Will cause an attempt to recover. |
| 6625 | break; |
| 6626 | default: |
| 6627 | ALOGE("unknown error %d on read from pipe", status); |
| 6628 | break; |
| 6629 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6630 | } |
| 6631 | // otherwise use the HAL / AudioStreamIn directly |
| 6632 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6633 | ATRACE_BEGIN("read"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6634 | size_t bytesRead; |
| 6635 | status_t result = mInput->stream->read( |
| 6636 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6637 | ATRACE_END(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6638 | if (result < 0) { |
| 6639 | framesRead = result; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6640 | } else { |
| 6641 | framesRead = bytesRead / mFrameSize; |
| 6642 | } |
| 6643 | } |
| 6644 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6645 | // Update server timestamp with server stats |
| 6646 | // systemTime() is optional if the hardware supports timestamps. |
| 6647 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6648 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6649 | |
| 6650 | // Update server timestamp with kernel stats |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6651 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6652 | int64_t position, time; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6653 | int ret = mInput->stream->getCapturePosition(&position, &time); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6654 | if (ret == NO_ERROR) { |
| 6655 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6656 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6657 | // Note: In general record buffers should tend to be empty in |
| 6658 | // a properly running pipeline. |
| 6659 | // |
| 6660 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6661 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6662 | } |
| 6663 | } |
| 6664 | // Use this to track timestamp information |
| 6665 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6666 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6667 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6668 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6669 | // Force input into standby so that it tries to recover at next read attempt |
| 6670 | inputStandBy(); |
| 6671 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6672 | } |
| 6673 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6674 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6675 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6676 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6677 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6678 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6679 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6680 | } |
| 6681 | // 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] | 6682 | { |
| 6683 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6684 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6685 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6686 | (framesRead - part1) * mFrameSize); |
| 6687 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6688 | } |
| 6689 | rear = mRsmpInRear += framesRead; |
| 6690 | |
| 6691 | size = activeTracks.size(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6692 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6693 | // loop over each active track |
| 6694 | for (size_t i = 0; i < size; i++) { |
| 6695 | activeTrack = activeTracks[i]; |
| 6696 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6697 | // skip fast tracks, as those are handled directly by FastCapture |
| 6698 | if (activeTrack->isFastTrack()) { |
| 6699 | continue; |
| 6700 | } |
| 6701 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6702 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6703 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6704 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6705 | enum { |
| 6706 | OVERRUN_UNKNOWN, |
| 6707 | OVERRUN_TRUE, |
| 6708 | OVERRUN_FALSE |
| 6709 | } overrun = OVERRUN_UNKNOWN; |
| 6710 | |
| 6711 | // loop over getNextBuffer to handle circular sink |
| 6712 | for (;;) { |
| 6713 | |
| 6714 | activeTrack->mSink.frameCount = ~0; |
| 6715 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6716 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6717 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6718 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6719 | // check available frames and handle overrun conditions |
| 6720 | // if the record track isn't draining fast enough. |
| 6721 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6722 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6723 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6724 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6725 | overrun = OVERRUN_TRUE; |
| 6726 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6727 | if (framesOut == 0 || framesIn == 0) { |
| 6728 | break; |
| 6729 | } |
| 6730 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6731 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6732 | // from framesIn. |
| 6733 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6734 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6735 | framesOut = min(framesOut, |
| 6736 | destinationFramesPossible( |
| 6737 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6738 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6739 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6740 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6741 | |
| 6742 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6743 | overrun = OVERRUN_FALSE; |
| 6744 | } |
| 6745 | |
| 6746 | if (activeTrack->mFramesToDrop == 0) { |
| 6747 | if (framesOut > 0) { |
| 6748 | activeTrack->mSink.frameCount = framesOut; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6749 | // Sanitize before releasing if the track has no access to the source data |
| 6750 | // An idle UID receives silence from non virtual devices until active |
| 6751 | if (activeTrack->isSilenced()) { |
| 6752 | memset(activeTrack->mSink.raw, 0, framesOut * mFrameSize); |
| 6753 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6754 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6755 | } |
| 6756 | } else { |
| 6757 | // FIXME could do a partial drop of framesOut |
| 6758 | if (activeTrack->mFramesToDrop > 0) { |
| 6759 | activeTrack->mFramesToDrop -= framesOut; |
| 6760 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6761 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6762 | } |
| 6763 | } else { |
| 6764 | activeTrack->mFramesToDrop += framesOut; |
| 6765 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6766 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6767 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6768 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6769 | activeTrack->sessionId(), |
| 6770 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6771 | activeTrack->mSyncStartEvent->triggerSession() : |
| 6772 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6773 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6774 | } |
| 6775 | } |
| 6776 | } |
| 6777 | |
| 6778 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6779 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6780 | } |
| 6781 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6782 | |
| 6783 | switch (overrun) { |
| 6784 | case OVERRUN_TRUE: |
| 6785 | // client isn't retrieving buffers fast enough |
| 6786 | if (!activeTrack->setOverflow()) { |
| 6787 | nsecs_t now = systemTime(); |
| 6788 | // FIXME should lastWarning per track? |
| 6789 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6790 | ALOGW("RecordThread: buffer overflow"); |
| 6791 | lastWarning = now; |
| 6792 | } |
| 6793 | } |
| 6794 | break; |
| 6795 | case OVERRUN_FALSE: |
| 6796 | activeTrack->clearOverflow(); |
| 6797 | break; |
| 6798 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6799 | break; |
| 6800 | } |
| 6801 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6802 | // update frame information and push timestamp out |
| 6803 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6804 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6805 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6806 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6807 | } |
| 6808 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6809 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6810 | // enable changes in effect chain |
| 6811 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6812 | // 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] | 6813 | } |
| 6814 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6815 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6816 | |
| 6817 | { |
| 6818 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6819 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6820 | sp<RecordTrack> track = mTracks[i]; |
| 6821 | track->invalidate(); |
| 6822 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6823 | mActiveTracks.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6824 | mStartStopCond.broadcast(); |
| 6825 | } |
| 6826 | |
| 6827 | releaseWakeLock(); |
| 6828 | |
| 6829 | ALOGV("RecordThread %p exiting", this); |
| 6830 | return false; |
| 6831 | } |
| 6832 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6833 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6834 | { |
| 6835 | if (!mStandby) { |
| 6836 | inputStandBy(); |
| 6837 | mStandby = true; |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | void AudioFlinger::RecordThread::inputStandBy() |
| 6842 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6843 | // Idle the fast capture if it's currently running |
| 6844 | if (mFastCapture != 0) { |
| 6845 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6846 | FastCaptureState *state = sq->begin(); |
| 6847 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6848 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6849 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6850 | state->mColdGen++; |
| 6851 | mFastCaptureFutex = 0; |
| 6852 | sq->end(); |
| 6853 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6854 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6855 | #if 0 |
| 6856 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6857 | // FIXME |
| 6858 | } |
| 6859 | #endif |
| 6860 | #ifdef AUDIO_WATCHDOG |
| 6861 | // FIXME |
| 6862 | #endif |
| 6863 | } else { |
| 6864 | sq->end(false /*didModify*/); |
| 6865 | } |
| 6866 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6867 | status_t result = mInput->stream->standby(); |
| 6868 | 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] | 6869 | |
| 6870 | // If going into standby, flush the pipe source. |
| 6871 | if (mPipeSource.get() != nullptr) { |
| 6872 | const ssize_t flushed = mPipeSource->flush(); |
| 6873 | if (flushed > 0) { |
| 6874 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); |
| 6875 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; |
| 6876 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6877 | } |
| 6878 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6879 | } |
| 6880 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6881 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6882 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6883 | const sp<AudioFlinger::Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 6884 | const audio_attributes_t& attr, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6885 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6886 | audio_format_t format, |
| 6887 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6888 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6889 | audio_session_t sessionId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6890 | size_t *pNotificationFrameCount, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6891 | uid_t uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6892 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6893 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6894 | status_t *status, |
| 6895 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6896 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6897 | size_t frameCount = *pFrameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6898 | size_t notificationFrameCount = *pNotificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6899 | sp<RecordTrack> track; |
| 6900 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6901 | audio_input_flags_t inputFlags = mInput->flags; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6902 | audio_input_flags_t requestedFlags = *flags; |
| 6903 | uint32_t sampleRate; |
| 6904 | |
| 6905 | lStatus = initCheck(); |
| 6906 | if (lStatus != NO_ERROR) { |
| 6907 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6908 | goto Exit; |
| 6909 | } |
| 6910 | |
| 6911 | if (*pSampleRate == 0) { |
| 6912 | *pSampleRate = mSampleRate; |
| 6913 | } |
| 6914 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6915 | |
| 6916 | // special case for FAST flag considered OK if fast capture is present |
| 6917 | if (hasFastCapture()) { |
| 6918 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 6919 | } |
| 6920 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6921 | // Check if requested flags are compatible with input stream flags |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6922 | if ((*flags & inputFlags) != *flags) { |
| 6923 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 6924 | " input flags (%08x)", |
| 6925 | *flags, inputFlags); |
| 6926 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 6927 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6928 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6929 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6930 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6931 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6932 | // we formerly checked for a callback handler (non-0 tid), |
| 6933 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6934 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6935 | // frame count is not specified, or is exactly the pipe depth |
| 6936 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6937 | // PCM data |
| 6938 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6939 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6940 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6941 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6942 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6943 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6944 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6945 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6946 | hasFastCapture() && |
| 6947 | // there are sufficient fast track slots available |
| 6948 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6949 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6950 | // check compatibility with audio effects. |
| 6951 | Mutex::Autolock _l(mLock); |
| 6952 | // Do not accept FAST flag if the session has software effects |
| 6953 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 6954 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 6955 | audio_input_flags_t old = *flags; |
| 6956 | chain->checkInputFlagCompatibility(flags); |
| 6957 | if (old != *flags) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6958 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 6959 | this, (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6960 | } |
| 6961 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6962 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6963 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 6964 | this, frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6965 | } else { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6966 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
| 6967 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6968 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6969 | this, frameCount, mFrameCount, mPipeFramesP2, |
| 6970 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6971 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6972 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6973 | } |
| 6974 | } |
| 6975 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6976 | // If FAST or RAW flags were corrected, ask caller to request new input from audio policy |
| 6977 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != |
| 6978 | (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { |
| 6979 | *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); |
| 6980 | lStatus = BAD_TYPE; |
| 6981 | goto Exit; |
| 6982 | } |
| 6983 | |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6984 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6985 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6986 | // fast track: frame count is exactly the pipe depth |
| 6987 | frameCount = mPipeFramesP2; |
| 6988 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6989 | notificationFrameCount = mFrameCount; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6990 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6991 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6992 | // or 20 ms if there is a fast capture |
| 6993 | // TODO This could be a roundupRatio inline, and const |
| 6994 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6995 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6996 | // minimum number of notification periods is at least kMinNotifications, |
| 6997 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6998 | static const size_t kMinNotifications = 3; |
| 6999 | static const uint32_t kMinMs = 30; |
| 7000 | // TODO This could be a roundupRatio inline |
| 7001 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 7002 | // TODO This could be a roundupRatio inline |
| 7003 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 7004 | maxNotificationFrames; |
| 7005 | const size_t minFrameCount = maxNotificationFrames * |
| 7006 | max(kMinNotifications, minNotificationsByMs); |
| 7007 | frameCount = max(frameCount, minFrameCount); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7008 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 7009 | notificationFrameCount = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7010 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7011 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 7012 | *pFrameCount = frameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7013 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7014 | |
| 7015 | { // scope for mLock |
| 7016 | Mutex::Autolock _l(mLock); |
| 7017 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 7018 | track = new RecordTrack(this, client, attr, sampleRate, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 7019 | format, channelMask, frameCount, |
| 7020 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 7021 | *flags, TrackBase::TYPE_DEFAULT, portId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7022 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 7023 | lStatus = track->initCheck(); |
| 7024 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 7025 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 7026 | // 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] | 7027 | goto Exit; |
| 7028 | } |
| 7029 | mTracks.add(track); |
| 7030 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7031 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7032 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 7033 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 7034 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 7035 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7036 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7037 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 7038 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7039 | lStatus = NO_ERROR; |
| 7040 | |
| 7041 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 7042 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7043 | return track; |
| 7044 | } |
| 7045 | |
| 7046 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 7047 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7048 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7049 | { |
| 7050 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 7051 | sp<ThreadBase> strongMe = this; |
| 7052 | status_t status = NO_ERROR; |
| 7053 | |
| 7054 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7055 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7056 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7057 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7058 | triggerSession, |
| 7059 | recordTrack->sessionId(), |
| 7060 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7061 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7062 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 7063 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7064 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7065 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7066 | } else { |
| 7067 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Ivan Lozano | 1b35dd6 | 2017-12-06 16:55:10 -0800 | [diff] [blame] | 7068 | recordTrack->mFramesToDrop = -(ssize_t) |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7069 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7074 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7075 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7076 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 7077 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 7078 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 7079 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7080 | } else { |
| 7081 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7082 | } |
| 7083 | return status; |
| 7084 | } |
| 7085 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7086 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 7087 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 7088 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7089 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7090 | mActiveTracks.add(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7091 | status_t status = NO_ERROR; |
| 7092 | if (recordTrack->isExternalTrack()) { |
| 7093 | mLock.unlock(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7094 | bool silenced; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7095 | status = AudioSystem::startInput(recordTrack->portId(), &silenced); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7096 | mLock.lock(); |
| 7097 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 7098 | if (status != NO_ERROR) { |
| 7099 | mActiveTracks.remove(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7100 | recordTrack->clearSyncStartEvent(); |
| 7101 | ALOGV("RecordThread::start error %d", status); |
| 7102 | return status; |
| 7103 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7104 | recordTrack->setSilenced(silenced); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7105 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7106 | // Catch up with current buffer indices if thread is already running. |
| 7107 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 7108 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 7109 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 7110 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7111 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7112 | // clear any converter state as new data will be discontinuous |
| 7113 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7114 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7115 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7116 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7117 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7118 | ALOGV("Record failed to start"); |
| 7119 | status = BAD_VALUE; |
| 7120 | goto startError; |
| 7121 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7122 | return status; |
| 7123 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 7124 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7125 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7126 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7127 | AudioSystem::stopInput(recordTrack->portId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7128 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7129 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7130 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7131 | return status; |
| 7132 | } |
| 7133 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7134 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 7135 | { |
| 7136 | sp<SyncEvent> strongEvent = event.promote(); |
| 7137 | |
| 7138 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 7139 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 7140 | if (ptr != 0) { |
| 7141 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 7142 | recordTrack->handleSyncStartEvent(strongEvent); |
| 7143 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7144 | } |
| 7145 | } |
| 7146 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 7147 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7148 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 7149 | AutoMutex _l(mLock); |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 7150 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7151 | return false; |
| 7152 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7153 | // 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] | 7154 | recordTrack->mState = TrackBase::PAUSING; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7155 | // signal thread to stop |
| 7156 | mWaitWorkCV.broadcast(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7157 | // do not wait for mStartStopCond if exiting |
| 7158 | if (exitPending()) { |
| 7159 | return true; |
| 7160 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7161 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7162 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7163 | // if we have been restarted, recordTrack is in mActiveTracks here |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 7164 | if (exitPending() || mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7165 | ALOGV("Record stopped OK"); |
| 7166 | return true; |
| 7167 | } |
| 7168 | return false; |
| 7169 | } |
| 7170 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7171 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7172 | { |
| 7173 | return false; |
| 7174 | } |
| 7175 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7176 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7177 | { |
| 7178 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 7179 | if (!isValidSyncEvent(event)) { |
| 7180 | return BAD_VALUE; |
| 7181 | } |
| 7182 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7183 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7184 | status_t ret = NAME_NOT_FOUND; |
| 7185 | |
| 7186 | Mutex::Autolock _l(mLock); |
| 7187 | |
| 7188 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7189 | sp<RecordTrack> track = mTracks[i]; |
| 7190 | if (eventSession == track->sessionId()) { |
| 7191 | (void) track->setSyncEvent(event); |
| 7192 | ret = NO_ERROR; |
| 7193 | } |
| 7194 | } |
| 7195 | return ret; |
| 7196 | #else |
| 7197 | return BAD_VALUE; |
| 7198 | #endif |
| 7199 | } |
| 7200 | |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 7201 | status_t AudioFlinger::RecordThread::getActiveMicrophones( |
| 7202 | std::vector<media::MicrophoneInfo>* activeMicrophones) |
| 7203 | { |
| 7204 | ALOGV("RecordThread::getActiveMicrophones"); |
| 7205 | AutoMutex _l(mLock); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 7206 | status_t status = mInput->stream->getActiveMicrophones(activeMicrophones); |
| 7207 | return status; |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 7208 | } |
| 7209 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 7210 | void AudioFlinger::RecordThread::updateMetadata_l() |
| 7211 | { |
| 7212 | if (mInput == nullptr || mInput->stream == nullptr || |
| 7213 | !mActiveTracks.readAndClearHasChanged()) { |
| 7214 | return; |
| 7215 | } |
| 7216 | StreamInHalInterface::SinkMetadata metadata; |
| 7217 | for (const sp<RecordTrack> &track : mActiveTracks) { |
| 7218 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
| 7219 | metadata.tracks.push_back({ |
| 7220 | .source = track->attributes().source, |
| 7221 | .gain = 1, // capture tracks do not have volumes |
| 7222 | }); |
| 7223 | } |
| 7224 | mInput->stream->updateSinkMetadata(metadata); |
| 7225 | } |
| 7226 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7227 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 7228 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 7229 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7230 | track->terminate(); |
| 7231 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7232 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7233 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7234 | removeTrack_l(track); |
| 7235 | } |
| 7236 | } |
| 7237 | |
| 7238 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 7239 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7240 | String8 result; |
| 7241 | track->appendDump(result, false /* active */); |
| 7242 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
| 7243 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7244 | mTracks.remove(track); |
| 7245 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7246 | if (track->isFastTrack()) { |
| 7247 | ALOG_ASSERT(!mFastTrackAvail); |
| 7248 | mFastTrackAvail = true; |
| 7249 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 7253 | { |
| 7254 | dumpInternals(fd, args); |
| 7255 | dumpTracks(fd, args); |
| 7256 | dumpEffectChains(fd, args); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7257 | dprintf(fd, " Local log:\n"); |
| 7258 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7259 | } |
| 7260 | |
| 7261 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 7262 | { |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 7263 | dumpBase(fd, args); |
| 7264 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 7265 | AudioStreamIn *input = mInput; |
| 7266 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; |
| 7267 | dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n", |
| 7268 | input, flags, inputFlagsToString(flags).c_str()); |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 7269 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 7270 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7271 | } |
Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 7272 | |
| 7273 | if (input != nullptr) { |
| 7274 | dprintf(fd, " Hal stream dump:\n"); |
| 7275 | (void)input->stream->dump(fd); |
| 7276 | } |
| 7277 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 7278 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7279 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 7280 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 7281 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 7282 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 7283 | // This is a large object so we place it on the heap. |
| 7284 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 7285 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 7286 | copy->dump(fd); |
| 7287 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7288 | } |
| 7289 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7290 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7291 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7292 | String8 result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7293 | size_t numtracks = mTracks.size(); |
| 7294 | size_t numactive = mActiveTracks.size(); |
| 7295 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7296 | dprintf(fd, " %zu Tracks", numtracks); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7297 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7298 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7299 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7300 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7301 | RecordTrack::appendDumpHeader(result); |
| 7302 | for (size_t i = 0; i < numtracks ; ++i) { |
| 7303 | sp<RecordTrack> track = mTracks[i]; |
| 7304 | if (track != 0) { |
| 7305 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 7306 | if (active) { |
| 7307 | numactiveseen++; |
| 7308 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7309 | result.append(prefix); |
| 7310 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7311 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7312 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7313 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 7314 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7315 | } |
| 7316 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7317 | if (numactiveseen != numactive) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7318 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7319 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7320 | result.append(prefix); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7321 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7322 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7323 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7324 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7325 | result.append(prefix); |
| 7326 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7327 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7328 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7329 | |
| 7330 | } |
| 7331 | write(fd, result.string(), result.size()); |
| 7332 | } |
| 7333 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7334 | void AudioFlinger::RecordThread::setRecordSilenced(uid_t uid, bool silenced) |
| 7335 | { |
| 7336 | Mutex::Autolock _l(mLock); |
| 7337 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 7338 | sp<RecordTrack> track = mTracks[i]; |
| 7339 | if (track != 0 && track->uid() == uid) { |
| 7340 | track->setSilenced(silenced); |
| 7341 | } |
| 7342 | } |
| 7343 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7344 | |
| 7345 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 7346 | { |
| 7347 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 7348 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7349 | mRsmpInFront = recordThread->mRsmpInRear; |
| 7350 | mRsmpInUnrel = 0; |
| 7351 | } |
| 7352 | |
| 7353 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 7354 | size_t *framesAvailable, bool *hasOverrun) |
| 7355 | { |
| 7356 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 7357 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7358 | const int32_t rear = recordThread->mRsmpInRear; |
| 7359 | const int32_t front = mRsmpInFront; |
| 7360 | const ssize_t filled = rear - front; |
| 7361 | |
| 7362 | size_t framesIn; |
| 7363 | bool overrun = false; |
| 7364 | if (filled < 0) { |
| 7365 | // should not happen, but treat like a massive overrun and re-sync |
| 7366 | framesIn = 0; |
| 7367 | mRsmpInFront = rear; |
| 7368 | overrun = true; |
| 7369 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 7370 | framesIn = (size_t) filled; |
| 7371 | } else { |
| 7372 | // client is not keeping up with server, but give it latest data |
| 7373 | framesIn = recordThread->mRsmpInFrames; |
| 7374 | mRsmpInFront = /* front = */ rear - framesIn; |
| 7375 | overrun = true; |
| 7376 | } |
| 7377 | if (framesAvailable != NULL) { |
| 7378 | *framesAvailable = framesIn; |
| 7379 | } |
| 7380 | if (hasOverrun != NULL) { |
| 7381 | *hasOverrun = overrun; |
| 7382 | } |
| 7383 | } |
| 7384 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7385 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7386 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7387 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7388 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7389 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7390 | if (threadBase == 0) { |
| 7391 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7392 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7393 | return NOT_ENOUGH_DATA; |
| 7394 | } |
| 7395 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7396 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7397 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7398 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7399 | // FIXME should not be P2 (don't want to increase latency) |
| 7400 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7401 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7402 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7403 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 7404 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7405 | if (part1 > (size_t) filled) { |
| 7406 | part1 = filled; |
| 7407 | } |
| 7408 | size_t ask = buffer->frameCount; |
| 7409 | ALOG_ASSERT(ask > 0); |
| 7410 | if (part1 > ask) { |
| 7411 | part1 = ask; |
| 7412 | } |
| 7413 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7414 | // 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] | 7415 | buffer->raw = NULL; |
| 7416 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7417 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7418 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7419 | } |
| 7420 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7421 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7422 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7423 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7424 | return NO_ERROR; |
| 7425 | } |
| 7426 | |
| 7427 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7428 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 7429 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7430 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7431 | size_t stepCount = buffer->frameCount; |
| 7432 | if (stepCount == 0) { |
| 7433 | return; |
| 7434 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7435 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 7436 | mRsmpInUnrel -= stepCount; |
| 7437 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7438 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7439 | buffer->frameCount = 0; |
| 7440 | } |
| 7441 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7442 | void AudioFlinger::RecordThread::checkBtNrec() |
| 7443 | { |
| 7444 | Mutex::Autolock _l(mLock); |
| 7445 | checkBtNrec_l(); |
| 7446 | } |
| 7447 | |
| 7448 | void AudioFlinger::RecordThread::checkBtNrec_l() |
| 7449 | { |
| 7450 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7451 | // pre processings |
| 7452 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7453 | mAudioFlinger->btNrecIsOff(); |
| 7454 | if (mBtNrecSuspended.exchange(suspend) != suspend) { |
| 7455 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7456 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); |
| 7457 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); |
| 7458 | } |
| 7459 | } |
| 7460 | } |
| 7461 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7462 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7463 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 7464 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7465 | { |
| 7466 | bool reconfig = false; |
| 7467 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7468 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7469 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7470 | audio_format_t reqFormat = mFormat; |
| 7471 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7472 | // 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] | 7473 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 7474 | |
| 7475 | AudioParameter param = AudioParameter(keyValuePair); |
| 7476 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7477 | |
| 7478 | // scope for AutoPark extends to end of method |
| 7479 | AutoPark<FastCapture> park(mFastCapture); |
| 7480 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7481 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 7482 | // channel count change can be requested. Do we mandate the first client defines the |
| 7483 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 7484 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 7485 | samplingRate = value; |
| 7486 | reconfig = true; |
| 7487 | } |
| 7488 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7489 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7490 | status = BAD_VALUE; |
| 7491 | } else { |
| 7492 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7493 | reconfig = true; |
| 7494 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7495 | } |
| 7496 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 7497 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7498 | if (!audio_is_input_channel(mask) || |
| 7499 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7500 | status = BAD_VALUE; |
| 7501 | } else { |
| 7502 | channelMask = mask; |
| 7503 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7504 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7505 | } |
| 7506 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7507 | // do not accept frame count changes if tracks are open as the track buffer |
| 7508 | // size depends on frame count and correct behavior would not be guaranteed |
| 7509 | // if frame count is changed after track creation |
| 7510 | if (mActiveTracks.size() > 0) { |
| 7511 | status = INVALID_OPERATION; |
| 7512 | } else { |
| 7513 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7514 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7515 | } |
| 7516 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 7517 | // forward device change to effects that have requested to be |
| 7518 | // aware of attached audio device. |
| 7519 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7520 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7521 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7522 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7523 | // store input device and output device but do not forward output device to audio HAL. |
| 7524 | // Note that status is ignored by the caller for output device |
| 7525 | // (see AudioFlinger::setParameters() |
| 7526 | if (audio_is_output_devices(value)) { |
| 7527 | mOutDevice = value; |
| 7528 | status = BAD_VALUE; |
| 7529 | } else { |
| 7530 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7531 | if (value != AUDIO_DEVICE_NONE) { |
| 7532 | mPrevInDevice = value; |
| 7533 | } |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7534 | checkBtNrec_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7535 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7536 | } |
| 7537 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7538 | mAudioSource != (audio_source_t)value) { |
| 7539 | // forward device change to effects that have requested to be |
| 7540 | // aware of attached audio device. |
| 7541 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7542 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7543 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7544 | mAudioSource = (audio_source_t)value; |
| 7545 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7546 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7547 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7548 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7549 | if (status == INVALID_OPERATION) { |
| 7550 | inputStandBy(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7551 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7552 | } |
| 7553 | if (reconfig) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7554 | if (status == BAD_VALUE) { |
| 7555 | uint32_t sRate; |
| 7556 | audio_channel_mask_t channelMask; |
| 7557 | audio_format_t format; |
| 7558 | if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK && |
| 7559 | audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) && |
| 7560 | sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && |
| 7561 | audio_channel_count_from_in_mask(channelMask) <= FCC_8) { |
| 7562 | status = NO_ERROR; |
| 7563 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7564 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7565 | if (status == NO_ERROR) { |
| 7566 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7567 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7568 | } |
| 7569 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7570 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7571 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7572 | return reconfig; |
| 7573 | } |
| 7574 | |
| 7575 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7576 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7577 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7578 | if (initCheck() == NO_ERROR) { |
| 7579 | String8 out_s8; |
| 7580 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { |
| 7581 | return out_s8; |
| 7582 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7583 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7584 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7585 | } |
| 7586 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7587 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7588 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7589 | |
| 7590 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7591 | |
| 7592 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7593 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 7594 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7595 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7596 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7597 | desc->mChannelMask = mChannelMask; |
| 7598 | desc->mSamplingRate = mSampleRate; |
| 7599 | desc->mFormat = mFormat; |
| 7600 | desc->mFrameCount = mFrameCount; |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7601 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7602 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7603 | break; |
| 7604 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7605 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7606 | default: |
| 7607 | break; |
| 7608 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7609 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7610 | } |
| 7611 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7612 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7613 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7614 | status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 7615 | 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] | 7616 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7617 | 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] | 7618 | mFormat = mHALFormat; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7619 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 7620 | result = mInput->stream->getFrameSize(&mFrameSize); |
| 7621 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
| 7622 | result = mInput->stream->getBufferSize(&mBufferSize); |
| 7623 | 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] | 7624 | mFrameCount = mBufferSize / mFrameSize; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7625 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, " |
| 7626 | "mBufferSize=%lld, mFrameCount=%lld", |
| 7627 | this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize, |
| 7628 | (long long)mFrameCount); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7629 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7630 | // 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] | 7631 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7632 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7633 | // A larger value should allow more old data to be read after a track calls start(), |
| 7634 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7635 | // |
| 7636 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7637 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7638 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7639 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7640 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7641 | |
| 7642 | // TODO optimize audio capture buffer sizes ... |
| 7643 | // Here we calculate the size of the sliding buffer used as a source |
| 7644 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7645 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7646 | // be better to have it derived from the pipe depth in the long term. |
| 7647 | // The current value is higher than necessary. However it should not add to latency. |
| 7648 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7649 | // 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] | 7650 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; |
| 7651 | (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7652 | // if posix_memalign fails, will segv here. |
| 7653 | memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7654 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7655 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7656 | // 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] | 7657 | } |
| 7658 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7659 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7660 | { |
| 7661 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7662 | uint32_t result; |
| 7663 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { |
| 7664 | return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7665 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7666 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7667 | } |
| 7668 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7669 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 7670 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7671 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7672 | uint32_t result = 0; |
| 7673 | if (getEffectChain_l(sessionId) != 0) { |
| 7674 | result = EFFECT_SESSION; |
| 7675 | } |
| 7676 | |
| 7677 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7678 | if (sessionId == mTracks[i]->sessionId()) { |
| 7679 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7680 | if (mTracks[i]->isFastTrack()) { |
| 7681 | result |= FAST_SESSION; |
| 7682 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7683 | break; |
| 7684 | } |
| 7685 | } |
| 7686 | |
| 7687 | return result; |
| 7688 | } |
| 7689 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7690 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7691 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7692 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7693 | Mutex::Autolock _l(mLock); |
| 7694 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7695 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7696 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7697 | if (ids.indexOfKey(sessionId) < 0) { |
| 7698 | ids.add(sessionId, true); |
| 7699 | } |
| 7700 | } |
| 7701 | return ids; |
| 7702 | } |
| 7703 | |
| 7704 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7705 | { |
| 7706 | Mutex::Autolock _l(mLock); |
| 7707 | AudioStreamIn *input = mInput; |
| 7708 | mInput = NULL; |
| 7709 | return input; |
| 7710 | } |
| 7711 | |
| 7712 | // 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] | 7713 | sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7714 | { |
| 7715 | if (mInput == NULL) { |
| 7716 | return NULL; |
| 7717 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7718 | return mInput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7719 | } |
| 7720 | |
| 7721 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7722 | { |
| 7723 | // only one chain per input thread |
| 7724 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7725 | 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] | 7726 | return INVALID_OPERATION; |
| 7727 | } |
| 7728 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7729 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7730 | chain->setInBuffer(NULL); |
| 7731 | chain->setOutBuffer(NULL); |
| 7732 | |
| 7733 | checkSuspendOnAddEffectChain_l(chain); |
| 7734 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7735 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7736 | // just moved them to a new input stream. |
| 7737 | chain->syncHalEffectsState(); |
| 7738 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7739 | mEffectChains.add(chain); |
| 7740 | |
| 7741 | return NO_ERROR; |
| 7742 | } |
| 7743 | |
| 7744 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7745 | { |
| 7746 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7747 | ALOGW_IF(mEffectChains.size() != 1, |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7748 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7749 | chain.get(), mEffectChains.size(), this); |
| 7750 | if (mEffectChains.size() == 1) { |
| 7751 | mEffectChains.removeAt(0); |
| 7752 | } |
| 7753 | return 0; |
| 7754 | } |
| 7755 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7756 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7757 | audio_patch_handle_t *handle) |
| 7758 | { |
| 7759 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7760 | |
| 7761 | // store new device and send to effects |
| 7762 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7763 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7764 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7765 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7766 | } |
| 7767 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7768 | checkBtNrec_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7769 | |
| 7770 | // store new source and send to effects |
| 7771 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7772 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7773 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7774 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7775 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7776 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7777 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7778 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7779 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 7780 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 7781 | patch->sources, |
| 7782 | patch->num_sinks, |
| 7783 | patch->sinks, |
| 7784 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7785 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7786 | char *address; |
| 7787 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7788 | address = audio_device_address_to_parameter( |
| 7789 | patch->sources[0].ext.device.type, |
| 7790 | patch->sources[0].ext.device.address); |
| 7791 | } else { |
| 7792 | address = (char *)calloc(1, 1); |
| 7793 | } |
| 7794 | AudioParameter param = AudioParameter(String8(address)); |
| 7795 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7796 | param.addInt(String8(AudioParameter::keyRouting), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7797 | (int)patch->sources[0].ext.device.type); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7798 | param.addInt(String8(AudioParameter::keyInputSource), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7799 | (int)patch->sinks[0].ext.mix.usecase.source); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7800 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7801 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7802 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7803 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7804 | if (mInDevice != mPrevInDevice) { |
| 7805 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7806 | mPrevInDevice = mInDevice; |
| 7807 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7808 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7809 | return status; |
| 7810 | } |
| 7811 | |
| 7812 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7813 | { |
| 7814 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7815 | |
| 7816 | mInDevice = AUDIO_DEVICE_NONE; |
| 7817 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7818 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7819 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 7820 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7821 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7822 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7823 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7824 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7825 | } |
| 7826 | return status; |
| 7827 | } |
| 7828 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7829 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7830 | { |
| 7831 | Mutex::Autolock _l(mLock); |
| 7832 | mTracks.add(record); |
| 7833 | } |
| 7834 | |
| 7835 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7836 | { |
| 7837 | Mutex::Autolock _l(mLock); |
| 7838 | destroyTrack_l(record); |
| 7839 | } |
| 7840 | |
| 7841 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7842 | { |
| 7843 | ThreadBase::getAudioPortConfig(config); |
| 7844 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7845 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7846 | config->ext.mix.usecase.source = mAudioSource; |
| 7847 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7848 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7849 | // ---------------------------------------------------------------------------- |
| 7850 | // Mmap |
| 7851 | // ---------------------------------------------------------------------------- |
| 7852 | |
| 7853 | AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread) |
| 7854 | : mThread(thread) |
| 7855 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7856 | assert(thread != 0); // thread must start non-null and stay non-null |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7857 | } |
| 7858 | |
| 7859 | AudioFlinger::MmapThreadHandle::~MmapThreadHandle() |
| 7860 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7861 | mThread->disconnect(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, |
| 7865 | struct audio_mmap_buffer_info *info) |
| 7866 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7867 | return mThread->createMmapBuffer(minSizeFrames, info); |
| 7868 | } |
| 7869 | |
| 7870 | status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position) |
| 7871 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7872 | return mThread->getMmapPosition(position); |
| 7873 | } |
| 7874 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7875 | status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client, |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7876 | audio_port_handle_t *handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7877 | |
| 7878 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7879 | return mThread->start(client, handle); |
| 7880 | } |
| 7881 | |
| 7882 | status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle) |
| 7883 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7884 | return mThread->stop(handle); |
| 7885 | } |
| 7886 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7887 | status_t AudioFlinger::MmapThreadHandle::standby() |
| 7888 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7889 | return mThread->standby(); |
| 7890 | } |
| 7891 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7892 | |
| 7893 | AudioFlinger::MmapThread::MmapThread( |
| 7894 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 7895 | AudioHwDevice *hwDev, sp<StreamHalInterface> stream, |
| 7896 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 7897 | : ThreadBase(audioFlinger, id, outDevice, inDevice, MMAP, systemReady), |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7898 | mSessionId(AUDIO_SESSION_NONE), |
| 7899 | mDeviceId(AUDIO_PORT_HANDLE_NONE), mPortId(AUDIO_PORT_HANDLE_NONE), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7900 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 7901 | mActiveTracks(&this->mLocalLog), mNoCallbackWarningCount(0) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7902 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7903 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7904 | readHalParameters_l(); |
| 7905 | } |
| 7906 | |
| 7907 | AudioFlinger::MmapThread::~MmapThread() |
| 7908 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7909 | releaseWakeLock_l(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7910 | } |
| 7911 | |
| 7912 | void AudioFlinger::MmapThread::onFirstRef() |
| 7913 | { |
| 7914 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 7915 | } |
| 7916 | |
| 7917 | void AudioFlinger::MmapThread::disconnect() |
| 7918 | { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 7919 | ActiveTracks<MmapTrack> activeTracks; |
| 7920 | { |
| 7921 | Mutex::Autolock _l(mLock); |
| 7922 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 7923 | activeTracks.add(t); |
| 7924 | } |
| 7925 | } |
| 7926 | for (const sp<MmapTrack> &t : activeTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7927 | stop(t->portId()); |
| 7928 | } |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7929 | // This will decrement references and may cause the destruction of this thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7930 | if (isOutput()) { |
| 7931 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); |
| 7932 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7933 | AudioSystem::releaseInput(mPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7934 | } |
| 7935 | } |
| 7936 | |
| 7937 | |
| 7938 | void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr, |
| 7939 | audio_stream_type_t streamType __unused, |
| 7940 | audio_session_t sessionId, |
| 7941 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7942 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7943 | audio_port_handle_t portId) |
| 7944 | { |
| 7945 | mAttr = *attr; |
| 7946 | mSessionId = sessionId; |
| 7947 | mCallback = callback; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7948 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7949 | mPortId = portId; |
| 7950 | } |
| 7951 | |
| 7952 | status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames, |
| 7953 | struct audio_mmap_buffer_info *info) |
| 7954 | { |
| 7955 | if (mHalStream == 0) { |
| 7956 | return NO_INIT; |
| 7957 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7958 | mStandby = true; |
| 7959 | acquireWakeLock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7960 | return mHalStream->createMmapBuffer(minSizeFrames, info); |
| 7961 | } |
| 7962 | |
| 7963 | status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position) |
| 7964 | { |
| 7965 | if (mHalStream == 0) { |
| 7966 | return NO_INIT; |
| 7967 | } |
| 7968 | return mHalStream->getMmapPosition(position); |
| 7969 | } |
| 7970 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 7971 | status_t AudioFlinger::MmapThread::exitStandby() |
| 7972 | { |
| 7973 | status_t ret = mHalStream->start(); |
| 7974 | if (ret != NO_ERROR) { |
| 7975 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); |
| 7976 | return ret; |
| 7977 | } |
| 7978 | mStandby = false; |
| 7979 | return NO_ERROR; |
| 7980 | } |
| 7981 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7982 | status_t AudioFlinger::MmapThread::start(const AudioClient& client, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7983 | audio_port_handle_t *handle) |
| 7984 | { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7985 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, |
| 7986 | client.clientUid, mStandby, mPortId, *handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7987 | if (mHalStream == 0) { |
| 7988 | return NO_INIT; |
| 7989 | } |
| 7990 | |
| 7991 | status_t ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7992 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7993 | if (*handle == mPortId) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7994 | // for the first track, reuse portId and session allocated when the stream was opened |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 7995 | return exitStandby(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7996 | } |
| 7997 | |
| 7998 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 7999 | |
| 8000 | audio_io_handle_t io = mId; |
| 8001 | if (isOutput()) { |
| 8002 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 8003 | config.sample_rate = mSampleRate; |
| 8004 | config.channel_mask = mChannelMask; |
| 8005 | config.format = mFormat; |
| 8006 | audio_stream_type_t stream = streamType(); |
| 8007 | audio_output_flags_t flags = |
| 8008 | (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] | 8009 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8010 | ret = AudioSystem::getOutputForAttr(&mAttr, &io, |
| 8011 | mSessionId, |
| 8012 | &stream, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 8013 | client.clientPid, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8014 | client.clientUid, |
| 8015 | &config, |
| 8016 | flags, |
| 8017 | &deviceId, |
| 8018 | &portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8019 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8020 | audio_config_base_t config; |
| 8021 | config.sample_rate = mSampleRate; |
| 8022 | config.channel_mask = mChannelMask; |
| 8023 | config.format = mFormat; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8024 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8025 | ret = AudioSystem::getInputForAttr(&mAttr, &io, |
| 8026 | mSessionId, |
| 8027 | client.clientPid, |
| 8028 | client.clientUid, |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 8029 | client.packageName, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8030 | &config, |
| 8031 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, |
| 8032 | &deviceId, |
| 8033 | &portId); |
| 8034 | } |
| 8035 | // APM should not chose a different input or output stream for the same set of attributes |
| 8036 | // and audo configuration |
| 8037 | if (ret != NO_ERROR || io != mId) { |
| 8038 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", |
| 8039 | __FUNCTION__, ret, io, mId); |
| 8040 | return BAD_VALUE; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8041 | } |
| 8042 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8043 | bool silenced = false; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8044 | if (isOutput()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8045 | ret = AudioSystem::startOutput(mId, streamType(), mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8046 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 8047 | ret = AudioSystem::startInput(portId, &silenced); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8048 | } |
| 8049 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8050 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8051 | // abort if start is rejected by audio policy manager |
| 8052 | if (ret != NO_ERROR) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8053 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8054 | if (mActiveTracks.size() != 0) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8055 | mLock.unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8056 | if (isOutput()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8057 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8058 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 8059 | AudioSystem::releaseInput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8060 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8061 | mLock.lock(); |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 8062 | } else { |
| 8063 | mHalStream->stop(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8064 | } |
| 8065 | return PERMISSION_DENIED; |
| 8066 | } |
| 8067 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8068 | if (!isOutput() && !silenced) { |
| 8069 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8070 | if (track->isSilenced_l() && track->uid() != client.clientUid) |
| 8071 | track->invalidate(); |
| 8072 | } |
| 8073 | } |
| 8074 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 8075 | // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ? |
| 8076 | sp<MmapTrack> track = new MmapTrack(this, mAttr, mSampleRate, mFormat, mChannelMask, mSessionId, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8077 | client.clientUid, client.clientPid, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8078 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8079 | track->setSilenced_l(silenced); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8080 | mActiveTracks.add(track); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8081 | sp<EffectChain> chain = getEffectChain_l(mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8082 | if (chain != 0) { |
| 8083 | chain->setStrategy(AudioSystem::getStrategyForStream(streamType())); |
| 8084 | chain->incTrackCnt(); |
| 8085 | chain->incActiveTrackCnt(); |
| 8086 | } |
| 8087 | |
| 8088 | *handle = portId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8089 | broadcast_l(); |
| 8090 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8091 | ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8092 | |
| 8093 | return NO_ERROR; |
| 8094 | } |
| 8095 | |
| 8096 | status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle) |
| 8097 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8098 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 8099 | |
| 8100 | if (mHalStream == 0) { |
| 8101 | return NO_INIT; |
| 8102 | } |
| 8103 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8104 | if (handle == mPortId) { |
| 8105 | mHalStream->stop(); |
| 8106 | return NO_ERROR; |
| 8107 | } |
| 8108 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8109 | Mutex::Autolock _l(mLock); |
| 8110 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8111 | sp<MmapTrack> track; |
| 8112 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 8113 | if (handle == t->portId()) { |
| 8114 | track = t; |
| 8115 | break; |
| 8116 | } |
| 8117 | } |
| 8118 | if (track == 0) { |
| 8119 | return BAD_VALUE; |
| 8120 | } |
| 8121 | |
| 8122 | mActiveTracks.remove(track); |
| 8123 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8124 | mLock.unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8125 | if (isOutput()) { |
| 8126 | AudioSystem::stopOutput(mId, streamType(), track->sessionId()); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8127 | AudioSystem::releaseOutput(mId, streamType(), track->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8128 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 8129 | AudioSystem::stopInput(track->portId()); |
| 8130 | AudioSystem::releaseInput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8131 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8132 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8133 | |
| 8134 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 8135 | if (chain != 0) { |
| 8136 | chain->decActiveTrackCnt(); |
| 8137 | chain->decTrackCnt(); |
| 8138 | } |
| 8139 | |
| 8140 | broadcast_l(); |
| 8141 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8142 | return NO_ERROR; |
| 8143 | } |
| 8144 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 8145 | status_t AudioFlinger::MmapThread::standby() |
| 8146 | { |
| 8147 | ALOGV("%s", __FUNCTION__); |
| 8148 | |
| 8149 | if (mHalStream == 0) { |
| 8150 | return NO_INIT; |
| 8151 | } |
| 8152 | if (mActiveTracks.size() != 0) { |
| 8153 | return INVALID_OPERATION; |
| 8154 | } |
| 8155 | mHalStream->standby(); |
| 8156 | mStandby = true; |
| 8157 | releaseWakeLock(); |
| 8158 | return NO_ERROR; |
| 8159 | } |
| 8160 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8161 | |
| 8162 | void AudioFlinger::MmapThread::readHalParameters_l() |
| 8163 | { |
| 8164 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 8165 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
| 8166 | mFormat = mHALFormat; |
| 8167 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 8168 | result = mHalStream->getFrameSize(&mFrameSize); |
| 8169 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
| 8170 | result = mHalStream->getBufferSize(&mBufferSize); |
| 8171 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
| 8172 | mFrameCount = mBufferSize / mFrameSize; |
| 8173 | } |
| 8174 | |
| 8175 | bool AudioFlinger::MmapThread::threadLoop() |
| 8176 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8177 | checkSilentMode_l(); |
| 8178 | |
| 8179 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 8180 | |
| 8181 | while (!exitPending()) |
| 8182 | { |
| 8183 | Mutex::Autolock _l(mLock); |
| 8184 | Vector< sp<EffectChain> > effectChains; |
| 8185 | |
| 8186 | if (mSignalPending) { |
| 8187 | // A signal was raised while we were unlocked |
| 8188 | mSignalPending = false; |
| 8189 | } else { |
| 8190 | if (mConfigEvents.isEmpty()) { |
| 8191 | // we're about to wait, flush the binder command buffer |
| 8192 | IPCThreadState::self()->flushCommands(); |
| 8193 | |
| 8194 | if (exitPending()) { |
| 8195 | break; |
| 8196 | } |
| 8197 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8198 | // wait until we have something to do... |
| 8199 | ALOGV("%s going to sleep", myName.string()); |
| 8200 | mWaitWorkCV.wait(mLock); |
| 8201 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8202 | |
| 8203 | checkSilentMode_l(); |
| 8204 | |
| 8205 | continue; |
| 8206 | } |
| 8207 | } |
| 8208 | |
| 8209 | processConfigEvents_l(); |
| 8210 | |
| 8211 | processVolume_l(); |
| 8212 | |
| 8213 | checkInvalidTracks_l(); |
| 8214 | |
| 8215 | mActiveTracks.updatePowerState(this); |
| 8216 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8217 | updateMetadata_l(); |
| 8218 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8219 | lockEffectChains_l(effectChains); |
| 8220 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 8221 | effectChains[i]->process_l(); |
| 8222 | } |
| 8223 | // enable changes in effect chain |
| 8224 | unlockEffectChains(effectChains); |
| 8225 | // Effect chains will be actually deleted here if they were removed from |
| 8226 | // mEffectChains list during mixing or effects processing |
| 8227 | } |
| 8228 | |
| 8229 | threadLoop_exit(); |
| 8230 | |
| 8231 | if (!mStandby) { |
| 8232 | threadLoop_standby(); |
| 8233 | mStandby = true; |
| 8234 | } |
| 8235 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8236 | ALOGV("Thread %p type %d exiting", this, mType); |
| 8237 | return false; |
| 8238 | } |
| 8239 | |
| 8240 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 8241 | bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair, |
| 8242 | status_t& status) |
| 8243 | { |
| 8244 | AudioParameter param = AudioParameter(keyValuePair); |
| 8245 | int value; |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8246 | bool sendToHal = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8247 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8248 | audio_devices_t device = (audio_devices_t)value; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8249 | // forward device change to effects that have requested to be |
| 8250 | // aware of attached audio device. |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8251 | if (device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8252 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8253 | mEffectChains[i]->setDevice_l(device); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8254 | } |
| 8255 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8256 | if (audio_is_output_devices(device)) { |
| 8257 | mOutDevice = device; |
| 8258 | if (!isOutput()) { |
| 8259 | sendToHal = false; |
| 8260 | } |
| 8261 | } else { |
| 8262 | mInDevice = device; |
| 8263 | if (device != AUDIO_DEVICE_NONE) { |
| 8264 | mPrevInDevice = value; |
| 8265 | } |
| 8266 | // TODO: implement and call checkBtNrec_l(); |
| 8267 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8268 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8269 | if (sendToHal) { |
| 8270 | status = mHalStream->setParameters(keyValuePair); |
| 8271 | } else { |
| 8272 | status = NO_ERROR; |
| 8273 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8274 | |
| 8275 | return false; |
| 8276 | } |
| 8277 | |
| 8278 | String8 AudioFlinger::MmapThread::getParameters(const String8& keys) |
| 8279 | { |
| 8280 | Mutex::Autolock _l(mLock); |
| 8281 | String8 out_s8; |
| 8282 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { |
| 8283 | return out_s8; |
| 8284 | } |
| 8285 | return String8(); |
| 8286 | } |
| 8287 | |
| 8288 | void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
| 8289 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 8290 | |
| 8291 | desc->mIoHandle = mId; |
| 8292 | |
| 8293 | switch (event) { |
| 8294 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8295 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8296 | case AUDIO_INPUT_CONFIG_CHANGED: |
| 8297 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8298 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8299 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 8300 | desc->mPatch = mPatch; |
| 8301 | desc->mChannelMask = mChannelMask; |
| 8302 | desc->mSamplingRate = mSampleRate; |
| 8303 | desc->mFormat = mFormat; |
| 8304 | desc->mFrameCount = mFrameCount; |
| 8305 | desc->mFrameCountHAL = mFrameCount; |
| 8306 | desc->mLatency = 0; |
| 8307 | break; |
| 8308 | |
| 8309 | case AUDIO_INPUT_CLOSED: |
| 8310 | case AUDIO_OUTPUT_CLOSED: |
| 8311 | default: |
| 8312 | break; |
| 8313 | } |
| 8314 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
| 8315 | } |
| 8316 | |
| 8317 | status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch, |
| 8318 | audio_patch_handle_t *handle) |
| 8319 | { |
| 8320 | status_t status = NO_ERROR; |
| 8321 | |
| 8322 | // store new device and send to effects |
| 8323 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 8324 | audio_port_handle_t deviceId; |
| 8325 | if (isOutput()) { |
| 8326 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 8327 | type |= patch->sinks[i].ext.device.type; |
| 8328 | } |
| 8329 | deviceId = patch->sinks[0].id; |
| 8330 | } else { |
| 8331 | type = patch->sources[0].ext.device.type; |
| 8332 | deviceId = patch->sources[0].id; |
| 8333 | } |
| 8334 | |
| 8335 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8336 | mEffectChains[i]->setDevice_l(type); |
| 8337 | } |
| 8338 | |
| 8339 | if (isOutput()) { |
| 8340 | mOutDevice = type; |
| 8341 | } else { |
| 8342 | mInDevice = type; |
| 8343 | // store new source and send to effects |
| 8344 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 8345 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 8346 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8347 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 8348 | } |
| 8349 | } |
| 8350 | } |
| 8351 | |
| 8352 | if (mAudioHwDev->supportsAudioPatches()) { |
| 8353 | status = mHalDevice->createAudioPatch(patch->num_sources, |
| 8354 | patch->sources, |
| 8355 | patch->num_sinks, |
| 8356 | patch->sinks, |
| 8357 | handle); |
| 8358 | } else { |
| 8359 | char *address; |
| 8360 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 8361 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 8362 | address = audio_device_address_to_parameter( |
| 8363 | patch->sinks[0].ext.device.type, |
| 8364 | patch->sinks[0].ext.device.address); |
| 8365 | } else { |
| 8366 | address = (char *)calloc(1, 1); |
| 8367 | } |
| 8368 | AudioParameter param = AudioParameter(String8(address)); |
| 8369 | free(address); |
| 8370 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
| 8371 | if (!isOutput()) { |
| 8372 | param.addInt(String8(AudioParameter::keyInputSource), |
| 8373 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 8374 | } |
| 8375 | status = mHalStream->setParameters(param.toString()); |
| 8376 | *handle = AUDIO_PATCH_HANDLE_NONE; |
| 8377 | } |
| 8378 | |
| 8379 | if (isOutput() && mPrevOutDevice != mOutDevice) { |
| 8380 | mPrevOutDevice = type; |
| 8381 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8382 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8383 | if (mDeviceId != deviceId && callback != 0) { |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8384 | mLock.unlock(); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8385 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8386 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8387 | } |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8388 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8389 | } |
| 8390 | if (!isOutput() && mPrevInDevice != mInDevice) { |
| 8391 | mPrevInDevice = type; |
| 8392 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8393 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8394 | if (mDeviceId != deviceId && callback != 0) { |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8395 | mLock.unlock(); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8396 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8397 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8398 | } |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8399 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8400 | } |
| 8401 | return status; |
| 8402 | } |
| 8403 | |
| 8404 | status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 8405 | { |
| 8406 | status_t status = NO_ERROR; |
| 8407 | |
| 8408 | mInDevice = AUDIO_DEVICE_NONE; |
| 8409 | |
| 8410 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? |
| 8411 | supportsAudioPatches : false; |
| 8412 | |
| 8413 | if (supportsAudioPatches) { |
| 8414 | status = mHalDevice->releaseAudioPatch(handle); |
| 8415 | } else { |
| 8416 | AudioParameter param; |
| 8417 | param.addInt(String8(AudioParameter::keyRouting), 0); |
| 8418 | status = mHalStream->setParameters(param.toString()); |
| 8419 | } |
| 8420 | return status; |
| 8421 | } |
| 8422 | |
| 8423 | void AudioFlinger::MmapThread::getAudioPortConfig(struct audio_port_config *config) |
| 8424 | { |
| 8425 | ThreadBase::getAudioPortConfig(config); |
| 8426 | if (isOutput()) { |
| 8427 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 8428 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 8429 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 8430 | } else { |
| 8431 | config->role = AUDIO_PORT_ROLE_SINK; |
| 8432 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 8433 | config->ext.mix.usecase.source = mAudioSource; |
| 8434 | } |
| 8435 | } |
| 8436 | |
| 8437 | status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 8438 | { |
| 8439 | audio_session_t session = chain->sessionId(); |
| 8440 | |
| 8441 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 8442 | // Attach all tracks with same session ID to this chain. |
| 8443 | // indicate all active tracks in the chain |
| 8444 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8445 | if (session == track->sessionId()) { |
| 8446 | chain->incTrackCnt(); |
| 8447 | chain->incActiveTrackCnt(); |
| 8448 | } |
| 8449 | } |
| 8450 | |
| 8451 | chain->setThread(this); |
| 8452 | chain->setInBuffer(nullptr); |
| 8453 | chain->setOutBuffer(nullptr); |
| 8454 | chain->syncHalEffectsState(); |
| 8455 | |
| 8456 | mEffectChains.add(chain); |
| 8457 | checkSuspendOnAddEffectChain_l(chain); |
| 8458 | return NO_ERROR; |
| 8459 | } |
| 8460 | |
| 8461 | size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 8462 | { |
| 8463 | audio_session_t session = chain->sessionId(); |
| 8464 | |
| 8465 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 8466 | |
| 8467 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8468 | if (chain == mEffectChains[i]) { |
| 8469 | mEffectChains.removeAt(i); |
| 8470 | // detach all active tracks from the chain |
| 8471 | // detach all tracks with same session ID from this chain |
| 8472 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8473 | if (session == track->sessionId()) { |
| 8474 | chain->decActiveTrackCnt(); |
| 8475 | chain->decTrackCnt(); |
| 8476 | } |
| 8477 | } |
| 8478 | break; |
| 8479 | } |
| 8480 | } |
| 8481 | return mEffectChains.size(); |
| 8482 | } |
| 8483 | |
| 8484 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 8485 | uint32_t AudioFlinger::MmapThread::hasAudioSession_l(audio_session_t sessionId) const |
| 8486 | { |
| 8487 | uint32_t result = 0; |
| 8488 | if (getEffectChain_l(sessionId) != 0) { |
| 8489 | result = EFFECT_SESSION; |
| 8490 | } |
| 8491 | |
| 8492 | for (size_t i = 0; i < mActiveTracks.size(); i++) { |
| 8493 | sp<MmapTrack> track = mActiveTracks[i]; |
| 8494 | if (sessionId == track->sessionId()) { |
| 8495 | result |= TRACK_SESSION; |
| 8496 | if (track->isFastTrack()) { |
| 8497 | result |= FAST_SESSION; |
| 8498 | } |
| 8499 | break; |
| 8500 | } |
| 8501 | } |
| 8502 | |
| 8503 | return result; |
| 8504 | } |
| 8505 | |
| 8506 | void AudioFlinger::MmapThread::threadLoop_standby() |
| 8507 | { |
| 8508 | mHalStream->standby(); |
| 8509 | } |
| 8510 | |
| 8511 | void AudioFlinger::MmapThread::threadLoop_exit() |
| 8512 | { |
Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 8513 | // Do not call callback->onTearDown() because it is redundant for thread exit |
| 8514 | // and because it can cause a recursive mutex lock on stop(). |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8515 | } |
| 8516 | |
| 8517 | status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
| 8518 | { |
| 8519 | return BAD_VALUE; |
| 8520 | } |
| 8521 | |
| 8522 | bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
| 8523 | { |
| 8524 | return false; |
| 8525 | } |
| 8526 | |
| 8527 | status_t AudioFlinger::MmapThread::checkEffectCompatibility_l( |
| 8528 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 8529 | { |
| 8530 | // No global effect sessions on mmap threads |
| 8531 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 8532 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 8533 | desc->name, mThreadName); |
| 8534 | return BAD_VALUE; |
| 8535 | } |
| 8536 | |
| 8537 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 8538 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", |
| 8539 | desc->name); |
| 8540 | return BAD_VALUE; |
| 8541 | } |
| 8542 | 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] | 8543 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " |
| 8544 | "thread", desc->name); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8545 | return BAD_VALUE; |
| 8546 | } |
| 8547 | |
| 8548 | // Only allow effects without processing load or latency |
| 8549 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { |
| 8550 | return BAD_VALUE; |
| 8551 | } |
| 8552 | |
| 8553 | return NO_ERROR; |
| 8554 | |
| 8555 | } |
| 8556 | |
| 8557 | void AudioFlinger::MmapThread::checkInvalidTracks_l() |
| 8558 | { |
| 8559 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8560 | if (track->isInvalid()) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8561 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8562 | if (callback != 0) { |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8563 | mLock.unlock(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8564 | callback->onTearDown(track->portId()); |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8565 | mLock.lock(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8566 | } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 8567 | ALOGW("Could not notify MMAP stream tear down: no onTearDown callback!"); |
| 8568 | mNoCallbackWarningCount++; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8569 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8570 | } |
| 8571 | } |
| 8572 | } |
| 8573 | |
| 8574 | void AudioFlinger::MmapThread::dump(int fd, const Vector<String16>& args) |
| 8575 | { |
| 8576 | dumpInternals(fd, args); |
| 8577 | dumpTracks(fd, args); |
| 8578 | dumpEffectChains(fd, args); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8579 | dprintf(fd, " Local log:\n"); |
| 8580 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8581 | } |
| 8582 | |
| 8583 | void AudioFlinger::MmapThread::dumpInternals(int fd, const Vector<String16>& args) |
| 8584 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8585 | dumpBase(fd, args); |
| 8586 | |
| 8587 | dprintf(fd, " Attributes: content type %d usage %d source %d\n", |
| 8588 | mAttr.content_type, mAttr.usage, mAttr.source); |
| 8589 | dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId); |
| 8590 | if (mActiveTracks.size() == 0) { |
| 8591 | dprintf(fd, " No active clients\n"); |
| 8592 | } |
| 8593 | } |
| 8594 | |
| 8595 | void AudioFlinger::MmapThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
| 8596 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8597 | String8 result; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8598 | size_t numtracks = mActiveTracks.size(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8599 | dprintf(fd, " %zu Tracks\n", numtracks); |
| 8600 | const char *prefix = " "; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8601 | if (numtracks) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8602 | result.append(prefix); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8603 | MmapTrack::appendDumpHeader(result); |
| 8604 | for (size_t i = 0; i < numtracks ; ++i) { |
| 8605 | sp<MmapTrack> track = mActiveTracks[i]; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8606 | result.append(prefix); |
| 8607 | track->appendDump(result, true /* active */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8608 | } |
| 8609 | } else { |
| 8610 | dprintf(fd, "\n"); |
| 8611 | } |
| 8612 | write(fd, result.string(), result.size()); |
| 8613 | } |
| 8614 | |
| 8615 | AudioFlinger::MmapPlaybackThread::MmapPlaybackThread( |
| 8616 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 8617 | AudioHwDevice *hwDev, AudioStreamOut *output, |
| 8618 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 8619 | : MmapThread(audioFlinger, id, hwDev, output->stream, outDevice, inDevice, systemReady), |
| 8620 | mStreamType(AUDIO_STREAM_MUSIC), |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 8621 | mStreamVolume(1.0), |
| 8622 | mStreamMute(false), |
| 8623 | mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later. |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 8624 | mOutput(output) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8625 | { |
| 8626 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); |
| 8627 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 8628 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 8629 | mMasterMute = audioFlinger->masterMute_l(); |
| 8630 | if (mAudioHwDev) { |
| 8631 | if (mAudioHwDev->canSetMasterVolume()) { |
| 8632 | mMasterVolume = 1.0; |
| 8633 | } |
| 8634 | |
| 8635 | if (mAudioHwDev->canSetMasterMute()) { |
| 8636 | mMasterMute = false; |
| 8637 | } |
| 8638 | } |
| 8639 | } |
| 8640 | |
| 8641 | void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr, |
| 8642 | audio_stream_type_t streamType, |
| 8643 | audio_session_t sessionId, |
| 8644 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8645 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8646 | audio_port_handle_t portId) |
| 8647 | { |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8648 | MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8649 | mStreamType = streamType; |
| 8650 | } |
| 8651 | |
| 8652 | AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput() |
| 8653 | { |
| 8654 | Mutex::Autolock _l(mLock); |
| 8655 | AudioStreamOut *output = mOutput; |
| 8656 | mOutput = NULL; |
| 8657 | return output; |
| 8658 | } |
| 8659 | |
| 8660 | void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value) |
| 8661 | { |
| 8662 | Mutex::Autolock _l(mLock); |
| 8663 | // Don't apply master volume in SW if our HAL can do it for us. |
| 8664 | if (mAudioHwDev && |
| 8665 | mAudioHwDev->canSetMasterVolume()) { |
| 8666 | mMasterVolume = 1.0; |
| 8667 | } else { |
| 8668 | mMasterVolume = value; |
| 8669 | } |
| 8670 | } |
| 8671 | |
| 8672 | void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted) |
| 8673 | { |
| 8674 | Mutex::Autolock _l(mLock); |
| 8675 | // Don't apply master mute in SW if our HAL can do it for us. |
| 8676 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { |
| 8677 | mMasterMute = false; |
| 8678 | } else { |
| 8679 | mMasterMute = muted; |
| 8680 | } |
| 8681 | } |
| 8682 | |
| 8683 | void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 8684 | { |
| 8685 | Mutex::Autolock _l(mLock); |
| 8686 | if (stream == mStreamType) { |
| 8687 | mStreamVolume = value; |
| 8688 | broadcast_l(); |
| 8689 | } |
| 8690 | } |
| 8691 | |
| 8692 | float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 8693 | { |
| 8694 | Mutex::Autolock _l(mLock); |
| 8695 | if (stream == mStreamType) { |
| 8696 | return mStreamVolume; |
| 8697 | } |
| 8698 | return 0.0f; |
| 8699 | } |
| 8700 | |
| 8701 | void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 8702 | { |
| 8703 | Mutex::Autolock _l(mLock); |
| 8704 | if (stream == mStreamType) { |
| 8705 | mStreamMute= muted; |
| 8706 | broadcast_l(); |
| 8707 | } |
| 8708 | } |
| 8709 | |
| 8710 | void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 8711 | { |
| 8712 | Mutex::Autolock _l(mLock); |
| 8713 | if (streamType == mStreamType) { |
| 8714 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8715 | track->invalidate(); |
| 8716 | } |
| 8717 | broadcast_l(); |
| 8718 | } |
| 8719 | } |
| 8720 | |
| 8721 | void AudioFlinger::MmapPlaybackThread::processVolume_l() |
| 8722 | { |
| 8723 | float volume; |
| 8724 | |
| 8725 | if (mMasterMute || mStreamMute) { |
| 8726 | volume = 0; |
| 8727 | } else { |
| 8728 | volume = mMasterVolume * mStreamVolume; |
| 8729 | } |
| 8730 | |
| 8731 | if (volume != mHalVolFloat) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8732 | |
| 8733 | // Convert volumes from float to 8.24 |
| 8734 | uint32_t vol = (uint32_t)(volume * (1 << 24)); |
| 8735 | |
| 8736 | // Delegate volume control to effect in track effect chain if needed |
| 8737 | // only one effect chain can be present on DirectOutputThread, so if |
| 8738 | // there is one, the track is connected to it |
| 8739 | if (!mEffectChains.isEmpty()) { |
| 8740 | mEffectChains[0]->setVolume_l(&vol, &vol); |
| 8741 | volume = (float)vol / (1 << 24); |
| 8742 | } |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8743 | // Try to use HW volume control and fall back to SW control if not implemented |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 8744 | if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) { |
| 8745 | mHalVolFloat = volume; // HW volume control worked, so update value. |
| 8746 | mNoCallbackWarningCount = 0; |
| 8747 | } else { |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8748 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8749 | if (callback != 0) { |
| 8750 | int channelCount; |
| 8751 | if (isOutput()) { |
| 8752 | channelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 8753 | } else { |
| 8754 | channelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8755 | } |
| 8756 | Vector<float> values; |
| 8757 | for (int i = 0; i < channelCount; i++) { |
| 8758 | values.add(volume); |
| 8759 | } |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 8760 | mHalVolFloat = volume; // SW volume control worked, so update value. |
| 8761 | mNoCallbackWarningCount = 0; |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 8762 | mLock.unlock(); |
| 8763 | callback->onVolumeChanged(mChannelMask, values); |
| 8764 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8765 | } else { |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 8766 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 8767 | ALOGW("Could not set MMAP stream volume: no volume callback!"); |
| 8768 | mNoCallbackWarningCount++; |
| 8769 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8770 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8771 | } |
| 8772 | } |
| 8773 | } |
| 8774 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8775 | void AudioFlinger::MmapPlaybackThread::updateMetadata_l() |
| 8776 | { |
| 8777 | if (mOutput == nullptr || mOutput->stream == nullptr || |
| 8778 | !mActiveTracks.readAndClearHasChanged()) { |
| 8779 | return; |
| 8780 | } |
| 8781 | StreamOutHalInterface::SourceMetadata metadata; |
| 8782 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8783 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
| 8784 | metadata.tracks.push_back({ |
| 8785 | .usage = track->attributes().usage, |
| 8786 | .content_type = track->attributes().content_type, |
| 8787 | .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume |
| 8788 | }); |
| 8789 | } |
| 8790 | mOutput->stream->updateSourceMetadata(metadata); |
| 8791 | } |
| 8792 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8793 | void AudioFlinger::MmapPlaybackThread::checkSilentMode_l() |
| 8794 | { |
| 8795 | if (!mMasterMute) { |
| 8796 | char value[PROPERTY_VALUE_MAX]; |
| 8797 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 8798 | char *endptr; |
| 8799 | unsigned long ul = strtoul(value, &endptr, 0); |
| 8800 | if (*endptr == '\0' && ul != 0) { |
| 8801 | ALOGD("Silence is golden"); |
| 8802 | // The setprop command will not allow a property to be changed after |
| 8803 | // the first time it is set, so we don't have to worry about un-muting. |
| 8804 | setMasterMute_l(true); |
| 8805 | } |
| 8806 | } |
| 8807 | } |
| 8808 | } |
| 8809 | |
| 8810 | void AudioFlinger::MmapPlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 8811 | { |
| 8812 | MmapThread::dumpInternals(fd, args); |
| 8813 | |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8814 | dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", |
| 8815 | mStreamType, mStreamVolume, mHalVolFloat, mStreamMute); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8816 | dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); |
| 8817 | } |
| 8818 | |
| 8819 | AudioFlinger::MmapCaptureThread::MmapCaptureThread( |
| 8820 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 8821 | AudioHwDevice *hwDev, AudioStreamIn *input, |
| 8822 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 8823 | : MmapThread(audioFlinger, id, hwDev, input->stream, outDevice, inDevice, systemReady), |
| 8824 | mInput(input) |
| 8825 | { |
| 8826 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); |
| 8827 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8828 | } |
| 8829 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8830 | status_t AudioFlinger::MmapCaptureThread::exitStandby() |
| 8831 | { |
| 8832 | mInput->stream->setGain(1.0f); |
| 8833 | return MmapThread::exitStandby(); |
| 8834 | } |
| 8835 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8836 | AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput() |
| 8837 | { |
| 8838 | Mutex::Autolock _l(mLock); |
| 8839 | AudioStreamIn *input = mInput; |
| 8840 | mInput = NULL; |
| 8841 | return input; |
| 8842 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8843 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8844 | |
| 8845 | void AudioFlinger::MmapCaptureThread::processVolume_l() |
| 8846 | { |
| 8847 | bool changed = false; |
| 8848 | bool silenced = false; |
| 8849 | |
| 8850 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8851 | if (callback == 0) { |
| 8852 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 8853 | ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!"); |
| 8854 | mNoCallbackWarningCount++; |
| 8855 | } |
| 8856 | } |
| 8857 | |
| 8858 | // After a change occurred in track silenced state, mute capture in audio DSP if at least one |
| 8859 | // track is silenced and unmute otherwise |
| 8860 | for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) { |
| 8861 | if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) { |
| 8862 | changed = true; |
| 8863 | silenced = mActiveTracks[i]->isSilenced_l(); |
| 8864 | } |
| 8865 | } |
| 8866 | |
| 8867 | if (changed) { |
| 8868 | mInput->stream->setGain(silenced ? 0.0f: 1.0f); |
| 8869 | } |
| 8870 | } |
| 8871 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8872 | void AudioFlinger::MmapCaptureThread::updateMetadata_l() |
| 8873 | { |
| 8874 | if (mInput == nullptr || mInput->stream == nullptr || |
| 8875 | !mActiveTracks.readAndClearHasChanged()) { |
| 8876 | return; |
| 8877 | } |
| 8878 | StreamInHalInterface::SinkMetadata metadata; |
| 8879 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8880 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
| 8881 | metadata.tracks.push_back({ |
| 8882 | .source = track->attributes().source, |
| 8883 | .gain = 1, // capture tracks do not have volumes |
| 8884 | }); |
| 8885 | } |
| 8886 | mInput->stream->updateSinkMetadata(metadata); |
| 8887 | } |
| 8888 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 8889 | void AudioFlinger::MmapCaptureThread::setRecordSilenced(uid_t uid, bool silenced) |
| 8890 | { |
| 8891 | Mutex::Autolock _l(mLock); |
| 8892 | for (size_t i = 0; i < mActiveTracks.size() ; i++) { |
| 8893 | if (mActiveTracks[i]->uid() == uid) { |
| 8894 | mActiveTracks[i]->setSilenced_l(silenced); |
| 8895 | broadcast_l(); |
| 8896 | } |
| 8897 | } |
| 8898 | } |
| 8899 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 8900 | } // namespace android |