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> |
| 60 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 61 | #include "AudioFlinger.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 63 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 | #include "ServiceUtilities.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 65 | #include "mediautils/SchedulingPolicyService.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 66 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 67 | #ifdef ADD_BATTERY_DATA |
| 68 | #include <media/IMediaPlayerService.h> |
| 69 | #include <media/IMediaDeathNotifier.h> |
| 70 | #endif |
| 71 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 72 | #ifdef DEBUG_CPU_USAGE |
| 73 | #include <cpustats/CentralTendencyStatistics.h> |
| 74 | #include <cpustats/ThreadCpuUsage.h> |
| 75 | #endif |
| 76 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 77 | #include "AutoPark.h" |
| 78 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 79 | #include <pthread.h> |
| 80 | #include "TypedLogger.h" |
| 81 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 82 | // ---------------------------------------------------------------------------- |
| 83 | |
| 84 | // Note: the following macro is used for extremely verbose logging message. In |
| 85 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 86 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 87 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 88 | // turned on. Do not uncomment the #def below unless you really know what you |
| 89 | // are doing and want to see all of the extremely verbose messages. |
| 90 | //#define VERY_VERY_VERBOSE_LOGGING |
| 91 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 92 | #define ALOGVV ALOGV |
| 93 | #else |
| 94 | #define ALOGVV(a...) do { } while(0) |
| 95 | #endif |
| 96 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 97 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 98 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 99 | template <typename T> |
| 100 | static inline T min(const T& a, const T& b) |
| 101 | { |
| 102 | return a < b ? a : b; |
| 103 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 104 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 105 | namespace android { |
| 106 | |
| 107 | // retry counts for buffer fill timeout |
| 108 | // 50 * ~20msecs = 1 second |
| 109 | static const int8_t kMaxTrackRetries = 50; |
| 110 | static const int8_t kMaxTrackStartupRetries = 50; |
| 111 | // allow less retry attempts on direct output thread. |
| 112 | // direct outputs can be a scarce resource in audio hardware and should |
| 113 | // be released as quickly as possible. |
| 114 | static const int8_t kMaxTrackRetriesDirect = 2; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 115 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 116 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 117 | |
| 118 | // don't warn about blocked writes or record buffer overflows more often than this |
| 119 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 120 | |
| 121 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 122 | static const int kRecordThreadSleepUs = 5000; |
| 123 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 124 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 125 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 126 | |
| 127 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 128 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 129 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 130 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 131 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 132 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 133 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 134 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 135 | // maximum normal sink buffer size |
| 136 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 137 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 138 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 139 | // FIXME This should be based on experimentally observed scheduling jitter |
| 140 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 141 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 142 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 143 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 144 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 145 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 146 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 147 | |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 148 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good |
| 149 | // balance between power consumption and latency, and allows threads to be scheduled reliably |
| 150 | // by the CFS scheduler. |
| 151 | // FIXME Express other hardcoded references to 20ms with references to this constant and move |
| 152 | // it appropriately. |
| 153 | #define FMS_20 20 |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 154 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 155 | // Whether to use fast mixer |
| 156 | static const enum { |
| 157 | FastMixer_Never, // never initialize or use: for debugging only |
| 158 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 159 | // normal mixer multiplier is 1 |
| 160 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 161 | // multiplier is calculated based on min & max normal mixer buffer size |
| 162 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 163 | // multiplier is calculated based on min & max normal mixer buffer size |
| 164 | // FIXME for FastMixer_Dynamic: |
| 165 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 166 | // For example, one HAL implementation returns an error from a large write, |
| 167 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 168 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 169 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 170 | } kUseFastMixer = FastMixer_Static; |
| 171 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 172 | // Whether to use fast capture |
| 173 | static const enum { |
| 174 | FastCapture_Never, // never initialize or use: for debugging only |
| 175 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 176 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 177 | } kUseFastCapture = FastCapture_Static; |
| 178 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 179 | // Priorities for requestPriority |
| 180 | static const int kPriorityAudioApp = 2; |
| 181 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 182 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 183 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 184 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 185 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 186 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 187 | |
| 188 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 189 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 190 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 191 | // The minimum and maximum allowed values |
| 192 | static const int kFastTrackMultiplierMin = 1; |
| 193 | static const int kFastTrackMultiplierMax = 2; |
| 194 | |
| 195 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 196 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 197 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 198 | // See Thread::readOnlyHeap(). |
| 199 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 200 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 201 | // 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] | 202 | static const size_t kRecordThreadReadOnlyHeapSize = 0x4000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 203 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 204 | // ---------------------------------------------------------------------------- |
| 205 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 206 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 207 | |
| 208 | static void sFastTrackMultiplierInit() |
| 209 | { |
| 210 | char value[PROPERTY_VALUE_MAX]; |
| 211 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 212 | char *endptr; |
| 213 | unsigned long ul = strtoul(value, &endptr, 0); |
| 214 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 215 | sFastTrackMultiplier = (int) ul; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // ---------------------------------------------------------------------------- |
| 221 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 222 | #ifdef ADD_BATTERY_DATA |
| 223 | // To collect the amplifier usage |
| 224 | static void addBatteryData(uint32_t params) { |
| 225 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 226 | if (service == NULL) { |
| 227 | // it already logged |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | service->addBatteryData(params); |
| 232 | } |
| 233 | #endif |
| 234 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 235 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 236 | struct { |
| 237 | // call when you acquire a partial wakelock |
| 238 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 239 | pthread_mutex_lock(&mLock); |
| 240 | if (wakeLockToken.get() == nullptr) { |
| 241 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 242 | } else { |
| 243 | if (mCount == 0) { |
| 244 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 245 | } |
| 246 | ++mCount; |
| 247 | } |
| 248 | pthread_mutex_unlock(&mLock); |
| 249 | } |
| 250 | |
| 251 | // call when you release a partial wakelock. |
| 252 | void release(const sp<IBinder> &wakeLockToken) { |
| 253 | if (wakeLockToken.get() == nullptr) { |
| 254 | return; |
| 255 | } |
| 256 | pthread_mutex_lock(&mLock); |
| 257 | if (--mCount < 0) { |
| 258 | ALOGE("negative wakelock count"); |
| 259 | mCount = 0; |
| 260 | } |
| 261 | pthread_mutex_unlock(&mLock); |
| 262 | } |
| 263 | |
| 264 | // retrieves the boottime timebase offset from monotonic. |
| 265 | int64_t getBoottimeOffset() { |
| 266 | pthread_mutex_lock(&mLock); |
| 267 | int64_t boottimeOffset = mBoottimeOffset; |
| 268 | pthread_mutex_unlock(&mLock); |
| 269 | return boottimeOffset; |
| 270 | } |
| 271 | |
| 272 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 273 | // and the selected timebase. |
| 274 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 275 | // |
| 276 | // This only needs to be called upon acquiring the first partial wakelock |
| 277 | // after all other partial wakelocks are released. |
| 278 | // |
| 279 | // We do an empirical measurement of the offset rather than parsing |
| 280 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 281 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 282 | int clockbase; |
| 283 | switch (timebase) { |
| 284 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 285 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 286 | break; |
| 287 | default: |
| 288 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 289 | break; |
| 290 | } |
| 291 | // try three times to get the clock offset, choose the one |
| 292 | // with the minimum gap in measurements. |
| 293 | const int tries = 3; |
| 294 | nsecs_t bestGap, measured; |
| 295 | for (int i = 0; i < tries; ++i) { |
| 296 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 297 | const nsecs_t tbase = systemTime(clockbase); |
| 298 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 299 | const nsecs_t gap = tmono2 - tmono; |
| 300 | if (i == 0 || gap < bestGap) { |
| 301 | bestGap = gap; |
| 302 | measured = tbase - ((tmono + tmono2) >> 1); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // to avoid micro-adjusting, we don't change the timebase |
| 307 | // unless it is significantly different. |
| 308 | // |
| 309 | // Assumption: It probably takes more than toleranceNs to |
| 310 | // suspend and resume the device. |
| 311 | static int64_t toleranceNs = 10000; // 10 us |
| 312 | if (llabs(*offset - measured) > toleranceNs) { |
| 313 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 314 | (long long)*offset, (long long)measured); |
| 315 | *offset = measured; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | pthread_mutex_t mLock; |
| 320 | int32_t mCount; |
| 321 | int64_t mBoottimeOffset; |
| 322 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 323 | |
| 324 | // ---------------------------------------------------------------------------- |
| 325 | // CPU Stats |
| 326 | // ---------------------------------------------------------------------------- |
| 327 | |
| 328 | class CpuStats { |
| 329 | public: |
| 330 | CpuStats(); |
| 331 | void sample(const String8 &title); |
| 332 | #ifdef DEBUG_CPU_USAGE |
| 333 | private: |
| 334 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 335 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 336 | |
| 337 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 338 | |
| 339 | int mCpuNum; // thread's current CPU number |
| 340 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 341 | #endif |
| 342 | }; |
| 343 | |
| 344 | CpuStats::CpuStats() |
| 345 | #ifdef DEBUG_CPU_USAGE |
| 346 | : mCpuNum(-1), mCpukHz(-1) |
| 347 | #endif |
| 348 | { |
| 349 | } |
| 350 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 351 | void CpuStats::sample(const String8 &title |
| 352 | #ifndef DEBUG_CPU_USAGE |
| 353 | __unused |
| 354 | #endif |
| 355 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 356 | #ifdef DEBUG_CPU_USAGE |
| 357 | // get current thread's delta CPU time in wall clock ns |
| 358 | double wcNs; |
| 359 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 360 | |
| 361 | // record sample for wall clock statistics |
| 362 | if (valid) { |
| 363 | mWcStats.sample(wcNs); |
| 364 | } |
| 365 | |
| 366 | // get the current CPU number |
| 367 | int cpuNum = sched_getcpu(); |
| 368 | |
| 369 | // get the current CPU frequency in kHz |
| 370 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 371 | |
| 372 | // check if either CPU number or frequency changed |
| 373 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 374 | mCpuNum = cpuNum; |
| 375 | mCpukHz = cpukHz; |
| 376 | // ignore sample for purposes of cycles |
| 377 | valid = false; |
| 378 | } |
| 379 | |
| 380 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 381 | if (valid && mCpukHz > 0) { |
| 382 | double cycles = wcNs * cpukHz * 0.000001; |
| 383 | mHzStats.sample(cycles); |
| 384 | } |
| 385 | |
| 386 | unsigned n = mWcStats.n(); |
| 387 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 388 | if ((n & 127) == 1) { |
| 389 | long long elapsed = mCpuUsage.elapsed(); |
| 390 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 391 | double perLoop = elapsed / (double) n; |
| 392 | double perLoop100 = perLoop * 0.01; |
| 393 | double perLoop1k = perLoop * 0.001; |
| 394 | double mean = mWcStats.mean(); |
| 395 | double stddev = mWcStats.stddev(); |
| 396 | double minimum = mWcStats.minimum(); |
| 397 | double maximum = mWcStats.maximum(); |
| 398 | double meanCycles = mHzStats.mean(); |
| 399 | double stddevCycles = mHzStats.stddev(); |
| 400 | double minCycles = mHzStats.minimum(); |
| 401 | double maxCycles = mHzStats.maximum(); |
| 402 | mCpuUsage.resetElapsed(); |
| 403 | mWcStats.reset(); |
| 404 | mHzStats.reset(); |
| 405 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 406 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 407 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 408 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 409 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 410 | title.string(), |
| 411 | elapsed * .000000001, n, perLoop * .000001, |
| 412 | mean * .001, |
| 413 | stddev * .001, |
| 414 | minimum * .001, |
| 415 | maximum * .001, |
| 416 | mean / perLoop100, |
| 417 | stddev / perLoop100, |
| 418 | minimum / perLoop100, |
| 419 | maximum / perLoop100, |
| 420 | meanCycles / perLoop1k, |
| 421 | stddevCycles / perLoop1k, |
| 422 | minCycles / perLoop1k, |
| 423 | maxCycles / perLoop1k); |
| 424 | |
| 425 | } |
| 426 | } |
| 427 | #endif |
| 428 | }; |
| 429 | |
| 430 | // ---------------------------------------------------------------------------- |
| 431 | // ThreadBase |
| 432 | // ---------------------------------------------------------------------------- |
| 433 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 434 | // static |
| 435 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 436 | { |
| 437 | switch (type) { |
| 438 | case MIXER: |
| 439 | return "MIXER"; |
| 440 | case DIRECT: |
| 441 | return "DIRECT"; |
| 442 | case DUPLICATING: |
| 443 | return "DUPLICATING"; |
| 444 | case RECORD: |
| 445 | return "RECORD"; |
| 446 | case OFFLOAD: |
| 447 | return "OFFLOAD"; |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 448 | case MMAP: |
| 449 | return "MMAP"; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 450 | default: |
| 451 | return "unknown"; |
| 452 | } |
| 453 | } |
| 454 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 455 | std::string devicesToString(audio_devices_t devices) |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 456 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 457 | std::string result; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 458 | if (devices & AUDIO_DEVICE_BIT_IN) { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 459 | InputDeviceConverter::maskToString(devices, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 460 | } else { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 461 | OutputDeviceConverter::maskToString(devices, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 462 | } |
| 463 | return result; |
| 464 | } |
| 465 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 466 | std::string inputFlagsToString(audio_input_flags_t flags) |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 467 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 468 | std::string result; |
| 469 | InputFlagConverter::maskToString(flags, result); |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 470 | return result; |
| 471 | } |
| 472 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 473 | std::string outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 474 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 475 | std::string result; |
| 476 | OutputFlagConverter::maskToString(flags, result); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 477 | return result; |
| 478 | } |
| 479 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 480 | const char *sourceToString(audio_source_t source) |
| 481 | { |
| 482 | switch (source) { |
| 483 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 484 | case AUDIO_SOURCE_MIC: return "mic"; |
| 485 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 486 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 487 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 488 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 489 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 490 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 491 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 492 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 493 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 494 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 495 | default: return "unknown"; |
| 496 | } |
| 497 | } |
| 498 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 499 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 500 | 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] | 501 | : Thread(false /*canCallJava*/), |
| 502 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 503 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 504 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 505 | // are set by PlaybackThread::readOutputParameters_l() or |
| 506 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 507 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 508 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 509 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 510 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 511 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 512 | mDeathRecipient(new PMDeathRecipient(this)), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 513 | mSystemReady(systemReady), |
| 514 | mSignalPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 515 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 516 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | AudioFlinger::ThreadBase::~ThreadBase() |
| 520 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 521 | // 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] | 522 | mConfigEvents.clear(); |
| 523 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 524 | // do not lock the mutex in destructor |
| 525 | releaseWakeLock_l(); |
| 526 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 527 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 528 | binder->unlinkToDeath(mDeathRecipient); |
| 529 | } |
| 530 | } |
| 531 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 532 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 533 | { |
| 534 | status_t status = initCheck(); |
| 535 | if (status == NO_ERROR) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 536 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 537 | } else { |
| 538 | ALOGE("No working audio driver found."); |
| 539 | } |
| 540 | return status; |
| 541 | } |
| 542 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 543 | void AudioFlinger::ThreadBase::exit() |
| 544 | { |
| 545 | ALOGV("ThreadBase::exit"); |
| 546 | // do any cleanup required for exit to succeed |
| 547 | preExit(); |
| 548 | { |
| 549 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 550 | // if (!exitPending()) { |
| 551 | // // context switch from here to exit() |
| 552 | // // exit() calls requestExit(), what exitPending() observes |
| 553 | // // exit() calls signal(), which is dropped since no waiters |
| 554 | // // context switch back from exit() to here |
| 555 | // mWaitWorkCV.wait(...); |
| 556 | // // now thread is hung |
| 557 | // } |
| 558 | AutoMutex lock(mLock); |
| 559 | requestExit(); |
| 560 | mWaitWorkCV.broadcast(); |
| 561 | } |
| 562 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 563 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 564 | requestExitAndWait(); |
| 565 | } |
| 566 | |
| 567 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 568 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 569 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 570 | Mutex::Autolock _l(mLock); |
| 571 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 572 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 573 | } |
| 574 | |
| 575 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 576 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 577 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 578 | { |
| 579 | status_t status = NO_ERROR; |
| 580 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 581 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 582 | event->mWaitStatus = false; |
| 583 | mPendingConfigEvents.add(event); |
| 584 | return status; |
| 585 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 586 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 587 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 588 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 589 | mLock.unlock(); |
| 590 | { |
| 591 | Mutex::Autolock _l(event->mLock); |
| 592 | while (event->mWaitStatus) { |
| 593 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 594 | event->mStatus = TIMED_OUT; |
| 595 | event->mWaitStatus = false; |
| 596 | } |
| 597 | } |
| 598 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 599 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 600 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 601 | return status; |
| 602 | } |
| 603 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 604 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 605 | { |
| 606 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 607 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 611 | 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] | 612 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 613 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 614 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 617 | 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] | 618 | { |
| 619 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 620 | sendPrioConfigEvent_l(pid, tid, prio, forApp); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 623 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 624 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l( |
| 625 | pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 626 | { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 627 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 628 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 631 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 632 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 633 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 634 | sp<ConfigEvent> configEvent; |
| 635 | AudioParameter param(keyValuePair); |
| 636 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 637 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 638 | setMasterMono_l(value != 0); |
| 639 | if (param.size() == 1) { |
| 640 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 641 | } |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 642 | param.remove(String8(AudioParameter::keyMonoOutput)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 643 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 644 | } else { |
| 645 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 646 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 647 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 650 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 651 | const struct audio_patch *patch, |
| 652 | audio_patch_handle_t *handle) |
| 653 | { |
| 654 | Mutex::Autolock _l(mLock); |
| 655 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 656 | status_t status = sendConfigEvent_l(configEvent); |
| 657 | if (status == NO_ERROR) { |
| 658 | CreateAudioPatchConfigEventData *data = |
| 659 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 660 | *handle = data->mHandle; |
| 661 | } |
| 662 | return status; |
| 663 | } |
| 664 | |
| 665 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 666 | const audio_patch_handle_t handle) |
| 667 | { |
| 668 | Mutex::Autolock _l(mLock); |
| 669 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 670 | return sendConfigEvent_l(configEvent); |
| 671 | } |
| 672 | |
| 673 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 674 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 675 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 676 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 677 | bool configChanged = false; |
| 678 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 679 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 680 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 681 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 682 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 683 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 684 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 685 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 686 | // FIXME Need to understand why this has to be done asynchronously |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 687 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 688 | true /*asynchronous*/); |
| 689 | if (err != 0) { |
| 690 | 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] | 691 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 692 | } |
| 693 | } break; |
| 694 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 695 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 696 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 697 | } break; |
| 698 | case CFG_EVENT_SET_PARAMETER: { |
| 699 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 700 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 701 | configChanged = true; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 702 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", |
| 703 | data->mKeyValuePairs.string()); |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 704 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 705 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 706 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 707 | const audio_devices_t oldDevice = getDevice(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 708 | CreateAudioPatchConfigEventData *data = |
| 709 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 710 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 711 | const audio_devices_t newDevice = getDevice(); |
| 712 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", |
| 713 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), |
| 714 | (unsigned)newDevice, devicesToString(newDevice).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 715 | } break; |
| 716 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 717 | const audio_devices_t oldDevice = getDevice(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 718 | ReleaseAudioPatchConfigEventData *data = |
| 719 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 720 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 721 | const audio_devices_t newDevice = getDevice(); |
| 722 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", |
| 723 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), |
| 724 | (unsigned)newDevice, devicesToString(newDevice).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 725 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 726 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 727 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 728 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 729 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 730 | { |
| 731 | Mutex::Autolock _l(event->mLock); |
| 732 | if (event->mWaitStatus) { |
| 733 | event->mWaitStatus = false; |
| 734 | event->mCond.signal(); |
| 735 | } |
| 736 | } |
| 737 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 738 | } |
| 739 | |
| 740 | if (configChanged) { |
| 741 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 742 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 745 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 746 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 747 | const audio_channel_representation_t representation = |
| 748 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 749 | |
| 750 | switch (representation) { |
| 751 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 752 | if (output) { |
| 753 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 754 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 755 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 756 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 757 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 758 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 759 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 760 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 761 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 762 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 763 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 764 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 765 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 766 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 767 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 768 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 769 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 770 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 771 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 772 | } else { |
| 773 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 774 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 775 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 776 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 777 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 778 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 779 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 780 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 781 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 782 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 783 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 784 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 785 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 786 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 787 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 788 | } |
| 789 | const int len = s.length(); |
| 790 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 791 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 792 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 793 | } |
| 794 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 795 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 796 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 797 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 798 | return s; |
| 799 | default: |
| 800 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 801 | representation, audio_channel_mask_get_bits(mask)); |
| 802 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 803 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 804 | } |
| 805 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 806 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 807 | { |
| 808 | const size_t SIZE = 256; |
| 809 | char buffer[SIZE]; |
| 810 | String8 result; |
| 811 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 812 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", |
| 813 | this, mThreadName, getTid(), type(), threadTypeToString(type())); |
| 814 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 815 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 816 | if (!locked) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 817 | dprintf(fd, " Thread may be deadlocked\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 820 | dprintf(fd, " I/O handle: %d\n", mId); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 821 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 822 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 823 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 824 | 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] | 825 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 826 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 827 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 828 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 829 | 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] | 830 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 831 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 832 | size_t numConfig = mConfigEvents.size(); |
| 833 | if (numConfig) { |
| 834 | for (size_t i = 0; i < numConfig; i++) { |
| 835 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 836 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 837 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 838 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 839 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 840 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 841 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 842 | // 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] | 843 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).c_str()); |
| 844 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).c_str()); |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 845 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 846 | |
| 847 | if (locked) { |
| 848 | mLock.unlock(); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 853 | { |
| 854 | const size_t SIZE = 256; |
| 855 | char buffer[SIZE]; |
| 856 | String8 result; |
| 857 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 858 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 859 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 860 | write(fd, buffer, strlen(buffer)); |
| 861 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 862 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 863 | sp<EffectChain> chain = mEffectChains[i]; |
| 864 | if (chain != 0) { |
| 865 | chain->dump(fd, args); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 870 | void AudioFlinger::ThreadBase::acquireWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 871 | { |
| 872 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 873 | acquireWakeLock_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 876 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 877 | { |
| 878 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 879 | case MIXER: |
| 880 | return String16("AudioMix"); |
| 881 | case DIRECT: |
| 882 | return String16("AudioDirectOut"); |
| 883 | case DUPLICATING: |
| 884 | return String16("AudioDup"); |
| 885 | case RECORD: |
| 886 | return String16("AudioIn"); |
| 887 | case OFFLOAD: |
| 888 | return String16("AudioOffload"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 889 | case MMAP: |
| 890 | return String16("Mmap"); |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 891 | default: |
| 892 | ALOG_ASSERT(false); |
| 893 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 897 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 898 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 899 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 900 | if (mPowerManager != 0) { |
| 901 | sp<IBinder> binder = new BBinder(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 902 | // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids. |
| 903 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 904 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 905 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 906 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 907 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 908 | if (status == NO_ERROR) { |
| 909 | mWakeLockToken = binder; |
| 910 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 911 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 912 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 913 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 914 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 915 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 916 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 920 | { |
| 921 | Mutex::Autolock _l(mLock); |
| 922 | releaseWakeLock_l(); |
| 923 | } |
| 924 | |
| 925 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 926 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 927 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 928 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 929 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 930 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 931 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 932 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 933 | } |
| 934 | mWakeLockToken.clear(); |
| 935 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 939 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 940 | // use checkService() to avoid blocking if power service is not up yet |
| 941 | sp<IBinder> binder = |
| 942 | defaultServiceManager()->checkService(String16("power")); |
| 943 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 944 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 945 | } else { |
| 946 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 947 | binder->linkToDeath(mDeathRecipient); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 952 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 953 | getPowerManager_l(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 954 | |
| 955 | #if !LOG_NDEBUG |
| 956 | std::stringstream s; |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 957 | for (uid_t uid : uids) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 958 | s << uid << " "; |
| 959 | } |
| 960 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); |
| 961 | #endif |
| 962 | |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 963 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 964 | if (mSystemReady) { |
| 965 | ALOGE("no wake lock to update, but system ready!"); |
| 966 | } else { |
| 967 | ALOGW("no wake lock to update, system not ready yet"); |
| 968 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 969 | return; |
| 970 | } |
| 971 | if (mPowerManager != 0) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 972 | std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints |
| 973 | status_t status = mPowerManager->updateWakeLockUids( |
| 974 | mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(), |
| 975 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 976 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 980 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 981 | { |
| 982 | Mutex::Autolock _l(mLock); |
| 983 | releaseWakeLock_l(); |
| 984 | mPowerManager.clear(); |
| 985 | } |
| 986 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 987 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 988 | { |
| 989 | sp<ThreadBase> thread = mThread.promote(); |
| 990 | if (thread != 0) { |
| 991 | thread->clearPowerManager(); |
| 992 | } |
| 993 | ALOGW("power manager service died !!!"); |
| 994 | } |
| 995 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 996 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 997 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 998 | { |
| 999 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1000 | if (chain != 0) { |
| 1001 | if (type != NULL) { |
| 1002 | chain->setEffectSuspended_l(type, suspend); |
| 1003 | } else { |
| 1004 | chain->setEffectSuspendedAll_l(suspend); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1009 | } |
| 1010 | |
| 1011 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1012 | { |
| 1013 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1014 | if (index < 0) { |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1019 | mSuspendedSessions.valueAt(index); |
| 1020 | |
| 1021 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1022 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1023 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1024 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1025 | chain->setEffectSuspendedAll_l(true); |
| 1026 | } else { |
| 1027 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1028 | desc->mType.timeLow); |
| 1029 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1036 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1037 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1038 | { |
| 1039 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1040 | |
| 1041 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1042 | |
| 1043 | if (suspend) { |
| 1044 | if (index >= 0) { |
| 1045 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1046 | } else { |
| 1047 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1048 | } |
| 1049 | } else { |
| 1050 | if (index < 0) { |
| 1051 | return; |
| 1052 | } |
| 1053 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | int key = EffectChain::kKeyForSuspendAll; |
| 1058 | if (type != NULL) { |
| 1059 | key = type->timeLow; |
| 1060 | } |
| 1061 | index = sessionEffects.indexOfKey(key); |
| 1062 | |
| 1063 | sp<SuspendedSessionDesc> desc; |
| 1064 | if (suspend) { |
| 1065 | if (index >= 0) { |
| 1066 | desc = sessionEffects.valueAt(index); |
| 1067 | } else { |
| 1068 | desc = new SuspendedSessionDesc(); |
| 1069 | if (type != NULL) { |
| 1070 | desc->mType = *type; |
| 1071 | } |
| 1072 | sessionEffects.add(key, desc); |
| 1073 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1074 | } |
| 1075 | desc->mRefCount++; |
| 1076 | } else { |
| 1077 | if (index < 0) { |
| 1078 | return; |
| 1079 | } |
| 1080 | desc = sessionEffects.valueAt(index); |
| 1081 | if (--desc->mRefCount == 0) { |
| 1082 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1083 | sessionEffects.removeItemsAt(index); |
| 1084 | if (sessionEffects.isEmpty()) { |
| 1085 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1086 | sessionId); |
| 1087 | mSuspendedSessions.removeItem(sessionId); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | if (!sessionEffects.isEmpty()) { |
| 1092 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1097 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1098 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1099 | { |
| 1100 | Mutex::Autolock _l(mLock); |
| 1101 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1102 | } |
| 1103 | |
| 1104 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1105 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1106 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1107 | { |
| 1108 | if (mType != RECORD) { |
| 1109 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1110 | // another session. This gives the priority to well behaved effect control panels |
| 1111 | // and applications not using global effects. |
| 1112 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1113 | // global effects |
| 1114 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1115 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1120 | if (chain != 0) { |
| 1121 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1125 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1126 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( |
| 1127 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1128 | { |
| 1129 | // No global effect sessions on record threads |
| 1130 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1131 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1132 | desc->name, mThreadName); |
| 1133 | return BAD_VALUE; |
| 1134 | } |
| 1135 | // only pre processing effects on record thread |
| 1136 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1137 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1138 | desc->name, mThreadName); |
| 1139 | return BAD_VALUE; |
| 1140 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1141 | |
| 1142 | // always allow effects without processing load or latency |
| 1143 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1144 | return NO_ERROR; |
| 1145 | } |
| 1146 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1147 | audio_input_flags_t flags = mInput->flags; |
| 1148 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1149 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1150 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1151 | desc->name, mThreadName); |
| 1152 | return BAD_VALUE; |
| 1153 | } |
| 1154 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1155 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1156 | desc->name, mThreadName); |
| 1157 | return BAD_VALUE; |
| 1158 | } |
| 1159 | } |
| 1160 | return NO_ERROR; |
| 1161 | } |
| 1162 | |
| 1163 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1164 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( |
| 1165 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1166 | { |
| 1167 | // no preprocessing on playback threads |
| 1168 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1169 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" |
| 1170 | " thread %s", desc->name, mThreadName); |
| 1171 | return BAD_VALUE; |
| 1172 | } |
| 1173 | |
Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1174 | // always allow effects without processing load or latency |
| 1175 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1176 | return NO_ERROR; |
| 1177 | } |
| 1178 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1179 | switch (mType) { |
| 1180 | case MIXER: { |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1181 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1182 | // Reject any effect on mixer multichannel sinks. |
| 1183 | // TODO: fix both format and multichannel issues with effects. |
| 1184 | if (mChannelCount != FCC_2) { |
| 1185 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" |
| 1186 | " thread %s", desc->name, mChannelCount, mThreadName); |
| 1187 | return BAD_VALUE; |
| 1188 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1189 | #endif |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1190 | audio_output_flags_t flags = mOutput->flags; |
| 1191 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1192 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1193 | // global effects are applied only to non fast tracks if they are SW |
| 1194 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1195 | break; |
| 1196 | } |
| 1197 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1198 | // only post processing on output stage session |
| 1199 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1200 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1201 | " on output stage session", desc->name); |
| 1202 | return BAD_VALUE; |
| 1203 | } |
| 1204 | } else { |
| 1205 | // no restriction on effects applied on non fast tracks |
| 1206 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1210 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1211 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
| 1212 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", |
| 1213 | desc->name); |
| 1214 | return BAD_VALUE; |
| 1215 | } |
| 1216 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1217 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" |
| 1218 | " in fast mode", desc->name); |
| 1219 | return BAD_VALUE; |
| 1220 | } |
| 1221 | } |
| 1222 | } break; |
| 1223 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1224 | // nothing actionable on offload threads, if the effect: |
| 1225 | // - is offloadable: the effect can be created |
| 1226 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1227 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1228 | break; |
| 1229 | case DIRECT: |
| 1230 | // Reject any effect on Direct output threads for now, since the format of |
| 1231 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1232 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", |
| 1233 | desc->name, mThreadName); |
| 1234 | return BAD_VALUE; |
| 1235 | case DUPLICATING: |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1236 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1237 | // Reject any effect on mixer multichannel sinks. |
| 1238 | // TODO: fix both format and multichannel issues with effects. |
| 1239 | if (mChannelCount != FCC_2) { |
| 1240 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" |
| 1241 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); |
| 1242 | return BAD_VALUE; |
| 1243 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1244 | #endif |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1245 | if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) { |
| 1246 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" |
| 1247 | " thread %s", desc->name, mThreadName); |
| 1248 | return BAD_VALUE; |
| 1249 | } |
| 1250 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 1251 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" |
| 1252 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1253 | return BAD_VALUE; |
| 1254 | } |
| 1255 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1256 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" |
| 1257 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1258 | return BAD_VALUE; |
| 1259 | } |
| 1260 | break; |
| 1261 | default: |
| 1262 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1263 | } |
| 1264 | |
| 1265 | return NO_ERROR; |
| 1266 | } |
| 1267 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1268 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1269 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1270 | const sp<AudioFlinger::Client>& client, |
| 1271 | const sp<IEffectClient>& effectClient, |
| 1272 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1273 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1274 | effect_descriptor_t *desc, |
| 1275 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1276 | status_t *status, |
| 1277 | bool pinned) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1278 | { |
| 1279 | sp<EffectModule> effect; |
| 1280 | sp<EffectHandle> handle; |
| 1281 | status_t lStatus; |
| 1282 | sp<EffectChain> chain; |
| 1283 | bool chainCreated = false; |
| 1284 | bool effectCreated = false; |
| 1285 | bool effectRegistered = false; |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1286 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1287 | |
| 1288 | lStatus = initCheck(); |
| 1289 | if (lStatus != NO_ERROR) { |
| 1290 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1291 | goto Exit; |
| 1292 | } |
| 1293 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1294 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1295 | |
| 1296 | { // scope for mLock |
| 1297 | Mutex::Autolock _l(mLock); |
| 1298 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1299 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
| 1300 | if (lStatus != NO_ERROR) { |
| 1301 | goto Exit; |
| 1302 | } |
| 1303 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1304 | // check for existing effect chain with the requested audio session |
| 1305 | chain = getEffectChain_l(sessionId); |
| 1306 | if (chain == 0) { |
| 1307 | // create a new chain for this session |
| 1308 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1309 | chain = new EffectChain(this, sessionId); |
| 1310 | addEffectChain_l(chain); |
| 1311 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1312 | chainCreated = true; |
| 1313 | } else { |
| 1314 | effect = chain->getEffectFromDesc_l(desc); |
| 1315 | } |
| 1316 | |
| 1317 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1318 | |
| 1319 | if (effect == 0) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1320 | effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1321 | // Check CPU and memory usage |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1322 | lStatus = AudioSystem::registerEffect( |
| 1323 | desc, mId, chain->strategy(), sessionId, effectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 | if (lStatus != NO_ERROR) { |
| 1325 | goto Exit; |
| 1326 | } |
| 1327 | effectRegistered = true; |
| 1328 | // create a new effect module if none present in the chain |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1329 | lStatus = chain->createEffect_l(effect, this, desc, effectId, sessionId, pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1330 | if (lStatus != NO_ERROR) { |
| 1331 | goto Exit; |
| 1332 | } |
| 1333 | effectCreated = true; |
| 1334 | |
| 1335 | effect->setDevice(mOutDevice); |
| 1336 | effect->setDevice(mInDevice); |
| 1337 | effect->setMode(mAudioFlinger->getMode()); |
| 1338 | effect->setAudioSource(mAudioSource); |
| 1339 | } |
| 1340 | // create effect handle and connect it to effect module |
| 1341 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1342 | lStatus = handle->initCheck(); |
| 1343 | if (lStatus == OK) { |
| 1344 | lStatus = effect->addHandle(handle.get()); |
| 1345 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1346 | if (enabled != NULL) { |
| 1347 | *enabled = (int)effect->isEnabled(); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | Exit: |
| 1352 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1353 | Mutex::Autolock _l(mLock); |
| 1354 | if (effectCreated) { |
| 1355 | chain->removeEffect_l(effect); |
| 1356 | } |
| 1357 | if (effectRegistered) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1358 | AudioSystem::unregisterEffect(effectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1359 | } |
| 1360 | if (chainCreated) { |
| 1361 | removeEffectChain_l(chain); |
| 1362 | } |
| 1363 | handle.clear(); |
| 1364 | } |
| 1365 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1366 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1367 | return handle; |
| 1368 | } |
| 1369 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1370 | void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle, |
| 1371 | bool unpinIfLast) |
| 1372 | { |
| 1373 | bool remove = false; |
| 1374 | sp<EffectModule> effect; |
| 1375 | { |
| 1376 | Mutex::Autolock _l(mLock); |
| 1377 | |
| 1378 | effect = handle->effect().promote(); |
| 1379 | if (effect == 0) { |
| 1380 | return; |
| 1381 | } |
| 1382 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 1383 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 1384 | if (remove) { |
| 1385 | removeEffect_l(effect, true); |
| 1386 | } |
| 1387 | } |
| 1388 | if (remove) { |
| 1389 | mAudioFlinger->updateOrphanEffectChains(effect); |
| 1390 | AudioSystem::unregisterEffect(effect->id()); |
| 1391 | if (handle->enabled()) { |
| 1392 | checkSuspendOnEffectEnabled(effect, false, effect->sessionId()); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1397 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1398 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1399 | { |
| 1400 | Mutex::Autolock _l(mLock); |
| 1401 | return getEffect_l(sessionId, effectId); |
| 1402 | } |
| 1403 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1404 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1405 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1406 | { |
| 1407 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1408 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1409 | } |
| 1410 | |
| 1411 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1412 | // PlaybackThread::mLock held |
| 1413 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1414 | { |
| 1415 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1416 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1417 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1418 | bool chainCreated = false; |
| 1419 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1420 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1421 | "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] | 1422 | this, effect->desc().name, effect->desc().flags); |
| 1423 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1424 | if (chain == 0) { |
| 1425 | // create a new chain for this session |
| 1426 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1427 | chain = new EffectChain(this, sessionId); |
| 1428 | addEffectChain_l(chain); |
| 1429 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1430 | chainCreated = true; |
| 1431 | } |
| 1432 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1433 | |
| 1434 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1435 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1436 | this, effect->desc().name, chain.get()); |
| 1437 | return BAD_VALUE; |
| 1438 | } |
| 1439 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1440 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1441 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1442 | status_t status = chain->addEffect_l(effect); |
| 1443 | if (status != NO_ERROR) { |
| 1444 | if (chainCreated) { |
| 1445 | removeEffectChain_l(chain); |
| 1446 | } |
| 1447 | return status; |
| 1448 | } |
| 1449 | |
| 1450 | effect->setDevice(mOutDevice); |
| 1451 | effect->setDevice(mInDevice); |
| 1452 | effect->setMode(mAudioFlinger->getMode()); |
| 1453 | effect->setAudioSource(mAudioSource); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1454 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1455 | return NO_ERROR; |
| 1456 | } |
| 1457 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1458 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1459 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1460 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | effect_descriptor_t desc = effect->desc(); |
| 1462 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1463 | detachAuxEffect_l(effect->id()); |
| 1464 | } |
| 1465 | |
| 1466 | sp<EffectChain> chain = effect->chain().promote(); |
| 1467 | if (chain != 0) { |
| 1468 | // remove effect chain if removing last effect |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1469 | if (chain->removeEffect_l(effect, release) == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1470 | removeEffectChain_l(chain); |
| 1471 | } |
| 1472 | } else { |
| 1473 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1478 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1479 | { |
| 1480 | effectChains = mEffectChains; |
| 1481 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1482 | mEffectChains[i]->lock(); |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1487 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1488 | { |
| 1489 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1490 | effectChains[i]->unlock(); |
| 1491 | } |
| 1492 | } |
| 1493 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1494 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1495 | { |
| 1496 | Mutex::Autolock _l(mLock); |
| 1497 | return getEffectChain_l(sessionId); |
| 1498 | } |
| 1499 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1500 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1501 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1502 | { |
| 1503 | size_t size = mEffectChains.size(); |
| 1504 | for (size_t i = 0; i < size; i++) { |
| 1505 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1506 | return mEffectChains[i]; |
| 1507 | } |
| 1508 | } |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1513 | { |
| 1514 | Mutex::Autolock _l(mLock); |
| 1515 | size_t size = mEffectChains.size(); |
| 1516 | for (size_t i = 0; i < size; i++) { |
| 1517 | mEffectChains[i]->setMode_l(mode); |
| 1518 | } |
| 1519 | } |
| 1520 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1521 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1522 | { |
| 1523 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1524 | config->ext.mix.handle = mId; |
| 1525 | config->sample_rate = mSampleRate; |
| 1526 | config->format = mFormat; |
| 1527 | config->channel_mask = mChannelMask; |
| 1528 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1529 | AUDIO_PORT_CONFIG_FORMAT; |
| 1530 | } |
| 1531 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1532 | void AudioFlinger::ThreadBase::systemReady() |
| 1533 | { |
| 1534 | Mutex::Autolock _l(mLock); |
| 1535 | if (mSystemReady) { |
| 1536 | return; |
| 1537 | } |
| 1538 | mSystemReady = true; |
| 1539 | |
| 1540 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1541 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1542 | } |
| 1543 | mPendingConfigEvents.clear(); |
| 1544 | } |
| 1545 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1546 | template <typename T> |
| 1547 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) { |
| 1548 | ssize_t index = mActiveTracks.indexOf(track); |
| 1549 | if (index >= 0) { |
| 1550 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); |
| 1551 | return index; |
| 1552 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1553 | logTrack("add", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1554 | mActiveTracksGeneration++; |
| 1555 | mLatestActiveTrack = track; |
| 1556 | ++mBatteryCounter[track->uid()].second; |
| 1557 | return mActiveTracks.add(track); |
| 1558 | } |
| 1559 | |
| 1560 | template <typename T> |
| 1561 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) { |
| 1562 | ssize_t index = mActiveTracks.remove(track); |
| 1563 | if (index < 0) { |
| 1564 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); |
| 1565 | return index; |
| 1566 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1567 | logTrack("remove", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1568 | mActiveTracksGeneration++; |
| 1569 | --mBatteryCounter[track->uid()].second; |
| 1570 | // mLatestActiveTrack is not cleared even if is the same as track. |
| 1571 | return index; |
| 1572 | } |
| 1573 | |
| 1574 | template <typename T> |
| 1575 | void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() { |
| 1576 | for (const sp<T> &track : mActiveTracks) { |
| 1577 | BatteryNotifier::getInstance().noteStopAudio(track->uid()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1578 | logTrack("clear", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1579 | } |
| 1580 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1581 | mActiveTracks.clear(); |
| 1582 | mLatestActiveTrack.clear(); |
| 1583 | mBatteryCounter.clear(); |
| 1584 | } |
| 1585 | |
| 1586 | template <typename T> |
| 1587 | void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState( |
| 1588 | sp<ThreadBase> thread, bool force) { |
| 1589 | // Updates ActiveTracks client uids to the thread wakelock. |
| 1590 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { |
| 1591 | thread->updateWakeLockUids_l(getWakeLockUids()); |
| 1592 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1593 | } |
| 1594 | |
| 1595 | // Updates BatteryNotifier uids |
| 1596 | for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) { |
| 1597 | const uid_t uid = it->first; |
| 1598 | ssize_t &previous = it->second.first; |
| 1599 | ssize_t ¤t = it->second.second; |
| 1600 | if (current > 0) { |
| 1601 | if (previous == 0) { |
| 1602 | BatteryNotifier::getInstance().noteStartAudio(uid); |
| 1603 | } |
| 1604 | previous = current; |
| 1605 | ++it; |
| 1606 | } else if (current == 0) { |
| 1607 | if (previous > 0) { |
| 1608 | BatteryNotifier::getInstance().noteStopAudio(uid); |
| 1609 | } |
| 1610 | it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase. |
| 1611 | } else /* (current < 0) */ { |
| 1612 | LOG_ALWAYS_FATAL("negative battery count %zd", current); |
| 1613 | } |
| 1614 | } |
| 1615 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1616 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1617 | template <typename T> |
| 1618 | void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack( |
| 1619 | const char *funcName, const sp<T> &track) const { |
| 1620 | if (mLocalLog != nullptr) { |
| 1621 | String8 result; |
| 1622 | track->appendDump(result, false /* active */); |
| 1623 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string()); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1627 | void AudioFlinger::ThreadBase::broadcast_l() |
| 1628 | { |
| 1629 | // Thread could be blocked waiting for async |
| 1630 | // so signal it to handle state changes immediately |
| 1631 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1632 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1633 | mSignalPending = true; |
| 1634 | mWaitWorkCV.broadcast(); |
| 1635 | } |
| 1636 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1637 | // ---------------------------------------------------------------------------- |
| 1638 | // Playback |
| 1639 | // ---------------------------------------------------------------------------- |
| 1640 | |
| 1641 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1642 | AudioStreamOut* output, |
| 1643 | audio_io_handle_t id, |
| 1644 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1645 | type_t type, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1646 | bool systemReady) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1647 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1648 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1649 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1650 | mMixerBuffer(NULL), |
| 1651 | mMixerBufferSize(0), |
| 1652 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1653 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1654 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1655 | mEffectBuffer(NULL), |
| 1656 | mEffectBufferSize(0), |
| 1657 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1658 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1659 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1660 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1661 | mSuspendedFrames(0), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1662 | mActiveTracks(&this->mLocalLog), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 | // mStreamTypes[] initialized in constructor body |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1664 | mTracks(type == MIXER), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1665 | mOutput(output), |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1666 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1667 | mMixerStatus(MIXER_IDLE), |
| 1668 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1669 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1670 | mBytesRemaining(0), |
| 1671 | mCurrentWriteLength(0), |
| 1672 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1673 | mWriteAckSequence(0), |
| 1674 | mDrainSequence(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1675 | mScreenState(AudioFlinger::mScreenState), |
| 1676 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1677 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 1678 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
| 1679 | mLeftVolFloat(-1.0), mRightVolFloat(-1.0) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1680 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1681 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1682 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1683 | |
| 1684 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1685 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1686 | // parameter. |
| 1687 | // |
| 1688 | // If the HAL we are using has support for master volume or master mute, |
| 1689 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1690 | // and the mute set to false). |
| 1691 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1692 | mMasterMute = audioFlinger->masterMute_l(); |
| 1693 | if (mOutput && mOutput->audioHwDev) { |
| 1694 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1695 | mMasterVolume = 1.0; |
| 1696 | } |
| 1697 | |
| 1698 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1699 | mMasterMute = false; |
| 1700 | } |
| 1701 | } |
| 1702 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1703 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1704 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1705 | // ++ operator does not compile |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1706 | 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] | 1707 | stream = (audio_stream_type_t) (stream + 1)) { |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1708 | mStreamTypes[stream].volume = 0.0f; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1709 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1710 | } |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1711 | // Audio patch volume is always max |
| 1712 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 1713 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1717 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1718 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1719 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1720 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1721 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1725 | { |
| 1726 | dumpInternals(fd, args); |
| 1727 | dumpTracks(fd, args); |
| 1728 | dumpEffectChains(fd, args); |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1729 | dprintf(fd, " Local log:\n"); |
| 1730 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1733 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1735 | String8 result; |
| 1736 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1737 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1738 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1739 | const stream_type_t *st = &mStreamTypes[i]; |
| 1740 | if (i > 0) { |
| 1741 | result.appendFormat(", "); |
| 1742 | } |
| 1743 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1744 | if (st->mute) { |
| 1745 | result.append("M"); |
| 1746 | } |
| 1747 | } |
| 1748 | result.append("\n"); |
| 1749 | write(fd, result.string(), result.length()); |
| 1750 | result.clear(); |
| 1751 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1752 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1753 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1754 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1755 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1756 | |
| 1757 | size_t numtracks = mTracks.size(); |
| 1758 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1759 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1760 | size_t numactiveseen = 0; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1761 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1762 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1763 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1764 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1765 | Track::appendDumpHeader(result); |
| 1766 | for (size_t i = 0; i < numtracks; ++i) { |
| 1767 | sp<Track> track = mTracks[i]; |
| 1768 | if (track != 0) { |
| 1769 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1770 | if (active) { |
| 1771 | numactiveseen++; |
| 1772 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1773 | result.append(prefix); |
| 1774 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | } else { |
| 1778 | result.append("\n"); |
| 1779 | } |
| 1780 | if (numactiveseen != numactive) { |
| 1781 | // some tracks in the active list were not in the tracks list |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1782 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1783 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1784 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1785 | Track::appendDumpHeader(result); |
| 1786 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1787 | sp<Track> track = mActiveTracks[i]; |
| 1788 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1789 | result.append(prefix); |
| 1790 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1799 | { |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1800 | dumpBase(fd, args); |
| 1801 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1802 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1803 | dprintf(fd, " Last write occurred (msecs): %llu\n", |
| 1804 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1805 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1806 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1807 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1808 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1809 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1810 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1811 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1812 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1813 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1814 | AudioStreamOut *output = mOutput; |
| 1815 | 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] | 1816 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", |
| 1817 | output, flags, outputFlagsToString(flags).c_str()); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 1818 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 1819 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 1820 | if (mPipeSink.get() != nullptr) { |
| 1821 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 1822 | } |
| 1823 | if (output != nullptr) { |
| 1824 | dprintf(fd, " Hal stream dump:\n"); |
| 1825 | (void)output->stream->dump(fd); |
| 1826 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1830 | |
| 1831 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1832 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1833 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | // ThreadBase virtuals |
| 1837 | void AudioFlinger::PlaybackThread::preExit() |
| 1838 | { |
| 1839 | ALOGV(" preExit()"); |
Mikhail Naganov | ad9c7e4 | 2018-03-05 12:25:58 -0800 | [diff] [blame] | 1840 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1841 | // converted to use audio HAL extensions required to support tunneling |
| 1842 | status_t result = mOutput->stream->setParameters(String8("exiting=1")); |
| 1843 | ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1847 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1848 | const sp<AudioFlinger::Client>& client, |
| 1849 | audio_stream_type_t streamType, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1850 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1851 | audio_format_t format, |
| 1852 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1853 | size_t *pFrameCount, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1854 | size_t *pNotificationFrameCount, |
| 1855 | uint32_t notificationsPerBuffer, |
| 1856 | float speed, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1857 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1858 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1859 | audio_output_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1860 | pid_t tid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1861 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1862 | status_t *status, |
| 1863 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1864 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1865 | size_t frameCount = *pFrameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1866 | size_t notificationFrameCount = *pNotificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | sp<Track> track; |
| 1868 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1869 | audio_output_flags_t outputFlags = mOutput->flags; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1870 | audio_output_flags_t requestedFlags = *flags; |
| 1871 | |
| 1872 | if (*pSampleRate == 0) { |
| 1873 | *pSampleRate = mSampleRate; |
| 1874 | } |
| 1875 | uint32_t sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1876 | |
| 1877 | // special case for FAST flag considered OK if fast mixer is present |
| 1878 | if (hasFastMixer()) { |
| 1879 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 1880 | } |
| 1881 | |
| 1882 | // Check if requested flags are compatible with output stream flags |
| 1883 | if ((*flags & outputFlags) != *flags) { |
| 1884 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 1885 | *flags, outputFlags); |
| 1886 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 1887 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1888 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1889 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1890 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1891 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1892 | // PCM data |
| 1893 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1894 | // TODO: extract as a data library function that checks that a computationally |
| 1895 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1896 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1897 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1898 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1899 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1900 | // hardware sample rate |
| 1901 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1902 | // normal mixer has an associated fast mixer |
| 1903 | hasFastMixer() && |
| 1904 | // there are sufficient fast track slots available |
| 1905 | (mFastTrackAvailMask != 0) |
| 1906 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1907 | // FIXME add a permission test also? |
| 1908 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1909 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 1910 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1911 | // read the fast track multiplier property the first time it is needed |
| 1912 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1913 | if (ok != 0) { |
| 1914 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1915 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1916 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1917 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1918 | |
| 1919 | // check compatibility with audio effects. |
| 1920 | { // scope for mLock |
| 1921 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 1922 | for (audio_session_t session : { |
| 1923 | AUDIO_SESSION_OUTPUT_STAGE, |
| 1924 | AUDIO_SESSION_OUTPUT_MIX, |
| 1925 | sessionId, |
| 1926 | }) { |
| 1927 | sp<EffectChain> chain = getEffectChain_l(session); |
| 1928 | if (chain.get() != nullptr) { |
| 1929 | audio_output_flags_t old = *flags; |
| 1930 | chain->checkOutputFlagCompatibility(flags); |
| 1931 | if (old != *flags) { |
| 1932 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 1933 | (int)session, (int)old, (int)*flags); |
| 1934 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1938 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1939 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 1940 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1941 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1942 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 1943 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1944 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1945 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1946 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1947 | audio_is_linear_pcm(format), |
| 1948 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1949 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1950 | } |
| 1951 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1952 | |
| 1953 | if (!audio_has_proportional_frames(format)) { |
| 1954 | if (sharedBuffer != 0) { |
| 1955 | // Same comment as below about ignoring frameCount parameter for set() |
| 1956 | frameCount = sharedBuffer->size(); |
| 1957 | } else if (frameCount == 0) { |
| 1958 | frameCount = mNormalFrameCount; |
| 1959 | } |
| 1960 | if (notificationFrameCount != frameCount) { |
| 1961 | notificationFrameCount = frameCount; |
| 1962 | } |
| 1963 | } else if (sharedBuffer != 0) { |
| 1964 | // FIXME: Ensure client side memory buffers need |
| 1965 | // not have additional alignment beyond sample |
| 1966 | // (e.g. 16 bit stereo accessed as 32 bit frame). |
| 1967 | size_t alignment = audio_bytes_per_sample(format); |
| 1968 | if (alignment & 1) { |
| 1969 | // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). |
| 1970 | alignment = 1; |
| 1971 | } |
| 1972 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1973 | size_t frameSize = channelCount * audio_bytes_per_sample(format); |
| 1974 | if (channelCount > 1) { |
| 1975 | // More than 2 channels does not require stronger alignment than stereo |
| 1976 | alignment <<= 1; |
| 1977 | } |
| 1978 | if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) { |
| 1979 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
| 1980 | sharedBuffer->pointer(), channelCount); |
| 1981 | lStatus = BAD_VALUE; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1982 | goto Exit; |
| 1983 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1984 | |
| 1985 | // When initializing a shared buffer AudioTrack via constructors, |
| 1986 | // there's no frameCount parameter. |
| 1987 | // But when initializing a shared buffer AudioTrack via set(), |
| 1988 | // there _is_ a frameCount parameter. We silently ignore it. |
| 1989 | frameCount = sharedBuffer->size() / frameSize; |
| 1990 | } else { |
| 1991 | size_t minFrameCount = 0; |
| 1992 | // For fast tracks we try to respect the application's request for notifications per buffer. |
| 1993 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 1994 | if (notificationsPerBuffer > 0) { |
| 1995 | // Avoid possible arithmetic overflow during multiplication. |
| 1996 | if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { |
| 1997 | ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", |
| 1998 | notificationsPerBuffer, mFrameCount); |
| 1999 | } else { |
| 2000 | minFrameCount = mFrameCount * notificationsPerBuffer; |
| 2001 | } |
| 2002 | } |
| 2003 | } else { |
| 2004 | // For normal PCM streaming tracks, update minimum frame count. |
| 2005 | // Buffer depth is forced to be at least 2 x the normal mixer frame count and |
| 2006 | // cover audio hardware latency. |
| 2007 | // This is probably too conservative, but legacy application code may depend on it. |
| 2008 | // If you change this calculation, also review the start threshold which is related. |
| 2009 | uint32_t latencyMs = latency_l(); |
| 2010 | if (latencyMs == 0) { |
| 2011 | ALOGE("Error when retrieving output stream latency"); |
| 2012 | lStatus = UNKNOWN_ERROR; |
| 2013 | goto Exit; |
| 2014 | } |
| 2015 | |
| 2016 | minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, |
| 2017 | mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); |
| 2018 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2019 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2020 | if (frameCount < minFrameCount) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2021 | frameCount = minFrameCount; |
| 2022 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2023 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2024 | |
| 2025 | // Make sure that application is notified with sufficient margin before underrun. |
| 2026 | // The client can divide the AudioTrack buffer into sub-buffers, |
| 2027 | // and expresses its desire to server as the notification frame count. |
| 2028 | if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { |
| 2029 | size_t maxNotificationFrames; |
| 2030 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2031 | // notify every HAL buffer, regardless of the size of the track buffer |
| 2032 | maxNotificationFrames = mFrameCount; |
| 2033 | } else { |
| 2034 | // For normal tracks, use at least double-buffering if no sample rate conversion, |
| 2035 | // or at least triple-buffering if there is sample rate conversion |
| 2036 | const int nBuffering = sampleRate == mSampleRate ? 2 : 3; |
| 2037 | maxNotificationFrames = frameCount / nBuffering; |
| 2038 | // If client requested a fast track but this was denied, then use the smaller maximum. |
| 2039 | if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2040 | size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; |
| 2041 | if (maxNotificationFrames > maxNotificationFramesFastDenied) { |
| 2042 | maxNotificationFrames = maxNotificationFramesFastDenied; |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 2047 | if (notificationFrameCount == 0) { |
| 2048 | ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", |
| 2049 | maxNotificationFrames, frameCount); |
| 2050 | } else { |
| 2051 | ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", |
| 2052 | notificationFrameCount, maxNotificationFrames, frameCount); |
| 2053 | } |
| 2054 | notificationFrameCount = maxNotificationFrames; |
| 2055 | } |
| 2056 | } |
| 2057 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2058 | *pFrameCount = frameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2059 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2060 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2061 | switch (mType) { |
| 2062 | |
| 2063 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2064 | 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] | 2065 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2066 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 2067 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2068 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2069 | lStatus = BAD_VALUE; |
| 2070 | goto Exit; |
| 2071 | } |
| 2072 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2073 | break; |
| 2074 | |
| 2075 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2076 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2077 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 2078 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2079 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2080 | lStatus = BAD_VALUE; |
| 2081 | goto Exit; |
| 2082 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2083 | break; |
| 2084 | |
| 2085 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2086 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2087 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 2088 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2089 | format, mOutput, mFormat); |
| 2090 | lStatus = BAD_VALUE; |
| 2091 | goto Exit; |
| 2092 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2093 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2094 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2095 | lStatus = BAD_VALUE; |
| 2096 | goto Exit; |
| 2097 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2098 | break; |
| 2099 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | lStatus = initCheck(); |
| 2103 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2104 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2105 | goto Exit; |
| 2106 | } |
| 2107 | |
| 2108 | { // scope for mLock |
| 2109 | Mutex::Autolock _l(mLock); |
| 2110 | |
| 2111 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2112 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2113 | // manager |
| 2114 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 2115 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2116 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2117 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2118 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 2119 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2120 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2121 | strategy, actual); |
| 2122 | lStatus = BAD_VALUE; |
| 2123 | goto Exit; |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2128 | track = new Track(this, client, streamType, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2129 | channelMask, frameCount, |
| 2130 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2131 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT, portId); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2132 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2133 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2134 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2135 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2136 | // 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] | 2137 | goto Exit; |
| 2138 | } |
| 2139 | mTracks.add(track); |
| 2140 | |
| 2141 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 2142 | if (chain != 0) { |
| 2143 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2144 | track->setMainBuffer(chain->inBuffer()); |
| 2145 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 2146 | chain->incTrackCnt(); |
| 2147 | } |
| 2148 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2149 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2150 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2151 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2152 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2153 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | lStatus = NO_ERROR; |
| 2158 | |
| 2159 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2160 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2161 | return track; |
| 2162 | } |
| 2163 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2164 | template<typename T> |
| 2165 | ssize_t AudioFlinger::PlaybackThread::Tracks<T>::add(const sp<T> &track) |
| 2166 | { |
| 2167 | const ssize_t index = mTracks.add(track); |
| 2168 | if (index >= 0) { |
| 2169 | // set name for track when adding. |
| 2170 | int name; |
| 2171 | if (mUnusedTrackNames.empty()) { |
| 2172 | name = mTracks.size() - 1; // new name {0 ... size-1}. |
| 2173 | } else { |
| 2174 | // reuse smallest name for deleted track. |
| 2175 | auto it = mUnusedTrackNames.begin(); |
| 2176 | name = *it; |
| 2177 | (void)mUnusedTrackNames.erase(it); |
| 2178 | } |
| 2179 | track->setName(name); |
| 2180 | } else { |
| 2181 | LOG_ALWAYS_FATAL("cannot add track"); |
| 2182 | } |
| 2183 | return index; |
| 2184 | } |
| 2185 | |
| 2186 | template<typename T> |
| 2187 | ssize_t AudioFlinger::PlaybackThread::Tracks<T>::remove(const sp<T> &track) |
| 2188 | { |
| 2189 | const int name = track->name(); |
| 2190 | const ssize_t index = mTracks.remove(track); |
| 2191 | if (index >= 0) { |
| 2192 | // invalidate name when removing from mTracks. |
| 2193 | LOG_ALWAYS_FATAL_IF(name < 0, "invalid name %d for track on mTracks", name); |
| 2194 | |
| 2195 | if (mSaveDeletedTrackNames) { |
| 2196 | // We can't directly access mAudioMixer since the caller may be outside of threadLoop. |
| 2197 | // Instead, we add to mDeletedTrackNames which is solely used for mAudioMixer update, |
| 2198 | // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer. |
| 2199 | mDeletedTrackNames.emplace(name); |
| 2200 | } |
| 2201 | |
| 2202 | mUnusedTrackNames.emplace(name); |
| 2203 | track->setName(T::TRACK_NAME_PENDING); |
| 2204 | } else { |
| 2205 | LOG_ALWAYS_FATAL_IF(name >= 0, |
| 2206 | "valid name %d for track not in mTracks (returned %zd)", name, index); |
| 2207 | } |
| 2208 | return index; |
| 2209 | } |
| 2210 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2211 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 2212 | { |
| 2213 | return latency; |
| 2214 | } |
| 2215 | |
| 2216 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 2217 | { |
| 2218 | Mutex::Autolock _l(mLock); |
| 2219 | return latency_l(); |
| 2220 | } |
| 2221 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 2222 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2223 | uint32_t latency; |
| 2224 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { |
| 2225 | return correctLatency_l(latency); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2226 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2227 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 2231 | { |
| 2232 | Mutex::Autolock _l(mLock); |
| 2233 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2234 | if (mOutput && mOutput->audioHwDev && |
| 2235 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2236 | mMasterVolume = 1.0; |
| 2237 | } else { |
| 2238 | mMasterVolume = value; |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 2243 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2244 | if (isDuplicating()) { |
| 2245 | return; |
| 2246 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2247 | Mutex::Autolock _l(mLock); |
| 2248 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2249 | if (mOutput && mOutput->audioHwDev && |
| 2250 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2251 | mMasterMute = false; |
| 2252 | } else { |
| 2253 | mMasterMute = muted; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2258 | { |
| 2259 | Mutex::Autolock _l(mLock); |
| 2260 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2261 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2265 | { |
| 2266 | Mutex::Autolock _l(mLock); |
| 2267 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2268 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2272 | { |
| 2273 | Mutex::Autolock _l(mLock); |
| 2274 | return mStreamTypes[stream].volume; |
| 2275 | } |
| 2276 | |
| 2277 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2278 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2279 | { |
| 2280 | status_t status = ALREADY_EXISTS; |
| 2281 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2282 | if (mActiveTracks.indexOf(track) < 0) { |
| 2283 | // the track is newly added, make sure it fills up all its |
| 2284 | // buffers before playing. This is to ensure the client will |
| 2285 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2286 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2287 | TrackBase::track_state state = track->mState; |
| 2288 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2289 | status = AudioSystem::startOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2290 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2291 | mLock.lock(); |
| 2292 | // abort track was stopped/paused while we released the lock |
| 2293 | if (state != track->mState) { |
| 2294 | if (status == NO_ERROR) { |
| 2295 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2296 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2297 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2298 | mLock.lock(); |
| 2299 | } |
| 2300 | return INVALID_OPERATION; |
| 2301 | } |
| 2302 | // abort if start is rejected by audio policy manager |
| 2303 | if (status != NO_ERROR) { |
| 2304 | return PERMISSION_DENIED; |
| 2305 | } |
| 2306 | #ifdef ADD_BATTERY_DATA |
| 2307 | // to track the speaker usage |
| 2308 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2309 | #endif |
| 2310 | } |
| 2311 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2312 | // set retry count for buffer fill |
| 2313 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2314 | if (track->isStopping_1()) { |
| 2315 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 2316 | } else { |
| 2317 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2318 | } |
| 2319 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2320 | } else { |
| 2321 | track->mRetryCount = kMaxTrackStartupRetries; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2322 | track->mFillingUpStatus = |
| 2323 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2324 | } |
| 2325 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2326 | track->mResetDone = false; |
| 2327 | track->mPresentationCompleteFrames = 0; |
| 2328 | mActiveTracks.add(track); |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2329 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2330 | if (chain != 0) { |
| 2331 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2332 | track->sessionId()); |
| 2333 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | status = NO_ERROR; |
| 2337 | } |
| 2338 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2339 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2340 | return status; |
| 2341 | } |
| 2342 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2343 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2344 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2345 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2346 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2347 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2348 | track->mState = TrackBase::STOPPED; |
| 2349 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2350 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2351 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2352 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2353 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2354 | |
| 2355 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2359 | { |
| 2360 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2361 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2362 | String8 result; |
| 2363 | track->appendDump(result, false /* active */); |
| 2364 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2365 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2366 | mTracks.remove(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2367 | if (track->isFastTrack()) { |
| 2368 | int index = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2369 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2370 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2371 | mFastTrackAvailMask |= 1 << index; |
| 2372 | // redundant as track is about to be destroyed, for dumpsys only |
| 2373 | track->mFastIndex = -1; |
| 2374 | } |
| 2375 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2376 | if (chain != 0) { |
| 2377 | chain->decTrackCnt(); |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2382 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2383 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2384 | String8 out_s8; |
| 2385 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { |
| 2386 | return out_s8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2387 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2388 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2391 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2392 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2393 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2394 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2395 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2396 | |
| 2397 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2398 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2399 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2400 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2401 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2402 | desc->mChannelMask = mChannelMask; |
| 2403 | desc->mSamplingRate = mSampleRate; |
| 2404 | desc->mFormat = mFormat; |
| 2405 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2406 | // AudioFlinger::frameCount(audio_io_handle_t) |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2407 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2408 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2409 | break; |
| 2410 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2411 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2412 | default: |
| 2413 | break; |
| 2414 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2415 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2416 | } |
| 2417 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2418 | void AudioFlinger::PlaybackThread::onWriteReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2419 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2420 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2423 | void AudioFlinger::PlaybackThread::onDrainReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2424 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2425 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2428 | void AudioFlinger::PlaybackThread::onError() |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2429 | { |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2430 | mCallbackThread->setAsyncError(); |
| 2431 | } |
| 2432 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2433 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2434 | { |
| 2435 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2436 | // reject out of sequence requests |
| 2437 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2438 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2439 | mWaitWorkCV.signal(); |
| 2440 | } |
| 2441 | } |
| 2442 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2443 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2444 | { |
| 2445 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2446 | // reject out of sequence requests |
| 2447 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2448 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2449 | mWaitWorkCV.signal(); |
| 2450 | } |
| 2451 | } |
| 2452 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2453 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2454 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2455 | // 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] | 2456 | mSampleRate = mOutput->getSampleRate(); |
| 2457 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2458 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2459 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2460 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2461 | if ((mType == MIXER || mType == DUPLICATING) |
| 2462 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2463 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2464 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2465 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2466 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2467 | |
| 2468 | // Get actual HAL format. |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2469 | status_t result = mOutput->stream->getFormat(&mHALFormat); |
| 2470 | 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] | 2471 | // Get format from the shim, which will be different than the HAL format |
| 2472 | // if playing compressed audio over HDMI passthrough. |
| 2473 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2474 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2475 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2476 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2477 | if ((mType == MIXER || mType == DUPLICATING) |
| 2478 | && !isValidPcmSinkFormat(mFormat)) { |
| 2479 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2480 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2481 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2482 | mFrameSize = mOutput->getFrameSize(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2483 | result = mOutput->stream->getBufferSize(&mBufferSize); |
| 2484 | LOG_ALWAYS_FATAL_IF(result != OK, |
| 2485 | "Error when retrieving output stream buffer size: %d", result); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2486 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2487 | if (mFrameCount & 15) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2488 | 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] | 2489 | mFrameCount); |
| 2490 | } |
| 2491 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2492 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) { |
| 2493 | if (mOutput->stream->setCallback(this) == OK) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2494 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2495 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2496 | } |
| 2497 | } |
| 2498 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2499 | mHwSupportsPause = false; |
| 2500 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2501 | bool supportsPause = false, supportsResume = false; |
| 2502 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { |
| 2503 | if (supportsPause && supportsResume) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2504 | mHwSupportsPause = true; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2505 | } else if (supportsPause) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2506 | ALOGW("direct output implements pause but not resume"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2507 | } else if (supportsResume) { |
| 2508 | ALOGW("direct output implements resume but not pause"); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2509 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2510 | } |
| 2511 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2512 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2513 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2514 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2515 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2516 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2517 | // For best precision, we use float instead of the associated output |
| 2518 | // device format (typically PCM 16 bit). |
| 2519 | |
| 2520 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2521 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2522 | mBufferSize = mFrameSize * mFrameCount; |
| 2523 | |
| 2524 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2525 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2526 | // (if a valid mask) to avoid premature downmix. |
| 2527 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2528 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2529 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2530 | } |
| 2531 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2532 | // 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] | 2533 | double multiplier = 1.0; |
| 2534 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2535 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2536 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2537 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2538 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2539 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2540 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2541 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2542 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2543 | maxNormalFrameCount = minNormalFrameCount; |
| 2544 | } |
| 2545 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2546 | if (multiplier <= 1.0) { |
| 2547 | multiplier = 1.0; |
| 2548 | } else if (multiplier <= 2.0) { |
| 2549 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2550 | multiplier = 2.0; |
| 2551 | } else { |
| 2552 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2553 | } |
| 2554 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2555 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | mNormalFrameCount = multiplier * mFrameCount; |
| 2559 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2560 | if (mType == MIXER || mType == DUPLICATING) { |
| 2561 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2562 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2563 | 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] | 2564 | mNormalFrameCount); |
| 2565 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2566 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2567 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2568 | mThreadThrottleTimeMs = 0; |
| 2569 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2570 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2571 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2572 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2573 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2574 | free(mSinkBuffer); |
| 2575 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2576 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2577 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2578 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2579 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2580 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2581 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2582 | // drives the output. |
| 2583 | free(mMixerBuffer); |
| 2584 | mMixerBuffer = NULL; |
| 2585 | if (mMixerBufferEnabled) { |
| 2586 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2587 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2588 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2589 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2590 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2591 | free(mEffectBuffer); |
| 2592 | mEffectBuffer = NULL; |
| 2593 | if (mEffectBufferEnabled) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2594 | mEffectBufferFormat = EFFECT_BUFFER_FORMAT; |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2595 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2596 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2597 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2598 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2599 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2600 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2601 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2602 | // 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] | 2603 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2604 | // matter. |
| 2605 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2606 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2607 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2608 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2613 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2614 | { |
| 2615 | if (halFrames == NULL || dspFrames == NULL) { |
| 2616 | return BAD_VALUE; |
| 2617 | } |
| 2618 | Mutex::Autolock _l(mLock); |
| 2619 | if (initCheck() != NO_ERROR) { |
| 2620 | return INVALID_OPERATION; |
| 2621 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2622 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2623 | *halFrames = framesWritten; |
| 2624 | |
| 2625 | if (isSuspended()) { |
| 2626 | // return an estimation of rendered frames when the output is suspended |
| 2627 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2628 | *dspFrames = (uint32_t) |
| 2629 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2630 | return NO_ERROR; |
| 2631 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2632 | status_t status; |
| 2633 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2634 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2635 | *dspFrames = (size_t)frames; |
| 2636 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2640 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 2641 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2642 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2643 | uint32_t result = 0; |
| 2644 | if (getEffectChain_l(sessionId) != 0) { |
| 2645 | result = EFFECT_SESSION; |
| 2646 | } |
| 2647 | |
| 2648 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2649 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2650 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2651 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2652 | if (track->isFastTrack()) { |
| 2653 | result |= FAST_SESSION; |
| 2654 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2655 | break; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | return result; |
| 2660 | } |
| 2661 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2662 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2663 | { |
| 2664 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2665 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2666 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2667 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2668 | } |
| 2669 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2670 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2671 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2672 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2673 | } |
| 2674 | } |
| 2675 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2676 | } |
| 2677 | |
| 2678 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2679 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2680 | { |
| 2681 | Mutex::Autolock _l(mLock); |
| 2682 | return mOutput; |
| 2683 | } |
| 2684 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2685 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2686 | { |
| 2687 | Mutex::Autolock _l(mLock); |
| 2688 | AudioStreamOut *output = mOutput; |
| 2689 | mOutput = NULL; |
| 2690 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2691 | // must push a NULL and wait for ack |
| 2692 | mOutputSink.clear(); |
| 2693 | mPipeSink.clear(); |
| 2694 | mNormalSink.clear(); |
| 2695 | return output; |
| 2696 | } |
| 2697 | |
| 2698 | // 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] | 2699 | sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2700 | { |
| 2701 | if (mOutput == NULL) { |
| 2702 | return NULL; |
| 2703 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2704 | return mOutput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2708 | { |
| 2709 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2710 | } |
| 2711 | |
| 2712 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2713 | { |
| 2714 | if (!isValidSyncEvent(event)) { |
| 2715 | return BAD_VALUE; |
| 2716 | } |
| 2717 | |
| 2718 | Mutex::Autolock _l(mLock); |
| 2719 | |
| 2720 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2721 | sp<Track> track = mTracks[i]; |
| 2722 | if (event->triggerSession() == track->sessionId()) { |
| 2723 | (void) track->setSyncEvent(event); |
| 2724 | return NO_ERROR; |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | return NAME_NOT_FOUND; |
| 2729 | } |
| 2730 | |
| 2731 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2732 | { |
| 2733 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2734 | } |
| 2735 | |
| 2736 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2737 | const Vector< sp<Track> >& tracksToRemove) |
| 2738 | { |
| 2739 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2740 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2741 | for (size_t i = 0 ; i < count ; i++) { |
| 2742 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2743 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2744 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2745 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2746 | #ifdef ADD_BATTERY_DATA |
| 2747 | // to track the speaker usage |
| 2748 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2749 | #endif |
| 2750 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2751 | AudioSystem::releaseOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2752 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2753 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2760 | { |
| 2761 | if (!mMasterMute) { |
| 2762 | char value[PROPERTY_VALUE_MAX]; |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2763 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 2764 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 2765 | return; |
| 2766 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2767 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2768 | char *endptr; |
| 2769 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2770 | if (*endptr == '\0' && ul != 0) { |
| 2771 | ALOGD("Silence is golden"); |
| 2772 | // The setprop command will not allow a property to be changed after |
| 2773 | // the first time it is set, so we don't have to worry about un-muting. |
| 2774 | setMasterMute_l(true); |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2781 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2782 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 2783 | LOG_HIST_TS(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2784 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2785 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2786 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2787 | |
| 2788 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2789 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2790 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2791 | const size_t count = mBytesRemaining / mFrameSize; |
| 2792 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2793 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2794 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2795 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2796 | if (screenState != mScreenState) { |
| 2797 | mScreenState = screenState; |
| 2798 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2799 | if (pipe != NULL) { |
| 2800 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2801 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2802 | } |
| 2803 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2804 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2805 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2806 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2807 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2808 | } else { |
| 2809 | bytesWritten = framesWritten; |
| 2810 | } |
| 2811 | // otherwise use the HAL / AudioStreamOut directly |
| 2812 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2813 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2814 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2815 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2816 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2817 | mWriteAckSequence += 2; |
| 2818 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2819 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2820 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2821 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2822 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2823 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2824 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2825 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2826 | if (mUseAsyncWrite && |
| 2827 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2828 | // 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] | 2829 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2830 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2831 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2832 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2835 | mNumWrites++; |
| 2836 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2837 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2838 | return bytesWritten; |
| 2839 | } |
| 2840 | |
| 2841 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2842 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2843 | bool supportsDrain = false; |
| 2844 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2845 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2846 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2847 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2848 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2849 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2850 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2851 | } |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 2852 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2853 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2858 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2859 | { |
| 2860 | Mutex::Autolock _l(mLock); |
| 2861 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2862 | sp<Track> track = mTracks[i]; |
| 2863 | track->invalidate(); |
| 2864 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2865 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. |
| 2866 | // After we exit there are no more track changes sent to BatteryNotifier |
| 2867 | // because that requires an active threadLoop. |
| 2868 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? |
| 2869 | mActiveTracks.clear(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2870 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | /* |
| 2874 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2875 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2876 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2877 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2878 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2879 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2880 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2881 | |
| 2882 | The parameters that affect these derived values are: |
| 2883 | - frame count |
| 2884 | - frame size |
| 2885 | - sample rate |
| 2886 | - device type: A2DP or not |
| 2887 | - device latency |
| 2888 | - format: PCM or not |
| 2889 | - active sleep time |
| 2890 | - idle sleep time |
| 2891 | */ |
| 2892 | |
| 2893 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2894 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2895 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2896 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2897 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2898 | |
| 2899 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2900 | // truncating audio when going to standby. |
| 2901 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2902 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2903 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2904 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2905 | } |
| 2906 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2907 | } |
| 2908 | |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2909 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2910 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2911 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2912 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2913 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2914 | size_t size = mTracks.size(); |
| 2915 | for (size_t i = 0; i < size; i++) { |
| 2916 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2917 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2918 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2919 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2920 | } |
| 2921 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2922 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2923 | } |
| 2924 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2925 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2926 | { |
| 2927 | Mutex::Autolock _l(mLock); |
| 2928 | invalidateTracks_l(streamType); |
| 2929 | } |
| 2930 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2931 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2932 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2933 | audio_session_t session = chain->sessionId(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2934 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 2935 | status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer( |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2936 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, |
| 2937 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, |
| 2938 | &halInBuffer); |
| 2939 | if (result != OK) return result; |
| 2940 | halOutBuffer = halInBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2941 | effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2942 | 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] | 2943 | if (session > AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2944 | // 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] | 2945 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2946 | if (mType != DIRECT) { |
| 2947 | size_t numSamples = mNormalFrameCount * mChannelCount; |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 2948 | status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer( |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2949 | numSamples * sizeof(effect_buffer_t), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2950 | &halInBuffer); |
| 2951 | if (result != OK) return result; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2952 | #ifdef FLOAT_EFFECT_CHAIN |
| 2953 | buffer = halInBuffer->audioBuffer()->f32; |
| 2954 | #else |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2955 | buffer = halInBuffer->audioBuffer()->s16; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2956 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2957 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 2958 | buffer, session); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | // Attach all tracks with same session ID to this chain. |
| 2962 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2963 | sp<Track> track = mTracks[i]; |
| 2964 | if (session == track->sessionId()) { |
| 2965 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2966 | buffer); |
| 2967 | track->setMainBuffer(buffer); |
| 2968 | chain->incTrackCnt(); |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | // indicate all active tracks in the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2973 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2974 | if (session == track->sessionId()) { |
| 2975 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2976 | chain->incActiveTrackCnt(); |
| 2977 | } |
| 2978 | } |
| 2979 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2980 | chain->setThread(this); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2981 | chain->setInBuffer(halInBuffer); |
| 2982 | chain->setOutBuffer(halOutBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2983 | // 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] | 2984 | // 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] | 2985 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2986 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2987 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2988 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2989 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2990 | // Effect chain for other sessions are inserted at beginning of effect |
| 2991 | // 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] | 2992 | // sessions is not important. |
| 2993 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
| 2994 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, |
| 2995 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2996 | size_t size = mEffectChains.size(); |
| 2997 | size_t i = 0; |
| 2998 | for (i = 0; i < size; i++) { |
| 2999 | if (mEffectChains[i]->sessionId() < session) { |
| 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | mEffectChains.insertAt(chain, i); |
| 3004 | checkSuspendOnAddEffectChain_l(chain); |
| 3005 | |
| 3006 | return NO_ERROR; |
| 3007 | } |
| 3008 | |
| 3009 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 3010 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3011 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3012 | |
| 3013 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 3014 | |
| 3015 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3016 | if (chain == mEffectChains[i]) { |
| 3017 | mEffectChains.removeAt(i); |
| 3018 | // detach all active tracks from the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3019 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3020 | if (session == track->sessionId()) { |
| 3021 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 3022 | chain.get(), session); |
| 3023 | chain->decActiveTrackCnt(); |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | // detach all tracks with same session ID from this chain |
| 3028 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3029 | sp<Track> track = mTracks[i]; |
| 3030 | if (session == track->sessionId()) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3031 | track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3032 | chain->decTrackCnt(); |
| 3033 | } |
| 3034 | } |
| 3035 | break; |
| 3036 | } |
| 3037 | } |
| 3038 | return mEffectChains.size(); |
| 3039 | } |
| 3040 | |
| 3041 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3042 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3043 | { |
| 3044 | Mutex::Autolock _l(mLock); |
| 3045 | return attachAuxEffect_l(track, EffectId); |
| 3046 | } |
| 3047 | |
| 3048 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3049 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3050 | { |
| 3051 | status_t status = NO_ERROR; |
| 3052 | |
| 3053 | if (EffectId == 0) { |
| 3054 | track->setAuxBuffer(0, NULL); |
| 3055 | } else { |
| 3056 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 3057 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 3058 | if (effect != 0) { |
| 3059 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 3060 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 3061 | } else { |
| 3062 | status = INVALID_OPERATION; |
| 3063 | } |
| 3064 | } else { |
| 3065 | status = BAD_VALUE; |
| 3066 | } |
| 3067 | } |
| 3068 | return status; |
| 3069 | } |
| 3070 | |
| 3071 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 3072 | { |
| 3073 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3074 | sp<Track> track = mTracks[i]; |
| 3075 | if (track->auxEffectId() == effectId) { |
| 3076 | attachAuxEffect_l(track, 0); |
| 3077 | } |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 3082 | { |
Glenn Kasten | 388d571 | 2017-04-07 14:38:41 -0700 | [diff] [blame] | 3083 | tlNBLogWriter = mNBLogWriter.get(); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3084 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3085 | Vector< sp<Track> > tracksToRemove; |
| 3086 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3087 | mStandbyTimeNs = systemTime(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3088 | nsecs_t lastWriteFinished = -1; // time last server write completed |
| 3089 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3090 | |
| 3091 | // MIXER |
| 3092 | nsecs_t lastWarning = 0; |
| 3093 | |
| 3094 | // DUPLICATING |
| 3095 | // FIXME could this be made local to while loop? |
| 3096 | writeFrames = 0; |
| 3097 | |
| 3098 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3099 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3100 | |
| 3101 | if (mType == MIXER) { |
| 3102 | sleepTimeShift = 0; |
| 3103 | } |
| 3104 | |
| 3105 | CpuStats cpuStats; |
| 3106 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 3107 | |
| 3108 | acquireWakeLock(); |
| 3109 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3110 | // mNBLogWriter logging APIs can only be called by a single thread, typically the |
| 3111 | // thread associated with this PlaybackThread. |
| 3112 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) |
| 3113 | // 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] | 3114 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 3115 | // and then that string will be logged at the next convenient opportunity. |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3116 | // See reference to logString below. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3117 | const char *logString = NULL; |
| 3118 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3119 | // Estimated time for next buffer to be written to hal. This is used only on |
| 3120 | // suspended mode (for now) to help schedule the wait time until next iteration. |
| 3121 | nsecs_t timeLoopNextNs = 0; |
| 3122 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 3123 | checkSilentMode_l(); |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3124 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3125 | while (!exitPending()) |
| 3126 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 3127 | // Log merge requests are performed during AudioFlinger binder transactions, but |
| 3128 | // that does not cover audio playback. It's requested here for that reason. |
| 3129 | mAudioFlinger->requestLogMerge(); |
| 3130 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3131 | cpuStats.sample(myName); |
| 3132 | |
| 3133 | Vector< sp<EffectChain> > effectChains; |
| 3134 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3135 | { // scope for mLock |
| 3136 | |
| 3137 | Mutex::Autolock _l(mLock); |
| 3138 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3139 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3140 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3141 | // 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] | 3142 | if (logString != NULL) { |
| 3143 | mNBLogWriter->logTimestamp(); |
| 3144 | mNBLogWriter->log(logString); |
| 3145 | logString = NULL; |
| 3146 | } |
| 3147 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3148 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3149 | // and associate with the sink frames written out. We need |
| 3150 | // this to convert the sink timestamp to the track timestamp. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3151 | bool kernelLocationUpdate = false; |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3152 | if (mNormalSink != 0) { |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3153 | // Note: The DuplicatingThread may not have a mNormalSink. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3154 | // We always fetch the timestamp here because often the downstream |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3155 | // sink will block while writing. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3156 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 3157 | (void) mNormalSink->getTimestamp(timestamp); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3158 | |
| 3159 | // We keep track of the last valid kernel position in case we are in underrun |
| 3160 | // and the normal mixer period is the same as the fast mixer period, or there |
| 3161 | // is some error from the HAL. |
| 3162 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3163 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3164 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3165 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3166 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3167 | |
| 3168 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3169 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 3170 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3171 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3175 | kernelLocationUpdate = true; |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3176 | } else { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3177 | ALOGVV("getTimestamp error - no valid kernel position"); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3180 | // copy over kernel info |
| 3181 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3182 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 3183 | + mSuspendedFrames; // add frames discarded when suspended |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3184 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 3185 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3186 | } |
| 3187 | // mFramesWritten for non-offloaded tracks are contiguous |
| 3188 | // even after standby() is called. This is useful for the track frame |
| 3189 | // to sink frame mapping. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3190 | bool serverLocationUpdate = false; |
| 3191 | if (mFramesWritten != lastFramesWritten) { |
| 3192 | serverLocationUpdate = true; |
| 3193 | lastFramesWritten = mFramesWritten; |
| 3194 | } |
| 3195 | // Only update timestamps if there is a meaningful change. |
| 3196 | // Either the kernel timestamp must be valid or we have written something. |
| 3197 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 3198 | if (serverLocationUpdate) { |
| 3199 | // use the time before we called the HAL write - it is a bit more accurate |
| 3200 | // to when the server last read data than the current time here. |
| 3201 | // |
| 3202 | // If we haven't written anything, mLastWriteTime will be -1 |
| 3203 | // and we use systemTime(). |
| 3204 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 3205 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 |
| 3206 | ? systemTime() : mLastWriteTime; |
| 3207 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3208 | |
| 3209 | for (const sp<Track> &t : mActiveTracks) { |
| 3210 | if (!t->isFastTrack()) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3211 | t->updateTrackFrameInfo( |
| 3212 | t->mAudioTrackServerProxy->framesReleased(), |
| 3213 | mFramesWritten, |
| 3214 | mTimestamp); |
| 3215 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3216 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3217 | } |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3218 | #if 0 |
| 3219 | // logFormat example |
Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 3220 | if (z % 100 == 0) { |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3221 | timespec ts; |
| 3222 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3223 | 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] | 3224 | "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] | 3225 | LOGT("A deceptive null-terminated string %\0"); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3226 | } |
| 3227 | ++z; |
| 3228 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3229 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3230 | if (mSignalPending) { |
| 3231 | // A signal was raised while we were unlocked |
| 3232 | mSignalPending = false; |
| 3233 | } else if (waitingAsyncCallback_l()) { |
| 3234 | if (exitPending()) { |
| 3235 | break; |
| 3236 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3237 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3238 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3239 | releaseWakeLock_l(); |
| 3240 | released = true; |
| 3241 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 3242 | |
| 3243 | const int64_t waitNs = computeWaitTimeNs_l(); |
| 3244 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); |
| 3245 | status_t status = mWaitWorkCV.waitRelative(mLock, waitNs); |
| 3246 | if (status == TIMED_OUT) { |
| 3247 | mSignalPending = true; // if timeout recheck everything |
| 3248 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3249 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3250 | if (released) { |
| 3251 | acquireWakeLock_l(); |
| 3252 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3253 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3254 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3255 | |
| 3256 | continue; |
| 3257 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3258 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3259 | isSuspended()) { |
| 3260 | // put audio hardware into standby after short delay |
| 3261 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3262 | |
| 3263 | threadLoop_standby(); |
| 3264 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3265 | // This is where we go into standby |
| 3266 | if (!mStandby) { |
| 3267 | LOG_AUDIO_STATE(); |
| 3268 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3269 | mStandby = true; |
| 3270 | } |
| 3271 | |
| 3272 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 3273 | // we're about to wait, flush the binder command buffer |
| 3274 | IPCThreadState::self()->flushCommands(); |
| 3275 | |
| 3276 | clearOutputTracks(); |
| 3277 | |
| 3278 | if (exitPending()) { |
| 3279 | break; |
| 3280 | } |
| 3281 | |
| 3282 | releaseWakeLock_l(); |
| 3283 | // wait until we have something to do... |
| 3284 | ALOGV("%s going to sleep", myName.string()); |
| 3285 | mWaitWorkCV.wait(mLock); |
| 3286 | ALOGV("%s waking up", myName.string()); |
| 3287 | acquireWakeLock_l(); |
| 3288 | |
| 3289 | mMixerStatus = MIXER_IDLE; |
| 3290 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 3291 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3292 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3293 | checkSilentMode_l(); |
| 3294 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3295 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3296 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3297 | if (mType == MIXER) { |
| 3298 | sleepTimeShift = 0; |
| 3299 | } |
| 3300 | |
| 3301 | continue; |
| 3302 | } |
| 3303 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3304 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 3305 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 3306 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3307 | mActiveTracks.updatePowerState(this); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3308 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3309 | // prevent any changes in effect chain list and in each effect chain |
| 3310 | // during mixing and effect process as the audio buffers could be deleted |
| 3311 | // or modified if an effect is created or deleted |
| 3312 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3313 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3314 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3315 | if (mBytesRemaining == 0) { |
| 3316 | mCurrentWriteLength = 0; |
| 3317 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3318 | // threadLoop_mix() sets mCurrentWriteLength |
| 3319 | threadLoop_mix(); |
| 3320 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3321 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3322 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3323 | // must be written to HAL |
| 3324 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3325 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3326 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3327 | } |
| 3328 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3329 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3330 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3331 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3332 | // or mSinkBuffer (if there are no effects). |
| 3333 | // |
| 3334 | // This is done pre-effects computation; if effects change to |
| 3335 | // support higher precision, this needs to move. |
| 3336 | // |
| 3337 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3338 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3339 | if (mMixerBufferValid) { |
| 3340 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3341 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3342 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3343 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3344 | // and is handled here if we're going directly to the sink. |
| 3345 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3346 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3347 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3348 | } |
| 3349 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3350 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3351 | mNormalFrameCount * mChannelCount); |
| 3352 | } |
| 3353 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3354 | mBytesRemaining = mCurrentWriteLength; |
| 3355 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3356 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 3357 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 3358 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 3359 | mBytesWritten += mBytesRemaining; |
| 3360 | mFramesWritten += framesRemaining; |
| 3361 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3362 | mBytesRemaining = 0; |
| 3363 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3364 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3365 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3366 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3367 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3368 | effectChains[i]->process_l(); |
| 3369 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3370 | } |
| 3371 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3372 | // Process effect chains for offloaded thread even if no audio |
| 3373 | // was read from audio track: process only updates effect state |
| 3374 | // and thus does have to be synchronized with audio writes but may have |
| 3375 | // to be called while waiting for async write callback |
| 3376 | if (mType == OFFLOAD) { |
| 3377 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3378 | effectChains[i]->process_l(); |
| 3379 | } |
| 3380 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3381 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3382 | // Only if the Effects buffer is enabled and there is data in the |
| 3383 | // Effects buffer (buffer valid), we need to |
| 3384 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3385 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3386 | if (mEffectBufferValid) { |
| 3387 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3388 | |
| 3389 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3390 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3391 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3392 | } |
| 3393 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3394 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3395 | mNormalFrameCount * mChannelCount); |
| 3396 | } |
| 3397 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3398 | // enable changes in effect chain |
| 3399 | unlockEffectChains(effectChains); |
| 3400 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3401 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3402 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3403 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3404 | ssize_t ret = 0; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3405 | // We save lastWriteFinished here, as previousLastWriteFinished, |
| 3406 | // for throttling. On thread start, previousLastWriteFinished will be |
| 3407 | // set to -1, which properly results in no throttling after the first write. |
| 3408 | nsecs_t previousLastWriteFinished = lastWriteFinished; |
| 3409 | nsecs_t delta = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3410 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3411 | // FIXME rewrite to reduce number of system calls |
| 3412 | mLastWriteTime = systemTime(); // also used for dumpsys |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3413 | ret = threadLoop_write(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3414 | lastWriteFinished = systemTime(); |
| 3415 | delta = lastWriteFinished - mLastWriteTime; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3416 | if (ret < 0) { |
| 3417 | mBytesRemaining = 0; |
| 3418 | } else { |
| 3419 | mBytesWritten += ret; |
| 3420 | mBytesRemaining -= ret; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3421 | mFramesWritten += ret / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3422 | } |
| 3423 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3424 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3425 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3426 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3427 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3428 | // write blocked detection |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3429 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3430 | mNumDelayedWrites++; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3431 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3432 | ATRACE_NAME("underrun"); |
| 3433 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3434 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3435 | lastWarning = lastWriteFinished; |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3436 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3437 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3438 | |
| 3439 | if (mThreadThrottle |
| 3440 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3441 | && ret > 0) { // we wrote something |
| 3442 | // Limit MixerThread data processing to no more than twice the |
| 3443 | // expected processing rate. |
| 3444 | // |
| 3445 | // This helps prevent underruns with NuPlayer and other applications |
| 3446 | // which may set up buffers that are close to the minimum size, or use |
| 3447 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3448 | // |
| 3449 | // The throttle smooths out sudden large data drains from the device, |
| 3450 | // e.g. when it comes out of standby, which often causes problems with |
| 3451 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3452 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3453 | // 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] | 3454 | // |
| 3455 | // Total time spent in last processing cycle equals time spent in |
| 3456 | // 1. threadLoop_write, as well as time spent in |
| 3457 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 3458 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3459 | |
Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 3460 | // it's OK if deltaMs (and deltaNs) is an overestimate. |
| 3461 | nsecs_t deltaNs; |
| 3462 | // deltaNs = lastWriteFinished - previousLastWriteFinished; |
| 3463 | __builtin_sub_overflow( |
| 3464 | lastWriteFinished,previousLastWriteFinished, &deltaNs); |
| 3465 | const int32_t deltaMs = deltaNs / 1000000; |
| 3466 | |
Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 3467 | const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3468 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3469 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3470 | // notify of throttle start on verbose log |
| 3471 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3472 | "mixer(%p) throttle begin:" |
| 3473 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3474 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3475 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3476 | // Throttle must be attributed to the previous mixer loop's write time |
| 3477 | // to allow back-to-back throttling. |
| 3478 | lastWriteFinished += throttleMs * 1000000; |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3479 | } else { |
| 3480 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3481 | if (diff > 0) { |
| 3482 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3483 | // but prevent spamming for bluetooth |
Jakub Pawlowski | 0568ded | 2018-03-14 11:20:05 -0700 | [diff] [blame^] | 3484 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()) && |
| 3485 | !audio_is_hearing_aid_out_device(outDevice()), |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3486 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3487 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3488 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3489 | } |
| 3490 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3491 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3492 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3493 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3494 | ATRACE_BEGIN("sleep"); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3495 | Mutex::Autolock _l(mLock); |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3496 | // suspended requires accurate metering of sleep time. |
| 3497 | if (isSuspended()) { |
| 3498 | // advance by expected sleepTime |
| 3499 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); |
| 3500 | const nsecs_t nowNs = systemTime(); |
| 3501 | |
| 3502 | // compute expected next time vs current time. |
| 3503 | // (negative deltas are treated as delays). |
| 3504 | nsecs_t deltaNs = timeLoopNextNs - nowNs; |
| 3505 | if (deltaNs < -kMaxNextBufferDelayNs) { |
| 3506 | // Delays longer than the max allowed trigger a reset. |
| 3507 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); |
| 3508 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); |
| 3509 | timeLoopNextNs = nowNs + deltaNs; |
| 3510 | } else if (deltaNs < 0) { |
| 3511 | // Delays within the max delay allowed: zero the delta/sleepTime |
| 3512 | // to help the system catch up in the next iteration(s) |
| 3513 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); |
| 3514 | deltaNs = 0; |
| 3515 | } |
| 3516 | // update sleep time (which is >= 0) |
| 3517 | mSleepTimeUs = deltaNs / 1000; |
| 3518 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3519 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
| 3520 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3521 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3522 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3523 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | // Finally let go of removed track(s), without the lock held |
| 3527 | // since we can't guarantee the destructors won't acquire that |
| 3528 | // same lock. This will also mutate and push a new fast mixer state. |
| 3529 | threadLoop_removeTracks(tracksToRemove); |
| 3530 | tracksToRemove.clear(); |
| 3531 | |
| 3532 | // FIXME I don't understand the need for this here; |
| 3533 | // it was in the original code but maybe the |
| 3534 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3535 | clearOutputTracks(); |
| 3536 | |
| 3537 | // Effect chains will be actually deleted here if they were removed from |
| 3538 | // mEffectChains list during mixing or effects processing |
| 3539 | effectChains.clear(); |
| 3540 | |
| 3541 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3542 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3543 | } |
| 3544 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3545 | threadLoop_exit(); |
| 3546 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3547 | if (!mStandby) { |
| 3548 | threadLoop_standby(); |
| 3549 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | releaseWakeLock(); |
| 3553 | |
| 3554 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3555 | return false; |
| 3556 | } |
| 3557 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3558 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3559 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3560 | { |
| 3561 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3562 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3563 | for (size_t i=0 ; i<count ; i++) { |
| 3564 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3565 | mActiveTracks.remove(track); |
| 3566 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3567 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3568 | if (chain != 0) { |
| 3569 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3570 | track->sessionId()); |
| 3571 | chain->decActiveTrackCnt(); |
| 3572 | } |
| 3573 | if (track->isTerminated()) { |
| 3574 | removeTrack_l(track); |
| 3575 | } |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3580 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3581 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3582 | { |
| 3583 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3584 | ExtendedTimestamp ets; |
| 3585 | status_t status = mNormalSink->getTimestamp(ets); |
| 3586 | if (status == NO_ERROR) { |
| 3587 | status = ets.getBestTimestamp(×tamp); |
| 3588 | } |
| 3589 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3590 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3591 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3592 | uint64_t position64; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3593 | if (mOutput->getPresentationPosition(&position64, ×tamp.mTime) == OK) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3594 | timestamp.mPosition = (uint32_t)position64; |
| 3595 | return NO_ERROR; |
| 3596 | } |
| 3597 | } |
| 3598 | return INVALID_OPERATION; |
| 3599 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3600 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3601 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3602 | audio_patch_handle_t *handle) |
| 3603 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3604 | status_t status; |
| 3605 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3606 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3607 | // or if HAL does not properly lock against access. |
| 3608 | AutoPark<FastMixer> park(mFastMixer); |
| 3609 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3610 | } else { |
| 3611 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3612 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3613 | return status; |
| 3614 | } |
| 3615 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3616 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3617 | audio_patch_handle_t *handle) |
| 3618 | { |
| 3619 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3620 | |
| 3621 | // store new device and send to effects |
| 3622 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3623 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3624 | type |= patch->sinks[i].ext.device.type; |
| 3625 | } |
| 3626 | |
| 3627 | #ifdef ADD_BATTERY_DATA |
| 3628 | // when changing the audio output device, call addBatteryData to notify |
| 3629 | // the change |
| 3630 | if (mOutDevice != type) { |
| 3631 | uint32_t params = 0; |
| 3632 | // check whether speaker is on |
| 3633 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3634 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3637 | audio_devices_t deviceWithoutSpeaker |
| 3638 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3639 | // check if any other device (except speaker) is on |
| 3640 | if (type & deviceWithoutSpeaker) { |
| 3641 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3642 | } |
| 3643 | |
| 3644 | if (params != 0) { |
| 3645 | addBatteryData(params); |
| 3646 | } |
| 3647 | } |
| 3648 | #endif |
| 3649 | |
| 3650 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3651 | mEffectChains[i]->setDevice_l(type); |
| 3652 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3653 | |
| 3654 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3655 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3656 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3657 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3658 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3659 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3660 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3661 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3662 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 3663 | patch->sources, |
| 3664 | patch->num_sinks, |
| 3665 | patch->sinks, |
| 3666 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3667 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3668 | char *address; |
| 3669 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3670 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3671 | address = audio_device_address_to_parameter( |
| 3672 | patch->sinks[0].ext.device.type, |
| 3673 | patch->sinks[0].ext.device.address); |
| 3674 | } else { |
| 3675 | address = (char *)calloc(1, 1); |
| 3676 | } |
| 3677 | AudioParameter param = AudioParameter(String8(address)); |
| 3678 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3679 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3680 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3681 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3682 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3683 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3684 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3685 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3686 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3687 | return status; |
| 3688 | } |
| 3689 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3690 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3691 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3692 | status_t status; |
| 3693 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3694 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3695 | // or if HAL does not properly lock against access. |
| 3696 | AutoPark<FastMixer> park(mFastMixer); |
| 3697 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3698 | } else { |
| 3699 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3700 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3701 | return status; |
| 3702 | } |
| 3703 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3704 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3705 | { |
| 3706 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3707 | |
| 3708 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3709 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3710 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3711 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3712 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3713 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3714 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3715 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3716 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3717 | } |
| 3718 | return status; |
| 3719 | } |
| 3720 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3721 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3722 | { |
| 3723 | Mutex::Autolock _l(mLock); |
| 3724 | mTracks.add(track); |
| 3725 | } |
| 3726 | |
| 3727 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3728 | { |
| 3729 | Mutex::Autolock _l(mLock); |
| 3730 | destroyTrack_l(track); |
| 3731 | } |
| 3732 | |
| 3733 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3734 | { |
| 3735 | ThreadBase::getAudioPortConfig(config); |
| 3736 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3737 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3738 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3739 | } |
| 3740 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3741 | // ---------------------------------------------------------------------------- |
| 3742 | |
| 3743 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3744 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3745 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3746 | // mAudioMixer below |
| 3747 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3748 | mFastMixerFutex(0), |
| 3749 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3750 | // mOutputSink below |
| 3751 | // mPipeSink below |
| 3752 | // mNormalSink below |
| 3753 | { |
| 3754 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3755 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3756 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3757 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3758 | mNormalFrameCount); |
| 3759 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3760 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3761 | if (type == DUPLICATING) { |
| 3762 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3763 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3764 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3765 | return; |
| 3766 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3767 | // create an NBAIO sink for the HAL output stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 3768 | mOutputSink = new AudioStreamOutSink(output->stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3769 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3770 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3771 | #if !LOG_NDEBUG |
| 3772 | ssize_t index = |
| 3773 | #else |
| 3774 | (void) |
| 3775 | #endif |
| 3776 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3777 | ALOG_ASSERT(index == 0); |
| 3778 | |
| 3779 | // initialize fast mixer depending on configuration |
| 3780 | bool initFastMixer; |
| 3781 | switch (kUseFastMixer) { |
| 3782 | case FastMixer_Never: |
| 3783 | initFastMixer = false; |
| 3784 | break; |
| 3785 | case FastMixer_Always: |
| 3786 | initFastMixer = true; |
| 3787 | break; |
| 3788 | case FastMixer_Static: |
| 3789 | case FastMixer_Dynamic: |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3790 | // FastMixer was designed to operate with a HAL that pulls at a regular rate, |
| 3791 | // where the period is less than an experimentally determined threshold that can be |
| 3792 | // scheduled reliably with CFS. However, the BT A2DP HAL is |
| 3793 | // bursty (does not pull at a regular rate) and so cannot operate with FastMixer. |
| 3794 | initFastMixer = mFrameCount < mNormalFrameCount |
| 3795 | && (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) == 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3796 | break; |
| 3797 | } |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3798 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, |
| 3799 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", |
| 3800 | mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3801 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3802 | audio_format_t fastMixerFormat; |
| 3803 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3804 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3805 | } else { |
| 3806 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3807 | } |
| 3808 | if (mFormat != fastMixerFormat) { |
| 3809 | // change our Sink format to accept our intermediate precision |
| 3810 | mFormat = fastMixerFormat; |
| 3811 | free(mSinkBuffer); |
| 3812 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3813 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3814 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3815 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3816 | |
| 3817 | // create a MonoPipe to connect our submix to FastMixer |
| 3818 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3819 | #ifdef TEE_SINK |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3820 | NBAIO_Format origformat = format; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3821 | #endif |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3822 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3823 | ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3824 | format.mFormat = fastMixerFormat; |
| 3825 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3826 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3827 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3828 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3829 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3830 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3831 | const NBAIO_Format offers[1] = {format}; |
| 3832 | size_t numCounterOffers = 0; |
Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3833 | #if !LOG_NDEBUG || defined(TEE_SINK) |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3834 | ssize_t index = |
| 3835 | #else |
| 3836 | (void) |
| 3837 | #endif |
| 3838 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3839 | ALOG_ASSERT(index == 0); |
| 3840 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3841 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3842 | mPipeSink = monoPipe; |
| 3843 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3844 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3845 | if (mTeeSinkOutputEnabled) { |
| 3846 | // 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] | 3847 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3848 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3849 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3850 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3851 | ALOG_ASSERT(index == 0); |
| 3852 | mTeeSink = teeSink; |
| 3853 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3854 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3855 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3856 | ALOG_ASSERT(index == 0); |
| 3857 | mTeeSource = teeSource; |
| 3858 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3859 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3860 | |
| 3861 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3862 | mFastMixer = new FastMixer(); |
| 3863 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3864 | #ifdef STATE_QUEUE_DUMP |
| 3865 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3866 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3867 | #endif |
| 3868 | FastMixerState *state = sq->begin(); |
| 3869 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3870 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3871 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3872 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3873 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3874 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3875 | fastTrack->mGeneration++; |
| 3876 | state->mFastTracksGen++; |
| 3877 | state->mTrackMask = 1; |
| 3878 | // fast mixer will use the HAL output sink |
| 3879 | state->mOutputSink = mOutputSink.get(); |
| 3880 | state->mOutputSinkGen++; |
| 3881 | state->mFrameCount = mFrameCount; |
| 3882 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3883 | // already done in constructor initialization list |
| 3884 | //mFastMixerFutex = 0; |
| 3885 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3886 | state->mColdGen++; |
| 3887 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3888 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3889 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3890 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3891 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3892 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3893 | sq->end(); |
| 3894 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3895 | |
| 3896 | // start the fast mixer |
| 3897 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3898 | pid_t tid = mFastMixer->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3899 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 3900 | stream()->setHalThreadPriority(kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3901 | |
| 3902 | #ifdef AUDIO_WATCHDOG |
| 3903 | // create and start the watchdog |
| 3904 | mAudioWatchdog = new AudioWatchdog(); |
| 3905 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3906 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3907 | tid = mAudioWatchdog->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3908 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3909 | #endif |
| 3910 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | switch (kUseFastMixer) { |
| 3914 | case FastMixer_Never: |
| 3915 | case FastMixer_Dynamic: |
| 3916 | mNormalSink = mOutputSink; |
| 3917 | break; |
| 3918 | case FastMixer_Always: |
| 3919 | mNormalSink = mPipeSink; |
| 3920 | break; |
| 3921 | case FastMixer_Static: |
| 3922 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3923 | break; |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | AudioFlinger::MixerThread::~MixerThread() |
| 3928 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3929 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3930 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3931 | FastMixerState *state = sq->begin(); |
| 3932 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3933 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3934 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3935 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3936 | } |
| 3937 | } |
| 3938 | state->mCommand = FastMixerState::EXIT; |
| 3939 | sq->end(); |
| 3940 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3941 | mFastMixer->join(); |
| 3942 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3943 | // We'll use that extract the final state which contains one remaining fast track |
| 3944 | // corresponding to our sub-mix. |
| 3945 | state = sq->begin(); |
| 3946 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3947 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3948 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3949 | delete fastTrack->mBufferProvider; |
| 3950 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3951 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3952 | #ifdef AUDIO_WATCHDOG |
| 3953 | if (mAudioWatchdog != 0) { |
| 3954 | mAudioWatchdog->requestExit(); |
| 3955 | mAudioWatchdog->requestExitAndWait(); |
| 3956 | mAudioWatchdog.clear(); |
| 3957 | } |
| 3958 | #endif |
| 3959 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3960 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3961 | delete mAudioMixer; |
| 3962 | } |
| 3963 | |
| 3964 | |
| 3965 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3966 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3967 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3968 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3969 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3970 | } |
| 3971 | return latency; |
| 3972 | } |
| 3973 | |
| 3974 | |
| 3975 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3976 | { |
| 3977 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3978 | } |
| 3979 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3980 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3981 | { |
| 3982 | // FIXME we should only do one push per cycle; confirm this is true |
| 3983 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3984 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3985 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3986 | FastMixerState *state = sq->begin(); |
| 3987 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3988 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3989 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3990 | |
| 3991 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 3992 | ATRACE_BEGIN("write"); |
| 3993 | mOutput->write((char *)mSinkBuffer, 0); |
| 3994 | ATRACE_END(); |
| 3995 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3996 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3997 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3998 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3999 | } |
| 4000 | #ifdef AUDIO_WATCHDOG |
| 4001 | if (mAudioWatchdog != 0) { |
| 4002 | mAudioWatchdog->resume(); |
| 4003 | } |
| 4004 | #endif |
| 4005 | } |
| 4006 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 4007 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 4008 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 4009 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 4010 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4011 | sq->end(); |
| 4012 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4013 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4014 | mNormalSink = mPipeSink; |
| 4015 | } |
| 4016 | } else { |
| 4017 | sq->end(false /*didModify*/); |
| 4018 | } |
| 4019 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4020 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 4024 | { |
| 4025 | // Idle the fast mixer if it's currently 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::IDLE)) { |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 4030 | // Report any frames trapped in the Monopipe |
| 4031 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); |
| 4032 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); |
| 4033 | mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld " |
| 4034 | "monoPipeWritten:%lld monoPipeLeft:%lld", |
| 4035 | (long long)mFramesWritten, (long long)mSuspendedFrames, |
| 4036 | (long long)mPipeSink->framesWritten(), pipeFrames); |
| 4037 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); |
| 4038 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4039 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4040 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4041 | state->mColdGen++; |
| 4042 | mFastMixerFutex = 0; |
| 4043 | sq->end(); |
| 4044 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 4045 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 4046 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4047 | mNormalSink = mOutputSink; |
| 4048 | } |
| 4049 | #ifdef AUDIO_WATCHDOG |
| 4050 | if (mAudioWatchdog != 0) { |
| 4051 | mAudioWatchdog->pause(); |
| 4052 | } |
| 4053 | #endif |
| 4054 | } else { |
| 4055 | sq->end(false /*didModify*/); |
| 4056 | } |
| 4057 | } |
| 4058 | PlaybackThread::threadLoop_standby(); |
| 4059 | } |
| 4060 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4061 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 4062 | { |
| 4063 | return false; |
| 4064 | } |
| 4065 | |
| 4066 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 4067 | { |
| 4068 | return !mStandby; |
| 4069 | } |
| 4070 | |
| 4071 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 4072 | { |
| 4073 | Mutex::Autolock _l(mLock); |
| 4074 | return waitingAsyncCallback_l(); |
| 4075 | } |
| 4076 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4077 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 4078 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 4079 | { |
| 4080 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4081 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4082 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4083 | // discard any pending drain or write ack by incrementing sequence |
| 4084 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 4085 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4086 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4087 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 4088 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4089 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4090 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4091 | } |
| 4092 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4093 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 4094 | { |
| 4095 | ALOGV("signal playback thread"); |
| 4096 | broadcast_l(); |
| 4097 | } |
| 4098 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 4099 | void AudioFlinger::PlaybackThread::onAsyncError() |
| 4100 | { |
| 4101 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 4102 | invalidateTracks((audio_stream_type_t)i); |
| 4103 | } |
| 4104 | } |
| 4105 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4106 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 4107 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4108 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 4109 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4110 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4111 | // increase sleep time progressively when application underrun condition clears. |
| 4112 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 4113 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 4114 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4115 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4116 | sleepTimeShift--; |
| 4117 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4118 | mSleepTimeUs = 0; |
| 4119 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4120 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 4121 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 4125 | { |
| 4126 | // If no tracks are ready, sleep once for the duration of an output |
| 4127 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4128 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4129 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4130 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 4131 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 4132 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4133 | } |
| 4134 | // reduce sleep time in case of consecutive application underruns to avoid |
| 4135 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 4136 | // duration we would end up writing less data than needed by the audio HAL if |
| 4137 | // the condition persists. |
| 4138 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 4139 | sleepTimeShift++; |
| 4140 | } |
| 4141 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4142 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4143 | } |
| 4144 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4145 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 4146 | // before effects processing or output. |
| 4147 | if (mMixerBufferValid) { |
| 4148 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4149 | } else { |
| 4150 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 4151 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4152 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4153 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 4154 | "anticipated start"); |
| 4155 | } |
| 4156 | // TODO add standby time extension fct of effect tail |
| 4157 | } |
| 4158 | |
| 4159 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 4160 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 4161 | Vector< sp<Track> > *tracksToRemove) |
| 4162 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4163 | // clean up deleted track names in AudioMixer before allocating new tracks |
| 4164 | (void)mTracks.processDeletedTrackNames([this](int name) { |
| 4165 | // for each name, destroy it in the AudioMixer |
| 4166 | if (mAudioMixer->exists(name)) { |
| 4167 | mAudioMixer->destroy(name); |
| 4168 | } |
| 4169 | }); |
| 4170 | mTracks.clearDeletedTrackNames(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4171 | |
| 4172 | mixer_state mixerStatus = MIXER_IDLE; |
| 4173 | // find out which tracks need to be processed |
| 4174 | size_t count = mActiveTracks.size(); |
| 4175 | size_t mixedTracks = 0; |
| 4176 | size_t tracksWithEffect = 0; |
| 4177 | // counts only _active_ fast tracks |
| 4178 | size_t fastTracks = 0; |
| 4179 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 4180 | |
| 4181 | float masterVolume = mMasterVolume; |
| 4182 | bool masterMute = mMasterMute; |
| 4183 | |
| 4184 | if (masterMute) { |
| 4185 | masterVolume = 0; |
| 4186 | } |
| 4187 | // Delegate master volume control to effect in output mix effect chain if needed |
| 4188 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 4189 | if (chain != 0) { |
| 4190 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 4191 | chain->setVolume_l(&v, &v); |
| 4192 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 4193 | chain.clear(); |
| 4194 | } |
| 4195 | |
| 4196 | // prepare a new state to push |
| 4197 | FastMixerStateQueue *sq = NULL; |
| 4198 | FastMixerState *state = NULL; |
| 4199 | bool didModify = false; |
| 4200 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4201 | bool coldIdle = false; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4202 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4203 | sq = mFastMixer->sq(); |
| 4204 | state = sq->begin(); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4205 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4206 | } |
| 4207 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4208 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4209 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4210 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4211 | for (size_t i=0 ; i<count ; i++) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4212 | const sp<Track> t = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4213 | |
| 4214 | // this const just means the local variable doesn't change |
| 4215 | Track* const track = t.get(); |
| 4216 | |
| 4217 | // process fast tracks |
| 4218 | if (track->isFastTrack()) { |
| 4219 | |
| 4220 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 4221 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 4222 | // the track never becomes active so it's fast mixer slot is never touched. |
| 4223 | // The converse, of removing an (active) track and then creating a new track |
| 4224 | // at the identical fast mixer slot within the same normal mix cycle, |
| 4225 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 4226 | int j = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4227 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4228 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 4229 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 4230 | |
| 4231 | // Determine whether the track is currently in underrun condition, |
| 4232 | // and whether it had a recent underrun. |
| 4233 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 4234 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 4235 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 4236 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 4237 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 4238 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 4239 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 4240 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 4241 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 4242 | track->mObservedUnderruns = underruns; |
| 4243 | // don't count underruns that occur while stopping or pausing |
| 4244 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4245 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 4246 | recentUnderruns > 0) { |
| 4247 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 4248 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4249 | } else { |
| 4250 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | // This is similar to the state machine for normal tracks, |
| 4254 | // with a few modifications for fast tracks. |
| 4255 | bool isActive = true; |
| 4256 | switch (track->mState) { |
| 4257 | case TrackBase::STOPPING_1: |
| 4258 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4259 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4260 | track->mState = TrackBase::STOPPING_2; |
| 4261 | } |
| 4262 | break; |
| 4263 | case TrackBase::PAUSING: |
| 4264 | // ramp down is not yet implemented |
| 4265 | track->setPaused(); |
| 4266 | break; |
| 4267 | case TrackBase::RESUMING: |
| 4268 | // ramp up is not yet implemented |
| 4269 | track->mState = TrackBase::ACTIVE; |
| 4270 | break; |
| 4271 | case TrackBase::ACTIVE: |
| 4272 | if (recentFull > 0 || recentPartial > 0) { |
| 4273 | // track has provided at least some frames recently: reset retry count |
| 4274 | track->mRetryCount = kMaxTrackRetries; |
| 4275 | } |
| 4276 | if (recentUnderruns == 0) { |
| 4277 | // no recent underruns: stay active |
| 4278 | break; |
| 4279 | } |
| 4280 | // there has recently been an underrun of some kind |
| 4281 | if (track->sharedBuffer() == 0) { |
| 4282 | // were any of the recent underruns "empty" (no frames available)? |
| 4283 | if (recentEmpty == 0) { |
| 4284 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 4285 | break; |
| 4286 | } |
| 4287 | // there has recently been an "empty" underrun: decrement the retry counter |
| 4288 | if (--(track->mRetryCount) > 0) { |
| 4289 | break; |
| 4290 | } |
| 4291 | // indicate to client process that the track was disabled because of underrun; |
| 4292 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4293 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4294 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 4295 | isActive = false; |
| 4296 | break; |
| 4297 | } |
| 4298 | // fall through |
| 4299 | case TrackBase::STOPPING_2: |
| 4300 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4301 | case TrackBase::STOPPED: |
| 4302 | case TrackBase::FLUSHED: // flush() while active |
| 4303 | // Check for presentation complete if track is inactive |
| 4304 | // We have consumed all the buffers of this track. |
| 4305 | // This would be incomplete if we auto-paused on underrun |
| 4306 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4307 | uint32_t latency = 0; |
| 4308 | status_t result = mOutput->stream->getLatency(&latency); |
| 4309 | ALOGE_IF(result != OK, |
| 4310 | "Error when retrieving output stream latency: %d", result); |
| 4311 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4312 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4313 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 4314 | // track stays in active list until presentation is complete |
| 4315 | break; |
| 4316 | } |
| 4317 | } |
| 4318 | if (track->isStopping_2()) { |
| 4319 | track->mState = TrackBase::STOPPED; |
| 4320 | } |
| 4321 | if (track->isStopped()) { |
| 4322 | // Can't reset directly, as fast mixer is still polling this track |
| 4323 | // track->reset(); |
| 4324 | // So instead mark this track as needing to be reset after push with ack |
| 4325 | resetMask |= 1 << i; |
| 4326 | } |
| 4327 | isActive = false; |
| 4328 | break; |
| 4329 | case TrackBase::IDLE: |
| 4330 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4331 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | if (isActive) { |
| 4335 | // was it previously inactive? |
| 4336 | if (!(state->mTrackMask & (1 << j))) { |
| 4337 | ExtendedAudioBufferProvider *eabp = track; |
| 4338 | VolumeProvider *vp = track; |
| 4339 | fastTrack->mBufferProvider = eabp; |
| 4340 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4341 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4342 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4343 | fastTrack->mGeneration++; |
| 4344 | state->mTrackMask |= 1 << j; |
| 4345 | didModify = true; |
| 4346 | // no acknowledgement required for newly active tracks |
| 4347 | } |
| 4348 | // cache the combined master volume and stream type volume for fast mixer; this |
| 4349 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4350 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4351 | track->mAudioTrackServerProxy->framesReleased()).first; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4352 | track->mCachedVolume = masterVolume |
| 4353 | * mStreamTypes[track->streamType()].volume |
| 4354 | * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4355 | ++fastTracks; |
| 4356 | } else { |
| 4357 | // was it previously active? |
| 4358 | if (state->mTrackMask & (1 << j)) { |
| 4359 | fastTrack->mBufferProvider = NULL; |
| 4360 | fastTrack->mGeneration++; |
| 4361 | state->mTrackMask &= ~(1 << j); |
| 4362 | didModify = true; |
| 4363 | // If any fast tracks were removed, we must wait for acknowledgement |
| 4364 | // because we're about to decrement the last sp<> on those tracks. |
| 4365 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4366 | } else { |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 4367 | // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an |
| 4368 | // AudioTrack may start (which may not be with a start() but with a write() |
| 4369 | // after underrun) and immediately paused or released. In that case the |
| 4370 | // FastTrack state hasn't had time to update. |
| 4371 | // TODO Remove the ALOGW when this theory is confirmed. |
| 4372 | ALOGW("fast track %d should have been active; " |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4373 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 4374 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 4375 | track->sharedBuffer() != 0); |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 4376 | // Since the FastMixer state already has the track inactive, do nothing here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4377 | } |
| 4378 | tracksToRemove->add(track); |
| 4379 | // Avoids a misleading display in dumpsys |
| 4380 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 4381 | } |
| 4382 | continue; |
| 4383 | } |
| 4384 | |
| 4385 | { // local variable scope to avoid goto warning |
| 4386 | |
| 4387 | audio_track_cblk_t* cblk = track->cblk(); |
| 4388 | |
| 4389 | // The first time a track is added we wait |
| 4390 | // for all its buffers to be filled before processing it |
| 4391 | int name = track->name(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4392 | |
| 4393 | // if an active track doesn't exist in the AudioMixer, create it. |
| 4394 | if (!mAudioMixer->exists(name)) { |
| 4395 | status_t status = mAudioMixer->create( |
| 4396 | name, |
| 4397 | track->mChannelMask, |
| 4398 | track->mFormat, |
| 4399 | track->mSessionId); |
| 4400 | if (status != OK) { |
| 4401 | ALOGW("%s: cannot create track name" |
| 4402 | " %d, mask %#x, format %#x, sessionId %d in AudioMixer", |
| 4403 | __func__, name, track->mChannelMask, track->mFormat, track->mSessionId); |
| 4404 | tracksToRemove->add(track); |
| 4405 | track->invalidate(); // consider it dead. |
| 4406 | continue; |
| 4407 | } |
| 4408 | } |
| 4409 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4410 | // make sure that we have enough frames to mix one full buffer. |
| 4411 | // enforce this condition only once to enable draining the buffer in case the client |
| 4412 | // app does not call stop() and relies on underrun to stop: |
| 4413 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4414 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4415 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4416 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4417 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4418 | |
| 4419 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4420 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4421 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4422 | // add frames already consumed but not yet released by the resampler |
| 4423 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4424 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4425 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4426 | uint32_t minFrames = 1; |
| 4427 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4428 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4429 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4430 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4431 | |
| 4432 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4433 | if (ATRACE_ENABLED()) { |
| 4434 | // I wish we had formatted trace names |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4435 | std::string traceName("nRdy"); |
| 4436 | traceName += std::to_string(track->name()); |
| 4437 | ATRACE_INT(traceName.c_str(), framesReady); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4438 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4439 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4440 | !track->isPaused() && !track->isTerminated()) |
| 4441 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4442 | 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] | 4443 | |
| 4444 | mixedTracks++; |
| 4445 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4446 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4447 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4448 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4449 | if (track->mainBuffer() != mSinkBuffer && |
| 4450 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4451 | if (mEffectBufferEnabled) { |
| 4452 | mEffectBufferValid = true; // Later can set directly. |
| 4453 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4454 | chain = getEffectChain_l(track->sessionId()); |
| 4455 | // Delegate volume control to effect in track effect chain if needed |
| 4456 | if (chain != 0) { |
| 4457 | tracksWithEffect++; |
| 4458 | } else { |
| 4459 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4460 | "session %d", |
| 4461 | name, track->sessionId()); |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | |
| 4466 | int param = AudioMixer::VOLUME; |
| 4467 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4468 | // no ramp for the first volume setting |
| 4469 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4470 | if (track->mState == TrackBase::RESUMING) { |
| 4471 | track->mState = TrackBase::ACTIVE; |
| 4472 | param = AudioMixer::RAMP_VOLUME; |
| 4473 | } |
| 4474 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4475 | mLeftVolFloat = -1.0; |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4476 | // FIXME should not make a decision based on mServer |
| 4477 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4478 | // If the track is stopped before the first frame was mixed, |
| 4479 | // do not apply ramp |
| 4480 | param = AudioMixer::RAMP_VOLUME; |
| 4481 | } |
| 4482 | |
| 4483 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4484 | uint32_t vl, vr; // in U8.24 integer format |
| 4485 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4486 | // read original volumes with volume control |
| 4487 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4488 | float v = masterVolume * typeVolume; |
| 4489 | |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4490 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4491 | vl = vr = 0; |
| 4492 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4493 | if (track->isPausing()) { |
| 4494 | track->setPaused(); |
| 4495 | } |
| 4496 | } else { |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4497 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4498 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4499 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4500 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4501 | // 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] | 4502 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4503 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4504 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4505 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4506 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4507 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4508 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4509 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4510 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4511 | track->mAudioTrackServerProxy->framesReleased()).first; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4512 | // now apply the master volume and stream type volume and shaper volume |
| 4513 | vlf *= v * vh; |
| 4514 | vrf *= v * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4515 | // 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] | 4516 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4517 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4518 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4519 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4520 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4521 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4522 | // send level comes from shared memory and so may be corrupt |
| 4523 | if (sendLevel > MAX_GAIN_INT) { |
| 4524 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4525 | sendLevel = MAX_GAIN_INT; |
| 4526 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4527 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4528 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4529 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4530 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4531 | // Delegate volume control to effect in track effect chain if needed |
| 4532 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4533 | // Do not ramp volume if volume is controlled by effect |
| 4534 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4535 | // Update remaining floating point volume levels |
| 4536 | vlf = (float)vl / (1 << 24); |
| 4537 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4538 | track->mHasVolumeController = true; |
| 4539 | } else { |
| 4540 | // force no volume ramp when volume controller was just disabled or removed |
| 4541 | // from effect chain to avoid volume spike |
| 4542 | if (track->mHasVolumeController) { |
| 4543 | param = AudioMixer::VOLUME; |
| 4544 | } |
| 4545 | track->mHasVolumeController = false; |
| 4546 | } |
| 4547 | |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4548 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is |
| 4549 | // still applied by the mixer. |
| 4550 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { |
| 4551 | v = mStreamTypes[track->streamType()].mute ? 0.0f : v; |
| 4552 | if (v != mLeftVolFloat) { |
| 4553 | status_t result = mOutput->stream->setVolume(v, v); |
| 4554 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); |
| 4555 | if (result == OK) { |
| 4556 | mLeftVolFloat = v; |
| 4557 | } |
| 4558 | } |
| 4559 | // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we |
| 4560 | // remove stream volume contribution from software volume. |
| 4561 | if (v != 0.0f && mLeftVolFloat == v) { |
| 4562 | vlf = min(1.0f, vlf / v); |
| 4563 | vrf = min(1.0f, vrf / v); |
| 4564 | vaf = min(1.0f, vaf / v); |
| 4565 | } |
| 4566 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4567 | // XXX: these things DON'T need to be done each time |
| 4568 | mAudioMixer->setBufferProvider(name, track); |
| 4569 | mAudioMixer->enable(name); |
| 4570 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4571 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4572 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4573 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4574 | mAudioMixer->setParameter( |
| 4575 | name, |
| 4576 | AudioMixer::TRACK, |
| 4577 | AudioMixer::FORMAT, (void *)track->format()); |
| 4578 | mAudioMixer->setParameter( |
| 4579 | name, |
| 4580 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4581 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4582 | mAudioMixer->setParameter( |
| 4583 | name, |
| 4584 | AudioMixer::TRACK, |
| 4585 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4586 | // 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] | 4587 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4588 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4589 | if (reqSampleRate == 0) { |
| 4590 | reqSampleRate = mSampleRate; |
| 4591 | } else if (reqSampleRate > maxSampleRate) { |
| 4592 | reqSampleRate = maxSampleRate; |
| 4593 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4594 | mAudioMixer->setParameter( |
| 4595 | name, |
| 4596 | AudioMixer::RESAMPLE, |
| 4597 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4598 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4599 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4600 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4601 | mAudioMixer->setParameter( |
| 4602 | name, |
| 4603 | AudioMixer::TIMESTRETCH, |
| 4604 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4605 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4606 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4607 | /* |
| 4608 | * Select the appropriate output buffer for the track. |
| 4609 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4610 | * Tracks with effects go into their own effects chain buffer |
| 4611 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4612 | * |
| 4613 | * Other tracks can use mMixerBuffer for higher precision |
| 4614 | * channel accumulation. If this buffer is enabled |
| 4615 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4616 | * into it. |
| 4617 | * |
| 4618 | */ |
| 4619 | if (mMixerBufferEnabled |
| 4620 | && (track->mainBuffer() == mSinkBuffer |
| 4621 | || track->mainBuffer() == mMixerBuffer)) { |
| 4622 | mAudioMixer->setParameter( |
| 4623 | name, |
| 4624 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4625 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4626 | mAudioMixer->setParameter( |
| 4627 | name, |
| 4628 | AudioMixer::TRACK, |
| 4629 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4630 | // TODO: override track->mainBuffer()? |
| 4631 | mMixerBufferValid = true; |
| 4632 | } else { |
| 4633 | mAudioMixer->setParameter( |
| 4634 | name, |
| 4635 | AudioMixer::TRACK, |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 4636 | AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4637 | mAudioMixer->setParameter( |
| 4638 | name, |
| 4639 | AudioMixer::TRACK, |
| 4640 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4641 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4642 | mAudioMixer->setParameter( |
| 4643 | name, |
| 4644 | AudioMixer::TRACK, |
| 4645 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4646 | |
| 4647 | // reset retry count |
| 4648 | track->mRetryCount = kMaxTrackRetries; |
| 4649 | |
| 4650 | // If one track is ready, set the mixer ready if: |
| 4651 | // - the mixer was not ready during previous round OR |
| 4652 | // - no other track is not ready |
| 4653 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4654 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4655 | mixerStatus = MIXER_TRACKS_READY; |
| 4656 | } |
| 4657 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4658 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4659 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4660 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4661 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4662 | } else { |
| 4663 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4664 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4665 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4666 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4667 | // previous audio buffer again to effects |
| 4668 | chain = getEffectChain_l(track->sessionId()); |
| 4669 | if (chain != 0) { |
| 4670 | chain->clearInputBuffer(); |
| 4671 | } |
| 4672 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4673 | 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] | 4674 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4675 | track->isStopped() || track->isPaused()) { |
| 4676 | // We have consumed all the buffers of this track. |
| 4677 | // Remove it from the list of active tracks. |
| 4678 | // TODO: use actual buffer filling status instead of latency when available from |
| 4679 | // audio HAL |
| 4680 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4681 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4682 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4683 | if (track->isStopped()) { |
| 4684 | track->reset(); |
| 4685 | } |
| 4686 | tracksToRemove->add(track); |
| 4687 | } |
| 4688 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4689 | // No buffers for this track. Give it a few chances to |
| 4690 | // fill a buffer, then remove it from active list. |
| 4691 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4692 | 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] | 4693 | tracksToRemove->add(track); |
| 4694 | // indicate to client process that the track was disabled because of underrun; |
| 4695 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4696 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4697 | // If one track is not ready, mark the mixer also not ready if: |
| 4698 | // - the mixer was ready during previous round OR |
| 4699 | // - no other track is ready |
| 4700 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4701 | mixerStatus != MIXER_TRACKS_READY) { |
| 4702 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4703 | } |
| 4704 | } |
| 4705 | mAudioMixer->disable(name); |
| 4706 | } |
| 4707 | |
| 4708 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4709 | |
| 4710 | } |
| 4711 | |
| 4712 | // Push the new FastMixer state if necessary |
| 4713 | bool pauseAudioWatchdog = false; |
| 4714 | if (didModify) { |
| 4715 | state->mFastTracksGen++; |
| 4716 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4717 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4718 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4719 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4720 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4721 | state->mColdGen++; |
| 4722 | mFastMixerFutex = 0; |
| 4723 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4724 | mNormalSink = mOutputSink; |
| 4725 | } |
| 4726 | // If we go into cold idle, need to wait for acknowledgement |
| 4727 | // so that fast mixer stops doing I/O. |
| 4728 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4729 | pauseAudioWatchdog = true; |
| 4730 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4731 | } |
| 4732 | if (sq != NULL) { |
| 4733 | sq->end(didModify); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4734 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread |
| 4735 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE |
| 4736 | // when bringing the output sink into standby.) |
| 4737 | // |
| 4738 | // We will get the latest FastMixer state when we come out of COLD_IDLE. |
| 4739 | // |
| 4740 | // This occurs with BT suspend when we idle the FastMixer with |
| 4741 | // active tracks, which may be added or removed. |
| 4742 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4743 | } |
| 4744 | #ifdef AUDIO_WATCHDOG |
| 4745 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4746 | mAudioWatchdog->pause(); |
| 4747 | } |
| 4748 | #endif |
| 4749 | |
| 4750 | // Now perform the deferred reset on fast tracks that have stopped |
| 4751 | while (resetMask != 0) { |
| 4752 | size_t i = __builtin_ctz(resetMask); |
| 4753 | ALOG_ASSERT(i < count); |
| 4754 | resetMask &= ~(1 << i); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4755 | sp<Track> track = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4756 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4757 | track->reset(); |
| 4758 | } |
| 4759 | |
| 4760 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4761 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4762 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4763 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4764 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4768 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4769 | // passing a non-clean buffer to the effect chain |
| 4770 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4771 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4772 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4773 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4774 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4775 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4776 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4777 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4778 | if (mMixerBufferValid) { |
| 4779 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4780 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4781 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4782 | // after mixing. |
| 4783 | // |
| 4784 | // To enforce this guarantee: |
| 4785 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4786 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4787 | // must imply MIXER_TRACKS_READY. |
| 4788 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4789 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4790 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4791 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | // if any fast tracks, then status is ready |
| 4795 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4796 | if (fastTracks > 0) { |
| 4797 | mixerStatus = MIXER_TRACKS_READY; |
| 4798 | } |
| 4799 | return mixerStatus; |
| 4800 | } |
| 4801 | |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4802 | // trackCountForUid_l() must be called with ThreadBase::mLock held |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4803 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) const |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4804 | { |
| 4805 | uint32_t trackCount = 0; |
| 4806 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 4807 | if (mTracks[i]->uid() == uid) { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4808 | trackCount++; |
| 4809 | } |
| 4810 | } |
| 4811 | return trackCount; |
| 4812 | } |
| 4813 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4814 | // isTrackAllowed_l() must be called with ThreadBase::mLock held |
| 4815 | bool AudioFlinger::MixerThread::isTrackAllowed_l( |
| 4816 | audio_channel_mask_t channelMask, audio_format_t format, |
| 4817 | audio_session_t sessionId, uid_t uid) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4818 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4819 | if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 4820 | return false; |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4821 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4822 | // Check validity as we don't call AudioMixer::create() here. |
| 4823 | if (!AudioMixer::isValidFormat(format)) { |
| 4824 | ALOGW("%s: invalid format: %#x", __func__, format); |
| 4825 | return false; |
| 4826 | } |
| 4827 | if (!AudioMixer::isValidChannelMask(channelMask)) { |
| 4828 | ALOGW("%s: invalid channelMask: %#x", __func__, channelMask); |
| 4829 | return false; |
| 4830 | } |
| 4831 | return true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4832 | } |
| 4833 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4834 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4835 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4836 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4837 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4838 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4839 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4840 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4841 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4842 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4843 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4844 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4845 | AudioParameter param = AudioParameter(keyValuePair); |
| 4846 | int value; |
| 4847 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4848 | reconfig = true; |
| 4849 | } |
| 4850 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4851 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4852 | status = BAD_VALUE; |
| 4853 | } else { |
| 4854 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4855 | reconfig = true; |
| 4856 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4857 | } |
| 4858 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4859 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4860 | status = BAD_VALUE; |
| 4861 | } else { |
| 4862 | // no need to save value, since it's constant |
| 4863 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4864 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4865 | } |
| 4866 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4867 | // do not accept frame count changes if tracks are open as the track buffer |
| 4868 | // size depends on frame count and correct behavior would not be guaranteed |
| 4869 | // if frame count is changed after track creation |
| 4870 | if (!mTracks.isEmpty()) { |
| 4871 | status = INVALID_OPERATION; |
| 4872 | } else { |
| 4873 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4874 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4875 | } |
| 4876 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4877 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4878 | // when changing the audio output device, call addBatteryData to notify |
| 4879 | // the change |
| 4880 | if (mOutDevice != value) { |
| 4881 | uint32_t params = 0; |
| 4882 | // check whether speaker is on |
| 4883 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4884 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4885 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4886 | |
| 4887 | audio_devices_t deviceWithoutSpeaker |
| 4888 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4889 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4890 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4891 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4892 | } |
| 4893 | |
| 4894 | if (params != 0) { |
| 4895 | addBatteryData(params); |
| 4896 | } |
| 4897 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4898 | #endif |
| 4899 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4900 | // forward device change to effects that have requested to be |
| 4901 | // aware of attached audio device. |
| 4902 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4903 | a2dpDeviceChanged = |
| 4904 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4905 | mOutDevice = value; |
| 4906 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4907 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4908 | } |
| 4909 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4910 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4911 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4912 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4913 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4914 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4915 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4916 | mStandby = true; |
| 4917 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4918 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4919 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4920 | if (status == NO_ERROR && reconfig) { |
| 4921 | readOutputParameters_l(); |
| 4922 | delete mAudioMixer; |
| 4923 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4924 | for (const auto &track : mTracks) { |
| 4925 | const int name = track->name(); |
| 4926 | status_t status = mAudioMixer->create( |
| 4927 | name, |
| 4928 | track->mChannelMask, |
| 4929 | track->mFormat, |
| 4930 | track->mSessionId); |
| 4931 | ALOGW_IF(status != NO_ERROR, |
| 4932 | "%s: cannot create track name" |
| 4933 | " %d, mask %#x, format %#x, sessionId %d in AudioMixer", |
| 4934 | __func__, |
| 4935 | name, track->mChannelMask, track->mFormat, track->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4936 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4937 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4938 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4939 | } |
| 4940 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4941 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | |
| 4945 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4946 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4947 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4948 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 4949 | dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4950 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4951 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4952 | if (hasFastMixer()) { |
| 4953 | dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); |
| 4954 | |
| 4955 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 4956 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 4957 | // This is a large object so we place it on the heap. |
| 4958 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 4959 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 4960 | copy->dump(fd); |
| 4961 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4962 | |
| 4963 | #ifdef STATE_QUEUE_DUMP |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4964 | // Similar for state queue |
| 4965 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4966 | observerCopy.dump(fd); |
| 4967 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4968 | mutatorCopy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4969 | #endif |
| 4970 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4971 | #ifdef AUDIO_WATCHDOG |
| 4972 | if (mAudioWatchdog != 0) { |
| 4973 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4974 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4975 | wdCopy.dump(fd); |
| 4976 | } |
| 4977 | #endif |
| 4978 | |
| 4979 | } else { |
| 4980 | dprintf(fd, " No FastMixer\n"); |
| 4981 | } |
| 4982 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4983 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4984 | // Write the tee output to a .wav file |
Glenn Kasten | 5b2191a | 2016-08-19 11:44:47 -0700 | [diff] [blame] | 4985 | dumpTee(fd, mTeeSource, mId, 'M'); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4986 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4987 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4991 | { |
| 4992 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4993 | } |
| 4994 | |
| 4995 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4996 | { |
| 4997 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4998 | } |
| 4999 | |
| 5000 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 5001 | { |
| 5002 | PlaybackThread::cacheParameters_l(); |
| 5003 | |
| 5004 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 5005 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 5006 | // increase threshold again due to low power audio mode. The way this warning |
| 5007 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 5008 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 5009 | } |
| 5010 | |
| 5011 | // ---------------------------------------------------------------------------- |
| 5012 | |
| 5013 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5014 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 5015 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5016 | { |
| 5017 | } |
| 5018 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5019 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 5020 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5021 | ThreadBase::type_t type, bool systemReady) |
| 5022 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5023 | , mVolumeShaperActive(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5024 | { |
| 5025 | } |
| 5026 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5027 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 5028 | { |
| 5029 | } |
| 5030 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5031 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5032 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5033 | float left, right; |
| 5034 | |
| 5035 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 5036 | left = right = 0; |
| 5037 | } else { |
| 5038 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 5039 | float v = mMasterVolume * typeVolume; |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 5040 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5041 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5042 | // Get volumeshaper scaling |
| 5043 | std::pair<float /* volume */, bool /* active */> |
| 5044 | vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5045 | track->mAudioTrackServerProxy->framesReleased()); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5046 | v *= vh.first; |
| 5047 | mVolumeShaperActive = vh.second; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5048 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5049 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 5050 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5051 | if (left > GAIN_FLOAT_UNITY) { |
| 5052 | left = GAIN_FLOAT_UNITY; |
| 5053 | } |
| 5054 | left *= v; |
| 5055 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 5056 | if (right > GAIN_FLOAT_UNITY) { |
| 5057 | right = GAIN_FLOAT_UNITY; |
| 5058 | } |
| 5059 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | if (lastTrack) { |
| 5063 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 5064 | mLeftVolFloat = left; |
| 5065 | mRightVolFloat = right; |
| 5066 | |
| 5067 | // Convert volumes from float to 8.24 |
| 5068 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 5069 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 5070 | |
| 5071 | // Delegate volume control to effect in track effect chain if needed |
| 5072 | // only one effect chain can be present on DirectOutputThread, so if |
| 5073 | // there is one, the track is connected to it |
| 5074 | if (!mEffectChains.isEmpty()) { |
| 5075 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 5076 | left = (float)vl / (1 << 24); |
| 5077 | right = (float)vr / (1 << 24); |
| 5078 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5079 | status_t result = mOutput->stream->setVolume(left, right); |
| 5080 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5081 | } |
| 5082 | } |
| 5083 | } |
| 5084 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5085 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 5086 | { |
| 5087 | sp<Track> previousTrack = mPreviousTrack.promote(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5088 | sp<Track> latestTrack = mActiveTracks.getLatest(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5089 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5090 | if (previousTrack != 0 && latestTrack != 0) { |
| 5091 | if (mType == DIRECT) { |
| 5092 | if (previousTrack.get() != latestTrack.get()) { |
| 5093 | mFlushPending = true; |
| 5094 | } |
| 5095 | } else /* mType == OFFLOAD */ { |
| 5096 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 5097 | mFlushPending = true; |
| 5098 | } |
| 5099 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5100 | } |
| 5101 | PlaybackThread::onAddNewTrack_l(); |
| 5102 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5103 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5104 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 5105 | Vector< sp<Track> > *tracksToRemove |
| 5106 | ) |
| 5107 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5108 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5109 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5110 | bool doHwPause = false; |
| 5111 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5112 | |
| 5113 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5114 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5115 | if (t->isInvalid()) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5116 | ALOGW("An invalidated track shouldn't be in active list"); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5117 | tracksToRemove->add(t); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5118 | continue; |
| 5119 | } |
| 5120 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5121 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5122 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5123 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5124 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5125 | // Only consider last track started for volume and mixer state control. |
| 5126 | // In theory an older track could underrun and restart after the new one starts |
| 5127 | // but as we only care about the transition phase between two tracks on a |
| 5128 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5129 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5130 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5131 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5132 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5133 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5134 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5135 | doHwPause = true; |
| 5136 | mHwPaused = true; |
| 5137 | } |
| 5138 | tracksToRemove->add(track); |
| 5139 | } else if (track->isFlushPending()) { |
| 5140 | track->flushAck(); |
| 5141 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5142 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5143 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5144 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5145 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5146 | if (last) { |
| 5147 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5148 | if (mHwPaused) { |
| 5149 | doHwResume = true; |
| 5150 | mHwPaused = false; |
| 5151 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5152 | } |
| 5153 | } |
| 5154 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5155 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5156 | // for all its buffers to be filled before processing it. |
| 5157 | // Allow draining the buffer in case the client |
| 5158 | // app does not call stop() and relies on underrun to stop: |
| 5159 | // hence the test on (track->mRetryCount > 1). |
| 5160 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5161 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5162 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5163 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5164 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5165 | minFrames = mNormalFrameCount; |
| 5166 | } else { |
| 5167 | minFrames = 1; |
| 5168 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5169 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5170 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 5171 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5172 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5173 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5174 | |
| 5175 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5176 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5177 | if (last) { |
| 5178 | // make sure processVolume_l() will apply new volume even if 0 |
| 5179 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5180 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5181 | if (!mHwSupportsPause) { |
| 5182 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5183 | } |
| 5184 | } |
| 5185 | |
| 5186 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5187 | processVolume_l(track, last); |
| 5188 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5189 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5190 | if (previousTrack != 0) { |
| 5191 | if (track != previousTrack.get()) { |
| 5192 | // Flush any data still being written from last track |
| 5193 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5194 | // Invalidate previous track to force a seek when resuming. |
| 5195 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | mPreviousTrack = track; |
| 5199 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5200 | // reset retry count |
| 5201 | track->mRetryCount = kMaxTrackRetriesDirect; |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5202 | mActiveTrack = t; |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5203 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5204 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5205 | doHwResume = true; |
| 5206 | mHwPaused = false; |
| 5207 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5208 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5209 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5210 | // clear effect chain input buffer if the last active track started underruns |
| 5211 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5212 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5213 | mEffectChains[0]->clearInputBuffer(); |
| 5214 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5215 | if (track->isStopping_1()) { |
| 5216 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5217 | if (last && mHwPaused) { |
| 5218 | doHwResume = true; |
| 5219 | mHwPaused = false; |
| 5220 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5221 | } |
| 5222 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 5223 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5224 | // We have consumed all the buffers of this track. |
| 5225 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5226 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5227 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5228 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 5229 | } else { |
| 5230 | audioHALFrames = 0; |
| 5231 | } |
| 5232 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5233 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5234 | if (mStandby || !last || |
| 5235 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5236 | if (track->isStopping_2()) { |
| 5237 | track->mState = TrackBase::STOPPED; |
| 5238 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5239 | if (track->isStopped()) { |
| 5240 | track->reset(); |
| 5241 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5242 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5243 | } |
| 5244 | } else { |
| 5245 | // No buffers for this track. Give it a few chances to |
| 5246 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5247 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5248 | if (--(track->mRetryCount) <= 0) { |
| 5249 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5250 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5251 | // indicate to client process that the track was disabled because of underrun; |
| 5252 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5253 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5254 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5255 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 5256 | "minFrames = %u, mFormat = %#x", |
| 5257 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5258 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5259 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5260 | doHwPause = true; |
| 5261 | mHwPaused = true; |
| 5262 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | } |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5268 | // 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] | 5269 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5270 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5271 | if (mTracks[i]->isFlushPending()) { |
| 5272 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5273 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5274 | } |
| 5275 | } |
| 5276 | } |
| 5277 | |
| 5278 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5279 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5280 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5281 | // if resume is received before pause is executed. |
| 5282 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5283 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5284 | status_t result = mOutput->stream->pause(); |
| 5285 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5286 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5287 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5288 | flushHw_l(); |
| 5289 | } |
| 5290 | if (mHwSupportsPause && !mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5291 | status_t result = mOutput->stream->resume(); |
| 5292 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5293 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5294 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5295 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5296 | |
| 5297 | return mixerStatus; |
| 5298 | } |
| 5299 | |
| 5300 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 5301 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5302 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5303 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5304 | // output audio to hardware |
| 5305 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5306 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5307 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5308 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 5309 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5310 | // no need to pad with 0 for compressed audio |
| 5311 | if (audio_has_proportional_frames(mFormat)) { |
| 5312 | memset(curBuf, 0, frameCount * mFrameSize); |
| 5313 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5314 | break; |
| 5315 | } |
| 5316 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 5317 | frameCount -= buffer.frameCount; |
| 5318 | curBuf += buffer.frameCount * mFrameSize; |
| 5319 | mActiveTrack->releaseBuffer(&buffer); |
| 5320 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5321 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5322 | mSleepTimeUs = 0; |
| 5323 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5324 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 5328 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5329 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5330 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5331 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5332 | return; |
| 5333 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5334 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5335 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5336 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5337 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5338 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5339 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5340 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5341 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5342 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5343 | } |
| 5344 | } |
| 5345 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5346 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 5347 | { |
| 5348 | { |
| 5349 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5350 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5351 | if (mTracks[i]->isFlushPending()) { |
| 5352 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5353 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5354 | } |
| 5355 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5356 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5357 | flushHw_l(); |
| 5358 | } |
| 5359 | } |
| 5360 | PlaybackThread::threadLoop_exit(); |
| 5361 | } |
| 5362 | |
| 5363 | // must be called with thread mutex locked |
| 5364 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 5365 | { |
| 5366 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5367 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5368 | |
vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5369 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { |
| 5370 | return !mStandby; |
| 5371 | } |
| 5372 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5373 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 5374 | // after a timeout and we will enter standby then. |
| 5375 | if (mTracks.size() > 0) { |
| 5376 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5377 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 5378 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5379 | } |
| 5380 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5381 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5382 | } |
| 5383 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5384 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 5385 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5386 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5387 | { |
| 5388 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5389 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5390 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5391 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5392 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5393 | AudioParameter param = AudioParameter(keyValuePair); |
| 5394 | int value; |
| 5395 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5396 | // forward device change to effects that have requested to be |
| 5397 | // aware of attached audio device. |
| 5398 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5399 | a2dpDeviceChanged = |
| 5400 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5401 | mOutDevice = value; |
| 5402 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5403 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5404 | } |
| 5405 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5406 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5407 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5408 | // do not accept frame count changes if tracks are open as the track buffer |
| 5409 | // size depends on frame count and correct behavior would not be garantied |
| 5410 | // if frame count is changed after track creation |
| 5411 | if (!mTracks.isEmpty()) { |
| 5412 | status = INVALID_OPERATION; |
| 5413 | } else { |
| 5414 | reconfig = true; |
| 5415 | } |
| 5416 | } |
| 5417 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5418 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5419 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5420 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5421 | mStandby = true; |
| 5422 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5423 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5424 | } |
| 5425 | if (status == NO_ERROR && reconfig) { |
| 5426 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5427 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5428 | } |
| 5429 | } |
| 5430 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5431 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5435 | { |
| 5436 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5437 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5438 | time = PlaybackThread::activeSleepTimeUs(); |
| 5439 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5440 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5441 | } |
| 5442 | return time; |
| 5443 | } |
| 5444 | |
| 5445 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5446 | { |
| 5447 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5448 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5449 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5450 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5451 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5452 | } |
| 5453 | return time; |
| 5454 | } |
| 5455 | |
| 5456 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5457 | { |
| 5458 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5459 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5460 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5461 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5462 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5463 | } |
| 5464 | return time; |
| 5465 | } |
| 5466 | |
| 5467 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5468 | { |
| 5469 | PlaybackThread::cacheParameters_l(); |
| 5470 | |
| 5471 | // use shorter standby delay as on normal output to release |
| 5472 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5473 | // no delay on outputs with HW A/V sync |
| 5474 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5475 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5476 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5477 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5478 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5479 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5480 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5481 | } |
| 5482 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5483 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5484 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5485 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5486 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5487 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5488 | } |
| 5489 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5490 | int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const { |
| 5491 | // If a VolumeShaper is active, we must wake up periodically to update volume. |
| 5492 | const int64_t NS_PER_MS = 1000000; |
| 5493 | return mVolumeShaperActive ? |
| 5494 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); |
| 5495 | } |
| 5496 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5497 | // ---------------------------------------------------------------------------- |
| 5498 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5499 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5500 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5501 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5502 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5503 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5504 | mDrainSequence(0), |
| 5505 | mAsyncError(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5506 | { |
| 5507 | } |
| 5508 | |
| 5509 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5510 | { |
| 5511 | } |
| 5512 | |
| 5513 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5514 | { |
| 5515 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5516 | } |
| 5517 | |
| 5518 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5519 | { |
| 5520 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5521 | uint32_t writeAckSequence; |
| 5522 | uint32_t drainSequence; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5523 | bool asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5524 | |
| 5525 | { |
| 5526 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5527 | while (!((mWriteAckSequence & 1) || |
| 5528 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5529 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5530 | exitPending())) { |
| 5531 | mWaitWorkCV.wait(mLock); |
| 5532 | } |
| 5533 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5534 | if (exitPending()) { |
| 5535 | break; |
| 5536 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5537 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5538 | mWriteAckSequence, mDrainSequence); |
| 5539 | writeAckSequence = mWriteAckSequence; |
| 5540 | mWriteAckSequence &= ~1; |
| 5541 | drainSequence = mDrainSequence; |
| 5542 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5543 | asyncError = mAsyncError; |
| 5544 | mAsyncError = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5545 | } |
| 5546 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5547 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5548 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5549 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5550 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5551 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5552 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5553 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5554 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5555 | if (asyncError) { |
| 5556 | playbackThread->onAsyncError(); |
| 5557 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5558 | } |
| 5559 | } |
| 5560 | } |
| 5561 | return false; |
| 5562 | } |
| 5563 | |
| 5564 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5565 | { |
| 5566 | ALOGV("AsyncCallbackThread::exit"); |
| 5567 | Mutex::Autolock _l(mLock); |
| 5568 | requestExit(); |
| 5569 | mWaitWorkCV.broadcast(); |
| 5570 | } |
| 5571 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5572 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5573 | { |
| 5574 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5575 | // bit 0 is cleared |
| 5576 | mWriteAckSequence = sequence << 1; |
| 5577 | } |
| 5578 | |
| 5579 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5580 | { |
| 5581 | Mutex::Autolock _l(mLock); |
| 5582 | // ignore unexpected callbacks |
| 5583 | if (mWriteAckSequence & 2) { |
| 5584 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5585 | mWaitWorkCV.signal(); |
| 5586 | } |
| 5587 | } |
| 5588 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5589 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5590 | { |
| 5591 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5592 | // bit 0 is cleared |
| 5593 | mDrainSequence = sequence << 1; |
| 5594 | } |
| 5595 | |
| 5596 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5597 | { |
| 5598 | Mutex::Autolock _l(mLock); |
| 5599 | // ignore unexpected callbacks |
| 5600 | if (mDrainSequence & 2) { |
| 5601 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5602 | mWaitWorkCV.signal(); |
| 5603 | } |
| 5604 | } |
| 5605 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5606 | void AudioFlinger::AsyncCallbackThread::setAsyncError() |
| 5607 | { |
| 5608 | Mutex::Autolock _l(mLock); |
| 5609 | mAsyncError = true; |
| 5610 | mWaitWorkCV.signal(); |
| 5611 | } |
| 5612 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5613 | |
| 5614 | // ---------------------------------------------------------------------------- |
| 5615 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5616 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5617 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5618 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), |
| 5619 | mOffloadUnderrunPosition(~0LL) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5620 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 5621 | //FIXME: mStandby should be set to true by ThreadBase constructo |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5622 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5623 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5624 | } |
| 5625 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5626 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5627 | { |
| 5628 | if (mFlushPending || mHwPaused) { |
| 5629 | // If a flush is pending or track was paused, just discard buffered data |
| 5630 | flushHw_l(); |
| 5631 | } else { |
| 5632 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5633 | threadLoop_drain(); |
| 5634 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5635 | if (mUseAsyncWrite) { |
| 5636 | ALOG_ASSERT(mCallbackThread != 0); |
| 5637 | mCallbackThread->exit(); |
| 5638 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5639 | PlaybackThread::threadLoop_exit(); |
| 5640 | } |
| 5641 | |
| 5642 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5643 | Vector< sp<Track> > *tracksToRemove |
| 5644 | ) |
| 5645 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5646 | size_t count = mActiveTracks.size(); |
| 5647 | |
| 5648 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5649 | bool doHwPause = false; |
| 5650 | bool doHwResume = false; |
| 5651 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5652 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5653 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5654 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5655 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5656 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5657 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5658 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5659 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5660 | // Only consider last track started for volume and mixer state control. |
| 5661 | // In theory an older track could underrun and restart after the new one starts |
| 5662 | // but as we only care about the transition phase between two tracks on a |
| 5663 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5664 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5665 | bool last = l.get() == track; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5666 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5667 | if (track->isInvalid()) { |
| 5668 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5669 | tracksToRemove->add(track); |
| 5670 | continue; |
| 5671 | } |
| 5672 | |
| 5673 | if (track->mState == TrackBase::IDLE) { |
| 5674 | ALOGW("An idle track shouldn't be in active list"); |
| 5675 | continue; |
| 5676 | } |
| 5677 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5678 | if (track->isPausing()) { |
| 5679 | track->setPaused(); |
| 5680 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5681 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5682 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5683 | mHwPaused = true; |
| 5684 | } |
| 5685 | // If we were part way through writing the mixbuffer to |
| 5686 | // the HAL we must save this until we resume |
| 5687 | // BUG - this will be wrong if a different track is made active, |
| 5688 | // in that case we want to discard the pending data in the |
| 5689 | // mixbuffer and tell the client to present it again when the |
| 5690 | // track is resumed |
| 5691 | mPausedWriteLength = mCurrentWriteLength; |
| 5692 | mPausedBytesRemaining = mBytesRemaining; |
| 5693 | mBytesRemaining = 0; // stop writing |
| 5694 | } |
| 5695 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5696 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5697 | if (track->isStopping_1()) { |
| 5698 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5699 | } else { |
| 5700 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5701 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5702 | track->flushAck(); |
| 5703 | if (last) { |
| 5704 | mFlushPending = true; |
| 5705 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5706 | } else if (track->isResumePending()){ |
| 5707 | track->resumeAck(); |
| 5708 | if (last) { |
| 5709 | if (mPausedBytesRemaining) { |
| 5710 | // Need to continue write that was interrupted |
| 5711 | mCurrentWriteLength = mPausedWriteLength; |
| 5712 | mBytesRemaining = mPausedBytesRemaining; |
| 5713 | mPausedBytesRemaining = 0; |
| 5714 | } |
| 5715 | if (mHwPaused) { |
| 5716 | doHwResume = true; |
| 5717 | mHwPaused = false; |
| 5718 | // threadLoop_mix() will handle the case that we need to |
| 5719 | // resume an interrupted write |
| 5720 | } |
| 5721 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5722 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5723 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5724 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5725 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5726 | // Do not handle new data in this iteration even if track->framesReady() |
| 5727 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5728 | } |
| 5729 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5730 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5731 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5732 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5733 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5734 | if (last) { |
| 5735 | // make sure processVolume_l() will apply new volume even if 0 |
| 5736 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5737 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5741 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5742 | if (previousTrack != 0) { |
| 5743 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5744 | // Flush any data still being written from last track |
| 5745 | mBytesRemaining = 0; |
| 5746 | if (mPausedBytesRemaining) { |
| 5747 | // Last track was paused so we also need to flush saved |
| 5748 | // mixbuffer state and invalidate track so that it will |
| 5749 | // re-submit that unwritten data when it is next resumed |
| 5750 | mPausedBytesRemaining = 0; |
| 5751 | // Invalidate is a bit drastic - would be more efficient |
| 5752 | // to have a flag to tell client that some of the |
| 5753 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5754 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5755 | } |
| 5756 | // flush data already sent to the DSP if changing audio session as audio |
| 5757 | // comes from a different source. Also invalidate previous track to force a |
| 5758 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5759 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5760 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5761 | } |
| 5762 | } |
| 5763 | } |
| 5764 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5765 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5766 | if (track->isStopping_1()) { |
| 5767 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5768 | } else { |
| 5769 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5770 | } |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5771 | mActiveTrack = t; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5772 | mixerStatus = MIXER_TRACKS_READY; |
| 5773 | } |
| 5774 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5775 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5776 | if (track->isStopping_1()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5777 | if (--(track->mRetryCount) <= 0) { |
| 5778 | // Hardware buffer can hold a large amount of audio so we must |
| 5779 | // wait for all current track's data to drain before we say |
| 5780 | // that the track is stopped. |
| 5781 | if (mBytesRemaining == 0) { |
| 5782 | // Only start draining when all data in mixbuffer |
| 5783 | // has been written |
| 5784 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5785 | track->mState = TrackBase::STOPPING_2; // so presentation completes after |
| 5786 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 5787 | if (last && !mStandby) { |
| 5788 | // do not modify drain sequence if we are already draining. This happens |
| 5789 | // when resuming from pause after drain. |
| 5790 | if ((mDrainSequence & 1) == 0) { |
| 5791 | mSleepTimeUs = 0; |
| 5792 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 5793 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5794 | mDrainSequence += 2; |
| 5795 | } |
| 5796 | if (mHwPaused) { |
| 5797 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5798 | // a resume so we must ensure hardware is running |
| 5799 | doHwResume = true; |
| 5800 | mHwPaused = false; |
| 5801 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5802 | } |
| 5803 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5804 | } else if (last) { |
| 5805 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); |
| 5806 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5807 | } |
| 5808 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5809 | // Drain has completed or we are in standby, signal presentation complete |
| 5810 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5811 | track->mState = TrackBase::STOPPED; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5812 | uint32_t latency = 0; |
| 5813 | status_t result = mOutput->stream->getLatency(&latency); |
| 5814 | ALOGE_IF(result != OK, |
| 5815 | "Error when retrieving output stream latency: %d", result); |
| 5816 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5817 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5818 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5819 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5820 | track->reset(); |
| 5821 | tracksToRemove->add(track); |
| 5822 | } |
| 5823 | } else { |
| 5824 | // No buffers for this track. Give it a few chances to |
| 5825 | // fill a buffer, then remove it from active list. |
| 5826 | if (--(track->mRetryCount) <= 0) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5827 | bool running = false; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5828 | uint64_t position = 0; |
| 5829 | struct timespec unused; |
| 5830 | // The running check restarts the retry counter at least once. |
| 5831 | status_t ret = mOutput->stream->getPresentationPosition(&position, &unused); |
| 5832 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { |
| 5833 | running = true; |
| 5834 | mOffloadUnderrunPosition = position; |
| 5835 | } |
| 5836 | if (ret == NO_ERROR) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5837 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, |
| 5838 | (long long)position, (long long)mOffloadUnderrunPosition); |
| 5839 | } |
| 5840 | if (running) { // still running, give us more time. |
| 5841 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5842 | } else { |
| 5843 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5844 | track->name()); |
| 5845 | tracksToRemove->add(track); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 5846 | // tell client process that the track was disabled because of underrun; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5847 | // it will then automatically call start() when data is available |
| 5848 | track->disable(); |
| 5849 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5850 | } else if (last){ |
| 5851 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5852 | } |
| 5853 | } |
| 5854 | } |
| 5855 | // compute volume for this track |
| 5856 | processVolume_l(track, last); |
| 5857 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5858 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5859 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5860 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5861 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5862 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5863 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5864 | status_t result = mOutput->stream->pause(); |
| 5865 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5866 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5867 | if (mFlushPending) { |
| 5868 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5869 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5870 | if (!mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5871 | status_t result = mOutput->stream->resume(); |
| 5872 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5873 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5874 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5875 | // remove all the tracks that need to be... |
| 5876 | removeTracks_l(*tracksToRemove); |
| 5877 | |
| 5878 | return mixerStatus; |
| 5879 | } |
| 5880 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5881 | // must be called with thread mutex locked |
| 5882 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5883 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5884 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5885 | mWriteAckSequence, mDrainSequence); |
| 5886 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5887 | return true; |
| 5888 | } |
| 5889 | return false; |
| 5890 | } |
| 5891 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5892 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5893 | { |
| 5894 | Mutex::Autolock _l(mLock); |
| 5895 | return waitingAsyncCallback_l(); |
| 5896 | } |
| 5897 | |
| 5898 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5899 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5900 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5901 | // Flush anything still waiting in the mixbuffer |
| 5902 | mCurrentWriteLength = 0; |
| 5903 | mBytesRemaining = 0; |
| 5904 | mPausedWriteLength = 0; |
| 5905 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5906 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 5907 | mBytesWritten = 0; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5908 | mOffloadUnderrunPosition = ~0LL; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5909 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5910 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5911 | // discard any pending drain or write ack by incrementing sequence |
| 5912 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5913 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5914 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5915 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5916 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5917 | } |
| 5918 | } |
| 5919 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5920 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
| 5921 | { |
| 5922 | Mutex::Autolock _l(mLock); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5923 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 5924 | mFlushPending = true; |
| 5925 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5926 | } |
| 5927 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5928 | // ---------------------------------------------------------------------------- |
| 5929 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5930 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5931 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5932 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5933 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5934 | mWaitTimeMs(UINT_MAX) |
| 5935 | { |
| 5936 | addOutputTrack(mainThread); |
| 5937 | } |
| 5938 | |
| 5939 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5940 | { |
| 5941 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5942 | mOutputTracks[i]->destroy(); |
| 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5947 | { |
| 5948 | // mix buffers... |
| 5949 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5950 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5951 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5952 | if (mMixerBufferValid) { |
| 5953 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5954 | } else { |
| 5955 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5956 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5957 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5958 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5959 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5960 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5961 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5965 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5966 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5967 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5968 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5969 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5970 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5971 | } |
| 5972 | } else if (mBytesWritten != 0) { |
| 5973 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5974 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5975 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5976 | } else { |
| 5977 | // flush remaining overflow buffers in output tracks |
| 5978 | writeFrames = 0; |
| 5979 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5980 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5981 | } |
| 5982 | } |
| 5983 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5984 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5985 | { |
| 5986 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5987 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5988 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5989 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5990 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5994 | { |
| 5995 | // DuplicatingThread implements standby by stopping all tracks |
| 5996 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5997 | outputTracks[i]->stop(); |
| 5998 | } |
| 5999 | } |
| 6000 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6001 | void AudioFlinger::DuplicatingThread::dumpInternals(int fd, const Vector<String16>& args __unused) |
| 6002 | { |
| 6003 | MixerThread::dumpInternals(fd, args); |
| 6004 | |
| 6005 | std::stringstream ss; |
| 6006 | const size_t numTracks = mOutputTracks.size(); |
| 6007 | ss << " " << numTracks << " OutputTracks"; |
| 6008 | if (numTracks > 0) { |
| 6009 | ss << ":"; |
| 6010 | for (const auto &track : mOutputTracks) { |
| 6011 | const sp<ThreadBase> thread = track->thread().promote(); |
| 6012 | ss << " (" << track->name() << " : "; |
| 6013 | if (thread.get() != nullptr) { |
| 6014 | ss << thread.get() << ", " << thread->id(); |
| 6015 | } else { |
| 6016 | ss << "null"; |
| 6017 | } |
| 6018 | ss << ")"; |
| 6019 | } |
| 6020 | } |
| 6021 | ss << "\n"; |
| 6022 | std::string result = ss.str(); |
| 6023 | write(fd, result.c_str(), result.size()); |
| 6024 | } |
| 6025 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6026 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 6027 | { |
| 6028 | outputTracks = mOutputTracks; |
| 6029 | } |
| 6030 | |
| 6031 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 6032 | { |
| 6033 | outputTracks.clear(); |
| 6034 | } |
| 6035 | |
| 6036 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 6037 | { |
| 6038 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6039 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 6040 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 6041 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 6042 | const size_t frameCount = |
| 6043 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 6044 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 6045 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 6046 | // nearly empty and the other may be dropping data). |
| 6047 | |
| 6048 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6049 | this, |
| 6050 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6051 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6052 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6053 | frameCount, |
| 6054 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 6055 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 6056 | if (status != NO_ERROR) { |
| 6057 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 6058 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6059 | } |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 6060 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
| 6061 | mOutputTracks.add(outputTrack); |
| 6062 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 6063 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 6067 | { |
| 6068 | Mutex::Autolock _l(mLock); |
| 6069 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6070 | if (mOutputTracks[i]->thread() == thread) { |
| 6071 | mOutputTracks[i]->destroy(); |
| 6072 | mOutputTracks.removeAt(i); |
| 6073 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 6074 | if (thread->getOutput() == mOutput) { |
| 6075 | mOutput = NULL; |
| 6076 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6077 | return; |
| 6078 | } |
| 6079 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 6080 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | // caller must hold mLock |
| 6084 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 6085 | { |
| 6086 | mWaitTimeMs = UINT_MAX; |
| 6087 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6088 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 6089 | if (strong != 0) { |
| 6090 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 6091 | if (waitTimeMs < mWaitTimeMs) { |
| 6092 | mWaitTimeMs = waitTimeMs; |
| 6093 | } |
| 6094 | } |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | |
| 6099 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 6100 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 6101 | { |
| 6102 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 6103 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 6104 | if (thread == 0) { |
| 6105 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 6106 | outputTracks[i].get()); |
| 6107 | return false; |
| 6108 | } |
| 6109 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 6110 | // see note at standby() declaration |
| 6111 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 6112 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 6113 | thread.get()); |
| 6114 | return false; |
| 6115 | } |
| 6116 | } |
| 6117 | return true; |
| 6118 | } |
| 6119 | |
| 6120 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 6121 | { |
| 6122 | return (mWaitTimeMs * 1000) / 2; |
| 6123 | } |
| 6124 | |
| 6125 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 6126 | { |
| 6127 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 6128 | updateWaitTime_l(); |
| 6129 | |
| 6130 | MixerThread::cacheParameters_l(); |
| 6131 | } |
| 6132 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 6133 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6134 | // ---------------------------------------------------------------------------- |
| 6135 | // Record |
| 6136 | // ---------------------------------------------------------------------------- |
| 6137 | |
| 6138 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 6139 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6140 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 6141 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6142 | audio_devices_t inDevice, |
| 6143 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6144 | #ifdef TEE_SINK |
| 6145 | , const sp<NBAIO_Sink>& teeSink |
| 6146 | #endif |
| 6147 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6148 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6149 | mInput(input), |
| 6150 | mActiveTracks(&this->mLocalLog), |
| 6151 | mRsmpInBuffer(NULL), |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6152 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6153 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6154 | #ifdef TEE_SINK |
| 6155 | , mTeeSink(teeSink) |
| 6156 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 6157 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 6158 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6159 | // mFastCapture below |
| 6160 | , mFastCaptureFutex(0) |
| 6161 | // mInputSource |
| 6162 | // mPipeSink |
| 6163 | // mPipeSource |
| 6164 | , mPipeFramesP2(0) |
| 6165 | // mPipeMemory |
| 6166 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6167 | , mFastTrackAvail(false) |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 6168 | , mBtNrecSuspended(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6169 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6170 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 6171 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6172 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6173 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6174 | |
| 6175 | // create an NBAIO source for the HAL input stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 6176 | mInputSource = new AudioStreamInSource(input->stream); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6177 | size_t numCounterOffers = 0; |
| 6178 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6179 | #if !LOG_NDEBUG |
| 6180 | ssize_t index = |
| 6181 | #else |
| 6182 | (void) |
| 6183 | #endif |
| 6184 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6185 | ALOG_ASSERT(index == 0); |
| 6186 | |
| 6187 | // initialize fast capture depending on configuration |
| 6188 | bool initFastCapture; |
| 6189 | switch (kUseFastCapture) { |
| 6190 | case FastCapture_Never: |
| 6191 | initFastCapture = false; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6192 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6193 | break; |
| 6194 | case FastCapture_Always: |
| 6195 | initFastCapture = true; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6196 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6197 | break; |
| 6198 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 6199 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6200 | ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d", |
| 6201 | this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs, |
| 6202 | initFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6203 | break; |
| 6204 | // case FastCapture_Dynamic: |
| 6205 | } |
| 6206 | |
| 6207 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 6208 | // 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] | 6209 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6210 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread |
| 6211 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6212 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6213 | void *pipeBuffer = nullptr; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6214 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 6215 | sp<IMemory> pipeMemory; |
| 6216 | if ((roHeap == 0) || |
| 6217 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6218 | (pipeBuffer = pipeMemory->pointer()) == nullptr) { |
| 6219 | ALOGE("not enough memory for pipe buffer size=%zu; " |
| 6220 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", |
| 6221 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, |
| 6222 | (long long)kRecordThreadReadOnlyHeapSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6223 | goto failed; |
| 6224 | } |
| 6225 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 6226 | memset(pipeBuffer, 0, pipeSize); |
| 6227 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 6228 | const NBAIO_Format offers[1] = {format}; |
| 6229 | size_t numCounterOffers = 0; |
| 6230 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 6231 | ALOG_ASSERT(index == 0); |
| 6232 | mPipeSink = pipe; |
| 6233 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 6234 | numCounterOffers = 0; |
| 6235 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 6236 | ALOG_ASSERT(index == 0); |
| 6237 | mPipeSource = pipeReader; |
| 6238 | mPipeFramesP2 = pipeFramesP2; |
| 6239 | mPipeMemory = pipeMemory; |
| 6240 | |
| 6241 | // create fast capture |
| 6242 | mFastCapture = new FastCapture(); |
| 6243 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6244 | #ifdef STATE_QUEUE_DUMP |
| 6245 | // FIXME |
| 6246 | #endif |
| 6247 | FastCaptureState *state = sq->begin(); |
| 6248 | state->mCblk = NULL; |
| 6249 | state->mInputSource = mInputSource.get(); |
| 6250 | state->mInputSourceGen++; |
| 6251 | state->mPipeSink = pipe; |
| 6252 | state->mPipeSinkGen++; |
| 6253 | state->mFrameCount = mFrameCount; |
| 6254 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6255 | // already done in constructor initialization list |
| 6256 | //mFastCaptureFutex = 0; |
| 6257 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6258 | state->mColdGen++; |
| 6259 | state->mDumpState = &mFastCaptureDumpState; |
| 6260 | #ifdef TEE_SINK |
| 6261 | // FIXME |
| 6262 | #endif |
| 6263 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 6264 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 6265 | sq->end(); |
| 6266 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6267 | |
| 6268 | // start the fast capture |
| 6269 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 6270 | pid_t tid = mFastCapture->getTid(); |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6271 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 6272 | stream()->setHalThreadPriority(kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6273 | #ifdef AUDIO_WATCHDOG |
| 6274 | // FIXME |
| 6275 | #endif |
| 6276 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6277 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6278 | } |
| 6279 | failed: ; |
| 6280 | |
| 6281 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6282 | } |
| 6283 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6284 | AudioFlinger::RecordThread::~RecordThread() |
| 6285 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6286 | if (mFastCapture != 0) { |
| 6287 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6288 | FastCaptureState *state = sq->begin(); |
| 6289 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6290 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6291 | if (old == -1) { |
| 6292 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6293 | } |
| 6294 | } |
| 6295 | state->mCommand = FastCaptureState::EXIT; |
| 6296 | sq->end(); |
| 6297 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6298 | mFastCapture->join(); |
| 6299 | mFastCapture.clear(); |
| 6300 | } |
| 6301 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 6302 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6303 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | void AudioFlinger::RecordThread::onFirstRef() |
| 6307 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6308 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6309 | } |
| 6310 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 6311 | void AudioFlinger::RecordThread::preExit() |
| 6312 | { |
| 6313 | ALOGV(" preExit()"); |
| 6314 | Mutex::Autolock _l(mLock); |
| 6315 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6316 | sp<RecordTrack> track = mTracks[i]; |
| 6317 | track->invalidate(); |
| 6318 | } |
| 6319 | mActiveTracks.clear(); |
| 6320 | mStartStopCond.broadcast(); |
| 6321 | } |
| 6322 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6323 | bool AudioFlinger::RecordThread::threadLoop() |
| 6324 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6325 | nsecs_t lastWarning = 0; |
| 6326 | |
| 6327 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6328 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6329 | reacquire_wakelock: |
| 6330 | sp<RecordTrack> activeTrack; |
| 6331 | { |
| 6332 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6333 | acquireWakeLock_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6334 | } |
| 6335 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6336 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 6337 | uint32_t sleepUs = 0; |
| 6338 | |
| 6339 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6340 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6341 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6342 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6343 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 6344 | Vector< sp<RecordTrack> > activeTracks; |
| 6345 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6346 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6347 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6348 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6349 | // reference to a fast track which is about to be removed |
| 6350 | sp<RecordTrack> fastTrackToRemove; |
| 6351 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6352 | { // scope for mLock |
| 6353 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6354 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6355 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6356 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6357 | // check exitPending here because checkForNewParameters_l() and |
| 6358 | // checkForNewParameters_l() can temporarily release mLock |
| 6359 | if (exitPending()) { |
| 6360 | break; |
| 6361 | } |
| 6362 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6363 | // sleep with mutex unlocked |
| 6364 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 6365 | ATRACE_BEGIN("sleepC"); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6366 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); |
| 6367 | ATRACE_END(); |
| 6368 | sleepUs = 0; |
| 6369 | continue; |
| 6370 | } |
| 6371 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6372 | // if no active track(s), then standby and release wakelock |
| 6373 | size_t size = mActiveTracks.size(); |
| 6374 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6375 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6376 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6377 | releaseWakeLock_l(); |
| 6378 | ALOGV("RecordThread: loop stopping"); |
| 6379 | // go to sleep |
| 6380 | mWaitWorkCV.wait(mLock); |
| 6381 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6382 | goto reacquire_wakelock; |
| 6383 | } |
| 6384 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6385 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6386 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6387 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6388 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6389 | activeTrack = mActiveTracks[i]; |
| 6390 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6391 | if (activeTrack->isFastTrack()) { |
| 6392 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 6393 | fastTrackToRemove = activeTrack; |
| 6394 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6395 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6396 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6397 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6398 | continue; |
| 6399 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6400 | |
| 6401 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 6402 | switch (activeTrackState) { |
| 6403 | |
| 6404 | case TrackBase::PAUSING: |
| 6405 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6406 | doBroadcast = true; |
| 6407 | size--; |
| 6408 | continue; |
| 6409 | |
| 6410 | case TrackBase::STARTING_1: |
| 6411 | sleepUs = 10000; |
| 6412 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6413 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6414 | continue; |
| 6415 | |
| 6416 | case TrackBase::STARTING_2: |
| 6417 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6418 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6419 | activeTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6420 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6421 | break; |
| 6422 | |
| 6423 | case TrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6424 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6425 | break; |
| 6426 | |
| 6427 | case TrackBase::IDLE: |
| 6428 | i++; |
| 6429 | continue; |
| 6430 | |
| 6431 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6432 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6433 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6434 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6435 | activeTracks.add(activeTrack); |
| 6436 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6437 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6438 | if (activeTrack->isFastTrack()) { |
| 6439 | ALOG_ASSERT(!mFastTrackAvail); |
| 6440 | ALOG_ASSERT(fastTrack == 0); |
| 6441 | fastTrack = activeTrack; |
| 6442 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6443 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6444 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6445 | mActiveTracks.updatePowerState(this); |
| 6446 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6447 | if (allStopped) { |
| 6448 | standbyIfNotAlreadyInStandby(); |
| 6449 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6450 | if (doBroadcast) { |
| 6451 | mStartStopCond.broadcast(); |
| 6452 | } |
| 6453 | |
| 6454 | // sleep if there are no active tracks to process |
| 6455 | if (activeTracks.size() == 0) { |
| 6456 | if (sleepUs == 0) { |
| 6457 | sleepUs = kRecordThreadSleepUs; |
| 6458 | } |
| 6459 | continue; |
| 6460 | } |
| 6461 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6462 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6463 | lockEffectChains_l(effectChains); |
| 6464 | } |
| 6465 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6466 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6467 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6468 | size_t size = effectChains.size(); |
| 6469 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6470 | // thread mutex is not locked, but effect chain is locked |
| 6471 | effectChains[i]->process_l(); |
| 6472 | } |
| 6473 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6474 | // 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] | 6475 | if (mFastCapture != 0) { |
| 6476 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6477 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6478 | bool didModify = false; |
| 6479 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6480 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 6481 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 6482 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6483 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6484 | if (old == -1) { |
| 6485 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6486 | } |
| 6487 | } |
| 6488 | state->mCommand = FastCaptureState::READ_WRITE; |
| 6489 | #if 0 // FIXME |
| 6490 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6491 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 6492 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6493 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6494 | didModify = true; |
| 6495 | } |
| 6496 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 6497 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 6498 | if (cblkNew != cblkOld) { |
| 6499 | state->mCblk = cblkNew; |
| 6500 | // block until acked if removing a fast track |
| 6501 | if (cblkOld != NULL) { |
| 6502 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 6503 | } |
| 6504 | didModify = true; |
| 6505 | } |
| 6506 | sq->end(didModify); |
| 6507 | if (didModify) { |
| 6508 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6509 | #if 0 |
| 6510 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6511 | mNormalSource = mPipeSource; |
| 6512 | } |
| 6513 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6517 | // now run the fast track destructor with thread mutex unlocked |
| 6518 | fastTrackToRemove.clear(); |
| 6519 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6520 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 6521 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 6522 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 6523 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 6524 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6525 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6526 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6527 | ssize_t framesRead; |
| 6528 | |
| 6529 | // If an NBAIO source is present, use it to read the normal capture's data |
| 6530 | if (mPipeSource != 0) { |
| 6531 | size_t framesToRead = mBufferSize / mFrameSize; |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6532 | framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6533 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6534 | framesToRead); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6535 | // since pipe is non-blocking, simulate blocking input by waiting for 1/2 of |
| 6536 | // buffer size or at least for 20ms. |
| 6537 | size_t sleepFrames = max( |
| 6538 | min(mPipeFramesP2, mRsmpInFramesP2) / 2, FMS_20 * mSampleRate / 1000); |
| 6539 | if (framesRead <= (ssize_t) sleepFrames) { |
| 6540 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; |
| 6541 | } |
| 6542 | if (framesRead < 0) { |
| 6543 | status_t status = (status_t) framesRead; |
| 6544 | switch (status) { |
| 6545 | case OVERRUN: |
| 6546 | ALOGW("overrun on read from pipe"); |
| 6547 | framesRead = 0; |
| 6548 | break; |
| 6549 | case NEGOTIATE: |
| 6550 | ALOGE("re-negotiation is needed"); |
| 6551 | framesRead = -1; // Will cause an attempt to recover. |
| 6552 | break; |
| 6553 | default: |
| 6554 | ALOGE("unknown error %d on read from pipe", status); |
| 6555 | break; |
| 6556 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6557 | } |
| 6558 | // otherwise use the HAL / AudioStreamIn directly |
| 6559 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6560 | ATRACE_BEGIN("read"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6561 | size_t bytesRead; |
| 6562 | status_t result = mInput->stream->read( |
| 6563 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6564 | ATRACE_END(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6565 | if (result < 0) { |
| 6566 | framesRead = result; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6567 | } else { |
| 6568 | framesRead = bytesRead / mFrameSize; |
| 6569 | } |
| 6570 | } |
| 6571 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6572 | // Update server timestamp with server stats |
| 6573 | // systemTime() is optional if the hardware supports timestamps. |
| 6574 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6575 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6576 | |
| 6577 | // Update server timestamp with kernel stats |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6578 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6579 | int64_t position, time; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6580 | int ret = mInput->stream->getCapturePosition(&position, &time); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6581 | if (ret == NO_ERROR) { |
| 6582 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6583 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6584 | // Note: In general record buffers should tend to be empty in |
| 6585 | // a properly running pipeline. |
| 6586 | // |
| 6587 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6588 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6589 | } |
| 6590 | } |
| 6591 | // Use this to track timestamp information |
| 6592 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6593 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6594 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6595 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6596 | // Force input into standby so that it tries to recover at next read attempt |
| 6597 | inputStandBy(); |
| 6598 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6599 | } |
| 6600 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6601 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6602 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6603 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6604 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6605 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6606 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6607 | } |
| 6608 | // 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] | 6609 | { |
| 6610 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6611 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6612 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6613 | (framesRead - part1) * mFrameSize); |
| 6614 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6615 | } |
| 6616 | rear = mRsmpInRear += framesRead; |
| 6617 | |
| 6618 | size = activeTracks.size(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6619 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6620 | // loop over each active track |
| 6621 | for (size_t i = 0; i < size; i++) { |
| 6622 | activeTrack = activeTracks[i]; |
| 6623 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6624 | // skip fast tracks, as those are handled directly by FastCapture |
| 6625 | if (activeTrack->isFastTrack()) { |
| 6626 | continue; |
| 6627 | } |
| 6628 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6629 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6630 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6631 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6632 | enum { |
| 6633 | OVERRUN_UNKNOWN, |
| 6634 | OVERRUN_TRUE, |
| 6635 | OVERRUN_FALSE |
| 6636 | } overrun = OVERRUN_UNKNOWN; |
| 6637 | |
| 6638 | // loop over getNextBuffer to handle circular sink |
| 6639 | for (;;) { |
| 6640 | |
| 6641 | activeTrack->mSink.frameCount = ~0; |
| 6642 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6643 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6644 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6645 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6646 | // check available frames and handle overrun conditions |
| 6647 | // if the record track isn't draining fast enough. |
| 6648 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6649 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6650 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6651 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6652 | overrun = OVERRUN_TRUE; |
| 6653 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6654 | if (framesOut == 0 || framesIn == 0) { |
| 6655 | break; |
| 6656 | } |
| 6657 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6658 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6659 | // from framesIn. |
| 6660 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6661 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6662 | framesOut = min(framesOut, |
| 6663 | destinationFramesPossible( |
| 6664 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6665 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6666 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6667 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6668 | |
| 6669 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6670 | overrun = OVERRUN_FALSE; |
| 6671 | } |
| 6672 | |
| 6673 | if (activeTrack->mFramesToDrop == 0) { |
| 6674 | if (framesOut > 0) { |
| 6675 | activeTrack->mSink.frameCount = framesOut; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6676 | // Sanitize before releasing if the track has no access to the source data |
| 6677 | // An idle UID receives silence from non virtual devices until active |
| 6678 | if (activeTrack->isSilenced()) { |
| 6679 | memset(activeTrack->mSink.raw, 0, framesOut * mFrameSize); |
| 6680 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6681 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6682 | } |
| 6683 | } else { |
| 6684 | // FIXME could do a partial drop of framesOut |
| 6685 | if (activeTrack->mFramesToDrop > 0) { |
| 6686 | activeTrack->mFramesToDrop -= framesOut; |
| 6687 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6688 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6689 | } |
| 6690 | } else { |
| 6691 | activeTrack->mFramesToDrop += framesOut; |
| 6692 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6693 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6694 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6695 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6696 | activeTrack->sessionId(), |
| 6697 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6698 | activeTrack->mSyncStartEvent->triggerSession() : |
| 6699 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6700 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6701 | } |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6706 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6707 | } |
| 6708 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6709 | |
| 6710 | switch (overrun) { |
| 6711 | case OVERRUN_TRUE: |
| 6712 | // client isn't retrieving buffers fast enough |
| 6713 | if (!activeTrack->setOverflow()) { |
| 6714 | nsecs_t now = systemTime(); |
| 6715 | // FIXME should lastWarning per track? |
| 6716 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6717 | ALOGW("RecordThread: buffer overflow"); |
| 6718 | lastWarning = now; |
| 6719 | } |
| 6720 | } |
| 6721 | break; |
| 6722 | case OVERRUN_FALSE: |
| 6723 | activeTrack->clearOverflow(); |
| 6724 | break; |
| 6725 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6726 | break; |
| 6727 | } |
| 6728 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6729 | // update frame information and push timestamp out |
| 6730 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6731 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6732 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6733 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6734 | } |
| 6735 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6736 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6737 | // enable changes in effect chain |
| 6738 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6739 | // 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] | 6740 | } |
| 6741 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6742 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6743 | |
| 6744 | { |
| 6745 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6746 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6747 | sp<RecordTrack> track = mTracks[i]; |
| 6748 | track->invalidate(); |
| 6749 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6750 | mActiveTracks.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6751 | mStartStopCond.broadcast(); |
| 6752 | } |
| 6753 | |
| 6754 | releaseWakeLock(); |
| 6755 | |
| 6756 | ALOGV("RecordThread %p exiting", this); |
| 6757 | return false; |
| 6758 | } |
| 6759 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6760 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6761 | { |
| 6762 | if (!mStandby) { |
| 6763 | inputStandBy(); |
| 6764 | mStandby = true; |
| 6765 | } |
| 6766 | } |
| 6767 | |
| 6768 | void AudioFlinger::RecordThread::inputStandBy() |
| 6769 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6770 | // Idle the fast capture if it's currently running |
| 6771 | if (mFastCapture != 0) { |
| 6772 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6773 | FastCaptureState *state = sq->begin(); |
| 6774 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6775 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6776 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6777 | state->mColdGen++; |
| 6778 | mFastCaptureFutex = 0; |
| 6779 | sq->end(); |
| 6780 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6781 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6782 | #if 0 |
| 6783 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6784 | // FIXME |
| 6785 | } |
| 6786 | #endif |
| 6787 | #ifdef AUDIO_WATCHDOG |
| 6788 | // FIXME |
| 6789 | #endif |
| 6790 | } else { |
| 6791 | sq->end(false /*didModify*/); |
| 6792 | } |
| 6793 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6794 | status_t result = mInput->stream->standby(); |
| 6795 | 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] | 6796 | |
| 6797 | // If going into standby, flush the pipe source. |
| 6798 | if (mPipeSource.get() != nullptr) { |
| 6799 | const ssize_t flushed = mPipeSource->flush(); |
| 6800 | if (flushed > 0) { |
| 6801 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); |
| 6802 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; |
| 6803 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6804 | } |
| 6805 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6806 | } |
| 6807 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6808 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6809 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6810 | const sp<AudioFlinger::Client>& client, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6811 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6812 | audio_format_t format, |
| 6813 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6814 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6815 | audio_session_t sessionId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6816 | size_t *pNotificationFrameCount, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6817 | uid_t uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6818 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6819 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6820 | status_t *status, |
| 6821 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6822 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6823 | size_t frameCount = *pFrameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6824 | size_t notificationFrameCount = *pNotificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6825 | sp<RecordTrack> track; |
| 6826 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6827 | audio_input_flags_t inputFlags = mInput->flags; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6828 | audio_input_flags_t requestedFlags = *flags; |
| 6829 | uint32_t sampleRate; |
| 6830 | |
| 6831 | lStatus = initCheck(); |
| 6832 | if (lStatus != NO_ERROR) { |
| 6833 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6834 | goto Exit; |
| 6835 | } |
| 6836 | |
| 6837 | if (*pSampleRate == 0) { |
| 6838 | *pSampleRate = mSampleRate; |
| 6839 | } |
| 6840 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6841 | |
| 6842 | // special case for FAST flag considered OK if fast capture is present |
| 6843 | if (hasFastCapture()) { |
| 6844 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 6845 | } |
| 6846 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6847 | // Check if requested flags are compatible with input stream flags |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6848 | if ((*flags & inputFlags) != *flags) { |
| 6849 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 6850 | " input flags (%08x)", |
| 6851 | *flags, inputFlags); |
| 6852 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 6853 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6854 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6855 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6856 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6857 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6858 | // we formerly checked for a callback handler (non-0 tid), |
| 6859 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6860 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6861 | // frame count is not specified, or is exactly the pipe depth |
| 6862 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6863 | // PCM data |
| 6864 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6865 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6866 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6867 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6868 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6869 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6870 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6871 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6872 | hasFastCapture() && |
| 6873 | // there are sufficient fast track slots available |
| 6874 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6875 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6876 | // check compatibility with audio effects. |
| 6877 | Mutex::Autolock _l(mLock); |
| 6878 | // Do not accept FAST flag if the session has software effects |
| 6879 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 6880 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 6881 | audio_input_flags_t old = *flags; |
| 6882 | chain->checkInputFlagCompatibility(flags); |
| 6883 | if (old != *flags) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6884 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 6885 | this, (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6886 | } |
| 6887 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6888 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6889 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 6890 | this, frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6891 | } else { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6892 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
| 6893 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6894 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6895 | this, frameCount, mFrameCount, mPipeFramesP2, |
| 6896 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6897 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6898 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6899 | } |
| 6900 | } |
| 6901 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6902 | // If FAST or RAW flags were corrected, ask caller to request new input from audio policy |
| 6903 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != |
| 6904 | (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { |
| 6905 | *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); |
| 6906 | lStatus = BAD_TYPE; |
| 6907 | goto Exit; |
| 6908 | } |
| 6909 | |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6910 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6911 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6912 | // fast track: frame count is exactly the pipe depth |
| 6913 | frameCount = mPipeFramesP2; |
| 6914 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6915 | notificationFrameCount = mFrameCount; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6916 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6917 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6918 | // or 20 ms if there is a fast capture |
| 6919 | // TODO This could be a roundupRatio inline, and const |
| 6920 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6921 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6922 | // minimum number of notification periods is at least kMinNotifications, |
| 6923 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6924 | static const size_t kMinNotifications = 3; |
| 6925 | static const uint32_t kMinMs = 30; |
| 6926 | // TODO This could be a roundupRatio inline |
| 6927 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6928 | // TODO This could be a roundupRatio inline |
| 6929 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6930 | maxNotificationFrames; |
| 6931 | const size_t minFrameCount = maxNotificationFrames * |
| 6932 | max(kMinNotifications, minNotificationsByMs); |
| 6933 | frameCount = max(frameCount, minFrameCount); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6934 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 6935 | notificationFrameCount = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6936 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6937 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6938 | *pFrameCount = frameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6939 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6940 | |
| 6941 | { // scope for mLock |
| 6942 | Mutex::Autolock _l(mLock); |
| 6943 | |
| 6944 | track = new RecordTrack(this, client, sampleRate, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 6945 | format, channelMask, frameCount, |
| 6946 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6947 | *flags, TrackBase::TYPE_DEFAULT, portId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6948 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6949 | lStatus = track->initCheck(); |
| 6950 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6951 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6952 | // 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] | 6953 | goto Exit; |
| 6954 | } |
| 6955 | mTracks.add(track); |
| 6956 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6957 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6958 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6959 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6960 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6961 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6962 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6963 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6964 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6965 | lStatus = NO_ERROR; |
| 6966 | |
| 6967 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6968 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6969 | return track; |
| 6970 | } |
| 6971 | |
| 6972 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6973 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6974 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6975 | { |
| 6976 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6977 | sp<ThreadBase> strongMe = this; |
| 6978 | status_t status = NO_ERROR; |
| 6979 | |
| 6980 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6981 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6982 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6983 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6984 | triggerSession, |
| 6985 | recordTrack->sessionId(), |
| 6986 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6987 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6988 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6989 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6990 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6991 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6992 | } else { |
| 6993 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Ivan Lozano | 1b35dd6 | 2017-12-06 16:55:10 -0800 | [diff] [blame] | 6994 | recordTrack->mFramesToDrop = -(ssize_t) |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6995 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6996 | } |
| 6997 | } |
| 6998 | |
| 6999 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7000 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7001 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7002 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 7003 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 7004 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 7005 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7006 | } else { |
| 7007 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7008 | } |
| 7009 | return status; |
| 7010 | } |
| 7011 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7012 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 7013 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 7014 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7015 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7016 | mActiveTracks.add(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7017 | status_t status = NO_ERROR; |
| 7018 | if (recordTrack->isExternalTrack()) { |
| 7019 | mLock.unlock(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7020 | bool silenced; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7021 | status = AudioSystem::startInput(recordTrack->portId(), &silenced); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7022 | mLock.lock(); |
| 7023 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 7024 | if (status != NO_ERROR) { |
| 7025 | mActiveTracks.remove(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7026 | recordTrack->clearSyncStartEvent(); |
| 7027 | ALOGV("RecordThread::start error %d", status); |
| 7028 | return status; |
| 7029 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7030 | recordTrack->setSilenced(silenced); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7031 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7032 | // Catch up with current buffer indices if thread is already running. |
| 7033 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 7034 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 7035 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 7036 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7037 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7038 | // clear any converter state as new data will be discontinuous |
| 7039 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7040 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7041 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7042 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7043 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7044 | ALOGV("Record failed to start"); |
| 7045 | status = BAD_VALUE; |
| 7046 | goto startError; |
| 7047 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7048 | return status; |
| 7049 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 7050 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7051 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7052 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7053 | AudioSystem::stopInput(recordTrack->portId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7054 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7055 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7056 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7057 | return status; |
| 7058 | } |
| 7059 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7060 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 7061 | { |
| 7062 | sp<SyncEvent> strongEvent = event.promote(); |
| 7063 | |
| 7064 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 7065 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 7066 | if (ptr != 0) { |
| 7067 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 7068 | recordTrack->handleSyncStartEvent(strongEvent); |
| 7069 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7070 | } |
| 7071 | } |
| 7072 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 7073 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7074 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 7075 | AutoMutex _l(mLock); |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 7076 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7077 | return false; |
| 7078 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7079 | // 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] | 7080 | recordTrack->mState = TrackBase::PAUSING; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7081 | // signal thread to stop |
| 7082 | mWaitWorkCV.broadcast(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7083 | // do not wait for mStartStopCond if exiting |
| 7084 | if (exitPending()) { |
| 7085 | return true; |
| 7086 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7087 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7088 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7089 | // if we have been restarted, recordTrack is in mActiveTracks here |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 7090 | if (exitPending() || mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7091 | ALOGV("Record stopped OK"); |
| 7092 | return true; |
| 7093 | } |
| 7094 | return false; |
| 7095 | } |
| 7096 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7097 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7098 | { |
| 7099 | return false; |
| 7100 | } |
| 7101 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7102 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7103 | { |
| 7104 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 7105 | if (!isValidSyncEvent(event)) { |
| 7106 | return BAD_VALUE; |
| 7107 | } |
| 7108 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7109 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7110 | status_t ret = NAME_NOT_FOUND; |
| 7111 | |
| 7112 | Mutex::Autolock _l(mLock); |
| 7113 | |
| 7114 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7115 | sp<RecordTrack> track = mTracks[i]; |
| 7116 | if (eventSession == track->sessionId()) { |
| 7117 | (void) track->setSyncEvent(event); |
| 7118 | ret = NO_ERROR; |
| 7119 | } |
| 7120 | } |
| 7121 | return ret; |
| 7122 | #else |
| 7123 | return BAD_VALUE; |
| 7124 | #endif |
| 7125 | } |
| 7126 | |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 7127 | status_t AudioFlinger::RecordThread::getActiveMicrophones( |
| 7128 | std::vector<media::MicrophoneInfo>* activeMicrophones) |
| 7129 | { |
| 7130 | ALOGV("RecordThread::getActiveMicrophones"); |
| 7131 | AutoMutex _l(mLock); |
| 7132 | // Fake data |
| 7133 | struct audio_microphone_characteristic_t characteristic; |
| 7134 | sprintf(characteristic.device_id, "builtin_mic"); |
rago | 1de79cf | 2018-02-01 15:21:02 -0800 | [diff] [blame] | 7135 | characteristic.device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 7136 | sprintf(characteristic.address, ""); |
| 7137 | characteristic.location = AUDIO_MICROPHONE_LOCATION_MAINBODY; |
| 7138 | characteristic.group = 0; |
| 7139 | characteristic.index_in_the_group = 0; |
| 7140 | characteristic.sensitivity = 1.0f; |
| 7141 | characteristic.max_spl = 100.0f; |
| 7142 | characteristic.min_spl = 0.0f; |
| 7143 | characteristic.directionality = AUDIO_MICROPHONE_DIRECTIONALITY_OMNI; |
| 7144 | characteristic.num_frequency_responses = 5; |
| 7145 | for (size_t i = 0; i < characteristic.num_frequency_responses; i++) { |
| 7146 | characteristic.frequency_responses[0][i] = 100.0f - i; |
| 7147 | characteristic.frequency_responses[1][i] = 100.0f + i; |
| 7148 | } |
| 7149 | for (size_t i = 0; i < AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 7150 | characteristic.channel_mapping[i] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED; |
| 7151 | } |
| 7152 | audio_microphone_channel_mapping_t channel_mappings[] = { |
| 7153 | AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT, |
| 7154 | AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED, |
| 7155 | }; |
| 7156 | for (size_t i = 0; i < mChannelCount; i++) { |
| 7157 | characteristic.channel_mapping[i] = channel_mappings[i % 2]; |
| 7158 | } |
| 7159 | characteristic.geometric_location.x = 0.1f; |
| 7160 | characteristic.geometric_location.y = 0.2f; |
| 7161 | characteristic.geometric_location.z = 0.3f; |
| 7162 | characteristic.orientation.x = 0.0f; |
| 7163 | characteristic.orientation.y = 1.0f; |
| 7164 | characteristic.orientation.z = 0.0f; |
| 7165 | media::MicrophoneInfo microphoneInfo = media::MicrophoneInfo(characteristic); |
| 7166 | activeMicrophones->push_back(microphoneInfo); |
| 7167 | return NO_ERROR; |
| 7168 | } |
| 7169 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7170 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 7171 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 7172 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7173 | track->terminate(); |
| 7174 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7175 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7176 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7177 | removeTrack_l(track); |
| 7178 | } |
| 7179 | } |
| 7180 | |
| 7181 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 7182 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7183 | String8 result; |
| 7184 | track->appendDump(result, false /* active */); |
| 7185 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
| 7186 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7187 | mTracks.remove(track); |
| 7188 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7189 | if (track->isFastTrack()) { |
| 7190 | ALOG_ASSERT(!mFastTrackAvail); |
| 7191 | mFastTrackAvail = true; |
| 7192 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7193 | } |
| 7194 | |
| 7195 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 7196 | { |
| 7197 | dumpInternals(fd, args); |
| 7198 | dumpTracks(fd, args); |
| 7199 | dumpEffectChains(fd, args); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7200 | dprintf(fd, " Local log:\n"); |
| 7201 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7202 | } |
| 7203 | |
| 7204 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 7205 | { |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 7206 | dumpBase(fd, args); |
| 7207 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 7208 | AudioStreamIn *input = mInput; |
| 7209 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; |
| 7210 | dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n", |
| 7211 | input, flags, inputFlagsToString(flags).c_str()); |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 7212 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 7213 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7214 | } |
Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 7215 | |
| 7216 | if (input != nullptr) { |
| 7217 | dprintf(fd, " Hal stream dump:\n"); |
| 7218 | (void)input->stream->dump(fd); |
| 7219 | } |
| 7220 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 7221 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7222 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 7223 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 7224 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 7225 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 7226 | // This is a large object so we place it on the heap. |
| 7227 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 7228 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 7229 | copy->dump(fd); |
| 7230 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7231 | } |
| 7232 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7233 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7234 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7235 | String8 result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7236 | size_t numtracks = mTracks.size(); |
| 7237 | size_t numactive = mActiveTracks.size(); |
| 7238 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7239 | dprintf(fd, " %zu Tracks", numtracks); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7240 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7241 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7242 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7243 | result.append(prefix); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7244 | RecordTrack::appendDumpHeader(result); |
| 7245 | for (size_t i = 0; i < numtracks ; ++i) { |
| 7246 | sp<RecordTrack> track = mTracks[i]; |
| 7247 | if (track != 0) { |
| 7248 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 7249 | if (active) { |
| 7250 | numactiveseen++; |
| 7251 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7252 | result.append(prefix); |
| 7253 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7254 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7255 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7256 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 7257 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7258 | } |
| 7259 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7260 | if (numactiveseen != numactive) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7261 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7262 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7263 | result.append(prefix); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7264 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7265 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7266 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7267 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7268 | result.append(prefix); |
| 7269 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7270 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7271 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7272 | |
| 7273 | } |
| 7274 | write(fd, result.string(), result.size()); |
| 7275 | } |
| 7276 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7277 | void AudioFlinger::RecordThread::setRecordSilenced(uid_t uid, bool silenced) |
| 7278 | { |
| 7279 | Mutex::Autolock _l(mLock); |
| 7280 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 7281 | sp<RecordTrack> track = mTracks[i]; |
| 7282 | if (track != 0 && track->uid() == uid) { |
| 7283 | track->setSilenced(silenced); |
| 7284 | } |
| 7285 | } |
| 7286 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7287 | |
| 7288 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 7289 | { |
| 7290 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 7291 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7292 | mRsmpInFront = recordThread->mRsmpInRear; |
| 7293 | mRsmpInUnrel = 0; |
| 7294 | } |
| 7295 | |
| 7296 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 7297 | size_t *framesAvailable, bool *hasOverrun) |
| 7298 | { |
| 7299 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 7300 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7301 | const int32_t rear = recordThread->mRsmpInRear; |
| 7302 | const int32_t front = mRsmpInFront; |
| 7303 | const ssize_t filled = rear - front; |
| 7304 | |
| 7305 | size_t framesIn; |
| 7306 | bool overrun = false; |
| 7307 | if (filled < 0) { |
| 7308 | // should not happen, but treat like a massive overrun and re-sync |
| 7309 | framesIn = 0; |
| 7310 | mRsmpInFront = rear; |
| 7311 | overrun = true; |
| 7312 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 7313 | framesIn = (size_t) filled; |
| 7314 | } else { |
| 7315 | // client is not keeping up with server, but give it latest data |
| 7316 | framesIn = recordThread->mRsmpInFrames; |
| 7317 | mRsmpInFront = /* front = */ rear - framesIn; |
| 7318 | overrun = true; |
| 7319 | } |
| 7320 | if (framesAvailable != NULL) { |
| 7321 | *framesAvailable = framesIn; |
| 7322 | } |
| 7323 | if (hasOverrun != NULL) { |
| 7324 | *hasOverrun = overrun; |
| 7325 | } |
| 7326 | } |
| 7327 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7328 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7329 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7330 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7331 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7332 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7333 | if (threadBase == 0) { |
| 7334 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7335 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7336 | return NOT_ENOUGH_DATA; |
| 7337 | } |
| 7338 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 7339 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7340 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7341 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7342 | // FIXME should not be P2 (don't want to increase latency) |
| 7343 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7344 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7345 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7346 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 7347 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7348 | if (part1 > (size_t) filled) { |
| 7349 | part1 = filled; |
| 7350 | } |
| 7351 | size_t ask = buffer->frameCount; |
| 7352 | ALOG_ASSERT(ask > 0); |
| 7353 | if (part1 > ask) { |
| 7354 | part1 = ask; |
| 7355 | } |
| 7356 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7357 | // 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] | 7358 | buffer->raw = NULL; |
| 7359 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7360 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7361 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7362 | } |
| 7363 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7364 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7365 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7366 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7367 | return NO_ERROR; |
| 7368 | } |
| 7369 | |
| 7370 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7371 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 7372 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7373 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7374 | size_t stepCount = buffer->frameCount; |
| 7375 | if (stepCount == 0) { |
| 7376 | return; |
| 7377 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7378 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 7379 | mRsmpInUnrel -= stepCount; |
| 7380 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7381 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7382 | buffer->frameCount = 0; |
| 7383 | } |
| 7384 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7385 | void AudioFlinger::RecordThread::checkBtNrec() |
| 7386 | { |
| 7387 | Mutex::Autolock _l(mLock); |
| 7388 | checkBtNrec_l(); |
| 7389 | } |
| 7390 | |
| 7391 | void AudioFlinger::RecordThread::checkBtNrec_l() |
| 7392 | { |
| 7393 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7394 | // pre processings |
| 7395 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7396 | mAudioFlinger->btNrecIsOff(); |
| 7397 | if (mBtNrecSuspended.exchange(suspend) != suspend) { |
| 7398 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7399 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); |
| 7400 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); |
| 7401 | } |
| 7402 | } |
| 7403 | } |
| 7404 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7405 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7406 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 7407 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7408 | { |
| 7409 | bool reconfig = false; |
| 7410 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7411 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7412 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7413 | audio_format_t reqFormat = mFormat; |
| 7414 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7415 | // 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] | 7416 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 7417 | |
| 7418 | AudioParameter param = AudioParameter(keyValuePair); |
| 7419 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7420 | |
| 7421 | // scope for AutoPark extends to end of method |
| 7422 | AutoPark<FastCapture> park(mFastCapture); |
| 7423 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7424 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 7425 | // channel count change can be requested. Do we mandate the first client defines the |
| 7426 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 7427 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 7428 | samplingRate = value; |
| 7429 | reconfig = true; |
| 7430 | } |
| 7431 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7432 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7433 | status = BAD_VALUE; |
| 7434 | } else { |
| 7435 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7436 | reconfig = true; |
| 7437 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7438 | } |
| 7439 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 7440 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7441 | if (!audio_is_input_channel(mask) || |
| 7442 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7443 | status = BAD_VALUE; |
| 7444 | } else { |
| 7445 | channelMask = mask; |
| 7446 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7447 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7448 | } |
| 7449 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7450 | // do not accept frame count changes if tracks are open as the track buffer |
| 7451 | // size depends on frame count and correct behavior would not be guaranteed |
| 7452 | // if frame count is changed after track creation |
| 7453 | if (mActiveTracks.size() > 0) { |
| 7454 | status = INVALID_OPERATION; |
| 7455 | } else { |
| 7456 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7457 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7458 | } |
| 7459 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 7460 | // forward device change to effects that have requested to be |
| 7461 | // aware of attached audio device. |
| 7462 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7463 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7464 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7465 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7466 | // store input device and output device but do not forward output device to audio HAL. |
| 7467 | // Note that status is ignored by the caller for output device |
| 7468 | // (see AudioFlinger::setParameters() |
| 7469 | if (audio_is_output_devices(value)) { |
| 7470 | mOutDevice = value; |
| 7471 | status = BAD_VALUE; |
| 7472 | } else { |
| 7473 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7474 | if (value != AUDIO_DEVICE_NONE) { |
| 7475 | mPrevInDevice = value; |
| 7476 | } |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7477 | checkBtNrec_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7478 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7479 | } |
| 7480 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7481 | mAudioSource != (audio_source_t)value) { |
| 7482 | // forward device change to effects that have requested to be |
| 7483 | // aware of attached audio device. |
| 7484 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7485 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7486 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7487 | mAudioSource = (audio_source_t)value; |
| 7488 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7489 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7490 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7491 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7492 | if (status == INVALID_OPERATION) { |
| 7493 | inputStandBy(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7494 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7495 | } |
| 7496 | if (reconfig) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7497 | if (status == BAD_VALUE) { |
| 7498 | uint32_t sRate; |
| 7499 | audio_channel_mask_t channelMask; |
| 7500 | audio_format_t format; |
| 7501 | if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK && |
| 7502 | audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) && |
| 7503 | sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && |
| 7504 | audio_channel_count_from_in_mask(channelMask) <= FCC_8) { |
| 7505 | status = NO_ERROR; |
| 7506 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7507 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7508 | if (status == NO_ERROR) { |
| 7509 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7510 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7511 | } |
| 7512 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7513 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7514 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7515 | return reconfig; |
| 7516 | } |
| 7517 | |
| 7518 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7519 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7520 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7521 | if (initCheck() == NO_ERROR) { |
| 7522 | String8 out_s8; |
| 7523 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { |
| 7524 | return out_s8; |
| 7525 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7526 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7527 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7528 | } |
| 7529 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7530 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7531 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7532 | |
| 7533 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7534 | |
| 7535 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7536 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 7537 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7538 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7539 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7540 | desc->mChannelMask = mChannelMask; |
| 7541 | desc->mSamplingRate = mSampleRate; |
| 7542 | desc->mFormat = mFormat; |
| 7543 | desc->mFrameCount = mFrameCount; |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7544 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7545 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7546 | break; |
| 7547 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7548 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7549 | default: |
| 7550 | break; |
| 7551 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7552 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7553 | } |
| 7554 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7555 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7556 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7557 | status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 7558 | 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] | 7559 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7560 | 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] | 7561 | mFormat = mHALFormat; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7562 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 7563 | result = mInput->stream->getFrameSize(&mFrameSize); |
| 7564 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
| 7565 | result = mInput->stream->getBufferSize(&mBufferSize); |
| 7566 | 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] | 7567 | mFrameCount = mBufferSize / mFrameSize; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7568 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, " |
| 7569 | "mBufferSize=%lld, mFrameCount=%lld", |
| 7570 | this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize, |
| 7571 | (long long)mFrameCount); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7572 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7573 | // 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] | 7574 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7575 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7576 | // A larger value should allow more old data to be read after a track calls start(), |
| 7577 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7578 | // |
| 7579 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7580 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7581 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7582 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7583 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7584 | |
| 7585 | // TODO optimize audio capture buffer sizes ... |
| 7586 | // Here we calculate the size of the sliding buffer used as a source |
| 7587 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7588 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7589 | // be better to have it derived from the pipe depth in the long term. |
| 7590 | // The current value is higher than necessary. However it should not add to latency. |
| 7591 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7592 | // 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] | 7593 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; |
| 7594 | (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7595 | // if posix_memalign fails, will segv here. |
| 7596 | memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7597 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7598 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7599 | // 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] | 7600 | } |
| 7601 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7602 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7603 | { |
| 7604 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7605 | uint32_t result; |
| 7606 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { |
| 7607 | return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7608 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7609 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7610 | } |
| 7611 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7612 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 7613 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7614 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7615 | uint32_t result = 0; |
| 7616 | if (getEffectChain_l(sessionId) != 0) { |
| 7617 | result = EFFECT_SESSION; |
| 7618 | } |
| 7619 | |
| 7620 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7621 | if (sessionId == mTracks[i]->sessionId()) { |
| 7622 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7623 | if (mTracks[i]->isFastTrack()) { |
| 7624 | result |= FAST_SESSION; |
| 7625 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7626 | break; |
| 7627 | } |
| 7628 | } |
| 7629 | |
| 7630 | return result; |
| 7631 | } |
| 7632 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7633 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7634 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7635 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7636 | Mutex::Autolock _l(mLock); |
| 7637 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7638 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7639 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7640 | if (ids.indexOfKey(sessionId) < 0) { |
| 7641 | ids.add(sessionId, true); |
| 7642 | } |
| 7643 | } |
| 7644 | return ids; |
| 7645 | } |
| 7646 | |
| 7647 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7648 | { |
| 7649 | Mutex::Autolock _l(mLock); |
| 7650 | AudioStreamIn *input = mInput; |
| 7651 | mInput = NULL; |
| 7652 | return input; |
| 7653 | } |
| 7654 | |
| 7655 | // 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] | 7656 | sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7657 | { |
| 7658 | if (mInput == NULL) { |
| 7659 | return NULL; |
| 7660 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7661 | return mInput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7665 | { |
| 7666 | // only one chain per input thread |
| 7667 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7668 | 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] | 7669 | return INVALID_OPERATION; |
| 7670 | } |
| 7671 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7672 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7673 | chain->setInBuffer(NULL); |
| 7674 | chain->setOutBuffer(NULL); |
| 7675 | |
| 7676 | checkSuspendOnAddEffectChain_l(chain); |
| 7677 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7678 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7679 | // just moved them to a new input stream. |
| 7680 | chain->syncHalEffectsState(); |
| 7681 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7682 | mEffectChains.add(chain); |
| 7683 | |
| 7684 | return NO_ERROR; |
| 7685 | } |
| 7686 | |
| 7687 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7688 | { |
| 7689 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7690 | ALOGW_IF(mEffectChains.size() != 1, |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7691 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7692 | chain.get(), mEffectChains.size(), this); |
| 7693 | if (mEffectChains.size() == 1) { |
| 7694 | mEffectChains.removeAt(0); |
| 7695 | } |
| 7696 | return 0; |
| 7697 | } |
| 7698 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7699 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7700 | audio_patch_handle_t *handle) |
| 7701 | { |
| 7702 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7703 | |
| 7704 | // store new device and send to effects |
| 7705 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7706 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7707 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7708 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7709 | } |
| 7710 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7711 | checkBtNrec_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7712 | |
| 7713 | // store new source and send to effects |
| 7714 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7715 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7716 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7717 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7718 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7719 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7720 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7721 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7722 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 7723 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 7724 | patch->sources, |
| 7725 | patch->num_sinks, |
| 7726 | patch->sinks, |
| 7727 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7728 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7729 | char *address; |
| 7730 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7731 | address = audio_device_address_to_parameter( |
| 7732 | patch->sources[0].ext.device.type, |
| 7733 | patch->sources[0].ext.device.address); |
| 7734 | } else { |
| 7735 | address = (char *)calloc(1, 1); |
| 7736 | } |
| 7737 | AudioParameter param = AudioParameter(String8(address)); |
| 7738 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7739 | param.addInt(String8(AudioParameter::keyRouting), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7740 | (int)patch->sources[0].ext.device.type); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7741 | param.addInt(String8(AudioParameter::keyInputSource), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7742 | (int)patch->sinks[0].ext.mix.usecase.source); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7743 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7744 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7745 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7746 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7747 | if (mInDevice != mPrevInDevice) { |
| 7748 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7749 | mPrevInDevice = mInDevice; |
| 7750 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7751 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7752 | return status; |
| 7753 | } |
| 7754 | |
| 7755 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7756 | { |
| 7757 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7758 | |
| 7759 | mInDevice = AUDIO_DEVICE_NONE; |
| 7760 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7761 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7762 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 7763 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7764 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7765 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7766 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7767 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7768 | } |
| 7769 | return status; |
| 7770 | } |
| 7771 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7772 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7773 | { |
| 7774 | Mutex::Autolock _l(mLock); |
| 7775 | mTracks.add(record); |
| 7776 | } |
| 7777 | |
| 7778 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7779 | { |
| 7780 | Mutex::Autolock _l(mLock); |
| 7781 | destroyTrack_l(record); |
| 7782 | } |
| 7783 | |
| 7784 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7785 | { |
| 7786 | ThreadBase::getAudioPortConfig(config); |
| 7787 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7788 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7789 | config->ext.mix.usecase.source = mAudioSource; |
| 7790 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7791 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7792 | // ---------------------------------------------------------------------------- |
| 7793 | // Mmap |
| 7794 | // ---------------------------------------------------------------------------- |
| 7795 | |
| 7796 | AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread) |
| 7797 | : mThread(thread) |
| 7798 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7799 | assert(thread != 0); // thread must start non-null and stay non-null |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7800 | } |
| 7801 | |
| 7802 | AudioFlinger::MmapThreadHandle::~MmapThreadHandle() |
| 7803 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7804 | mThread->disconnect(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, |
| 7808 | struct audio_mmap_buffer_info *info) |
| 7809 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7810 | return mThread->createMmapBuffer(minSizeFrames, info); |
| 7811 | } |
| 7812 | |
| 7813 | status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position) |
| 7814 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7815 | return mThread->getMmapPosition(position); |
| 7816 | } |
| 7817 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7818 | status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client, |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7819 | audio_port_handle_t *handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7820 | |
| 7821 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7822 | return mThread->start(client, handle); |
| 7823 | } |
| 7824 | |
| 7825 | status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle) |
| 7826 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7827 | return mThread->stop(handle); |
| 7828 | } |
| 7829 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7830 | status_t AudioFlinger::MmapThreadHandle::standby() |
| 7831 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7832 | return mThread->standby(); |
| 7833 | } |
| 7834 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7835 | |
| 7836 | AudioFlinger::MmapThread::MmapThread( |
| 7837 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 7838 | AudioHwDevice *hwDev, sp<StreamHalInterface> stream, |
| 7839 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 7840 | : ThreadBase(audioFlinger, id, outDevice, inDevice, MMAP, systemReady), |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7841 | mSessionId(AUDIO_SESSION_NONE), |
| 7842 | mDeviceId(AUDIO_PORT_HANDLE_NONE), mPortId(AUDIO_PORT_HANDLE_NONE), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7843 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), |
| 7844 | mActiveTracks(&this->mLocalLog) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7845 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7846 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7847 | readHalParameters_l(); |
| 7848 | } |
| 7849 | |
| 7850 | AudioFlinger::MmapThread::~MmapThread() |
| 7851 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7852 | releaseWakeLock_l(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7853 | } |
| 7854 | |
| 7855 | void AudioFlinger::MmapThread::onFirstRef() |
| 7856 | { |
| 7857 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 7858 | } |
| 7859 | |
| 7860 | void AudioFlinger::MmapThread::disconnect() |
| 7861 | { |
| 7862 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 7863 | stop(t->portId()); |
| 7864 | } |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7865 | // This will decrement references and may cause the destruction of this thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7866 | if (isOutput()) { |
| 7867 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); |
| 7868 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7869 | AudioSystem::releaseInput(mPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7870 | } |
| 7871 | } |
| 7872 | |
| 7873 | |
| 7874 | void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr, |
| 7875 | audio_stream_type_t streamType __unused, |
| 7876 | audio_session_t sessionId, |
| 7877 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7878 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7879 | audio_port_handle_t portId) |
| 7880 | { |
| 7881 | mAttr = *attr; |
| 7882 | mSessionId = sessionId; |
| 7883 | mCallback = callback; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7884 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7885 | mPortId = portId; |
| 7886 | } |
| 7887 | |
| 7888 | status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames, |
| 7889 | struct audio_mmap_buffer_info *info) |
| 7890 | { |
| 7891 | if (mHalStream == 0) { |
| 7892 | return NO_INIT; |
| 7893 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7894 | mStandby = true; |
| 7895 | acquireWakeLock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7896 | return mHalStream->createMmapBuffer(minSizeFrames, info); |
| 7897 | } |
| 7898 | |
| 7899 | status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position) |
| 7900 | { |
| 7901 | if (mHalStream == 0) { |
| 7902 | return NO_INIT; |
| 7903 | } |
| 7904 | return mHalStream->getMmapPosition(position); |
| 7905 | } |
| 7906 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7907 | status_t AudioFlinger::MmapThread::start(const AudioClient& client, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7908 | audio_port_handle_t *handle) |
| 7909 | { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7910 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, |
| 7911 | client.clientUid, mStandby, mPortId, *handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7912 | if (mHalStream == 0) { |
| 7913 | return NO_INIT; |
| 7914 | } |
| 7915 | |
| 7916 | status_t ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7917 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7918 | if (*handle == mPortId) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7919 | // for the first track, reuse portId and session allocated when the stream was opened |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7920 | ret = mHalStream->start(); |
| 7921 | if (ret != NO_ERROR) { |
| 7922 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); |
| 7923 | return ret; |
| 7924 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7925 | mStandby = false; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7926 | return NO_ERROR; |
| 7927 | } |
| 7928 | |
| 7929 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 7930 | |
| 7931 | audio_io_handle_t io = mId; |
| 7932 | if (isOutput()) { |
| 7933 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 7934 | config.sample_rate = mSampleRate; |
| 7935 | config.channel_mask = mChannelMask; |
| 7936 | config.format = mFormat; |
| 7937 | audio_stream_type_t stream = streamType(); |
| 7938 | audio_output_flags_t flags = |
| 7939 | (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] | 7940 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7941 | ret = AudioSystem::getOutputForAttr(&mAttr, &io, |
| 7942 | mSessionId, |
| 7943 | &stream, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 7944 | client.clientPid, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7945 | client.clientUid, |
| 7946 | &config, |
| 7947 | flags, |
| 7948 | &deviceId, |
| 7949 | &portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7950 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7951 | audio_config_base_t config; |
| 7952 | config.sample_rate = mSampleRate; |
| 7953 | config.channel_mask = mChannelMask; |
| 7954 | config.format = mFormat; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7955 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7956 | ret = AudioSystem::getInputForAttr(&mAttr, &io, |
| 7957 | mSessionId, |
| 7958 | client.clientPid, |
| 7959 | client.clientUid, |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7960 | client.packageName, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7961 | &config, |
| 7962 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, |
| 7963 | &deviceId, |
| 7964 | &portId); |
| 7965 | } |
| 7966 | // APM should not chose a different input or output stream for the same set of attributes |
| 7967 | // and audo configuration |
| 7968 | if (ret != NO_ERROR || io != mId) { |
| 7969 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", |
| 7970 | __FUNCTION__, ret, io, mId); |
| 7971 | return BAD_VALUE; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7972 | } |
| 7973 | |
| 7974 | if (isOutput()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7975 | ret = AudioSystem::startOutput(mId, streamType(), mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7976 | } else { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7977 | // TODO: Block recording for idle UIDs (b/72134552) |
| 7978 | bool silenced; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7979 | ret = AudioSystem::startInput(portId, &silenced); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | // abort if start is rejected by audio policy manager |
| 7983 | if (ret != NO_ERROR) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7984 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7985 | if (mActiveTracks.size() != 0) { |
| 7986 | if (isOutput()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7987 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7988 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 7989 | AudioSystem::releaseInput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7990 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7991 | } else { |
| 7992 | mHalStream->stop(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7993 | } |
| 7994 | return PERMISSION_DENIED; |
| 7995 | } |
| 7996 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7997 | sp<MmapTrack> track = new MmapTrack(this, mSampleRate, mFormat, mChannelMask, mSessionId, |
| 7998 | client.clientUid, client.clientPid, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7999 | |
| 8000 | mActiveTracks.add(track); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8001 | sp<EffectChain> chain = getEffectChain_l(mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8002 | if (chain != 0) { |
| 8003 | chain->setStrategy(AudioSystem::getStrategyForStream(streamType())); |
| 8004 | chain->incTrackCnt(); |
| 8005 | chain->incActiveTrackCnt(); |
| 8006 | } |
| 8007 | |
| 8008 | *handle = portId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8009 | broadcast_l(); |
| 8010 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8011 | ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8012 | |
| 8013 | return NO_ERROR; |
| 8014 | } |
| 8015 | |
| 8016 | status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle) |
| 8017 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8018 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 8019 | |
| 8020 | if (mHalStream == 0) { |
| 8021 | return NO_INIT; |
| 8022 | } |
| 8023 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8024 | if (handle == mPortId) { |
| 8025 | mHalStream->stop(); |
| 8026 | return NO_ERROR; |
| 8027 | } |
| 8028 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8029 | sp<MmapTrack> track; |
| 8030 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 8031 | if (handle == t->portId()) { |
| 8032 | track = t; |
| 8033 | break; |
| 8034 | } |
| 8035 | } |
| 8036 | if (track == 0) { |
| 8037 | return BAD_VALUE; |
| 8038 | } |
| 8039 | |
| 8040 | mActiveTracks.remove(track); |
| 8041 | |
| 8042 | if (isOutput()) { |
| 8043 | AudioSystem::stopOutput(mId, streamType(), track->sessionId()); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 8044 | AudioSystem::releaseOutput(mId, streamType(), track->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8045 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 8046 | AudioSystem::stopInput(track->portId()); |
| 8047 | AudioSystem::releaseInput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8048 | } |
| 8049 | |
| 8050 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 8051 | if (chain != 0) { |
| 8052 | chain->decActiveTrackCnt(); |
| 8053 | chain->decTrackCnt(); |
| 8054 | } |
| 8055 | |
| 8056 | broadcast_l(); |
| 8057 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8058 | return NO_ERROR; |
| 8059 | } |
| 8060 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 8061 | status_t AudioFlinger::MmapThread::standby() |
| 8062 | { |
| 8063 | ALOGV("%s", __FUNCTION__); |
| 8064 | |
| 8065 | if (mHalStream == 0) { |
| 8066 | return NO_INIT; |
| 8067 | } |
| 8068 | if (mActiveTracks.size() != 0) { |
| 8069 | return INVALID_OPERATION; |
| 8070 | } |
| 8071 | mHalStream->standby(); |
| 8072 | mStandby = true; |
| 8073 | releaseWakeLock(); |
| 8074 | return NO_ERROR; |
| 8075 | } |
| 8076 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8077 | |
| 8078 | void AudioFlinger::MmapThread::readHalParameters_l() |
| 8079 | { |
| 8080 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 8081 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
| 8082 | mFormat = mHALFormat; |
| 8083 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 8084 | result = mHalStream->getFrameSize(&mFrameSize); |
| 8085 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
| 8086 | result = mHalStream->getBufferSize(&mBufferSize); |
| 8087 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
| 8088 | mFrameCount = mBufferSize / mFrameSize; |
| 8089 | } |
| 8090 | |
| 8091 | bool AudioFlinger::MmapThread::threadLoop() |
| 8092 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8093 | checkSilentMode_l(); |
| 8094 | |
| 8095 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 8096 | |
| 8097 | while (!exitPending()) |
| 8098 | { |
| 8099 | Mutex::Autolock _l(mLock); |
| 8100 | Vector< sp<EffectChain> > effectChains; |
| 8101 | |
| 8102 | if (mSignalPending) { |
| 8103 | // A signal was raised while we were unlocked |
| 8104 | mSignalPending = false; |
| 8105 | } else { |
| 8106 | if (mConfigEvents.isEmpty()) { |
| 8107 | // we're about to wait, flush the binder command buffer |
| 8108 | IPCThreadState::self()->flushCommands(); |
| 8109 | |
| 8110 | if (exitPending()) { |
| 8111 | break; |
| 8112 | } |
| 8113 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8114 | // wait until we have something to do... |
| 8115 | ALOGV("%s going to sleep", myName.string()); |
| 8116 | mWaitWorkCV.wait(mLock); |
| 8117 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8118 | |
| 8119 | checkSilentMode_l(); |
| 8120 | |
| 8121 | continue; |
| 8122 | } |
| 8123 | } |
| 8124 | |
| 8125 | processConfigEvents_l(); |
| 8126 | |
| 8127 | processVolume_l(); |
| 8128 | |
| 8129 | checkInvalidTracks_l(); |
| 8130 | |
| 8131 | mActiveTracks.updatePowerState(this); |
| 8132 | |
| 8133 | lockEffectChains_l(effectChains); |
| 8134 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 8135 | effectChains[i]->process_l(); |
| 8136 | } |
| 8137 | // enable changes in effect chain |
| 8138 | unlockEffectChains(effectChains); |
| 8139 | // Effect chains will be actually deleted here if they were removed from |
| 8140 | // mEffectChains list during mixing or effects processing |
| 8141 | } |
| 8142 | |
| 8143 | threadLoop_exit(); |
| 8144 | |
| 8145 | if (!mStandby) { |
| 8146 | threadLoop_standby(); |
| 8147 | mStandby = true; |
| 8148 | } |
| 8149 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8150 | ALOGV("Thread %p type %d exiting", this, mType); |
| 8151 | return false; |
| 8152 | } |
| 8153 | |
| 8154 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 8155 | bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair, |
| 8156 | status_t& status) |
| 8157 | { |
| 8158 | AudioParameter param = AudioParameter(keyValuePair); |
| 8159 | int value; |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8160 | bool sendToHal = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8161 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8162 | audio_devices_t device = (audio_devices_t)value; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8163 | // forward device change to effects that have requested to be |
| 8164 | // aware of attached audio device. |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8165 | if (device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8166 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8167 | mEffectChains[i]->setDevice_l(device); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8168 | } |
| 8169 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8170 | if (audio_is_output_devices(device)) { |
| 8171 | mOutDevice = device; |
| 8172 | if (!isOutput()) { |
| 8173 | sendToHal = false; |
| 8174 | } |
| 8175 | } else { |
| 8176 | mInDevice = device; |
| 8177 | if (device != AUDIO_DEVICE_NONE) { |
| 8178 | mPrevInDevice = value; |
| 8179 | } |
| 8180 | // TODO: implement and call checkBtNrec_l(); |
| 8181 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8182 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8183 | if (sendToHal) { |
| 8184 | status = mHalStream->setParameters(keyValuePair); |
| 8185 | } else { |
| 8186 | status = NO_ERROR; |
| 8187 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8188 | |
| 8189 | return false; |
| 8190 | } |
| 8191 | |
| 8192 | String8 AudioFlinger::MmapThread::getParameters(const String8& keys) |
| 8193 | { |
| 8194 | Mutex::Autolock _l(mLock); |
| 8195 | String8 out_s8; |
| 8196 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { |
| 8197 | return out_s8; |
| 8198 | } |
| 8199 | return String8(); |
| 8200 | } |
| 8201 | |
| 8202 | void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
| 8203 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 8204 | |
| 8205 | desc->mIoHandle = mId; |
| 8206 | |
| 8207 | switch (event) { |
| 8208 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8209 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8210 | case AUDIO_INPUT_CONFIG_CHANGED: |
| 8211 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8212 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8213 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 8214 | desc->mPatch = mPatch; |
| 8215 | desc->mChannelMask = mChannelMask; |
| 8216 | desc->mSamplingRate = mSampleRate; |
| 8217 | desc->mFormat = mFormat; |
| 8218 | desc->mFrameCount = mFrameCount; |
| 8219 | desc->mFrameCountHAL = mFrameCount; |
| 8220 | desc->mLatency = 0; |
| 8221 | break; |
| 8222 | |
| 8223 | case AUDIO_INPUT_CLOSED: |
| 8224 | case AUDIO_OUTPUT_CLOSED: |
| 8225 | default: |
| 8226 | break; |
| 8227 | } |
| 8228 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
| 8229 | } |
| 8230 | |
| 8231 | status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch, |
| 8232 | audio_patch_handle_t *handle) |
| 8233 | { |
| 8234 | status_t status = NO_ERROR; |
| 8235 | |
| 8236 | // store new device and send to effects |
| 8237 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 8238 | audio_port_handle_t deviceId; |
| 8239 | if (isOutput()) { |
| 8240 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 8241 | type |= patch->sinks[i].ext.device.type; |
| 8242 | } |
| 8243 | deviceId = patch->sinks[0].id; |
| 8244 | } else { |
| 8245 | type = patch->sources[0].ext.device.type; |
| 8246 | deviceId = patch->sources[0].id; |
| 8247 | } |
| 8248 | |
| 8249 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8250 | mEffectChains[i]->setDevice_l(type); |
| 8251 | } |
| 8252 | |
| 8253 | if (isOutput()) { |
| 8254 | mOutDevice = type; |
| 8255 | } else { |
| 8256 | mInDevice = type; |
| 8257 | // store new source and send to effects |
| 8258 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 8259 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 8260 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8261 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 8262 | } |
| 8263 | } |
| 8264 | } |
| 8265 | |
| 8266 | if (mAudioHwDev->supportsAudioPatches()) { |
| 8267 | status = mHalDevice->createAudioPatch(patch->num_sources, |
| 8268 | patch->sources, |
| 8269 | patch->num_sinks, |
| 8270 | patch->sinks, |
| 8271 | handle); |
| 8272 | } else { |
| 8273 | char *address; |
| 8274 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 8275 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 8276 | address = audio_device_address_to_parameter( |
| 8277 | patch->sinks[0].ext.device.type, |
| 8278 | patch->sinks[0].ext.device.address); |
| 8279 | } else { |
| 8280 | address = (char *)calloc(1, 1); |
| 8281 | } |
| 8282 | AudioParameter param = AudioParameter(String8(address)); |
| 8283 | free(address); |
| 8284 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
| 8285 | if (!isOutput()) { |
| 8286 | param.addInt(String8(AudioParameter::keyInputSource), |
| 8287 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 8288 | } |
| 8289 | status = mHalStream->setParameters(param.toString()); |
| 8290 | *handle = AUDIO_PATCH_HANDLE_NONE; |
| 8291 | } |
| 8292 | |
| 8293 | if (isOutput() && mPrevOutDevice != mOutDevice) { |
| 8294 | mPrevOutDevice = type; |
| 8295 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8296 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8297 | if (mDeviceId != deviceId && callback != 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8298 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8299 | } |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8300 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8301 | } |
| 8302 | if (!isOutput() && mPrevInDevice != mInDevice) { |
| 8303 | mPrevInDevice = type; |
| 8304 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8305 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8306 | if (mDeviceId != deviceId && callback != 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8307 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8308 | } |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8309 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8310 | } |
| 8311 | return status; |
| 8312 | } |
| 8313 | |
| 8314 | status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 8315 | { |
| 8316 | status_t status = NO_ERROR; |
| 8317 | |
| 8318 | mInDevice = AUDIO_DEVICE_NONE; |
| 8319 | |
| 8320 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? |
| 8321 | supportsAudioPatches : false; |
| 8322 | |
| 8323 | if (supportsAudioPatches) { |
| 8324 | status = mHalDevice->releaseAudioPatch(handle); |
| 8325 | } else { |
| 8326 | AudioParameter param; |
| 8327 | param.addInt(String8(AudioParameter::keyRouting), 0); |
| 8328 | status = mHalStream->setParameters(param.toString()); |
| 8329 | } |
| 8330 | return status; |
| 8331 | } |
| 8332 | |
| 8333 | void AudioFlinger::MmapThread::getAudioPortConfig(struct audio_port_config *config) |
| 8334 | { |
| 8335 | ThreadBase::getAudioPortConfig(config); |
| 8336 | if (isOutput()) { |
| 8337 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 8338 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 8339 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 8340 | } else { |
| 8341 | config->role = AUDIO_PORT_ROLE_SINK; |
| 8342 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 8343 | config->ext.mix.usecase.source = mAudioSource; |
| 8344 | } |
| 8345 | } |
| 8346 | |
| 8347 | status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 8348 | { |
| 8349 | audio_session_t session = chain->sessionId(); |
| 8350 | |
| 8351 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 8352 | // Attach all tracks with same session ID to this chain. |
| 8353 | // indicate all active tracks in the chain |
| 8354 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8355 | if (session == track->sessionId()) { |
| 8356 | chain->incTrackCnt(); |
| 8357 | chain->incActiveTrackCnt(); |
| 8358 | } |
| 8359 | } |
| 8360 | |
| 8361 | chain->setThread(this); |
| 8362 | chain->setInBuffer(nullptr); |
| 8363 | chain->setOutBuffer(nullptr); |
| 8364 | chain->syncHalEffectsState(); |
| 8365 | |
| 8366 | mEffectChains.add(chain); |
| 8367 | checkSuspendOnAddEffectChain_l(chain); |
| 8368 | return NO_ERROR; |
| 8369 | } |
| 8370 | |
| 8371 | size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 8372 | { |
| 8373 | audio_session_t session = chain->sessionId(); |
| 8374 | |
| 8375 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 8376 | |
| 8377 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8378 | if (chain == mEffectChains[i]) { |
| 8379 | mEffectChains.removeAt(i); |
| 8380 | // detach all active tracks from the chain |
| 8381 | // detach all tracks with same session ID from this chain |
| 8382 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8383 | if (session == track->sessionId()) { |
| 8384 | chain->decActiveTrackCnt(); |
| 8385 | chain->decTrackCnt(); |
| 8386 | } |
| 8387 | } |
| 8388 | break; |
| 8389 | } |
| 8390 | } |
| 8391 | return mEffectChains.size(); |
| 8392 | } |
| 8393 | |
| 8394 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 8395 | uint32_t AudioFlinger::MmapThread::hasAudioSession_l(audio_session_t sessionId) const |
| 8396 | { |
| 8397 | uint32_t result = 0; |
| 8398 | if (getEffectChain_l(sessionId) != 0) { |
| 8399 | result = EFFECT_SESSION; |
| 8400 | } |
| 8401 | |
| 8402 | for (size_t i = 0; i < mActiveTracks.size(); i++) { |
| 8403 | sp<MmapTrack> track = mActiveTracks[i]; |
| 8404 | if (sessionId == track->sessionId()) { |
| 8405 | result |= TRACK_SESSION; |
| 8406 | if (track->isFastTrack()) { |
| 8407 | result |= FAST_SESSION; |
| 8408 | } |
| 8409 | break; |
| 8410 | } |
| 8411 | } |
| 8412 | |
| 8413 | return result; |
| 8414 | } |
| 8415 | |
| 8416 | void AudioFlinger::MmapThread::threadLoop_standby() |
| 8417 | { |
| 8418 | mHalStream->standby(); |
| 8419 | } |
| 8420 | |
| 8421 | void AudioFlinger::MmapThread::threadLoop_exit() |
| 8422 | { |
Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 8423 | // Do not call callback->onTearDown() because it is redundant for thread exit |
| 8424 | // and because it can cause a recursive mutex lock on stop(). |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8425 | } |
| 8426 | |
| 8427 | status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
| 8428 | { |
| 8429 | return BAD_VALUE; |
| 8430 | } |
| 8431 | |
| 8432 | bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
| 8433 | { |
| 8434 | return false; |
| 8435 | } |
| 8436 | |
| 8437 | status_t AudioFlinger::MmapThread::checkEffectCompatibility_l( |
| 8438 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 8439 | { |
| 8440 | // No global effect sessions on mmap threads |
| 8441 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 8442 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 8443 | desc->name, mThreadName); |
| 8444 | return BAD_VALUE; |
| 8445 | } |
| 8446 | |
| 8447 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 8448 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", |
| 8449 | desc->name); |
| 8450 | return BAD_VALUE; |
| 8451 | } |
| 8452 | 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] | 8453 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " |
| 8454 | "thread", desc->name); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8455 | return BAD_VALUE; |
| 8456 | } |
| 8457 | |
| 8458 | // Only allow effects without processing load or latency |
| 8459 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { |
| 8460 | return BAD_VALUE; |
| 8461 | } |
| 8462 | |
| 8463 | return NO_ERROR; |
| 8464 | |
| 8465 | } |
| 8466 | |
| 8467 | void AudioFlinger::MmapThread::checkInvalidTracks_l() |
| 8468 | { |
| 8469 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8470 | if (track->isInvalid()) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8471 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8472 | if (callback != 0) { |
| 8473 | callback->onTearDown(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8474 | } |
| 8475 | break; |
| 8476 | } |
| 8477 | } |
| 8478 | } |
| 8479 | |
| 8480 | void AudioFlinger::MmapThread::dump(int fd, const Vector<String16>& args) |
| 8481 | { |
| 8482 | dumpInternals(fd, args); |
| 8483 | dumpTracks(fd, args); |
| 8484 | dumpEffectChains(fd, args); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8485 | dprintf(fd, " Local log:\n"); |
| 8486 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8487 | } |
| 8488 | |
| 8489 | void AudioFlinger::MmapThread::dumpInternals(int fd, const Vector<String16>& args) |
| 8490 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8491 | dumpBase(fd, args); |
| 8492 | |
| 8493 | dprintf(fd, " Attributes: content type %d usage %d source %d\n", |
| 8494 | mAttr.content_type, mAttr.usage, mAttr.source); |
| 8495 | dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId); |
| 8496 | if (mActiveTracks.size() == 0) { |
| 8497 | dprintf(fd, " No active clients\n"); |
| 8498 | } |
| 8499 | } |
| 8500 | |
| 8501 | void AudioFlinger::MmapThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
| 8502 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8503 | String8 result; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8504 | size_t numtracks = mActiveTracks.size(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8505 | dprintf(fd, " %zu Tracks\n", numtracks); |
| 8506 | const char *prefix = " "; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8507 | if (numtracks) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8508 | result.append(prefix); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8509 | MmapTrack::appendDumpHeader(result); |
| 8510 | for (size_t i = 0; i < numtracks ; ++i) { |
| 8511 | sp<MmapTrack> track = mActiveTracks[i]; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8512 | result.append(prefix); |
| 8513 | track->appendDump(result, true /* active */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8514 | } |
| 8515 | } else { |
| 8516 | dprintf(fd, "\n"); |
| 8517 | } |
| 8518 | write(fd, result.string(), result.size()); |
| 8519 | } |
| 8520 | |
| 8521 | AudioFlinger::MmapPlaybackThread::MmapPlaybackThread( |
| 8522 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 8523 | AudioHwDevice *hwDev, AudioStreamOut *output, |
| 8524 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 8525 | : MmapThread(audioFlinger, id, hwDev, output->stream, outDevice, inDevice, systemReady), |
| 8526 | mStreamType(AUDIO_STREAM_MUSIC), |
| 8527 | mStreamVolume(1.0), mStreamMute(false), mOutput(output) |
| 8528 | { |
| 8529 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); |
| 8530 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 8531 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 8532 | mMasterMute = audioFlinger->masterMute_l(); |
| 8533 | if (mAudioHwDev) { |
| 8534 | if (mAudioHwDev->canSetMasterVolume()) { |
| 8535 | mMasterVolume = 1.0; |
| 8536 | } |
| 8537 | |
| 8538 | if (mAudioHwDev->canSetMasterMute()) { |
| 8539 | mMasterMute = false; |
| 8540 | } |
| 8541 | } |
| 8542 | } |
| 8543 | |
| 8544 | void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr, |
| 8545 | audio_stream_type_t streamType, |
| 8546 | audio_session_t sessionId, |
| 8547 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8548 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8549 | audio_port_handle_t portId) |
| 8550 | { |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8551 | MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8552 | mStreamType = streamType; |
| 8553 | } |
| 8554 | |
| 8555 | AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput() |
| 8556 | { |
| 8557 | Mutex::Autolock _l(mLock); |
| 8558 | AudioStreamOut *output = mOutput; |
| 8559 | mOutput = NULL; |
| 8560 | return output; |
| 8561 | } |
| 8562 | |
| 8563 | void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value) |
| 8564 | { |
| 8565 | Mutex::Autolock _l(mLock); |
| 8566 | // Don't apply master volume in SW if our HAL can do it for us. |
| 8567 | if (mAudioHwDev && |
| 8568 | mAudioHwDev->canSetMasterVolume()) { |
| 8569 | mMasterVolume = 1.0; |
| 8570 | } else { |
| 8571 | mMasterVolume = value; |
| 8572 | } |
| 8573 | } |
| 8574 | |
| 8575 | void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted) |
| 8576 | { |
| 8577 | Mutex::Autolock _l(mLock); |
| 8578 | // Don't apply master mute in SW if our HAL can do it for us. |
| 8579 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { |
| 8580 | mMasterMute = false; |
| 8581 | } else { |
| 8582 | mMasterMute = muted; |
| 8583 | } |
| 8584 | } |
| 8585 | |
| 8586 | void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 8587 | { |
| 8588 | Mutex::Autolock _l(mLock); |
| 8589 | if (stream == mStreamType) { |
| 8590 | mStreamVolume = value; |
| 8591 | broadcast_l(); |
| 8592 | } |
| 8593 | } |
| 8594 | |
| 8595 | float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 8596 | { |
| 8597 | Mutex::Autolock _l(mLock); |
| 8598 | if (stream == mStreamType) { |
| 8599 | return mStreamVolume; |
| 8600 | } |
| 8601 | return 0.0f; |
| 8602 | } |
| 8603 | |
| 8604 | void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 8605 | { |
| 8606 | Mutex::Autolock _l(mLock); |
| 8607 | if (stream == mStreamType) { |
| 8608 | mStreamMute= muted; |
| 8609 | broadcast_l(); |
| 8610 | } |
| 8611 | } |
| 8612 | |
| 8613 | void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 8614 | { |
| 8615 | Mutex::Autolock _l(mLock); |
| 8616 | if (streamType == mStreamType) { |
| 8617 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 8618 | track->invalidate(); |
| 8619 | } |
| 8620 | broadcast_l(); |
| 8621 | } |
| 8622 | } |
| 8623 | |
| 8624 | void AudioFlinger::MmapPlaybackThread::processVolume_l() |
| 8625 | { |
| 8626 | float volume; |
| 8627 | |
| 8628 | if (mMasterMute || mStreamMute) { |
| 8629 | volume = 0; |
| 8630 | } else { |
| 8631 | volume = mMasterVolume * mStreamVolume; |
| 8632 | } |
| 8633 | |
| 8634 | if (volume != mHalVolFloat) { |
| 8635 | mHalVolFloat = volume; |
| 8636 | |
| 8637 | // Convert volumes from float to 8.24 |
| 8638 | uint32_t vol = (uint32_t)(volume * (1 << 24)); |
| 8639 | |
| 8640 | // Delegate volume control to effect in track effect chain if needed |
| 8641 | // only one effect chain can be present on DirectOutputThread, so if |
| 8642 | // there is one, the track is connected to it |
| 8643 | if (!mEffectChains.isEmpty()) { |
| 8644 | mEffectChains[0]->setVolume_l(&vol, &vol); |
| 8645 | volume = (float)vol / (1 << 24); |
| 8646 | } |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8647 | // Try to use HW volume control and fall back to SW control if not implemented |
| 8648 | if (mOutput->stream->setVolume(volume, volume) != NO_ERROR) { |
| 8649 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 8650 | if (callback != 0) { |
| 8651 | int channelCount; |
| 8652 | if (isOutput()) { |
| 8653 | channelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 8654 | } else { |
| 8655 | channelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8656 | } |
| 8657 | Vector<float> values; |
| 8658 | for (int i = 0; i < channelCount; i++) { |
| 8659 | values.add(volume); |
| 8660 | } |
| 8661 | callback->onVolumeChanged(mChannelMask, values); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8662 | } else { |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8663 | ALOGW("Could not set MMAP stream volume: no volume callback!"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8664 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8665 | } |
| 8666 | } |
| 8667 | } |
| 8668 | |
| 8669 | void AudioFlinger::MmapPlaybackThread::checkSilentMode_l() |
| 8670 | { |
| 8671 | if (!mMasterMute) { |
| 8672 | char value[PROPERTY_VALUE_MAX]; |
| 8673 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 8674 | char *endptr; |
| 8675 | unsigned long ul = strtoul(value, &endptr, 0); |
| 8676 | if (*endptr == '\0' && ul != 0) { |
| 8677 | ALOGD("Silence is golden"); |
| 8678 | // The setprop command will not allow a property to be changed after |
| 8679 | // the first time it is set, so we don't have to worry about un-muting. |
| 8680 | setMasterMute_l(true); |
| 8681 | } |
| 8682 | } |
| 8683 | } |
| 8684 | } |
| 8685 | |
| 8686 | void AudioFlinger::MmapPlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 8687 | { |
| 8688 | MmapThread::dumpInternals(fd, args); |
| 8689 | |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8690 | dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", |
| 8691 | mStreamType, mStreamVolume, mHalVolFloat, mStreamMute); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8692 | dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); |
| 8693 | } |
| 8694 | |
| 8695 | AudioFlinger::MmapCaptureThread::MmapCaptureThread( |
| 8696 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 8697 | AudioHwDevice *hwDev, AudioStreamIn *input, |
| 8698 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) |
| 8699 | : MmapThread(audioFlinger, id, hwDev, input->stream, outDevice, inDevice, systemReady), |
| 8700 | mInput(input) |
| 8701 | { |
| 8702 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); |
| 8703 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8704 | } |
| 8705 | |
| 8706 | AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput() |
| 8707 | { |
| 8708 | Mutex::Autolock _l(mLock); |
| 8709 | AudioStreamIn *input = mInput; |
| 8710 | mInput = NULL; |
| 8711 | return input; |
| 8712 | } |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 8713 | } // namespace android |