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> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 34 | |
| 35 | #include <private/media/AudioTrackShared.h> |
| 36 | #include <hardware/audio.h> |
| 37 | #include <audio_effects/effect_ns.h> |
| 38 | #include <audio_effects/effect_aec.h> |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 39 | #include <audio_utils/conversion.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> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 43 | |
| 44 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 45 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 46 | #include <media/nbaio/AudioStreamOutSink.h> |
| 47 | #include <media/nbaio/MonoPipe.h> |
| 48 | #include <media/nbaio/MonoPipeReader.h> |
| 49 | #include <media/nbaio/Pipe.h> |
| 50 | #include <media/nbaio/PipeReader.h> |
| 51 | #include <media/nbaio/SourceAudioBufferProvider.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 52 | #include <mediautils/BatteryNotifier.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 53 | |
| 54 | #include <powermanager/PowerManager.h> |
| 55 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 56 | #include "AudioFlinger.h" |
| 57 | #include "AudioMixer.h" |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 58 | #include "BufferProviders.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 60 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 61 | #include "ServiceUtilities.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 62 | #include "mediautils/SchedulingPolicyService.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 63 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 | #ifdef ADD_BATTERY_DATA |
| 65 | #include <media/IMediaPlayerService.h> |
| 66 | #include <media/IMediaDeathNotifier.h> |
| 67 | #endif |
| 68 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 69 | #ifdef DEBUG_CPU_USAGE |
| 70 | #include <cpustats/CentralTendencyStatistics.h> |
| 71 | #include <cpustats/ThreadCpuUsage.h> |
| 72 | #endif |
| 73 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 74 | #include "AutoPark.h" |
| 75 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 76 | // ---------------------------------------------------------------------------- |
| 77 | |
| 78 | // Note: the following macro is used for extremely verbose logging message. In |
| 79 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 80 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 81 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 82 | // turned on. Do not uncomment the #def below unless you really know what you |
| 83 | // are doing and want to see all of the extremely verbose messages. |
| 84 | //#define VERY_VERY_VERBOSE_LOGGING |
| 85 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 86 | #define ALOGVV ALOGV |
| 87 | #else |
| 88 | #define ALOGVV(a...) do { } while(0) |
| 89 | #endif |
| 90 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 91 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 92 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 93 | template <typename T> |
| 94 | static inline T min(const T& a, const T& b) |
| 95 | { |
| 96 | return a < b ? a : b; |
| 97 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 98 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 99 | #ifndef ARRAY_SIZE |
| 100 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 101 | #endif |
| 102 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 103 | namespace android { |
| 104 | |
| 105 | // retry counts for buffer fill timeout |
| 106 | // 50 * ~20msecs = 1 second |
| 107 | static const int8_t kMaxTrackRetries = 50; |
| 108 | static const int8_t kMaxTrackStartupRetries = 50; |
| 109 | // allow less retry attempts on direct output thread. |
| 110 | // direct outputs can be a scarce resource in audio hardware and should |
| 111 | // be released as quickly as possible. |
| 112 | static const int8_t kMaxTrackRetriesDirect = 2; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 113 | // retry count before removing active track in case of underrun on offloaded thread: |
| 114 | // we need to make sure that AudioTrack client has enough time to send large buffers |
| 115 | //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled |
| 116 | // for offloaded tracks |
| 117 | static const int8_t kMaxTrackRetriesOffload = 10; |
| 118 | static const int8_t kMaxTrackStartupRetriesOffload = 100; |
| 119 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 120 | |
| 121 | // don't warn about blocked writes or record buffer overflows more often than this |
| 122 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 123 | |
| 124 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 125 | static const int kRecordThreadSleepUs = 5000; |
| 126 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 127 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 128 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 129 | |
| 130 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 131 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 132 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 133 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 134 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 135 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 136 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 137 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 138 | // maximum normal sink buffer size |
| 139 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 140 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 141 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 142 | // FIXME This should be based on experimentally observed scheduling jitter |
| 143 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 144 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 145 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 146 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 147 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 148 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 149 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 150 | |
| 151 | // Offloaded output bit rate in bits per second when unknown. |
| 152 | // Used for sleep time calculation, so use a high default bitrate to be conservative on sleep time. |
| 153 | static const uint32_t kOffloadDefaultBitRateBps = 1500000; |
| 154 | |
| 155 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 156 | // Whether to use fast mixer |
| 157 | static const enum { |
| 158 | FastMixer_Never, // never initialize or use: for debugging only |
| 159 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 160 | // normal mixer multiplier is 1 |
| 161 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 162 | // multiplier is calculated based on min & max normal mixer buffer size |
| 163 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 164 | // multiplier is calculated based on min & max normal mixer buffer size |
| 165 | // FIXME for FastMixer_Dynamic: |
| 166 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 167 | // For example, one HAL implementation returns an error from a large write, |
| 168 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 169 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 170 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 171 | } kUseFastMixer = FastMixer_Static; |
| 172 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 173 | // Whether to use fast capture |
| 174 | static const enum { |
| 175 | FastCapture_Never, // never initialize or use: for debugging only |
| 176 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 177 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 178 | } kUseFastCapture = FastCapture_Static; |
| 179 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 180 | // Priorities for requestPriority |
| 181 | static const int kPriorityAudioApp = 2; |
| 182 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 183 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 184 | |
| 185 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area |
| 186 | // for the track. The client then sub-divides this into smaller buffers for its use. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 187 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. |
| 188 | // So for now we just assume that client is double-buffered for fast tracks. |
| 189 | // FIXME It would be better for client to tell AudioFlinger the value of N, |
| 190 | // so AudioFlinger could allocate the right amount of memory. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 191 | // See the client's minBufCount and mNotificationFramesAct calculations for details. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 192 | |
| 193 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 194 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 195 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 196 | // The minimum and maximum allowed values |
| 197 | static const int kFastTrackMultiplierMin = 1; |
| 198 | static const int kFastTrackMultiplierMax = 2; |
| 199 | |
| 200 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 201 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 202 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 203 | // See Thread::readOnlyHeap(). |
| 204 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 205 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 206 | // and that all "fast" AudioRecord clients read from. In either case, the size can be small. |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 207 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 208 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 209 | // ---------------------------------------------------------------------------- |
| 210 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 211 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 212 | |
| 213 | static void sFastTrackMultiplierInit() |
| 214 | { |
| 215 | char value[PROPERTY_VALUE_MAX]; |
| 216 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 217 | char *endptr; |
| 218 | unsigned long ul = strtoul(value, &endptr, 0); |
| 219 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 220 | sFastTrackMultiplier = (int) ul; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // ---------------------------------------------------------------------------- |
| 226 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 227 | #ifdef ADD_BATTERY_DATA |
| 228 | // To collect the amplifier usage |
| 229 | static void addBatteryData(uint32_t params) { |
| 230 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 231 | if (service == NULL) { |
| 232 | // it already logged |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | service->addBatteryData(params); |
| 237 | } |
| 238 | #endif |
| 239 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 240 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 241 | struct { |
| 242 | // call when you acquire a partial wakelock |
| 243 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 244 | pthread_mutex_lock(&mLock); |
| 245 | if (wakeLockToken.get() == nullptr) { |
| 246 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 247 | } else { |
| 248 | if (mCount == 0) { |
| 249 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 250 | } |
| 251 | ++mCount; |
| 252 | } |
| 253 | pthread_mutex_unlock(&mLock); |
| 254 | } |
| 255 | |
| 256 | // call when you release a partial wakelock. |
| 257 | void release(const sp<IBinder> &wakeLockToken) { |
| 258 | if (wakeLockToken.get() == nullptr) { |
| 259 | return; |
| 260 | } |
| 261 | pthread_mutex_lock(&mLock); |
| 262 | if (--mCount < 0) { |
| 263 | ALOGE("negative wakelock count"); |
| 264 | mCount = 0; |
| 265 | } |
| 266 | pthread_mutex_unlock(&mLock); |
| 267 | } |
| 268 | |
| 269 | // retrieves the boottime timebase offset from monotonic. |
| 270 | int64_t getBoottimeOffset() { |
| 271 | pthread_mutex_lock(&mLock); |
| 272 | int64_t boottimeOffset = mBoottimeOffset; |
| 273 | pthread_mutex_unlock(&mLock); |
| 274 | return boottimeOffset; |
| 275 | } |
| 276 | |
| 277 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 278 | // and the selected timebase. |
| 279 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 280 | // |
| 281 | // This only needs to be called upon acquiring the first partial wakelock |
| 282 | // after all other partial wakelocks are released. |
| 283 | // |
| 284 | // We do an empirical measurement of the offset rather than parsing |
| 285 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 286 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 287 | int clockbase; |
| 288 | switch (timebase) { |
| 289 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 290 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 291 | break; |
| 292 | default: |
| 293 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 294 | break; |
| 295 | } |
| 296 | // try three times to get the clock offset, choose the one |
| 297 | // with the minimum gap in measurements. |
| 298 | const int tries = 3; |
| 299 | nsecs_t bestGap, measured; |
| 300 | for (int i = 0; i < tries; ++i) { |
| 301 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 302 | const nsecs_t tbase = systemTime(clockbase); |
| 303 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 304 | const nsecs_t gap = tmono2 - tmono; |
| 305 | if (i == 0 || gap < bestGap) { |
| 306 | bestGap = gap; |
| 307 | measured = tbase - ((tmono + tmono2) >> 1); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // to avoid micro-adjusting, we don't change the timebase |
| 312 | // unless it is significantly different. |
| 313 | // |
| 314 | // Assumption: It probably takes more than toleranceNs to |
| 315 | // suspend and resume the device. |
| 316 | static int64_t toleranceNs = 10000; // 10 us |
| 317 | if (llabs(*offset - measured) > toleranceNs) { |
| 318 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 319 | (long long)*offset, (long long)measured); |
| 320 | *offset = measured; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | pthread_mutex_t mLock; |
| 325 | int32_t mCount; |
| 326 | int64_t mBoottimeOffset; |
| 327 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 328 | |
| 329 | // ---------------------------------------------------------------------------- |
| 330 | // CPU Stats |
| 331 | // ---------------------------------------------------------------------------- |
| 332 | |
| 333 | class CpuStats { |
| 334 | public: |
| 335 | CpuStats(); |
| 336 | void sample(const String8 &title); |
| 337 | #ifdef DEBUG_CPU_USAGE |
| 338 | private: |
| 339 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 340 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 341 | |
| 342 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 343 | |
| 344 | int mCpuNum; // thread's current CPU number |
| 345 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 346 | #endif |
| 347 | }; |
| 348 | |
| 349 | CpuStats::CpuStats() |
| 350 | #ifdef DEBUG_CPU_USAGE |
| 351 | : mCpuNum(-1), mCpukHz(-1) |
| 352 | #endif |
| 353 | { |
| 354 | } |
| 355 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 356 | void CpuStats::sample(const String8 &title |
| 357 | #ifndef DEBUG_CPU_USAGE |
| 358 | __unused |
| 359 | #endif |
| 360 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 361 | #ifdef DEBUG_CPU_USAGE |
| 362 | // get current thread's delta CPU time in wall clock ns |
| 363 | double wcNs; |
| 364 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 365 | |
| 366 | // record sample for wall clock statistics |
| 367 | if (valid) { |
| 368 | mWcStats.sample(wcNs); |
| 369 | } |
| 370 | |
| 371 | // get the current CPU number |
| 372 | int cpuNum = sched_getcpu(); |
| 373 | |
| 374 | // get the current CPU frequency in kHz |
| 375 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 376 | |
| 377 | // check if either CPU number or frequency changed |
| 378 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 379 | mCpuNum = cpuNum; |
| 380 | mCpukHz = cpukHz; |
| 381 | // ignore sample for purposes of cycles |
| 382 | valid = false; |
| 383 | } |
| 384 | |
| 385 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 386 | if (valid && mCpukHz > 0) { |
| 387 | double cycles = wcNs * cpukHz * 0.000001; |
| 388 | mHzStats.sample(cycles); |
| 389 | } |
| 390 | |
| 391 | unsigned n = mWcStats.n(); |
| 392 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 393 | if ((n & 127) == 1) { |
| 394 | long long elapsed = mCpuUsage.elapsed(); |
| 395 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 396 | double perLoop = elapsed / (double) n; |
| 397 | double perLoop100 = perLoop * 0.01; |
| 398 | double perLoop1k = perLoop * 0.001; |
| 399 | double mean = mWcStats.mean(); |
| 400 | double stddev = mWcStats.stddev(); |
| 401 | double minimum = mWcStats.minimum(); |
| 402 | double maximum = mWcStats.maximum(); |
| 403 | double meanCycles = mHzStats.mean(); |
| 404 | double stddevCycles = mHzStats.stddev(); |
| 405 | double minCycles = mHzStats.minimum(); |
| 406 | double maxCycles = mHzStats.maximum(); |
| 407 | mCpuUsage.resetElapsed(); |
| 408 | mWcStats.reset(); |
| 409 | mHzStats.reset(); |
| 410 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 411 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 412 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 413 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 414 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 415 | title.string(), |
| 416 | elapsed * .000000001, n, perLoop * .000001, |
| 417 | mean * .001, |
| 418 | stddev * .001, |
| 419 | minimum * .001, |
| 420 | maximum * .001, |
| 421 | mean / perLoop100, |
| 422 | stddev / perLoop100, |
| 423 | minimum / perLoop100, |
| 424 | maximum / perLoop100, |
| 425 | meanCycles / perLoop1k, |
| 426 | stddevCycles / perLoop1k, |
| 427 | minCycles / perLoop1k, |
| 428 | maxCycles / perLoop1k); |
| 429 | |
| 430 | } |
| 431 | } |
| 432 | #endif |
| 433 | }; |
| 434 | |
| 435 | // ---------------------------------------------------------------------------- |
| 436 | // ThreadBase |
| 437 | // ---------------------------------------------------------------------------- |
| 438 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 439 | // static |
| 440 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 441 | { |
| 442 | switch (type) { |
| 443 | case MIXER: |
| 444 | return "MIXER"; |
| 445 | case DIRECT: |
| 446 | return "DIRECT"; |
| 447 | case DUPLICATING: |
| 448 | return "DUPLICATING"; |
| 449 | case RECORD: |
| 450 | return "RECORD"; |
| 451 | case OFFLOAD: |
| 452 | return "OFFLOAD"; |
| 453 | default: |
| 454 | return "unknown"; |
| 455 | } |
| 456 | } |
| 457 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 458 | String8 devicesToString(audio_devices_t devices) |
| 459 | { |
| 460 | static const struct mapping { |
| 461 | audio_devices_t mDevices; |
| 462 | const char * mString; |
| 463 | } mappingsOut[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 464 | {AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE"}, |
| 465 | {AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER"}, |
| 466 | {AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 467 | {AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE"}, |
| 468 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "BLUETOOTH_SCO"}, |
| 469 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 470 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, "BLUETOOTH_SCO_CARKIT"}, |
| 471 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 472 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,"BLUETOOTH_A2DP_HEADPHONES"}, |
| 473 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, "BLUETOOTH_A2DP_SPEAKER"}, |
| 474 | {AUDIO_DEVICE_OUT_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 475 | {AUDIO_DEVICE_OUT_HDMI, "HDMI"}, |
| 476 | {AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,"ANLG_DOCK_HEADSET"}, |
| 477 | {AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,"DGTL_DOCK_HEADSET"}, |
| 478 | {AUDIO_DEVICE_OUT_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 479 | {AUDIO_DEVICE_OUT_USB_DEVICE, "USB_DEVICE"}, |
| 480 | {AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX"}, |
| 481 | {AUDIO_DEVICE_OUT_LINE, "LINE"}, |
| 482 | {AUDIO_DEVICE_OUT_HDMI_ARC, "HDMI_ARC"}, |
| 483 | {AUDIO_DEVICE_OUT_SPDIF, "SPDIF"}, |
| 484 | {AUDIO_DEVICE_OUT_FM, "FM"}, |
| 485 | {AUDIO_DEVICE_OUT_AUX_LINE, "AUX_LINE"}, |
| 486 | {AUDIO_DEVICE_OUT_SPEAKER_SAFE, "SPEAKER_SAFE"}, |
| 487 | {AUDIO_DEVICE_OUT_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame] | 488 | {AUDIO_DEVICE_OUT_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 489 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 490 | }, mappingsIn[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 491 | {AUDIO_DEVICE_IN_COMMUNICATION, "COMMUNICATION"}, |
| 492 | {AUDIO_DEVICE_IN_AMBIENT, "AMBIENT"}, |
| 493 | {AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC"}, |
| 494 | {AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 495 | {AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 496 | {AUDIO_DEVICE_IN_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 497 | {AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL"}, |
| 498 | {AUDIO_DEVICE_IN_TELEPHONY_RX, "TELEPHONY_RX"}, |
| 499 | {AUDIO_DEVICE_IN_BACK_MIC, "BACK_MIC"}, |
| 500 | {AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX"}, |
| 501 | {AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET"}, |
| 502 | {AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET"}, |
| 503 | {AUDIO_DEVICE_IN_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 504 | {AUDIO_DEVICE_IN_USB_DEVICE, "USB_DEVICE"}, |
| 505 | {AUDIO_DEVICE_IN_FM_TUNER, "FM_TUNER"}, |
| 506 | {AUDIO_DEVICE_IN_TV_TUNER, "TV_TUNER"}, |
| 507 | {AUDIO_DEVICE_IN_LINE, "LINE"}, |
| 508 | {AUDIO_DEVICE_IN_SPDIF, "SPDIF"}, |
| 509 | {AUDIO_DEVICE_IN_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 510 | {AUDIO_DEVICE_IN_LOOPBACK, "LOOPBACK"}, |
| 511 | {AUDIO_DEVICE_IN_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame] | 512 | {AUDIO_DEVICE_IN_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 513 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 514 | }; |
| 515 | String8 result; |
| 516 | audio_devices_t allDevices = AUDIO_DEVICE_NONE; |
| 517 | const mapping *entry; |
| 518 | if (devices & AUDIO_DEVICE_BIT_IN) { |
| 519 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 520 | entry = mappingsIn; |
| 521 | } else { |
| 522 | entry = mappingsOut; |
| 523 | } |
| 524 | for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { |
| 525 | allDevices = (audio_devices_t) (allDevices | entry->mDevices); |
| 526 | if (devices & entry->mDevices) { |
| 527 | if (!result.isEmpty()) { |
| 528 | result.append("|"); |
| 529 | } |
| 530 | result.append(entry->mString); |
| 531 | } |
| 532 | } |
| 533 | if (devices & ~allDevices) { |
| 534 | if (!result.isEmpty()) { |
| 535 | result.append("|"); |
| 536 | } |
| 537 | result.appendFormat("0x%X", devices & ~allDevices); |
| 538 | } |
| 539 | if (result.isEmpty()) { |
| 540 | result.append(entry->mString); |
| 541 | } |
| 542 | return result; |
| 543 | } |
| 544 | |
| 545 | String8 inputFlagsToString(audio_input_flags_t flags) |
| 546 | { |
| 547 | static const struct mapping { |
| 548 | audio_input_flags_t mFlag; |
| 549 | const char * mString; |
| 550 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 551 | {AUDIO_INPUT_FLAG_FAST, "FAST"}, |
| 552 | {AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD"}, |
| 553 | {AUDIO_INPUT_FLAG_RAW, "RAW"}, |
| 554 | {AUDIO_INPUT_FLAG_SYNC, "SYNC"}, |
| 555 | {AUDIO_INPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 556 | }; |
| 557 | String8 result; |
| 558 | audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; |
| 559 | const mapping *entry; |
| 560 | for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { |
| 561 | allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); |
| 562 | if (flags & entry->mFlag) { |
| 563 | if (!result.isEmpty()) { |
| 564 | result.append("|"); |
| 565 | } |
| 566 | result.append(entry->mString); |
| 567 | } |
| 568 | } |
| 569 | if (flags & ~allFlags) { |
| 570 | if (!result.isEmpty()) { |
| 571 | result.append("|"); |
| 572 | } |
| 573 | result.appendFormat("0x%X", flags & ~allFlags); |
| 574 | } |
| 575 | if (result.isEmpty()) { |
| 576 | result.append(entry->mString); |
| 577 | } |
| 578 | return result; |
| 579 | } |
| 580 | |
| 581 | String8 outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 582 | { |
| 583 | static const struct mapping { |
| 584 | audio_output_flags_t mFlag; |
| 585 | const char * mString; |
| 586 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 587 | {AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT"}, |
| 588 | {AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY"}, |
| 589 | {AUDIO_OUTPUT_FLAG_FAST, "FAST"}, |
| 590 | {AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER"}, |
| 591 | {AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD,"COMPRESS_OFFLOAD"}, |
| 592 | {AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING"}, |
| 593 | {AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC"}, |
| 594 | {AUDIO_OUTPUT_FLAG_RAW, "RAW"}, |
| 595 | {AUDIO_OUTPUT_FLAG_SYNC, "SYNC"}, |
| 596 | {AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO, "IEC958_NONAUDIO"}, |
| 597 | {AUDIO_OUTPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 598 | }; |
| 599 | String8 result; |
| 600 | audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 601 | const mapping *entry; |
| 602 | for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { |
| 603 | allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); |
| 604 | if (flags & entry->mFlag) { |
| 605 | if (!result.isEmpty()) { |
| 606 | result.append("|"); |
| 607 | } |
| 608 | result.append(entry->mString); |
| 609 | } |
| 610 | } |
| 611 | if (flags & ~allFlags) { |
| 612 | if (!result.isEmpty()) { |
| 613 | result.append("|"); |
| 614 | } |
| 615 | result.appendFormat("0x%X", flags & ~allFlags); |
| 616 | } |
| 617 | if (result.isEmpty()) { |
| 618 | result.append(entry->mString); |
| 619 | } |
| 620 | return result; |
| 621 | } |
| 622 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 623 | const char *sourceToString(audio_source_t source) |
| 624 | { |
| 625 | switch (source) { |
| 626 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 627 | case AUDIO_SOURCE_MIC: return "mic"; |
| 628 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 629 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 630 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 631 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 632 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 633 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 634 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 635 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 636 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 637 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 638 | default: return "unknown"; |
| 639 | } |
| 640 | } |
| 641 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 642 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 643 | 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] | 644 | : Thread(false /*canCallJava*/), |
| 645 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 646 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 647 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 648 | // are set by PlaybackThread::readOutputParameters_l() or |
| 649 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 650 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 651 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 652 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 653 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 654 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 655 | mDeathRecipient(new PMDeathRecipient(this)), |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 656 | mSystemReady(systemReady), |
| 657 | mNotifiedBatteryStart(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 658 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 659 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | AudioFlinger::ThreadBase::~ThreadBase() |
| 663 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 664 | // 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] | 665 | mConfigEvents.clear(); |
| 666 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 667 | // do not lock the mutex in destructor |
| 668 | releaseWakeLock_l(); |
| 669 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 670 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 671 | binder->unlinkToDeath(mDeathRecipient); |
| 672 | } |
| 673 | } |
| 674 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 675 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 676 | { |
| 677 | status_t status = initCheck(); |
| 678 | if (status == NO_ERROR) { |
| 679 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 680 | } else { |
| 681 | ALOGE("No working audio driver found."); |
| 682 | } |
| 683 | return status; |
| 684 | } |
| 685 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 686 | void AudioFlinger::ThreadBase::exit() |
| 687 | { |
| 688 | ALOGV("ThreadBase::exit"); |
| 689 | // do any cleanup required for exit to succeed |
| 690 | preExit(); |
| 691 | { |
| 692 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 693 | // if (!exitPending()) { |
| 694 | // // context switch from here to exit() |
| 695 | // // exit() calls requestExit(), what exitPending() observes |
| 696 | // // exit() calls signal(), which is dropped since no waiters |
| 697 | // // context switch back from exit() to here |
| 698 | // mWaitWorkCV.wait(...); |
| 699 | // // now thread is hung |
| 700 | // } |
| 701 | AutoMutex lock(mLock); |
| 702 | requestExit(); |
| 703 | mWaitWorkCV.broadcast(); |
| 704 | } |
| 705 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 706 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 707 | requestExitAndWait(); |
| 708 | } |
| 709 | |
| 710 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 711 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 712 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 713 | Mutex::Autolock _l(mLock); |
| 714 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 715 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 716 | } |
| 717 | |
| 718 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 719 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 720 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 721 | { |
| 722 | status_t status = NO_ERROR; |
| 723 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 724 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 725 | event->mWaitStatus = false; |
| 726 | mPendingConfigEvents.add(event); |
| 727 | return status; |
| 728 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 729 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 730 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 731 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 732 | mLock.unlock(); |
| 733 | { |
| 734 | Mutex::Autolock _l(event->mLock); |
| 735 | while (event->mWaitStatus) { |
| 736 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 737 | event->mStatus = TIMED_OUT; |
| 738 | event->mWaitStatus = false; |
| 739 | } |
| 740 | } |
| 741 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 742 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 743 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 744 | return status; |
| 745 | } |
| 746 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 747 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 748 | { |
| 749 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 750 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 754 | 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] | 755 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 756 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 757 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 760 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) |
| 761 | { |
| 762 | Mutex::Autolock _l(mLock); |
| 763 | sendPrioConfigEvent_l(pid, tid, prio); |
| 764 | } |
| 765 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 766 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 767 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 768 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 769 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 770 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 773 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 774 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 775 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 776 | sp<ConfigEvent> configEvent; |
| 777 | AudioParameter param(keyValuePair); |
| 778 | int value; |
| 779 | if (param.getInt(String8(AUDIO_PARAMETER_MONO_OUTPUT), value) == NO_ERROR) { |
| 780 | setMasterMono_l(value != 0); |
| 781 | if (param.size() == 1) { |
| 782 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 783 | } |
| 784 | param.remove(String8(AUDIO_PARAMETER_MONO_OUTPUT)); |
| 785 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 786 | } else { |
| 787 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 788 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 789 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 792 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 793 | const struct audio_patch *patch, |
| 794 | audio_patch_handle_t *handle) |
| 795 | { |
| 796 | Mutex::Autolock _l(mLock); |
| 797 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 798 | status_t status = sendConfigEvent_l(configEvent); |
| 799 | if (status == NO_ERROR) { |
| 800 | CreateAudioPatchConfigEventData *data = |
| 801 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 802 | *handle = data->mHandle; |
| 803 | } |
| 804 | return status; |
| 805 | } |
| 806 | |
| 807 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 808 | const audio_patch_handle_t handle) |
| 809 | { |
| 810 | Mutex::Autolock _l(mLock); |
| 811 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 812 | return sendConfigEvent_l(configEvent); |
| 813 | } |
| 814 | |
| 815 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 816 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 817 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 818 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 819 | bool configChanged = false; |
| 820 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 821 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 822 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 823 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 824 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 825 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 826 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 827 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 828 | // FIXME Need to understand why this has to be done asynchronously |
| 829 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 830 | true /*asynchronous*/); |
| 831 | if (err != 0) { |
| 832 | 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] | 833 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 834 | } |
| 835 | } break; |
| 836 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 837 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 838 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 839 | } break; |
| 840 | case CFG_EVENT_SET_PARAMETER: { |
| 841 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 842 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 843 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 844 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 845 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 846 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 847 | CreateAudioPatchConfigEventData *data = |
| 848 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 849 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 850 | } break; |
| 851 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 852 | ReleaseAudioPatchConfigEventData *data = |
| 853 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 854 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 855 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 856 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 857 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 858 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 859 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 860 | { |
| 861 | Mutex::Autolock _l(event->mLock); |
| 862 | if (event->mWaitStatus) { |
| 863 | event->mWaitStatus = false; |
| 864 | event->mCond.signal(); |
| 865 | } |
| 866 | } |
| 867 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 868 | } |
| 869 | |
| 870 | if (configChanged) { |
| 871 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 875 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 876 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 877 | const audio_channel_representation_t representation = |
| 878 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 879 | |
| 880 | switch (representation) { |
| 881 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 882 | if (output) { |
| 883 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 884 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 885 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 886 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 887 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 888 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 889 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 890 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 891 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 892 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 893 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 894 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 895 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 896 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 897 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 898 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 899 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 900 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 901 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 902 | } else { |
| 903 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 904 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 905 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 906 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 907 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 908 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 909 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 910 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 911 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 912 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 913 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 914 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 915 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 916 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 917 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 918 | } |
| 919 | const int len = s.length(); |
| 920 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 921 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 922 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 923 | } |
| 924 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 925 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 926 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 927 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 928 | return s; |
| 929 | default: |
| 930 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 931 | representation, audio_channel_mask_get_bits(mask)); |
| 932 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 933 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 936 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 937 | { |
| 938 | const size_t SIZE = 256; |
| 939 | char buffer[SIZE]; |
| 940 | String8 result; |
| 941 | |
| 942 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 943 | if (!locked) { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 944 | dprintf(fd, "thread %p may be deadlocked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 947 | dprintf(fd, " Thread name: %s\n", mThreadName); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 948 | dprintf(fd, " I/O handle: %d\n", mId); |
| 949 | dprintf(fd, " TID: %d\n", getTid()); |
| 950 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 951 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 952 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 953 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 954 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 955 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 956 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 957 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 958 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); |
| 959 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 960 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 961 | size_t numConfig = mConfigEvents.size(); |
| 962 | if (numConfig) { |
| 963 | for (size_t i = 0; i < numConfig; i++) { |
| 964 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 965 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 966 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 967 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 968 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 969 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 970 | } |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 971 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); |
| 972 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); |
| 973 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 974 | |
| 975 | if (locked) { |
| 976 | mLock.unlock(); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 981 | { |
| 982 | const size_t SIZE = 256; |
| 983 | char buffer[SIZE]; |
| 984 | String8 result; |
| 985 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 986 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 987 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 988 | write(fd, buffer, strlen(buffer)); |
| 989 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 990 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 991 | sp<EffectChain> chain = mEffectChains[i]; |
| 992 | if (chain != 0) { |
| 993 | chain->dump(fd, args); |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 998 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 999 | { |
| 1000 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1001 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1004 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 1005 | { |
| 1006 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1007 | case MIXER: |
| 1008 | return String16("AudioMix"); |
| 1009 | case DIRECT: |
| 1010 | return String16("AudioDirectOut"); |
| 1011 | case DUPLICATING: |
| 1012 | return String16("AudioDup"); |
| 1013 | case RECORD: |
| 1014 | return String16("AudioIn"); |
| 1015 | case OFFLOAD: |
| 1016 | return String16("AudioOffload"); |
| 1017 | default: |
| 1018 | ALOG_ASSERT(false); |
| 1019 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1023 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1024 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1025 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1026 | if (mPowerManager != 0) { |
| 1027 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1028 | status_t status; |
| 1029 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1030 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1031 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1032 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1033 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1034 | uid, |
| 1035 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1036 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1037 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1038 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1039 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1040 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1041 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1042 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1043 | if (status == NO_ERROR) { |
| 1044 | mWakeLockToken = binder; |
| 1045 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1046 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1047 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1048 | |
| 1049 | if (!mNotifiedBatteryStart) { |
| 1050 | BatteryNotifier::getInstance().noteStartAudio(); |
| 1051 | mNotifiedBatteryStart = true; |
| 1052 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1053 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1054 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 1055 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1059 | { |
| 1060 | Mutex::Autolock _l(mLock); |
| 1061 | releaseWakeLock_l(); |
| 1062 | } |
| 1063 | |
| 1064 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1065 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1066 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1067 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1068 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1069 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1070 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 1071 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1072 | } |
| 1073 | mWakeLockToken.clear(); |
| 1074 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1075 | |
| 1076 | if (mNotifiedBatteryStart) { |
| 1077 | BatteryNotifier::getInstance().noteStopAudio(); |
| 1078 | mNotifiedBatteryStart = false; |
| 1079 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1082 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 1083 | Mutex::Autolock _l(mLock); |
| 1084 | updateWakeLockUids_l(uids); |
| 1085 | } |
| 1086 | |
| 1087 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1088 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1089 | // use checkService() to avoid blocking if power service is not up yet |
| 1090 | sp<IBinder> binder = |
| 1091 | defaultServiceManager()->checkService(String16("power")); |
| 1092 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1093 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1094 | } else { |
| 1095 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1096 | binder->linkToDeath(mDeathRecipient); |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1102 | getPowerManager_l(); |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1103 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1104 | if (mSystemReady) { |
| 1105 | ALOGE("no wake lock to update, but system ready!"); |
| 1106 | } else { |
| 1107 | ALOGW("no wake lock to update, system not ready yet"); |
| 1108 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1109 | return; |
| 1110 | } |
| 1111 | if (mPowerManager != 0) { |
| 1112 | sp<IBinder> binder = new BBinder(); |
| 1113 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1114 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 1115 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1116 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1120 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1121 | { |
| 1122 | Mutex::Autolock _l(mLock); |
| 1123 | releaseWakeLock_l(); |
| 1124 | mPowerManager.clear(); |
| 1125 | } |
| 1126 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1127 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1128 | { |
| 1129 | sp<ThreadBase> thread = mThread.promote(); |
| 1130 | if (thread != 0) { |
| 1131 | thread->clearPowerManager(); |
| 1132 | } |
| 1133 | ALOGW("power manager service died !!!"); |
| 1134 | } |
| 1135 | |
| 1136 | void AudioFlinger::ThreadBase::setEffectSuspended( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1137 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1138 | { |
| 1139 | Mutex::Autolock _l(mLock); |
| 1140 | setEffectSuspended_l(type, suspend, sessionId); |
| 1141 | } |
| 1142 | |
| 1143 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1144 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1145 | { |
| 1146 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1147 | if (chain != 0) { |
| 1148 | if (type != NULL) { |
| 1149 | chain->setEffectSuspended_l(type, suspend); |
| 1150 | } else { |
| 1151 | chain->setEffectSuspendedAll_l(suspend); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1156 | } |
| 1157 | |
| 1158 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1159 | { |
| 1160 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1161 | if (index < 0) { |
| 1162 | return; |
| 1163 | } |
| 1164 | |
| 1165 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1166 | mSuspendedSessions.valueAt(index); |
| 1167 | |
| 1168 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1169 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1170 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1171 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1172 | chain->setEffectSuspendedAll_l(true); |
| 1173 | } else { |
| 1174 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1175 | desc->mType.timeLow); |
| 1176 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1183 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1184 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1185 | { |
| 1186 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1187 | |
| 1188 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1189 | |
| 1190 | if (suspend) { |
| 1191 | if (index >= 0) { |
| 1192 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1193 | } else { |
| 1194 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1195 | } |
| 1196 | } else { |
| 1197 | if (index < 0) { |
| 1198 | return; |
| 1199 | } |
| 1200 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | int key = EffectChain::kKeyForSuspendAll; |
| 1205 | if (type != NULL) { |
| 1206 | key = type->timeLow; |
| 1207 | } |
| 1208 | index = sessionEffects.indexOfKey(key); |
| 1209 | |
| 1210 | sp<SuspendedSessionDesc> desc; |
| 1211 | if (suspend) { |
| 1212 | if (index >= 0) { |
| 1213 | desc = sessionEffects.valueAt(index); |
| 1214 | } else { |
| 1215 | desc = new SuspendedSessionDesc(); |
| 1216 | if (type != NULL) { |
| 1217 | desc->mType = *type; |
| 1218 | } |
| 1219 | sessionEffects.add(key, desc); |
| 1220 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1221 | } |
| 1222 | desc->mRefCount++; |
| 1223 | } else { |
| 1224 | if (index < 0) { |
| 1225 | return; |
| 1226 | } |
| 1227 | desc = sessionEffects.valueAt(index); |
| 1228 | if (--desc->mRefCount == 0) { |
| 1229 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1230 | sessionEffects.removeItemsAt(index); |
| 1231 | if (sessionEffects.isEmpty()) { |
| 1232 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1233 | sessionId); |
| 1234 | mSuspendedSessions.removeItem(sessionId); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | if (!sessionEffects.isEmpty()) { |
| 1239 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1244 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1245 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1246 | { |
| 1247 | Mutex::Autolock _l(mLock); |
| 1248 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1249 | } |
| 1250 | |
| 1251 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1252 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1253 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1254 | { |
| 1255 | if (mType != RECORD) { |
| 1256 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1257 | // another session. This gives the priority to well behaved effect control panels |
| 1258 | // and applications not using global effects. |
| 1259 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1260 | // global effects |
| 1261 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1262 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1267 | if (chain != 0) { |
| 1268 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1273 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1274 | const sp<AudioFlinger::Client>& client, |
| 1275 | const sp<IEffectClient>& effectClient, |
| 1276 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1277 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1278 | effect_descriptor_t *desc, |
| 1279 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1280 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1281 | { |
| 1282 | sp<EffectModule> effect; |
| 1283 | sp<EffectHandle> handle; |
| 1284 | status_t lStatus; |
| 1285 | sp<EffectChain> chain; |
| 1286 | bool chainCreated = false; |
| 1287 | bool effectCreated = false; |
| 1288 | bool effectRegistered = false; |
| 1289 | |
| 1290 | lStatus = initCheck(); |
| 1291 | if (lStatus != NO_ERROR) { |
| 1292 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1293 | goto Exit; |
| 1294 | } |
| 1295 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1296 | // Reject any effect on Direct output threads for now, since the format of |
| 1297 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1298 | if (mType == DIRECT) { |
| 1299 | ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1300 | desc->name, mThreadName); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1301 | lStatus = BAD_VALUE; |
| 1302 | goto Exit; |
| 1303 | } |
| 1304 | |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1305 | // Reject any effect on mixer or duplicating multichannel sinks. |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1306 | // TODO: fix both format and multichannel issues with effects. |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1307 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { |
| 1308 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", |
| 1309 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1310 | lStatus = BAD_VALUE; |
| 1311 | goto Exit; |
| 1312 | } |
| 1313 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1314 | // Allow global effects only on offloaded and mixer threads |
| 1315 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1316 | switch (mType) { |
| 1317 | case MIXER: |
| 1318 | case OFFLOAD: |
| 1319 | break; |
| 1320 | case DIRECT: |
| 1321 | case DUPLICATING: |
| 1322 | case RECORD: |
| 1323 | default: |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1324 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", |
| 1325 | desc->name, mThreadName); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1326 | lStatus = BAD_VALUE; |
| 1327 | goto Exit; |
| 1328 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1329 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1330 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1331 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 1332 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 1333 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 1334 | desc->name, desc->flags, mType); |
| 1335 | lStatus = BAD_VALUE; |
| 1336 | goto Exit; |
| 1337 | } |
| 1338 | |
| 1339 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1340 | |
| 1341 | { // scope for mLock |
| 1342 | Mutex::Autolock _l(mLock); |
| 1343 | |
| 1344 | // check for existing effect chain with the requested audio session |
| 1345 | chain = getEffectChain_l(sessionId); |
| 1346 | if (chain == 0) { |
| 1347 | // create a new chain for this session |
| 1348 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1349 | chain = new EffectChain(this, sessionId); |
| 1350 | addEffectChain_l(chain); |
| 1351 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1352 | chainCreated = true; |
| 1353 | } else { |
| 1354 | effect = chain->getEffectFromDesc_l(desc); |
| 1355 | } |
| 1356 | |
| 1357 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1358 | |
| 1359 | if (effect == 0) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1360 | audio_unique_id_t id = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1361 | // Check CPU and memory usage |
| 1362 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 1363 | if (lStatus != NO_ERROR) { |
| 1364 | goto Exit; |
| 1365 | } |
| 1366 | effectRegistered = true; |
| 1367 | // create a new effect module if none present in the chain |
| 1368 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 1369 | lStatus = effect->status(); |
| 1370 | if (lStatus != NO_ERROR) { |
| 1371 | goto Exit; |
| 1372 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1373 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1374 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1375 | lStatus = chain->addEffect_l(effect); |
| 1376 | if (lStatus != NO_ERROR) { |
| 1377 | goto Exit; |
| 1378 | } |
| 1379 | effectCreated = true; |
| 1380 | |
| 1381 | effect->setDevice(mOutDevice); |
| 1382 | effect->setDevice(mInDevice); |
| 1383 | effect->setMode(mAudioFlinger->getMode()); |
| 1384 | effect->setAudioSource(mAudioSource); |
| 1385 | } |
| 1386 | // create effect handle and connect it to effect module |
| 1387 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1388 | lStatus = handle->initCheck(); |
| 1389 | if (lStatus == OK) { |
| 1390 | lStatus = effect->addHandle(handle.get()); |
| 1391 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1392 | if (enabled != NULL) { |
| 1393 | *enabled = (int)effect->isEnabled(); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | Exit: |
| 1398 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1399 | Mutex::Autolock _l(mLock); |
| 1400 | if (effectCreated) { |
| 1401 | chain->removeEffect_l(effect); |
| 1402 | } |
| 1403 | if (effectRegistered) { |
| 1404 | AudioSystem::unregisterEffect(effect->id()); |
| 1405 | } |
| 1406 | if (chainCreated) { |
| 1407 | removeEffectChain_l(chain); |
| 1408 | } |
| 1409 | handle.clear(); |
| 1410 | } |
| 1411 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1412 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1413 | return handle; |
| 1414 | } |
| 1415 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1416 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1417 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1418 | { |
| 1419 | Mutex::Autolock _l(mLock); |
| 1420 | return getEffect_l(sessionId, effectId); |
| 1421 | } |
| 1422 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1423 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1424 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1425 | { |
| 1426 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1427 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1428 | } |
| 1429 | |
| 1430 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1431 | // PlaybackThread::mLock held |
| 1432 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1433 | { |
| 1434 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1435 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1436 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1437 | bool chainCreated = false; |
| 1438 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1439 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1440 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1441 | this, effect->desc().name, effect->desc().flags); |
| 1442 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1443 | if (chain == 0) { |
| 1444 | // create a new chain for this session |
| 1445 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1446 | chain = new EffectChain(this, sessionId); |
| 1447 | addEffectChain_l(chain); |
| 1448 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1449 | chainCreated = true; |
| 1450 | } |
| 1451 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1452 | |
| 1453 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1454 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1455 | this, effect->desc().name, chain.get()); |
| 1456 | return BAD_VALUE; |
| 1457 | } |
| 1458 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1459 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1460 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | status_t status = chain->addEffect_l(effect); |
| 1462 | if (status != NO_ERROR) { |
| 1463 | if (chainCreated) { |
| 1464 | removeEffectChain_l(chain); |
| 1465 | } |
| 1466 | return status; |
| 1467 | } |
| 1468 | |
| 1469 | effect->setDevice(mOutDevice); |
| 1470 | effect->setDevice(mInDevice); |
| 1471 | effect->setMode(mAudioFlinger->getMode()); |
| 1472 | effect->setAudioSource(mAudioSource); |
| 1473 | return NO_ERROR; |
| 1474 | } |
| 1475 | |
| 1476 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1477 | |
| 1478 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1479 | effect_descriptor_t desc = effect->desc(); |
| 1480 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1481 | detachAuxEffect_l(effect->id()); |
| 1482 | } |
| 1483 | |
| 1484 | sp<EffectChain> chain = effect->chain().promote(); |
| 1485 | if (chain != 0) { |
| 1486 | // remove effect chain if removing last effect |
| 1487 | if (chain->removeEffect_l(effect) == 0) { |
| 1488 | removeEffectChain_l(chain); |
| 1489 | } |
| 1490 | } else { |
| 1491 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1496 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1497 | { |
| 1498 | effectChains = mEffectChains; |
| 1499 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1500 | mEffectChains[i]->lock(); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1505 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1506 | { |
| 1507 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1508 | effectChains[i]->unlock(); |
| 1509 | } |
| 1510 | } |
| 1511 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1512 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1513 | { |
| 1514 | Mutex::Autolock _l(mLock); |
| 1515 | return getEffectChain_l(sessionId); |
| 1516 | } |
| 1517 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1518 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1519 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1520 | { |
| 1521 | size_t size = mEffectChains.size(); |
| 1522 | for (size_t i = 0; i < size; i++) { |
| 1523 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1524 | return mEffectChains[i]; |
| 1525 | } |
| 1526 | } |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1531 | { |
| 1532 | Mutex::Autolock _l(mLock); |
| 1533 | size_t size = mEffectChains.size(); |
| 1534 | for (size_t i = 0; i < size; i++) { |
| 1535 | mEffectChains[i]->setMode_l(mode); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1539 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1540 | { |
| 1541 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1542 | config->ext.mix.handle = mId; |
| 1543 | config->sample_rate = mSampleRate; |
| 1544 | config->format = mFormat; |
| 1545 | config->channel_mask = mChannelMask; |
| 1546 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1547 | AUDIO_PORT_CONFIG_FORMAT; |
| 1548 | } |
| 1549 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1550 | void AudioFlinger::ThreadBase::systemReady() |
| 1551 | { |
| 1552 | Mutex::Autolock _l(mLock); |
| 1553 | if (mSystemReady) { |
| 1554 | return; |
| 1555 | } |
| 1556 | mSystemReady = true; |
| 1557 | |
| 1558 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1559 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1560 | } |
| 1561 | mPendingConfigEvents.clear(); |
| 1562 | } |
| 1563 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1564 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1565 | // ---------------------------------------------------------------------------- |
| 1566 | // Playback |
| 1567 | // ---------------------------------------------------------------------------- |
| 1568 | |
| 1569 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1570 | AudioStreamOut* output, |
| 1571 | audio_io_handle_t id, |
| 1572 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1573 | type_t type, |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 1574 | bool systemReady, |
| 1575 | uint32_t bitRate) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1576 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1577 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1578 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1579 | mMixerBuffer(NULL), |
| 1580 | mMixerBufferSize(0), |
| 1581 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1582 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1583 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1584 | mEffectBuffer(NULL), |
| 1585 | mEffectBufferSize(0), |
| 1586 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1587 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1588 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1589 | mFramesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1590 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1591 | // mStreamTypes[] initialized in constructor body |
| 1592 | mOutput(output), |
| 1593 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1594 | mMixerStatus(MIXER_IDLE), |
| 1595 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1596 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1597 | mBytesRemaining(0), |
| 1598 | mCurrentWriteLength(0), |
| 1599 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1600 | mWriteAckSequence(0), |
| 1601 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1602 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1603 | mScreenState(AudioFlinger::mScreenState), |
| 1604 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1605 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1606 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1607 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1608 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1609 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1610 | |
| 1611 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1612 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1613 | // parameter. |
| 1614 | // |
| 1615 | // If the HAL we are using has support for master volume or master mute, |
| 1616 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1617 | // and the mute set to false). |
| 1618 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1619 | mMasterMute = audioFlinger->masterMute_l(); |
| 1620 | if (mOutput && mOutput->audioHwDev) { |
| 1621 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1622 | mMasterVolume = 1.0; |
| 1623 | } |
| 1624 | |
| 1625 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1626 | mMasterMute = false; |
| 1627 | } |
| 1628 | } |
| 1629 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1630 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1631 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1632 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1633 | for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1634 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1635 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1636 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1637 | } |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 1638 | |
| 1639 | if (audio_has_proportional_frames(mFormat)) { |
| 1640 | mBufferDurationUs = (uint32_t)((mNormalFrameCount * 1000000LL) / mSampleRate); |
| 1641 | } else { |
| 1642 | bitRate = bitRate != 0 ? bitRate : kOffloadDefaultBitRateBps; |
| 1643 | mBufferDurationUs = (uint32_t)((mBufferSize * 8 * 1000000LL) / bitRate); |
| 1644 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1648 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1649 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1650 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1651 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1652 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1656 | { |
| 1657 | dumpInternals(fd, args); |
| 1658 | dumpTracks(fd, args); |
| 1659 | dumpEffectChains(fd, args); |
| 1660 | } |
| 1661 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1662 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 | { |
| 1664 | const size_t SIZE = 256; |
| 1665 | char buffer[SIZE]; |
| 1666 | String8 result; |
| 1667 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1668 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1669 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1670 | const stream_type_t *st = &mStreamTypes[i]; |
| 1671 | if (i > 0) { |
| 1672 | result.appendFormat(", "); |
| 1673 | } |
| 1674 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1675 | if (st->mute) { |
| 1676 | result.append("M"); |
| 1677 | } |
| 1678 | } |
| 1679 | result.append("\n"); |
| 1680 | write(fd, result.string(), result.length()); |
| 1681 | result.clear(); |
| 1682 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1683 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1684 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1685 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1686 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1687 | |
| 1688 | size_t numtracks = mTracks.size(); |
| 1689 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1690 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1691 | size_t numactiveseen = 0; |
| 1692 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1693 | dprintf(fd, " of which %zu are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1694 | Track::appendDumpHeader(result); |
| 1695 | for (size_t i = 0; i < numtracks; ++i) { |
| 1696 | sp<Track> track = mTracks[i]; |
| 1697 | if (track != 0) { |
| 1698 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1699 | if (active) { |
| 1700 | numactiveseen++; |
| 1701 | } |
| 1702 | track->dump(buffer, SIZE, active); |
| 1703 | result.append(buffer); |
| 1704 | } |
| 1705 | } |
| 1706 | } else { |
| 1707 | result.append("\n"); |
| 1708 | } |
| 1709 | if (numactiveseen != numactive) { |
| 1710 | // some tracks in the active list were not in the tracks list |
| 1711 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1712 | " not in the track list\n"); |
| 1713 | result.append(buffer); |
| 1714 | Track::appendDumpHeader(result); |
| 1715 | for (size_t i = 0; i < numactive; ++i) { |
| 1716 | sp<Track> track = mActiveTracks[i].promote(); |
| 1717 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1718 | track->dump(buffer, SIZE, true); |
| 1719 | result.append(buffer); |
| 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1728 | { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1729 | dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1730 | |
| 1731 | dumpBase(fd, args); |
| 1732 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1733 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1734 | dprintf(fd, " Last write occurred (msecs): %llu\n", |
| 1735 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1736 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1737 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1738 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1739 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1740 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1741 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1742 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1743 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1744 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1745 | AudioStreamOut *output = mOutput; |
| 1746 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
| 1747 | String8 flagsAsString = outputFlagsToString(flags); |
| 1748 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1752 | |
| 1753 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1754 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1755 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | // ThreadBase virtuals |
| 1759 | void AudioFlinger::PlaybackThread::preExit() |
| 1760 | { |
| 1761 | ALOGV(" preExit()"); |
| 1762 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1763 | // converted to use audio HAL extensions required to support tunneling |
| 1764 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1765 | } |
| 1766 | |
| 1767 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1768 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1769 | const sp<AudioFlinger::Client>& client, |
| 1770 | audio_stream_type_t streamType, |
| 1771 | uint32_t sampleRate, |
| 1772 | audio_format_t format, |
| 1773 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1774 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1775 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1776 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1777 | IAudioFlinger::track_flags_t *flags, |
| 1778 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1779 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1780 | status_t *status) |
| 1781 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1782 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1783 | sp<Track> track; |
| 1784 | status_t lStatus; |
| 1785 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1786 | // client expresses a preference for FAST, but we get the final say |
| 1787 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 1788 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1789 | // PCM data |
| 1790 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1791 | // TODO: extract as a data library function that checks that a computationally |
| 1792 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1793 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1794 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1795 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1796 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1797 | // hardware sample rate |
| 1798 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1799 | // normal mixer has an associated fast mixer |
| 1800 | hasFastMixer() && |
| 1801 | // there are sufficient fast track slots available |
| 1802 | (mFastTrackAvailMask != 0) |
| 1803 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1804 | // FIXME add a permission test also? |
| 1805 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1806 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 1807 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1808 | // read the fast track multiplier property the first time it is needed |
| 1809 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1810 | if (ok != 0) { |
| 1811 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1812 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1813 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1814 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1815 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1816 | frameCount, mFrameCount); |
| 1817 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1818 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 1819 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1820 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1821 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1822 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1823 | audio_is_linear_pcm(format), |
| 1824 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
| 1825 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | // For normal PCM streaming tracks, update minimum frame count. |
| 1829 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1830 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1831 | // This is probably too conservative, but legacy application code may depend on it. |
| 1832 | // If you change this calculation, also review the start threshold which is related. |
| 1833 | if (!(*flags & IAudioFlinger::TRACK_FAST) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1834 | && audio_has_proportional_frames(format) && sharedBuffer == 0) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1835 | // this must match AudioTrack.cpp calculateMinFrameCount(). |
| 1836 | // TODO: Move to a common library |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1837 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1838 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1839 | if (minBufCount < 2) { |
| 1840 | minBufCount = 2; |
| 1841 | } |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1842 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack |
| 1843 | // or the client should compute and pass in a larger buffer request. |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1844 | size_t minFrameCount = |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1845 | minBufCount * sourceFramesNeededWithTimestretch( |
| 1846 | sampleRate, mNormalFrameCount, |
| 1847 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1848 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1849 | frameCount = minFrameCount; |
| 1850 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1851 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1852 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1853 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1854 | switch (mType) { |
| 1855 | |
| 1856 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1857 | 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] | 1858 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1859 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1860 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1861 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1862 | lStatus = BAD_VALUE; |
| 1863 | goto Exit; |
| 1864 | } |
| 1865 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1866 | break; |
| 1867 | |
| 1868 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1869 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1870 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1871 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1872 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1873 | lStatus = BAD_VALUE; |
| 1874 | goto Exit; |
| 1875 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1876 | break; |
| 1877 | |
| 1878 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1879 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1880 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1881 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1882 | format, mOutput, mFormat); |
| 1883 | lStatus = BAD_VALUE; |
| 1884 | goto Exit; |
| 1885 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1886 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1887 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1888 | lStatus = BAD_VALUE; |
| 1889 | goto Exit; |
| 1890 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1891 | break; |
| 1892 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | lStatus = initCheck(); |
| 1896 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1897 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1898 | goto Exit; |
| 1899 | } |
| 1900 | |
| 1901 | { // scope for mLock |
| 1902 | Mutex::Autolock _l(mLock); |
| 1903 | |
| 1904 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1905 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1906 | // manager |
| 1907 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 1908 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1909 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1910 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1911 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 1912 | if (sessionId == t->sessionId() && strategy != actual) { |
| 1913 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 1914 | strategy, actual); |
| 1915 | lStatus = BAD_VALUE; |
| 1916 | goto Exit; |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1921 | track = new Track(this, client, streamType, sampleRate, format, |
| 1922 | channelMask, frameCount, NULL, sharedBuffer, |
| 1923 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1924 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1925 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 1926 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1927 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1928 | // 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] | 1929 | goto Exit; |
| 1930 | } |
| 1931 | mTracks.add(track); |
| 1932 | |
| 1933 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1934 | if (chain != 0) { |
| 1935 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1936 | track->setMainBuffer(chain->inBuffer()); |
| 1937 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 1938 | chain->incTrackCnt(); |
| 1939 | } |
| 1940 | |
| 1941 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1942 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1943 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1944 | // so ask activity manager to do this on our behalf |
| 1945 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | lStatus = NO_ERROR; |
| 1950 | |
| 1951 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1952 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1953 | return track; |
| 1954 | } |
| 1955 | |
| 1956 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 1957 | { |
| 1958 | return latency; |
| 1959 | } |
| 1960 | |
| 1961 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1962 | { |
| 1963 | Mutex::Autolock _l(mLock); |
| 1964 | return latency_l(); |
| 1965 | } |
| 1966 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 1967 | { |
| 1968 | if (initCheck() == NO_ERROR) { |
| 1969 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 1970 | } else { |
| 1971 | return 0; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1976 | { |
| 1977 | Mutex::Autolock _l(mLock); |
| 1978 | // Don't apply master volume in SW if our HAL can do it for us. |
| 1979 | if (mOutput && mOutput->audioHwDev && |
| 1980 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 1981 | mMasterVolume = 1.0; |
| 1982 | } else { |
| 1983 | mMasterVolume = value; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1988 | { |
| 1989 | Mutex::Autolock _l(mLock); |
| 1990 | // Don't apply master mute in SW if our HAL can do it for us. |
| 1991 | if (mOutput && mOutput->audioHwDev && |
| 1992 | mOutput->audioHwDev->canSetMasterMute()) { |
| 1993 | mMasterMute = false; |
| 1994 | } else { |
| 1995 | mMasterMute = muted; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2000 | { |
| 2001 | Mutex::Autolock _l(mLock); |
| 2002 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2003 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2007 | { |
| 2008 | Mutex::Autolock _l(mLock); |
| 2009 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2010 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2014 | { |
| 2015 | Mutex::Autolock _l(mLock); |
| 2016 | return mStreamTypes[stream].volume; |
| 2017 | } |
| 2018 | |
| 2019 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2020 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2021 | { |
| 2022 | status_t status = ALREADY_EXISTS; |
| 2023 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2024 | if (mActiveTracks.indexOf(track) < 0) { |
| 2025 | // the track is newly added, make sure it fills up all its |
| 2026 | // buffers before playing. This is to ensure the client will |
| 2027 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2028 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2029 | TrackBase::track_state state = track->mState; |
| 2030 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2031 | status = AudioSystem::startOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2032 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2033 | mLock.lock(); |
| 2034 | // abort track was stopped/paused while we released the lock |
| 2035 | if (state != track->mState) { |
| 2036 | if (status == NO_ERROR) { |
| 2037 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2038 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2039 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2040 | mLock.lock(); |
| 2041 | } |
| 2042 | return INVALID_OPERATION; |
| 2043 | } |
| 2044 | // abort if start is rejected by audio policy manager |
| 2045 | if (status != NO_ERROR) { |
| 2046 | return PERMISSION_DENIED; |
| 2047 | } |
| 2048 | #ifdef ADD_BATTERY_DATA |
| 2049 | // to track the speaker usage |
| 2050 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2051 | #endif |
| 2052 | } |
| 2053 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2054 | // set retry count for buffer fill |
| 2055 | if (track->isOffloaded()) { |
| 2056 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2057 | } else { |
| 2058 | track->mRetryCount = kMaxTrackStartupRetries; |
| 2059 | } |
| 2060 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2061 | track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2062 | track->mResetDone = false; |
| 2063 | track->mPresentationCompleteFrames = 0; |
| 2064 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2065 | mWakeLockUids.add(track->uid()); |
| 2066 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2067 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2068 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2069 | if (chain != 0) { |
| 2070 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2071 | track->sessionId()); |
| 2072 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | status = NO_ERROR; |
| 2076 | } |
| 2077 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2078 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2079 | return status; |
| 2080 | } |
| 2081 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2082 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2083 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2084 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2085 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2086 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2087 | track->mState = TrackBase::STOPPED; |
| 2088 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2089 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2090 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2091 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2092 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2093 | |
| 2094 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2098 | { |
| 2099 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 2100 | mTracks.remove(track); |
| 2101 | deleteTrackName_l(track->name()); |
| 2102 | // redundant as track is about to be destroyed, for dumpsys only |
| 2103 | track->mName = -1; |
| 2104 | if (track->isFastTrack()) { |
| 2105 | int index = track->mFastIndex; |
| 2106 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
| 2107 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2108 | mFastTrackAvailMask |= 1 << index; |
| 2109 | // redundant as track is about to be destroyed, for dumpsys only |
| 2110 | track->mFastIndex = -1; |
| 2111 | } |
| 2112 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2113 | if (chain != 0) { |
| 2114 | chain->decTrackCnt(); |
| 2115 | } |
| 2116 | } |
| 2117 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2118 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2119 | { |
| 2120 | // Thread could be blocked waiting for async |
| 2121 | // so signal it to handle state changes immediately |
| 2122 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 2123 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 2124 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2125 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2128 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2129 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2130 | Mutex::Autolock _l(mLock); |
| 2131 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2132 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2135 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 2136 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2137 | free(s); |
| 2138 | return out_s8; |
| 2139 | } |
| 2140 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2141 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2142 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2143 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2144 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2145 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2146 | |
| 2147 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2148 | case AUDIO_OUTPUT_OPENED: |
| 2149 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2150 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2151 | desc->mChannelMask = mChannelMask; |
| 2152 | desc->mSamplingRate = mSampleRate; |
| 2153 | desc->mFormat = mFormat; |
| 2154 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2155 | // AudioFlinger::frameCount(audio_io_handle_t) |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2156 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2157 | break; |
| 2158 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2159 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2160 | default: |
| 2161 | break; |
| 2162 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2163 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2164 | } |
| 2165 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2166 | void AudioFlinger::PlaybackThread::writeCallback() |
| 2167 | { |
| 2168 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2169 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | void AudioFlinger::PlaybackThread::drainCallback() |
| 2173 | { |
| 2174 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2175 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2178 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2179 | { |
| 2180 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2181 | // reject out of sequence requests |
| 2182 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2183 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2184 | mWaitWorkCV.signal(); |
| 2185 | } |
| 2186 | } |
| 2187 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2188 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2189 | { |
| 2190 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2191 | // reject out of sequence requests |
| 2192 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2193 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2194 | mWaitWorkCV.signal(); |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | // static |
| 2199 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2200 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2201 | void *cookie) |
| 2202 | { |
| 2203 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 2204 | ALOGV("asyncCallback() event %d", event); |
| 2205 | switch (event) { |
| 2206 | case STREAM_CBK_EVENT_WRITE_READY: |
| 2207 | me->writeCallback(); |
| 2208 | break; |
| 2209 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 2210 | me->drainCallback(); |
| 2211 | break; |
| 2212 | default: |
| 2213 | ALOGW("asyncCallback() unknown event %d", event); |
| 2214 | break; |
| 2215 | } |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2219 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2220 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2221 | // 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] | 2222 | mSampleRate = mOutput->getSampleRate(); |
| 2223 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2224 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2225 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2226 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2227 | if ((mType == MIXER || mType == DUPLICATING) |
| 2228 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2229 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2230 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2231 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2232 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2233 | |
| 2234 | // Get actual HAL format. |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2235 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2236 | // Get format from the shim, which will be different than the HAL format |
| 2237 | // if playing compressed audio over HDMI passthrough. |
| 2238 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2239 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2240 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2241 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2242 | if ((mType == MIXER || mType == DUPLICATING) |
| 2243 | && !isValidPcmSinkFormat(mFormat)) { |
| 2244 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2245 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2246 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2247 | mFrameSize = mOutput->getFrameSize(); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2248 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 2249 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2250 | if (mFrameCount & 15) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2251 | 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] | 2252 | mFrameCount); |
| 2253 | } |
| 2254 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2255 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 2256 | (mOutput->stream->set_callback != NULL)) { |
| 2257 | if (mOutput->stream->set_callback(mOutput->stream, |
| 2258 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 2259 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2260 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2261 | } |
| 2262 | } |
| 2263 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2264 | mHwSupportsPause = false; |
| 2265 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2266 | if (mOutput->stream->pause != NULL) { |
| 2267 | if (mOutput->stream->resume != NULL) { |
| 2268 | mHwSupportsPause = true; |
| 2269 | } else { |
| 2270 | ALOGW("direct output implements pause but not resume"); |
| 2271 | } |
| 2272 | } else if (mOutput->stream->resume != NULL) { |
| 2273 | ALOGW("direct output implements resume but not pause"); |
| 2274 | } |
| 2275 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2276 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2277 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2278 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2279 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2280 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2281 | // For best precision, we use float instead of the associated output |
| 2282 | // device format (typically PCM 16 bit). |
| 2283 | |
| 2284 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2285 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2286 | mBufferSize = mFrameSize * mFrameCount; |
| 2287 | |
| 2288 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2289 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2290 | // (if a valid mask) to avoid premature downmix. |
| 2291 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2292 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2293 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2294 | } |
| 2295 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2296 | // 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] | 2297 | double multiplier = 1.0; |
| 2298 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2299 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2300 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2301 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2302 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2303 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2304 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2305 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2306 | maxNormalFrameCount = minNormalFrameCount; |
| 2307 | } |
| 2308 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2309 | if (multiplier <= 1.0) { |
| 2310 | multiplier = 1.0; |
| 2311 | } else if (multiplier <= 2.0) { |
| 2312 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2313 | multiplier = 2.0; |
| 2314 | } else { |
| 2315 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2316 | } |
| 2317 | } else { |
| 2318 | // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2319 | // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2320 | // track, but we sometimes have to do this to satisfy the maximum frame count |
| 2321 | // constraint) |
| 2322 | // FIXME this rounding up should not be done if no HAL SRC |
| 2323 | uint32_t truncMult = (uint32_t) multiplier; |
| 2324 | if ((truncMult & 1)) { |
| 2325 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 2326 | ++truncMult; |
| 2327 | } |
| 2328 | } |
| 2329 | multiplier = (double) truncMult; |
| 2330 | } |
| 2331 | } |
| 2332 | mNormalFrameCount = multiplier * mFrameCount; |
| 2333 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2334 | if (mType == MIXER || mType == DUPLICATING) { |
| 2335 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2336 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2337 | 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] | 2338 | mNormalFrameCount); |
| 2339 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2340 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2341 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2342 | mThreadThrottleTimeMs = 0; |
| 2343 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2344 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2345 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2346 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2347 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2348 | free(mSinkBuffer); |
| 2349 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2350 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2351 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2352 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2353 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2354 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2355 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2356 | // drives the output. |
| 2357 | free(mMixerBuffer); |
| 2358 | mMixerBuffer = NULL; |
| 2359 | if (mMixerBufferEnabled) { |
| 2360 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2361 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2362 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2363 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2364 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2365 | free(mEffectBuffer); |
| 2366 | mEffectBuffer = NULL; |
| 2367 | if (mEffectBufferEnabled) { |
| 2368 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2369 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2370 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2371 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2372 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2373 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2374 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2375 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2376 | // 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] | 2377 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2378 | // matter. |
| 2379 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2380 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2381 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2382 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2387 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2388 | { |
| 2389 | if (halFrames == NULL || dspFrames == NULL) { |
| 2390 | return BAD_VALUE; |
| 2391 | } |
| 2392 | Mutex::Autolock _l(mLock); |
| 2393 | if (initCheck() != NO_ERROR) { |
| 2394 | return INVALID_OPERATION; |
| 2395 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2396 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2397 | *halFrames = framesWritten; |
| 2398 | |
| 2399 | if (isSuspended()) { |
| 2400 | // return an estimation of rendered frames when the output is suspended |
| 2401 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2402 | *dspFrames = (uint32_t) |
| 2403 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2404 | return NO_ERROR; |
| 2405 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2406 | status_t status; |
| 2407 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2408 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2409 | *dspFrames = (size_t)frames; |
| 2410 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2414 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2415 | { |
| 2416 | Mutex::Autolock _l(mLock); |
| 2417 | uint32_t result = 0; |
| 2418 | if (getEffectChain_l(sessionId) != 0) { |
| 2419 | result = EFFECT_SESSION; |
| 2420 | } |
| 2421 | |
| 2422 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2423 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2424 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2425 | result |= TRACK_SESSION; |
| 2426 | break; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | return result; |
| 2431 | } |
| 2432 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2433 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2434 | { |
| 2435 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2436 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2437 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2438 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2439 | } |
| 2440 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2441 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2442 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2443 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2444 | } |
| 2445 | } |
| 2446 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2447 | } |
| 2448 | |
| 2449 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2450 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2451 | { |
| 2452 | Mutex::Autolock _l(mLock); |
| 2453 | return mOutput; |
| 2454 | } |
| 2455 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2456 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2457 | { |
| 2458 | Mutex::Autolock _l(mLock); |
| 2459 | AudioStreamOut *output = mOutput; |
| 2460 | mOutput = NULL; |
| 2461 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2462 | // must push a NULL and wait for ack |
| 2463 | mOutputSink.clear(); |
| 2464 | mPipeSink.clear(); |
| 2465 | mNormalSink.clear(); |
| 2466 | return output; |
| 2467 | } |
| 2468 | |
| 2469 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2470 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2471 | { |
| 2472 | if (mOutput == NULL) { |
| 2473 | return NULL; |
| 2474 | } |
| 2475 | return &mOutput->stream->common; |
| 2476 | } |
| 2477 | |
| 2478 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2479 | { |
| 2480 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2481 | } |
| 2482 | |
| 2483 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2484 | { |
| 2485 | if (!isValidSyncEvent(event)) { |
| 2486 | return BAD_VALUE; |
| 2487 | } |
| 2488 | |
| 2489 | Mutex::Autolock _l(mLock); |
| 2490 | |
| 2491 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2492 | sp<Track> track = mTracks[i]; |
| 2493 | if (event->triggerSession() == track->sessionId()) { |
| 2494 | (void) track->setSyncEvent(event); |
| 2495 | return NO_ERROR; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | return NAME_NOT_FOUND; |
| 2500 | } |
| 2501 | |
| 2502 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2503 | { |
| 2504 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2505 | } |
| 2506 | |
| 2507 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2508 | const Vector< sp<Track> >& tracksToRemove) |
| 2509 | { |
| 2510 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2511 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2512 | for (size_t i = 0 ; i < count ; i++) { |
| 2513 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2514 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2515 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2516 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2517 | #ifdef ADD_BATTERY_DATA |
| 2518 | // to track the speaker usage |
| 2519 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2520 | #endif |
| 2521 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2522 | AudioSystem::releaseOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2523 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2524 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2531 | { |
| 2532 | if (!mMasterMute) { |
| 2533 | char value[PROPERTY_VALUE_MAX]; |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2534 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 2535 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 2536 | return; |
| 2537 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2538 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2539 | char *endptr; |
| 2540 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2541 | if (*endptr == '\0' && ul != 0) { |
| 2542 | ALOGD("Silence is golden"); |
| 2543 | // The setprop command will not allow a property to be changed after |
| 2544 | // the first time it is set, so we don't have to worry about un-muting. |
| 2545 | setMasterMute_l(true); |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2552 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2553 | { |
| 2554 | // FIXME rewrite to reduce number of system calls |
| 2555 | mLastWriteTime = systemTime(); |
| 2556 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2557 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2558 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2559 | |
| 2560 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2561 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2562 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2563 | const size_t count = mBytesRemaining / mFrameSize; |
| 2564 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2565 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2566 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2567 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2568 | if (screenState != mScreenState) { |
| 2569 | mScreenState = screenState; |
| 2570 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2571 | if (pipe != NULL) { |
| 2572 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2573 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2574 | } |
| 2575 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2576 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2577 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2578 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2579 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2580 | } else { |
| 2581 | bytesWritten = framesWritten; |
| 2582 | } |
| 2583 | // otherwise use the HAL / AudioStreamOut directly |
| 2584 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2585 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2586 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2587 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2588 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2589 | mWriteAckSequence += 2; |
| 2590 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2591 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2592 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2593 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2594 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2595 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2596 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2597 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2598 | if (mUseAsyncWrite && |
| 2599 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2600 | // 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] | 2601 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2602 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2603 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2604 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2605 | } |
| 2606 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2607 | mNumWrites++; |
| 2608 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2609 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2610 | return bytesWritten; |
| 2611 | } |
| 2612 | |
| 2613 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2614 | { |
| 2615 | if (mOutput->stream->drain) { |
| 2616 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2617 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2618 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2619 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2620 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2621 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2622 | } |
| 2623 | mOutput->stream->drain(mOutput->stream, |
| 2624 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2625 | : AUDIO_DRAIN_ALL); |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2630 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2631 | { |
| 2632 | Mutex::Autolock _l(mLock); |
| 2633 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2634 | sp<Track> track = mTracks[i]; |
| 2635 | track->invalidate(); |
| 2636 | } |
| 2637 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | /* |
| 2641 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2642 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2643 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2644 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2645 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2646 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2647 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2648 | |
| 2649 | The parameters that affect these derived values are: |
| 2650 | - frame count |
| 2651 | - frame size |
| 2652 | - sample rate |
| 2653 | - device type: A2DP or not |
| 2654 | - device latency |
| 2655 | - format: PCM or not |
| 2656 | - active sleep time |
| 2657 | - idle sleep time |
| 2658 | */ |
| 2659 | |
| 2660 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2661 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2662 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2663 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2664 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2665 | |
| 2666 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2667 | // truncating audio when going to standby. |
| 2668 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2669 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2670 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2671 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2672 | } |
| 2673 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2677 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2678 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2679 | this, streamType, mTracks.size()); |
| 2680 | Mutex::Autolock _l(mLock); |
| 2681 | |
| 2682 | size_t size = mTracks.size(); |
| 2683 | for (size_t i = 0; i < size; i++) { |
| 2684 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2685 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2686 | t->invalidate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2687 | } |
| 2688 | } |
| 2689 | } |
| 2690 | |
| 2691 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2692 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2693 | audio_session_t session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2694 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2695 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2696 | bool ownsBuffer = false; |
| 2697 | |
| 2698 | 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] | 2699 | if (session > AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2700 | // 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] | 2701 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2702 | if (mType != DIRECT) { |
| 2703 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2704 | buffer = new int16_t[numSamples]; |
| 2705 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2706 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2707 | ownsBuffer = true; |
| 2708 | } |
| 2709 | |
| 2710 | // Attach all tracks with same session ID to this chain. |
| 2711 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2712 | sp<Track> track = mTracks[i]; |
| 2713 | if (session == track->sessionId()) { |
| 2714 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2715 | buffer); |
| 2716 | track->setMainBuffer(buffer); |
| 2717 | chain->incTrackCnt(); |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | // indicate all active tracks in the chain |
| 2722 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2723 | sp<Track> track = mActiveTracks[i].promote(); |
| 2724 | if (track == 0) { |
| 2725 | continue; |
| 2726 | } |
| 2727 | if (session == track->sessionId()) { |
| 2728 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2729 | chain->incActiveTrackCnt(); |
| 2730 | } |
| 2731 | } |
| 2732 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2733 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2734 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2735 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2736 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2737 | // 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] | 2738 | // 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] | 2739 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2740 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2741 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2742 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2743 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2744 | // Effect chain for other sessions are inserted at beginning of effect |
| 2745 | // 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] | 2746 | // sessions is not important. |
| 2747 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
| 2748 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, |
| 2749 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2750 | size_t size = mEffectChains.size(); |
| 2751 | size_t i = 0; |
| 2752 | for (i = 0; i < size; i++) { |
| 2753 | if (mEffectChains[i]->sessionId() < session) { |
| 2754 | break; |
| 2755 | } |
| 2756 | } |
| 2757 | mEffectChains.insertAt(chain, i); |
| 2758 | checkSuspendOnAddEffectChain_l(chain); |
| 2759 | |
| 2760 | return NO_ERROR; |
| 2761 | } |
| 2762 | |
| 2763 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2764 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2765 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2766 | |
| 2767 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2768 | |
| 2769 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2770 | if (chain == mEffectChains[i]) { |
| 2771 | mEffectChains.removeAt(i); |
| 2772 | // detach all active tracks from the chain |
| 2773 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2774 | sp<Track> track = mActiveTracks[i].promote(); |
| 2775 | if (track == 0) { |
| 2776 | continue; |
| 2777 | } |
| 2778 | if (session == track->sessionId()) { |
| 2779 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2780 | chain.get(), session); |
| 2781 | chain->decActiveTrackCnt(); |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | // detach all tracks with same session ID from this chain |
| 2786 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2787 | sp<Track> track = mTracks[i]; |
| 2788 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2789 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2790 | chain->decTrackCnt(); |
| 2791 | } |
| 2792 | } |
| 2793 | break; |
| 2794 | } |
| 2795 | } |
| 2796 | return mEffectChains.size(); |
| 2797 | } |
| 2798 | |
| 2799 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2800 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2801 | { |
| 2802 | Mutex::Autolock _l(mLock); |
| 2803 | return attachAuxEffect_l(track, EffectId); |
| 2804 | } |
| 2805 | |
| 2806 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2807 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2808 | { |
| 2809 | status_t status = NO_ERROR; |
| 2810 | |
| 2811 | if (EffectId == 0) { |
| 2812 | track->setAuxBuffer(0, NULL); |
| 2813 | } else { |
| 2814 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2815 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2816 | if (effect != 0) { |
| 2817 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2818 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2819 | } else { |
| 2820 | status = INVALID_OPERATION; |
| 2821 | } |
| 2822 | } else { |
| 2823 | status = BAD_VALUE; |
| 2824 | } |
| 2825 | } |
| 2826 | return status; |
| 2827 | } |
| 2828 | |
| 2829 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2830 | { |
| 2831 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2832 | sp<Track> track = mTracks[i]; |
| 2833 | if (track->auxEffectId() == effectId) { |
| 2834 | attachAuxEffect_l(track, 0); |
| 2835 | } |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2840 | { |
| 2841 | Vector< sp<Track> > tracksToRemove; |
| 2842 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2843 | mStandbyTimeNs = systemTime(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2844 | |
| 2845 | // MIXER |
| 2846 | nsecs_t lastWarning = 0; |
| 2847 | |
| 2848 | // DUPLICATING |
| 2849 | // FIXME could this be made local to while loop? |
| 2850 | writeFrames = 0; |
| 2851 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2852 | int lastGeneration = 0; |
| 2853 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2854 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2855 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2856 | |
| 2857 | if (mType == MIXER) { |
| 2858 | sleepTimeShift = 0; |
| 2859 | } |
| 2860 | |
| 2861 | CpuStats cpuStats; |
| 2862 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2863 | |
| 2864 | acquireWakeLock(); |
| 2865 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2866 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 2867 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2868 | // and then that string will be logged at the next convenient opportunity. |
| 2869 | const char *logString = NULL; |
| 2870 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2871 | checkSilentMode_l(); |
| 2872 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2873 | while (!exitPending()) |
| 2874 | { |
| 2875 | cpuStats.sample(myName); |
| 2876 | |
| 2877 | Vector< sp<EffectChain> > effectChains; |
| 2878 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2879 | { // scope for mLock |
| 2880 | |
| 2881 | Mutex::Autolock _l(mLock); |
| 2882 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2883 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2884 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2885 | if (logString != NULL) { |
| 2886 | mNBLogWriter->logTimestamp(); |
| 2887 | mNBLogWriter->log(logString); |
| 2888 | logString = NULL; |
| 2889 | } |
| 2890 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2891 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2892 | // and associate with the sink frames written out. We need |
| 2893 | // this to convert the sink timestamp to the track timestamp. |
| 2894 | if (mNormalSink != 0) { |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2895 | // Note: The DuplicatingThread may not have a mNormalSink. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2896 | // We always fetch the timestamp here because often the downstream |
| 2897 | // sink will block whie writing. |
| 2898 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 2899 | (void) mNormalSink->getTimestamp(timestamp); |
| 2900 | // copy over kernel info |
| 2901 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
| 2902 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 2903 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 2904 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2905 | } |
| 2906 | // mFramesWritten for non-offloaded tracks are contiguous |
| 2907 | // even after standby() is called. This is useful for the track frame |
| 2908 | // to sink frame mapping. |
| 2909 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 2910 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 2911 | const size_t size = mActiveTracks.size(); |
| 2912 | for (size_t i = 0; i < size; ++i) { |
| 2913 | sp<Track> t = mActiveTracks[i].promote(); |
| 2914 | if (t != 0 && !t->isFastTrack()) { |
| 2915 | t->updateTrackFrameInfo( |
| 2916 | t->mAudioTrackServerProxy->framesReleased(), |
| 2917 | mFramesWritten, |
| 2918 | mTimestamp); |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2919 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2922 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2923 | if (mSignalPending) { |
| 2924 | // A signal was raised while we were unlocked |
| 2925 | mSignalPending = false; |
| 2926 | } else if (waitingAsyncCallback_l()) { |
| 2927 | if (exitPending()) { |
| 2928 | break; |
| 2929 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2930 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 2931 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2932 | releaseWakeLock_l(); |
| 2933 | released = true; |
| 2934 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2935 | mWakeLockUids.clear(); |
| 2936 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2937 | ALOGV("wait async completion"); |
| 2938 | mWaitWorkCV.wait(mLock); |
| 2939 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2940 | if (released) { |
| 2941 | acquireWakeLock_l(); |
| 2942 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2943 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 2944 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2945 | |
| 2946 | continue; |
| 2947 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2948 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2949 | isSuspended()) { |
| 2950 | // put audio hardware into standby after short delay |
| 2951 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2952 | |
| 2953 | threadLoop_standby(); |
| 2954 | |
| 2955 | mStandby = true; |
| 2956 | } |
| 2957 | |
| 2958 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2959 | // we're about to wait, flush the binder command buffer |
| 2960 | IPCThreadState::self()->flushCommands(); |
| 2961 | |
| 2962 | clearOutputTracks(); |
| 2963 | |
| 2964 | if (exitPending()) { |
| 2965 | break; |
| 2966 | } |
| 2967 | |
| 2968 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2969 | mWakeLockUids.clear(); |
| 2970 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2971 | // wait until we have something to do... |
| 2972 | ALOGV("%s going to sleep", myName.string()); |
| 2973 | mWaitWorkCV.wait(mLock); |
| 2974 | ALOGV("%s waking up", myName.string()); |
| 2975 | acquireWakeLock_l(); |
| 2976 | |
| 2977 | mMixerStatus = MIXER_IDLE; |
| 2978 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 2979 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2980 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2981 | checkSilentMode_l(); |
| 2982 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2983 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 2984 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2985 | if (mType == MIXER) { |
| 2986 | sleepTimeShift = 0; |
| 2987 | } |
| 2988 | |
| 2989 | continue; |
| 2990 | } |
| 2991 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2992 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 2993 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 2994 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2995 | // compare with previously applied list |
| 2996 | if (lastGeneration != mActiveTracksGeneration) { |
| 2997 | // update wakelock |
| 2998 | updateWakeLockUids_l(mWakeLockUids); |
| 2999 | lastGeneration = mActiveTracksGeneration; |
| 3000 | } |
| 3001 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3002 | // prevent any changes in effect chain list and in each effect chain |
| 3003 | // during mixing and effect process as the audio buffers could be deleted |
| 3004 | // or modified if an effect is created or deleted |
| 3005 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3006 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3007 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3008 | if (mBytesRemaining == 0) { |
| 3009 | mCurrentWriteLength = 0; |
| 3010 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3011 | // threadLoop_mix() sets mCurrentWriteLength |
| 3012 | threadLoop_mix(); |
| 3013 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3014 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3015 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3016 | // must be written to HAL |
| 3017 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3018 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3019 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3020 | } |
| 3021 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3022 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3023 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3024 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3025 | // or mSinkBuffer (if there are no effects). |
| 3026 | // |
| 3027 | // This is done pre-effects computation; if effects change to |
| 3028 | // support higher precision, this needs to move. |
| 3029 | // |
| 3030 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3031 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3032 | if (mMixerBufferValid) { |
| 3033 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3034 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3035 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3036 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3037 | // and is handled here if we're going directly to the sink. |
| 3038 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3039 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3040 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3041 | } |
| 3042 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3043 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3044 | mNormalFrameCount * mChannelCount); |
| 3045 | } |
| 3046 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3047 | mBytesRemaining = mCurrentWriteLength; |
| 3048 | if (isSuspended()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3049 | mSleepTimeUs = suspendSleepTimeUs(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3050 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3051 | mBytesWritten += mSinkBufferSize; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3052 | mFramesWritten += mSinkBufferSize / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3053 | mBytesRemaining = 0; |
| 3054 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3055 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3056 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3057 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3058 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3059 | effectChains[i]->process_l(); |
| 3060 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3061 | } |
| 3062 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3063 | // Process effect chains for offloaded thread even if no audio |
| 3064 | // was read from audio track: process only updates effect state |
| 3065 | // and thus does have to be synchronized with audio writes but may have |
| 3066 | // to be called while waiting for async write callback |
| 3067 | if (mType == OFFLOAD) { |
| 3068 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3069 | effectChains[i]->process_l(); |
| 3070 | } |
| 3071 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3072 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3073 | // Only if the Effects buffer is enabled and there is data in the |
| 3074 | // Effects buffer (buffer valid), we need to |
| 3075 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3076 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3077 | if (mEffectBufferValid) { |
| 3078 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3079 | |
| 3080 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3081 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3082 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3083 | } |
| 3084 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3085 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3086 | mNormalFrameCount * mChannelCount); |
| 3087 | } |
| 3088 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3089 | // enable changes in effect chain |
| 3090 | unlockEffectChains(effectChains); |
| 3091 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3092 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3093 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3094 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3095 | ssize_t ret = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3096 | if (mBytesRemaining) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3097 | ret = threadLoop_write(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3098 | if (ret < 0) { |
| 3099 | mBytesRemaining = 0; |
| 3100 | } else { |
| 3101 | mBytesWritten += ret; |
| 3102 | mBytesRemaining -= ret; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3103 | mFramesWritten += ret / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3104 | } |
| 3105 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3106 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3107 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3108 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3109 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3110 | // write blocked detection |
| 3111 | nsecs_t now = systemTime(); |
| 3112 | nsecs_t delta = now - mLastWriteTime; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3113 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3114 | mNumDelayedWrites++; |
| 3115 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 3116 | ATRACE_NAME("underrun"); |
| 3117 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3118 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3119 | lastWarning = now; |
| 3120 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3121 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3122 | |
| 3123 | if (mThreadThrottle |
| 3124 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3125 | && ret > 0) { // we wrote something |
| 3126 | // Limit MixerThread data processing to no more than twice the |
| 3127 | // expected processing rate. |
| 3128 | // |
| 3129 | // This helps prevent underruns with NuPlayer and other applications |
| 3130 | // which may set up buffers that are close to the minimum size, or use |
| 3131 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3132 | // |
| 3133 | // The throttle smooths out sudden large data drains from the device, |
| 3134 | // e.g. when it comes out of standby, which often causes problems with |
| 3135 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3136 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3137 | // the app won't fill fast enough to handle the sudden draw). |
| 3138 | |
| 3139 | const int32_t deltaMs = delta / 1000000; |
| 3140 | const int32_t throttleMs = mHalfBufferMs - deltaMs; |
| 3141 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3142 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3143 | // notify of throttle start on verbose log |
| 3144 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3145 | "mixer(%p) throttle begin:" |
| 3146 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3147 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3148 | mThreadThrottleTimeMs += throttleMs; |
| 3149 | } else { |
| 3150 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3151 | if (diff > 0) { |
| 3152 | // notify of throttle end on debug log |
| 3153 | ALOGD("mixer(%p) throttle end: throttle time(%u)", this, diff); |
| 3154 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3155 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3156 | } |
| 3157 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3158 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3159 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3160 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3161 | ATRACE_BEGIN("sleep"); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3162 | if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
| 3163 | Mutex::Autolock _l(mLock); |
| 3164 | if (!mSignalPending && !exitPending()) { |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 3165 | // If more than one buffer has been written to the audio HAL since exiting |
| 3166 | // standby or last flush, do not sleep more than one buffer duration |
| 3167 | // since last write and not less than kDirectMinSleepTimeUs. |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3168 | // Wake up if a command is received |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3169 | uint32_t timeoutUs = mSleepTimeUs; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 3170 | if (mBytesWritten >= (int64_t) mBufferSize) { |
| 3171 | nsecs_t now = systemTime(); |
| 3172 | uint32_t deltaUs = (uint32_t)((now - mLastWriteTime) / 1000); |
| 3173 | if (timeoutUs + deltaUs > mBufferDurationUs) { |
| 3174 | if (mBufferDurationUs > deltaUs) { |
| 3175 | timeoutUs = mBufferDurationUs - deltaUs; |
| 3176 | if (timeoutUs < kDirectMinSleepTimeUs) { |
| 3177 | timeoutUs = kDirectMinSleepTimeUs; |
| 3178 | } |
| 3179 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3180 | timeoutUs = kDirectMinSleepTimeUs; |
| 3181 | } |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)timeoutUs)); |
| 3185 | } |
| 3186 | } else { |
| 3187 | usleep(mSleepTimeUs); |
| 3188 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3189 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3190 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | // Finally let go of removed track(s), without the lock held |
| 3194 | // since we can't guarantee the destructors won't acquire that |
| 3195 | // same lock. This will also mutate and push a new fast mixer state. |
| 3196 | threadLoop_removeTracks(tracksToRemove); |
| 3197 | tracksToRemove.clear(); |
| 3198 | |
| 3199 | // FIXME I don't understand the need for this here; |
| 3200 | // it was in the original code but maybe the |
| 3201 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3202 | clearOutputTracks(); |
| 3203 | |
| 3204 | // Effect chains will be actually deleted here if they were removed from |
| 3205 | // mEffectChains list during mixing or effects processing |
| 3206 | effectChains.clear(); |
| 3207 | |
| 3208 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3209 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3210 | } |
| 3211 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3212 | threadLoop_exit(); |
| 3213 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3214 | if (!mStandby) { |
| 3215 | threadLoop_standby(); |
| 3216 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3220 | mWakeLockUids.clear(); |
| 3221 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3222 | |
| 3223 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3224 | return false; |
| 3225 | } |
| 3226 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3227 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3228 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3229 | { |
| 3230 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3231 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3232 | for (size_t i=0 ; i<count ; i++) { |
| 3233 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3234 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3235 | mWakeLockUids.remove(track->uid()); |
| 3236 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3237 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3238 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3239 | if (chain != 0) { |
| 3240 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3241 | track->sessionId()); |
| 3242 | chain->decActiveTrackCnt(); |
| 3243 | } |
| 3244 | if (track->isTerminated()) { |
| 3245 | removeTrack_l(track); |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3251 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3252 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3253 | { |
| 3254 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3255 | ExtendedTimestamp ets; |
| 3256 | status_t status = mNormalSink->getTimestamp(ets); |
| 3257 | if (status == NO_ERROR) { |
| 3258 | status = ets.getBestTimestamp(×tamp); |
| 3259 | } |
| 3260 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3261 | } |
Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3262 | if ((mType == OFFLOAD || mType == DIRECT) |
| 3263 | && mOutput != NULL && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3264 | uint64_t position64; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3265 | int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3266 | if (ret == 0) { |
| 3267 | timestamp.mPosition = (uint32_t)position64; |
| 3268 | return NO_ERROR; |
| 3269 | } |
| 3270 | } |
| 3271 | return INVALID_OPERATION; |
| 3272 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3273 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3274 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3275 | audio_patch_handle_t *handle) |
| 3276 | { |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3277 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3278 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3279 | status_t status = PlaybackThread::createAudioPatch_l(patch, handle); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3280 | |
| 3281 | return status; |
| 3282 | } |
| 3283 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3284 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3285 | audio_patch_handle_t *handle) |
| 3286 | { |
| 3287 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3288 | |
| 3289 | // store new device and send to effects |
| 3290 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3291 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3292 | type |= patch->sinks[i].ext.device.type; |
| 3293 | } |
| 3294 | |
| 3295 | #ifdef ADD_BATTERY_DATA |
| 3296 | // when changing the audio output device, call addBatteryData to notify |
| 3297 | // the change |
| 3298 | if (mOutDevice != type) { |
| 3299 | uint32_t params = 0; |
| 3300 | // check whether speaker is on |
| 3301 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3302 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3305 | audio_devices_t deviceWithoutSpeaker |
| 3306 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3307 | // check if any other device (except speaker) is on |
| 3308 | if (type & deviceWithoutSpeaker) { |
| 3309 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3310 | } |
| 3311 | |
| 3312 | if (params != 0) { |
| 3313 | addBatteryData(params); |
| 3314 | } |
| 3315 | } |
| 3316 | #endif |
| 3317 | |
| 3318 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3319 | mEffectChains[i]->setDevice_l(type); |
| 3320 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3321 | |
| 3322 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3323 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3324 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3325 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3326 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3327 | |
| 3328 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3329 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3330 | status = hwDevice->create_audio_patch(hwDevice, |
| 3331 | patch->num_sources, |
| 3332 | patch->sources, |
| 3333 | patch->num_sinks, |
| 3334 | patch->sinks, |
| 3335 | handle); |
| 3336 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3337 | char *address; |
| 3338 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3339 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3340 | address = audio_device_address_to_parameter( |
| 3341 | patch->sinks[0].ext.device.type, |
| 3342 | patch->sinks[0].ext.device.address); |
| 3343 | } else { |
| 3344 | address = (char *)calloc(1, 1); |
| 3345 | } |
| 3346 | AudioParameter param = AudioParameter(String8(address)); |
| 3347 | free(address); |
| 3348 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
| 3349 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3350 | param.toString().string()); |
| 3351 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3352 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3353 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3354 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3355 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3356 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3357 | return status; |
| 3358 | } |
| 3359 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3360 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3361 | { |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3362 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3363 | |
| 3364 | status_t status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3365 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3366 | return status; |
| 3367 | } |
| 3368 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3369 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3370 | { |
| 3371 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3372 | |
| 3373 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3374 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3375 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 3376 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3377 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 3378 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3379 | AudioParameter param; |
| 3380 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 3381 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3382 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3383 | } |
| 3384 | return status; |
| 3385 | } |
| 3386 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3387 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3388 | { |
| 3389 | Mutex::Autolock _l(mLock); |
| 3390 | mTracks.add(track); |
| 3391 | } |
| 3392 | |
| 3393 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3394 | { |
| 3395 | Mutex::Autolock _l(mLock); |
| 3396 | destroyTrack_l(track); |
| 3397 | } |
| 3398 | |
| 3399 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3400 | { |
| 3401 | ThreadBase::getAudioPortConfig(config); |
| 3402 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3403 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3404 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3405 | } |
| 3406 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3407 | // ---------------------------------------------------------------------------- |
| 3408 | |
| 3409 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3410 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3411 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3412 | // mAudioMixer below |
| 3413 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3414 | mFastMixerFutex(0), |
| 3415 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3416 | // mOutputSink below |
| 3417 | // mPipeSink below |
| 3418 | // mNormalSink below |
| 3419 | { |
| 3420 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3421 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%zu, " |
| 3422 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3423 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3424 | mNormalFrameCount); |
| 3425 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3426 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3427 | if (type == DUPLICATING) { |
| 3428 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3429 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3430 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3431 | return; |
| 3432 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3433 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 3434 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 3435 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3436 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3437 | #if !LOG_NDEBUG |
| 3438 | ssize_t index = |
| 3439 | #else |
| 3440 | (void) |
| 3441 | #endif |
| 3442 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3443 | ALOG_ASSERT(index == 0); |
| 3444 | |
| 3445 | // initialize fast mixer depending on configuration |
| 3446 | bool initFastMixer; |
| 3447 | switch (kUseFastMixer) { |
| 3448 | case FastMixer_Never: |
| 3449 | initFastMixer = false; |
| 3450 | break; |
| 3451 | case FastMixer_Always: |
| 3452 | initFastMixer = true; |
| 3453 | break; |
| 3454 | case FastMixer_Static: |
| 3455 | case FastMixer_Dynamic: |
| 3456 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 3457 | break; |
| 3458 | } |
| 3459 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3460 | audio_format_t fastMixerFormat; |
| 3461 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3462 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3463 | } else { |
| 3464 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3465 | } |
| 3466 | if (mFormat != fastMixerFormat) { |
| 3467 | // change our Sink format to accept our intermediate precision |
| 3468 | mFormat = fastMixerFormat; |
| 3469 | free(mSinkBuffer); |
| 3470 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3471 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3472 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3473 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3474 | |
| 3475 | // create a MonoPipe to connect our submix to FastMixer |
| 3476 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3477 | #ifdef TEE_SINK |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3478 | NBAIO_Format origformat = format; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3479 | #endif |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3480 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3481 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3482 | format.mFormat = fastMixerFormat; |
| 3483 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3484 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3485 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3486 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3487 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3488 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3489 | const NBAIO_Format offers[1] = {format}; |
| 3490 | size_t numCounterOffers = 0; |
Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame^] | 3491 | #if !LOG_NDEBUG || defined(TEE_SINK) |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3492 | ssize_t index = |
| 3493 | #else |
| 3494 | (void) |
| 3495 | #endif |
| 3496 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3497 | ALOG_ASSERT(index == 0); |
| 3498 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3499 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3500 | mPipeSink = monoPipe; |
| 3501 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3502 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3503 | if (mTeeSinkOutputEnabled) { |
| 3504 | // 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] | 3505 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3506 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3507 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3508 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3509 | ALOG_ASSERT(index == 0); |
| 3510 | mTeeSink = teeSink; |
| 3511 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3512 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3513 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3514 | ALOG_ASSERT(index == 0); |
| 3515 | mTeeSource = teeSource; |
| 3516 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3517 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3518 | |
| 3519 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3520 | mFastMixer = new FastMixer(); |
| 3521 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3522 | #ifdef STATE_QUEUE_DUMP |
| 3523 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3524 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3525 | #endif |
| 3526 | FastMixerState *state = sq->begin(); |
| 3527 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3528 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3529 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3530 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3531 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3532 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3533 | fastTrack->mGeneration++; |
| 3534 | state->mFastTracksGen++; |
| 3535 | state->mTrackMask = 1; |
| 3536 | // fast mixer will use the HAL output sink |
| 3537 | state->mOutputSink = mOutputSink.get(); |
| 3538 | state->mOutputSinkGen++; |
| 3539 | state->mFrameCount = mFrameCount; |
| 3540 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3541 | // already done in constructor initialization list |
| 3542 | //mFastMixerFutex = 0; |
| 3543 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3544 | state->mColdGen++; |
| 3545 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3546 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3547 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3548 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3549 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3550 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3551 | sq->end(); |
| 3552 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3553 | |
| 3554 | // start the fast mixer |
| 3555 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3556 | pid_t tid = mFastMixer->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3557 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3558 | |
| 3559 | #ifdef AUDIO_WATCHDOG |
| 3560 | // create and start the watchdog |
| 3561 | mAudioWatchdog = new AudioWatchdog(); |
| 3562 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3563 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3564 | tid = mAudioWatchdog->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3565 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3566 | #endif |
| 3567 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | switch (kUseFastMixer) { |
| 3571 | case FastMixer_Never: |
| 3572 | case FastMixer_Dynamic: |
| 3573 | mNormalSink = mOutputSink; |
| 3574 | break; |
| 3575 | case FastMixer_Always: |
| 3576 | mNormalSink = mPipeSink; |
| 3577 | break; |
| 3578 | case FastMixer_Static: |
| 3579 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3580 | break; |
| 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | AudioFlinger::MixerThread::~MixerThread() |
| 3585 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3586 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3587 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3588 | FastMixerState *state = sq->begin(); |
| 3589 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3590 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3591 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3592 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3593 | } |
| 3594 | } |
| 3595 | state->mCommand = FastMixerState::EXIT; |
| 3596 | sq->end(); |
| 3597 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3598 | mFastMixer->join(); |
| 3599 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3600 | // We'll use that extract the final state which contains one remaining fast track |
| 3601 | // corresponding to our sub-mix. |
| 3602 | state = sq->begin(); |
| 3603 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3604 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3605 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3606 | delete fastTrack->mBufferProvider; |
| 3607 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3608 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3609 | #ifdef AUDIO_WATCHDOG |
| 3610 | if (mAudioWatchdog != 0) { |
| 3611 | mAudioWatchdog->requestExit(); |
| 3612 | mAudioWatchdog->requestExitAndWait(); |
| 3613 | mAudioWatchdog.clear(); |
| 3614 | } |
| 3615 | #endif |
| 3616 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3617 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3618 | delete mAudioMixer; |
| 3619 | } |
| 3620 | |
| 3621 | |
| 3622 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3623 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3624 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3625 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3626 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3627 | } |
| 3628 | return latency; |
| 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3633 | { |
| 3634 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3635 | } |
| 3636 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3637 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3638 | { |
| 3639 | // FIXME we should only do one push per cycle; confirm this is true |
| 3640 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3641 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3642 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3643 | FastMixerState *state = sq->begin(); |
| 3644 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3645 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3646 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3647 | |
| 3648 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 3649 | ATRACE_BEGIN("write"); |
| 3650 | mOutput->write((char *)mSinkBuffer, 0); |
| 3651 | ATRACE_END(); |
| 3652 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3653 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3654 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3655 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3656 | } |
| 3657 | #ifdef AUDIO_WATCHDOG |
| 3658 | if (mAudioWatchdog != 0) { |
| 3659 | mAudioWatchdog->resume(); |
| 3660 | } |
| 3661 | #endif |
| 3662 | } |
| 3663 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3664 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3665 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3666 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3667 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3668 | sq->end(); |
| 3669 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3670 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3671 | mNormalSink = mPipeSink; |
| 3672 | } |
| 3673 | } else { |
| 3674 | sq->end(false /*didModify*/); |
| 3675 | } |
| 3676 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3677 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3681 | { |
| 3682 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3683 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3684 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3685 | FastMixerState *state = sq->begin(); |
| 3686 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3687 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3688 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3689 | state->mColdGen++; |
| 3690 | mFastMixerFutex = 0; |
| 3691 | sq->end(); |
| 3692 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3693 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3694 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3695 | mNormalSink = mOutputSink; |
| 3696 | } |
| 3697 | #ifdef AUDIO_WATCHDOG |
| 3698 | if (mAudioWatchdog != 0) { |
| 3699 | mAudioWatchdog->pause(); |
| 3700 | } |
| 3701 | #endif |
| 3702 | } else { |
| 3703 | sq->end(false /*didModify*/); |
| 3704 | } |
| 3705 | } |
| 3706 | PlaybackThread::threadLoop_standby(); |
| 3707 | } |
| 3708 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3709 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3710 | { |
| 3711 | return false; |
| 3712 | } |
| 3713 | |
| 3714 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3715 | { |
| 3716 | return !mStandby; |
| 3717 | } |
| 3718 | |
| 3719 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3720 | { |
| 3721 | Mutex::Autolock _l(mLock); |
| 3722 | return waitingAsyncCallback_l(); |
| 3723 | } |
| 3724 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3725 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3726 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3727 | { |
| 3728 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3729 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3730 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3731 | // discard any pending drain or write ack by incrementing sequence |
| 3732 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3733 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3734 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3735 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3736 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3737 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3738 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3739 | } |
| 3740 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3741 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3742 | { |
| 3743 | ALOGV("signal playback thread"); |
| 3744 | broadcast_l(); |
| 3745 | } |
| 3746 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3747 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3748 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3749 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3750 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3751 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3752 | // increase sleep time progressively when application underrun condition clears. |
| 3753 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3754 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3755 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3756 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3757 | sleepTimeShift--; |
| 3758 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3759 | mSleepTimeUs = 0; |
| 3760 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3761 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3762 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3766 | { |
| 3767 | // If no tracks are ready, sleep once for the duration of an output |
| 3768 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3769 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3770 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3771 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 3772 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 3773 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3774 | } |
| 3775 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3776 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3777 | // duration we would end up writing less data than needed by the audio HAL if |
| 3778 | // the condition persists. |
| 3779 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3780 | sleepTimeShift++; |
| 3781 | } |
| 3782 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3783 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3784 | } |
| 3785 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3786 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3787 | // before effects processing or output. |
| 3788 | if (mMixerBufferValid) { |
| 3789 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3790 | } else { |
| 3791 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3792 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3793 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3794 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3795 | "anticipated start"); |
| 3796 | } |
| 3797 | // TODO add standby time extension fct of effect tail |
| 3798 | } |
| 3799 | |
| 3800 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3801 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3802 | Vector< sp<Track> > *tracksToRemove) |
| 3803 | { |
| 3804 | |
| 3805 | mixer_state mixerStatus = MIXER_IDLE; |
| 3806 | // find out which tracks need to be processed |
| 3807 | size_t count = mActiveTracks.size(); |
| 3808 | size_t mixedTracks = 0; |
| 3809 | size_t tracksWithEffect = 0; |
| 3810 | // counts only _active_ fast tracks |
| 3811 | size_t fastTracks = 0; |
| 3812 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3813 | |
| 3814 | float masterVolume = mMasterVolume; |
| 3815 | bool masterMute = mMasterMute; |
| 3816 | |
| 3817 | if (masterMute) { |
| 3818 | masterVolume = 0; |
| 3819 | } |
| 3820 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3821 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3822 | if (chain != 0) { |
| 3823 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 3824 | chain->setVolume_l(&v, &v); |
| 3825 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 3826 | chain.clear(); |
| 3827 | } |
| 3828 | |
| 3829 | // prepare a new state to push |
| 3830 | FastMixerStateQueue *sq = NULL; |
| 3831 | FastMixerState *state = NULL; |
| 3832 | bool didModify = false; |
| 3833 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3834 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3835 | sq = mFastMixer->sq(); |
| 3836 | state = sq->begin(); |
| 3837 | } |
| 3838 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3839 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3840 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3841 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3842 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3843 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3844 | if (t == 0) { |
| 3845 | continue; |
| 3846 | } |
| 3847 | |
| 3848 | // this const just means the local variable doesn't change |
| 3849 | Track* const track = t.get(); |
| 3850 | |
| 3851 | // process fast tracks |
| 3852 | if (track->isFastTrack()) { |
| 3853 | |
| 3854 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 3855 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 3856 | // the track never becomes active so it's fast mixer slot is never touched. |
| 3857 | // The converse, of removing an (active) track and then creating a new track |
| 3858 | // at the identical fast mixer slot within the same normal mix cycle, |
| 3859 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 3860 | int j = track->mFastIndex; |
| 3861 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); |
| 3862 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 3863 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 3864 | |
| 3865 | // Determine whether the track is currently in underrun condition, |
| 3866 | // and whether it had a recent underrun. |
| 3867 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 3868 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 3869 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 3870 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 3871 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 3872 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 3873 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 3874 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 3875 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 3876 | track->mObservedUnderruns = underruns; |
| 3877 | // don't count underruns that occur while stopping or pausing |
| 3878 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3879 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 3880 | recentUnderruns > 0) { |
| 3881 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 3882 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 3883 | } else { |
| 3884 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | // This is similar to the state machine for normal tracks, |
| 3888 | // with a few modifications for fast tracks. |
| 3889 | bool isActive = true; |
| 3890 | switch (track->mState) { |
| 3891 | case TrackBase::STOPPING_1: |
| 3892 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3893 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3894 | track->mState = TrackBase::STOPPING_2; |
| 3895 | } |
| 3896 | break; |
| 3897 | case TrackBase::PAUSING: |
| 3898 | // ramp down is not yet implemented |
| 3899 | track->setPaused(); |
| 3900 | break; |
| 3901 | case TrackBase::RESUMING: |
| 3902 | // ramp up is not yet implemented |
| 3903 | track->mState = TrackBase::ACTIVE; |
| 3904 | break; |
| 3905 | case TrackBase::ACTIVE: |
| 3906 | if (recentFull > 0 || recentPartial > 0) { |
| 3907 | // track has provided at least some frames recently: reset retry count |
| 3908 | track->mRetryCount = kMaxTrackRetries; |
| 3909 | } |
| 3910 | if (recentUnderruns == 0) { |
| 3911 | // no recent underruns: stay active |
| 3912 | break; |
| 3913 | } |
| 3914 | // there has recently been an underrun of some kind |
| 3915 | if (track->sharedBuffer() == 0) { |
| 3916 | // were any of the recent underruns "empty" (no frames available)? |
| 3917 | if (recentEmpty == 0) { |
| 3918 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 3919 | break; |
| 3920 | } |
| 3921 | // there has recently been an "empty" underrun: decrement the retry counter |
| 3922 | if (--(track->mRetryCount) > 0) { |
| 3923 | break; |
| 3924 | } |
| 3925 | // indicate to client process that the track was disabled because of underrun; |
| 3926 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 3927 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3928 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 3929 | isActive = false; |
| 3930 | break; |
| 3931 | } |
| 3932 | // fall through |
| 3933 | case TrackBase::STOPPING_2: |
| 3934 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3935 | case TrackBase::STOPPED: |
| 3936 | case TrackBase::FLUSHED: // flush() while active |
| 3937 | // Check for presentation complete if track is inactive |
| 3938 | // We have consumed all the buffers of this track. |
| 3939 | // This would be incomplete if we auto-paused on underrun |
| 3940 | { |
| 3941 | size_t audioHALFrames = |
| 3942 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3943 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3944 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 3945 | // track stays in active list until presentation is complete |
| 3946 | break; |
| 3947 | } |
| 3948 | } |
| 3949 | if (track->isStopping_2()) { |
| 3950 | track->mState = TrackBase::STOPPED; |
| 3951 | } |
| 3952 | if (track->isStopped()) { |
| 3953 | // Can't reset directly, as fast mixer is still polling this track |
| 3954 | // track->reset(); |
| 3955 | // So instead mark this track as needing to be reset after push with ack |
| 3956 | resetMask |= 1 << i; |
| 3957 | } |
| 3958 | isActive = false; |
| 3959 | break; |
| 3960 | case TrackBase::IDLE: |
| 3961 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3962 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | if (isActive) { |
| 3966 | // was it previously inactive? |
| 3967 | if (!(state->mTrackMask & (1 << j))) { |
| 3968 | ExtendedAudioBufferProvider *eabp = track; |
| 3969 | VolumeProvider *vp = track; |
| 3970 | fastTrack->mBufferProvider = eabp; |
| 3971 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3972 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3973 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3974 | fastTrack->mGeneration++; |
| 3975 | state->mTrackMask |= 1 << j; |
| 3976 | didModify = true; |
| 3977 | // no acknowledgement required for newly active tracks |
| 3978 | } |
| 3979 | // cache the combined master volume and stream type volume for fast mixer; this |
| 3980 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3981 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3982 | ++fastTracks; |
| 3983 | } else { |
| 3984 | // was it previously active? |
| 3985 | if (state->mTrackMask & (1 << j)) { |
| 3986 | fastTrack->mBufferProvider = NULL; |
| 3987 | fastTrack->mGeneration++; |
| 3988 | state->mTrackMask &= ~(1 << j); |
| 3989 | didModify = true; |
| 3990 | // If any fast tracks were removed, we must wait for acknowledgement |
| 3991 | // because we're about to decrement the last sp<> on those tracks. |
| 3992 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3993 | } else { |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 3994 | LOG_ALWAYS_FATAL("fast track %d should have been active; " |
| 3995 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 3996 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 3997 | track->sharedBuffer() != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3998 | } |
| 3999 | tracksToRemove->add(track); |
| 4000 | // Avoids a misleading display in dumpsys |
| 4001 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 4002 | } |
| 4003 | continue; |
| 4004 | } |
| 4005 | |
| 4006 | { // local variable scope to avoid goto warning |
| 4007 | |
| 4008 | audio_track_cblk_t* cblk = track->cblk(); |
| 4009 | |
| 4010 | // The first time a track is added we wait |
| 4011 | // for all its buffers to be filled before processing it |
| 4012 | int name = track->name(); |
| 4013 | // make sure that we have enough frames to mix one full buffer. |
| 4014 | // enforce this condition only once to enable draining the buffer in case the client |
| 4015 | // app does not call stop() and relies on underrun to stop: |
| 4016 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4017 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4018 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4019 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4020 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4021 | |
| 4022 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4023 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4024 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4025 | // add frames already consumed but not yet released by the resampler |
| 4026 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4027 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4028 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4029 | uint32_t minFrames = 1; |
| 4030 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4031 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4032 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4033 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4034 | |
| 4035 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4036 | if (ATRACE_ENABLED()) { |
| 4037 | // I wish we had formatted trace names |
| 4038 | char traceName[16]; |
| 4039 | strcpy(traceName, "nRdy"); |
| 4040 | int name = track->name(); |
| 4041 | if (AudioMixer::TRACK0 <= name && |
| 4042 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 4043 | name -= AudioMixer::TRACK0; |
| 4044 | traceName[4] = (name / 10) + '0'; |
| 4045 | traceName[5] = (name % 10) + '0'; |
| 4046 | } else { |
| 4047 | traceName[4] = '?'; |
| 4048 | traceName[5] = '?'; |
| 4049 | } |
| 4050 | traceName[6] = '\0'; |
| 4051 | ATRACE_INT(traceName, framesReady); |
| 4052 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4053 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4054 | !track->isPaused() && !track->isTerminated()) |
| 4055 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4056 | 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] | 4057 | |
| 4058 | mixedTracks++; |
| 4059 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4060 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4061 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4062 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4063 | if (track->mainBuffer() != mSinkBuffer && |
| 4064 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4065 | if (mEffectBufferEnabled) { |
| 4066 | mEffectBufferValid = true; // Later can set directly. |
| 4067 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4068 | chain = getEffectChain_l(track->sessionId()); |
| 4069 | // Delegate volume control to effect in track effect chain if needed |
| 4070 | if (chain != 0) { |
| 4071 | tracksWithEffect++; |
| 4072 | } else { |
| 4073 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4074 | "session %d", |
| 4075 | name, track->sessionId()); |
| 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | |
| 4080 | int param = AudioMixer::VOLUME; |
| 4081 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4082 | // no ramp for the first volume setting |
| 4083 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4084 | if (track->mState == TrackBase::RESUMING) { |
| 4085 | track->mState = TrackBase::ACTIVE; |
| 4086 | param = AudioMixer::RAMP_VOLUME; |
| 4087 | } |
| 4088 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4089 | // FIXME should not make a decision based on mServer |
| 4090 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4091 | // If the track is stopped before the first frame was mixed, |
| 4092 | // do not apply ramp |
| 4093 | param = AudioMixer::RAMP_VOLUME; |
| 4094 | } |
| 4095 | |
| 4096 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4097 | uint32_t vl, vr; // in U8.24 integer format |
| 4098 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4099 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4100 | vl = vr = 0; |
| 4101 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4102 | if (track->isPausing()) { |
| 4103 | track->setPaused(); |
| 4104 | } |
| 4105 | } else { |
| 4106 | |
| 4107 | // read original volumes with volume control |
| 4108 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4109 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4110 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4111 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4112 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4113 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4114 | // 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] | 4115 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4116 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4117 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4118 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4119 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4120 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4121 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4122 | } |
| 4123 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4124 | vlf *= v; |
| 4125 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4126 | // 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] | 4127 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4128 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4129 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4130 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4131 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4132 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4133 | // send level comes from shared memory and so may be corrupt |
| 4134 | if (sendLevel > MAX_GAIN_INT) { |
| 4135 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4136 | sendLevel = MAX_GAIN_INT; |
| 4137 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4138 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4139 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4140 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4141 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4142 | // Delegate volume control to effect in track effect chain if needed |
| 4143 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4144 | // Do not ramp volume if volume is controlled by effect |
| 4145 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4146 | // Update remaining floating point volume levels |
| 4147 | vlf = (float)vl / (1 << 24); |
| 4148 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4149 | track->mHasVolumeController = true; |
| 4150 | } else { |
| 4151 | // force no volume ramp when volume controller was just disabled or removed |
| 4152 | // from effect chain to avoid volume spike |
| 4153 | if (track->mHasVolumeController) { |
| 4154 | param = AudioMixer::VOLUME; |
| 4155 | } |
| 4156 | track->mHasVolumeController = false; |
| 4157 | } |
| 4158 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4159 | // XXX: these things DON'T need to be done each time |
| 4160 | mAudioMixer->setBufferProvider(name, track); |
| 4161 | mAudioMixer->enable(name); |
| 4162 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4163 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4164 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4165 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4166 | mAudioMixer->setParameter( |
| 4167 | name, |
| 4168 | AudioMixer::TRACK, |
| 4169 | AudioMixer::FORMAT, (void *)track->format()); |
| 4170 | mAudioMixer->setParameter( |
| 4171 | name, |
| 4172 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4173 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4174 | mAudioMixer->setParameter( |
| 4175 | name, |
| 4176 | AudioMixer::TRACK, |
| 4177 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4178 | // 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] | 4179 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4180 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4181 | if (reqSampleRate == 0) { |
| 4182 | reqSampleRate = mSampleRate; |
| 4183 | } else if (reqSampleRate > maxSampleRate) { |
| 4184 | reqSampleRate = maxSampleRate; |
| 4185 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4186 | mAudioMixer->setParameter( |
| 4187 | name, |
| 4188 | AudioMixer::RESAMPLE, |
| 4189 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4190 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4191 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4192 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4193 | mAudioMixer->setParameter( |
| 4194 | name, |
| 4195 | AudioMixer::TIMESTRETCH, |
| 4196 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4197 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4198 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4199 | /* |
| 4200 | * Select the appropriate output buffer for the track. |
| 4201 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4202 | * Tracks with effects go into their own effects chain buffer |
| 4203 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4204 | * |
| 4205 | * Other tracks can use mMixerBuffer for higher precision |
| 4206 | * channel accumulation. If this buffer is enabled |
| 4207 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4208 | * into it. |
| 4209 | * |
| 4210 | */ |
| 4211 | if (mMixerBufferEnabled |
| 4212 | && (track->mainBuffer() == mSinkBuffer |
| 4213 | || track->mainBuffer() == mMixerBuffer)) { |
| 4214 | mAudioMixer->setParameter( |
| 4215 | name, |
| 4216 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4217 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4218 | mAudioMixer->setParameter( |
| 4219 | name, |
| 4220 | AudioMixer::TRACK, |
| 4221 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4222 | // TODO: override track->mainBuffer()? |
| 4223 | mMixerBufferValid = true; |
| 4224 | } else { |
| 4225 | mAudioMixer->setParameter( |
| 4226 | name, |
| 4227 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4228 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4229 | mAudioMixer->setParameter( |
| 4230 | name, |
| 4231 | AudioMixer::TRACK, |
| 4232 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4233 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4234 | mAudioMixer->setParameter( |
| 4235 | name, |
| 4236 | AudioMixer::TRACK, |
| 4237 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4238 | |
| 4239 | // reset retry count |
| 4240 | track->mRetryCount = kMaxTrackRetries; |
| 4241 | |
| 4242 | // If one track is ready, set the mixer ready if: |
| 4243 | // - the mixer was not ready during previous round OR |
| 4244 | // - no other track is not ready |
| 4245 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4246 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4247 | mixerStatus = MIXER_TRACKS_READY; |
| 4248 | } |
| 4249 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4250 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4251 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4252 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4253 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4254 | } else { |
| 4255 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4256 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4257 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4258 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4259 | // previous audio buffer again to effects |
| 4260 | chain = getEffectChain_l(track->sessionId()); |
| 4261 | if (chain != 0) { |
| 4262 | chain->clearInputBuffer(); |
| 4263 | } |
| 4264 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4265 | 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] | 4266 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4267 | track->isStopped() || track->isPaused()) { |
| 4268 | // We have consumed all the buffers of this track. |
| 4269 | // Remove it from the list of active tracks. |
| 4270 | // TODO: use actual buffer filling status instead of latency when available from |
| 4271 | // audio HAL |
| 4272 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4273 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4274 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4275 | if (track->isStopped()) { |
| 4276 | track->reset(); |
| 4277 | } |
| 4278 | tracksToRemove->add(track); |
| 4279 | } |
| 4280 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4281 | // No buffers for this track. Give it a few chances to |
| 4282 | // fill a buffer, then remove it from active list. |
| 4283 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4284 | 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] | 4285 | tracksToRemove->add(track); |
| 4286 | // indicate to client process that the track was disabled because of underrun; |
| 4287 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4288 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4289 | // If one track is not ready, mark the mixer also not ready if: |
| 4290 | // - the mixer was ready during previous round OR |
| 4291 | // - no other track is ready |
| 4292 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4293 | mixerStatus != MIXER_TRACKS_READY) { |
| 4294 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4295 | } |
| 4296 | } |
| 4297 | mAudioMixer->disable(name); |
| 4298 | } |
| 4299 | |
| 4300 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4301 | |
| 4302 | } |
| 4303 | |
| 4304 | // Push the new FastMixer state if necessary |
| 4305 | bool pauseAudioWatchdog = false; |
| 4306 | if (didModify) { |
| 4307 | state->mFastTracksGen++; |
| 4308 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4309 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4310 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4311 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4312 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4313 | state->mColdGen++; |
| 4314 | mFastMixerFutex = 0; |
| 4315 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4316 | mNormalSink = mOutputSink; |
| 4317 | } |
| 4318 | // If we go into cold idle, need to wait for acknowledgement |
| 4319 | // so that fast mixer stops doing I/O. |
| 4320 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4321 | pauseAudioWatchdog = true; |
| 4322 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4323 | } |
| 4324 | if (sq != NULL) { |
| 4325 | sq->end(didModify); |
| 4326 | sq->push(block); |
| 4327 | } |
| 4328 | #ifdef AUDIO_WATCHDOG |
| 4329 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4330 | mAudioWatchdog->pause(); |
| 4331 | } |
| 4332 | #endif |
| 4333 | |
| 4334 | // Now perform the deferred reset on fast tracks that have stopped |
| 4335 | while (resetMask != 0) { |
| 4336 | size_t i = __builtin_ctz(resetMask); |
| 4337 | ALOG_ASSERT(i < count); |
| 4338 | resetMask &= ~(1 << i); |
| 4339 | sp<Track> t = mActiveTracks[i].promote(); |
| 4340 | if (t == 0) { |
| 4341 | continue; |
| 4342 | } |
| 4343 | Track* track = t.get(); |
| 4344 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4345 | track->reset(); |
| 4346 | } |
| 4347 | |
| 4348 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4349 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4350 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4351 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4352 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4356 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4357 | // passing a non-clean buffer to the effect chain |
| 4358 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4359 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4360 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4361 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4362 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4363 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4364 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4365 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4366 | if (mMixerBufferValid) { |
| 4367 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4368 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4369 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4370 | // after mixing. |
| 4371 | // |
| 4372 | // To enforce this guarantee: |
| 4373 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4374 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4375 | // must imply MIXER_TRACKS_READY. |
| 4376 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4377 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4378 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4379 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | // if any fast tracks, then status is ready |
| 4383 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4384 | if (fastTracks > 0) { |
| 4385 | mixerStatus = MIXER_TRACKS_READY; |
| 4386 | } |
| 4387 | return mixerStatus; |
| 4388 | } |
| 4389 | |
| 4390 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4391 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 4392 | audio_format_t format, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4393 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4394 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 4398 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 4399 | { |
| 4400 | ALOGV("remove track (%d) and delete from mixer", name); |
| 4401 | mAudioMixer->deleteTrackName(name); |
| 4402 | } |
| 4403 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4404 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4405 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4406 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4407 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4408 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4409 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4410 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4411 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4412 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4413 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4414 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4415 | AudioParameter param = AudioParameter(keyValuePair); |
| 4416 | int value; |
| 4417 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4418 | reconfig = true; |
| 4419 | } |
| 4420 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4421 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4422 | status = BAD_VALUE; |
| 4423 | } else { |
| 4424 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4425 | reconfig = true; |
| 4426 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4427 | } |
| 4428 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4429 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4430 | status = BAD_VALUE; |
| 4431 | } else { |
| 4432 | // no need to save value, since it's constant |
| 4433 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4434 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4435 | } |
| 4436 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4437 | // do not accept frame count changes if tracks are open as the track buffer |
| 4438 | // size depends on frame count and correct behavior would not be guaranteed |
| 4439 | // if frame count is changed after track creation |
| 4440 | if (!mTracks.isEmpty()) { |
| 4441 | status = INVALID_OPERATION; |
| 4442 | } else { |
| 4443 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4444 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4445 | } |
| 4446 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4447 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4448 | // when changing the audio output device, call addBatteryData to notify |
| 4449 | // the change |
| 4450 | if (mOutDevice != value) { |
| 4451 | uint32_t params = 0; |
| 4452 | // check whether speaker is on |
| 4453 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4454 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4455 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4456 | |
| 4457 | audio_devices_t deviceWithoutSpeaker |
| 4458 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4459 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4460 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4461 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4462 | } |
| 4463 | |
| 4464 | if (params != 0) { |
| 4465 | addBatteryData(params); |
| 4466 | } |
| 4467 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4468 | #endif |
| 4469 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4470 | // forward device change to effects that have requested to be |
| 4471 | // aware of attached audio device. |
| 4472 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4473 | a2dpDeviceChanged = |
| 4474 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4475 | mOutDevice = value; |
| 4476 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4477 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4478 | } |
| 4479 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4480 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4481 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4482 | if (status == NO_ERROR) { |
| 4483 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4484 | keyValuePair.string()); |
| 4485 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4486 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4487 | mStandby = true; |
| 4488 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4489 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4490 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4491 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4492 | if (status == NO_ERROR && reconfig) { |
| 4493 | readOutputParameters_l(); |
| 4494 | delete mAudioMixer; |
| 4495 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4496 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4497 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 4498 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4499 | if (name < 0) { |
| 4500 | break; |
| 4501 | } |
| 4502 | mTracks[i]->mName = name; |
| 4503 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4504 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4505 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4506 | } |
| 4507 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4508 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | |
| 4512 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4513 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4514 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4515 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4516 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4517 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4518 | |
| 4519 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 4520 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 4521 | // This is a large object so we place it on the heap. |
| 4522 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 4523 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 4524 | copy->dump(fd); |
| 4525 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4526 | |
| 4527 | #ifdef STATE_QUEUE_DUMP |
| 4528 | // Similar for state queue |
| 4529 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4530 | observerCopy.dump(fd); |
| 4531 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4532 | mutatorCopy.dump(fd); |
| 4533 | #endif |
| 4534 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4535 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4536 | // Write the tee output to a .wav file |
| 4537 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4538 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4539 | |
| 4540 | #ifdef AUDIO_WATCHDOG |
| 4541 | if (mAudioWatchdog != 0) { |
| 4542 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4543 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4544 | wdCopy.dump(fd); |
| 4545 | } |
| 4546 | #endif |
| 4547 | } |
| 4548 | |
| 4549 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4550 | { |
| 4551 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4552 | } |
| 4553 | |
| 4554 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4555 | { |
| 4556 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4557 | } |
| 4558 | |
| 4559 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4560 | { |
| 4561 | PlaybackThread::cacheParameters_l(); |
| 4562 | |
| 4563 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4564 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4565 | // increase threshold again due to low power audio mode. The way this warning |
| 4566 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4567 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4568 | } |
| 4569 | |
| 4570 | // ---------------------------------------------------------------------------- |
| 4571 | |
| 4572 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4573 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady, |
| 4574 | uint32_t bitRate) |
| 4575 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady, bitRate) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4576 | // mLeftVolFloat, mRightVolFloat |
| 4577 | { |
| 4578 | } |
| 4579 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4580 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4581 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4582 | ThreadBase::type_t type, bool systemReady, uint32_t bitRate) |
| 4583 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady, bitRate) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4584 | // mLeftVolFloat, mRightVolFloat |
| 4585 | { |
| 4586 | } |
| 4587 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4588 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4589 | { |
| 4590 | } |
| 4591 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4592 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 4593 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4594 | float left, right; |
| 4595 | |
| 4596 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4597 | left = right = 0; |
| 4598 | } else { |
| 4599 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4600 | float v = mMasterVolume * typeVolume; |
| 4601 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4602 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4603 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4604 | if (left > GAIN_FLOAT_UNITY) { |
| 4605 | left = GAIN_FLOAT_UNITY; |
| 4606 | } |
| 4607 | left *= v; |
| 4608 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4609 | if (right > GAIN_FLOAT_UNITY) { |
| 4610 | right = GAIN_FLOAT_UNITY; |
| 4611 | } |
| 4612 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | if (lastTrack) { |
| 4616 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4617 | mLeftVolFloat = left; |
| 4618 | mRightVolFloat = right; |
| 4619 | |
| 4620 | // Convert volumes from float to 8.24 |
| 4621 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4622 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4623 | |
| 4624 | // Delegate volume control to effect in track effect chain if needed |
| 4625 | // only one effect chain can be present on DirectOutputThread, so if |
| 4626 | // there is one, the track is connected to it |
| 4627 | if (!mEffectChains.isEmpty()) { |
| 4628 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4629 | left = (float)vl / (1 << 24); |
| 4630 | right = (float)vr / (1 << 24); |
| 4631 | } |
| 4632 | if (mOutput->stream->set_volume) { |
| 4633 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 4634 | } |
| 4635 | } |
| 4636 | } |
| 4637 | } |
| 4638 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4639 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 4640 | { |
| 4641 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4642 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4643 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4644 | if (previousTrack != 0 && latestTrack != 0) { |
| 4645 | if (mType == DIRECT) { |
| 4646 | if (previousTrack.get() != latestTrack.get()) { |
| 4647 | mFlushPending = true; |
| 4648 | } |
| 4649 | } else /* mType == OFFLOAD */ { |
| 4650 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 4651 | mFlushPending = true; |
| 4652 | } |
| 4653 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4654 | } |
| 4655 | PlaybackThread::onAddNewTrack_l(); |
| 4656 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4657 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4658 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4659 | Vector< sp<Track> > *tracksToRemove |
| 4660 | ) |
| 4661 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4662 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4663 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4664 | bool doHwPause = false; |
| 4665 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4666 | |
| 4667 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4668 | for (size_t i = 0; i < count; i++) { |
| 4669 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4670 | // The track died recently |
| 4671 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4672 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4673 | } |
| 4674 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4675 | if (t->isInvalid()) { |
| 4676 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4677 | tracksToRemove->add(t); |
| 4678 | continue; |
| 4679 | } |
| 4680 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4681 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4682 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4683 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4684 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4685 | // Only consider last track started for volume and mixer state control. |
| 4686 | // In theory an older track could underrun and restart after the new one starts |
| 4687 | // but as we only care about the transition phase between two tracks on a |
| 4688 | // direct output, it is not a problem to ignore the underrun case. |
| 4689 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4690 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4691 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4692 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4693 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4694 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4695 | doHwPause = true; |
| 4696 | mHwPaused = true; |
| 4697 | } |
| 4698 | tracksToRemove->add(track); |
| 4699 | } else if (track->isFlushPending()) { |
| 4700 | track->flushAck(); |
| 4701 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4702 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4703 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4704 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4705 | track->resumeAck(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4706 | if (last && mHwPaused) { |
| 4707 | doHwResume = true; |
| 4708 | mHwPaused = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4712 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4713 | // for all its buffers to be filled before processing it. |
| 4714 | // Allow draining the buffer in case the client |
| 4715 | // app does not call stop() and relies on underrun to stop: |
| 4716 | // hence the test on (track->mRetryCount > 1). |
| 4717 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4718 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4719 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4720 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4721 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4722 | minFrames = mNormalFrameCount; |
| 4723 | } else { |
| 4724 | minFrames = 1; |
| 4725 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4726 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4727 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4728 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4729 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4730 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4731 | |
| 4732 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4733 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4734 | // make sure processVolume_l() will apply new volume even if 0 |
| 4735 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4736 | if (!mHwSupportsPause) { |
| 4737 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4742 | processVolume_l(track, last); |
| 4743 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4744 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4745 | if (previousTrack != 0) { |
| 4746 | if (track != previousTrack.get()) { |
| 4747 | // Flush any data still being written from last track |
| 4748 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4749 | // Invalidate previous track to force a seek when resuming. |
| 4750 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4751 | } |
| 4752 | } |
| 4753 | mPreviousTrack = track; |
| 4754 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4755 | // reset retry count |
| 4756 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4757 | mActiveTrack = t; |
| 4758 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4759 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4760 | doHwResume = true; |
| 4761 | mHwPaused = false; |
| 4762 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4763 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4764 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4765 | // clear effect chain input buffer if the last active track started underruns |
| 4766 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4767 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4768 | mEffectChains[0]->clearInputBuffer(); |
| 4769 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4770 | if (track->isStopping_1()) { |
| 4771 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4772 | if (last && mHwPaused) { |
| 4773 | doHwResume = true; |
| 4774 | mHwPaused = false; |
| 4775 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4776 | } |
| 4777 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4778 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4779 | // We have consumed all the buffers of this track. |
| 4780 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4781 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4782 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4783 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4784 | } else { |
| 4785 | audioHALFrames = 0; |
| 4786 | } |
| 4787 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4788 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4789 | if (mStandby || !last || |
| 4790 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4791 | if (track->isStopping_2()) { |
| 4792 | track->mState = TrackBase::STOPPED; |
| 4793 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4794 | if (track->isStopped()) { |
| 4795 | track->reset(); |
| 4796 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4797 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4798 | } |
| 4799 | } else { |
| 4800 | // No buffers for this track. Give it a few chances to |
| 4801 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4802 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4803 | if (--(track->mRetryCount) <= 0) { |
| 4804 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4805 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4806 | // indicate to client process that the track was disabled because of underrun; |
| 4807 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4808 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4809 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4810 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 4811 | "minFrames = %u, mFormat = %#x", |
| 4812 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4813 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4814 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4815 | doHwPause = true; |
| 4816 | mHwPaused = true; |
| 4817 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4818 | } |
| 4819 | } |
| 4820 | } |
| 4821 | } |
| 4822 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4823 | // 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] | 4824 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4825 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4826 | if (mTracks[i]->isFlushPending()) { |
| 4827 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4828 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4829 | } |
| 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4834 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4835 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4836 | // if resume is received before pause is executed. |
| 4837 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4838 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4839 | mOutput->stream->pause(mOutput->stream); |
| 4840 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4841 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4842 | flushHw_l(); |
| 4843 | } |
| 4844 | if (mHwSupportsPause && !mStandby && doHwResume) { |
| 4845 | mOutput->stream->resume(mOutput->stream); |
| 4846 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4847 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4848 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4849 | |
| 4850 | return mixerStatus; |
| 4851 | } |
| 4852 | |
| 4853 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 4854 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4855 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4856 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4857 | // output audio to hardware |
| 4858 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4859 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4860 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4861 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 4862 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4863 | // no need to pad with 0 for compressed audio |
| 4864 | if (audio_has_proportional_frames(mFormat)) { |
| 4865 | memset(curBuf, 0, frameCount * mFrameSize); |
| 4866 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4867 | break; |
| 4868 | } |
| 4869 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 4870 | frameCount -= buffer.frameCount; |
| 4871 | curBuf += buffer.frameCount * mFrameSize; |
| 4872 | mActiveTrack->releaseBuffer(&buffer); |
| 4873 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4874 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4875 | mSleepTimeUs = 0; |
| 4876 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4877 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 4881 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4882 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4883 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4884 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4885 | return; |
| 4886 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4887 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4888 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4889 | // For compressed offload, use faster sleep time when underruning until more than an |
| 4890 | // entire buffer was written to the audio HAL |
| 4891 | if (!audio_has_proportional_frames(mFormat) && |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 4892 | (mType == OFFLOAD) && (mBytesWritten < (int64_t) mBufferSize)) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4893 | mSleepTimeUs = kDirectMinSleepTimeUs; |
| 4894 | } else { |
| 4895 | mSleepTimeUs = mActiveSleepTimeUs; |
| 4896 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4897 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4898 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4899 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4900 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4901 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4902 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4906 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 4907 | { |
| 4908 | { |
| 4909 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4910 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4911 | if (mTracks[i]->isFlushPending()) { |
| 4912 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4913 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4914 | } |
| 4915 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4916 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4917 | flushHw_l(); |
| 4918 | } |
| 4919 | } |
| 4920 | PlaybackThread::threadLoop_exit(); |
| 4921 | } |
| 4922 | |
| 4923 | // must be called with thread mutex locked |
| 4924 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 4925 | { |
| 4926 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4927 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4928 | |
vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 4929 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { |
| 4930 | return !mStandby; |
| 4931 | } |
| 4932 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4933 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 4934 | // after a timeout and we will enter standby then. |
| 4935 | if (mTracks.size() > 0) { |
| 4936 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4937 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 4938 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4939 | } |
| 4940 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4941 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4942 | } |
| 4943 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4944 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4945 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 4946 | audio_format_t format __unused, audio_session_t sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4947 | { |
| 4948 | return 0; |
| 4949 | } |
| 4950 | |
| 4951 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4952 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4953 | { |
| 4954 | } |
| 4955 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4956 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4957 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4958 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4959 | { |
| 4960 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4961 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4962 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4963 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4964 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4965 | AudioParameter param = AudioParameter(keyValuePair); |
| 4966 | int value; |
| 4967 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4968 | // forward device change to effects that have requested to be |
| 4969 | // aware of attached audio device. |
| 4970 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4971 | a2dpDeviceChanged = |
| 4972 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4973 | mOutDevice = value; |
| 4974 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4975 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4976 | } |
| 4977 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4978 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4979 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4980 | // do not accept frame count changes if tracks are open as the track buffer |
| 4981 | // size depends on frame count and correct behavior would not be garantied |
| 4982 | // if frame count is changed after track creation |
| 4983 | if (!mTracks.isEmpty()) { |
| 4984 | status = INVALID_OPERATION; |
| 4985 | } else { |
| 4986 | reconfig = true; |
| 4987 | } |
| 4988 | } |
| 4989 | if (status == NO_ERROR) { |
| 4990 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4991 | keyValuePair.string()); |
| 4992 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4993 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4994 | mStandby = true; |
| 4995 | mBytesWritten = 0; |
| 4996 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4997 | keyValuePair.string()); |
| 4998 | } |
| 4999 | if (status == NO_ERROR && reconfig) { |
| 5000 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5001 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5002 | } |
| 5003 | } |
| 5004 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5005 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5009 | { |
| 5010 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5011 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5012 | time = PlaybackThread::activeSleepTimeUs(); |
| 5013 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5014 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5015 | } |
| 5016 | return time; |
| 5017 | } |
| 5018 | |
| 5019 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5020 | { |
| 5021 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5022 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5023 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5024 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5025 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5026 | } |
| 5027 | return time; |
| 5028 | } |
| 5029 | |
| 5030 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5031 | { |
| 5032 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5033 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5034 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5035 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5036 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5037 | } |
| 5038 | return time; |
| 5039 | } |
| 5040 | |
| 5041 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5042 | { |
| 5043 | PlaybackThread::cacheParameters_l(); |
| 5044 | |
| 5045 | // use shorter standby delay as on normal output to release |
| 5046 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5047 | // no delay on outputs with HW A/V sync |
| 5048 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5049 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5050 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5051 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5052 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5053 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5054 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5055 | } |
| 5056 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5057 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5058 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5059 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5060 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5061 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5062 | } |
| 5063 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5064 | // ---------------------------------------------------------------------------- |
| 5065 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5066 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5067 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5068 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5069 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5070 | mWriteAckSequence(0), |
| 5071 | mDrainSequence(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5072 | { |
| 5073 | } |
| 5074 | |
| 5075 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5076 | { |
| 5077 | } |
| 5078 | |
| 5079 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5080 | { |
| 5081 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5082 | } |
| 5083 | |
| 5084 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5085 | { |
| 5086 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5087 | uint32_t writeAckSequence; |
| 5088 | uint32_t drainSequence; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5089 | |
| 5090 | { |
| 5091 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5092 | while (!((mWriteAckSequence & 1) || |
| 5093 | (mDrainSequence & 1) || |
| 5094 | exitPending())) { |
| 5095 | mWaitWorkCV.wait(mLock); |
| 5096 | } |
| 5097 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5098 | if (exitPending()) { |
| 5099 | break; |
| 5100 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5101 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5102 | mWriteAckSequence, mDrainSequence); |
| 5103 | writeAckSequence = mWriteAckSequence; |
| 5104 | mWriteAckSequence &= ~1; |
| 5105 | drainSequence = mDrainSequence; |
| 5106 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5107 | } |
| 5108 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5109 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5110 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5111 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5112 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5113 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5114 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5115 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5116 | } |
| 5117 | } |
| 5118 | } |
| 5119 | } |
| 5120 | return false; |
| 5121 | } |
| 5122 | |
| 5123 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5124 | { |
| 5125 | ALOGV("AsyncCallbackThread::exit"); |
| 5126 | Mutex::Autolock _l(mLock); |
| 5127 | requestExit(); |
| 5128 | mWaitWorkCV.broadcast(); |
| 5129 | } |
| 5130 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5131 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5132 | { |
| 5133 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5134 | // bit 0 is cleared |
| 5135 | mWriteAckSequence = sequence << 1; |
| 5136 | } |
| 5137 | |
| 5138 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5139 | { |
| 5140 | Mutex::Autolock _l(mLock); |
| 5141 | // ignore unexpected callbacks |
| 5142 | if (mWriteAckSequence & 2) { |
| 5143 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5144 | mWaitWorkCV.signal(); |
| 5145 | } |
| 5146 | } |
| 5147 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5148 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5149 | { |
| 5150 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5151 | // bit 0 is cleared |
| 5152 | mDrainSequence = sequence << 1; |
| 5153 | } |
| 5154 | |
| 5155 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5156 | { |
| 5157 | Mutex::Autolock _l(mLock); |
| 5158 | // ignore unexpected callbacks |
| 5159 | if (mDrainSequence & 2) { |
| 5160 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5161 | mWaitWorkCV.signal(); |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | |
| 5166 | // ---------------------------------------------------------------------------- |
| 5167 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5168 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady, |
| 5169 | uint32_t bitRate) |
| 5170 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady, bitRate), |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5171 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5172 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5173 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 5174 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5175 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5176 | } |
| 5177 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5178 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5179 | { |
| 5180 | if (mFlushPending || mHwPaused) { |
| 5181 | // If a flush is pending or track was paused, just discard buffered data |
| 5182 | flushHw_l(); |
| 5183 | } else { |
| 5184 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5185 | threadLoop_drain(); |
| 5186 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5187 | if (mUseAsyncWrite) { |
| 5188 | ALOG_ASSERT(mCallbackThread != 0); |
| 5189 | mCallbackThread->exit(); |
| 5190 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5191 | PlaybackThread::threadLoop_exit(); |
| 5192 | } |
| 5193 | |
| 5194 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5195 | Vector< sp<Track> > *tracksToRemove |
| 5196 | ) |
| 5197 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5198 | size_t count = mActiveTracks.size(); |
| 5199 | |
| 5200 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5201 | bool doHwPause = false; |
| 5202 | bool doHwResume = false; |
| 5203 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5204 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5205 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5206 | // find out which tracks need to be processed |
| 5207 | for (size_t i = 0; i < count; i++) { |
| 5208 | sp<Track> t = mActiveTracks[i].promote(); |
| 5209 | // The track died recently |
| 5210 | if (t == 0) { |
| 5211 | continue; |
| 5212 | } |
| 5213 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5214 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5215 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5216 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5217 | // Only consider last track started for volume and mixer state control. |
| 5218 | // In theory an older track could underrun and restart after the new one starts |
| 5219 | // but as we only care about the transition phase between two tracks on a |
| 5220 | // direct output, it is not a problem to ignore the underrun case. |
| 5221 | sp<Track> l = mLatestActiveTrack.promote(); |
| 5222 | bool last = l.get() == track; |
| 5223 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5224 | if (track->isInvalid()) { |
| 5225 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5226 | tracksToRemove->add(track); |
| 5227 | continue; |
| 5228 | } |
| 5229 | |
| 5230 | if (track->mState == TrackBase::IDLE) { |
| 5231 | ALOGW("An idle track shouldn't be in active list"); |
| 5232 | continue; |
| 5233 | } |
| 5234 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5235 | if (track->isPausing()) { |
| 5236 | track->setPaused(); |
| 5237 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5238 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5239 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5240 | mHwPaused = true; |
| 5241 | } |
| 5242 | // If we were part way through writing the mixbuffer to |
| 5243 | // the HAL we must save this until we resume |
| 5244 | // BUG - this will be wrong if a different track is made active, |
| 5245 | // in that case we want to discard the pending data in the |
| 5246 | // mixbuffer and tell the client to present it again when the |
| 5247 | // track is resumed |
| 5248 | mPausedWriteLength = mCurrentWriteLength; |
| 5249 | mPausedBytesRemaining = mBytesRemaining; |
| 5250 | mBytesRemaining = 0; // stop writing |
| 5251 | } |
| 5252 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5253 | } else if (track->isFlushPending()) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5254 | track->mRetryCount = kMaxTrackRetriesOffload; |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5255 | track->flushAck(); |
| 5256 | if (last) { |
| 5257 | mFlushPending = true; |
| 5258 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5259 | } else if (track->isResumePending()){ |
| 5260 | track->resumeAck(); |
| 5261 | if (last) { |
| 5262 | if (mPausedBytesRemaining) { |
| 5263 | // Need to continue write that was interrupted |
| 5264 | mCurrentWriteLength = mPausedWriteLength; |
| 5265 | mBytesRemaining = mPausedBytesRemaining; |
| 5266 | mPausedBytesRemaining = 0; |
| 5267 | } |
| 5268 | if (mHwPaused) { |
| 5269 | doHwResume = true; |
| 5270 | mHwPaused = false; |
| 5271 | // threadLoop_mix() will handle the case that we need to |
| 5272 | // resume an interrupted write |
| 5273 | } |
| 5274 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5275 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5276 | |
| 5277 | // Do not handle new data in this iteration even if track->framesReady() |
| 5278 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5279 | } |
| 5280 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5281 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5282 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5283 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5284 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5285 | // make sure processVolume_l() will apply new volume even if 0 |
| 5286 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5290 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5291 | if (previousTrack != 0) { |
| 5292 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5293 | // Flush any data still being written from last track |
| 5294 | mBytesRemaining = 0; |
| 5295 | if (mPausedBytesRemaining) { |
| 5296 | // Last track was paused so we also need to flush saved |
| 5297 | // mixbuffer state and invalidate track so that it will |
| 5298 | // re-submit that unwritten data when it is next resumed |
| 5299 | mPausedBytesRemaining = 0; |
| 5300 | // Invalidate is a bit drastic - would be more efficient |
| 5301 | // to have a flag to tell client that some of the |
| 5302 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5303 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5304 | } |
| 5305 | // flush data already sent to the DSP if changing audio session as audio |
| 5306 | // comes from a different source. Also invalidate previous track to force a |
| 5307 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5308 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5309 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5310 | } |
| 5311 | } |
| 5312 | } |
| 5313 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5314 | // reset retry count |
| 5315 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5316 | mActiveTrack = t; |
| 5317 | mixerStatus = MIXER_TRACKS_READY; |
| 5318 | } |
| 5319 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5320 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5321 | if (track->isStopping_1()) { |
| 5322 | // Hardware buffer can hold a large amount of audio so we must |
| 5323 | // wait for all current track's data to drain before we say |
| 5324 | // that the track is stopped. |
| 5325 | if (mBytesRemaining == 0) { |
| 5326 | // Only start draining when all data in mixbuffer |
| 5327 | // has been written |
| 5328 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5329 | track->mState = TrackBase::STOPPING_2; // so presentation completes after drain |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5330 | // do not drain if no data was ever sent to HAL (mStandby == true) |
| 5331 | if (last && !mStandby) { |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5332 | // do not modify drain sequence if we are already draining. This happens |
| 5333 | // when resuming from pause after drain. |
| 5334 | if ((mDrainSequence & 1) == 0) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5335 | mSleepTimeUs = 0; |
| 5336 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5337 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5338 | mDrainSequence += 2; |
| 5339 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5340 | if (mHwPaused) { |
| 5341 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5342 | // a resume so we must ensure hardware is running |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5343 | doHwResume = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5344 | mHwPaused = false; |
| 5345 | } |
| 5346 | } |
| 5347 | } |
| 5348 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5349 | // Drain has completed or we are in standby, signal presentation complete |
| 5350 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5351 | track->mState = TrackBase::STOPPED; |
| 5352 | size_t audioHALFrames = |
| 5353 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5354 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5355 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5356 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5357 | track->reset(); |
| 5358 | tracksToRemove->add(track); |
| 5359 | } |
| 5360 | } else { |
| 5361 | // No buffers for this track. Give it a few chances to |
| 5362 | // fill a buffer, then remove it from active list. |
| 5363 | if (--(track->mRetryCount) <= 0) { |
| 5364 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5365 | track->name()); |
| 5366 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5367 | // indicate to client process that the track was disabled because of underrun; |
| 5368 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5369 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5370 | } else if (last){ |
| 5371 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5372 | } |
| 5373 | } |
| 5374 | } |
| 5375 | // compute volume for this track |
| 5376 | processVolume_l(track, last); |
| 5377 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5378 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5379 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5380 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5381 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5382 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5383 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5384 | mOutput->stream->pause(mOutput->stream); |
| 5385 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5386 | if (mFlushPending) { |
| 5387 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5388 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5389 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5390 | mOutput->stream->resume(mOutput->stream); |
| 5391 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5392 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5393 | // remove all the tracks that need to be... |
| 5394 | removeTracks_l(*tracksToRemove); |
| 5395 | |
| 5396 | return mixerStatus; |
| 5397 | } |
| 5398 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5399 | // must be called with thread mutex locked |
| 5400 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5401 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5402 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5403 | mWriteAckSequence, mDrainSequence); |
| 5404 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5405 | return true; |
| 5406 | } |
| 5407 | return false; |
| 5408 | } |
| 5409 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5410 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5411 | { |
| 5412 | Mutex::Autolock _l(mLock); |
| 5413 | return waitingAsyncCallback_l(); |
| 5414 | } |
| 5415 | |
| 5416 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5417 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5418 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5419 | // Flush anything still waiting in the mixbuffer |
| 5420 | mCurrentWriteLength = 0; |
| 5421 | mBytesRemaining = 0; |
| 5422 | mPausedWriteLength = 0; |
| 5423 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5424 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 5425 | mBytesWritten = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5426 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5427 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5428 | // discard any pending drain or write ack by incrementing sequence |
| 5429 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5430 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5431 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5432 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5433 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5434 | } |
| 5435 | } |
| 5436 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5437 | uint32_t AudioFlinger::OffloadThread::activeSleepTimeUs() const |
| 5438 | { |
| 5439 | uint32_t time; |
| 5440 | if (audio_has_proportional_frames(mFormat)) { |
| 5441 | time = PlaybackThread::activeSleepTimeUs(); |
| 5442 | } else { |
| 5443 | // sleep time is half the duration of an audio HAL buffer. |
| 5444 | // Note: This can be problematic in case of underrun with variable bit rate and |
| 5445 | // current rate is much less than initial rate. |
| 5446 | time = (uint32_t)max(kDirectMinSleepTimeUs, mBufferDurationUs / 2); |
| 5447 | } |
| 5448 | return time; |
| 5449 | } |
| 5450 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5451 | // ---------------------------------------------------------------------------- |
| 5452 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5453 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5454 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5455 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5456 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5457 | mWaitTimeMs(UINT_MAX) |
| 5458 | { |
| 5459 | addOutputTrack(mainThread); |
| 5460 | } |
| 5461 | |
| 5462 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5463 | { |
| 5464 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5465 | mOutputTracks[i]->destroy(); |
| 5466 | } |
| 5467 | } |
| 5468 | |
| 5469 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5470 | { |
| 5471 | // mix buffers... |
| 5472 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5473 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5474 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5475 | if (mMixerBufferValid) { |
| 5476 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5477 | } else { |
| 5478 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5479 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5480 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5481 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5482 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5483 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5484 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5488 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5489 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5490 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5491 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5492 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5493 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5494 | } |
| 5495 | } else if (mBytesWritten != 0) { |
| 5496 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5497 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5498 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5499 | } else { |
| 5500 | // flush remaining overflow buffers in output tracks |
| 5501 | writeFrames = 0; |
| 5502 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5503 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5504 | } |
| 5505 | } |
| 5506 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5507 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5508 | { |
| 5509 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5510 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5511 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5512 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5513 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5514 | } |
| 5515 | |
| 5516 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5517 | { |
| 5518 | // DuplicatingThread implements standby by stopping all tracks |
| 5519 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5520 | outputTracks[i]->stop(); |
| 5521 | } |
| 5522 | } |
| 5523 | |
| 5524 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5525 | { |
| 5526 | outputTracks = mOutputTracks; |
| 5527 | } |
| 5528 | |
| 5529 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5530 | { |
| 5531 | outputTracks.clear(); |
| 5532 | } |
| 5533 | |
| 5534 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5535 | { |
| 5536 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5537 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5538 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5539 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5540 | const size_t frameCount = |
| 5541 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5542 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5543 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5544 | // nearly empty and the other may be dropping data). |
| 5545 | |
| 5546 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5547 | this, |
| 5548 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5549 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5550 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5551 | frameCount, |
| 5552 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5553 | if (outputTrack->cblk() != NULL) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5554 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5555 | mOutputTracks.add(outputTrack); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5556 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5557 | updateWaitTime_l(); |
| 5558 | } |
| 5559 | } |
| 5560 | |
| 5561 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5562 | { |
| 5563 | Mutex::Autolock _l(mLock); |
| 5564 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5565 | if (mOutputTracks[i]->thread() == thread) { |
| 5566 | mOutputTracks[i]->destroy(); |
| 5567 | mOutputTracks.removeAt(i); |
| 5568 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5569 | if (thread->getOutput() == mOutput) { |
| 5570 | mOutput = NULL; |
| 5571 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5572 | return; |
| 5573 | } |
| 5574 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5575 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | // caller must hold mLock |
| 5579 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5580 | { |
| 5581 | mWaitTimeMs = UINT_MAX; |
| 5582 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5583 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5584 | if (strong != 0) { |
| 5585 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5586 | if (waitTimeMs < mWaitTimeMs) { |
| 5587 | mWaitTimeMs = waitTimeMs; |
| 5588 | } |
| 5589 | } |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | |
| 5594 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5595 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5596 | { |
| 5597 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5598 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5599 | if (thread == 0) { |
| 5600 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5601 | outputTracks[i].get()); |
| 5602 | return false; |
| 5603 | } |
| 5604 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5605 | // see note at standby() declaration |
| 5606 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5607 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5608 | thread.get()); |
| 5609 | return false; |
| 5610 | } |
| 5611 | } |
| 5612 | return true; |
| 5613 | } |
| 5614 | |
| 5615 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5616 | { |
| 5617 | return (mWaitTimeMs * 1000) / 2; |
| 5618 | } |
| 5619 | |
| 5620 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5621 | { |
| 5622 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5623 | updateWaitTime_l(); |
| 5624 | |
| 5625 | MixerThread::cacheParameters_l(); |
| 5626 | } |
| 5627 | |
| 5628 | // ---------------------------------------------------------------------------- |
| 5629 | // Record |
| 5630 | // ---------------------------------------------------------------------------- |
| 5631 | |
| 5632 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5633 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5634 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5635 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5636 | audio_devices_t inDevice, |
| 5637 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5638 | #ifdef TEE_SINK |
| 5639 | , const sp<NBAIO_Sink>& teeSink |
| 5640 | #endif |
| 5641 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5642 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5643 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5644 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5645 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5646 | #ifdef TEE_SINK |
| 5647 | , mTeeSink(teeSink) |
| 5648 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5649 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5650 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5651 | // mFastCapture below |
| 5652 | , mFastCaptureFutex(0) |
| 5653 | // mInputSource |
| 5654 | // mPipeSink |
| 5655 | // mPipeSource |
| 5656 | , mPipeFramesP2(0) |
| 5657 | // mPipeMemory |
| 5658 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5659 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5660 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5661 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5662 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5663 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5664 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5665 | |
| 5666 | // create an NBAIO source for the HAL input stream, and negotiate |
| 5667 | mInputSource = new AudioStreamInSource(input->stream); |
| 5668 | size_t numCounterOffers = 0; |
| 5669 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5670 | #if !LOG_NDEBUG |
| 5671 | ssize_t index = |
| 5672 | #else |
| 5673 | (void) |
| 5674 | #endif |
| 5675 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5676 | ALOG_ASSERT(index == 0); |
| 5677 | |
| 5678 | // initialize fast capture depending on configuration |
| 5679 | bool initFastCapture; |
| 5680 | switch (kUseFastCapture) { |
| 5681 | case FastCapture_Never: |
| 5682 | initFastCapture = false; |
| 5683 | break; |
| 5684 | case FastCapture_Always: |
| 5685 | initFastCapture = true; |
| 5686 | break; |
| 5687 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5688 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5689 | break; |
| 5690 | // case FastCapture_Dynamic: |
| 5691 | } |
| 5692 | |
| 5693 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5694 | // 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] | 5695 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5696 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5697 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 5698 | void *pipeBuffer; |
| 5699 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 5700 | sp<IMemory> pipeMemory; |
| 5701 | if ((roHeap == 0) || |
| 5702 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 5703 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 5704 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 5705 | goto failed; |
| 5706 | } |
| 5707 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 5708 | memset(pipeBuffer, 0, pipeSize); |
| 5709 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 5710 | const NBAIO_Format offers[1] = {format}; |
| 5711 | size_t numCounterOffers = 0; |
| 5712 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 5713 | ALOG_ASSERT(index == 0); |
| 5714 | mPipeSink = pipe; |
| 5715 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 5716 | numCounterOffers = 0; |
| 5717 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 5718 | ALOG_ASSERT(index == 0); |
| 5719 | mPipeSource = pipeReader; |
| 5720 | mPipeFramesP2 = pipeFramesP2; |
| 5721 | mPipeMemory = pipeMemory; |
| 5722 | |
| 5723 | // create fast capture |
| 5724 | mFastCapture = new FastCapture(); |
| 5725 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5726 | #ifdef STATE_QUEUE_DUMP |
| 5727 | // FIXME |
| 5728 | #endif |
| 5729 | FastCaptureState *state = sq->begin(); |
| 5730 | state->mCblk = NULL; |
| 5731 | state->mInputSource = mInputSource.get(); |
| 5732 | state->mInputSourceGen++; |
| 5733 | state->mPipeSink = pipe; |
| 5734 | state->mPipeSinkGen++; |
| 5735 | state->mFrameCount = mFrameCount; |
| 5736 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5737 | // already done in constructor initialization list |
| 5738 | //mFastCaptureFutex = 0; |
| 5739 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5740 | state->mColdGen++; |
| 5741 | state->mDumpState = &mFastCaptureDumpState; |
| 5742 | #ifdef TEE_SINK |
| 5743 | // FIXME |
| 5744 | #endif |
| 5745 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 5746 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 5747 | sq->end(); |
| 5748 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5749 | |
| 5750 | // start the fast capture |
| 5751 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5752 | pid_t tid = mFastCapture->getTid(); |
Glenn Kasten | 8379b72 | 2016-03-18 14:54:17 -0700 | [diff] [blame] | 5753 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5754 | #ifdef AUDIO_WATCHDOG |
| 5755 | // FIXME |
| 5756 | #endif |
| 5757 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5758 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5759 | } |
| 5760 | failed: ; |
| 5761 | |
| 5762 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5763 | } |
| 5764 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5765 | AudioFlinger::RecordThread::~RecordThread() |
| 5766 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5767 | if (mFastCapture != 0) { |
| 5768 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5769 | FastCaptureState *state = sq->begin(); |
| 5770 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5771 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5772 | if (old == -1) { |
| 5773 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5774 | } |
| 5775 | } |
| 5776 | state->mCommand = FastCaptureState::EXIT; |
| 5777 | sq->end(); |
| 5778 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5779 | mFastCapture->join(); |
| 5780 | mFastCapture.clear(); |
| 5781 | } |
| 5782 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5783 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5784 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | void AudioFlinger::RecordThread::onFirstRef() |
| 5788 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5789 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5790 | } |
| 5791 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5792 | bool AudioFlinger::RecordThread::threadLoop() |
| 5793 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5794 | nsecs_t lastWarning = 0; |
| 5795 | |
| 5796 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5797 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5798 | reacquire_wakelock: |
| 5799 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5800 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5801 | { |
| 5802 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5803 | size_t size = mActiveTracks.size(); |
| 5804 | activeTracksGen = mActiveTracksGen; |
| 5805 | if (size > 0) { |
| 5806 | // FIXME an arbitrary choice |
| 5807 | activeTrack = mActiveTracks[0]; |
| 5808 | acquireWakeLock_l(activeTrack->uid()); |
| 5809 | if (size > 1) { |
| 5810 | SortedVector<int> tmp; |
| 5811 | for (size_t i = 0; i < size; i++) { |
| 5812 | tmp.add(mActiveTracks[i]->uid()); |
| 5813 | } |
| 5814 | updateWakeLockUids_l(tmp); |
| 5815 | } |
| 5816 | } else { |
| 5817 | acquireWakeLock_l(-1); |
| 5818 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5819 | } |
| 5820 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5821 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 5822 | uint32_t sleepUs = 0; |
| 5823 | |
| 5824 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5825 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5826 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5827 | |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5828 | // sleep with mutex unlocked |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5829 | if (sleepUs > 0) { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5830 | ATRACE_BEGIN("sleep"); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5831 | usleep(sleepUs); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5832 | ATRACE_END(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5833 | sleepUs = 0; |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5834 | } |
| 5835 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5836 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 5837 | Vector< sp<RecordTrack> > activeTracks; |
| 5838 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5839 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5840 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5841 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5842 | // reference to a fast track which is about to be removed |
| 5843 | sp<RecordTrack> fastTrackToRemove; |
| 5844 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5845 | { // scope for mLock |
| 5846 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5847 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5848 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5849 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5850 | // check exitPending here because checkForNewParameters_l() and |
| 5851 | // checkForNewParameters_l() can temporarily release mLock |
| 5852 | if (exitPending()) { |
| 5853 | break; |
| 5854 | } |
| 5855 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5856 | // if no active track(s), then standby and release wakelock |
| 5857 | size_t size = mActiveTracks.size(); |
| 5858 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5859 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5860 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5861 | releaseWakeLock_l(); |
| 5862 | ALOGV("RecordThread: loop stopping"); |
| 5863 | // go to sleep |
| 5864 | mWaitWorkCV.wait(mLock); |
| 5865 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5866 | goto reacquire_wakelock; |
| 5867 | } |
| 5868 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5869 | if (mActiveTracksGen != activeTracksGen) { |
| 5870 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5871 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5872 | for (size_t i = 0; i < size; i++) { |
| 5873 | tmp.add(mActiveTracks[i]->uid()); |
| 5874 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5875 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5876 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5877 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5878 | bool doBroadcast = false; |
| 5879 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5880 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5881 | activeTrack = mActiveTracks[i]; |
| 5882 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5883 | if (activeTrack->isFastTrack()) { |
| 5884 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 5885 | fastTrackToRemove = activeTrack; |
| 5886 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5887 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5888 | mActiveTracks.remove(activeTrack); |
| 5889 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5890 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5891 | continue; |
| 5892 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5893 | |
| 5894 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 5895 | switch (activeTrackState) { |
| 5896 | |
| 5897 | case TrackBase::PAUSING: |
| 5898 | mActiveTracks.remove(activeTrack); |
| 5899 | mActiveTracksGen++; |
| 5900 | doBroadcast = true; |
| 5901 | size--; |
| 5902 | continue; |
| 5903 | |
| 5904 | case TrackBase::STARTING_1: |
| 5905 | sleepUs = 10000; |
| 5906 | i++; |
| 5907 | continue; |
| 5908 | |
| 5909 | case TrackBase::STARTING_2: |
| 5910 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5911 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5912 | activeTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5913 | break; |
| 5914 | |
| 5915 | case TrackBase::ACTIVE: |
| 5916 | break; |
| 5917 | |
| 5918 | case TrackBase::IDLE: |
| 5919 | i++; |
| 5920 | continue; |
| 5921 | |
| 5922 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5923 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5924 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5925 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5926 | activeTracks.add(activeTrack); |
| 5927 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5928 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5929 | if (activeTrack->isFastTrack()) { |
| 5930 | ALOG_ASSERT(!mFastTrackAvail); |
| 5931 | ALOG_ASSERT(fastTrack == 0); |
| 5932 | fastTrack = activeTrack; |
| 5933 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5934 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5935 | if (doBroadcast) { |
| 5936 | mStartStopCond.broadcast(); |
| 5937 | } |
| 5938 | |
| 5939 | // sleep if there are no active tracks to process |
| 5940 | if (activeTracks.size() == 0) { |
| 5941 | if (sleepUs == 0) { |
| 5942 | sleepUs = kRecordThreadSleepUs; |
| 5943 | } |
| 5944 | continue; |
| 5945 | } |
| 5946 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5947 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5948 | lockEffectChains_l(effectChains); |
| 5949 | } |
| 5950 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5951 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5952 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5953 | size_t size = effectChains.size(); |
| 5954 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5955 | // thread mutex is not locked, but effect chain is locked |
| 5956 | effectChains[i]->process_l(); |
| 5957 | } |
| 5958 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5959 | // 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] | 5960 | if (mFastCapture != 0) { |
| 5961 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5962 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5963 | bool didModify = false; |
| 5964 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5965 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 5966 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 5967 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5968 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5969 | if (old == -1) { |
| 5970 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5971 | } |
| 5972 | } |
| 5973 | state->mCommand = FastCaptureState::READ_WRITE; |
| 5974 | #if 0 // FIXME |
| 5975 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5976 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 5977 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5978 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5979 | didModify = true; |
| 5980 | } |
| 5981 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 5982 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 5983 | if (cblkNew != cblkOld) { |
| 5984 | state->mCblk = cblkNew; |
| 5985 | // block until acked if removing a fast track |
| 5986 | if (cblkOld != NULL) { |
| 5987 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 5988 | } |
| 5989 | didModify = true; |
| 5990 | } |
| 5991 | sq->end(didModify); |
| 5992 | if (didModify) { |
| 5993 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5994 | #if 0 |
| 5995 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5996 | mNormalSource = mPipeSource; |
| 5997 | } |
| 5998 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5999 | } |
| 6000 | } |
| 6001 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6002 | // now run the fast track destructor with thread mutex unlocked |
| 6003 | fastTrackToRemove.clear(); |
| 6004 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6005 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 6006 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 6007 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 6008 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 6009 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6010 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6011 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6012 | ssize_t framesRead; |
| 6013 | |
| 6014 | // If an NBAIO source is present, use it to read the normal capture's data |
| 6015 | if (mPipeSource != 0) { |
| 6016 | size_t framesToRead = mBufferSize / mFrameSize; |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6017 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6018 | framesToRead); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6019 | if (framesRead == 0) { |
| 6020 | // since pipe is non-blocking, simulate blocking input |
| 6021 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 6022 | } |
| 6023 | // otherwise use the HAL / AudioStreamIn directly |
| 6024 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6025 | ATRACE_BEGIN("read"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6026 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6027 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6028 | ATRACE_END(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6029 | if (bytesRead < 0) { |
| 6030 | framesRead = bytesRead; |
| 6031 | } else { |
| 6032 | framesRead = bytesRead / mFrameSize; |
| 6033 | } |
| 6034 | } |
| 6035 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6036 | // Update server timestamp with server stats |
| 6037 | // systemTime() is optional if the hardware supports timestamps. |
| 6038 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6039 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6040 | |
| 6041 | // Update server timestamp with kernel stats |
| 6042 | if (mInput->stream->get_capture_position != nullptr) { |
| 6043 | int64_t position, time; |
| 6044 | int ret = mInput->stream->get_capture_position(mInput->stream, &position, &time); |
| 6045 | if (ret == NO_ERROR) { |
| 6046 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6047 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6048 | // Note: In general record buffers should tend to be empty in |
| 6049 | // a properly running pipeline. |
| 6050 | // |
| 6051 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6052 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6053 | } |
| 6054 | } |
| 6055 | // Use this to track timestamp information |
| 6056 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6057 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6058 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6059 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6060 | // Force input into standby so that it tries to recover at next read attempt |
| 6061 | inputStandBy(); |
| 6062 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6063 | } |
| 6064 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6065 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6066 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6067 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6068 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6069 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6070 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6071 | } |
| 6072 | // 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] | 6073 | { |
| 6074 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6075 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6076 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6077 | (framesRead - part1) * mFrameSize); |
| 6078 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6079 | } |
| 6080 | rear = mRsmpInRear += framesRead; |
| 6081 | |
| 6082 | size = activeTracks.size(); |
| 6083 | // loop over each active track |
| 6084 | for (size_t i = 0; i < size; i++) { |
| 6085 | activeTrack = activeTracks[i]; |
| 6086 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6087 | // skip fast tracks, as those are handled directly by FastCapture |
| 6088 | if (activeTrack->isFastTrack()) { |
| 6089 | continue; |
| 6090 | } |
| 6091 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6092 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6093 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6094 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6095 | enum { |
| 6096 | OVERRUN_UNKNOWN, |
| 6097 | OVERRUN_TRUE, |
| 6098 | OVERRUN_FALSE |
| 6099 | } overrun = OVERRUN_UNKNOWN; |
| 6100 | |
| 6101 | // loop over getNextBuffer to handle circular sink |
| 6102 | for (;;) { |
| 6103 | |
| 6104 | activeTrack->mSink.frameCount = ~0; |
| 6105 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6106 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6107 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6108 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6109 | // check available frames and handle overrun conditions |
| 6110 | // if the record track isn't draining fast enough. |
| 6111 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6112 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6113 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6114 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6115 | overrun = OVERRUN_TRUE; |
| 6116 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6117 | if (framesOut == 0 || framesIn == 0) { |
| 6118 | break; |
| 6119 | } |
| 6120 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6121 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6122 | // from framesIn. |
| 6123 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6124 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6125 | framesOut = min(framesOut, |
| 6126 | destinationFramesPossible( |
| 6127 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6128 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6129 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6130 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6131 | |
| 6132 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6133 | overrun = OVERRUN_FALSE; |
| 6134 | } |
| 6135 | |
| 6136 | if (activeTrack->mFramesToDrop == 0) { |
| 6137 | if (framesOut > 0) { |
| 6138 | activeTrack->mSink.frameCount = framesOut; |
| 6139 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6140 | } |
| 6141 | } else { |
| 6142 | // FIXME could do a partial drop of framesOut |
| 6143 | if (activeTrack->mFramesToDrop > 0) { |
| 6144 | activeTrack->mFramesToDrop -= framesOut; |
| 6145 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6146 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6147 | } |
| 6148 | } else { |
| 6149 | activeTrack->mFramesToDrop += framesOut; |
| 6150 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6151 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6152 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6153 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6154 | activeTrack->sessionId(), |
| 6155 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6156 | activeTrack->mSyncStartEvent->triggerSession() : |
| 6157 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6158 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | } |
| 6162 | |
| 6163 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6164 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6165 | } |
| 6166 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6167 | |
| 6168 | switch (overrun) { |
| 6169 | case OVERRUN_TRUE: |
| 6170 | // client isn't retrieving buffers fast enough |
| 6171 | if (!activeTrack->setOverflow()) { |
| 6172 | nsecs_t now = systemTime(); |
| 6173 | // FIXME should lastWarning per track? |
| 6174 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6175 | ALOGW("RecordThread: buffer overflow"); |
| 6176 | lastWarning = now; |
| 6177 | } |
| 6178 | } |
| 6179 | break; |
| 6180 | case OVERRUN_FALSE: |
| 6181 | activeTrack->clearOverflow(); |
| 6182 | break; |
| 6183 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6184 | break; |
| 6185 | } |
| 6186 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6187 | // update frame information and push timestamp out |
| 6188 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6189 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6190 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6191 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6192 | } |
| 6193 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6194 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6195 | // enable changes in effect chain |
| 6196 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6197 | // 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] | 6198 | } |
| 6199 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6200 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6201 | |
| 6202 | { |
| 6203 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6204 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6205 | sp<RecordTrack> track = mTracks[i]; |
| 6206 | track->invalidate(); |
| 6207 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6208 | mActiveTracks.clear(); |
| 6209 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6210 | mStartStopCond.broadcast(); |
| 6211 | } |
| 6212 | |
| 6213 | releaseWakeLock(); |
| 6214 | |
| 6215 | ALOGV("RecordThread %p exiting", this); |
| 6216 | return false; |
| 6217 | } |
| 6218 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6219 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6220 | { |
| 6221 | if (!mStandby) { |
| 6222 | inputStandBy(); |
| 6223 | mStandby = true; |
| 6224 | } |
| 6225 | } |
| 6226 | |
| 6227 | void AudioFlinger::RecordThread::inputStandBy() |
| 6228 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6229 | // Idle the fast capture if it's currently running |
| 6230 | if (mFastCapture != 0) { |
| 6231 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6232 | FastCaptureState *state = sq->begin(); |
| 6233 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6234 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6235 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6236 | state->mColdGen++; |
| 6237 | mFastCaptureFutex = 0; |
| 6238 | sq->end(); |
| 6239 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6240 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6241 | #if 0 |
| 6242 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6243 | // FIXME |
| 6244 | } |
| 6245 | #endif |
| 6246 | #ifdef AUDIO_WATCHDOG |
| 6247 | // FIXME |
| 6248 | #endif |
| 6249 | } else { |
| 6250 | sq->end(false /*didModify*/); |
| 6251 | } |
| 6252 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6253 | mInput->stream->common.standby(&mInput->stream->common); |
| 6254 | } |
| 6255 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6256 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6257 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6258 | const sp<AudioFlinger::Client>& client, |
| 6259 | uint32_t sampleRate, |
| 6260 | audio_format_t format, |
| 6261 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6262 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6263 | audio_session_t sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6264 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6265 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 6266 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6267 | pid_t tid, |
| 6268 | status_t *status) |
| 6269 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6270 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6271 | sp<RecordTrack> track; |
| 6272 | status_t lStatus; |
| 6273 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6274 | // client expresses a preference for FAST, but we get the final say |
| 6275 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 6276 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6277 | // we formerly checked for a callback handler (non-0 tid), |
| 6278 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6279 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6280 | // frame count is not specified, or is exactly the pipe depth |
| 6281 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6282 | // PCM data |
| 6283 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6284 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6285 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6286 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6287 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6288 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6289 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6290 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6291 | hasFastCapture() && |
| 6292 | // there are sufficient fast track slots available |
| 6293 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6294 | ) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6295 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6296 | frameCount, mFrameCount); |
| 6297 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6298 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6299 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6300 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6301 | frameCount, mFrameCount, mPipeFramesP2, |
| 6302 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 6303 | hasFastCapture(), tid, mFastTrackAvail); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6304 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6305 | } |
| 6306 | } |
| 6307 | |
| 6308 | // compute track buffer size in frames, and suggest the notification frame count |
| 6309 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 6310 | // fast track: frame count is exactly the pipe depth |
| 6311 | frameCount = mPipeFramesP2; |
| 6312 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 6313 | *notificationFrames = mFrameCount; |
| 6314 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6315 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6316 | // or 20 ms if there is a fast capture |
| 6317 | // TODO This could be a roundupRatio inline, and const |
| 6318 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6319 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6320 | // minimum number of notification periods is at least kMinNotifications, |
| 6321 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6322 | static const size_t kMinNotifications = 3; |
| 6323 | static const uint32_t kMinMs = 30; |
| 6324 | // TODO This could be a roundupRatio inline |
| 6325 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6326 | // TODO This could be a roundupRatio inline |
| 6327 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6328 | maxNotificationFrames; |
| 6329 | const size_t minFrameCount = maxNotificationFrames * |
| 6330 | max(kMinNotifications, minNotificationsByMs); |
| 6331 | frameCount = max(frameCount, minFrameCount); |
| 6332 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 6333 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6334 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6335 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6336 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6337 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6338 | lStatus = initCheck(); |
| 6339 | if (lStatus != NO_ERROR) { |
| 6340 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6341 | goto Exit; |
| 6342 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6343 | |
| 6344 | { // scope for mLock |
| 6345 | Mutex::Autolock _l(mLock); |
| 6346 | |
| 6347 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6348 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 6349 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6350 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6351 | lStatus = track->initCheck(); |
| 6352 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6353 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6354 | // 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] | 6355 | goto Exit; |
| 6356 | } |
| 6357 | mTracks.add(track); |
| 6358 | |
| 6359 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6360 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6361 | mAudioFlinger->btNrecIsOff(); |
| 6362 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 6363 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6364 | |
| 6365 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 6366 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6367 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6368 | // so ask activity manager to do this on our behalf |
| 6369 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 6370 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6371 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6372 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6373 | lStatus = NO_ERROR; |
| 6374 | |
| 6375 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6376 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6377 | return track; |
| 6378 | } |
| 6379 | |
| 6380 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6381 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6382 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6383 | { |
| 6384 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6385 | sp<ThreadBase> strongMe = this; |
| 6386 | status_t status = NO_ERROR; |
| 6387 | |
| 6388 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6389 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6390 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6391 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6392 | triggerSession, |
| 6393 | recordTrack->sessionId(), |
| 6394 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6395 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6396 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6397 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6398 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6399 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6400 | } else { |
| 6401 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6402 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6403 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6404 | } |
| 6405 | } |
| 6406 | |
| 6407 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6408 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6409 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6410 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6411 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6412 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6413 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6414 | } else { |
| 6415 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6416 | } |
| 6417 | return status; |
| 6418 | } |
| 6419 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6420 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6421 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6422 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6423 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6424 | mActiveTracks.add(recordTrack); |
| 6425 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6426 | status_t status = NO_ERROR; |
| 6427 | if (recordTrack->isExternalTrack()) { |
| 6428 | mLock.unlock(); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6429 | status = AudioSystem::startInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6430 | mLock.lock(); |
| 6431 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6432 | if (status != NO_ERROR) { |
| 6433 | mActiveTracks.remove(recordTrack); |
| 6434 | mActiveTracksGen++; |
| 6435 | recordTrack->clearSyncStartEvent(); |
| 6436 | ALOGV("RecordThread::start error %d", status); |
| 6437 | return status; |
| 6438 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6439 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6440 | // Catch up with current buffer indices if thread is already running. |
| 6441 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6442 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6443 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6444 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6445 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6446 | // clear any converter state as new data will be discontinuous |
| 6447 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6448 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6449 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6450 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6451 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6452 | ALOGV("Record failed to start"); |
| 6453 | status = BAD_VALUE; |
| 6454 | goto startError; |
| 6455 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6456 | return status; |
| 6457 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6458 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6459 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6460 | if (recordTrack->isExternalTrack()) { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6461 | AudioSystem::stopInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6462 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6463 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6464 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6465 | return status; |
| 6466 | } |
| 6467 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6468 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6469 | { |
| 6470 | sp<SyncEvent> strongEvent = event.promote(); |
| 6471 | |
| 6472 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6473 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6474 | if (ptr != 0) { |
| 6475 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6476 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6477 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6478 | } |
| 6479 | } |
| 6480 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6481 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6482 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6483 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6484 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6485 | return false; |
| 6486 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6487 | // 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] | 6488 | recordTrack->mState = TrackBase::PAUSING; |
| 6489 | // do not wait for mStartStopCond if exiting |
| 6490 | if (exitPending()) { |
| 6491 | return true; |
| 6492 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6493 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6494 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6495 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 6496 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6497 | ALOGV("Record stopped OK"); |
| 6498 | return true; |
| 6499 | } |
| 6500 | return false; |
| 6501 | } |
| 6502 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6503 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6504 | { |
| 6505 | return false; |
| 6506 | } |
| 6507 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6508 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6509 | { |
| 6510 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6511 | if (!isValidSyncEvent(event)) { |
| 6512 | return BAD_VALUE; |
| 6513 | } |
| 6514 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6515 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6516 | status_t ret = NAME_NOT_FOUND; |
| 6517 | |
| 6518 | Mutex::Autolock _l(mLock); |
| 6519 | |
| 6520 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6521 | sp<RecordTrack> track = mTracks[i]; |
| 6522 | if (eventSession == track->sessionId()) { |
| 6523 | (void) track->setSyncEvent(event); |
| 6524 | ret = NO_ERROR; |
| 6525 | } |
| 6526 | } |
| 6527 | return ret; |
| 6528 | #else |
| 6529 | return BAD_VALUE; |
| 6530 | #endif |
| 6531 | } |
| 6532 | |
| 6533 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6534 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6535 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6536 | track->terminate(); |
| 6537 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6538 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6539 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6540 | removeTrack_l(track); |
| 6541 | } |
| 6542 | } |
| 6543 | |
| 6544 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6545 | { |
| 6546 | mTracks.remove(track); |
| 6547 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6548 | if (track->isFastTrack()) { |
| 6549 | ALOG_ASSERT(!mFastTrackAvail); |
| 6550 | mFastTrackAvail = true; |
| 6551 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6555 | { |
| 6556 | dumpInternals(fd, args); |
| 6557 | dumpTracks(fd, args); |
| 6558 | dumpEffectChains(fd, args); |
| 6559 | } |
| 6560 | |
| 6561 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6562 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6563 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6564 | |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6565 | dumpBase(fd, args); |
| 6566 | |
| 6567 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6568 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6569 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6570 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6571 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6572 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6573 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 6574 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6575 | // This is a large object so we place it on the heap. |
| 6576 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 6577 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 6578 | copy->dump(fd); |
| 6579 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6580 | } |
| 6581 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6582 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6583 | { |
| 6584 | const size_t SIZE = 256; |
| 6585 | char buffer[SIZE]; |
| 6586 | String8 result; |
| 6587 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6588 | size_t numtracks = mTracks.size(); |
| 6589 | size_t numactive = mActiveTracks.size(); |
| 6590 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6591 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6592 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6593 | dprintf(fd, " of which %zu are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6594 | RecordTrack::appendDumpHeader(result); |
| 6595 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6596 | sp<RecordTrack> track = mTracks[i]; |
| 6597 | if (track != 0) { |
| 6598 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6599 | if (active) { |
| 6600 | numactiveseen++; |
| 6601 | } |
| 6602 | track->dump(buffer, SIZE, active); |
| 6603 | result.append(buffer); |
| 6604 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6605 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6606 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6607 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6608 | } |
| 6609 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6610 | if (numactiveseen != numactive) { |
| 6611 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 6612 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6613 | result.append(buffer); |
| 6614 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6615 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6616 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6617 | if (mTracks.indexOf(track) < 0) { |
| 6618 | track->dump(buffer, SIZE, true); |
| 6619 | result.append(buffer); |
| 6620 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6621 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6622 | |
| 6623 | } |
| 6624 | write(fd, result.string(), result.size()); |
| 6625 | } |
| 6626 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6627 | |
| 6628 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 6629 | { |
| 6630 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6631 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6632 | mRsmpInFront = recordThread->mRsmpInRear; |
| 6633 | mRsmpInUnrel = 0; |
| 6634 | } |
| 6635 | |
| 6636 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 6637 | size_t *framesAvailable, bool *hasOverrun) |
| 6638 | { |
| 6639 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6640 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6641 | const int32_t rear = recordThread->mRsmpInRear; |
| 6642 | const int32_t front = mRsmpInFront; |
| 6643 | const ssize_t filled = rear - front; |
| 6644 | |
| 6645 | size_t framesIn; |
| 6646 | bool overrun = false; |
| 6647 | if (filled < 0) { |
| 6648 | // should not happen, but treat like a massive overrun and re-sync |
| 6649 | framesIn = 0; |
| 6650 | mRsmpInFront = rear; |
| 6651 | overrun = true; |
| 6652 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 6653 | framesIn = (size_t) filled; |
| 6654 | } else { |
| 6655 | // client is not keeping up with server, but give it latest data |
| 6656 | framesIn = recordThread->mRsmpInFrames; |
| 6657 | mRsmpInFront = /* front = */ rear - framesIn; |
| 6658 | overrun = true; |
| 6659 | } |
| 6660 | if (framesAvailable != NULL) { |
| 6661 | *framesAvailable = framesIn; |
| 6662 | } |
| 6663 | if (hasOverrun != NULL) { |
| 6664 | *hasOverrun = overrun; |
| 6665 | } |
| 6666 | } |
| 6667 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6668 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6669 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6670 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6671 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6672 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6673 | if (threadBase == 0) { |
| 6674 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6675 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6676 | return NOT_ENOUGH_DATA; |
| 6677 | } |
| 6678 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6679 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6680 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6681 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6682 | // FIXME should not be P2 (don't want to increase latency) |
| 6683 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6684 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6685 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6686 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 6687 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6688 | if (part1 > (size_t) filled) { |
| 6689 | part1 = filled; |
| 6690 | } |
| 6691 | size_t ask = buffer->frameCount; |
| 6692 | ALOG_ASSERT(ask > 0); |
| 6693 | if (part1 > ask) { |
| 6694 | part1 = ask; |
| 6695 | } |
| 6696 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6697 | // 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] | 6698 | buffer->raw = NULL; |
| 6699 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6700 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6701 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6702 | } |
| 6703 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6704 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6705 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6706 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6707 | return NO_ERROR; |
| 6708 | } |
| 6709 | |
| 6710 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6711 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 6712 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6713 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6714 | size_t stepCount = buffer->frameCount; |
| 6715 | if (stepCount == 0) { |
| 6716 | return; |
| 6717 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6718 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 6719 | mRsmpInUnrel -= stepCount; |
| 6720 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6721 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6722 | buffer->frameCount = 0; |
| 6723 | } |
| 6724 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6725 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( |
| 6726 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6727 | uint32_t srcSampleRate, |
| 6728 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6729 | uint32_t dstSampleRate) : |
| 6730 | mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars |
| 6731 | // mSrcFormat |
| 6732 | // mSrcSampleRate |
| 6733 | // mDstChannelMask |
| 6734 | // mDstFormat |
| 6735 | // mDstSampleRate |
| 6736 | // mSrcChannelCount |
| 6737 | // mDstChannelCount |
| 6738 | // mDstFrameSize |
| 6739 | mBuf(NULL), mBufFrames(0), mBufFrameSize(0), |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6740 | mResampler(NULL), |
| 6741 | mIsLegacyDownmix(false), |
| 6742 | mIsLegacyUpmix(false), |
| 6743 | mRequiresFloat(false), |
| 6744 | mInputConverterProvider(NULL) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6745 | { |
| 6746 | (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, |
| 6747 | dstChannelMask, dstFormat, dstSampleRate); |
| 6748 | } |
| 6749 | |
| 6750 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { |
| 6751 | free(mBuf); |
| 6752 | delete mResampler; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6753 | delete mInputConverterProvider; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6754 | } |
| 6755 | |
| 6756 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, |
| 6757 | AudioBufferProvider *provider, size_t frames) |
| 6758 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6759 | if (mInputConverterProvider != NULL) { |
| 6760 | mInputConverterProvider->setBufferProvider(provider); |
| 6761 | provider = mInputConverterProvider; |
| 6762 | } |
| 6763 | |
| 6764 | if (mResampler == NULL) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6765 | ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6766 | mSrcSampleRate, mSrcFormat, mDstFormat); |
| 6767 | |
| 6768 | AudioBufferProvider::Buffer buffer; |
| 6769 | for (size_t i = frames; i > 0; ) { |
| 6770 | buffer.frameCount = i; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6771 | status_t status = provider->getNextBuffer(&buffer); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6772 | if (status != OK || buffer.frameCount == 0) { |
| 6773 | frames -= i; // cannot fill request. |
| 6774 | break; |
| 6775 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6776 | // format convert to destination buffer |
| 6777 | convertNoResampler(dst, buffer.raw, buffer.frameCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6778 | |
| 6779 | dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; |
| 6780 | i -= buffer.frameCount; |
| 6781 | provider->releaseBuffer(&buffer); |
| 6782 | } |
| 6783 | } else { |
| 6784 | ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6785 | mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); |
| 6786 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6787 | // reallocate buffer if needed |
| 6788 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 6789 | free(mBuf); |
| 6790 | mBufFrames = frames; |
| 6791 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 6792 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6793 | // resampler accumulates, but we only have one source track |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6794 | memset(mBuf, 0, frames * mBufFrameSize); |
| 6795 | frames = mResampler->resample((int32_t*)mBuf, frames, provider); |
| 6796 | // format convert to destination buffer |
| 6797 | convertResampler(dst, mBuf, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6798 | } |
| 6799 | return frames; |
| 6800 | } |
| 6801 | |
| 6802 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( |
| 6803 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6804 | uint32_t srcSampleRate, |
| 6805 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6806 | uint32_t dstSampleRate) |
| 6807 | { |
| 6808 | // quick evaluation if there is any change. |
| 6809 | if (mSrcFormat == srcFormat |
| 6810 | && mSrcChannelMask == srcChannelMask |
| 6811 | && mSrcSampleRate == srcSampleRate |
| 6812 | && mDstFormat == dstFormat |
| 6813 | && mDstChannelMask == dstChannelMask |
| 6814 | && mDstSampleRate == dstSampleRate) { |
| 6815 | return NO_ERROR; |
| 6816 | } |
| 6817 | |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 6818 | ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" |
| 6819 | " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", |
| 6820 | srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6821 | const bool valid = |
| 6822 | audio_is_input_channel(srcChannelMask) |
| 6823 | && audio_is_input_channel(dstChannelMask) |
| 6824 | && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) |
| 6825 | && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) |
| 6826 | && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) |
| 6827 | ; // no upsampling checks for now |
| 6828 | if (!valid) { |
| 6829 | return BAD_VALUE; |
| 6830 | } |
| 6831 | |
| 6832 | mSrcFormat = srcFormat; |
| 6833 | mSrcChannelMask = srcChannelMask; |
| 6834 | mSrcSampleRate = srcSampleRate; |
| 6835 | mDstFormat = dstFormat; |
| 6836 | mDstChannelMask = dstChannelMask; |
| 6837 | mDstSampleRate = dstSampleRate; |
| 6838 | |
| 6839 | // compute derived parameters |
| 6840 | mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); |
| 6841 | mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); |
| 6842 | mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); |
| 6843 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6844 | // do we need to resample? |
| 6845 | delete mResampler; |
| 6846 | mResampler = NULL; |
| 6847 | if (mSrcSampleRate != mDstSampleRate) { |
| 6848 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, |
| 6849 | mSrcChannelCount, mDstSampleRate); |
| 6850 | mResampler->setSampleRate(mSrcSampleRate); |
| 6851 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); |
| 6852 | } |
| 6853 | |
| 6854 | // are we running legacy channel conversion modes? |
| 6855 | mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 6856 | || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) |
| 6857 | && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; |
| 6858 | mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO |
| 6859 | && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 6860 | || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); |
| 6861 | |
| 6862 | // do we need to process in float? |
| 6863 | mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; |
| 6864 | |
| 6865 | // do we need a staging buffer to convert for destination (we can still optimize this)? |
| 6866 | // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity |
| 6867 | if (mResampler != NULL) { |
| 6868 | mBufFrameSize = max(mSrcChannelCount, FCC_2) |
| 6869 | * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame] | 6870 | } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6871 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
| 6872 | } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6873 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); |
| 6874 | } else { |
| 6875 | mBufFrameSize = 0; |
| 6876 | } |
| 6877 | mBufFrames = 0; // force the buffer to be resized. |
| 6878 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6879 | // do we need an input converter buffer provider to give us float? |
| 6880 | delete mInputConverterProvider; |
| 6881 | mInputConverterProvider = NULL; |
| 6882 | if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 6883 | mInputConverterProvider = new ReformatBufferProvider( |
| 6884 | audio_channel_count_from_in_mask(mSrcChannelMask), |
| 6885 | mSrcFormat, |
| 6886 | AUDIO_FORMAT_PCM_FLOAT, |
| 6887 | 256 /* provider buffer frame count */); |
| 6888 | } |
| 6889 | |
| 6890 | // do we need a remixer to do channel mask conversion |
| 6891 | if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { |
| 6892 | (void) memcpy_by_index_array_initialization_from_channel_mask( |
| 6893 | mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6894 | } |
| 6895 | return NO_ERROR; |
| 6896 | } |
| 6897 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6898 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( |
| 6899 | void *dst, const void *src, size_t frames) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6900 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6901 | // src is native type unless there is legacy upmix or downmix, whereupon it is float. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6902 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 6903 | free(mBuf); |
| 6904 | mBufFrames = frames; |
| 6905 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 6906 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6907 | // do we need to do legacy upmix and downmix? |
| 6908 | if (mIsLegacyUpmix || mIsLegacyDownmix) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6909 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6910 | if (mIsLegacyUpmix) { |
| 6911 | upmix_to_stereo_float_from_mono_float((float *)dstBuf, |
| 6912 | (const float *)src, frames); |
| 6913 | } else /*mIsLegacyDownmix */ { |
| 6914 | downmix_to_mono_float_from_stereo_float((float *)dstBuf, |
| 6915 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6916 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6917 | if (mBuf != NULL) { |
| 6918 | memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, |
| 6919 | frames * mDstChannelCount); |
| 6920 | } |
| 6921 | return; |
| 6922 | } |
| 6923 | // do we need to do channel mask conversion? |
| 6924 | if (mSrcChannelMask != mDstChannelMask) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6925 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6926 | memcpy_by_index_array(dstBuf, mDstChannelCount, |
| 6927 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); |
| 6928 | if (dstBuf == dst) { |
| 6929 | return; // format is the same |
| 6930 | } |
| 6931 | } |
| 6932 | // convert to destination buffer |
| 6933 | const void *convertBuf = mBuf != NULL ? mBuf : src; |
| 6934 | memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, |
| 6935 | frames * mDstChannelCount); |
| 6936 | } |
| 6937 | |
| 6938 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( |
| 6939 | void *dst, /*not-a-const*/ void *src, size_t frames) |
| 6940 | { |
| 6941 | // src buffer format is ALWAYS float when entering this routine |
| 6942 | if (mIsLegacyUpmix) { |
| 6943 | ; // mono to stereo already handled by resampler |
| 6944 | } else if (mIsLegacyDownmix |
| 6945 | || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { |
| 6946 | // the resampler outputs stereo for mono input channel (a feature?) |
| 6947 | // must convert to mono |
| 6948 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 6949 | (const float *)src, frames); |
| 6950 | } else if (mSrcChannelMask != mDstChannelMask) { |
| 6951 | // convert to mono channel again for channel mask conversion (could be skipped |
| 6952 | // with further optimization). |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6953 | if (mSrcChannelCount == 1) { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6954 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 6955 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6956 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6957 | // convert to destination format (in place, OK as float is larger than other types) |
| 6958 | if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 6959 | memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 6960 | frames * mSrcChannelCount); |
| 6961 | } |
| 6962 | // channel convert and save to dst |
| 6963 | memcpy_by_index_array(dst, mDstChannelCount, |
| 6964 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); |
| 6965 | return; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6966 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6967 | // convert to destination format and save to dst |
| 6968 | memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 6969 | frames * mDstChannelCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6970 | } |
| 6971 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6972 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 6973 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6974 | { |
| 6975 | bool reconfig = false; |
| 6976 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6977 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6978 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6979 | audio_format_t reqFormat = mFormat; |
| 6980 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 6981 | // 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] | 6982 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 6983 | |
| 6984 | AudioParameter param = AudioParameter(keyValuePair); |
| 6985 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 6986 | |
| 6987 | // scope for AutoPark extends to end of method |
| 6988 | AutoPark<FastCapture> park(mFastCapture); |
| 6989 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6990 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 6991 | // channel count change can be requested. Do we mandate the first client defines the |
| 6992 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 6993 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6994 | samplingRate = value; |
| 6995 | reconfig = true; |
| 6996 | } |
| 6997 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6998 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6999 | status = BAD_VALUE; |
| 7000 | } else { |
| 7001 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7002 | reconfig = true; |
| 7003 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7004 | } |
| 7005 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 7006 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7007 | if (!audio_is_input_channel(mask) || |
| 7008 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7009 | status = BAD_VALUE; |
| 7010 | } else { |
| 7011 | channelMask = mask; |
| 7012 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7013 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7014 | } |
| 7015 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7016 | // do not accept frame count changes if tracks are open as the track buffer |
| 7017 | // size depends on frame count and correct behavior would not be guaranteed |
| 7018 | // if frame count is changed after track creation |
| 7019 | if (mActiveTracks.size() > 0) { |
| 7020 | status = INVALID_OPERATION; |
| 7021 | } else { |
| 7022 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7023 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7024 | } |
| 7025 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 7026 | // forward device change to effects that have requested to be |
| 7027 | // aware of attached audio device. |
| 7028 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7029 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7030 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7031 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7032 | // store input device and output device but do not forward output device to audio HAL. |
| 7033 | // Note that status is ignored by the caller for output device |
| 7034 | // (see AudioFlinger::setParameters() |
| 7035 | if (audio_is_output_devices(value)) { |
| 7036 | mOutDevice = value; |
| 7037 | status = BAD_VALUE; |
| 7038 | } else { |
| 7039 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7040 | if (value != AUDIO_DEVICE_NONE) { |
| 7041 | mPrevInDevice = value; |
| 7042 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7043 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7044 | // pre processings |
| 7045 | if (mTracks.size() > 0) { |
| 7046 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7047 | mAudioFlinger->btNrecIsOff(); |
| 7048 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7049 | sp<RecordTrack> track = mTracks[i]; |
| 7050 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7051 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7052 | } |
| 7053 | } |
| 7054 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7055 | } |
| 7056 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7057 | mAudioSource != (audio_source_t)value) { |
| 7058 | // forward device change to effects that have requested to be |
| 7059 | // aware of attached audio device. |
| 7060 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7061 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7062 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7063 | mAudioSource = (audio_source_t)value; |
| 7064 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7065 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7066 | if (status == NO_ERROR) { |
| 7067 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7068 | keyValuePair.string()); |
| 7069 | if (status == INVALID_OPERATION) { |
| 7070 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7071 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7072 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7073 | } |
| 7074 | if (reconfig) { |
| 7075 | if (status == BAD_VALUE && |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7076 | audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && |
| 7077 | audio_is_linear_pcm(reqFormat) && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7078 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7079 | <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7080 | audio_channel_count_from_in_mask( |
Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 7081 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7082 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7083 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7084 | if (status == NO_ERROR) { |
| 7085 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7086 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7087 | } |
| 7088 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7089 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7090 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7091 | return reconfig; |
| 7092 | } |
| 7093 | |
| 7094 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7095 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7096 | Mutex::Autolock _l(mLock); |
| 7097 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7098 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7099 | } |
| 7100 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7101 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 7102 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7103 | free(s); |
| 7104 | return out_s8; |
| 7105 | } |
| 7106 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7107 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7108 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7109 | |
| 7110 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7111 | |
| 7112 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7113 | case AUDIO_INPUT_OPENED: |
| 7114 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7115 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7116 | desc->mChannelMask = mChannelMask; |
| 7117 | desc->mSamplingRate = mSampleRate; |
| 7118 | desc->mFormat = mFormat; |
| 7119 | desc->mFrameCount = mFrameCount; |
| 7120 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7121 | break; |
| 7122 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7123 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7124 | default: |
| 7125 | break; |
| 7126 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7127 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7128 | } |
| 7129 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7130 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7131 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7132 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 7133 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7134 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7135 | if (mChannelCount > FCC_8) { |
| 7136 | ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); |
| 7137 | } |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7138 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 7139 | mFormat = mHALFormat; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7140 | if (!audio_is_linear_pcm(mFormat)) { |
| 7141 | ALOGE("HAL format %#x is not linear pcm", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 7142 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 7143 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7144 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 7145 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7146 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7147 | // 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] | 7148 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7149 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7150 | // A larger value should allow more old data to be read after a track calls start(), |
| 7151 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7152 | // |
| 7153 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7154 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7155 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7156 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7157 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7158 | |
| 7159 | // TODO optimize audio capture buffer sizes ... |
| 7160 | // Here we calculate the size of the sliding buffer used as a source |
| 7161 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7162 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7163 | // be better to have it derived from the pipe depth in the long term. |
| 7164 | // The current value is higher than necessary. However it should not add to latency. |
| 7165 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7166 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7167 | size_t bufferSize = (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize; |
| 7168 | (void)posix_memalign(&mRsmpInBuffer, 32, bufferSize); |
| 7169 | memset(mRsmpInBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7170 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7171 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7172 | // 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] | 7173 | } |
| 7174 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7175 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7176 | { |
| 7177 | Mutex::Autolock _l(mLock); |
| 7178 | if (initCheck() != NO_ERROR) { |
| 7179 | return 0; |
| 7180 | } |
| 7181 | |
| 7182 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 7183 | } |
| 7184 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7185 | uint32_t AudioFlinger::RecordThread::hasAudioSession(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7186 | { |
| 7187 | Mutex::Autolock _l(mLock); |
| 7188 | uint32_t result = 0; |
| 7189 | if (getEffectChain_l(sessionId) != 0) { |
| 7190 | result = EFFECT_SESSION; |
| 7191 | } |
| 7192 | |
| 7193 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7194 | if (sessionId == mTracks[i]->sessionId()) { |
| 7195 | result |= TRACK_SESSION; |
| 7196 | break; |
| 7197 | } |
| 7198 | } |
| 7199 | |
| 7200 | return result; |
| 7201 | } |
| 7202 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7203 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7204 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7205 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7206 | Mutex::Autolock _l(mLock); |
| 7207 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7208 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7209 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7210 | if (ids.indexOfKey(sessionId) < 0) { |
| 7211 | ids.add(sessionId, true); |
| 7212 | } |
| 7213 | } |
| 7214 | return ids; |
| 7215 | } |
| 7216 | |
| 7217 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7218 | { |
| 7219 | Mutex::Autolock _l(mLock); |
| 7220 | AudioStreamIn *input = mInput; |
| 7221 | mInput = NULL; |
| 7222 | return input; |
| 7223 | } |
| 7224 | |
| 7225 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 7226 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 7227 | { |
| 7228 | if (mInput == NULL) { |
| 7229 | return NULL; |
| 7230 | } |
| 7231 | return &mInput->stream->common; |
| 7232 | } |
| 7233 | |
| 7234 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7235 | { |
| 7236 | // only one chain per input thread |
| 7237 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7238 | 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] | 7239 | return INVALID_OPERATION; |
| 7240 | } |
| 7241 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7242 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7243 | chain->setInBuffer(NULL); |
| 7244 | chain->setOutBuffer(NULL); |
| 7245 | |
| 7246 | checkSuspendOnAddEffectChain_l(chain); |
| 7247 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7248 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7249 | // just moved them to a new input stream. |
| 7250 | chain->syncHalEffectsState(); |
| 7251 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7252 | mEffectChains.add(chain); |
| 7253 | |
| 7254 | return NO_ERROR; |
| 7255 | } |
| 7256 | |
| 7257 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7258 | { |
| 7259 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7260 | ALOGW_IF(mEffectChains.size() != 1, |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7261 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7262 | chain.get(), mEffectChains.size(), this); |
| 7263 | if (mEffectChains.size() == 1) { |
| 7264 | mEffectChains.removeAt(0); |
| 7265 | } |
| 7266 | return 0; |
| 7267 | } |
| 7268 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7269 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7270 | audio_patch_handle_t *handle) |
| 7271 | { |
| 7272 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7273 | |
| 7274 | // store new device and send to effects |
| 7275 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7276 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7277 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7278 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7279 | } |
| 7280 | |
| 7281 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7282 | // pre processings |
| 7283 | if (mTracks.size() > 0) { |
| 7284 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7285 | mAudioFlinger->btNrecIsOff(); |
| 7286 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7287 | sp<RecordTrack> track = mTracks[i]; |
| 7288 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7289 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 7290 | } |
| 7291 | } |
| 7292 | |
| 7293 | // store new source and send to effects |
| 7294 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7295 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7296 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7297 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7298 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7299 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7300 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7301 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7302 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7303 | status = hwDevice->create_audio_patch(hwDevice, |
| 7304 | patch->num_sources, |
| 7305 | patch->sources, |
| 7306 | patch->num_sinks, |
| 7307 | patch->sinks, |
| 7308 | handle); |
| 7309 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7310 | char *address; |
| 7311 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7312 | address = audio_device_address_to_parameter( |
| 7313 | patch->sources[0].ext.device.type, |
| 7314 | patch->sources[0].ext.device.address); |
| 7315 | } else { |
| 7316 | address = (char *)calloc(1, 1); |
| 7317 | } |
| 7318 | AudioParameter param = AudioParameter(String8(address)); |
| 7319 | free(address); |
| 7320 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), |
| 7321 | (int)patch->sources[0].ext.device.type); |
| 7322 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), |
| 7323 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 7324 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7325 | param.toString().string()); |
| 7326 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7327 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7328 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7329 | if (mInDevice != mPrevInDevice) { |
| 7330 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7331 | mPrevInDevice = mInDevice; |
| 7332 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7333 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7334 | return status; |
| 7335 | } |
| 7336 | |
| 7337 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7338 | { |
| 7339 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7340 | |
| 7341 | mInDevice = AUDIO_DEVICE_NONE; |
| 7342 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7343 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 7344 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7345 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 7346 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7347 | AudioParameter param; |
| 7348 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 7349 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7350 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7351 | } |
| 7352 | return status; |
| 7353 | } |
| 7354 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7355 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7356 | { |
| 7357 | Mutex::Autolock _l(mLock); |
| 7358 | mTracks.add(record); |
| 7359 | } |
| 7360 | |
| 7361 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7362 | { |
| 7363 | Mutex::Autolock _l(mLock); |
| 7364 | destroyTrack_l(record); |
| 7365 | } |
| 7366 | |
| 7367 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7368 | { |
| 7369 | ThreadBase::getAudioPortConfig(config); |
| 7370 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7371 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7372 | config->ext.mix.usecase.source = mAudioSource; |
| 7373 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7374 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7375 | } // namespace android |