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 | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 113 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 114 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 115 | |
| 116 | // don't warn about blocked writes or record buffer overflows more often than this |
| 117 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 118 | |
| 119 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 120 | static const int kRecordThreadSleepUs = 5000; |
| 121 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 122 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 123 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 124 | |
| 125 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 126 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 127 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 128 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 129 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 130 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 131 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 132 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 133 | // maximum normal sink buffer size |
| 134 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 135 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 136 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 137 | // FIXME This should be based on experimentally observed scheduling jitter |
| 138 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 139 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 140 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 141 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 142 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 143 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 144 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 145 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 146 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 147 | // Whether to use fast mixer |
| 148 | static const enum { |
| 149 | FastMixer_Never, // never initialize or use: for debugging only |
| 150 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 151 | // normal mixer multiplier is 1 |
| 152 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 153 | // multiplier is calculated based on min & max normal mixer buffer size |
| 154 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 155 | // multiplier is calculated based on min & max normal mixer buffer size |
| 156 | // FIXME for FastMixer_Dynamic: |
| 157 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 158 | // For example, one HAL implementation returns an error from a large write, |
| 159 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 160 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 161 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 162 | } kUseFastMixer = FastMixer_Static; |
| 163 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 164 | // Whether to use fast capture |
| 165 | static const enum { |
| 166 | FastCapture_Never, // never initialize or use: for debugging only |
| 167 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 168 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 169 | } kUseFastCapture = FastCapture_Static; |
| 170 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 171 | // Priorities for requestPriority |
| 172 | static const int kPriorityAudioApp = 2; |
| 173 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 174 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 175 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 176 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 177 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 178 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 179 | |
| 180 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 181 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 182 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 183 | // The minimum and maximum allowed values |
| 184 | static const int kFastTrackMultiplierMin = 1; |
| 185 | static const int kFastTrackMultiplierMax = 2; |
| 186 | |
| 187 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 188 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 189 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 190 | // See Thread::readOnlyHeap(). |
| 191 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 192 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 193 | // 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] | 194 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 195 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 196 | // ---------------------------------------------------------------------------- |
| 197 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 198 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 199 | |
| 200 | static void sFastTrackMultiplierInit() |
| 201 | { |
| 202 | char value[PROPERTY_VALUE_MAX]; |
| 203 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 204 | char *endptr; |
| 205 | unsigned long ul = strtoul(value, &endptr, 0); |
| 206 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 207 | sFastTrackMultiplier = (int) ul; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // ---------------------------------------------------------------------------- |
| 213 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 214 | #ifdef ADD_BATTERY_DATA |
| 215 | // To collect the amplifier usage |
| 216 | static void addBatteryData(uint32_t params) { |
| 217 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 218 | if (service == NULL) { |
| 219 | // it already logged |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | service->addBatteryData(params); |
| 224 | } |
| 225 | #endif |
| 226 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 227 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 228 | struct { |
| 229 | // call when you acquire a partial wakelock |
| 230 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 231 | pthread_mutex_lock(&mLock); |
| 232 | if (wakeLockToken.get() == nullptr) { |
| 233 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 234 | } else { |
| 235 | if (mCount == 0) { |
| 236 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 237 | } |
| 238 | ++mCount; |
| 239 | } |
| 240 | pthread_mutex_unlock(&mLock); |
| 241 | } |
| 242 | |
| 243 | // call when you release a partial wakelock. |
| 244 | void release(const sp<IBinder> &wakeLockToken) { |
| 245 | if (wakeLockToken.get() == nullptr) { |
| 246 | return; |
| 247 | } |
| 248 | pthread_mutex_lock(&mLock); |
| 249 | if (--mCount < 0) { |
| 250 | ALOGE("negative wakelock count"); |
| 251 | mCount = 0; |
| 252 | } |
| 253 | pthread_mutex_unlock(&mLock); |
| 254 | } |
| 255 | |
| 256 | // retrieves the boottime timebase offset from monotonic. |
| 257 | int64_t getBoottimeOffset() { |
| 258 | pthread_mutex_lock(&mLock); |
| 259 | int64_t boottimeOffset = mBoottimeOffset; |
| 260 | pthread_mutex_unlock(&mLock); |
| 261 | return boottimeOffset; |
| 262 | } |
| 263 | |
| 264 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 265 | // and the selected timebase. |
| 266 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 267 | // |
| 268 | // This only needs to be called upon acquiring the first partial wakelock |
| 269 | // after all other partial wakelocks are released. |
| 270 | // |
| 271 | // We do an empirical measurement of the offset rather than parsing |
| 272 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 273 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 274 | int clockbase; |
| 275 | switch (timebase) { |
| 276 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 277 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 278 | break; |
| 279 | default: |
| 280 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 281 | break; |
| 282 | } |
| 283 | // try three times to get the clock offset, choose the one |
| 284 | // with the minimum gap in measurements. |
| 285 | const int tries = 3; |
| 286 | nsecs_t bestGap, measured; |
| 287 | for (int i = 0; i < tries; ++i) { |
| 288 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 289 | const nsecs_t tbase = systemTime(clockbase); |
| 290 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 291 | const nsecs_t gap = tmono2 - tmono; |
| 292 | if (i == 0 || gap < bestGap) { |
| 293 | bestGap = gap; |
| 294 | measured = tbase - ((tmono + tmono2) >> 1); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // to avoid micro-adjusting, we don't change the timebase |
| 299 | // unless it is significantly different. |
| 300 | // |
| 301 | // Assumption: It probably takes more than toleranceNs to |
| 302 | // suspend and resume the device. |
| 303 | static int64_t toleranceNs = 10000; // 10 us |
| 304 | if (llabs(*offset - measured) > toleranceNs) { |
| 305 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 306 | (long long)*offset, (long long)measured); |
| 307 | *offset = measured; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | pthread_mutex_t mLock; |
| 312 | int32_t mCount; |
| 313 | int64_t mBoottimeOffset; |
| 314 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 315 | |
| 316 | // ---------------------------------------------------------------------------- |
| 317 | // CPU Stats |
| 318 | // ---------------------------------------------------------------------------- |
| 319 | |
| 320 | class CpuStats { |
| 321 | public: |
| 322 | CpuStats(); |
| 323 | void sample(const String8 &title); |
| 324 | #ifdef DEBUG_CPU_USAGE |
| 325 | private: |
| 326 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 327 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 328 | |
| 329 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 330 | |
| 331 | int mCpuNum; // thread's current CPU number |
| 332 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 333 | #endif |
| 334 | }; |
| 335 | |
| 336 | CpuStats::CpuStats() |
| 337 | #ifdef DEBUG_CPU_USAGE |
| 338 | : mCpuNum(-1), mCpukHz(-1) |
| 339 | #endif |
| 340 | { |
| 341 | } |
| 342 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 343 | void CpuStats::sample(const String8 &title |
| 344 | #ifndef DEBUG_CPU_USAGE |
| 345 | __unused |
| 346 | #endif |
| 347 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 348 | #ifdef DEBUG_CPU_USAGE |
| 349 | // get current thread's delta CPU time in wall clock ns |
| 350 | double wcNs; |
| 351 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 352 | |
| 353 | // record sample for wall clock statistics |
| 354 | if (valid) { |
| 355 | mWcStats.sample(wcNs); |
| 356 | } |
| 357 | |
| 358 | // get the current CPU number |
| 359 | int cpuNum = sched_getcpu(); |
| 360 | |
| 361 | // get the current CPU frequency in kHz |
| 362 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 363 | |
| 364 | // check if either CPU number or frequency changed |
| 365 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 366 | mCpuNum = cpuNum; |
| 367 | mCpukHz = cpukHz; |
| 368 | // ignore sample for purposes of cycles |
| 369 | valid = false; |
| 370 | } |
| 371 | |
| 372 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 373 | if (valid && mCpukHz > 0) { |
| 374 | double cycles = wcNs * cpukHz * 0.000001; |
| 375 | mHzStats.sample(cycles); |
| 376 | } |
| 377 | |
| 378 | unsigned n = mWcStats.n(); |
| 379 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 380 | if ((n & 127) == 1) { |
| 381 | long long elapsed = mCpuUsage.elapsed(); |
| 382 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 383 | double perLoop = elapsed / (double) n; |
| 384 | double perLoop100 = perLoop * 0.01; |
| 385 | double perLoop1k = perLoop * 0.001; |
| 386 | double mean = mWcStats.mean(); |
| 387 | double stddev = mWcStats.stddev(); |
| 388 | double minimum = mWcStats.minimum(); |
| 389 | double maximum = mWcStats.maximum(); |
| 390 | double meanCycles = mHzStats.mean(); |
| 391 | double stddevCycles = mHzStats.stddev(); |
| 392 | double minCycles = mHzStats.minimum(); |
| 393 | double maxCycles = mHzStats.maximum(); |
| 394 | mCpuUsage.resetElapsed(); |
| 395 | mWcStats.reset(); |
| 396 | mHzStats.reset(); |
| 397 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 398 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 399 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 400 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 401 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 402 | title.string(), |
| 403 | elapsed * .000000001, n, perLoop * .000001, |
| 404 | mean * .001, |
| 405 | stddev * .001, |
| 406 | minimum * .001, |
| 407 | maximum * .001, |
| 408 | mean / perLoop100, |
| 409 | stddev / perLoop100, |
| 410 | minimum / perLoop100, |
| 411 | maximum / perLoop100, |
| 412 | meanCycles / perLoop1k, |
| 413 | stddevCycles / perLoop1k, |
| 414 | minCycles / perLoop1k, |
| 415 | maxCycles / perLoop1k); |
| 416 | |
| 417 | } |
| 418 | } |
| 419 | #endif |
| 420 | }; |
| 421 | |
| 422 | // ---------------------------------------------------------------------------- |
| 423 | // ThreadBase |
| 424 | // ---------------------------------------------------------------------------- |
| 425 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 426 | // static |
| 427 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 428 | { |
| 429 | switch (type) { |
| 430 | case MIXER: |
| 431 | return "MIXER"; |
| 432 | case DIRECT: |
| 433 | return "DIRECT"; |
| 434 | case DUPLICATING: |
| 435 | return "DUPLICATING"; |
| 436 | case RECORD: |
| 437 | return "RECORD"; |
| 438 | case OFFLOAD: |
| 439 | return "OFFLOAD"; |
| 440 | default: |
| 441 | return "unknown"; |
| 442 | } |
| 443 | } |
| 444 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 445 | String8 devicesToString(audio_devices_t devices) |
| 446 | { |
| 447 | static const struct mapping { |
| 448 | audio_devices_t mDevices; |
| 449 | const char * mString; |
| 450 | } mappingsOut[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 451 | {AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE"}, |
| 452 | {AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER"}, |
| 453 | {AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 454 | {AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE"}, |
| 455 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "BLUETOOTH_SCO"}, |
| 456 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 457 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, "BLUETOOTH_SCO_CARKIT"}, |
| 458 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 459 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,"BLUETOOTH_A2DP_HEADPHONES"}, |
| 460 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, "BLUETOOTH_A2DP_SPEAKER"}, |
| 461 | {AUDIO_DEVICE_OUT_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 462 | {AUDIO_DEVICE_OUT_HDMI, "HDMI"}, |
| 463 | {AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,"ANLG_DOCK_HEADSET"}, |
| 464 | {AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,"DGTL_DOCK_HEADSET"}, |
| 465 | {AUDIO_DEVICE_OUT_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 466 | {AUDIO_DEVICE_OUT_USB_DEVICE, "USB_DEVICE"}, |
| 467 | {AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX"}, |
| 468 | {AUDIO_DEVICE_OUT_LINE, "LINE"}, |
| 469 | {AUDIO_DEVICE_OUT_HDMI_ARC, "HDMI_ARC"}, |
| 470 | {AUDIO_DEVICE_OUT_SPDIF, "SPDIF"}, |
| 471 | {AUDIO_DEVICE_OUT_FM, "FM"}, |
| 472 | {AUDIO_DEVICE_OUT_AUX_LINE, "AUX_LINE"}, |
| 473 | {AUDIO_DEVICE_OUT_SPEAKER_SAFE, "SPEAKER_SAFE"}, |
| 474 | {AUDIO_DEVICE_OUT_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame] | 475 | {AUDIO_DEVICE_OUT_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 476 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 477 | }, mappingsIn[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 478 | {AUDIO_DEVICE_IN_COMMUNICATION, "COMMUNICATION"}, |
| 479 | {AUDIO_DEVICE_IN_AMBIENT, "AMBIENT"}, |
| 480 | {AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC"}, |
| 481 | {AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 482 | {AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 483 | {AUDIO_DEVICE_IN_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 484 | {AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL"}, |
| 485 | {AUDIO_DEVICE_IN_TELEPHONY_RX, "TELEPHONY_RX"}, |
| 486 | {AUDIO_DEVICE_IN_BACK_MIC, "BACK_MIC"}, |
| 487 | {AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX"}, |
| 488 | {AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET"}, |
| 489 | {AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET"}, |
| 490 | {AUDIO_DEVICE_IN_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 491 | {AUDIO_DEVICE_IN_USB_DEVICE, "USB_DEVICE"}, |
| 492 | {AUDIO_DEVICE_IN_FM_TUNER, "FM_TUNER"}, |
| 493 | {AUDIO_DEVICE_IN_TV_TUNER, "TV_TUNER"}, |
| 494 | {AUDIO_DEVICE_IN_LINE, "LINE"}, |
| 495 | {AUDIO_DEVICE_IN_SPDIF, "SPDIF"}, |
| 496 | {AUDIO_DEVICE_IN_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 497 | {AUDIO_DEVICE_IN_LOOPBACK, "LOOPBACK"}, |
| 498 | {AUDIO_DEVICE_IN_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame] | 499 | {AUDIO_DEVICE_IN_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 500 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 501 | }; |
| 502 | String8 result; |
| 503 | audio_devices_t allDevices = AUDIO_DEVICE_NONE; |
| 504 | const mapping *entry; |
| 505 | if (devices & AUDIO_DEVICE_BIT_IN) { |
| 506 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 507 | entry = mappingsIn; |
| 508 | } else { |
| 509 | entry = mappingsOut; |
| 510 | } |
| 511 | for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { |
| 512 | allDevices = (audio_devices_t) (allDevices | entry->mDevices); |
| 513 | if (devices & entry->mDevices) { |
| 514 | if (!result.isEmpty()) { |
| 515 | result.append("|"); |
| 516 | } |
| 517 | result.append(entry->mString); |
| 518 | } |
| 519 | } |
| 520 | if (devices & ~allDevices) { |
| 521 | if (!result.isEmpty()) { |
| 522 | result.append("|"); |
| 523 | } |
| 524 | result.appendFormat("0x%X", devices & ~allDevices); |
| 525 | } |
| 526 | if (result.isEmpty()) { |
| 527 | result.append(entry->mString); |
| 528 | } |
| 529 | return result; |
| 530 | } |
| 531 | |
| 532 | String8 inputFlagsToString(audio_input_flags_t flags) |
| 533 | { |
| 534 | static const struct mapping { |
| 535 | audio_input_flags_t mFlag; |
| 536 | const char * mString; |
| 537 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 538 | {AUDIO_INPUT_FLAG_FAST, "FAST"}, |
| 539 | {AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD"}, |
| 540 | {AUDIO_INPUT_FLAG_RAW, "RAW"}, |
| 541 | {AUDIO_INPUT_FLAG_SYNC, "SYNC"}, |
| 542 | {AUDIO_INPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 543 | }; |
| 544 | String8 result; |
| 545 | audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; |
| 546 | const mapping *entry; |
| 547 | for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { |
| 548 | allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); |
| 549 | if (flags & entry->mFlag) { |
| 550 | if (!result.isEmpty()) { |
| 551 | result.append("|"); |
| 552 | } |
| 553 | result.append(entry->mString); |
| 554 | } |
| 555 | } |
| 556 | if (flags & ~allFlags) { |
| 557 | if (!result.isEmpty()) { |
| 558 | result.append("|"); |
| 559 | } |
| 560 | result.appendFormat("0x%X", flags & ~allFlags); |
| 561 | } |
| 562 | if (result.isEmpty()) { |
| 563 | result.append(entry->mString); |
| 564 | } |
| 565 | return result; |
| 566 | } |
| 567 | |
| 568 | String8 outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 569 | { |
| 570 | static const struct mapping { |
| 571 | audio_output_flags_t mFlag; |
| 572 | const char * mString; |
| 573 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 574 | {AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT"}, |
| 575 | {AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY"}, |
| 576 | {AUDIO_OUTPUT_FLAG_FAST, "FAST"}, |
| 577 | {AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER"}, |
| 578 | {AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD,"COMPRESS_OFFLOAD"}, |
| 579 | {AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING"}, |
| 580 | {AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC"}, |
| 581 | {AUDIO_OUTPUT_FLAG_RAW, "RAW"}, |
| 582 | {AUDIO_OUTPUT_FLAG_SYNC, "SYNC"}, |
| 583 | {AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO, "IEC958_NONAUDIO"}, |
| 584 | {AUDIO_OUTPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 585 | }; |
| 586 | String8 result; |
| 587 | audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 588 | const mapping *entry; |
| 589 | for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { |
| 590 | allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); |
| 591 | if (flags & entry->mFlag) { |
| 592 | if (!result.isEmpty()) { |
| 593 | result.append("|"); |
| 594 | } |
| 595 | result.append(entry->mString); |
| 596 | } |
| 597 | } |
| 598 | if (flags & ~allFlags) { |
| 599 | if (!result.isEmpty()) { |
| 600 | result.append("|"); |
| 601 | } |
| 602 | result.appendFormat("0x%X", flags & ~allFlags); |
| 603 | } |
| 604 | if (result.isEmpty()) { |
| 605 | result.append(entry->mString); |
| 606 | } |
| 607 | return result; |
| 608 | } |
| 609 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 610 | const char *sourceToString(audio_source_t source) |
| 611 | { |
| 612 | switch (source) { |
| 613 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 614 | case AUDIO_SOURCE_MIC: return "mic"; |
| 615 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 616 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 617 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 618 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 619 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 620 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 621 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 622 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 623 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 624 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 625 | default: return "unknown"; |
| 626 | } |
| 627 | } |
| 628 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 629 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 630 | 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] | 631 | : Thread(false /*canCallJava*/), |
| 632 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 633 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 634 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 635 | // are set by PlaybackThread::readOutputParameters_l() or |
| 636 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 637 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 638 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 639 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 640 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 642 | mDeathRecipient(new PMDeathRecipient(this)), |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 643 | mSystemReady(systemReady), |
| 644 | mNotifiedBatteryStart(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 645 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 646 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | AudioFlinger::ThreadBase::~ThreadBase() |
| 650 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 651 | // 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] | 652 | mConfigEvents.clear(); |
| 653 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 654 | // do not lock the mutex in destructor |
| 655 | releaseWakeLock_l(); |
| 656 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 657 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 658 | binder->unlinkToDeath(mDeathRecipient); |
| 659 | } |
| 660 | } |
| 661 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 662 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 663 | { |
| 664 | status_t status = initCheck(); |
| 665 | if (status == NO_ERROR) { |
| 666 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 667 | } else { |
| 668 | ALOGE("No working audio driver found."); |
| 669 | } |
| 670 | return status; |
| 671 | } |
| 672 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 673 | void AudioFlinger::ThreadBase::exit() |
| 674 | { |
| 675 | ALOGV("ThreadBase::exit"); |
| 676 | // do any cleanup required for exit to succeed |
| 677 | preExit(); |
| 678 | { |
| 679 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 680 | // if (!exitPending()) { |
| 681 | // // context switch from here to exit() |
| 682 | // // exit() calls requestExit(), what exitPending() observes |
| 683 | // // exit() calls signal(), which is dropped since no waiters |
| 684 | // // context switch back from exit() to here |
| 685 | // mWaitWorkCV.wait(...); |
| 686 | // // now thread is hung |
| 687 | // } |
| 688 | AutoMutex lock(mLock); |
| 689 | requestExit(); |
| 690 | mWaitWorkCV.broadcast(); |
| 691 | } |
| 692 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 693 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 694 | requestExitAndWait(); |
| 695 | } |
| 696 | |
| 697 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 698 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 699 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 700 | Mutex::Autolock _l(mLock); |
| 701 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 702 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 703 | } |
| 704 | |
| 705 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 706 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 707 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 708 | { |
| 709 | status_t status = NO_ERROR; |
| 710 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 711 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 712 | event->mWaitStatus = false; |
| 713 | mPendingConfigEvents.add(event); |
| 714 | return status; |
| 715 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 716 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 717 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 718 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 719 | mLock.unlock(); |
| 720 | { |
| 721 | Mutex::Autolock _l(event->mLock); |
| 722 | while (event->mWaitStatus) { |
| 723 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 724 | event->mStatus = TIMED_OUT; |
| 725 | event->mWaitStatus = false; |
| 726 | } |
| 727 | } |
| 728 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 729 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 730 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 731 | return status; |
| 732 | } |
| 733 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 734 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 735 | { |
| 736 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 737 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 741 | 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] | 742 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 743 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 744 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 747 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) |
| 748 | { |
| 749 | Mutex::Autolock _l(mLock); |
| 750 | sendPrioConfigEvent_l(pid, tid, prio); |
| 751 | } |
| 752 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 753 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 754 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 755 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 756 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 757 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 760 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 761 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 762 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 763 | sp<ConfigEvent> configEvent; |
| 764 | AudioParameter param(keyValuePair); |
| 765 | int value; |
| 766 | if (param.getInt(String8(AUDIO_PARAMETER_MONO_OUTPUT), value) == NO_ERROR) { |
| 767 | setMasterMono_l(value != 0); |
| 768 | if (param.size() == 1) { |
| 769 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 770 | } |
| 771 | param.remove(String8(AUDIO_PARAMETER_MONO_OUTPUT)); |
| 772 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 773 | } else { |
| 774 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 775 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 776 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 779 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 780 | const struct audio_patch *patch, |
| 781 | audio_patch_handle_t *handle) |
| 782 | { |
| 783 | Mutex::Autolock _l(mLock); |
| 784 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 785 | status_t status = sendConfigEvent_l(configEvent); |
| 786 | if (status == NO_ERROR) { |
| 787 | CreateAudioPatchConfigEventData *data = |
| 788 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 789 | *handle = data->mHandle; |
| 790 | } |
| 791 | return status; |
| 792 | } |
| 793 | |
| 794 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 795 | const audio_patch_handle_t handle) |
| 796 | { |
| 797 | Mutex::Autolock _l(mLock); |
| 798 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 799 | return sendConfigEvent_l(configEvent); |
| 800 | } |
| 801 | |
| 802 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 803 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 804 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 805 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 806 | bool configChanged = false; |
| 807 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 808 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 809 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 810 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 811 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 812 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 813 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 814 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 815 | // FIXME Need to understand why this has to be done asynchronously |
| 816 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 817 | true /*asynchronous*/); |
| 818 | if (err != 0) { |
| 819 | 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] | 820 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 821 | } |
| 822 | } break; |
| 823 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 824 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 825 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 826 | } break; |
| 827 | case CFG_EVENT_SET_PARAMETER: { |
| 828 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 829 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 830 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 831 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 832 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 833 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 834 | CreateAudioPatchConfigEventData *data = |
| 835 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 836 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 837 | } break; |
| 838 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 839 | ReleaseAudioPatchConfigEventData *data = |
| 840 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 841 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 842 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 843 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 844 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 845 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 846 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 847 | { |
| 848 | Mutex::Autolock _l(event->mLock); |
| 849 | if (event->mWaitStatus) { |
| 850 | event->mWaitStatus = false; |
| 851 | event->mCond.signal(); |
| 852 | } |
| 853 | } |
| 854 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 855 | } |
| 856 | |
| 857 | if (configChanged) { |
| 858 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 859 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 862 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 863 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 864 | const audio_channel_representation_t representation = |
| 865 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 866 | |
| 867 | switch (representation) { |
| 868 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 869 | if (output) { |
| 870 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 871 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 872 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 873 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 874 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 875 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 876 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 877 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 878 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 879 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 880 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 881 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 882 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 883 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 884 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 885 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 886 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 887 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 888 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 889 | } else { |
| 890 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 891 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 892 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 893 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 894 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 895 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 896 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 897 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 898 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 899 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 900 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 901 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 902 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 903 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 904 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 905 | } |
| 906 | const int len = s.length(); |
| 907 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 908 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 909 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 910 | } |
| 911 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 912 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 913 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 914 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 915 | return s; |
| 916 | default: |
| 917 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 918 | representation, audio_channel_mask_get_bits(mask)); |
| 919 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 920 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 921 | } |
| 922 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 923 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 924 | { |
| 925 | const size_t SIZE = 256; |
| 926 | char buffer[SIZE]; |
| 927 | String8 result; |
| 928 | |
| 929 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 930 | if (!locked) { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 931 | dprintf(fd, "thread %p may be deadlocked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 934 | dprintf(fd, " Thread name: %s\n", mThreadName); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 935 | dprintf(fd, " I/O handle: %d\n", mId); |
| 936 | dprintf(fd, " TID: %d\n", getTid()); |
| 937 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 938 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 939 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 940 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 941 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 942 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 943 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 944 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 945 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); |
| 946 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 947 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 948 | size_t numConfig = mConfigEvents.size(); |
| 949 | if (numConfig) { |
| 950 | for (size_t i = 0; i < numConfig; i++) { |
| 951 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 952 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 953 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 954 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 955 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 956 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 957 | } |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 958 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); |
| 959 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); |
| 960 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 961 | |
| 962 | if (locked) { |
| 963 | mLock.unlock(); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 968 | { |
| 969 | const size_t SIZE = 256; |
| 970 | char buffer[SIZE]; |
| 971 | String8 result; |
| 972 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 973 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 974 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 975 | write(fd, buffer, strlen(buffer)); |
| 976 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 977 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 978 | sp<EffectChain> chain = mEffectChains[i]; |
| 979 | if (chain != 0) { |
| 980 | chain->dump(fd, args); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 985 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 986 | { |
| 987 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 988 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 991 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 992 | { |
| 993 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 994 | case MIXER: |
| 995 | return String16("AudioMix"); |
| 996 | case DIRECT: |
| 997 | return String16("AudioDirectOut"); |
| 998 | case DUPLICATING: |
| 999 | return String16("AudioDup"); |
| 1000 | case RECORD: |
| 1001 | return String16("AudioIn"); |
| 1002 | case OFFLOAD: |
| 1003 | return String16("AudioOffload"); |
| 1004 | default: |
| 1005 | ALOG_ASSERT(false); |
| 1006 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1010 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1011 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1012 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1013 | if (mPowerManager != 0) { |
| 1014 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1015 | status_t status; |
| 1016 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1017 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1018 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1019 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1020 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1021 | uid, |
| 1022 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1023 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1024 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1025 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1026 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1027 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1028 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1029 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1030 | if (status == NO_ERROR) { |
| 1031 | mWakeLockToken = binder; |
| 1032 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1033 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1034 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1035 | |
| 1036 | if (!mNotifiedBatteryStart) { |
| 1037 | BatteryNotifier::getInstance().noteStartAudio(); |
| 1038 | mNotifiedBatteryStart = true; |
| 1039 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1040 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1041 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 1042 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1046 | { |
| 1047 | Mutex::Autolock _l(mLock); |
| 1048 | releaseWakeLock_l(); |
| 1049 | } |
| 1050 | |
| 1051 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1052 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1053 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1054 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1055 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1056 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1057 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 1058 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1059 | } |
| 1060 | mWakeLockToken.clear(); |
| 1061 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1062 | |
| 1063 | if (mNotifiedBatteryStart) { |
| 1064 | BatteryNotifier::getInstance().noteStopAudio(); |
| 1065 | mNotifiedBatteryStart = false; |
| 1066 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1069 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 1070 | Mutex::Autolock _l(mLock); |
| 1071 | updateWakeLockUids_l(uids); |
| 1072 | } |
| 1073 | |
| 1074 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1075 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1076 | // use checkService() to avoid blocking if power service is not up yet |
| 1077 | sp<IBinder> binder = |
| 1078 | defaultServiceManager()->checkService(String16("power")); |
| 1079 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1080 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1081 | } else { |
| 1082 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1083 | binder->linkToDeath(mDeathRecipient); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1089 | getPowerManager_l(); |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1090 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1091 | if (mSystemReady) { |
| 1092 | ALOGE("no wake lock to update, but system ready!"); |
| 1093 | } else { |
| 1094 | ALOGW("no wake lock to update, system not ready yet"); |
| 1095 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | if (mPowerManager != 0) { |
| 1099 | sp<IBinder> binder = new BBinder(); |
| 1100 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1101 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 1102 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1103 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1107 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1108 | { |
| 1109 | Mutex::Autolock _l(mLock); |
| 1110 | releaseWakeLock_l(); |
| 1111 | mPowerManager.clear(); |
| 1112 | } |
| 1113 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1114 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1115 | { |
| 1116 | sp<ThreadBase> thread = mThread.promote(); |
| 1117 | if (thread != 0) { |
| 1118 | thread->clearPowerManager(); |
| 1119 | } |
| 1120 | ALOGW("power manager service died !!!"); |
| 1121 | } |
| 1122 | |
| 1123 | void AudioFlinger::ThreadBase::setEffectSuspended( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1124 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1125 | { |
| 1126 | Mutex::Autolock _l(mLock); |
| 1127 | setEffectSuspended_l(type, suspend, sessionId); |
| 1128 | } |
| 1129 | |
| 1130 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1131 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1132 | { |
| 1133 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1134 | if (chain != 0) { |
| 1135 | if (type != NULL) { |
| 1136 | chain->setEffectSuspended_l(type, suspend); |
| 1137 | } else { |
| 1138 | chain->setEffectSuspendedAll_l(suspend); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1143 | } |
| 1144 | |
| 1145 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1146 | { |
| 1147 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1148 | if (index < 0) { |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1153 | mSuspendedSessions.valueAt(index); |
| 1154 | |
| 1155 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1156 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1157 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1158 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1159 | chain->setEffectSuspendedAll_l(true); |
| 1160 | } else { |
| 1161 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1162 | desc->mType.timeLow); |
| 1163 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1170 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1171 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1172 | { |
| 1173 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1174 | |
| 1175 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1176 | |
| 1177 | if (suspend) { |
| 1178 | if (index >= 0) { |
| 1179 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1180 | } else { |
| 1181 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1182 | } |
| 1183 | } else { |
| 1184 | if (index < 0) { |
| 1185 | return; |
| 1186 | } |
| 1187 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | int key = EffectChain::kKeyForSuspendAll; |
| 1192 | if (type != NULL) { |
| 1193 | key = type->timeLow; |
| 1194 | } |
| 1195 | index = sessionEffects.indexOfKey(key); |
| 1196 | |
| 1197 | sp<SuspendedSessionDesc> desc; |
| 1198 | if (suspend) { |
| 1199 | if (index >= 0) { |
| 1200 | desc = sessionEffects.valueAt(index); |
| 1201 | } else { |
| 1202 | desc = new SuspendedSessionDesc(); |
| 1203 | if (type != NULL) { |
| 1204 | desc->mType = *type; |
| 1205 | } |
| 1206 | sessionEffects.add(key, desc); |
| 1207 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1208 | } |
| 1209 | desc->mRefCount++; |
| 1210 | } else { |
| 1211 | if (index < 0) { |
| 1212 | return; |
| 1213 | } |
| 1214 | desc = sessionEffects.valueAt(index); |
| 1215 | if (--desc->mRefCount == 0) { |
| 1216 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1217 | sessionEffects.removeItemsAt(index); |
| 1218 | if (sessionEffects.isEmpty()) { |
| 1219 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1220 | sessionId); |
| 1221 | mSuspendedSessions.removeItem(sessionId); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | if (!sessionEffects.isEmpty()) { |
| 1226 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1231 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1232 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1233 | { |
| 1234 | Mutex::Autolock _l(mLock); |
| 1235 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1236 | } |
| 1237 | |
| 1238 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1239 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1240 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1241 | { |
| 1242 | if (mType != RECORD) { |
| 1243 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1244 | // another session. This gives the priority to well behaved effect control panels |
| 1245 | // and applications not using global effects. |
| 1246 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1247 | // global effects |
| 1248 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1249 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1254 | if (chain != 0) { |
| 1255 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1256 | } |
| 1257 | } |
| 1258 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1259 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1260 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( |
| 1261 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1262 | { |
| 1263 | // No global effect sessions on record threads |
| 1264 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1265 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1266 | desc->name, mThreadName); |
| 1267 | return BAD_VALUE; |
| 1268 | } |
| 1269 | // only pre processing effects on record thread |
| 1270 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1271 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1272 | desc->name, mThreadName); |
| 1273 | return BAD_VALUE; |
| 1274 | } |
| 1275 | audio_input_flags_t flags = mInput->flags; |
| 1276 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1277 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1278 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1279 | desc->name, mThreadName); |
| 1280 | return BAD_VALUE; |
| 1281 | } |
| 1282 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1283 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1284 | desc->name, mThreadName); |
| 1285 | return BAD_VALUE; |
| 1286 | } |
| 1287 | } |
| 1288 | return NO_ERROR; |
| 1289 | } |
| 1290 | |
| 1291 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1292 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( |
| 1293 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1294 | { |
| 1295 | // no preprocessing on playback threads |
| 1296 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1297 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" |
| 1298 | " thread %s", desc->name, mThreadName); |
| 1299 | return BAD_VALUE; |
| 1300 | } |
| 1301 | |
| 1302 | switch (mType) { |
| 1303 | case MIXER: { |
| 1304 | // Reject any effect on mixer multichannel sinks. |
| 1305 | // TODO: fix both format and multichannel issues with effects. |
| 1306 | if (mChannelCount != FCC_2) { |
| 1307 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" |
| 1308 | " thread %s", desc->name, mChannelCount, mThreadName); |
| 1309 | return BAD_VALUE; |
| 1310 | } |
| 1311 | audio_output_flags_t flags = mOutput->flags; |
| 1312 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1313 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1314 | // global effects are applied only to non fast tracks if they are SW |
| 1315 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1316 | break; |
| 1317 | } |
| 1318 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1319 | // only post processing on output stage session |
| 1320 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1321 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1322 | " on output stage session", desc->name); |
| 1323 | return BAD_VALUE; |
| 1324 | } |
| 1325 | } else { |
| 1326 | // no restriction on effects applied on non fast tracks |
| 1327 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
| 1331 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
| 1332 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", |
| 1333 | desc->name); |
| 1334 | return BAD_VALUE; |
| 1335 | } |
| 1336 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1337 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" |
| 1338 | " in fast mode", desc->name); |
| 1339 | return BAD_VALUE; |
| 1340 | } |
| 1341 | } |
| 1342 | } break; |
| 1343 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1344 | // nothing actionable on offload threads, if the effect: |
| 1345 | // - is offloadable: the effect can be created |
| 1346 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1347 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1348 | break; |
| 1349 | case DIRECT: |
| 1350 | // Reject any effect on Direct output threads for now, since the format of |
| 1351 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1352 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", |
| 1353 | desc->name, mThreadName); |
| 1354 | return BAD_VALUE; |
| 1355 | case DUPLICATING: |
| 1356 | // Reject any effect on mixer multichannel sinks. |
| 1357 | // TODO: fix both format and multichannel issues with effects. |
| 1358 | if (mChannelCount != FCC_2) { |
| 1359 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" |
| 1360 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); |
| 1361 | return BAD_VALUE; |
| 1362 | } |
| 1363 | if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) { |
| 1364 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" |
| 1365 | " thread %s", desc->name, mThreadName); |
| 1366 | return BAD_VALUE; |
| 1367 | } |
| 1368 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 1369 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" |
| 1370 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1371 | return BAD_VALUE; |
| 1372 | } |
| 1373 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1374 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" |
| 1375 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1376 | return BAD_VALUE; |
| 1377 | } |
| 1378 | break; |
| 1379 | default: |
| 1380 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1381 | } |
| 1382 | |
| 1383 | return NO_ERROR; |
| 1384 | } |
| 1385 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1386 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1387 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1388 | const sp<AudioFlinger::Client>& client, |
| 1389 | const sp<IEffectClient>& effectClient, |
| 1390 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1391 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1392 | effect_descriptor_t *desc, |
| 1393 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1394 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1395 | { |
| 1396 | sp<EffectModule> effect; |
| 1397 | sp<EffectHandle> handle; |
| 1398 | status_t lStatus; |
| 1399 | sp<EffectChain> chain; |
| 1400 | bool chainCreated = false; |
| 1401 | bool effectCreated = false; |
| 1402 | bool effectRegistered = false; |
| 1403 | |
| 1404 | lStatus = initCheck(); |
| 1405 | if (lStatus != NO_ERROR) { |
| 1406 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1407 | goto Exit; |
| 1408 | } |
| 1409 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1410 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1411 | |
| 1412 | { // scope for mLock |
| 1413 | Mutex::Autolock _l(mLock); |
| 1414 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1415 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
| 1416 | if (lStatus != NO_ERROR) { |
| 1417 | goto Exit; |
| 1418 | } |
| 1419 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1420 | // check for existing effect chain with the requested audio session |
| 1421 | chain = getEffectChain_l(sessionId); |
| 1422 | if (chain == 0) { |
| 1423 | // create a new chain for this session |
| 1424 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1425 | chain = new EffectChain(this, sessionId); |
| 1426 | addEffectChain_l(chain); |
| 1427 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1428 | chainCreated = true; |
| 1429 | } else { |
| 1430 | effect = chain->getEffectFromDesc_l(desc); |
| 1431 | } |
| 1432 | |
| 1433 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1434 | |
| 1435 | if (effect == 0) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1436 | audio_unique_id_t id = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1437 | // Check CPU and memory usage |
| 1438 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 1439 | if (lStatus != NO_ERROR) { |
| 1440 | goto Exit; |
| 1441 | } |
| 1442 | effectRegistered = true; |
| 1443 | // create a new effect module if none present in the chain |
| 1444 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 1445 | lStatus = effect->status(); |
| 1446 | if (lStatus != NO_ERROR) { |
| 1447 | goto Exit; |
| 1448 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1449 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1450 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1451 | lStatus = chain->addEffect_l(effect); |
| 1452 | if (lStatus != NO_ERROR) { |
| 1453 | goto Exit; |
| 1454 | } |
| 1455 | effectCreated = true; |
| 1456 | |
| 1457 | effect->setDevice(mOutDevice); |
| 1458 | effect->setDevice(mInDevice); |
| 1459 | effect->setMode(mAudioFlinger->getMode()); |
| 1460 | effect->setAudioSource(mAudioSource); |
| 1461 | } |
| 1462 | // create effect handle and connect it to effect module |
| 1463 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1464 | lStatus = handle->initCheck(); |
| 1465 | if (lStatus == OK) { |
| 1466 | lStatus = effect->addHandle(handle.get()); |
| 1467 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1468 | if (enabled != NULL) { |
| 1469 | *enabled = (int)effect->isEnabled(); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | Exit: |
| 1474 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1475 | Mutex::Autolock _l(mLock); |
| 1476 | if (effectCreated) { |
| 1477 | chain->removeEffect_l(effect); |
| 1478 | } |
| 1479 | if (effectRegistered) { |
| 1480 | AudioSystem::unregisterEffect(effect->id()); |
| 1481 | } |
| 1482 | if (chainCreated) { |
| 1483 | removeEffectChain_l(chain); |
| 1484 | } |
| 1485 | handle.clear(); |
| 1486 | } |
| 1487 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1488 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1489 | return handle; |
| 1490 | } |
| 1491 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1492 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1493 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1494 | { |
| 1495 | Mutex::Autolock _l(mLock); |
| 1496 | return getEffect_l(sessionId, effectId); |
| 1497 | } |
| 1498 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1499 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1500 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1501 | { |
| 1502 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1503 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1504 | } |
| 1505 | |
| 1506 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1507 | // PlaybackThread::mLock held |
| 1508 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1509 | { |
| 1510 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1511 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1512 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1513 | bool chainCreated = false; |
| 1514 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1515 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1516 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1517 | this, effect->desc().name, effect->desc().flags); |
| 1518 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1519 | if (chain == 0) { |
| 1520 | // create a new chain for this session |
| 1521 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1522 | chain = new EffectChain(this, sessionId); |
| 1523 | addEffectChain_l(chain); |
| 1524 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1525 | chainCreated = true; |
| 1526 | } |
| 1527 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1528 | |
| 1529 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1530 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1531 | this, effect->desc().name, chain.get()); |
| 1532 | return BAD_VALUE; |
| 1533 | } |
| 1534 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1535 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1536 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1537 | status_t status = chain->addEffect_l(effect); |
| 1538 | if (status != NO_ERROR) { |
| 1539 | if (chainCreated) { |
| 1540 | removeEffectChain_l(chain); |
| 1541 | } |
| 1542 | return status; |
| 1543 | } |
| 1544 | |
| 1545 | effect->setDevice(mOutDevice); |
| 1546 | effect->setDevice(mInDevice); |
| 1547 | effect->setMode(mAudioFlinger->getMode()); |
| 1548 | effect->setAudioSource(mAudioSource); |
| 1549 | return NO_ERROR; |
| 1550 | } |
| 1551 | |
| 1552 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1553 | |
| 1554 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1555 | effect_descriptor_t desc = effect->desc(); |
| 1556 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1557 | detachAuxEffect_l(effect->id()); |
| 1558 | } |
| 1559 | |
| 1560 | sp<EffectChain> chain = effect->chain().promote(); |
| 1561 | if (chain != 0) { |
| 1562 | // remove effect chain if removing last effect |
| 1563 | if (chain->removeEffect_l(effect) == 0) { |
| 1564 | removeEffectChain_l(chain); |
| 1565 | } |
| 1566 | } else { |
| 1567 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1572 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1573 | { |
| 1574 | effectChains = mEffectChains; |
| 1575 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1576 | mEffectChains[i]->lock(); |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1581 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1582 | { |
| 1583 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1584 | effectChains[i]->unlock(); |
| 1585 | } |
| 1586 | } |
| 1587 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1588 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1589 | { |
| 1590 | Mutex::Autolock _l(mLock); |
| 1591 | return getEffectChain_l(sessionId); |
| 1592 | } |
| 1593 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1594 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1595 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1596 | { |
| 1597 | size_t size = mEffectChains.size(); |
| 1598 | for (size_t i = 0; i < size; i++) { |
| 1599 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1600 | return mEffectChains[i]; |
| 1601 | } |
| 1602 | } |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1607 | { |
| 1608 | Mutex::Autolock _l(mLock); |
| 1609 | size_t size = mEffectChains.size(); |
| 1610 | for (size_t i = 0; i < size; i++) { |
| 1611 | mEffectChains[i]->setMode_l(mode); |
| 1612 | } |
| 1613 | } |
| 1614 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1615 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1616 | { |
| 1617 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1618 | config->ext.mix.handle = mId; |
| 1619 | config->sample_rate = mSampleRate; |
| 1620 | config->format = mFormat; |
| 1621 | config->channel_mask = mChannelMask; |
| 1622 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1623 | AUDIO_PORT_CONFIG_FORMAT; |
| 1624 | } |
| 1625 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1626 | void AudioFlinger::ThreadBase::systemReady() |
| 1627 | { |
| 1628 | Mutex::Autolock _l(mLock); |
| 1629 | if (mSystemReady) { |
| 1630 | return; |
| 1631 | } |
| 1632 | mSystemReady = true; |
| 1633 | |
| 1634 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1635 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1636 | } |
| 1637 | mPendingConfigEvents.clear(); |
| 1638 | } |
| 1639 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1640 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1641 | // ---------------------------------------------------------------------------- |
| 1642 | // Playback |
| 1643 | // ---------------------------------------------------------------------------- |
| 1644 | |
| 1645 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1646 | AudioStreamOut* output, |
| 1647 | audio_io_handle_t id, |
| 1648 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1649 | type_t type, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1650 | bool systemReady) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1651 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1652 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1653 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1654 | mMixerBuffer(NULL), |
| 1655 | mMixerBufferSize(0), |
| 1656 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1657 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1658 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1659 | mEffectBuffer(NULL), |
| 1660 | mEffectBufferSize(0), |
| 1661 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1662 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1663 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1664 | mFramesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1665 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1666 | // mStreamTypes[] initialized in constructor body |
| 1667 | mOutput(output), |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1668 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1669 | mMixerStatus(MIXER_IDLE), |
| 1670 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1671 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1672 | mBytesRemaining(0), |
| 1673 | mCurrentWriteLength(0), |
| 1674 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1675 | mWriteAckSequence(0), |
| 1676 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1677 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1678 | mScreenState(AudioFlinger::mScreenState), |
| 1679 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1680 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1681 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1682 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1683 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1684 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1685 | |
| 1686 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1687 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1688 | // parameter. |
| 1689 | // |
| 1690 | // If the HAL we are using has support for master volume or master mute, |
| 1691 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1692 | // and the mute set to false). |
| 1693 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1694 | mMasterMute = audioFlinger->masterMute_l(); |
| 1695 | if (mOutput && mOutput->audioHwDev) { |
| 1696 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1697 | mMasterVolume = 1.0; |
| 1698 | } |
| 1699 | |
| 1700 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1701 | mMasterMute = false; |
| 1702 | } |
| 1703 | } |
| 1704 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1705 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1706 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1707 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1708 | 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] | 1709 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1710 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1711 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1712 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1716 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1717 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1718 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1719 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1720 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1724 | { |
| 1725 | dumpInternals(fd, args); |
| 1726 | dumpTracks(fd, args); |
| 1727 | dumpEffectChains(fd, args); |
| 1728 | } |
| 1729 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1730 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1731 | { |
| 1732 | const size_t SIZE = 256; |
| 1733 | char buffer[SIZE]; |
| 1734 | String8 result; |
| 1735 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1736 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1737 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1738 | const stream_type_t *st = &mStreamTypes[i]; |
| 1739 | if (i > 0) { |
| 1740 | result.appendFormat(", "); |
| 1741 | } |
| 1742 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1743 | if (st->mute) { |
| 1744 | result.append("M"); |
| 1745 | } |
| 1746 | } |
| 1747 | result.append("\n"); |
| 1748 | write(fd, result.string(), result.length()); |
| 1749 | result.clear(); |
| 1750 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1751 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1752 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1753 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1754 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1755 | |
| 1756 | size_t numtracks = mTracks.size(); |
| 1757 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1758 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1759 | size_t numactiveseen = 0; |
| 1760 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1761 | dprintf(fd, " of which %zu are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1762 | Track::appendDumpHeader(result); |
| 1763 | for (size_t i = 0; i < numtracks; ++i) { |
| 1764 | sp<Track> track = mTracks[i]; |
| 1765 | if (track != 0) { |
| 1766 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1767 | if (active) { |
| 1768 | numactiveseen++; |
| 1769 | } |
| 1770 | track->dump(buffer, SIZE, active); |
| 1771 | result.append(buffer); |
| 1772 | } |
| 1773 | } |
| 1774 | } else { |
| 1775 | result.append("\n"); |
| 1776 | } |
| 1777 | if (numactiveseen != numactive) { |
| 1778 | // some tracks in the active list were not in the tracks list |
| 1779 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1780 | " not in the track list\n"); |
| 1781 | result.append(buffer); |
| 1782 | Track::appendDumpHeader(result); |
| 1783 | for (size_t i = 0; i < numactive; ++i) { |
| 1784 | sp<Track> track = mActiveTracks[i].promote(); |
| 1785 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1786 | track->dump(buffer, SIZE, true); |
| 1787 | result.append(buffer); |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1796 | { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1797 | 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] | 1798 | |
| 1799 | dumpBase(fd, args); |
| 1800 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1801 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1802 | dprintf(fd, " Last write occurred (msecs): %llu\n", |
| 1803 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1804 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1805 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1806 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1807 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1808 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1809 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1810 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1811 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1812 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1813 | AudioStreamOut *output = mOutput; |
| 1814 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
| 1815 | String8 flagsAsString = outputFlagsToString(flags); |
| 1816 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1820 | |
| 1821 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1822 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1823 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | // ThreadBase virtuals |
| 1827 | void AudioFlinger::PlaybackThread::preExit() |
| 1828 | { |
| 1829 | ALOGV(" preExit()"); |
| 1830 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1831 | // converted to use audio HAL extensions required to support tunneling |
| 1832 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1833 | } |
| 1834 | |
| 1835 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1836 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1837 | const sp<AudioFlinger::Client>& client, |
| 1838 | audio_stream_type_t streamType, |
| 1839 | uint32_t sampleRate, |
| 1840 | audio_format_t format, |
| 1841 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1842 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1843 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1844 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1845 | audio_output_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1846 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1847 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1848 | status_t *status) |
| 1849 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1850 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1851 | sp<Track> track; |
| 1852 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1853 | audio_output_flags_t outputFlags = mOutput->flags; |
| 1854 | |
| 1855 | // special case for FAST flag considered OK if fast mixer is present |
| 1856 | if (hasFastMixer()) { |
| 1857 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 1858 | } |
| 1859 | |
| 1860 | // Check if requested flags are compatible with output stream flags |
| 1861 | if ((*flags & outputFlags) != *flags) { |
| 1862 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 1863 | *flags, outputFlags); |
| 1864 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 1865 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1866 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1868 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1869 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1870 | // PCM data |
| 1871 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1872 | // TODO: extract as a data library function that checks that a computationally |
| 1873 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1874 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1875 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1876 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1877 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1878 | // hardware sample rate |
| 1879 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1880 | // normal mixer has an associated fast mixer |
| 1881 | hasFastMixer() && |
| 1882 | // there are sufficient fast track slots available |
| 1883 | (mFastTrackAvailMask != 0) |
| 1884 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1885 | // FIXME add a permission test also? |
| 1886 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1887 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 1888 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1889 | // read the fast track multiplier property the first time it is needed |
| 1890 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1891 | if (ok != 0) { |
| 1892 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1893 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1894 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1895 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1896 | |
| 1897 | // check compatibility with audio effects. |
| 1898 | { // scope for mLock |
| 1899 | Mutex::Autolock _l(mLock); |
| 1900 | // do not accept RAW flag if post processing are present. Note that post processing on |
| 1901 | // a fast mixer are necessarily hardware |
| 1902 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); |
| 1903 | if (chain != 0) { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1904 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1905 | "AUDIO_OUTPUT_FLAG_RAW denied: post processing effect present"); |
| 1906 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW); |
| 1907 | } |
| 1908 | // Do not accept FAST flag if software global effects are present |
| 1909 | chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 1910 | if (chain != 0) { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1911 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1912 | "AUDIO_OUTPUT_FLAG_RAW denied: global effect present"); |
| 1913 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW); |
| 1914 | if (chain->hasSoftwareEffect()) { |
| 1915 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: software global effect present"); |
| 1916 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 1917 | } |
| 1918 | } |
| 1919 | // Do not accept FAST flag if the session has software effects |
| 1920 | chain = getEffectChain_l(sessionId); |
| 1921 | if (chain != 0) { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1922 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1923 | "AUDIO_OUTPUT_FLAG_RAW denied: effect present on session"); |
| 1924 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW); |
| 1925 | if (chain->hasSoftwareEffect()) { |
| 1926 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: software effect present on session"); |
| 1927 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 1928 | } |
| 1929 | } |
| 1930 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1931 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1932 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 1933 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1934 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1935 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 1936 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1937 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1938 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1939 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1940 | audio_is_linear_pcm(format), |
| 1941 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1942 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1943 | } |
| 1944 | } |
| 1945 | // For normal PCM streaming tracks, update minimum frame count. |
| 1946 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1947 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1948 | // This is probably too conservative, but legacy application code may depend on it. |
| 1949 | // If you change this calculation, also review the start threshold which is related. |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1950 | if (!(*flags & AUDIO_OUTPUT_FLAG_FAST) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1951 | && audio_has_proportional_frames(format) && sharedBuffer == 0) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1952 | // this must match AudioTrack.cpp calculateMinFrameCount(). |
| 1953 | // TODO: Move to a common library |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1954 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1955 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1956 | if (minBufCount < 2) { |
| 1957 | minBufCount = 2; |
| 1958 | } |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1959 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack |
| 1960 | // or the client should compute and pass in a larger buffer request. |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1961 | size_t minFrameCount = |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1962 | minBufCount * sourceFramesNeededWithTimestretch( |
| 1963 | sampleRate, mNormalFrameCount, |
| 1964 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1965 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1966 | frameCount = minFrameCount; |
| 1967 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1968 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1969 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1970 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1971 | switch (mType) { |
| 1972 | |
| 1973 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1974 | 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] | 1975 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1976 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1977 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1978 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1979 | lStatus = BAD_VALUE; |
| 1980 | goto Exit; |
| 1981 | } |
| 1982 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1983 | break; |
| 1984 | |
| 1985 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1986 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1987 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1988 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1989 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1990 | lStatus = BAD_VALUE; |
| 1991 | goto Exit; |
| 1992 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1993 | break; |
| 1994 | |
| 1995 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1996 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1997 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1998 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1999 | format, mOutput, mFormat); |
| 2000 | lStatus = BAD_VALUE; |
| 2001 | goto Exit; |
| 2002 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2003 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2004 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2005 | lStatus = BAD_VALUE; |
| 2006 | goto Exit; |
| 2007 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2008 | break; |
| 2009 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | lStatus = initCheck(); |
| 2013 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2014 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2015 | goto Exit; |
| 2016 | } |
| 2017 | |
| 2018 | { // scope for mLock |
| 2019 | Mutex::Autolock _l(mLock); |
| 2020 | |
| 2021 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2022 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2023 | // manager |
| 2024 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 2025 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2026 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2027 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2028 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 2029 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2030 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2031 | strategy, actual); |
| 2032 | lStatus = BAD_VALUE; |
| 2033 | goto Exit; |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2038 | track = new Track(this, client, streamType, sampleRate, format, |
| 2039 | channelMask, frameCount, NULL, sharedBuffer, |
| 2040 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2041 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2042 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2043 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2044 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2045 | // 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] | 2046 | goto Exit; |
| 2047 | } |
| 2048 | mTracks.add(track); |
| 2049 | |
| 2050 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 2051 | if (chain != 0) { |
| 2052 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2053 | track->setMainBuffer(chain->inBuffer()); |
| 2054 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 2055 | chain->incTrackCnt(); |
| 2056 | } |
| 2057 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2058 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2059 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2060 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2061 | // so ask activity manager to do this on our behalf |
| 2062 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | lStatus = NO_ERROR; |
| 2067 | |
| 2068 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2069 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2070 | return track; |
| 2071 | } |
| 2072 | |
| 2073 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 2074 | { |
| 2075 | return latency; |
| 2076 | } |
| 2077 | |
| 2078 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 2079 | { |
| 2080 | Mutex::Autolock _l(mLock); |
| 2081 | return latency_l(); |
| 2082 | } |
| 2083 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 2084 | { |
| 2085 | if (initCheck() == NO_ERROR) { |
| 2086 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 2087 | } else { |
| 2088 | return 0; |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 2093 | { |
| 2094 | Mutex::Autolock _l(mLock); |
| 2095 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2096 | if (mOutput && mOutput->audioHwDev && |
| 2097 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2098 | mMasterVolume = 1.0; |
| 2099 | } else { |
| 2100 | mMasterVolume = value; |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 2105 | { |
| 2106 | Mutex::Autolock _l(mLock); |
| 2107 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2108 | if (mOutput && mOutput->audioHwDev && |
| 2109 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2110 | mMasterMute = false; |
| 2111 | } else { |
| 2112 | mMasterMute = muted; |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2117 | { |
| 2118 | Mutex::Autolock _l(mLock); |
| 2119 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2120 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2124 | { |
| 2125 | Mutex::Autolock _l(mLock); |
| 2126 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2127 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2131 | { |
| 2132 | Mutex::Autolock _l(mLock); |
| 2133 | return mStreamTypes[stream].volume; |
| 2134 | } |
| 2135 | |
| 2136 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2137 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2138 | { |
| 2139 | status_t status = ALREADY_EXISTS; |
| 2140 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2141 | if (mActiveTracks.indexOf(track) < 0) { |
| 2142 | // the track is newly added, make sure it fills up all its |
| 2143 | // buffers before playing. This is to ensure the client will |
| 2144 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2145 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2146 | TrackBase::track_state state = track->mState; |
| 2147 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2148 | status = AudioSystem::startOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2149 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2150 | mLock.lock(); |
| 2151 | // abort track was stopped/paused while we released the lock |
| 2152 | if (state != track->mState) { |
| 2153 | if (status == NO_ERROR) { |
| 2154 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2155 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2156 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2157 | mLock.lock(); |
| 2158 | } |
| 2159 | return INVALID_OPERATION; |
| 2160 | } |
| 2161 | // abort if start is rejected by audio policy manager |
| 2162 | if (status != NO_ERROR) { |
| 2163 | return PERMISSION_DENIED; |
| 2164 | } |
| 2165 | #ifdef ADD_BATTERY_DATA |
| 2166 | // to track the speaker usage |
| 2167 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2168 | #endif |
| 2169 | } |
| 2170 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2171 | // set retry count for buffer fill |
| 2172 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2173 | if (track->isStopping_1()) { |
| 2174 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 2175 | } else { |
| 2176 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2177 | } |
| 2178 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2179 | } else { |
| 2180 | track->mRetryCount = kMaxTrackStartupRetries; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2181 | track->mFillingUpStatus = |
| 2182 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2183 | } |
| 2184 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2185 | track->mResetDone = false; |
| 2186 | track->mPresentationCompleteFrames = 0; |
| 2187 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2188 | mWakeLockUids.add(track->uid()); |
| 2189 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2190 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2191 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2192 | if (chain != 0) { |
| 2193 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2194 | track->sessionId()); |
| 2195 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | status = NO_ERROR; |
| 2199 | } |
| 2200 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2201 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2202 | return status; |
| 2203 | } |
| 2204 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2205 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2206 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2207 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2208 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2209 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2210 | track->mState = TrackBase::STOPPED; |
| 2211 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2212 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2213 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2214 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2215 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2216 | |
| 2217 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2221 | { |
| 2222 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 2223 | mTracks.remove(track); |
| 2224 | deleteTrackName_l(track->name()); |
| 2225 | // redundant as track is about to be destroyed, for dumpsys only |
| 2226 | track->mName = -1; |
| 2227 | if (track->isFastTrack()) { |
| 2228 | int index = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2229 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2230 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2231 | mFastTrackAvailMask |= 1 << index; |
| 2232 | // redundant as track is about to be destroyed, for dumpsys only |
| 2233 | track->mFastIndex = -1; |
| 2234 | } |
| 2235 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2236 | if (chain != 0) { |
| 2237 | chain->decTrackCnt(); |
| 2238 | } |
| 2239 | } |
| 2240 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2241 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2242 | { |
| 2243 | // Thread could be blocked waiting for async |
| 2244 | // so signal it to handle state changes immediately |
| 2245 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 2246 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 2247 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2248 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2249 | } |
| 2250 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2251 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2252 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2253 | Mutex::Autolock _l(mLock); |
| 2254 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2255 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2256 | } |
| 2257 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2258 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 2259 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2260 | free(s); |
| 2261 | return out_s8; |
| 2262 | } |
| 2263 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2264 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2265 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2266 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2267 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2268 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2269 | |
| 2270 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2271 | case AUDIO_OUTPUT_OPENED: |
| 2272 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2273 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2274 | desc->mChannelMask = mChannelMask; |
| 2275 | desc->mSamplingRate = mSampleRate; |
| 2276 | desc->mFormat = mFormat; |
| 2277 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2278 | // AudioFlinger::frameCount(audio_io_handle_t) |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2279 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2280 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2281 | break; |
| 2282 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2283 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2284 | default: |
| 2285 | break; |
| 2286 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2287 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2288 | } |
| 2289 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2290 | void AudioFlinger::PlaybackThread::writeCallback() |
| 2291 | { |
| 2292 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2293 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | void AudioFlinger::PlaybackThread::drainCallback() |
| 2297 | { |
| 2298 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2299 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2302 | void AudioFlinger::PlaybackThread::errorCallback() |
| 2303 | { |
| 2304 | ALOG_ASSERT(mCallbackThread != 0); |
| 2305 | mCallbackThread->setAsyncError(); |
| 2306 | } |
| 2307 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2308 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2309 | { |
| 2310 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2311 | // reject out of sequence requests |
| 2312 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2313 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2314 | mWaitWorkCV.signal(); |
| 2315 | } |
| 2316 | } |
| 2317 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2318 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2319 | { |
| 2320 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2321 | // reject out of sequence requests |
| 2322 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2323 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2324 | mWaitWorkCV.signal(); |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | // static |
| 2329 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2330 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2331 | void *cookie) |
| 2332 | { |
| 2333 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 2334 | ALOGV("asyncCallback() event %d", event); |
| 2335 | switch (event) { |
| 2336 | case STREAM_CBK_EVENT_WRITE_READY: |
| 2337 | me->writeCallback(); |
| 2338 | break; |
| 2339 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 2340 | me->drainCallback(); |
| 2341 | break; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2342 | case STREAM_CBK_EVENT_ERROR: |
| 2343 | me->errorCallback(); |
| 2344 | break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2345 | default: |
| 2346 | ALOGW("asyncCallback() unknown event %d", event); |
| 2347 | break; |
| 2348 | } |
| 2349 | return 0; |
| 2350 | } |
| 2351 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2352 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2353 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2354 | // 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] | 2355 | mSampleRate = mOutput->getSampleRate(); |
| 2356 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2357 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2358 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2359 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2360 | if ((mType == MIXER || mType == DUPLICATING) |
| 2361 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2362 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2363 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2364 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2365 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2366 | |
| 2367 | // Get actual HAL format. |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2368 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2369 | // Get format from the shim, which will be different than the HAL format |
| 2370 | // if playing compressed audio over HDMI passthrough. |
| 2371 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2372 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2373 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2374 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2375 | if ((mType == MIXER || mType == DUPLICATING) |
| 2376 | && !isValidPcmSinkFormat(mFormat)) { |
| 2377 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2378 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2379 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2380 | mFrameSize = mOutput->getFrameSize(); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2381 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 2382 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2383 | if (mFrameCount & 15) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2384 | 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] | 2385 | mFrameCount); |
| 2386 | } |
| 2387 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2388 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 2389 | (mOutput->stream->set_callback != NULL)) { |
| 2390 | if (mOutput->stream->set_callback(mOutput->stream, |
| 2391 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 2392 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2393 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2397 | mHwSupportsPause = false; |
| 2398 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2399 | if (mOutput->stream->pause != NULL) { |
| 2400 | if (mOutput->stream->resume != NULL) { |
| 2401 | mHwSupportsPause = true; |
| 2402 | } else { |
| 2403 | ALOGW("direct output implements pause but not resume"); |
| 2404 | } |
| 2405 | } else if (mOutput->stream->resume != NULL) { |
| 2406 | ALOGW("direct output implements resume but not pause"); |
| 2407 | } |
| 2408 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2409 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2410 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2411 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2412 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2413 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2414 | // For best precision, we use float instead of the associated output |
| 2415 | // device format (typically PCM 16 bit). |
| 2416 | |
| 2417 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2418 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2419 | mBufferSize = mFrameSize * mFrameCount; |
| 2420 | |
| 2421 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2422 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2423 | // (if a valid mask) to avoid premature downmix. |
| 2424 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2425 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2426 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2427 | } |
| 2428 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2429 | // 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] | 2430 | double multiplier = 1.0; |
| 2431 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2432 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2433 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2434 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2435 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2436 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2437 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2438 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2439 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2440 | maxNormalFrameCount = minNormalFrameCount; |
| 2441 | } |
| 2442 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2443 | if (multiplier <= 1.0) { |
| 2444 | multiplier = 1.0; |
| 2445 | } else if (multiplier <= 2.0) { |
| 2446 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2447 | multiplier = 2.0; |
| 2448 | } else { |
| 2449 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2450 | } |
| 2451 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2452 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | mNormalFrameCount = multiplier * mFrameCount; |
| 2456 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2457 | if (mType == MIXER || mType == DUPLICATING) { |
| 2458 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2459 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2460 | 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] | 2461 | mNormalFrameCount); |
| 2462 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2463 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2464 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2465 | mThreadThrottleTimeMs = 0; |
| 2466 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2467 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2468 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2469 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2470 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2471 | free(mSinkBuffer); |
| 2472 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2473 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2474 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2475 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2476 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2477 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2478 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2479 | // drives the output. |
| 2480 | free(mMixerBuffer); |
| 2481 | mMixerBuffer = NULL; |
| 2482 | if (mMixerBufferEnabled) { |
| 2483 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2484 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2485 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2486 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2487 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2488 | free(mEffectBuffer); |
| 2489 | mEffectBuffer = NULL; |
| 2490 | if (mEffectBufferEnabled) { |
| 2491 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2492 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2493 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2494 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2495 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2496 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2497 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2498 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2499 | // 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] | 2500 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2501 | // matter. |
| 2502 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2503 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2504 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2505 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2510 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2511 | { |
| 2512 | if (halFrames == NULL || dspFrames == NULL) { |
| 2513 | return BAD_VALUE; |
| 2514 | } |
| 2515 | Mutex::Autolock _l(mLock); |
| 2516 | if (initCheck() != NO_ERROR) { |
| 2517 | return INVALID_OPERATION; |
| 2518 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2519 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2520 | *halFrames = framesWritten; |
| 2521 | |
| 2522 | if (isSuspended()) { |
| 2523 | // return an estimation of rendered frames when the output is suspended |
| 2524 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2525 | *dspFrames = (uint32_t) |
| 2526 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2527 | return NO_ERROR; |
| 2528 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2529 | status_t status; |
| 2530 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2531 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2532 | *dspFrames = (size_t)frames; |
| 2533 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2537 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 2538 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2539 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2540 | uint32_t result = 0; |
| 2541 | if (getEffectChain_l(sessionId) != 0) { |
| 2542 | result = EFFECT_SESSION; |
| 2543 | } |
| 2544 | |
| 2545 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2546 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2547 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2548 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2549 | if (track->isFastTrack()) { |
| 2550 | result |= FAST_SESSION; |
| 2551 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2552 | break; |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | return result; |
| 2557 | } |
| 2558 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2559 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2560 | { |
| 2561 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2562 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2563 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2564 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2565 | } |
| 2566 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2567 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2568 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2569 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2570 | } |
| 2571 | } |
| 2572 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2573 | } |
| 2574 | |
| 2575 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2576 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2577 | { |
| 2578 | Mutex::Autolock _l(mLock); |
| 2579 | return mOutput; |
| 2580 | } |
| 2581 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2582 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2583 | { |
| 2584 | Mutex::Autolock _l(mLock); |
| 2585 | AudioStreamOut *output = mOutput; |
| 2586 | mOutput = NULL; |
| 2587 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2588 | // must push a NULL and wait for ack |
| 2589 | mOutputSink.clear(); |
| 2590 | mPipeSink.clear(); |
| 2591 | mNormalSink.clear(); |
| 2592 | return output; |
| 2593 | } |
| 2594 | |
| 2595 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2596 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2597 | { |
| 2598 | if (mOutput == NULL) { |
| 2599 | return NULL; |
| 2600 | } |
| 2601 | return &mOutput->stream->common; |
| 2602 | } |
| 2603 | |
| 2604 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2605 | { |
| 2606 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2607 | } |
| 2608 | |
| 2609 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2610 | { |
| 2611 | if (!isValidSyncEvent(event)) { |
| 2612 | return BAD_VALUE; |
| 2613 | } |
| 2614 | |
| 2615 | Mutex::Autolock _l(mLock); |
| 2616 | |
| 2617 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2618 | sp<Track> track = mTracks[i]; |
| 2619 | if (event->triggerSession() == track->sessionId()) { |
| 2620 | (void) track->setSyncEvent(event); |
| 2621 | return NO_ERROR; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | return NAME_NOT_FOUND; |
| 2626 | } |
| 2627 | |
| 2628 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2629 | { |
| 2630 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2631 | } |
| 2632 | |
| 2633 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2634 | const Vector< sp<Track> >& tracksToRemove) |
| 2635 | { |
| 2636 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2637 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2638 | for (size_t i = 0 ; i < count ; i++) { |
| 2639 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2640 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2641 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2642 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2643 | #ifdef ADD_BATTERY_DATA |
| 2644 | // to track the speaker usage |
| 2645 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2646 | #endif |
| 2647 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2648 | AudioSystem::releaseOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2649 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2650 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2657 | { |
| 2658 | if (!mMasterMute) { |
| 2659 | char value[PROPERTY_VALUE_MAX]; |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2660 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 2661 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 2662 | return; |
| 2663 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2664 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2665 | char *endptr; |
| 2666 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2667 | if (*endptr == '\0' && ul != 0) { |
| 2668 | ALOGD("Silence is golden"); |
| 2669 | // The setprop command will not allow a property to be changed after |
| 2670 | // the first time it is set, so we don't have to worry about un-muting. |
| 2671 | setMasterMute_l(true); |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
| 2677 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2678 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2679 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2680 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2681 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2682 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2683 | |
| 2684 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2685 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2686 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2687 | const size_t count = mBytesRemaining / mFrameSize; |
| 2688 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2689 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2690 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2691 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2692 | if (screenState != mScreenState) { |
| 2693 | mScreenState = screenState; |
| 2694 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2695 | if (pipe != NULL) { |
| 2696 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2697 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2698 | } |
| 2699 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2700 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2701 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2702 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2703 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2704 | } else { |
| 2705 | bytesWritten = framesWritten; |
| 2706 | } |
| 2707 | // otherwise use the HAL / AudioStreamOut directly |
| 2708 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2709 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2710 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2711 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2712 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2713 | mWriteAckSequence += 2; |
| 2714 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2715 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2716 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2717 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2718 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2719 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2720 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2721 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2722 | if (mUseAsyncWrite && |
| 2723 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2724 | // 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] | 2725 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2726 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2727 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2728 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2731 | mNumWrites++; |
| 2732 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2733 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2734 | return bytesWritten; |
| 2735 | } |
| 2736 | |
| 2737 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2738 | { |
| 2739 | if (mOutput->stream->drain) { |
| 2740 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2741 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2742 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2743 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2744 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2745 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2746 | } |
| 2747 | mOutput->stream->drain(mOutput->stream, |
| 2748 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2749 | : AUDIO_DRAIN_ALL); |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2754 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2755 | { |
| 2756 | Mutex::Autolock _l(mLock); |
| 2757 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2758 | sp<Track> track = mTracks[i]; |
| 2759 | track->invalidate(); |
| 2760 | } |
| 2761 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2766 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2767 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2768 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2769 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2770 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2771 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2772 | |
| 2773 | The parameters that affect these derived values are: |
| 2774 | - frame count |
| 2775 | - frame size |
| 2776 | - sample rate |
| 2777 | - device type: A2DP or not |
| 2778 | - device latency |
| 2779 | - format: PCM or not |
| 2780 | - active sleep time |
| 2781 | - idle sleep time |
| 2782 | */ |
| 2783 | |
| 2784 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2785 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2786 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2787 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2788 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2789 | |
| 2790 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2791 | // truncating audio when going to standby. |
| 2792 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2793 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2794 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2795 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2796 | } |
| 2797 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2798 | } |
| 2799 | |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2800 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2801 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2802 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2803 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2804 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2805 | size_t size = mTracks.size(); |
| 2806 | for (size_t i = 0; i < size; i++) { |
| 2807 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2808 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2809 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2810 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2811 | } |
| 2812 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2813 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2814 | } |
| 2815 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2816 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2817 | { |
| 2818 | Mutex::Autolock _l(mLock); |
| 2819 | invalidateTracks_l(streamType); |
| 2820 | } |
| 2821 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2822 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2823 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2824 | audio_session_t session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2825 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2826 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2827 | bool ownsBuffer = false; |
| 2828 | |
| 2829 | 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] | 2830 | if (session > AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2831 | // 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] | 2832 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2833 | if (mType != DIRECT) { |
| 2834 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2835 | buffer = new int16_t[numSamples]; |
| 2836 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2837 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2838 | ownsBuffer = true; |
| 2839 | } |
| 2840 | |
| 2841 | // Attach all tracks with same session ID to this chain. |
| 2842 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2843 | sp<Track> track = mTracks[i]; |
| 2844 | if (session == track->sessionId()) { |
| 2845 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2846 | buffer); |
| 2847 | track->setMainBuffer(buffer); |
| 2848 | chain->incTrackCnt(); |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | // indicate all active tracks in the chain |
| 2853 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2854 | sp<Track> track = mActiveTracks[i].promote(); |
| 2855 | if (track == 0) { |
| 2856 | continue; |
| 2857 | } |
| 2858 | if (session == track->sessionId()) { |
| 2859 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2860 | chain->incActiveTrackCnt(); |
| 2861 | } |
| 2862 | } |
| 2863 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2864 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2865 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2866 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2867 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2868 | // 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] | 2869 | // 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] | 2870 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2871 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2872 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2873 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2874 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2875 | // Effect chain for other sessions are inserted at beginning of effect |
| 2876 | // 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] | 2877 | // sessions is not important. |
| 2878 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
| 2879 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, |
| 2880 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2881 | size_t size = mEffectChains.size(); |
| 2882 | size_t i = 0; |
| 2883 | for (i = 0; i < size; i++) { |
| 2884 | if (mEffectChains[i]->sessionId() < session) { |
| 2885 | break; |
| 2886 | } |
| 2887 | } |
| 2888 | mEffectChains.insertAt(chain, i); |
| 2889 | checkSuspendOnAddEffectChain_l(chain); |
| 2890 | |
| 2891 | return NO_ERROR; |
| 2892 | } |
| 2893 | |
| 2894 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2895 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2896 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2897 | |
| 2898 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2899 | |
| 2900 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2901 | if (chain == mEffectChains[i]) { |
| 2902 | mEffectChains.removeAt(i); |
| 2903 | // detach all active tracks from the chain |
| 2904 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2905 | sp<Track> track = mActiveTracks[i].promote(); |
| 2906 | if (track == 0) { |
| 2907 | continue; |
| 2908 | } |
| 2909 | if (session == track->sessionId()) { |
| 2910 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2911 | chain.get(), session); |
| 2912 | chain->decActiveTrackCnt(); |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | // detach all tracks with same session ID from this chain |
| 2917 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2918 | sp<Track> track = mTracks[i]; |
| 2919 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2920 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2921 | chain->decTrackCnt(); |
| 2922 | } |
| 2923 | } |
| 2924 | break; |
| 2925 | } |
| 2926 | } |
| 2927 | return mEffectChains.size(); |
| 2928 | } |
| 2929 | |
| 2930 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2931 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2932 | { |
| 2933 | Mutex::Autolock _l(mLock); |
| 2934 | return attachAuxEffect_l(track, EffectId); |
| 2935 | } |
| 2936 | |
| 2937 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2938 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2939 | { |
| 2940 | status_t status = NO_ERROR; |
| 2941 | |
| 2942 | if (EffectId == 0) { |
| 2943 | track->setAuxBuffer(0, NULL); |
| 2944 | } else { |
| 2945 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2946 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2947 | if (effect != 0) { |
| 2948 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2949 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2950 | } else { |
| 2951 | status = INVALID_OPERATION; |
| 2952 | } |
| 2953 | } else { |
| 2954 | status = BAD_VALUE; |
| 2955 | } |
| 2956 | } |
| 2957 | return status; |
| 2958 | } |
| 2959 | |
| 2960 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2961 | { |
| 2962 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2963 | sp<Track> track = mTracks[i]; |
| 2964 | if (track->auxEffectId() == effectId) { |
| 2965 | attachAuxEffect_l(track, 0); |
| 2966 | } |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2971 | { |
| 2972 | Vector< sp<Track> > tracksToRemove; |
| 2973 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2974 | mStandbyTimeNs = systemTime(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2975 | nsecs_t lastWriteFinished = -1; // time last server write completed |
| 2976 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2977 | |
| 2978 | // MIXER |
| 2979 | nsecs_t lastWarning = 0; |
| 2980 | |
| 2981 | // DUPLICATING |
| 2982 | // FIXME could this be made local to while loop? |
| 2983 | writeFrames = 0; |
| 2984 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2985 | int lastGeneration = 0; |
| 2986 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2987 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2988 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2989 | |
| 2990 | if (mType == MIXER) { |
| 2991 | sleepTimeShift = 0; |
| 2992 | } |
| 2993 | |
| 2994 | CpuStats cpuStats; |
| 2995 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2996 | |
| 2997 | acquireWakeLock(); |
| 2998 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2999 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 3000 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 3001 | // and then that string will be logged at the next convenient opportunity. |
| 3002 | const char *logString = NULL; |
| 3003 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 3004 | checkSilentMode_l(); |
| 3005 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3006 | while (!exitPending()) |
| 3007 | { |
| 3008 | cpuStats.sample(myName); |
| 3009 | |
| 3010 | Vector< sp<EffectChain> > effectChains; |
| 3011 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3012 | { // scope for mLock |
| 3013 | |
| 3014 | Mutex::Autolock _l(mLock); |
| 3015 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3016 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3017 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3018 | if (logString != NULL) { |
| 3019 | mNBLogWriter->logTimestamp(); |
| 3020 | mNBLogWriter->log(logString); |
| 3021 | logString = NULL; |
| 3022 | } |
| 3023 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3024 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3025 | // and associate with the sink frames written out. We need |
| 3026 | // this to convert the sink timestamp to the track timestamp. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3027 | bool kernelLocationUpdate = false; |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3028 | if (mNormalSink != 0) { |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3029 | // Note: The DuplicatingThread may not have a mNormalSink. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3030 | // We always fetch the timestamp here because often the downstream |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3031 | // sink will block while writing. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3032 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 3033 | (void) mNormalSink->getTimestamp(timestamp); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3034 | |
| 3035 | // We keep track of the last valid kernel position in case we are in underrun |
| 3036 | // and the normal mixer period is the same as the fast mixer period, or there |
| 3037 | // is some error from the HAL. |
| 3038 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3039 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3040 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3041 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3042 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3043 | |
| 3044 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3045 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 3046 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3047 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3051 | kernelLocationUpdate = true; |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3052 | } else { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3053 | ALOGVV("getTimestamp error - no valid kernel position"); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3054 | } |
| 3055 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3056 | // copy over kernel info |
| 3057 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
| 3058 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3059 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 3060 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3061 | } |
| 3062 | // mFramesWritten for non-offloaded tracks are contiguous |
| 3063 | // even after standby() is called. This is useful for the track frame |
| 3064 | // to sink frame mapping. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3065 | bool serverLocationUpdate = false; |
| 3066 | if (mFramesWritten != lastFramesWritten) { |
| 3067 | serverLocationUpdate = true; |
| 3068 | lastFramesWritten = mFramesWritten; |
| 3069 | } |
| 3070 | // Only update timestamps if there is a meaningful change. |
| 3071 | // Either the kernel timestamp must be valid or we have written something. |
| 3072 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 3073 | if (serverLocationUpdate) { |
| 3074 | // use the time before we called the HAL write - it is a bit more accurate |
| 3075 | // to when the server last read data than the current time here. |
| 3076 | // |
| 3077 | // If we haven't written anything, mLastWriteTime will be -1 |
| 3078 | // and we use systemTime(). |
| 3079 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 3080 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 |
| 3081 | ? systemTime() : mLastWriteTime; |
| 3082 | } |
| 3083 | const size_t size = mActiveTracks.size(); |
| 3084 | for (size_t i = 0; i < size; ++i) { |
| 3085 | sp<Track> t = mActiveTracks[i].promote(); |
| 3086 | if (t != 0 && !t->isFastTrack()) { |
| 3087 | t->updateTrackFrameInfo( |
| 3088 | t->mAudioTrackServerProxy->framesReleased(), |
| 3089 | mFramesWritten, |
| 3090 | mTimestamp); |
| 3091 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3092 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3095 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3096 | if (mSignalPending) { |
| 3097 | // A signal was raised while we were unlocked |
| 3098 | mSignalPending = false; |
| 3099 | } else if (waitingAsyncCallback_l()) { |
| 3100 | if (exitPending()) { |
| 3101 | break; |
| 3102 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3103 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3104 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3105 | releaseWakeLock_l(); |
| 3106 | released = true; |
| 3107 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3108 | mWakeLockUids.clear(); |
| 3109 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3110 | ALOGV("wait async completion"); |
| 3111 | mWaitWorkCV.wait(mLock); |
| 3112 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3113 | if (released) { |
| 3114 | acquireWakeLock_l(); |
| 3115 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3116 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3117 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3118 | |
| 3119 | continue; |
| 3120 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3121 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3122 | isSuspended()) { |
| 3123 | // put audio hardware into standby after short delay |
| 3124 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3125 | |
| 3126 | threadLoop_standby(); |
| 3127 | |
| 3128 | mStandby = true; |
| 3129 | } |
| 3130 | |
| 3131 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 3132 | // we're about to wait, flush the binder command buffer |
| 3133 | IPCThreadState::self()->flushCommands(); |
| 3134 | |
| 3135 | clearOutputTracks(); |
| 3136 | |
| 3137 | if (exitPending()) { |
| 3138 | break; |
| 3139 | } |
| 3140 | |
| 3141 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3142 | mWakeLockUids.clear(); |
| 3143 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3144 | // wait until we have something to do... |
| 3145 | ALOGV("%s going to sleep", myName.string()); |
| 3146 | mWaitWorkCV.wait(mLock); |
| 3147 | ALOGV("%s waking up", myName.string()); |
| 3148 | acquireWakeLock_l(); |
| 3149 | |
| 3150 | mMixerStatus = MIXER_IDLE; |
| 3151 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 3152 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3153 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3154 | checkSilentMode_l(); |
| 3155 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3156 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3157 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3158 | if (mType == MIXER) { |
| 3159 | sleepTimeShift = 0; |
| 3160 | } |
| 3161 | |
| 3162 | continue; |
| 3163 | } |
| 3164 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3165 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 3166 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 3167 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3168 | // compare with previously applied list |
| 3169 | if (lastGeneration != mActiveTracksGeneration) { |
| 3170 | // update wakelock |
| 3171 | updateWakeLockUids_l(mWakeLockUids); |
| 3172 | lastGeneration = mActiveTracksGeneration; |
| 3173 | } |
| 3174 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3175 | // prevent any changes in effect chain list and in each effect chain |
| 3176 | // during mixing and effect process as the audio buffers could be deleted |
| 3177 | // or modified if an effect is created or deleted |
| 3178 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3179 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3180 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3181 | if (mBytesRemaining == 0) { |
| 3182 | mCurrentWriteLength = 0; |
| 3183 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3184 | // threadLoop_mix() sets mCurrentWriteLength |
| 3185 | threadLoop_mix(); |
| 3186 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3187 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3188 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3189 | // must be written to HAL |
| 3190 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3191 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3192 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3193 | } |
| 3194 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3195 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3196 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3197 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3198 | // or mSinkBuffer (if there are no effects). |
| 3199 | // |
| 3200 | // This is done pre-effects computation; if effects change to |
| 3201 | // support higher precision, this needs to move. |
| 3202 | // |
| 3203 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3204 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3205 | if (mMixerBufferValid) { |
| 3206 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3207 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3208 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3209 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3210 | // and is handled here if we're going directly to the sink. |
| 3211 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3212 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3213 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3214 | } |
| 3215 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3216 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3217 | mNormalFrameCount * mChannelCount); |
| 3218 | } |
| 3219 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3220 | mBytesRemaining = mCurrentWriteLength; |
| 3221 | if (isSuspended()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3222 | mSleepTimeUs = suspendSleepTimeUs(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3223 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3224 | mBytesWritten += mSinkBufferSize; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3225 | mFramesWritten += mSinkBufferSize / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3226 | mBytesRemaining = 0; |
| 3227 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3228 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3229 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3230 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3231 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3232 | effectChains[i]->process_l(); |
| 3233 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3234 | } |
| 3235 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3236 | // Process effect chains for offloaded thread even if no audio |
| 3237 | // was read from audio track: process only updates effect state |
| 3238 | // and thus does have to be synchronized with audio writes but may have |
| 3239 | // to be called while waiting for async write callback |
| 3240 | if (mType == OFFLOAD) { |
| 3241 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3242 | effectChains[i]->process_l(); |
| 3243 | } |
| 3244 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3245 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3246 | // Only if the Effects buffer is enabled and there is data in the |
| 3247 | // Effects buffer (buffer valid), we need to |
| 3248 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3249 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3250 | if (mEffectBufferValid) { |
| 3251 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3252 | |
| 3253 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3254 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3255 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3258 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3259 | mNormalFrameCount * mChannelCount); |
| 3260 | } |
| 3261 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3262 | // enable changes in effect chain |
| 3263 | unlockEffectChains(effectChains); |
| 3264 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3265 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3266 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3267 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3268 | ssize_t ret = 0; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3269 | // We save lastWriteFinished here, as previousLastWriteFinished, |
| 3270 | // for throttling. On thread start, previousLastWriteFinished will be |
| 3271 | // set to -1, which properly results in no throttling after the first write. |
| 3272 | nsecs_t previousLastWriteFinished = lastWriteFinished; |
| 3273 | nsecs_t delta = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3274 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3275 | // FIXME rewrite to reduce number of system calls |
| 3276 | mLastWriteTime = systemTime(); // also used for dumpsys |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3277 | ret = threadLoop_write(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3278 | lastWriteFinished = systemTime(); |
| 3279 | delta = lastWriteFinished - mLastWriteTime; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3280 | if (ret < 0) { |
| 3281 | mBytesRemaining = 0; |
| 3282 | } else { |
| 3283 | mBytesWritten += ret; |
| 3284 | mBytesRemaining -= ret; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3285 | mFramesWritten += ret / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3286 | } |
| 3287 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3288 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3289 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3290 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3291 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3292 | // write blocked detection |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3293 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3294 | mNumDelayedWrites++; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3295 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3296 | ATRACE_NAME("underrun"); |
| 3297 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3298 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3299 | lastWarning = lastWriteFinished; |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3300 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3301 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3302 | |
| 3303 | if (mThreadThrottle |
| 3304 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3305 | && ret > 0) { // we wrote something |
| 3306 | // Limit MixerThread data processing to no more than twice the |
| 3307 | // expected processing rate. |
| 3308 | // |
| 3309 | // This helps prevent underruns with NuPlayer and other applications |
| 3310 | // which may set up buffers that are close to the minimum size, or use |
| 3311 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3312 | // |
| 3313 | // The throttle smooths out sudden large data drains from the device, |
| 3314 | // e.g. when it comes out of standby, which often causes problems with |
| 3315 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3316 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3317 | // the app won't fill fast enough to handle the sudden draw). |
Haynes Mathew George | f92b217 | 2016-05-09 11:34:15 -0700 | [diff] [blame] | 3318 | // |
| 3319 | // Total time spent in last processing cycle equals time spent in |
| 3320 | // 1. threadLoop_write, as well as time spent in |
| 3321 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 3322 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3323 | |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3324 | // it's OK if deltaMs is an overestimate. |
| 3325 | const int32_t deltaMs = |
| 3326 | (lastWriteFinished - previousLastWriteFinished) / 1000000; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3327 | const int32_t throttleMs = mHalfBufferMs - deltaMs; |
| 3328 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3329 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3330 | // notify of throttle start on verbose log |
| 3331 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3332 | "mixer(%p) throttle begin:" |
| 3333 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3334 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3335 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3336 | // Throttle must be attributed to the previous mixer loop's write time |
| 3337 | // to allow back-to-back throttling. |
| 3338 | lastWriteFinished += throttleMs * 1000000; |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3339 | } else { |
| 3340 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3341 | if (diff > 0) { |
| 3342 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3343 | // but prevent spamming for bluetooth |
| 3344 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()), |
| 3345 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3346 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3347 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3348 | } |
| 3349 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3350 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3351 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3352 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3353 | ATRACE_BEGIN("sleep"); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3354 | Mutex::Autolock _l(mLock); |
| 3355 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
| 3356 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3357 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3358 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3359 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | // Finally let go of removed track(s), without the lock held |
| 3363 | // since we can't guarantee the destructors won't acquire that |
| 3364 | // same lock. This will also mutate and push a new fast mixer state. |
| 3365 | threadLoop_removeTracks(tracksToRemove); |
| 3366 | tracksToRemove.clear(); |
| 3367 | |
| 3368 | // FIXME I don't understand the need for this here; |
| 3369 | // it was in the original code but maybe the |
| 3370 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3371 | clearOutputTracks(); |
| 3372 | |
| 3373 | // Effect chains will be actually deleted here if they were removed from |
| 3374 | // mEffectChains list during mixing or effects processing |
| 3375 | effectChains.clear(); |
| 3376 | |
| 3377 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3378 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3379 | } |
| 3380 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3381 | threadLoop_exit(); |
| 3382 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3383 | if (!mStandby) { |
| 3384 | threadLoop_standby(); |
| 3385 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3389 | mWakeLockUids.clear(); |
| 3390 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3391 | |
| 3392 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3393 | return false; |
| 3394 | } |
| 3395 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3396 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3397 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3398 | { |
| 3399 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3400 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3401 | for (size_t i=0 ; i<count ; i++) { |
| 3402 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3403 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3404 | mWakeLockUids.remove(track->uid()); |
| 3405 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3406 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3407 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3408 | if (chain != 0) { |
| 3409 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3410 | track->sessionId()); |
| 3411 | chain->decActiveTrackCnt(); |
| 3412 | } |
| 3413 | if (track->isTerminated()) { |
| 3414 | removeTrack_l(track); |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3420 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3421 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3422 | { |
| 3423 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3424 | ExtendedTimestamp ets; |
| 3425 | status_t status = mNormalSink->getTimestamp(ets); |
| 3426 | if (status == NO_ERROR) { |
| 3427 | status = ets.getBestTimestamp(×tamp); |
| 3428 | } |
| 3429 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3430 | } |
Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3431 | if ((mType == OFFLOAD || mType == DIRECT) |
| 3432 | && mOutput != NULL && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3433 | uint64_t position64; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3434 | int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3435 | if (ret == 0) { |
| 3436 | timestamp.mPosition = (uint32_t)position64; |
| 3437 | return NO_ERROR; |
| 3438 | } |
| 3439 | } |
| 3440 | return INVALID_OPERATION; |
| 3441 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3442 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3443 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3444 | audio_patch_handle_t *handle) |
| 3445 | { |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3446 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3447 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3448 | status_t status = PlaybackThread::createAudioPatch_l(patch, handle); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3449 | |
| 3450 | return status; |
| 3451 | } |
| 3452 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3453 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3454 | audio_patch_handle_t *handle) |
| 3455 | { |
| 3456 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3457 | |
| 3458 | // store new device and send to effects |
| 3459 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3460 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3461 | type |= patch->sinks[i].ext.device.type; |
| 3462 | } |
| 3463 | |
| 3464 | #ifdef ADD_BATTERY_DATA |
| 3465 | // when changing the audio output device, call addBatteryData to notify |
| 3466 | // the change |
| 3467 | if (mOutDevice != type) { |
| 3468 | uint32_t params = 0; |
| 3469 | // check whether speaker is on |
| 3470 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3471 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3472 | } |
| 3473 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3474 | audio_devices_t deviceWithoutSpeaker |
| 3475 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3476 | // check if any other device (except speaker) is on |
| 3477 | if (type & deviceWithoutSpeaker) { |
| 3478 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3479 | } |
| 3480 | |
| 3481 | if (params != 0) { |
| 3482 | addBatteryData(params); |
| 3483 | } |
| 3484 | } |
| 3485 | #endif |
| 3486 | |
| 3487 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3488 | mEffectChains[i]->setDevice_l(type); |
| 3489 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3490 | |
| 3491 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3492 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3493 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3494 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3495 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3496 | |
| 3497 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3498 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3499 | status = hwDevice->create_audio_patch(hwDevice, |
| 3500 | patch->num_sources, |
| 3501 | patch->sources, |
| 3502 | patch->num_sinks, |
| 3503 | patch->sinks, |
| 3504 | handle); |
| 3505 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3506 | char *address; |
| 3507 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3508 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3509 | address = audio_device_address_to_parameter( |
| 3510 | patch->sinks[0].ext.device.type, |
| 3511 | patch->sinks[0].ext.device.address); |
| 3512 | } else { |
| 3513 | address = (char *)calloc(1, 1); |
| 3514 | } |
| 3515 | AudioParameter param = AudioParameter(String8(address)); |
| 3516 | free(address); |
| 3517 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
| 3518 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3519 | param.toString().string()); |
| 3520 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3521 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3522 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3523 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3524 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3525 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3526 | return status; |
| 3527 | } |
| 3528 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3529 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3530 | { |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3531 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3532 | |
| 3533 | status_t status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3534 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3535 | return status; |
| 3536 | } |
| 3537 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3538 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3539 | { |
| 3540 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3541 | |
| 3542 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3543 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3544 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 3545 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3546 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 3547 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3548 | AudioParameter param; |
| 3549 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 3550 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3551 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3552 | } |
| 3553 | return status; |
| 3554 | } |
| 3555 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3556 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3557 | { |
| 3558 | Mutex::Autolock _l(mLock); |
| 3559 | mTracks.add(track); |
| 3560 | } |
| 3561 | |
| 3562 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3563 | { |
| 3564 | Mutex::Autolock _l(mLock); |
| 3565 | destroyTrack_l(track); |
| 3566 | } |
| 3567 | |
| 3568 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3569 | { |
| 3570 | ThreadBase::getAudioPortConfig(config); |
| 3571 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3572 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3573 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3574 | } |
| 3575 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3576 | // ---------------------------------------------------------------------------- |
| 3577 | |
| 3578 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3579 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3580 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3581 | // mAudioMixer below |
| 3582 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3583 | mFastMixerFutex(0), |
| 3584 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3585 | // mOutputSink below |
| 3586 | // mPipeSink below |
| 3587 | // mNormalSink below |
| 3588 | { |
| 3589 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3590 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%zu, " |
| 3591 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3592 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3593 | mNormalFrameCount); |
| 3594 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3595 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3596 | if (type == DUPLICATING) { |
| 3597 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3598 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3599 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3600 | return; |
| 3601 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3602 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 3603 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 3604 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3605 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3606 | #if !LOG_NDEBUG |
| 3607 | ssize_t index = |
| 3608 | #else |
| 3609 | (void) |
| 3610 | #endif |
| 3611 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 | ALOG_ASSERT(index == 0); |
| 3613 | |
| 3614 | // initialize fast mixer depending on configuration |
| 3615 | bool initFastMixer; |
| 3616 | switch (kUseFastMixer) { |
| 3617 | case FastMixer_Never: |
| 3618 | initFastMixer = false; |
| 3619 | break; |
| 3620 | case FastMixer_Always: |
| 3621 | initFastMixer = true; |
| 3622 | break; |
| 3623 | case FastMixer_Static: |
| 3624 | case FastMixer_Dynamic: |
| 3625 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 3626 | break; |
| 3627 | } |
| 3628 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3629 | audio_format_t fastMixerFormat; |
| 3630 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3631 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3632 | } else { |
| 3633 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3634 | } |
| 3635 | if (mFormat != fastMixerFormat) { |
| 3636 | // change our Sink format to accept our intermediate precision |
| 3637 | mFormat = fastMixerFormat; |
| 3638 | free(mSinkBuffer); |
| 3639 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3640 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3641 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3642 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3643 | |
| 3644 | // create a MonoPipe to connect our submix to FastMixer |
| 3645 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3646 | #ifdef TEE_SINK |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3647 | NBAIO_Format origformat = format; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3648 | #endif |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3649 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3650 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3651 | format.mFormat = fastMixerFormat; |
| 3652 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3653 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3654 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3655 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3656 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3657 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3658 | const NBAIO_Format offers[1] = {format}; |
| 3659 | size_t numCounterOffers = 0; |
Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3660 | #if !LOG_NDEBUG || defined(TEE_SINK) |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3661 | ssize_t index = |
| 3662 | #else |
| 3663 | (void) |
| 3664 | #endif |
| 3665 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3666 | ALOG_ASSERT(index == 0); |
| 3667 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3668 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3669 | mPipeSink = monoPipe; |
| 3670 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3671 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3672 | if (mTeeSinkOutputEnabled) { |
| 3673 | // 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] | 3674 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3675 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3676 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3677 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3678 | ALOG_ASSERT(index == 0); |
| 3679 | mTeeSink = teeSink; |
| 3680 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3681 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3682 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3683 | ALOG_ASSERT(index == 0); |
| 3684 | mTeeSource = teeSource; |
| 3685 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3686 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3687 | |
| 3688 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3689 | mFastMixer = new FastMixer(); |
| 3690 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3691 | #ifdef STATE_QUEUE_DUMP |
| 3692 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3693 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3694 | #endif |
| 3695 | FastMixerState *state = sq->begin(); |
| 3696 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3697 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3698 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3699 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3700 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3701 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3702 | fastTrack->mGeneration++; |
| 3703 | state->mFastTracksGen++; |
| 3704 | state->mTrackMask = 1; |
| 3705 | // fast mixer will use the HAL output sink |
| 3706 | state->mOutputSink = mOutputSink.get(); |
| 3707 | state->mOutputSinkGen++; |
| 3708 | state->mFrameCount = mFrameCount; |
| 3709 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3710 | // already done in constructor initialization list |
| 3711 | //mFastMixerFutex = 0; |
| 3712 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3713 | state->mColdGen++; |
| 3714 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3715 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3716 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3717 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3718 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3719 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3720 | sq->end(); |
| 3721 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3722 | |
| 3723 | // start the fast mixer |
| 3724 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3725 | pid_t tid = mFastMixer->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3726 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3727 | |
| 3728 | #ifdef AUDIO_WATCHDOG |
| 3729 | // create and start the watchdog |
| 3730 | mAudioWatchdog = new AudioWatchdog(); |
| 3731 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3732 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3733 | tid = mAudioWatchdog->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3734 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3735 | #endif |
| 3736 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | switch (kUseFastMixer) { |
| 3740 | case FastMixer_Never: |
| 3741 | case FastMixer_Dynamic: |
| 3742 | mNormalSink = mOutputSink; |
| 3743 | break; |
| 3744 | case FastMixer_Always: |
| 3745 | mNormalSink = mPipeSink; |
| 3746 | break; |
| 3747 | case FastMixer_Static: |
| 3748 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3749 | break; |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | AudioFlinger::MixerThread::~MixerThread() |
| 3754 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3755 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3756 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3757 | FastMixerState *state = sq->begin(); |
| 3758 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3759 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3760 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3761 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3762 | } |
| 3763 | } |
| 3764 | state->mCommand = FastMixerState::EXIT; |
| 3765 | sq->end(); |
| 3766 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3767 | mFastMixer->join(); |
| 3768 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3769 | // We'll use that extract the final state which contains one remaining fast track |
| 3770 | // corresponding to our sub-mix. |
| 3771 | state = sq->begin(); |
| 3772 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3773 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3774 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3775 | delete fastTrack->mBufferProvider; |
| 3776 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3777 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3778 | #ifdef AUDIO_WATCHDOG |
| 3779 | if (mAudioWatchdog != 0) { |
| 3780 | mAudioWatchdog->requestExit(); |
| 3781 | mAudioWatchdog->requestExitAndWait(); |
| 3782 | mAudioWatchdog.clear(); |
| 3783 | } |
| 3784 | #endif |
| 3785 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3786 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3787 | delete mAudioMixer; |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3792 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3793 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3794 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3795 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3796 | } |
| 3797 | return latency; |
| 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3802 | { |
| 3803 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3804 | } |
| 3805 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3806 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3807 | { |
| 3808 | // FIXME we should only do one push per cycle; confirm this is true |
| 3809 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3810 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3811 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3812 | FastMixerState *state = sq->begin(); |
| 3813 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3814 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3815 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3816 | |
| 3817 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 3818 | ATRACE_BEGIN("write"); |
| 3819 | mOutput->write((char *)mSinkBuffer, 0); |
| 3820 | ATRACE_END(); |
| 3821 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3822 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3823 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3824 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3825 | } |
| 3826 | #ifdef AUDIO_WATCHDOG |
| 3827 | if (mAudioWatchdog != 0) { |
| 3828 | mAudioWatchdog->resume(); |
| 3829 | } |
| 3830 | #endif |
| 3831 | } |
| 3832 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3833 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3834 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3835 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3836 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3837 | sq->end(); |
| 3838 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3839 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3840 | mNormalSink = mPipeSink; |
| 3841 | } |
| 3842 | } else { |
| 3843 | sq->end(false /*didModify*/); |
| 3844 | } |
| 3845 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3846 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3850 | { |
| 3851 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3852 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3853 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3854 | FastMixerState *state = sq->begin(); |
| 3855 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3856 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3857 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3858 | state->mColdGen++; |
| 3859 | mFastMixerFutex = 0; |
| 3860 | sq->end(); |
| 3861 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3862 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3863 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3864 | mNormalSink = mOutputSink; |
| 3865 | } |
| 3866 | #ifdef AUDIO_WATCHDOG |
| 3867 | if (mAudioWatchdog != 0) { |
| 3868 | mAudioWatchdog->pause(); |
| 3869 | } |
| 3870 | #endif |
| 3871 | } else { |
| 3872 | sq->end(false /*didModify*/); |
| 3873 | } |
| 3874 | } |
| 3875 | PlaybackThread::threadLoop_standby(); |
| 3876 | } |
| 3877 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3878 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3879 | { |
| 3880 | return false; |
| 3881 | } |
| 3882 | |
| 3883 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3884 | { |
| 3885 | return !mStandby; |
| 3886 | } |
| 3887 | |
| 3888 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3889 | { |
| 3890 | Mutex::Autolock _l(mLock); |
| 3891 | return waitingAsyncCallback_l(); |
| 3892 | } |
| 3893 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3894 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3895 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3896 | { |
| 3897 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3898 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3899 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3900 | // discard any pending drain or write ack by incrementing sequence |
| 3901 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3902 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3903 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3904 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3905 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3906 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3907 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3910 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3911 | { |
| 3912 | ALOGV("signal playback thread"); |
| 3913 | broadcast_l(); |
| 3914 | } |
| 3915 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3916 | void AudioFlinger::PlaybackThread::onAsyncError() |
| 3917 | { |
| 3918 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 3919 | invalidateTracks((audio_stream_type_t)i); |
| 3920 | } |
| 3921 | } |
| 3922 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3923 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3924 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3925 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3926 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3927 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3928 | // increase sleep time progressively when application underrun condition clears. |
| 3929 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3930 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3931 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3932 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3933 | sleepTimeShift--; |
| 3934 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3935 | mSleepTimeUs = 0; |
| 3936 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3937 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3938 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3942 | { |
| 3943 | // If no tracks are ready, sleep once for the duration of an output |
| 3944 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3945 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3946 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3947 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 3948 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 3949 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3950 | } |
| 3951 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3952 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3953 | // duration we would end up writing less data than needed by the audio HAL if |
| 3954 | // the condition persists. |
| 3955 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3956 | sleepTimeShift++; |
| 3957 | } |
| 3958 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3959 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3960 | } |
| 3961 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3962 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3963 | // before effects processing or output. |
| 3964 | if (mMixerBufferValid) { |
| 3965 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3966 | } else { |
| 3967 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3968 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3969 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3970 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3971 | "anticipated start"); |
| 3972 | } |
| 3973 | // TODO add standby time extension fct of effect tail |
| 3974 | } |
| 3975 | |
| 3976 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3977 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3978 | Vector< sp<Track> > *tracksToRemove) |
| 3979 | { |
| 3980 | |
| 3981 | mixer_state mixerStatus = MIXER_IDLE; |
| 3982 | // find out which tracks need to be processed |
| 3983 | size_t count = mActiveTracks.size(); |
| 3984 | size_t mixedTracks = 0; |
| 3985 | size_t tracksWithEffect = 0; |
| 3986 | // counts only _active_ fast tracks |
| 3987 | size_t fastTracks = 0; |
| 3988 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3989 | |
| 3990 | float masterVolume = mMasterVolume; |
| 3991 | bool masterMute = mMasterMute; |
| 3992 | |
| 3993 | if (masterMute) { |
| 3994 | masterVolume = 0; |
| 3995 | } |
| 3996 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3997 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3998 | if (chain != 0) { |
| 3999 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 4000 | chain->setVolume_l(&v, &v); |
| 4001 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 4002 | chain.clear(); |
| 4003 | } |
| 4004 | |
| 4005 | // prepare a new state to push |
| 4006 | FastMixerStateQueue *sq = NULL; |
| 4007 | FastMixerState *state = NULL; |
| 4008 | bool didModify = false; |
| 4009 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4010 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4011 | sq = mFastMixer->sq(); |
| 4012 | state = sq->begin(); |
| 4013 | } |
| 4014 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4015 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4016 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4017 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4018 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 4019 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4020 | if (t == 0) { |
| 4021 | continue; |
| 4022 | } |
| 4023 | |
| 4024 | // this const just means the local variable doesn't change |
| 4025 | Track* const track = t.get(); |
| 4026 | |
| 4027 | // process fast tracks |
| 4028 | if (track->isFastTrack()) { |
| 4029 | |
| 4030 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 4031 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 4032 | // the track never becomes active so it's fast mixer slot is never touched. |
| 4033 | // The converse, of removing an (active) track and then creating a new track |
| 4034 | // at the identical fast mixer slot within the same normal mix cycle, |
| 4035 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 4036 | int j = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4037 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4038 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 4039 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 4040 | |
| 4041 | // Determine whether the track is currently in underrun condition, |
| 4042 | // and whether it had a recent underrun. |
| 4043 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 4044 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 4045 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 4046 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 4047 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 4048 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 4049 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 4050 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 4051 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 4052 | track->mObservedUnderruns = underruns; |
| 4053 | // don't count underruns that occur while stopping or pausing |
| 4054 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4055 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 4056 | recentUnderruns > 0) { |
| 4057 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 4058 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4059 | } else { |
| 4060 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | // This is similar to the state machine for normal tracks, |
| 4064 | // with a few modifications for fast tracks. |
| 4065 | bool isActive = true; |
| 4066 | switch (track->mState) { |
| 4067 | case TrackBase::STOPPING_1: |
| 4068 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4069 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4070 | track->mState = TrackBase::STOPPING_2; |
| 4071 | } |
| 4072 | break; |
| 4073 | case TrackBase::PAUSING: |
| 4074 | // ramp down is not yet implemented |
| 4075 | track->setPaused(); |
| 4076 | break; |
| 4077 | case TrackBase::RESUMING: |
| 4078 | // ramp up is not yet implemented |
| 4079 | track->mState = TrackBase::ACTIVE; |
| 4080 | break; |
| 4081 | case TrackBase::ACTIVE: |
| 4082 | if (recentFull > 0 || recentPartial > 0) { |
| 4083 | // track has provided at least some frames recently: reset retry count |
| 4084 | track->mRetryCount = kMaxTrackRetries; |
| 4085 | } |
| 4086 | if (recentUnderruns == 0) { |
| 4087 | // no recent underruns: stay active |
| 4088 | break; |
| 4089 | } |
| 4090 | // there has recently been an underrun of some kind |
| 4091 | if (track->sharedBuffer() == 0) { |
| 4092 | // were any of the recent underruns "empty" (no frames available)? |
| 4093 | if (recentEmpty == 0) { |
| 4094 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 4095 | break; |
| 4096 | } |
| 4097 | // there has recently been an "empty" underrun: decrement the retry counter |
| 4098 | if (--(track->mRetryCount) > 0) { |
| 4099 | break; |
| 4100 | } |
| 4101 | // indicate to client process that the track was disabled because of underrun; |
| 4102 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4103 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4104 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 4105 | isActive = false; |
| 4106 | break; |
| 4107 | } |
| 4108 | // fall through |
| 4109 | case TrackBase::STOPPING_2: |
| 4110 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4111 | case TrackBase::STOPPED: |
| 4112 | case TrackBase::FLUSHED: // flush() while active |
| 4113 | // Check for presentation complete if track is inactive |
| 4114 | // We have consumed all the buffers of this track. |
| 4115 | // This would be incomplete if we auto-paused on underrun |
| 4116 | { |
| 4117 | size_t audioHALFrames = |
| 4118 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4119 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4120 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 4121 | // track stays in active list until presentation is complete |
| 4122 | break; |
| 4123 | } |
| 4124 | } |
| 4125 | if (track->isStopping_2()) { |
| 4126 | track->mState = TrackBase::STOPPED; |
| 4127 | } |
| 4128 | if (track->isStopped()) { |
| 4129 | // Can't reset directly, as fast mixer is still polling this track |
| 4130 | // track->reset(); |
| 4131 | // So instead mark this track as needing to be reset after push with ack |
| 4132 | resetMask |= 1 << i; |
| 4133 | } |
| 4134 | isActive = false; |
| 4135 | break; |
| 4136 | case TrackBase::IDLE: |
| 4137 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4138 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | if (isActive) { |
| 4142 | // was it previously inactive? |
| 4143 | if (!(state->mTrackMask & (1 << j))) { |
| 4144 | ExtendedAudioBufferProvider *eabp = track; |
| 4145 | VolumeProvider *vp = track; |
| 4146 | fastTrack->mBufferProvider = eabp; |
| 4147 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4148 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4149 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4150 | fastTrack->mGeneration++; |
| 4151 | state->mTrackMask |= 1 << j; |
| 4152 | didModify = true; |
| 4153 | // no acknowledgement required for newly active tracks |
| 4154 | } |
| 4155 | // cache the combined master volume and stream type volume for fast mixer; this |
| 4156 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4157 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4158 | ++fastTracks; |
| 4159 | } else { |
| 4160 | // was it previously active? |
| 4161 | if (state->mTrackMask & (1 << j)) { |
| 4162 | fastTrack->mBufferProvider = NULL; |
| 4163 | fastTrack->mGeneration++; |
| 4164 | state->mTrackMask &= ~(1 << j); |
| 4165 | didModify = true; |
| 4166 | // If any fast tracks were removed, we must wait for acknowledgement |
| 4167 | // because we're about to decrement the last sp<> on those tracks. |
| 4168 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4169 | } else { |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4170 | LOG_ALWAYS_FATAL("fast track %d should have been active; " |
| 4171 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 4172 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 4173 | track->sharedBuffer() != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4174 | } |
| 4175 | tracksToRemove->add(track); |
| 4176 | // Avoids a misleading display in dumpsys |
| 4177 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 4178 | } |
| 4179 | continue; |
| 4180 | } |
| 4181 | |
| 4182 | { // local variable scope to avoid goto warning |
| 4183 | |
| 4184 | audio_track_cblk_t* cblk = track->cblk(); |
| 4185 | |
| 4186 | // The first time a track is added we wait |
| 4187 | // for all its buffers to be filled before processing it |
| 4188 | int name = track->name(); |
| 4189 | // make sure that we have enough frames to mix one full buffer. |
| 4190 | // enforce this condition only once to enable draining the buffer in case the client |
| 4191 | // app does not call stop() and relies on underrun to stop: |
| 4192 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4193 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4194 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4195 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4196 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4197 | |
| 4198 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4199 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4200 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4201 | // add frames already consumed but not yet released by the resampler |
| 4202 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4203 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4204 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4205 | uint32_t minFrames = 1; |
| 4206 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4207 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4208 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4209 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4210 | |
| 4211 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4212 | if (ATRACE_ENABLED()) { |
| 4213 | // I wish we had formatted trace names |
| 4214 | char traceName[16]; |
| 4215 | strcpy(traceName, "nRdy"); |
| 4216 | int name = track->name(); |
| 4217 | if (AudioMixer::TRACK0 <= name && |
| 4218 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 4219 | name -= AudioMixer::TRACK0; |
| 4220 | traceName[4] = (name / 10) + '0'; |
| 4221 | traceName[5] = (name % 10) + '0'; |
| 4222 | } else { |
| 4223 | traceName[4] = '?'; |
| 4224 | traceName[5] = '?'; |
| 4225 | } |
| 4226 | traceName[6] = '\0'; |
| 4227 | ATRACE_INT(traceName, framesReady); |
| 4228 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4229 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4230 | !track->isPaused() && !track->isTerminated()) |
| 4231 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4232 | 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] | 4233 | |
| 4234 | mixedTracks++; |
| 4235 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4236 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4237 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4238 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4239 | if (track->mainBuffer() != mSinkBuffer && |
| 4240 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4241 | if (mEffectBufferEnabled) { |
| 4242 | mEffectBufferValid = true; // Later can set directly. |
| 4243 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4244 | chain = getEffectChain_l(track->sessionId()); |
| 4245 | // Delegate volume control to effect in track effect chain if needed |
| 4246 | if (chain != 0) { |
| 4247 | tracksWithEffect++; |
| 4248 | } else { |
| 4249 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4250 | "session %d", |
| 4251 | name, track->sessionId()); |
| 4252 | } |
| 4253 | } |
| 4254 | |
| 4255 | |
| 4256 | int param = AudioMixer::VOLUME; |
| 4257 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4258 | // no ramp for the first volume setting |
| 4259 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4260 | if (track->mState == TrackBase::RESUMING) { |
| 4261 | track->mState = TrackBase::ACTIVE; |
| 4262 | param = AudioMixer::RAMP_VOLUME; |
| 4263 | } |
| 4264 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4265 | // FIXME should not make a decision based on mServer |
| 4266 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4267 | // If the track is stopped before the first frame was mixed, |
| 4268 | // do not apply ramp |
| 4269 | param = AudioMixer::RAMP_VOLUME; |
| 4270 | } |
| 4271 | |
| 4272 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4273 | uint32_t vl, vr; // in U8.24 integer format |
| 4274 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4275 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4276 | vl = vr = 0; |
| 4277 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4278 | if (track->isPausing()) { |
| 4279 | track->setPaused(); |
| 4280 | } |
| 4281 | } else { |
| 4282 | |
| 4283 | // read original volumes with volume control |
| 4284 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4285 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4286 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4287 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4288 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4289 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4290 | // 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] | 4291 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4292 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4293 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4294 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4295 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4296 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4297 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4298 | } |
| 4299 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4300 | vlf *= v; |
| 4301 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4302 | // 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] | 4303 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4304 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4305 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4306 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4307 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4308 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4309 | // send level comes from shared memory and so may be corrupt |
| 4310 | if (sendLevel > MAX_GAIN_INT) { |
| 4311 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4312 | sendLevel = MAX_GAIN_INT; |
| 4313 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4314 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4315 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4316 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4317 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4318 | // Delegate volume control to effect in track effect chain if needed |
| 4319 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4320 | // Do not ramp volume if volume is controlled by effect |
| 4321 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4322 | // Update remaining floating point volume levels |
| 4323 | vlf = (float)vl / (1 << 24); |
| 4324 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4325 | track->mHasVolumeController = true; |
| 4326 | } else { |
| 4327 | // force no volume ramp when volume controller was just disabled or removed |
| 4328 | // from effect chain to avoid volume spike |
| 4329 | if (track->mHasVolumeController) { |
| 4330 | param = AudioMixer::VOLUME; |
| 4331 | } |
| 4332 | track->mHasVolumeController = false; |
| 4333 | } |
| 4334 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4335 | // XXX: these things DON'T need to be done each time |
| 4336 | mAudioMixer->setBufferProvider(name, track); |
| 4337 | mAudioMixer->enable(name); |
| 4338 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4339 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4340 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4341 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4342 | mAudioMixer->setParameter( |
| 4343 | name, |
| 4344 | AudioMixer::TRACK, |
| 4345 | AudioMixer::FORMAT, (void *)track->format()); |
| 4346 | mAudioMixer->setParameter( |
| 4347 | name, |
| 4348 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4349 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4350 | mAudioMixer->setParameter( |
| 4351 | name, |
| 4352 | AudioMixer::TRACK, |
| 4353 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4354 | // 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] | 4355 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4356 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4357 | if (reqSampleRate == 0) { |
| 4358 | reqSampleRate = mSampleRate; |
| 4359 | } else if (reqSampleRate > maxSampleRate) { |
| 4360 | reqSampleRate = maxSampleRate; |
| 4361 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4362 | mAudioMixer->setParameter( |
| 4363 | name, |
| 4364 | AudioMixer::RESAMPLE, |
| 4365 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4366 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4367 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4368 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4369 | mAudioMixer->setParameter( |
| 4370 | name, |
| 4371 | AudioMixer::TIMESTRETCH, |
| 4372 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4373 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4374 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4375 | /* |
| 4376 | * Select the appropriate output buffer for the track. |
| 4377 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4378 | * Tracks with effects go into their own effects chain buffer |
| 4379 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4380 | * |
| 4381 | * Other tracks can use mMixerBuffer for higher precision |
| 4382 | * channel accumulation. If this buffer is enabled |
| 4383 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4384 | * into it. |
| 4385 | * |
| 4386 | */ |
| 4387 | if (mMixerBufferEnabled |
| 4388 | && (track->mainBuffer() == mSinkBuffer |
| 4389 | || track->mainBuffer() == mMixerBuffer)) { |
| 4390 | mAudioMixer->setParameter( |
| 4391 | name, |
| 4392 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4393 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4394 | mAudioMixer->setParameter( |
| 4395 | name, |
| 4396 | AudioMixer::TRACK, |
| 4397 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4398 | // TODO: override track->mainBuffer()? |
| 4399 | mMixerBufferValid = true; |
| 4400 | } else { |
| 4401 | mAudioMixer->setParameter( |
| 4402 | name, |
| 4403 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4404 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4405 | mAudioMixer->setParameter( |
| 4406 | name, |
| 4407 | AudioMixer::TRACK, |
| 4408 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4409 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4410 | mAudioMixer->setParameter( |
| 4411 | name, |
| 4412 | AudioMixer::TRACK, |
| 4413 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4414 | |
| 4415 | // reset retry count |
| 4416 | track->mRetryCount = kMaxTrackRetries; |
| 4417 | |
| 4418 | // If one track is ready, set the mixer ready if: |
| 4419 | // - the mixer was not ready during previous round OR |
| 4420 | // - no other track is not ready |
| 4421 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4422 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4423 | mixerStatus = MIXER_TRACKS_READY; |
| 4424 | } |
| 4425 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4426 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4427 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4428 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4429 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4430 | } else { |
| 4431 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4432 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4433 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4434 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4435 | // previous audio buffer again to effects |
| 4436 | chain = getEffectChain_l(track->sessionId()); |
| 4437 | if (chain != 0) { |
| 4438 | chain->clearInputBuffer(); |
| 4439 | } |
| 4440 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4441 | 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] | 4442 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4443 | track->isStopped() || track->isPaused()) { |
| 4444 | // We have consumed all the buffers of this track. |
| 4445 | // Remove it from the list of active tracks. |
| 4446 | // TODO: use actual buffer filling status instead of latency when available from |
| 4447 | // audio HAL |
| 4448 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4449 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4450 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4451 | if (track->isStopped()) { |
| 4452 | track->reset(); |
| 4453 | } |
| 4454 | tracksToRemove->add(track); |
| 4455 | } |
| 4456 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4457 | // No buffers for this track. Give it a few chances to |
| 4458 | // fill a buffer, then remove it from active list. |
| 4459 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4460 | 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] | 4461 | tracksToRemove->add(track); |
| 4462 | // indicate to client process that the track was disabled because of underrun; |
| 4463 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4464 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4465 | // If one track is not ready, mark the mixer also not ready if: |
| 4466 | // - the mixer was ready during previous round OR |
| 4467 | // - no other track is ready |
| 4468 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4469 | mixerStatus != MIXER_TRACKS_READY) { |
| 4470 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4471 | } |
| 4472 | } |
| 4473 | mAudioMixer->disable(name); |
| 4474 | } |
| 4475 | |
| 4476 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4477 | |
| 4478 | } |
| 4479 | |
| 4480 | // Push the new FastMixer state if necessary |
| 4481 | bool pauseAudioWatchdog = false; |
| 4482 | if (didModify) { |
| 4483 | state->mFastTracksGen++; |
| 4484 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4485 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4486 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4487 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4488 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4489 | state->mColdGen++; |
| 4490 | mFastMixerFutex = 0; |
| 4491 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4492 | mNormalSink = mOutputSink; |
| 4493 | } |
| 4494 | // If we go into cold idle, need to wait for acknowledgement |
| 4495 | // so that fast mixer stops doing I/O. |
| 4496 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4497 | pauseAudioWatchdog = true; |
| 4498 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4499 | } |
| 4500 | if (sq != NULL) { |
| 4501 | sq->end(didModify); |
| 4502 | sq->push(block); |
| 4503 | } |
| 4504 | #ifdef AUDIO_WATCHDOG |
| 4505 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4506 | mAudioWatchdog->pause(); |
| 4507 | } |
| 4508 | #endif |
| 4509 | |
| 4510 | // Now perform the deferred reset on fast tracks that have stopped |
| 4511 | while (resetMask != 0) { |
| 4512 | size_t i = __builtin_ctz(resetMask); |
| 4513 | ALOG_ASSERT(i < count); |
| 4514 | resetMask &= ~(1 << i); |
| 4515 | sp<Track> t = mActiveTracks[i].promote(); |
| 4516 | if (t == 0) { |
| 4517 | continue; |
| 4518 | } |
| 4519 | Track* track = t.get(); |
| 4520 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4521 | track->reset(); |
| 4522 | } |
| 4523 | |
| 4524 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4525 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4526 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4527 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4528 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4532 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4533 | // passing a non-clean buffer to the effect chain |
| 4534 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4535 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4536 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4537 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4538 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4539 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4540 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4541 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4542 | if (mMixerBufferValid) { |
| 4543 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4544 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4545 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4546 | // after mixing. |
| 4547 | // |
| 4548 | // To enforce this guarantee: |
| 4549 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4550 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4551 | // must imply MIXER_TRACKS_READY. |
| 4552 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4553 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4554 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4555 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | // if any fast tracks, then status is ready |
| 4559 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4560 | if (fastTracks > 0) { |
| 4561 | mixerStatus = MIXER_TRACKS_READY; |
| 4562 | } |
| 4563 | return mixerStatus; |
| 4564 | } |
| 4565 | |
| 4566 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4567 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 4568 | audio_format_t format, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4569 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4570 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 4574 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 4575 | { |
| 4576 | ALOGV("remove track (%d) and delete from mixer", name); |
| 4577 | mAudioMixer->deleteTrackName(name); |
| 4578 | } |
| 4579 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4580 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4581 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4582 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4583 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4584 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4585 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4586 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4587 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4588 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4589 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4590 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4591 | AudioParameter param = AudioParameter(keyValuePair); |
| 4592 | int value; |
| 4593 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4594 | reconfig = true; |
| 4595 | } |
| 4596 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4597 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4598 | status = BAD_VALUE; |
| 4599 | } else { |
| 4600 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4601 | reconfig = true; |
| 4602 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4603 | } |
| 4604 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4605 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4606 | status = BAD_VALUE; |
| 4607 | } else { |
| 4608 | // no need to save value, since it's constant |
| 4609 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4610 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4611 | } |
| 4612 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4613 | // do not accept frame count changes if tracks are open as the track buffer |
| 4614 | // size depends on frame count and correct behavior would not be guaranteed |
| 4615 | // if frame count is changed after track creation |
| 4616 | if (!mTracks.isEmpty()) { |
| 4617 | status = INVALID_OPERATION; |
| 4618 | } else { |
| 4619 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4620 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4621 | } |
| 4622 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4623 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4624 | // when changing the audio output device, call addBatteryData to notify |
| 4625 | // the change |
| 4626 | if (mOutDevice != value) { |
| 4627 | uint32_t params = 0; |
| 4628 | // check whether speaker is on |
| 4629 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4630 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4631 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4632 | |
| 4633 | audio_devices_t deviceWithoutSpeaker |
| 4634 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4635 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4636 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4637 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4638 | } |
| 4639 | |
| 4640 | if (params != 0) { |
| 4641 | addBatteryData(params); |
| 4642 | } |
| 4643 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4644 | #endif |
| 4645 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4646 | // forward device change to effects that have requested to be |
| 4647 | // aware of attached audio device. |
| 4648 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4649 | a2dpDeviceChanged = |
| 4650 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4651 | mOutDevice = value; |
| 4652 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4653 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4654 | } |
| 4655 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4656 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4657 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4658 | if (status == NO_ERROR) { |
| 4659 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4660 | keyValuePair.string()); |
| 4661 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4662 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4663 | mStandby = true; |
| 4664 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4665 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4666 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4667 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4668 | if (status == NO_ERROR && reconfig) { |
| 4669 | readOutputParameters_l(); |
| 4670 | delete mAudioMixer; |
| 4671 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4672 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4673 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 4674 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4675 | if (name < 0) { |
| 4676 | break; |
| 4677 | } |
| 4678 | mTracks[i]->mName = name; |
| 4679 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4680 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4681 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4682 | } |
| 4683 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4684 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | |
| 4688 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4689 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4690 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4691 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4692 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4693 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4694 | |
| 4695 | // 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] | 4696 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 4697 | // This is a large object so we place it on the heap. |
| 4698 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 4699 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 4700 | copy->dump(fd); |
| 4701 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4702 | |
| 4703 | #ifdef STATE_QUEUE_DUMP |
| 4704 | // Similar for state queue |
| 4705 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4706 | observerCopy.dump(fd); |
| 4707 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4708 | mutatorCopy.dump(fd); |
| 4709 | #endif |
| 4710 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4711 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4712 | // Write the tee output to a .wav file |
| 4713 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4714 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4715 | |
| 4716 | #ifdef AUDIO_WATCHDOG |
| 4717 | if (mAudioWatchdog != 0) { |
| 4718 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4719 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4720 | wdCopy.dump(fd); |
| 4721 | } |
| 4722 | #endif |
| 4723 | } |
| 4724 | |
| 4725 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4726 | { |
| 4727 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4728 | } |
| 4729 | |
| 4730 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4731 | { |
| 4732 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4733 | } |
| 4734 | |
| 4735 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4736 | { |
| 4737 | PlaybackThread::cacheParameters_l(); |
| 4738 | |
| 4739 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4740 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4741 | // increase threshold again due to low power audio mode. The way this warning |
| 4742 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4743 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4744 | } |
| 4745 | |
| 4746 | // ---------------------------------------------------------------------------- |
| 4747 | |
| 4748 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4749 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 4750 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4751 | // mLeftVolFloat, mRightVolFloat |
| 4752 | { |
| 4753 | } |
| 4754 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4755 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4756 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4757 | ThreadBase::type_t type, bool systemReady) |
| 4758 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4759 | // mLeftVolFloat, mRightVolFloat |
| 4760 | { |
| 4761 | } |
| 4762 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4763 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4764 | { |
| 4765 | } |
| 4766 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4767 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 4768 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4769 | float left, right; |
| 4770 | |
| 4771 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4772 | left = right = 0; |
| 4773 | } else { |
| 4774 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4775 | float v = mMasterVolume * typeVolume; |
| 4776 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4777 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4778 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4779 | if (left > GAIN_FLOAT_UNITY) { |
| 4780 | left = GAIN_FLOAT_UNITY; |
| 4781 | } |
| 4782 | left *= v; |
| 4783 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4784 | if (right > GAIN_FLOAT_UNITY) { |
| 4785 | right = GAIN_FLOAT_UNITY; |
| 4786 | } |
| 4787 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | if (lastTrack) { |
| 4791 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4792 | mLeftVolFloat = left; |
| 4793 | mRightVolFloat = right; |
| 4794 | |
| 4795 | // Convert volumes from float to 8.24 |
| 4796 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4797 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4798 | |
| 4799 | // Delegate volume control to effect in track effect chain if needed |
| 4800 | // only one effect chain can be present on DirectOutputThread, so if |
| 4801 | // there is one, the track is connected to it |
| 4802 | if (!mEffectChains.isEmpty()) { |
| 4803 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4804 | left = (float)vl / (1 << 24); |
| 4805 | right = (float)vr / (1 << 24); |
| 4806 | } |
| 4807 | if (mOutput->stream->set_volume) { |
| 4808 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4814 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 4815 | { |
| 4816 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4817 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4818 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4819 | if (previousTrack != 0 && latestTrack != 0) { |
| 4820 | if (mType == DIRECT) { |
| 4821 | if (previousTrack.get() != latestTrack.get()) { |
| 4822 | mFlushPending = true; |
| 4823 | } |
| 4824 | } else /* mType == OFFLOAD */ { |
| 4825 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 4826 | mFlushPending = true; |
| 4827 | } |
| 4828 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4829 | } |
| 4830 | PlaybackThread::onAddNewTrack_l(); |
| 4831 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4832 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4833 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4834 | Vector< sp<Track> > *tracksToRemove |
| 4835 | ) |
| 4836 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4837 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4838 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4839 | bool doHwPause = false; |
| 4840 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4841 | |
| 4842 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4843 | for (size_t i = 0; i < count; i++) { |
| 4844 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4845 | // The track died recently |
| 4846 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4847 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4848 | } |
| 4849 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4850 | if (t->isInvalid()) { |
| 4851 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4852 | tracksToRemove->add(t); |
| 4853 | continue; |
| 4854 | } |
| 4855 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4856 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4857 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4858 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4859 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4860 | // Only consider last track started for volume and mixer state control. |
| 4861 | // In theory an older track could underrun and restart after the new one starts |
| 4862 | // but as we only care about the transition phase between two tracks on a |
| 4863 | // direct output, it is not a problem to ignore the underrun case. |
| 4864 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4865 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4866 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4867 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4868 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4869 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4870 | doHwPause = true; |
| 4871 | mHwPaused = true; |
| 4872 | } |
| 4873 | tracksToRemove->add(track); |
| 4874 | } else if (track->isFlushPending()) { |
| 4875 | track->flushAck(); |
| 4876 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4877 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4878 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4879 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4880 | track->resumeAck(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4881 | if (last && mHwPaused) { |
| 4882 | doHwResume = true; |
| 4883 | mHwPaused = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4884 | } |
| 4885 | } |
| 4886 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4887 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4888 | // for all its buffers to be filled before processing it. |
| 4889 | // Allow draining the buffer in case the client |
| 4890 | // app does not call stop() and relies on underrun to stop: |
| 4891 | // hence the test on (track->mRetryCount > 1). |
| 4892 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4893 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4894 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4895 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4896 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4897 | minFrames = mNormalFrameCount; |
| 4898 | } else { |
| 4899 | minFrames = 1; |
| 4900 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4901 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4902 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4903 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4904 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4905 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4906 | |
| 4907 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4908 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4909 | // make sure processVolume_l() will apply new volume even if 0 |
| 4910 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4911 | if (!mHwSupportsPause) { |
| 4912 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4917 | processVolume_l(track, last); |
| 4918 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4919 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4920 | if (previousTrack != 0) { |
| 4921 | if (track != previousTrack.get()) { |
| 4922 | // Flush any data still being written from last track |
| 4923 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4924 | // Invalidate previous track to force a seek when resuming. |
| 4925 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | mPreviousTrack = track; |
| 4929 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4930 | // reset retry count |
| 4931 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4932 | mActiveTrack = t; |
| 4933 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4934 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4935 | doHwResume = true; |
| 4936 | mHwPaused = false; |
| 4937 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4938 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4939 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4940 | // clear effect chain input buffer if the last active track started underruns |
| 4941 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4942 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4943 | mEffectChains[0]->clearInputBuffer(); |
| 4944 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4945 | if (track->isStopping_1()) { |
| 4946 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4947 | if (last && mHwPaused) { |
| 4948 | doHwResume = true; |
| 4949 | mHwPaused = false; |
| 4950 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4951 | } |
| 4952 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4953 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4954 | // We have consumed all the buffers of this track. |
| 4955 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4956 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4957 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4958 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4959 | } else { |
| 4960 | audioHALFrames = 0; |
| 4961 | } |
| 4962 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4963 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4964 | if (mStandby || !last || |
| 4965 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4966 | if (track->isStopping_2()) { |
| 4967 | track->mState = TrackBase::STOPPED; |
| 4968 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4969 | if (track->isStopped()) { |
| 4970 | track->reset(); |
| 4971 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4972 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4973 | } |
| 4974 | } else { |
| 4975 | // No buffers for this track. Give it a few chances to |
| 4976 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4977 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4978 | if (--(track->mRetryCount) <= 0) { |
| 4979 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4980 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4981 | // indicate to client process that the track was disabled because of underrun; |
| 4982 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4983 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4984 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4985 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 4986 | "minFrames = %u, mFormat = %#x", |
| 4987 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4988 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4989 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4990 | doHwPause = true; |
| 4991 | mHwPaused = true; |
| 4992 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4993 | } |
| 4994 | } |
| 4995 | } |
| 4996 | } |
| 4997 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4998 | // 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] | 4999 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5000 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5001 | if (mTracks[i]->isFlushPending()) { |
| 5002 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5003 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5004 | } |
| 5005 | } |
| 5006 | } |
| 5007 | |
| 5008 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5009 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5010 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5011 | // if resume is received before pause is executed. |
| 5012 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5013 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5014 | mOutput->stream->pause(mOutput->stream); |
| 5015 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5016 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5017 | flushHw_l(); |
| 5018 | } |
| 5019 | if (mHwSupportsPause && !mStandby && doHwResume) { |
| 5020 | mOutput->stream->resume(mOutput->stream); |
| 5021 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5022 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5023 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5024 | |
| 5025 | return mixerStatus; |
| 5026 | } |
| 5027 | |
| 5028 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 5029 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5030 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5031 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5032 | // output audio to hardware |
| 5033 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5034 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5035 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5036 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 5037 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5038 | // no need to pad with 0 for compressed audio |
| 5039 | if (audio_has_proportional_frames(mFormat)) { |
| 5040 | memset(curBuf, 0, frameCount * mFrameSize); |
| 5041 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5042 | break; |
| 5043 | } |
| 5044 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 5045 | frameCount -= buffer.frameCount; |
| 5046 | curBuf += buffer.frameCount * mFrameSize; |
| 5047 | mActiveTrack->releaseBuffer(&buffer); |
| 5048 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5049 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5050 | mSleepTimeUs = 0; |
| 5051 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5052 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 5056 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5057 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5058 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5059 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5060 | return; |
| 5061 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5062 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5063 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5064 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5065 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5066 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5067 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5068 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5069 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5070 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5071 | } |
| 5072 | } |
| 5073 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5074 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 5075 | { |
| 5076 | { |
| 5077 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5078 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5079 | if (mTracks[i]->isFlushPending()) { |
| 5080 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5081 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5082 | } |
| 5083 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5084 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5085 | flushHw_l(); |
| 5086 | } |
| 5087 | } |
| 5088 | PlaybackThread::threadLoop_exit(); |
| 5089 | } |
| 5090 | |
| 5091 | // must be called with thread mutex locked |
| 5092 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 5093 | { |
| 5094 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5095 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5096 | |
vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5097 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { |
| 5098 | return !mStandby; |
| 5099 | } |
| 5100 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5101 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 5102 | // after a timeout and we will enter standby then. |
| 5103 | if (mTracks.size() > 0) { |
| 5104 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5105 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 5106 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5109 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5110 | } |
| 5111 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5112 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5113 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 5114 | audio_format_t format __unused, audio_session_t sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5115 | { |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
| 5119 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5120 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5121 | { |
| 5122 | } |
| 5123 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5124 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 5125 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5126 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5127 | { |
| 5128 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5129 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5130 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5131 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5132 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5133 | AudioParameter param = AudioParameter(keyValuePair); |
| 5134 | int value; |
| 5135 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5136 | // forward device change to effects that have requested to be |
| 5137 | // aware of attached audio device. |
| 5138 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5139 | a2dpDeviceChanged = |
| 5140 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5141 | mOutDevice = value; |
| 5142 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5143 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5144 | } |
| 5145 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5146 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5147 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5148 | // do not accept frame count changes if tracks are open as the track buffer |
| 5149 | // size depends on frame count and correct behavior would not be garantied |
| 5150 | // if frame count is changed after track creation |
| 5151 | if (!mTracks.isEmpty()) { |
| 5152 | status = INVALID_OPERATION; |
| 5153 | } else { |
| 5154 | reconfig = true; |
| 5155 | } |
| 5156 | } |
| 5157 | if (status == NO_ERROR) { |
| 5158 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 5159 | keyValuePair.string()); |
| 5160 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5161 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5162 | mStandby = true; |
| 5163 | mBytesWritten = 0; |
| 5164 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 5165 | keyValuePair.string()); |
| 5166 | } |
| 5167 | if (status == NO_ERROR && reconfig) { |
| 5168 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5169 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5170 | } |
| 5171 | } |
| 5172 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5173 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5177 | { |
| 5178 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5179 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5180 | time = PlaybackThread::activeSleepTimeUs(); |
| 5181 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5182 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5183 | } |
| 5184 | return time; |
| 5185 | } |
| 5186 | |
| 5187 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5188 | { |
| 5189 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5190 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5191 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5192 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5193 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5194 | } |
| 5195 | return time; |
| 5196 | } |
| 5197 | |
| 5198 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5199 | { |
| 5200 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5201 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5202 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5203 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5204 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5205 | } |
| 5206 | return time; |
| 5207 | } |
| 5208 | |
| 5209 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5210 | { |
| 5211 | PlaybackThread::cacheParameters_l(); |
| 5212 | |
| 5213 | // use shorter standby delay as on normal output to release |
| 5214 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5215 | // no delay on outputs with HW A/V sync |
| 5216 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5217 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5218 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5219 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5220 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5221 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5222 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5223 | } |
| 5224 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5225 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5226 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5227 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5228 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5229 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5230 | } |
| 5231 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5232 | // ---------------------------------------------------------------------------- |
| 5233 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5234 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5235 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5236 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5237 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5238 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5239 | mDrainSequence(0), |
| 5240 | mAsyncError(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5241 | { |
| 5242 | } |
| 5243 | |
| 5244 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5245 | { |
| 5246 | } |
| 5247 | |
| 5248 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5249 | { |
| 5250 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5251 | } |
| 5252 | |
| 5253 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5254 | { |
| 5255 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5256 | uint32_t writeAckSequence; |
| 5257 | uint32_t drainSequence; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5258 | bool asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5259 | |
| 5260 | { |
| 5261 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5262 | while (!((mWriteAckSequence & 1) || |
| 5263 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5264 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5265 | exitPending())) { |
| 5266 | mWaitWorkCV.wait(mLock); |
| 5267 | } |
| 5268 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5269 | if (exitPending()) { |
| 5270 | break; |
| 5271 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5272 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5273 | mWriteAckSequence, mDrainSequence); |
| 5274 | writeAckSequence = mWriteAckSequence; |
| 5275 | mWriteAckSequence &= ~1; |
| 5276 | drainSequence = mDrainSequence; |
| 5277 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5278 | asyncError = mAsyncError; |
| 5279 | mAsyncError = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5280 | } |
| 5281 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5282 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5283 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5284 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5285 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5286 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5287 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5288 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5289 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5290 | if (asyncError) { |
| 5291 | playbackThread->onAsyncError(); |
| 5292 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5293 | } |
| 5294 | } |
| 5295 | } |
| 5296 | return false; |
| 5297 | } |
| 5298 | |
| 5299 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5300 | { |
| 5301 | ALOGV("AsyncCallbackThread::exit"); |
| 5302 | Mutex::Autolock _l(mLock); |
| 5303 | requestExit(); |
| 5304 | mWaitWorkCV.broadcast(); |
| 5305 | } |
| 5306 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5307 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5308 | { |
| 5309 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5310 | // bit 0 is cleared |
| 5311 | mWriteAckSequence = sequence << 1; |
| 5312 | } |
| 5313 | |
| 5314 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5315 | { |
| 5316 | Mutex::Autolock _l(mLock); |
| 5317 | // ignore unexpected callbacks |
| 5318 | if (mWriteAckSequence & 2) { |
| 5319 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5320 | mWaitWorkCV.signal(); |
| 5321 | } |
| 5322 | } |
| 5323 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5324 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5325 | { |
| 5326 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5327 | // bit 0 is cleared |
| 5328 | mDrainSequence = sequence << 1; |
| 5329 | } |
| 5330 | |
| 5331 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5332 | { |
| 5333 | Mutex::Autolock _l(mLock); |
| 5334 | // ignore unexpected callbacks |
| 5335 | if (mDrainSequence & 2) { |
| 5336 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5337 | mWaitWorkCV.signal(); |
| 5338 | } |
| 5339 | } |
| 5340 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5341 | void AudioFlinger::AsyncCallbackThread::setAsyncError() |
| 5342 | { |
| 5343 | Mutex::Autolock _l(mLock); |
| 5344 | mAsyncError = true; |
| 5345 | mWaitWorkCV.signal(); |
| 5346 | } |
| 5347 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5348 | |
| 5349 | // ---------------------------------------------------------------------------- |
| 5350 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5351 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5352 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5353 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5354 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5355 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 5356 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5357 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5358 | } |
| 5359 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5360 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5361 | { |
| 5362 | if (mFlushPending || mHwPaused) { |
| 5363 | // If a flush is pending or track was paused, just discard buffered data |
| 5364 | flushHw_l(); |
| 5365 | } else { |
| 5366 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5367 | threadLoop_drain(); |
| 5368 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5369 | if (mUseAsyncWrite) { |
| 5370 | ALOG_ASSERT(mCallbackThread != 0); |
| 5371 | mCallbackThread->exit(); |
| 5372 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5373 | PlaybackThread::threadLoop_exit(); |
| 5374 | } |
| 5375 | |
| 5376 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5377 | Vector< sp<Track> > *tracksToRemove |
| 5378 | ) |
| 5379 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5380 | size_t count = mActiveTracks.size(); |
| 5381 | |
| 5382 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5383 | bool doHwPause = false; |
| 5384 | bool doHwResume = false; |
| 5385 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5386 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5387 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5388 | // find out which tracks need to be processed |
| 5389 | for (size_t i = 0; i < count; i++) { |
| 5390 | sp<Track> t = mActiveTracks[i].promote(); |
| 5391 | // The track died recently |
| 5392 | if (t == 0) { |
| 5393 | continue; |
| 5394 | } |
| 5395 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5396 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5397 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5398 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5399 | // Only consider last track started for volume and mixer state control. |
| 5400 | // In theory an older track could underrun and restart after the new one starts |
| 5401 | // but as we only care about the transition phase between two tracks on a |
| 5402 | // direct output, it is not a problem to ignore the underrun case. |
| 5403 | sp<Track> l = mLatestActiveTrack.promote(); |
| 5404 | bool last = l.get() == track; |
| 5405 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5406 | if (track->isInvalid()) { |
| 5407 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5408 | tracksToRemove->add(track); |
| 5409 | continue; |
| 5410 | } |
| 5411 | |
| 5412 | if (track->mState == TrackBase::IDLE) { |
| 5413 | ALOGW("An idle track shouldn't be in active list"); |
| 5414 | continue; |
| 5415 | } |
| 5416 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5417 | if (track->isPausing()) { |
| 5418 | track->setPaused(); |
| 5419 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5420 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5421 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5422 | mHwPaused = true; |
| 5423 | } |
| 5424 | // If we were part way through writing the mixbuffer to |
| 5425 | // the HAL we must save this until we resume |
| 5426 | // BUG - this will be wrong if a different track is made active, |
| 5427 | // in that case we want to discard the pending data in the |
| 5428 | // mixbuffer and tell the client to present it again when the |
| 5429 | // track is resumed |
| 5430 | mPausedWriteLength = mCurrentWriteLength; |
| 5431 | mPausedBytesRemaining = mBytesRemaining; |
| 5432 | mBytesRemaining = 0; // stop writing |
| 5433 | } |
| 5434 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5435 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5436 | if (track->isStopping_1()) { |
| 5437 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5438 | } else { |
| 5439 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5440 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5441 | track->flushAck(); |
| 5442 | if (last) { |
| 5443 | mFlushPending = true; |
| 5444 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5445 | } else if (track->isResumePending()){ |
| 5446 | track->resumeAck(); |
| 5447 | if (last) { |
| 5448 | if (mPausedBytesRemaining) { |
| 5449 | // Need to continue write that was interrupted |
| 5450 | mCurrentWriteLength = mPausedWriteLength; |
| 5451 | mBytesRemaining = mPausedBytesRemaining; |
| 5452 | mPausedBytesRemaining = 0; |
| 5453 | } |
| 5454 | if (mHwPaused) { |
| 5455 | doHwResume = true; |
| 5456 | mHwPaused = false; |
| 5457 | // threadLoop_mix() will handle the case that we need to |
| 5458 | // resume an interrupted write |
| 5459 | } |
| 5460 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5461 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5462 | |
| 5463 | // Do not handle new data in this iteration even if track->framesReady() |
| 5464 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5465 | } |
| 5466 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5467 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5468 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5469 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5470 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5471 | // make sure processVolume_l() will apply new volume even if 0 |
| 5472 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5476 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5477 | if (previousTrack != 0) { |
| 5478 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5479 | // Flush any data still being written from last track |
| 5480 | mBytesRemaining = 0; |
| 5481 | if (mPausedBytesRemaining) { |
| 5482 | // Last track was paused so we also need to flush saved |
| 5483 | // mixbuffer state and invalidate track so that it will |
| 5484 | // re-submit that unwritten data when it is next resumed |
| 5485 | mPausedBytesRemaining = 0; |
| 5486 | // Invalidate is a bit drastic - would be more efficient |
| 5487 | // to have a flag to tell client that some of the |
| 5488 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5489 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5490 | } |
| 5491 | // flush data already sent to the DSP if changing audio session as audio |
| 5492 | // comes from a different source. Also invalidate previous track to force a |
| 5493 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5494 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5495 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5496 | } |
| 5497 | } |
| 5498 | } |
| 5499 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5500 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5501 | if (track->isStopping_1()) { |
| 5502 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5503 | } else { |
| 5504 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5505 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5506 | mActiveTrack = t; |
| 5507 | mixerStatus = MIXER_TRACKS_READY; |
| 5508 | } |
| 5509 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5510 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5511 | if (track->isStopping_1()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5512 | if (--(track->mRetryCount) <= 0) { |
| 5513 | // Hardware buffer can hold a large amount of audio so we must |
| 5514 | // wait for all current track's data to drain before we say |
| 5515 | // that the track is stopped. |
| 5516 | if (mBytesRemaining == 0) { |
| 5517 | // Only start draining when all data in mixbuffer |
| 5518 | // has been written |
| 5519 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5520 | track->mState = TrackBase::STOPPING_2; // so presentation completes after |
| 5521 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 5522 | if (last && !mStandby) { |
| 5523 | // do not modify drain sequence if we are already draining. This happens |
| 5524 | // when resuming from pause after drain. |
| 5525 | if ((mDrainSequence & 1) == 0) { |
| 5526 | mSleepTimeUs = 0; |
| 5527 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 5528 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5529 | mDrainSequence += 2; |
| 5530 | } |
| 5531 | if (mHwPaused) { |
| 5532 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5533 | // a resume so we must ensure hardware is running |
| 5534 | doHwResume = true; |
| 5535 | mHwPaused = false; |
| 5536 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5537 | } |
| 5538 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5539 | } else if (last) { |
| 5540 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); |
| 5541 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5542 | } |
| 5543 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5544 | // Drain has completed or we are in standby, signal presentation complete |
| 5545 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5546 | track->mState = TrackBase::STOPPED; |
| 5547 | size_t audioHALFrames = |
| 5548 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5549 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5550 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5551 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5552 | track->reset(); |
| 5553 | tracksToRemove->add(track); |
| 5554 | } |
| 5555 | } else { |
| 5556 | // No buffers for this track. Give it a few chances to |
| 5557 | // fill a buffer, then remove it from active list. |
| 5558 | if (--(track->mRetryCount) <= 0) { |
| 5559 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5560 | track->name()); |
| 5561 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5562 | // indicate to client process that the track was disabled because of underrun; |
| 5563 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5564 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5565 | } else if (last){ |
| 5566 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | // compute volume for this track |
| 5571 | processVolume_l(track, last); |
| 5572 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5573 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5574 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5575 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5576 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5577 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5578 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5579 | mOutput->stream->pause(mOutput->stream); |
| 5580 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5581 | if (mFlushPending) { |
| 5582 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5583 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5584 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5585 | mOutput->stream->resume(mOutput->stream); |
| 5586 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5587 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5588 | // remove all the tracks that need to be... |
| 5589 | removeTracks_l(*tracksToRemove); |
| 5590 | |
| 5591 | return mixerStatus; |
| 5592 | } |
| 5593 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5594 | // must be called with thread mutex locked |
| 5595 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5596 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5597 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5598 | mWriteAckSequence, mDrainSequence); |
| 5599 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5600 | return true; |
| 5601 | } |
| 5602 | return false; |
| 5603 | } |
| 5604 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5605 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5606 | { |
| 5607 | Mutex::Autolock _l(mLock); |
| 5608 | return waitingAsyncCallback_l(); |
| 5609 | } |
| 5610 | |
| 5611 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5612 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5613 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5614 | // Flush anything still waiting in the mixbuffer |
| 5615 | mCurrentWriteLength = 0; |
| 5616 | mBytesRemaining = 0; |
| 5617 | mPausedWriteLength = 0; |
| 5618 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5619 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 5620 | mBytesWritten = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5621 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5622 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5623 | // discard any pending drain or write ack by incrementing sequence |
| 5624 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5625 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5626 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5627 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5628 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5632 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
| 5633 | { |
| 5634 | Mutex::Autolock _l(mLock); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5635 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 5636 | mFlushPending = true; |
| 5637 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5638 | } |
| 5639 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5640 | // ---------------------------------------------------------------------------- |
| 5641 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5642 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5643 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5644 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5645 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5646 | mWaitTimeMs(UINT_MAX) |
| 5647 | { |
| 5648 | addOutputTrack(mainThread); |
| 5649 | } |
| 5650 | |
| 5651 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5652 | { |
| 5653 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5654 | mOutputTracks[i]->destroy(); |
| 5655 | } |
| 5656 | } |
| 5657 | |
| 5658 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5659 | { |
| 5660 | // mix buffers... |
| 5661 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5662 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5663 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5664 | if (mMixerBufferValid) { |
| 5665 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5666 | } else { |
| 5667 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5668 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5669 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5670 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5671 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5672 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5673 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5677 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5678 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5679 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5680 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5681 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5682 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5683 | } |
| 5684 | } else if (mBytesWritten != 0) { |
| 5685 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5686 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5687 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5688 | } else { |
| 5689 | // flush remaining overflow buffers in output tracks |
| 5690 | writeFrames = 0; |
| 5691 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5692 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5693 | } |
| 5694 | } |
| 5695 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5696 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5697 | { |
| 5698 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5699 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5700 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5701 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5702 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5706 | { |
| 5707 | // DuplicatingThread implements standby by stopping all tracks |
| 5708 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5709 | outputTracks[i]->stop(); |
| 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5714 | { |
| 5715 | outputTracks = mOutputTracks; |
| 5716 | } |
| 5717 | |
| 5718 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5719 | { |
| 5720 | outputTracks.clear(); |
| 5721 | } |
| 5722 | |
| 5723 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5724 | { |
| 5725 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5726 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5727 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5728 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5729 | const size_t frameCount = |
| 5730 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5731 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5732 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5733 | // nearly empty and the other may be dropping data). |
| 5734 | |
| 5735 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5736 | this, |
| 5737 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5738 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5739 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5740 | frameCount, |
| 5741 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5742 | if (outputTrack->cblk() != NULL) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5743 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5744 | mOutputTracks.add(outputTrack); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5745 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5746 | updateWaitTime_l(); |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5751 | { |
| 5752 | Mutex::Autolock _l(mLock); |
| 5753 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5754 | if (mOutputTracks[i]->thread() == thread) { |
| 5755 | mOutputTracks[i]->destroy(); |
| 5756 | mOutputTracks.removeAt(i); |
| 5757 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5758 | if (thread->getOutput() == mOutput) { |
| 5759 | mOutput = NULL; |
| 5760 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5761 | return; |
| 5762 | } |
| 5763 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5764 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | // caller must hold mLock |
| 5768 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5769 | { |
| 5770 | mWaitTimeMs = UINT_MAX; |
| 5771 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5772 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5773 | if (strong != 0) { |
| 5774 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5775 | if (waitTimeMs < mWaitTimeMs) { |
| 5776 | mWaitTimeMs = waitTimeMs; |
| 5777 | } |
| 5778 | } |
| 5779 | } |
| 5780 | } |
| 5781 | |
| 5782 | |
| 5783 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5784 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5785 | { |
| 5786 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5787 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5788 | if (thread == 0) { |
| 5789 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5790 | outputTracks[i].get()); |
| 5791 | return false; |
| 5792 | } |
| 5793 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5794 | // see note at standby() declaration |
| 5795 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5796 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5797 | thread.get()); |
| 5798 | return false; |
| 5799 | } |
| 5800 | } |
| 5801 | return true; |
| 5802 | } |
| 5803 | |
| 5804 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5805 | { |
| 5806 | return (mWaitTimeMs * 1000) / 2; |
| 5807 | } |
| 5808 | |
| 5809 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5810 | { |
| 5811 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5812 | updateWaitTime_l(); |
| 5813 | |
| 5814 | MixerThread::cacheParameters_l(); |
| 5815 | } |
| 5816 | |
| 5817 | // ---------------------------------------------------------------------------- |
| 5818 | // Record |
| 5819 | // ---------------------------------------------------------------------------- |
| 5820 | |
| 5821 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5822 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5823 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5824 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5825 | audio_devices_t inDevice, |
| 5826 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5827 | #ifdef TEE_SINK |
| 5828 | , const sp<NBAIO_Sink>& teeSink |
| 5829 | #endif |
| 5830 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5831 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5832 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5833 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5834 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5835 | #ifdef TEE_SINK |
| 5836 | , mTeeSink(teeSink) |
| 5837 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5838 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5839 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5840 | // mFastCapture below |
| 5841 | , mFastCaptureFutex(0) |
| 5842 | // mInputSource |
| 5843 | // mPipeSink |
| 5844 | // mPipeSource |
| 5845 | , mPipeFramesP2(0) |
| 5846 | // mPipeMemory |
| 5847 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5848 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5849 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5850 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5851 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5852 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5853 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5854 | |
| 5855 | // create an NBAIO source for the HAL input stream, and negotiate |
| 5856 | mInputSource = new AudioStreamInSource(input->stream); |
| 5857 | size_t numCounterOffers = 0; |
| 5858 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5859 | #if !LOG_NDEBUG |
| 5860 | ssize_t index = |
| 5861 | #else |
| 5862 | (void) |
| 5863 | #endif |
| 5864 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5865 | ALOG_ASSERT(index == 0); |
| 5866 | |
| 5867 | // initialize fast capture depending on configuration |
| 5868 | bool initFastCapture; |
| 5869 | switch (kUseFastCapture) { |
| 5870 | case FastCapture_Never: |
| 5871 | initFastCapture = false; |
| 5872 | break; |
| 5873 | case FastCapture_Always: |
| 5874 | initFastCapture = true; |
| 5875 | break; |
| 5876 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5877 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5878 | break; |
| 5879 | // case FastCapture_Dynamic: |
| 5880 | } |
| 5881 | |
| 5882 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5883 | // 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] | 5884 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5885 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5886 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 5887 | void *pipeBuffer; |
| 5888 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 5889 | sp<IMemory> pipeMemory; |
| 5890 | if ((roHeap == 0) || |
| 5891 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 5892 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 5893 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 5894 | goto failed; |
| 5895 | } |
| 5896 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 5897 | memset(pipeBuffer, 0, pipeSize); |
| 5898 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 5899 | const NBAIO_Format offers[1] = {format}; |
| 5900 | size_t numCounterOffers = 0; |
| 5901 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 5902 | ALOG_ASSERT(index == 0); |
| 5903 | mPipeSink = pipe; |
| 5904 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 5905 | numCounterOffers = 0; |
| 5906 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 5907 | ALOG_ASSERT(index == 0); |
| 5908 | mPipeSource = pipeReader; |
| 5909 | mPipeFramesP2 = pipeFramesP2; |
| 5910 | mPipeMemory = pipeMemory; |
| 5911 | |
| 5912 | // create fast capture |
| 5913 | mFastCapture = new FastCapture(); |
| 5914 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5915 | #ifdef STATE_QUEUE_DUMP |
| 5916 | // FIXME |
| 5917 | #endif |
| 5918 | FastCaptureState *state = sq->begin(); |
| 5919 | state->mCblk = NULL; |
| 5920 | state->mInputSource = mInputSource.get(); |
| 5921 | state->mInputSourceGen++; |
| 5922 | state->mPipeSink = pipe; |
| 5923 | state->mPipeSinkGen++; |
| 5924 | state->mFrameCount = mFrameCount; |
| 5925 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5926 | // already done in constructor initialization list |
| 5927 | //mFastCaptureFutex = 0; |
| 5928 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5929 | state->mColdGen++; |
| 5930 | state->mDumpState = &mFastCaptureDumpState; |
| 5931 | #ifdef TEE_SINK |
| 5932 | // FIXME |
| 5933 | #endif |
| 5934 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 5935 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 5936 | sq->end(); |
| 5937 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5938 | |
| 5939 | // start the fast capture |
| 5940 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5941 | pid_t tid = mFastCapture->getTid(); |
Glenn Kasten | 8379b72 | 2016-03-18 14:54:17 -0700 | [diff] [blame] | 5942 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5943 | #ifdef AUDIO_WATCHDOG |
| 5944 | // FIXME |
| 5945 | #endif |
| 5946 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5947 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5948 | } |
| 5949 | failed: ; |
| 5950 | |
| 5951 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5952 | } |
| 5953 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5954 | AudioFlinger::RecordThread::~RecordThread() |
| 5955 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5956 | if (mFastCapture != 0) { |
| 5957 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5958 | FastCaptureState *state = sq->begin(); |
| 5959 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5960 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5961 | if (old == -1) { |
| 5962 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5963 | } |
| 5964 | } |
| 5965 | state->mCommand = FastCaptureState::EXIT; |
| 5966 | sq->end(); |
| 5967 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5968 | mFastCapture->join(); |
| 5969 | mFastCapture.clear(); |
| 5970 | } |
| 5971 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5972 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5973 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | void AudioFlinger::RecordThread::onFirstRef() |
| 5977 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5978 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5979 | } |
| 5980 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5981 | bool AudioFlinger::RecordThread::threadLoop() |
| 5982 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5983 | nsecs_t lastWarning = 0; |
| 5984 | |
| 5985 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5986 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5987 | reacquire_wakelock: |
| 5988 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5989 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5990 | { |
| 5991 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5992 | size_t size = mActiveTracks.size(); |
| 5993 | activeTracksGen = mActiveTracksGen; |
| 5994 | if (size > 0) { |
| 5995 | // FIXME an arbitrary choice |
| 5996 | activeTrack = mActiveTracks[0]; |
| 5997 | acquireWakeLock_l(activeTrack->uid()); |
| 5998 | if (size > 1) { |
| 5999 | SortedVector<int> tmp; |
| 6000 | for (size_t i = 0; i < size; i++) { |
| 6001 | tmp.add(mActiveTracks[i]->uid()); |
| 6002 | } |
| 6003 | updateWakeLockUids_l(tmp); |
| 6004 | } |
| 6005 | } else { |
| 6006 | acquireWakeLock_l(-1); |
| 6007 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6008 | } |
| 6009 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6010 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 6011 | uint32_t sleepUs = 0; |
| 6012 | |
| 6013 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6014 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6015 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6016 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6017 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 6018 | Vector< sp<RecordTrack> > activeTracks; |
| 6019 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6020 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6021 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6022 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6023 | // reference to a fast track which is about to be removed |
| 6024 | sp<RecordTrack> fastTrackToRemove; |
| 6025 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6026 | { // scope for mLock |
| 6027 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6028 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6029 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6030 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6031 | // check exitPending here because checkForNewParameters_l() and |
| 6032 | // checkForNewParameters_l() can temporarily release mLock |
| 6033 | if (exitPending()) { |
| 6034 | break; |
| 6035 | } |
| 6036 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6037 | // sleep with mutex unlocked |
| 6038 | if (sleepUs > 0) { |
| 6039 | ATRACE_BEGIN("sleep"); |
| 6040 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); |
| 6041 | ATRACE_END(); |
| 6042 | sleepUs = 0; |
| 6043 | continue; |
| 6044 | } |
| 6045 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6046 | // if no active track(s), then standby and release wakelock |
| 6047 | size_t size = mActiveTracks.size(); |
| 6048 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6049 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6050 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6051 | releaseWakeLock_l(); |
| 6052 | ALOGV("RecordThread: loop stopping"); |
| 6053 | // go to sleep |
| 6054 | mWaitWorkCV.wait(mLock); |
| 6055 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6056 | goto reacquire_wakelock; |
| 6057 | } |
| 6058 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6059 | if (mActiveTracksGen != activeTracksGen) { |
| 6060 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6061 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6062 | for (size_t i = 0; i < size; i++) { |
| 6063 | tmp.add(mActiveTracks[i]->uid()); |
| 6064 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6065 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6066 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6067 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6068 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6069 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6070 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6071 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6072 | activeTrack = mActiveTracks[i]; |
| 6073 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6074 | if (activeTrack->isFastTrack()) { |
| 6075 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 6076 | fastTrackToRemove = activeTrack; |
| 6077 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6078 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6079 | mActiveTracks.remove(activeTrack); |
| 6080 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6081 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6082 | continue; |
| 6083 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6084 | |
| 6085 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 6086 | switch (activeTrackState) { |
| 6087 | |
| 6088 | case TrackBase::PAUSING: |
| 6089 | mActiveTracks.remove(activeTrack); |
| 6090 | mActiveTracksGen++; |
| 6091 | doBroadcast = true; |
| 6092 | size--; |
| 6093 | continue; |
| 6094 | |
| 6095 | case TrackBase::STARTING_1: |
| 6096 | sleepUs = 10000; |
| 6097 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6098 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6099 | continue; |
| 6100 | |
| 6101 | case TrackBase::STARTING_2: |
| 6102 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6103 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6104 | activeTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6105 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6106 | break; |
| 6107 | |
| 6108 | case TrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6109 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6110 | break; |
| 6111 | |
| 6112 | case TrackBase::IDLE: |
| 6113 | i++; |
| 6114 | continue; |
| 6115 | |
| 6116 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6117 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6118 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6119 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6120 | activeTracks.add(activeTrack); |
| 6121 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6122 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6123 | if (activeTrack->isFastTrack()) { |
| 6124 | ALOG_ASSERT(!mFastTrackAvail); |
| 6125 | ALOG_ASSERT(fastTrack == 0); |
| 6126 | fastTrack = activeTrack; |
| 6127 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6128 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6129 | |
| 6130 | if (allStopped) { |
| 6131 | standbyIfNotAlreadyInStandby(); |
| 6132 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6133 | if (doBroadcast) { |
| 6134 | mStartStopCond.broadcast(); |
| 6135 | } |
| 6136 | |
| 6137 | // sleep if there are no active tracks to process |
| 6138 | if (activeTracks.size() == 0) { |
| 6139 | if (sleepUs == 0) { |
| 6140 | sleepUs = kRecordThreadSleepUs; |
| 6141 | } |
| 6142 | continue; |
| 6143 | } |
| 6144 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6145 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6146 | lockEffectChains_l(effectChains); |
| 6147 | } |
| 6148 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6149 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6150 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6151 | size_t size = effectChains.size(); |
| 6152 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6153 | // thread mutex is not locked, but effect chain is locked |
| 6154 | effectChains[i]->process_l(); |
| 6155 | } |
| 6156 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6157 | // 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] | 6158 | if (mFastCapture != 0) { |
| 6159 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6160 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6161 | bool didModify = false; |
| 6162 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6163 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 6164 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 6165 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6166 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6167 | if (old == -1) { |
| 6168 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6169 | } |
| 6170 | } |
| 6171 | state->mCommand = FastCaptureState::READ_WRITE; |
| 6172 | #if 0 // FIXME |
| 6173 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6174 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 6175 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6176 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6177 | didModify = true; |
| 6178 | } |
| 6179 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 6180 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 6181 | if (cblkNew != cblkOld) { |
| 6182 | state->mCblk = cblkNew; |
| 6183 | // block until acked if removing a fast track |
| 6184 | if (cblkOld != NULL) { |
| 6185 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 6186 | } |
| 6187 | didModify = true; |
| 6188 | } |
| 6189 | sq->end(didModify); |
| 6190 | if (didModify) { |
| 6191 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6192 | #if 0 |
| 6193 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6194 | mNormalSource = mPipeSource; |
| 6195 | } |
| 6196 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6197 | } |
| 6198 | } |
| 6199 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6200 | // now run the fast track destructor with thread mutex unlocked |
| 6201 | fastTrackToRemove.clear(); |
| 6202 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6203 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 6204 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 6205 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 6206 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 6207 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6208 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6209 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6210 | ssize_t framesRead; |
| 6211 | |
| 6212 | // If an NBAIO source is present, use it to read the normal capture's data |
| 6213 | if (mPipeSource != 0) { |
| 6214 | size_t framesToRead = mBufferSize / mFrameSize; |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6215 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6216 | framesToRead); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6217 | if (framesRead == 0) { |
| 6218 | // since pipe is non-blocking, simulate blocking input |
| 6219 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 6220 | } |
| 6221 | // otherwise use the HAL / AudioStreamIn directly |
| 6222 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6223 | ATRACE_BEGIN("read"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6224 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6225 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6226 | ATRACE_END(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6227 | if (bytesRead < 0) { |
| 6228 | framesRead = bytesRead; |
| 6229 | } else { |
| 6230 | framesRead = bytesRead / mFrameSize; |
| 6231 | } |
| 6232 | } |
| 6233 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6234 | // Update server timestamp with server stats |
| 6235 | // systemTime() is optional if the hardware supports timestamps. |
| 6236 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6237 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6238 | |
| 6239 | // Update server timestamp with kernel stats |
Andy Hung | 69ce44d | 2016-07-18 12:14:25 -0700 | [diff] [blame^] | 6240 | if (mInput->stream->get_capture_position != nullptr |
| 6241 | && mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6242 | int64_t position, time; |
| 6243 | int ret = mInput->stream->get_capture_position(mInput->stream, &position, &time); |
| 6244 | if (ret == NO_ERROR) { |
| 6245 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6246 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6247 | // Note: In general record buffers should tend to be empty in |
| 6248 | // a properly running pipeline. |
| 6249 | // |
| 6250 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6251 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6252 | } |
| 6253 | } |
| 6254 | // Use this to track timestamp information |
| 6255 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6256 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6257 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6258 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6259 | // Force input into standby so that it tries to recover at next read attempt |
| 6260 | inputStandBy(); |
| 6261 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6262 | } |
| 6263 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6264 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6265 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6266 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6267 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6268 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6269 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6270 | } |
| 6271 | // 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] | 6272 | { |
| 6273 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6274 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6275 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6276 | (framesRead - part1) * mFrameSize); |
| 6277 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6278 | } |
| 6279 | rear = mRsmpInRear += framesRead; |
| 6280 | |
| 6281 | size = activeTracks.size(); |
| 6282 | // loop over each active track |
| 6283 | for (size_t i = 0; i < size; i++) { |
| 6284 | activeTrack = activeTracks[i]; |
| 6285 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6286 | // skip fast tracks, as those are handled directly by FastCapture |
| 6287 | if (activeTrack->isFastTrack()) { |
| 6288 | continue; |
| 6289 | } |
| 6290 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6291 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6292 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6293 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6294 | enum { |
| 6295 | OVERRUN_UNKNOWN, |
| 6296 | OVERRUN_TRUE, |
| 6297 | OVERRUN_FALSE |
| 6298 | } overrun = OVERRUN_UNKNOWN; |
| 6299 | |
| 6300 | // loop over getNextBuffer to handle circular sink |
| 6301 | for (;;) { |
| 6302 | |
| 6303 | activeTrack->mSink.frameCount = ~0; |
| 6304 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6305 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6306 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6307 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6308 | // check available frames and handle overrun conditions |
| 6309 | // if the record track isn't draining fast enough. |
| 6310 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6311 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6312 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6313 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6314 | overrun = OVERRUN_TRUE; |
| 6315 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6316 | if (framesOut == 0 || framesIn == 0) { |
| 6317 | break; |
| 6318 | } |
| 6319 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6320 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6321 | // from framesIn. |
| 6322 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6323 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6324 | framesOut = min(framesOut, |
| 6325 | destinationFramesPossible( |
| 6326 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6327 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6328 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6329 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6330 | |
| 6331 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6332 | overrun = OVERRUN_FALSE; |
| 6333 | } |
| 6334 | |
| 6335 | if (activeTrack->mFramesToDrop == 0) { |
| 6336 | if (framesOut > 0) { |
| 6337 | activeTrack->mSink.frameCount = framesOut; |
| 6338 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6339 | } |
| 6340 | } else { |
| 6341 | // FIXME could do a partial drop of framesOut |
| 6342 | if (activeTrack->mFramesToDrop > 0) { |
| 6343 | activeTrack->mFramesToDrop -= framesOut; |
| 6344 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6345 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6346 | } |
| 6347 | } else { |
| 6348 | activeTrack->mFramesToDrop += framesOut; |
| 6349 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6350 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6351 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6352 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6353 | activeTrack->sessionId(), |
| 6354 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6355 | activeTrack->mSyncStartEvent->triggerSession() : |
| 6356 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6357 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6358 | } |
| 6359 | } |
| 6360 | } |
| 6361 | |
| 6362 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6363 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6364 | } |
| 6365 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6366 | |
| 6367 | switch (overrun) { |
| 6368 | case OVERRUN_TRUE: |
| 6369 | // client isn't retrieving buffers fast enough |
| 6370 | if (!activeTrack->setOverflow()) { |
| 6371 | nsecs_t now = systemTime(); |
| 6372 | // FIXME should lastWarning per track? |
| 6373 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6374 | ALOGW("RecordThread: buffer overflow"); |
| 6375 | lastWarning = now; |
| 6376 | } |
| 6377 | } |
| 6378 | break; |
| 6379 | case OVERRUN_FALSE: |
| 6380 | activeTrack->clearOverflow(); |
| 6381 | break; |
| 6382 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6383 | break; |
| 6384 | } |
| 6385 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6386 | // update frame information and push timestamp out |
| 6387 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6388 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6389 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6390 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6391 | } |
| 6392 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6393 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6394 | // enable changes in effect chain |
| 6395 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6396 | // 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] | 6397 | } |
| 6398 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6399 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6400 | |
| 6401 | { |
| 6402 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6403 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6404 | sp<RecordTrack> track = mTracks[i]; |
| 6405 | track->invalidate(); |
| 6406 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6407 | mActiveTracks.clear(); |
| 6408 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6409 | mStartStopCond.broadcast(); |
| 6410 | } |
| 6411 | |
| 6412 | releaseWakeLock(); |
| 6413 | |
| 6414 | ALOGV("RecordThread %p exiting", this); |
| 6415 | return false; |
| 6416 | } |
| 6417 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6418 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6419 | { |
| 6420 | if (!mStandby) { |
| 6421 | inputStandBy(); |
| 6422 | mStandby = true; |
| 6423 | } |
| 6424 | } |
| 6425 | |
| 6426 | void AudioFlinger::RecordThread::inputStandBy() |
| 6427 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6428 | // Idle the fast capture if it's currently running |
| 6429 | if (mFastCapture != 0) { |
| 6430 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6431 | FastCaptureState *state = sq->begin(); |
| 6432 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6433 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6434 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6435 | state->mColdGen++; |
| 6436 | mFastCaptureFutex = 0; |
| 6437 | sq->end(); |
| 6438 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6439 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6440 | #if 0 |
| 6441 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6442 | // FIXME |
| 6443 | } |
| 6444 | #endif |
| 6445 | #ifdef AUDIO_WATCHDOG |
| 6446 | // FIXME |
| 6447 | #endif |
| 6448 | } else { |
| 6449 | sq->end(false /*didModify*/); |
| 6450 | } |
| 6451 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6452 | mInput->stream->common.standby(&mInput->stream->common); |
| 6453 | } |
| 6454 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6455 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6456 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6457 | const sp<AudioFlinger::Client>& client, |
| 6458 | uint32_t sampleRate, |
| 6459 | audio_format_t format, |
| 6460 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6461 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6462 | audio_session_t sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6463 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6464 | int uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6465 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6466 | pid_t tid, |
| 6467 | status_t *status) |
| 6468 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6469 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6470 | sp<RecordTrack> track; |
| 6471 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6472 | audio_input_flags_t inputFlags = mInput->flags; |
| 6473 | |
| 6474 | // special case for FAST flag considered OK if fast capture is present |
| 6475 | if (hasFastCapture()) { |
| 6476 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 6477 | } |
| 6478 | |
| 6479 | // Check if requested flags are compatible with output stream flags |
| 6480 | if ((*flags & inputFlags) != *flags) { |
| 6481 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 6482 | " input flags (%08x)", |
| 6483 | *flags, inputFlags); |
| 6484 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 6485 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6486 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6487 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6488 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6489 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6490 | // we formerly checked for a callback handler (non-0 tid), |
| 6491 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6492 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6493 | // frame count is not specified, or is exactly the pipe depth |
| 6494 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6495 | // PCM data |
| 6496 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6497 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6498 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6499 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6500 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6501 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6502 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6503 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6504 | hasFastCapture() && |
| 6505 | // there are sufficient fast track slots available |
| 6506 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6507 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6508 | // check compatibility with audio effects. |
| 6509 | Mutex::Autolock _l(mLock); |
| 6510 | // Do not accept FAST flag if the session has software effects |
| 6511 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 6512 | if (chain != 0) { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6513 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_RAW) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6514 | "AUDIO_INPUT_FLAG_RAW denied: effect present on session"); |
| 6515 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW); |
| 6516 | if (chain->hasSoftwareEffect()) { |
| 6517 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: software effect present on session"); |
| 6518 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
| 6519 | } |
| 6520 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6521 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6522 | "AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 6523 | frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6524 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6525 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6526 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6527 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6528 | frameCount, mFrameCount, mPipeFramesP2, |
| 6529 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 6530 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6531 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6532 | } |
| 6533 | } |
| 6534 | |
| 6535 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6536 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6537 | // fast track: frame count is exactly the pipe depth |
| 6538 | frameCount = mPipeFramesP2; |
| 6539 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 6540 | *notificationFrames = mFrameCount; |
| 6541 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6542 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6543 | // or 20 ms if there is a fast capture |
| 6544 | // TODO This could be a roundupRatio inline, and const |
| 6545 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6546 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6547 | // minimum number of notification periods is at least kMinNotifications, |
| 6548 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6549 | static const size_t kMinNotifications = 3; |
| 6550 | static const uint32_t kMinMs = 30; |
| 6551 | // TODO This could be a roundupRatio inline |
| 6552 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6553 | // TODO This could be a roundupRatio inline |
| 6554 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6555 | maxNotificationFrames; |
| 6556 | const size_t minFrameCount = maxNotificationFrames * |
| 6557 | max(kMinNotifications, minNotificationsByMs); |
| 6558 | frameCount = max(frameCount, minFrameCount); |
| 6559 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 6560 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6561 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6562 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6563 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6564 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6565 | lStatus = initCheck(); |
| 6566 | if (lStatus != NO_ERROR) { |
| 6567 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6568 | goto Exit; |
| 6569 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6570 | |
| 6571 | { // scope for mLock |
| 6572 | Mutex::Autolock _l(mLock); |
| 6573 | |
| 6574 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6575 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 6576 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6577 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6578 | lStatus = track->initCheck(); |
| 6579 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6580 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6581 | // 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] | 6582 | goto Exit; |
| 6583 | } |
| 6584 | mTracks.add(track); |
| 6585 | |
| 6586 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6587 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6588 | mAudioFlinger->btNrecIsOff(); |
| 6589 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 6590 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6591 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6592 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6593 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6594 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6595 | // so ask activity manager to do this on our behalf |
| 6596 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 6597 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6598 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6599 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6600 | lStatus = NO_ERROR; |
| 6601 | |
| 6602 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6603 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6604 | return track; |
| 6605 | } |
| 6606 | |
| 6607 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6608 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6609 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6610 | { |
| 6611 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6612 | sp<ThreadBase> strongMe = this; |
| 6613 | status_t status = NO_ERROR; |
| 6614 | |
| 6615 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6616 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6617 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6618 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6619 | triggerSession, |
| 6620 | recordTrack->sessionId(), |
| 6621 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6622 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6623 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6624 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6625 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6626 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6627 | } else { |
| 6628 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6629 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6630 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6631 | } |
| 6632 | } |
| 6633 | |
| 6634 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6635 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6636 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6637 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6638 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6639 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6640 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6641 | } else { |
| 6642 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6643 | } |
| 6644 | return status; |
| 6645 | } |
| 6646 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6647 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6648 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6649 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6650 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6651 | mActiveTracks.add(recordTrack); |
| 6652 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6653 | status_t status = NO_ERROR; |
| 6654 | if (recordTrack->isExternalTrack()) { |
| 6655 | mLock.unlock(); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6656 | status = AudioSystem::startInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6657 | mLock.lock(); |
| 6658 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6659 | if (status != NO_ERROR) { |
| 6660 | mActiveTracks.remove(recordTrack); |
| 6661 | mActiveTracksGen++; |
| 6662 | recordTrack->clearSyncStartEvent(); |
| 6663 | ALOGV("RecordThread::start error %d", status); |
| 6664 | return status; |
| 6665 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6666 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6667 | // Catch up with current buffer indices if thread is already running. |
| 6668 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6669 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6670 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6671 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6672 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6673 | // clear any converter state as new data will be discontinuous |
| 6674 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6675 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6676 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6677 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6678 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6679 | ALOGV("Record failed to start"); |
| 6680 | status = BAD_VALUE; |
| 6681 | goto startError; |
| 6682 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6683 | return status; |
| 6684 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6685 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6686 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6687 | if (recordTrack->isExternalTrack()) { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6688 | AudioSystem::stopInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6689 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6690 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6691 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6692 | return status; |
| 6693 | } |
| 6694 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6695 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6696 | { |
| 6697 | sp<SyncEvent> strongEvent = event.promote(); |
| 6698 | |
| 6699 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6700 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6701 | if (ptr != 0) { |
| 6702 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6703 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6704 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6705 | } |
| 6706 | } |
| 6707 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6708 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6709 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6710 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6711 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6712 | return false; |
| 6713 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6714 | // 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] | 6715 | recordTrack->mState = TrackBase::PAUSING; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6716 | // signal thread to stop |
| 6717 | mWaitWorkCV.broadcast(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6718 | // do not wait for mStartStopCond if exiting |
| 6719 | if (exitPending()) { |
| 6720 | return true; |
| 6721 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6722 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6723 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6724 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 6725 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6726 | ALOGV("Record stopped OK"); |
| 6727 | return true; |
| 6728 | } |
| 6729 | return false; |
| 6730 | } |
| 6731 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6732 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6733 | { |
| 6734 | return false; |
| 6735 | } |
| 6736 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6737 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6738 | { |
| 6739 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6740 | if (!isValidSyncEvent(event)) { |
| 6741 | return BAD_VALUE; |
| 6742 | } |
| 6743 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6744 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6745 | status_t ret = NAME_NOT_FOUND; |
| 6746 | |
| 6747 | Mutex::Autolock _l(mLock); |
| 6748 | |
| 6749 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6750 | sp<RecordTrack> track = mTracks[i]; |
| 6751 | if (eventSession == track->sessionId()) { |
| 6752 | (void) track->setSyncEvent(event); |
| 6753 | ret = NO_ERROR; |
| 6754 | } |
| 6755 | } |
| 6756 | return ret; |
| 6757 | #else |
| 6758 | return BAD_VALUE; |
| 6759 | #endif |
| 6760 | } |
| 6761 | |
| 6762 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6763 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6764 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6765 | track->terminate(); |
| 6766 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6767 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6768 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6769 | removeTrack_l(track); |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6774 | { |
| 6775 | mTracks.remove(track); |
| 6776 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6777 | if (track->isFastTrack()) { |
| 6778 | ALOG_ASSERT(!mFastTrackAvail); |
| 6779 | mFastTrackAvail = true; |
| 6780 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6784 | { |
| 6785 | dumpInternals(fd, args); |
| 6786 | dumpTracks(fd, args); |
| 6787 | dumpEffectChains(fd, args); |
| 6788 | } |
| 6789 | |
| 6790 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6791 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6792 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6793 | |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6794 | dumpBase(fd, args); |
| 6795 | |
| 6796 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6797 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6798 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6799 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6800 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6801 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6802 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 6803 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6804 | // This is a large object so we place it on the heap. |
| 6805 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 6806 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 6807 | copy->dump(fd); |
| 6808 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6809 | } |
| 6810 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6811 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6812 | { |
| 6813 | const size_t SIZE = 256; |
| 6814 | char buffer[SIZE]; |
| 6815 | String8 result; |
| 6816 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6817 | size_t numtracks = mTracks.size(); |
| 6818 | size_t numactive = mActiveTracks.size(); |
| 6819 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6820 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6821 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6822 | dprintf(fd, " of which %zu are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6823 | RecordTrack::appendDumpHeader(result); |
| 6824 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6825 | sp<RecordTrack> track = mTracks[i]; |
| 6826 | if (track != 0) { |
| 6827 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6828 | if (active) { |
| 6829 | numactiveseen++; |
| 6830 | } |
| 6831 | track->dump(buffer, SIZE, active); |
| 6832 | result.append(buffer); |
| 6833 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6834 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6835 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6836 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6837 | } |
| 6838 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6839 | if (numactiveseen != numactive) { |
| 6840 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 6841 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6842 | result.append(buffer); |
| 6843 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6844 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6845 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6846 | if (mTracks.indexOf(track) < 0) { |
| 6847 | track->dump(buffer, SIZE, true); |
| 6848 | result.append(buffer); |
| 6849 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6850 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6851 | |
| 6852 | } |
| 6853 | write(fd, result.string(), result.size()); |
| 6854 | } |
| 6855 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6856 | |
| 6857 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 6858 | { |
| 6859 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6860 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6861 | mRsmpInFront = recordThread->mRsmpInRear; |
| 6862 | mRsmpInUnrel = 0; |
| 6863 | } |
| 6864 | |
| 6865 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 6866 | size_t *framesAvailable, bool *hasOverrun) |
| 6867 | { |
| 6868 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6869 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6870 | const int32_t rear = recordThread->mRsmpInRear; |
| 6871 | const int32_t front = mRsmpInFront; |
| 6872 | const ssize_t filled = rear - front; |
| 6873 | |
| 6874 | size_t framesIn; |
| 6875 | bool overrun = false; |
| 6876 | if (filled < 0) { |
| 6877 | // should not happen, but treat like a massive overrun and re-sync |
| 6878 | framesIn = 0; |
| 6879 | mRsmpInFront = rear; |
| 6880 | overrun = true; |
| 6881 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 6882 | framesIn = (size_t) filled; |
| 6883 | } else { |
| 6884 | // client is not keeping up with server, but give it latest data |
| 6885 | framesIn = recordThread->mRsmpInFrames; |
| 6886 | mRsmpInFront = /* front = */ rear - framesIn; |
| 6887 | overrun = true; |
| 6888 | } |
| 6889 | if (framesAvailable != NULL) { |
| 6890 | *framesAvailable = framesIn; |
| 6891 | } |
| 6892 | if (hasOverrun != NULL) { |
| 6893 | *hasOverrun = overrun; |
| 6894 | } |
| 6895 | } |
| 6896 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6897 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6898 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6899 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6900 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6901 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6902 | if (threadBase == 0) { |
| 6903 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6904 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6905 | return NOT_ENOUGH_DATA; |
| 6906 | } |
| 6907 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6908 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6909 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6910 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6911 | // FIXME should not be P2 (don't want to increase latency) |
| 6912 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6913 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6914 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6915 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 6916 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6917 | if (part1 > (size_t) filled) { |
| 6918 | part1 = filled; |
| 6919 | } |
| 6920 | size_t ask = buffer->frameCount; |
| 6921 | ALOG_ASSERT(ask > 0); |
| 6922 | if (part1 > ask) { |
| 6923 | part1 = ask; |
| 6924 | } |
| 6925 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6926 | // 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] | 6927 | buffer->raw = NULL; |
| 6928 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6929 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6930 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6931 | } |
| 6932 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6933 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6934 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6935 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6936 | return NO_ERROR; |
| 6937 | } |
| 6938 | |
| 6939 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6940 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 6941 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6942 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6943 | size_t stepCount = buffer->frameCount; |
| 6944 | if (stepCount == 0) { |
| 6945 | return; |
| 6946 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6947 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 6948 | mRsmpInUnrel -= stepCount; |
| 6949 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6950 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6951 | buffer->frameCount = 0; |
| 6952 | } |
| 6953 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6954 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( |
| 6955 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6956 | uint32_t srcSampleRate, |
| 6957 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6958 | uint32_t dstSampleRate) : |
| 6959 | mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars |
| 6960 | // mSrcFormat |
| 6961 | // mSrcSampleRate |
| 6962 | // mDstChannelMask |
| 6963 | // mDstFormat |
| 6964 | // mDstSampleRate |
| 6965 | // mSrcChannelCount |
| 6966 | // mDstChannelCount |
| 6967 | // mDstFrameSize |
| 6968 | mBuf(NULL), mBufFrames(0), mBufFrameSize(0), |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6969 | mResampler(NULL), |
| 6970 | mIsLegacyDownmix(false), |
| 6971 | mIsLegacyUpmix(false), |
| 6972 | mRequiresFloat(false), |
| 6973 | mInputConverterProvider(NULL) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6974 | { |
| 6975 | (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, |
| 6976 | dstChannelMask, dstFormat, dstSampleRate); |
| 6977 | } |
| 6978 | |
| 6979 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { |
| 6980 | free(mBuf); |
| 6981 | delete mResampler; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6982 | delete mInputConverterProvider; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, |
| 6986 | AudioBufferProvider *provider, size_t frames) |
| 6987 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6988 | if (mInputConverterProvider != NULL) { |
| 6989 | mInputConverterProvider->setBufferProvider(provider); |
| 6990 | provider = mInputConverterProvider; |
| 6991 | } |
| 6992 | |
| 6993 | if (mResampler == NULL) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6994 | ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6995 | mSrcSampleRate, mSrcFormat, mDstFormat); |
| 6996 | |
| 6997 | AudioBufferProvider::Buffer buffer; |
| 6998 | for (size_t i = frames; i > 0; ) { |
| 6999 | buffer.frameCount = i; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7000 | status_t status = provider->getNextBuffer(&buffer); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7001 | if (status != OK || buffer.frameCount == 0) { |
| 7002 | frames -= i; // cannot fill request. |
| 7003 | break; |
| 7004 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7005 | // format convert to destination buffer |
| 7006 | convertNoResampler(dst, buffer.raw, buffer.frameCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7007 | |
| 7008 | dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; |
| 7009 | i -= buffer.frameCount; |
| 7010 | provider->releaseBuffer(&buffer); |
| 7011 | } |
| 7012 | } else { |
| 7013 | ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 7014 | mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); |
| 7015 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7016 | // reallocate buffer if needed |
| 7017 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 7018 | free(mBuf); |
| 7019 | mBufFrames = frames; |
| 7020 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 7021 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7022 | // resampler accumulates, but we only have one source track |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7023 | memset(mBuf, 0, frames * mBufFrameSize); |
| 7024 | frames = mResampler->resample((int32_t*)mBuf, frames, provider); |
| 7025 | // format convert to destination buffer |
| 7026 | convertResampler(dst, mBuf, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7027 | } |
| 7028 | return frames; |
| 7029 | } |
| 7030 | |
| 7031 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( |
| 7032 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 7033 | uint32_t srcSampleRate, |
| 7034 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 7035 | uint32_t dstSampleRate) |
| 7036 | { |
| 7037 | // quick evaluation if there is any change. |
| 7038 | if (mSrcFormat == srcFormat |
| 7039 | && mSrcChannelMask == srcChannelMask |
| 7040 | && mSrcSampleRate == srcSampleRate |
| 7041 | && mDstFormat == dstFormat |
| 7042 | && mDstChannelMask == dstChannelMask |
| 7043 | && mDstSampleRate == dstSampleRate) { |
| 7044 | return NO_ERROR; |
| 7045 | } |
| 7046 | |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 7047 | ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" |
| 7048 | " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", |
| 7049 | srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7050 | const bool valid = |
| 7051 | audio_is_input_channel(srcChannelMask) |
| 7052 | && audio_is_input_channel(dstChannelMask) |
| 7053 | && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) |
| 7054 | && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) |
| 7055 | && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) |
| 7056 | ; // no upsampling checks for now |
| 7057 | if (!valid) { |
| 7058 | return BAD_VALUE; |
| 7059 | } |
| 7060 | |
| 7061 | mSrcFormat = srcFormat; |
| 7062 | mSrcChannelMask = srcChannelMask; |
| 7063 | mSrcSampleRate = srcSampleRate; |
| 7064 | mDstFormat = dstFormat; |
| 7065 | mDstChannelMask = dstChannelMask; |
| 7066 | mDstSampleRate = dstSampleRate; |
| 7067 | |
| 7068 | // compute derived parameters |
| 7069 | mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); |
| 7070 | mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); |
| 7071 | mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); |
| 7072 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7073 | // do we need to resample? |
| 7074 | delete mResampler; |
| 7075 | mResampler = NULL; |
| 7076 | if (mSrcSampleRate != mDstSampleRate) { |
| 7077 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, |
| 7078 | mSrcChannelCount, mDstSampleRate); |
| 7079 | mResampler->setSampleRate(mSrcSampleRate); |
| 7080 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); |
| 7081 | } |
| 7082 | |
| 7083 | // are we running legacy channel conversion modes? |
| 7084 | mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 7085 | || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) |
| 7086 | && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; |
| 7087 | mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO |
| 7088 | && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 7089 | || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); |
| 7090 | |
| 7091 | // do we need to process in float? |
| 7092 | mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; |
| 7093 | |
| 7094 | // do we need a staging buffer to convert for destination (we can still optimize this)? |
| 7095 | // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity |
| 7096 | if (mResampler != NULL) { |
| 7097 | mBufFrameSize = max(mSrcChannelCount, FCC_2) |
| 7098 | * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame] | 7099 | } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7100 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
| 7101 | } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7102 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); |
| 7103 | } else { |
| 7104 | mBufFrameSize = 0; |
| 7105 | } |
| 7106 | mBufFrames = 0; // force the buffer to be resized. |
| 7107 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7108 | // do we need an input converter buffer provider to give us float? |
| 7109 | delete mInputConverterProvider; |
| 7110 | mInputConverterProvider = NULL; |
| 7111 | if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 7112 | mInputConverterProvider = new ReformatBufferProvider( |
| 7113 | audio_channel_count_from_in_mask(mSrcChannelMask), |
| 7114 | mSrcFormat, |
| 7115 | AUDIO_FORMAT_PCM_FLOAT, |
| 7116 | 256 /* provider buffer frame count */); |
| 7117 | } |
| 7118 | |
| 7119 | // do we need a remixer to do channel mask conversion |
| 7120 | if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { |
| 7121 | (void) memcpy_by_index_array_initialization_from_channel_mask( |
| 7122 | mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7123 | } |
| 7124 | return NO_ERROR; |
| 7125 | } |
| 7126 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7127 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( |
| 7128 | void *dst, const void *src, size_t frames) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7129 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7130 | // 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] | 7131 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 7132 | free(mBuf); |
| 7133 | mBufFrames = frames; |
| 7134 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 7135 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7136 | // do we need to do legacy upmix and downmix? |
| 7137 | if (mIsLegacyUpmix || mIsLegacyDownmix) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7138 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7139 | if (mIsLegacyUpmix) { |
| 7140 | upmix_to_stereo_float_from_mono_float((float *)dstBuf, |
| 7141 | (const float *)src, frames); |
| 7142 | } else /*mIsLegacyDownmix */ { |
| 7143 | downmix_to_mono_float_from_stereo_float((float *)dstBuf, |
| 7144 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7145 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7146 | if (mBuf != NULL) { |
| 7147 | memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, |
| 7148 | frames * mDstChannelCount); |
| 7149 | } |
| 7150 | return; |
| 7151 | } |
| 7152 | // do we need to do channel mask conversion? |
| 7153 | if (mSrcChannelMask != mDstChannelMask) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7154 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7155 | memcpy_by_index_array(dstBuf, mDstChannelCount, |
| 7156 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); |
| 7157 | if (dstBuf == dst) { |
| 7158 | return; // format is the same |
| 7159 | } |
| 7160 | } |
| 7161 | // convert to destination buffer |
| 7162 | const void *convertBuf = mBuf != NULL ? mBuf : src; |
| 7163 | memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, |
| 7164 | frames * mDstChannelCount); |
| 7165 | } |
| 7166 | |
| 7167 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( |
| 7168 | void *dst, /*not-a-const*/ void *src, size_t frames) |
| 7169 | { |
| 7170 | // src buffer format is ALWAYS float when entering this routine |
| 7171 | if (mIsLegacyUpmix) { |
| 7172 | ; // mono to stereo already handled by resampler |
| 7173 | } else if (mIsLegacyDownmix |
| 7174 | || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { |
| 7175 | // the resampler outputs stereo for mono input channel (a feature?) |
| 7176 | // must convert to mono |
| 7177 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 7178 | (const float *)src, frames); |
| 7179 | } else if (mSrcChannelMask != mDstChannelMask) { |
| 7180 | // convert to mono channel again for channel mask conversion (could be skipped |
| 7181 | // with further optimization). |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7182 | if (mSrcChannelCount == 1) { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7183 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 7184 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7185 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7186 | // convert to destination format (in place, OK as float is larger than other types) |
| 7187 | if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 7188 | memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 7189 | frames * mSrcChannelCount); |
| 7190 | } |
| 7191 | // channel convert and save to dst |
| 7192 | memcpy_by_index_array(dst, mDstChannelCount, |
| 7193 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); |
| 7194 | return; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7195 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7196 | // convert to destination format and save to dst |
| 7197 | memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 7198 | frames * mDstChannelCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7199 | } |
| 7200 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7201 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 7202 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7203 | { |
| 7204 | bool reconfig = false; |
| 7205 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7206 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7207 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7208 | audio_format_t reqFormat = mFormat; |
| 7209 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7210 | // 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] | 7211 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 7212 | |
| 7213 | AudioParameter param = AudioParameter(keyValuePair); |
| 7214 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7215 | |
| 7216 | // scope for AutoPark extends to end of method |
| 7217 | AutoPark<FastCapture> park(mFastCapture); |
| 7218 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7219 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 7220 | // channel count change can be requested. Do we mandate the first client defines the |
| 7221 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 7222 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 7223 | samplingRate = value; |
| 7224 | reconfig = true; |
| 7225 | } |
| 7226 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7227 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7228 | status = BAD_VALUE; |
| 7229 | } else { |
| 7230 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7231 | reconfig = true; |
| 7232 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7233 | } |
| 7234 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 7235 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7236 | if (!audio_is_input_channel(mask) || |
| 7237 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7238 | status = BAD_VALUE; |
| 7239 | } else { |
| 7240 | channelMask = mask; |
| 7241 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7242 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7243 | } |
| 7244 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7245 | // do not accept frame count changes if tracks are open as the track buffer |
| 7246 | // size depends on frame count and correct behavior would not be guaranteed |
| 7247 | // if frame count is changed after track creation |
| 7248 | if (mActiveTracks.size() > 0) { |
| 7249 | status = INVALID_OPERATION; |
| 7250 | } else { |
| 7251 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7252 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7253 | } |
| 7254 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 7255 | // forward device change to effects that have requested to be |
| 7256 | // aware of attached audio device. |
| 7257 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7258 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7259 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7260 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7261 | // store input device and output device but do not forward output device to audio HAL. |
| 7262 | // Note that status is ignored by the caller for output device |
| 7263 | // (see AudioFlinger::setParameters() |
| 7264 | if (audio_is_output_devices(value)) { |
| 7265 | mOutDevice = value; |
| 7266 | status = BAD_VALUE; |
| 7267 | } else { |
| 7268 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7269 | if (value != AUDIO_DEVICE_NONE) { |
| 7270 | mPrevInDevice = value; |
| 7271 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7272 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7273 | // pre processings |
| 7274 | if (mTracks.size() > 0) { |
| 7275 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7276 | mAudioFlinger->btNrecIsOff(); |
| 7277 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7278 | sp<RecordTrack> track = mTracks[i]; |
| 7279 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7280 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7281 | } |
| 7282 | } |
| 7283 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7284 | } |
| 7285 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7286 | mAudioSource != (audio_source_t)value) { |
| 7287 | // forward device change to effects that have requested to be |
| 7288 | // aware of attached audio device. |
| 7289 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7290 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7291 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7292 | mAudioSource = (audio_source_t)value; |
| 7293 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7294 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7295 | if (status == NO_ERROR) { |
| 7296 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7297 | keyValuePair.string()); |
| 7298 | if (status == INVALID_OPERATION) { |
| 7299 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7300 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7301 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7302 | } |
| 7303 | if (reconfig) { |
| 7304 | if (status == BAD_VALUE && |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7305 | audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && |
| 7306 | audio_is_linear_pcm(reqFormat) && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7307 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7308 | <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7309 | audio_channel_count_from_in_mask( |
Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 7310 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7311 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7312 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7313 | if (status == NO_ERROR) { |
| 7314 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7315 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7316 | } |
| 7317 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7318 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7319 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7320 | return reconfig; |
| 7321 | } |
| 7322 | |
| 7323 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7324 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7325 | Mutex::Autolock _l(mLock); |
| 7326 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7327 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7328 | } |
| 7329 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7330 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 7331 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7332 | free(s); |
| 7333 | return out_s8; |
| 7334 | } |
| 7335 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7336 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7337 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7338 | |
| 7339 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7340 | |
| 7341 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7342 | case AUDIO_INPUT_OPENED: |
| 7343 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7344 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7345 | desc->mChannelMask = mChannelMask; |
| 7346 | desc->mSamplingRate = mSampleRate; |
| 7347 | desc->mFormat = mFormat; |
| 7348 | desc->mFrameCount = mFrameCount; |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7349 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7350 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7351 | break; |
| 7352 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7353 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7354 | default: |
| 7355 | break; |
| 7356 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7357 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7358 | } |
| 7359 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7360 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7361 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7362 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 7363 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7364 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7365 | if (mChannelCount > FCC_8) { |
| 7366 | ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); |
| 7367 | } |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7368 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 7369 | mFormat = mHALFormat; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7370 | if (!audio_is_linear_pcm(mFormat)) { |
| 7371 | ALOGE("HAL format %#x is not linear pcm", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 7372 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 7373 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7374 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 7375 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7376 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7377 | // 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] | 7378 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7379 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7380 | // A larger value should allow more old data to be read after a track calls start(), |
| 7381 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7382 | // |
| 7383 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7384 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7385 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7386 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7387 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7388 | |
| 7389 | // TODO optimize audio capture buffer sizes ... |
| 7390 | // Here we calculate the size of the sliding buffer used as a source |
| 7391 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7392 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7393 | // be better to have it derived from the pipe depth in the long term. |
| 7394 | // The current value is higher than necessary. However it should not add to latency. |
| 7395 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7396 | // 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] | 7397 | size_t bufferSize = (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize; |
| 7398 | (void)posix_memalign(&mRsmpInBuffer, 32, bufferSize); |
| 7399 | memset(mRsmpInBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7400 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7401 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7402 | // 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] | 7403 | } |
| 7404 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7405 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7406 | { |
| 7407 | Mutex::Autolock _l(mLock); |
| 7408 | if (initCheck() != NO_ERROR) { |
| 7409 | return 0; |
| 7410 | } |
| 7411 | |
| 7412 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 7413 | } |
| 7414 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7415 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 7416 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7417 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7418 | uint32_t result = 0; |
| 7419 | if (getEffectChain_l(sessionId) != 0) { |
| 7420 | result = EFFECT_SESSION; |
| 7421 | } |
| 7422 | |
| 7423 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7424 | if (sessionId == mTracks[i]->sessionId()) { |
| 7425 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7426 | if (mTracks[i]->isFastTrack()) { |
| 7427 | result |= FAST_SESSION; |
| 7428 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7429 | break; |
| 7430 | } |
| 7431 | } |
| 7432 | |
| 7433 | return result; |
| 7434 | } |
| 7435 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7436 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7437 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7438 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7439 | Mutex::Autolock _l(mLock); |
| 7440 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7441 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7442 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7443 | if (ids.indexOfKey(sessionId) < 0) { |
| 7444 | ids.add(sessionId, true); |
| 7445 | } |
| 7446 | } |
| 7447 | return ids; |
| 7448 | } |
| 7449 | |
| 7450 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7451 | { |
| 7452 | Mutex::Autolock _l(mLock); |
| 7453 | AudioStreamIn *input = mInput; |
| 7454 | mInput = NULL; |
| 7455 | return input; |
| 7456 | } |
| 7457 | |
| 7458 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 7459 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 7460 | { |
| 7461 | if (mInput == NULL) { |
| 7462 | return NULL; |
| 7463 | } |
| 7464 | return &mInput->stream->common; |
| 7465 | } |
| 7466 | |
| 7467 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7468 | { |
| 7469 | // only one chain per input thread |
| 7470 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7471 | 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] | 7472 | return INVALID_OPERATION; |
| 7473 | } |
| 7474 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7475 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7476 | chain->setInBuffer(NULL); |
| 7477 | chain->setOutBuffer(NULL); |
| 7478 | |
| 7479 | checkSuspendOnAddEffectChain_l(chain); |
| 7480 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7481 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7482 | // just moved them to a new input stream. |
| 7483 | chain->syncHalEffectsState(); |
| 7484 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7485 | mEffectChains.add(chain); |
| 7486 | |
| 7487 | return NO_ERROR; |
| 7488 | } |
| 7489 | |
| 7490 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7491 | { |
| 7492 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7493 | ALOGW_IF(mEffectChains.size() != 1, |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7494 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7495 | chain.get(), mEffectChains.size(), this); |
| 7496 | if (mEffectChains.size() == 1) { |
| 7497 | mEffectChains.removeAt(0); |
| 7498 | } |
| 7499 | return 0; |
| 7500 | } |
| 7501 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7502 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7503 | audio_patch_handle_t *handle) |
| 7504 | { |
| 7505 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7506 | |
| 7507 | // store new device and send to effects |
| 7508 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7509 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7510 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7511 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7512 | } |
| 7513 | |
| 7514 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7515 | // pre processings |
| 7516 | if (mTracks.size() > 0) { |
| 7517 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7518 | mAudioFlinger->btNrecIsOff(); |
| 7519 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7520 | sp<RecordTrack> track = mTracks[i]; |
| 7521 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7522 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 7523 | } |
| 7524 | } |
| 7525 | |
| 7526 | // store new source and send to effects |
| 7527 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7528 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7529 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7530 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7531 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7532 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7533 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7534 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7535 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7536 | status = hwDevice->create_audio_patch(hwDevice, |
| 7537 | patch->num_sources, |
| 7538 | patch->sources, |
| 7539 | patch->num_sinks, |
| 7540 | patch->sinks, |
| 7541 | handle); |
| 7542 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7543 | char *address; |
| 7544 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7545 | address = audio_device_address_to_parameter( |
| 7546 | patch->sources[0].ext.device.type, |
| 7547 | patch->sources[0].ext.device.address); |
| 7548 | } else { |
| 7549 | address = (char *)calloc(1, 1); |
| 7550 | } |
| 7551 | AudioParameter param = AudioParameter(String8(address)); |
| 7552 | free(address); |
| 7553 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), |
| 7554 | (int)patch->sources[0].ext.device.type); |
| 7555 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), |
| 7556 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 7557 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7558 | param.toString().string()); |
| 7559 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7560 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7561 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7562 | if (mInDevice != mPrevInDevice) { |
| 7563 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7564 | mPrevInDevice = mInDevice; |
| 7565 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7566 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7567 | return status; |
| 7568 | } |
| 7569 | |
| 7570 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7571 | { |
| 7572 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7573 | |
| 7574 | mInDevice = AUDIO_DEVICE_NONE; |
| 7575 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7576 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 7577 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7578 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 7579 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7580 | AudioParameter param; |
| 7581 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 7582 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7583 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7584 | } |
| 7585 | return status; |
| 7586 | } |
| 7587 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7588 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7589 | { |
| 7590 | Mutex::Autolock _l(mLock); |
| 7591 | mTracks.add(record); |
| 7592 | } |
| 7593 | |
| 7594 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7595 | { |
| 7596 | Mutex::Autolock _l(mLock); |
| 7597 | destroyTrack_l(record); |
| 7598 | } |
| 7599 | |
| 7600 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7601 | { |
| 7602 | ThreadBase::getAudioPortConfig(config); |
| 7603 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7604 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7605 | config->ext.mix.usecase.source = mAudioSource; |
| 7606 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7607 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7608 | } // namespace android |