| 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 |  | 
|  | 1259 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held | 
|  | 1260 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
|  | 1261 | const sp<AudioFlinger::Client>& client, | 
|  | 1262 | const sp<IEffectClient>& effectClient, | 
|  | 1263 | int32_t priority, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1264 | audio_session_t sessionId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1265 | effect_descriptor_t *desc, | 
|  | 1266 | int *enabled, | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1267 | status_t *status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1268 | { | 
|  | 1269 | sp<EffectModule> effect; | 
|  | 1270 | sp<EffectHandle> handle; | 
|  | 1271 | status_t lStatus; | 
|  | 1272 | sp<EffectChain> chain; | 
|  | 1273 | bool chainCreated = false; | 
|  | 1274 | bool effectCreated = false; | 
|  | 1275 | bool effectRegistered = false; | 
|  | 1276 |  | 
|  | 1277 | lStatus = initCheck(); | 
|  | 1278 | if (lStatus != NO_ERROR) { | 
|  | 1279 | ALOGW("createEffect_l() Audio driver not initialized."); | 
|  | 1280 | goto Exit; | 
|  | 1281 | } | 
|  | 1282 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1283 | // Reject any effect on Direct output threads for now, since the format of | 
|  | 1284 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). | 
|  | 1285 | if (mType == DIRECT) { | 
|  | 1286 | ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1287 | desc->name, mThreadName); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1288 | lStatus = BAD_VALUE; | 
|  | 1289 | goto Exit; | 
|  | 1290 | } | 
|  | 1291 |  | 
| Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1292 | // Reject any effect on mixer or duplicating multichannel sinks. | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1293 | // TODO: fix both format and multichannel issues with effects. | 
| Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1294 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { | 
|  | 1295 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", | 
|  | 1296 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1297 | lStatus = BAD_VALUE; | 
|  | 1298 | goto Exit; | 
|  | 1299 | } | 
|  | 1300 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1301 | // Allow global effects only on offloaded and mixer threads | 
|  | 1302 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 1303 | switch (mType) { | 
|  | 1304 | case MIXER: | 
|  | 1305 | case OFFLOAD: | 
|  | 1306 | break; | 
|  | 1307 | case DIRECT: | 
|  | 1308 | case DUPLICATING: | 
|  | 1309 | case RECORD: | 
|  | 1310 | default: | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1311 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", | 
|  | 1312 | desc->name, mThreadName); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1313 | lStatus = BAD_VALUE; | 
|  | 1314 | goto Exit; | 
|  | 1315 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1316 | } | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1317 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1318 | // Only Pre processor effects are allowed on input threads and only on input threads | 
|  | 1319 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
|  | 1320 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", | 
|  | 1321 | desc->name, desc->flags, mType); | 
|  | 1322 | lStatus = BAD_VALUE; | 
|  | 1323 | goto Exit; | 
|  | 1324 | } | 
|  | 1325 |  | 
|  | 1326 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
|  | 1327 |  | 
|  | 1328 | { // scope for mLock | 
|  | 1329 | Mutex::Autolock _l(mLock); | 
|  | 1330 |  | 
|  | 1331 | // check for existing effect chain with the requested audio session | 
|  | 1332 | chain = getEffectChain_l(sessionId); | 
|  | 1333 | if (chain == 0) { | 
|  | 1334 | // create a new chain for this session | 
|  | 1335 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
|  | 1336 | chain = new EffectChain(this, sessionId); | 
|  | 1337 | addEffectChain_l(chain); | 
|  | 1338 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 1339 | chainCreated = true; | 
|  | 1340 | } else { | 
|  | 1341 | effect = chain->getEffectFromDesc_l(desc); | 
|  | 1342 | } | 
|  | 1343 |  | 
|  | 1344 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
|  | 1345 |  | 
|  | 1346 | if (effect == 0) { | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1347 | audio_unique_id_t id = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1348 | // Check CPU and memory usage | 
|  | 1349 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); | 
|  | 1350 | if (lStatus != NO_ERROR) { | 
|  | 1351 | goto Exit; | 
|  | 1352 | } | 
|  | 1353 | effectRegistered = true; | 
|  | 1354 | // create a new effect module if none present in the chain | 
|  | 1355 | effect = new EffectModule(this, chain, desc, id, sessionId); | 
|  | 1356 | lStatus = effect->status(); | 
|  | 1357 | if (lStatus != NO_ERROR) { | 
|  | 1358 | goto Exit; | 
|  | 1359 | } | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1360 | effect->setOffloaded(mType == OFFLOAD, mId); | 
|  | 1361 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1362 | lStatus = chain->addEffect_l(effect); | 
|  | 1363 | if (lStatus != NO_ERROR) { | 
|  | 1364 | goto Exit; | 
|  | 1365 | } | 
|  | 1366 | effectCreated = true; | 
|  | 1367 |  | 
|  | 1368 | effect->setDevice(mOutDevice); | 
|  | 1369 | effect->setDevice(mInDevice); | 
|  | 1370 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 1371 | effect->setAudioSource(mAudioSource); | 
|  | 1372 | } | 
|  | 1373 | // create effect handle and connect it to effect module | 
|  | 1374 | handle = new EffectHandle(effect, client, effectClient, priority); | 
| Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1375 | lStatus = handle->initCheck(); | 
|  | 1376 | if (lStatus == OK) { | 
|  | 1377 | lStatus = effect->addHandle(handle.get()); | 
|  | 1378 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1379 | if (enabled != NULL) { | 
|  | 1380 | *enabled = (int)effect->isEnabled(); | 
|  | 1381 | } | 
|  | 1382 | } | 
|  | 1383 |  | 
|  | 1384 | Exit: | 
|  | 1385 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
|  | 1386 | Mutex::Autolock _l(mLock); | 
|  | 1387 | if (effectCreated) { | 
|  | 1388 | chain->removeEffect_l(effect); | 
|  | 1389 | } | 
|  | 1390 | if (effectRegistered) { | 
|  | 1391 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 1392 | } | 
|  | 1393 | if (chainCreated) { | 
|  | 1394 | removeEffectChain_l(chain); | 
|  | 1395 | } | 
|  | 1396 | handle.clear(); | 
|  | 1397 | } | 
|  | 1398 |  | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1399 | *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1400 | return handle; | 
|  | 1401 | } | 
|  | 1402 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1403 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, | 
|  | 1404 | int effectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1405 | { | 
|  | 1406 | Mutex::Autolock _l(mLock); | 
|  | 1407 | return getEffect_l(sessionId, effectId); | 
|  | 1408 | } | 
|  | 1409 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1410 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, | 
|  | 1411 | int effectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1412 | { | 
|  | 1413 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1414 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; | 
|  | 1415 | } | 
|  | 1416 |  | 
|  | 1417 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
|  | 1418 | // PlaybackThread::mLock held | 
|  | 1419 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
|  | 1420 | { | 
|  | 1421 | // check for existing effect chain with the requested audio session | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1422 | audio_session_t sessionId = effect->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1423 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1424 | bool chainCreated = false; | 
|  | 1425 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1426 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), | 
|  | 1427 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", | 
|  | 1428 | this, effect->desc().name, effect->desc().flags); | 
|  | 1429 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1430 | if (chain == 0) { | 
|  | 1431 | // create a new chain for this session | 
|  | 1432 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
|  | 1433 | chain = new EffectChain(this, sessionId); | 
|  | 1434 | addEffectChain_l(chain); | 
|  | 1435 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 1436 | chainCreated = true; | 
|  | 1437 | } | 
|  | 1438 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); | 
|  | 1439 |  | 
|  | 1440 | if (chain->getEffectFromId_l(effect->id()) != 0) { | 
|  | 1441 | ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
|  | 1442 | this, effect->desc().name, chain.get()); | 
|  | 1443 | return BAD_VALUE; | 
|  | 1444 | } | 
|  | 1445 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1446 | effect->setOffloaded(mType == OFFLOAD, mId); | 
|  | 1447 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1448 | status_t status = chain->addEffect_l(effect); | 
|  | 1449 | if (status != NO_ERROR) { | 
|  | 1450 | if (chainCreated) { | 
|  | 1451 | removeEffectChain_l(chain); | 
|  | 1452 | } | 
|  | 1453 | return status; | 
|  | 1454 | } | 
|  | 1455 |  | 
|  | 1456 | effect->setDevice(mOutDevice); | 
|  | 1457 | effect->setDevice(mInDevice); | 
|  | 1458 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 1459 | effect->setAudioSource(mAudioSource); | 
|  | 1460 | return NO_ERROR; | 
|  | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { | 
|  | 1464 |  | 
|  | 1465 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); | 
|  | 1466 | effect_descriptor_t desc = effect->desc(); | 
|  | 1467 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 1468 | detachAuxEffect_l(effect->id()); | 
|  | 1469 | } | 
|  | 1470 |  | 
|  | 1471 | sp<EffectChain> chain = effect->chain().promote(); | 
|  | 1472 | if (chain != 0) { | 
|  | 1473 | // remove effect chain if removing last effect | 
|  | 1474 | if (chain->removeEffect_l(effect) == 0) { | 
|  | 1475 | removeEffectChain_l(chain); | 
|  | 1476 | } | 
|  | 1477 | } else { | 
|  | 1478 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
|  | 1479 | } | 
|  | 1480 | } | 
|  | 1481 |  | 
|  | 1482 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
|  | 1483 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
|  | 1484 | { | 
|  | 1485 | effectChains = mEffectChains; | 
|  | 1486 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 1487 | mEffectChains[i]->lock(); | 
|  | 1488 | } | 
|  | 1489 | } | 
|  | 1490 |  | 
|  | 1491 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
|  | 1492 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
|  | 1493 | { | 
|  | 1494 | for (size_t i = 0; i < effectChains.size(); i++) { | 
|  | 1495 | effectChains[i]->unlock(); | 
|  | 1496 | } | 
|  | 1497 | } | 
|  | 1498 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1499 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1500 | { | 
|  | 1501 | Mutex::Autolock _l(mLock); | 
|  | 1502 | return getEffectChain_l(sessionId); | 
|  | 1503 | } | 
|  | 1504 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1505 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) | 
|  | 1506 | const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1507 | { | 
|  | 1508 | size_t size = mEffectChains.size(); | 
|  | 1509 | for (size_t i = 0; i < size; i++) { | 
|  | 1510 | if (mEffectChains[i]->sessionId() == sessionId) { | 
|  | 1511 | return mEffectChains[i]; | 
|  | 1512 | } | 
|  | 1513 | } | 
|  | 1514 | return 0; | 
|  | 1515 | } | 
|  | 1516 |  | 
|  | 1517 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
|  | 1518 | { | 
|  | 1519 | Mutex::Autolock _l(mLock); | 
|  | 1520 | size_t size = mEffectChains.size(); | 
|  | 1521 | for (size_t i = 0; i < size; i++) { | 
|  | 1522 | mEffectChains[i]->setMode_l(mode); | 
|  | 1523 | } | 
|  | 1524 | } | 
|  | 1525 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1526 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) | 
|  | 1527 | { | 
|  | 1528 | config->type = AUDIO_PORT_TYPE_MIX; | 
|  | 1529 | config->ext.mix.handle = mId; | 
|  | 1530 | config->sample_rate = mSampleRate; | 
|  | 1531 | config->format = mFormat; | 
|  | 1532 | config->channel_mask = mChannelMask; | 
|  | 1533 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
|  | 1534 | AUDIO_PORT_CONFIG_FORMAT; | 
|  | 1535 | } | 
|  | 1536 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1537 | void AudioFlinger::ThreadBase::systemReady() | 
|  | 1538 | { | 
|  | 1539 | Mutex::Autolock _l(mLock); | 
|  | 1540 | if (mSystemReady) { | 
|  | 1541 | return; | 
|  | 1542 | } | 
|  | 1543 | mSystemReady = true; | 
|  | 1544 |  | 
|  | 1545 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { | 
|  | 1546 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); | 
|  | 1547 | } | 
|  | 1548 | mPendingConfigEvents.clear(); | 
|  | 1549 | } | 
|  | 1550 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1551 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1552 | // ---------------------------------------------------------------------------- | 
|  | 1553 | //      Playback | 
|  | 1554 | // ---------------------------------------------------------------------------- | 
|  | 1555 |  | 
|  | 1556 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 1557 | AudioStreamOut* output, | 
|  | 1558 | audio_io_handle_t id, | 
|  | 1559 | audio_devices_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1560 | type_t type, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1561 | bool systemReady) | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1562 | :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1563 | mNormalFrameCount(0), mSinkBuffer(NULL), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1564 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1565 | mMixerBuffer(NULL), | 
|  | 1566 | mMixerBufferSize(0), | 
|  | 1567 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), | 
|  | 1568 | mMixerBufferValid(false), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1569 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1570 | mEffectBuffer(NULL), | 
|  | 1571 | mEffectBufferSize(0), | 
|  | 1572 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), | 
|  | 1573 | mEffectBufferValid(false), | 
| Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1574 | mSuspended(0), mBytesWritten(0), | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1575 | mFramesWritten(0), | 
| Andy Hung | 9ebe29b | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1576 | mSuspendedFrames(0), | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1577 | mActiveTracksGeneration(0), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1578 | // mStreamTypes[] initialized in constructor body | 
|  | 1579 | mOutput(output), | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1580 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1581 | mMixerStatus(MIXER_IDLE), | 
|  | 1582 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1583 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1584 | mBytesRemaining(0), | 
|  | 1585 | mCurrentWriteLength(0), | 
|  | 1586 | mUseAsyncWrite(false), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1587 | mWriteAckSequence(0), | 
|  | 1588 | mDrainSequence(0), | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1589 | mSignalPending(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1590 | mScreenState(AudioFlinger::mScreenState), | 
|  | 1591 | // index 0 is reserved for normal mixer's submix | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1592 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1593 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1594 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1595 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); | 
|  | 1596 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1597 |  | 
|  | 1598 | // Assumes constructor is called by AudioFlinger with it's mLock held, but | 
|  | 1599 | // it would be safer to explicitly pass initial masterVolume/masterMute as | 
|  | 1600 | // parameter. | 
|  | 1601 | // | 
|  | 1602 | // If the HAL we are using has support for master volume or master mute, | 
|  | 1603 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
|  | 1604 | // and the mute set to false). | 
|  | 1605 | mMasterVolume = audioFlinger->masterVolume_l(); | 
|  | 1606 | mMasterMute = audioFlinger->masterMute_l(); | 
|  | 1607 | if (mOutput && mOutput->audioHwDev) { | 
|  | 1608 | if (mOutput->audioHwDev->canSetMasterVolume()) { | 
|  | 1609 | mMasterVolume = 1.0; | 
|  | 1610 | } | 
|  | 1611 |  | 
|  | 1612 | if (mOutput->audioHwDev->canSetMasterMute()) { | 
|  | 1613 | mMasterMute = false; | 
|  | 1614 | } | 
|  | 1615 | } | 
|  | 1616 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1617 | readOutputParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1618 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1619 | // ++ operator does not compile | 
| Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1620 | 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] | 1621 | stream = (audio_stream_type_t) (stream + 1)) { | 
|  | 1622 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); | 
|  | 1623 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); | 
|  | 1624 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1625 | } | 
|  | 1626 |  | 
|  | 1627 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
|  | 1628 | { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1629 | mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1630 | free(mSinkBuffer); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1631 | free(mMixerBuffer); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1632 | free(mEffectBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1633 | } | 
|  | 1634 |  | 
|  | 1635 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
|  | 1636 | { | 
|  | 1637 | dumpInternals(fd, args); | 
|  | 1638 | dumpTracks(fd, args); | 
|  | 1639 | dumpEffectChains(fd, args); | 
|  | 1640 | } | 
|  | 1641 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1642 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1643 | { | 
|  | 1644 | const size_t SIZE = 256; | 
|  | 1645 | char buffer[SIZE]; | 
|  | 1646 | String8 result; | 
|  | 1647 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1648 | result.appendFormat("  Stream volumes in dB: "); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1649 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
|  | 1650 | const stream_type_t *st = &mStreamTypes[i]; | 
|  | 1651 | if (i > 0) { | 
|  | 1652 | result.appendFormat(", "); | 
|  | 1653 | } | 
|  | 1654 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
|  | 1655 | if (st->mute) { | 
|  | 1656 | result.append("M"); | 
|  | 1657 | } | 
|  | 1658 | } | 
|  | 1659 | result.append("\n"); | 
|  | 1660 | write(fd, result.string(), result.length()); | 
|  | 1661 | result.clear(); | 
|  | 1662 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 | // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
|  | 1664 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1665 | dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1666 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1667 |  | 
|  | 1668 | size_t numtracks = mTracks.size(); | 
|  | 1669 | size_t numactive = mActiveTracks.size(); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1670 | dprintf(fd, "  %zu Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1671 | size_t numactiveseen = 0; | 
|  | 1672 | if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1673 | dprintf(fd, " of which %zu are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1674 | Track::appendDumpHeader(result); | 
|  | 1675 | for (size_t i = 0; i < numtracks; ++i) { | 
|  | 1676 | sp<Track> track = mTracks[i]; | 
|  | 1677 | if (track != 0) { | 
|  | 1678 | bool active = mActiveTracks.indexOf(track) >= 0; | 
|  | 1679 | if (active) { | 
|  | 1680 | numactiveseen++; | 
|  | 1681 | } | 
|  | 1682 | track->dump(buffer, SIZE, active); | 
|  | 1683 | result.append(buffer); | 
|  | 1684 | } | 
|  | 1685 | } | 
|  | 1686 | } else { | 
|  | 1687 | result.append("\n"); | 
|  | 1688 | } | 
|  | 1689 | if (numactiveseen != numactive) { | 
|  | 1690 | // some tracks in the active list were not in the tracks list | 
|  | 1691 | snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
|  | 1692 | " not in the track list\n"); | 
|  | 1693 | result.append(buffer); | 
|  | 1694 | Track::appendDumpHeader(result); | 
|  | 1695 | for (size_t i = 0; i < numactive; ++i) { | 
|  | 1696 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 1697 | if (track != 0 && mTracks.indexOf(track) < 0) { | 
|  | 1698 | track->dump(buffer, SIZE, true); | 
|  | 1699 | result.append(buffer); | 
|  | 1700 | } | 
|  | 1701 | } | 
|  | 1702 | } | 
|  | 1703 |  | 
|  | 1704 | write(fd, result.string(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1705 | } | 
|  | 1706 |  | 
|  | 1707 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 1708 | { | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1709 | 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] | 1710 |  | 
|  | 1711 | dumpBase(fd, args); | 
|  | 1712 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1713 | dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1714 | dprintf(fd, "  Last write occurred (msecs): %llu\n", | 
|  | 1715 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1716 | dprintf(fd, "  Total writes: %d\n", mNumWrites); | 
|  | 1717 | dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites); | 
|  | 1718 | dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no"); | 
|  | 1719 | dprintf(fd, "  Suspend count: %d\n", mSuspended); | 
|  | 1720 | dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer); | 
|  | 1721 | dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer); | 
|  | 1722 | dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer); | 
|  | 1723 | dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1724 | dprintf(fd, "  Standby delay ns=%lld\n", (long long)mStandbyDelayNs); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1725 | AudioStreamOut *output = mOutput; | 
|  | 1726 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; | 
|  | 1727 | String8 flagsAsString = outputFlagsToString(flags); | 
|  | 1728 | dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1729 | } | 
|  | 1730 |  | 
|  | 1731 | // Thread virtuals | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1732 |  | 
|  | 1733 | void AudioFlinger::PlaybackThread::onFirstRef() | 
|  | 1734 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1735 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1736 | } | 
|  | 1737 |  | 
|  | 1738 | // ThreadBase virtuals | 
|  | 1739 | void AudioFlinger::PlaybackThread::preExit() | 
|  | 1740 | { | 
|  | 1741 | ALOGV("  preExit()"); | 
|  | 1742 | // FIXME this is using hard-coded strings but in the future, this functionality will be | 
|  | 1743 | //       converted to use audio HAL extensions required to support tunneling | 
|  | 1744 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); | 
|  | 1745 | } | 
|  | 1746 |  | 
|  | 1747 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
|  | 1748 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( | 
|  | 1749 | const sp<AudioFlinger::Client>& client, | 
|  | 1750 | audio_stream_type_t streamType, | 
|  | 1751 | uint32_t sampleRate, | 
|  | 1752 | audio_format_t format, | 
|  | 1753 | audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1754 | size_t *pFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1755 | const sp<IMemory>& sharedBuffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1756 | audio_session_t sessionId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1757 | IAudioFlinger::track_flags_t *flags, | 
|  | 1758 | pid_t tid, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1759 | int uid, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1760 | status_t *status) | 
|  | 1761 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1762 | size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1763 | sp<Track> track; | 
|  | 1764 | status_t lStatus; | 
|  | 1765 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1766 | // client expresses a preference for FAST, but we get the final say | 
|  | 1767 | if (*flags & IAudioFlinger::TRACK_FAST) { | 
|  | 1768 | if ( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1769 | // PCM data | 
|  | 1770 | audio_is_linear_pcm(format) && | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1771 | // TODO: extract as a data library function that checks that a computationally | 
|  | 1772 | // expensive downmixer is not required: isFastOutputChannelConversion() | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1773 | (channelMask == mChannelMask || | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1774 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || | 
|  | 1775 | (channelMask == AUDIO_CHANNEL_OUT_MONO | 
|  | 1776 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1777 | // hardware sample rate | 
|  | 1778 | (sampleRate == mSampleRate) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1779 | // normal mixer has an associated fast mixer | 
|  | 1780 | hasFastMixer() && | 
|  | 1781 | // there are sufficient fast track slots available | 
|  | 1782 | (mFastTrackAvailMask != 0) | 
|  | 1783 | // FIXME test that MixerThread for this fast track has a capable output HAL | 
|  | 1784 | // FIXME add a permission test also? | 
|  | 1785 | ) { | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1786 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. | 
|  | 1787 | if (sharedBuffer == 0) { | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1788 | // read the fast track multiplier property the first time it is needed | 
|  | 1789 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); | 
|  | 1790 | if (ok != 0) { | 
|  | 1791 | ALOGE("%s pthread_once failed: %d", __func__, ok); | 
|  | 1792 | } | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1793 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1794 | } | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1795 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1796 | frameCount, mFrameCount); | 
|  | 1797 | } else { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1798 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " | 
|  | 1799 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1800 | "sampleRate=%u mSampleRate=%u " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1801 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1802 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1803 | audio_is_linear_pcm(format), | 
|  | 1804 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
|  | 1805 | *flags &= ~IAudioFlinger::TRACK_FAST; | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1806 | } | 
|  | 1807 | } | 
|  | 1808 | // For normal PCM streaming tracks, update minimum frame count. | 
|  | 1809 | // For compatibility with AudioTrack calculation, buffer depth is forced | 
|  | 1810 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. | 
|  | 1811 | // This is probably too conservative, but legacy application code may depend on it. | 
|  | 1812 | // If you change this calculation, also review the start threshold which is related. | 
|  | 1813 | if (!(*flags & IAudioFlinger::TRACK_FAST) | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1814 | && audio_has_proportional_frames(format) && sharedBuffer == 0) { | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1815 | // this must match AudioTrack.cpp calculateMinFrameCount(). | 
|  | 1816 | // TODO: Move to a common library | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1817 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); | 
|  | 1818 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); | 
|  | 1819 | if (minBufCount < 2) { | 
|  | 1820 | minBufCount = 2; | 
|  | 1821 | } | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1822 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack | 
|  | 1823 | // or the client should compute and pass in a larger buffer request. | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1824 | size_t minFrameCount = | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1825 | minBufCount * sourceFramesNeededWithTimestretch( | 
|  | 1826 | sampleRate, mNormalFrameCount, | 
|  | 1827 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1828 | if (frameCount < minFrameCount) { // including frameCount == 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1829 | frameCount = minFrameCount; | 
|  | 1830 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1831 | } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1832 | *pFrameCount = frameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1833 |  | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1834 | switch (mType) { | 
|  | 1835 |  | 
|  | 1836 | case DIRECT: | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1837 | 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] | 1838 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1839 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " | 
|  | 1840 | "for output %p with format %#x", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1841 | sampleRate, format, channelMask, mOutput, mFormat); | 
|  | 1842 | lStatus = BAD_VALUE; | 
|  | 1843 | goto Exit; | 
|  | 1844 | } | 
|  | 1845 | } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1846 | break; | 
|  | 1847 |  | 
|  | 1848 | case OFFLOAD: | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1849 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1850 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" | 
|  | 1851 | "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1852 | sampleRate, format, channelMask, mOutput, mFormat); | 
|  | 1853 | lStatus = BAD_VALUE; | 
|  | 1854 | goto Exit; | 
|  | 1855 | } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1856 | break; | 
|  | 1857 |  | 
|  | 1858 | default: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1859 | if (!audio_is_linear_pcm(format)) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1860 | ALOGE("createTrack_l() Bad parameter: format %#x \"" | 
|  | 1861 | "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1862 | format, mOutput, mFormat); | 
|  | 1863 | lStatus = BAD_VALUE; | 
|  | 1864 | goto Exit; | 
|  | 1865 | } | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1866 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
|  | 1868 | lStatus = BAD_VALUE; | 
|  | 1869 | goto Exit; | 
|  | 1870 | } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1871 | break; | 
|  | 1872 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1873 | } | 
|  | 1874 |  | 
|  | 1875 | lStatus = initCheck(); | 
|  | 1876 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1877 | ALOGE("createTrack_l() audio driver not initialized"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1878 | goto Exit; | 
|  | 1879 | } | 
|  | 1880 |  | 
|  | 1881 | { // scope for mLock | 
|  | 1882 | Mutex::Autolock _l(mLock); | 
|  | 1883 |  | 
|  | 1884 | // all tracks in same audio session must share the same routing strategy otherwise | 
|  | 1885 | // conflicts will happen when tracks are moved from one output to another by audio policy | 
|  | 1886 | // manager | 
|  | 1887 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); | 
|  | 1888 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1889 | sp<Track> t = mTracks[i]; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1890 | if (t != 0 && t->isExternalTrack()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1891 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); | 
|  | 1892 | if (sessionId == t->sessionId() && strategy != actual) { | 
|  | 1893 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
|  | 1894 | strategy, actual); | 
|  | 1895 | lStatus = BAD_VALUE; | 
|  | 1896 | goto Exit; | 
|  | 1897 | } | 
|  | 1898 | } | 
|  | 1899 | } | 
|  | 1900 |  | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1901 | track = new Track(this, client, streamType, sampleRate, format, | 
|  | 1902 | channelMask, frameCount, NULL, sharedBuffer, | 
|  | 1903 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1904 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1905 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; | 
|  | 1906 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1907 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1908 | // 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] | 1909 | goto Exit; | 
|  | 1910 | } | 
|  | 1911 | mTracks.add(track); | 
|  | 1912 |  | 
|  | 1913 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1914 | if (chain != 0) { | 
|  | 1915 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
|  | 1916 | track->setMainBuffer(chain->inBuffer()); | 
|  | 1917 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); | 
|  | 1918 | chain->incTrackCnt(); | 
|  | 1919 | } | 
|  | 1920 |  | 
|  | 1921 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
|  | 1922 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
|  | 1923 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
|  | 1924 | // so ask activity manager to do this on our behalf | 
|  | 1925 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
|  | 1926 | } | 
|  | 1927 | } | 
|  | 1928 |  | 
|  | 1929 | lStatus = NO_ERROR; | 
|  | 1930 |  | 
|  | 1931 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1932 | *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1933 | return track; | 
|  | 1934 | } | 
|  | 1935 |  | 
|  | 1936 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const | 
|  | 1937 | { | 
|  | 1938 | return latency; | 
|  | 1939 | } | 
|  | 1940 |  | 
|  | 1941 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
|  | 1942 | { | 
|  | 1943 | Mutex::Autolock _l(mLock); | 
|  | 1944 | return latency_l(); | 
|  | 1945 | } | 
|  | 1946 | uint32_t AudioFlinger::PlaybackThread::latency_l() const | 
|  | 1947 | { | 
|  | 1948 | if (initCheck() == NO_ERROR) { | 
|  | 1949 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); | 
|  | 1950 | } else { | 
|  | 1951 | return 0; | 
|  | 1952 | } | 
|  | 1953 | } | 
|  | 1954 |  | 
|  | 1955 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
|  | 1956 | { | 
|  | 1957 | Mutex::Autolock _l(mLock); | 
|  | 1958 | // Don't apply master volume in SW if our HAL can do it for us. | 
|  | 1959 | if (mOutput && mOutput->audioHwDev && | 
|  | 1960 | mOutput->audioHwDev->canSetMasterVolume()) { | 
|  | 1961 | mMasterVolume = 1.0; | 
|  | 1962 | } else { | 
|  | 1963 | mMasterVolume = value; | 
|  | 1964 | } | 
|  | 1965 | } | 
|  | 1966 |  | 
|  | 1967 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
|  | 1968 | { | 
|  | 1969 | Mutex::Autolock _l(mLock); | 
|  | 1970 | // Don't apply master mute in SW if our HAL can do it for us. | 
|  | 1971 | if (mOutput && mOutput->audioHwDev && | 
|  | 1972 | mOutput->audioHwDev->canSetMasterMute()) { | 
|  | 1973 | mMasterMute = false; | 
|  | 1974 | } else { | 
|  | 1975 | mMasterMute = muted; | 
|  | 1976 | } | 
|  | 1977 | } | 
|  | 1978 |  | 
|  | 1979 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
|  | 1980 | { | 
|  | 1981 | Mutex::Autolock _l(mLock); | 
|  | 1982 | mStreamTypes[stream].volume = value; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1983 | broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1984 | } | 
|  | 1985 |  | 
|  | 1986 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
|  | 1987 | { | 
|  | 1988 | Mutex::Autolock _l(mLock); | 
|  | 1989 | mStreamTypes[stream].mute = muted; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1990 | broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1991 | } | 
|  | 1992 |  | 
|  | 1993 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
|  | 1994 | { | 
|  | 1995 | Mutex::Autolock _l(mLock); | 
|  | 1996 | return mStreamTypes[stream].volume; | 
|  | 1997 | } | 
|  | 1998 |  | 
|  | 1999 | // addTrack_l() must be called with ThreadBase::mLock held | 
|  | 2000 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
|  | 2001 | { | 
|  | 2002 | status_t status = ALREADY_EXISTS; | 
|  | 2003 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2004 | if (mActiveTracks.indexOf(track) < 0) { | 
|  | 2005 | // the track is newly added, make sure it fills up all its | 
|  | 2006 | // buffers before playing. This is to ensure the client will | 
|  | 2007 | // effectively get the latency it requested. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2008 | if (track->isExternalTrack()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2009 | TrackBase::track_state state = track->mState; | 
|  | 2010 | mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2011 | status = AudioSystem::startOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2012 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2013 | mLock.lock(); | 
|  | 2014 | // abort track was stopped/paused while we released the lock | 
|  | 2015 | if (state != track->mState) { | 
|  | 2016 | if (status == NO_ERROR) { | 
|  | 2017 | mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2018 | AudioSystem::stopOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2019 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2020 | mLock.lock(); | 
|  | 2021 | } | 
|  | 2022 | return INVALID_OPERATION; | 
|  | 2023 | } | 
|  | 2024 | // abort if start is rejected by audio policy manager | 
|  | 2025 | if (status != NO_ERROR) { | 
|  | 2026 | return PERMISSION_DENIED; | 
|  | 2027 | } | 
|  | 2028 | #ifdef ADD_BATTERY_DATA | 
|  | 2029 | // to track the speaker usage | 
|  | 2030 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
|  | 2031 | #endif | 
|  | 2032 | } | 
|  | 2033 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2034 | // set retry count for buffer fill | 
|  | 2035 | if (track->isOffloaded()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2036 | if (track->isStopping_1()) { | 
|  | 2037 | track->mRetryCount = kMaxTrackStopRetriesOffload; | 
|  | 2038 | } else { | 
|  | 2039 | track->mRetryCount = kMaxTrackStartupRetriesOffload; | 
|  | 2040 | } | 
|  | 2041 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2042 | } else { | 
|  | 2043 | track->mRetryCount = kMaxTrackStartupRetries; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2044 | track->mFillingUpStatus = | 
|  | 2045 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2046 | } | 
|  | 2047 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2048 | track->mResetDone = false; | 
|  | 2049 | track->mPresentationCompleteFrames = 0; | 
|  | 2050 | mActiveTracks.add(track); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2051 | mWakeLockUids.add(track->uid()); | 
|  | 2052 | mActiveTracksGeneration++; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2053 | mLatestActiveTrack = track; | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2054 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 2055 | if (chain != 0) { | 
|  | 2056 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
|  | 2057 | track->sessionId()); | 
|  | 2058 | chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2059 | } | 
|  | 2060 |  | 
|  | 2061 | status = NO_ERROR; | 
|  | 2062 | } | 
|  | 2063 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2064 | onAddNewTrack_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2065 | return status; | 
|  | 2066 | } | 
|  | 2067 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2068 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2069 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2070 | track->terminate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2071 | // active tracks are removed by threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2072 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); | 
|  | 2073 | track->mState = TrackBase::STOPPED; | 
|  | 2074 | if (!trackActive) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2075 | removeTrack_l(track); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2076 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2077 | track->mState = TrackBase::STOPPING_1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2078 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2079 |  | 
|  | 2080 | return trackActive; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2081 | } | 
|  | 2082 |  | 
|  | 2083 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
|  | 2084 | { | 
|  | 2085 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
|  | 2086 | mTracks.remove(track); | 
|  | 2087 | deleteTrackName_l(track->name()); | 
|  | 2088 | // redundant as track is about to be destroyed, for dumpsys only | 
|  | 2089 | track->mName = -1; | 
|  | 2090 | if (track->isFastTrack()) { | 
|  | 2091 | int index = track->mFastIndex; | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2092 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2093 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
|  | 2094 | mFastTrackAvailMask |= 1 << index; | 
|  | 2095 | // redundant as track is about to be destroyed, for dumpsys only | 
|  | 2096 | track->mFastIndex = -1; | 
|  | 2097 | } | 
|  | 2098 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 2099 | if (chain != 0) { | 
|  | 2100 | chain->decTrackCnt(); | 
|  | 2101 | } | 
|  | 2102 | } | 
|  | 2103 |  | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2104 | void AudioFlinger::PlaybackThread::broadcast_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2105 | { | 
|  | 2106 | // Thread could be blocked waiting for async | 
|  | 2107 | // so signal it to handle state changes immediately | 
|  | 2108 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will | 
|  | 2109 | // be lost so we also flag to prevent it blocking on mWaitWorkCV | 
|  | 2110 | mSignalPending = true; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2111 | mWaitWorkCV.broadcast(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2112 | } | 
|  | 2113 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2114 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
|  | 2115 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2116 | Mutex::Autolock _l(mLock); | 
|  | 2117 | if (initCheck() != NO_ERROR) { | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2118 | return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2119 | } | 
|  | 2120 |  | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2121 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); | 
|  | 2122 | const String8 out_s8(s); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2123 | free(s); | 
|  | 2124 | return out_s8; | 
|  | 2125 | } | 
|  | 2126 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2127 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2128 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
|  | 2129 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2130 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2131 | desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2132 |  | 
|  | 2133 | switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2134 | case AUDIO_OUTPUT_OPENED: | 
|  | 2135 | case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2136 | desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2137 | desc->mChannelMask = mChannelMask; | 
|  | 2138 | desc->mSamplingRate = mSampleRate; | 
|  | 2139 | desc->mFormat = mFormat; | 
|  | 2140 | desc->mFrameCount = mNormalFrameCount; // FIXME see | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2141 | // AudioFlinger::frameCount(audio_io_handle_t) | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2142 | desc->mFrameCountHAL = mFrameCount; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2143 | desc->mLatency = latency_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2144 | break; | 
|  | 2145 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2146 | case AUDIO_OUTPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2147 | default: | 
|  | 2148 | break; | 
|  | 2149 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2150 | mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2151 | } | 
|  | 2152 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2153 | void AudioFlinger::PlaybackThread::writeCallback() | 
|  | 2154 | { | 
|  | 2155 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2156 | mCallbackThread->resetWriteBlocked(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2157 | } | 
|  | 2158 |  | 
|  | 2159 | void AudioFlinger::PlaybackThread::drainCallback() | 
|  | 2160 | { | 
|  | 2161 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2162 | mCallbackThread->resetDraining(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2163 | } | 
|  | 2164 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2165 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2166 | { | 
|  | 2167 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2168 | // reject out of sequence requests | 
|  | 2169 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { | 
|  | 2170 | mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2171 | mWaitWorkCV.signal(); | 
|  | 2172 | } | 
|  | 2173 | } | 
|  | 2174 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2175 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2176 | { | 
|  | 2177 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2178 | // reject out of sequence requests | 
|  | 2179 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { | 
|  | 2180 | mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2181 | mWaitWorkCV.signal(); | 
|  | 2182 | } | 
|  | 2183 | } | 
|  | 2184 |  | 
|  | 2185 | // static | 
|  | 2186 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2187 | void *param __unused, | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2188 | void *cookie) | 
|  | 2189 | { | 
|  | 2190 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; | 
|  | 2191 | ALOGV("asyncCallback() event %d", event); | 
|  | 2192 | switch (event) { | 
|  | 2193 | case STREAM_CBK_EVENT_WRITE_READY: | 
|  | 2194 | me->writeCallback(); | 
|  | 2195 | break; | 
|  | 2196 | case STREAM_CBK_EVENT_DRAIN_READY: | 
|  | 2197 | me->drainCallback(); | 
|  | 2198 | break; | 
|  | 2199 | default: | 
|  | 2200 | ALOGW("asyncCallback() unknown event %d", event); | 
|  | 2201 | break; | 
|  | 2202 | } | 
|  | 2203 | return 0; | 
|  | 2204 | } | 
|  | 2205 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2206 | void AudioFlinger::PlaybackThread::readOutputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2207 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2208 | // 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] | 2209 | mSampleRate = mOutput->getSampleRate(); | 
|  | 2210 | mChannelMask = mOutput->getChannelMask(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2211 | if (!audio_is_output_channel(mChannelMask)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2212 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2213 | } | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2214 | if ((mType == MIXER || mType == DUPLICATING) | 
|  | 2215 | && !isValidPcmSinkChannelMask(mChannelMask)) { | 
|  | 2216 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", | 
|  | 2217 | mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2218 | } | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2219 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2220 |  | 
|  | 2221 | // Get actual HAL format. | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2222 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2223 | // Get format from the shim, which will be different than the HAL format | 
|  | 2224 | // if playing compressed audio over HDMI passthrough. | 
|  | 2225 | mFormat = mOutput->getFormat(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2226 | if (!audio_is_valid_format(mFormat)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2227 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2228 | } | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2229 | if ((mType == MIXER || mType == DUPLICATING) | 
|  | 2230 | && !isValidPcmSinkFormat(mFormat)) { | 
|  | 2231 | LOG_FATAL("HAL format %#x not supported for mixed output", | 
|  | 2232 | mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2233 | } | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2234 | mFrameSize = mOutput->getFrameSize(); | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2235 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); | 
|  | 2236 | mFrameCount = mBufferSize / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2237 | if (mFrameCount & 15) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2238 | 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] | 2239 | mFrameCount); | 
|  | 2240 | } | 
|  | 2241 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2242 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && | 
|  | 2243 | (mOutput->stream->set_callback != NULL)) { | 
|  | 2244 | if (mOutput->stream->set_callback(mOutput->stream, | 
|  | 2245 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { | 
|  | 2246 | mUseAsyncWrite = true; | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2247 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2248 | } | 
|  | 2249 | } | 
|  | 2250 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2251 | mHwSupportsPause = false; | 
|  | 2252 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
|  | 2253 | if (mOutput->stream->pause != NULL) { | 
|  | 2254 | if (mOutput->stream->resume != NULL) { | 
|  | 2255 | mHwSupportsPause = true; | 
|  | 2256 | } else { | 
|  | 2257 | ALOGW("direct output implements pause but not resume"); | 
|  | 2258 | } | 
|  | 2259 | } else if (mOutput->stream->resume != NULL) { | 
|  | 2260 | ALOGW("direct output implements resume but not pause"); | 
|  | 2261 | } | 
|  | 2262 | } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2263 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { | 
|  | 2264 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); | 
|  | 2265 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2266 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2267 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { | 
|  | 2268 | // For best precision, we use float instead of the associated output | 
|  | 2269 | // device format (typically PCM 16 bit). | 
|  | 2270 |  | 
|  | 2271 | mFormat = AUDIO_FORMAT_PCM_FLOAT; | 
|  | 2272 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
|  | 2273 | mBufferSize = mFrameSize * mFrameCount; | 
|  | 2274 |  | 
|  | 2275 | // TODO: We currently use the associated output device channel mask and sample rate. | 
|  | 2276 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads | 
|  | 2277 | // (if a valid mask) to avoid premature downmix. | 
|  | 2278 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads | 
|  | 2279 | // instead of the output device sample rate to avoid loss of high frequency information. | 
|  | 2280 | // This may need to be updated as MixerThread/OutputTracks are added and not here. | 
|  | 2281 | } | 
|  | 2282 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2283 | // 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] | 2284 | double multiplier = 1.0; | 
|  | 2285 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
|  | 2286 | kUseFastMixer == FastMixer_Dynamic)) { | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2287 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
|  | 2288 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2289 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
|  | 2290 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
|  | 2291 | maxNormalFrameCount = maxNormalFrameCount & ~15; | 
|  | 2292 | if (maxNormalFrameCount < minNormalFrameCount) { | 
|  | 2293 | maxNormalFrameCount = minNormalFrameCount; | 
|  | 2294 | } | 
|  | 2295 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
|  | 2296 | if (multiplier <= 1.0) { | 
|  | 2297 | multiplier = 1.0; | 
|  | 2298 | } else if (multiplier <= 2.0) { | 
|  | 2299 | if (2 * mFrameCount <= maxNormalFrameCount) { | 
|  | 2300 | multiplier = 2.0; | 
|  | 2301 | } else { | 
|  | 2302 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
|  | 2303 | } | 
|  | 2304 | } else { | 
|  | 2305 | // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2306 | // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2307 | // track, but we sometimes have to do this to satisfy the maximum frame count | 
|  | 2308 | // constraint) | 
|  | 2309 | // FIXME this rounding up should not be done if no HAL SRC | 
|  | 2310 | uint32_t truncMult = (uint32_t) multiplier; | 
|  | 2311 | if ((truncMult & 1)) { | 
|  | 2312 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { | 
|  | 2313 | ++truncMult; | 
|  | 2314 | } | 
|  | 2315 | } | 
|  | 2316 | multiplier = (double) truncMult; | 
|  | 2317 | } | 
|  | 2318 | } | 
|  | 2319 | mNormalFrameCount = multiplier * mFrameCount; | 
|  | 2320 | // round up to nearest 16 frames to satisfy AudioMixer | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2321 | if (mType == MIXER || mType == DUPLICATING) { | 
|  | 2322 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
|  | 2323 | } | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2324 | 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] | 2325 | mNormalFrameCount); | 
|  | 2326 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2327 | // Check if we want to throttle the processing to no more than 2x normal rate | 
|  | 2328 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2329 | mThreadThrottleTimeMs = 0; | 
|  | 2330 | mThreadThrottleEndMs = 0; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2331 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); | 
|  | 2332 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2333 | // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames. | 
|  | 2334 | // Originally this was int16_t[] array, need to remove legacy implications. | 
|  | 2335 | free(mSinkBuffer); | 
|  | 2336 | mSinkBuffer = NULL; | 
| Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2337 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems | 
|  | 2338 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. | 
|  | 2339 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2340 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2341 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2342 | // We resize the mMixerBuffer according to the requirements of the sink buffer which | 
|  | 2343 | // drives the output. | 
|  | 2344 | free(mMixerBuffer); | 
|  | 2345 | mMixerBuffer = NULL; | 
|  | 2346 | if (mMixerBufferEnabled) { | 
|  | 2347 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. | 
|  | 2348 | mMixerBufferSize = mNormalFrameCount * mChannelCount | 
|  | 2349 | * audio_bytes_per_sample(mMixerBufferFormat); | 
|  | 2350 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); | 
|  | 2351 | } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2352 | free(mEffectBuffer); | 
|  | 2353 | mEffectBuffer = NULL; | 
|  | 2354 | if (mEffectBufferEnabled) { | 
|  | 2355 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only | 
|  | 2356 | mEffectBufferSize = mNormalFrameCount * mChannelCount | 
|  | 2357 | * audio_bytes_per_sample(mEffectBufferFormat); | 
|  | 2358 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); | 
|  | 2359 | } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2360 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2361 | // force reconfiguration of effect chains and engines to take new buffer size and audio | 
|  | 2362 | // parameters into account | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2363 | // 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] | 2364 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
|  | 2365 | // matter. | 
|  | 2366 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
|  | 2367 | Vector< sp<EffectChain> > effectChains = mEffectChains; | 
|  | 2368 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 2369 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
|  | 2370 | } | 
|  | 2371 | } | 
|  | 2372 |  | 
|  | 2373 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2374 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2375 | { | 
|  | 2376 | if (halFrames == NULL || dspFrames == NULL) { | 
|  | 2377 | return BAD_VALUE; | 
|  | 2378 | } | 
|  | 2379 | Mutex::Autolock _l(mLock); | 
|  | 2380 | if (initCheck() != NO_ERROR) { | 
|  | 2381 | return INVALID_OPERATION; | 
|  | 2382 | } | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2383 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2384 | *halFrames = framesWritten; | 
|  | 2385 |  | 
|  | 2386 | if (isSuspended()) { | 
|  | 2387 | // return an estimation of rendered frames when the output is suspended | 
|  | 2388 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2389 | *dspFrames = (uint32_t) | 
|  | 2390 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2391 | return NO_ERROR; | 
|  | 2392 | } else { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2393 | status_t status; | 
|  | 2394 | uint32_t frames; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2395 | status = mOutput->getRenderPosition(&frames); | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2396 | *dspFrames = (size_t)frames; | 
|  | 2397 | return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2398 | } | 
|  | 2399 | } | 
|  | 2400 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2401 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2402 | { | 
|  | 2403 | Mutex::Autolock _l(mLock); | 
|  | 2404 | uint32_t result = 0; | 
|  | 2405 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 2406 | result = EFFECT_SESSION; | 
|  | 2407 | } | 
|  | 2408 |  | 
|  | 2409 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2410 | sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2411 | if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2412 | result |= TRACK_SESSION; | 
|  | 2413 | break; | 
|  | 2414 | } | 
|  | 2415 | } | 
|  | 2416 |  | 
|  | 2417 | return result; | 
|  | 2418 | } | 
|  | 2419 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2420 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2421 | { | 
|  | 2422 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
|  | 2423 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
|  | 2424 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 2425 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
|  | 2426 | } | 
|  | 2427 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 2428 | sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2429 | if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2430 | return AudioSystem::getStrategyForStream(track->streamType()); | 
|  | 2431 | } | 
|  | 2432 | } | 
|  | 2433 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
|  | 2434 | } | 
|  | 2435 |  | 
|  | 2436 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2437 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2438 | { | 
|  | 2439 | Mutex::Autolock _l(mLock); | 
|  | 2440 | return mOutput; | 
|  | 2441 | } | 
|  | 2442 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2443 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2444 | { | 
|  | 2445 | Mutex::Autolock _l(mLock); | 
|  | 2446 | AudioStreamOut *output = mOutput; | 
|  | 2447 | mOutput = NULL; | 
|  | 2448 | // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
|  | 2449 | //       must push a NULL and wait for ack | 
|  | 2450 | mOutputSink.clear(); | 
|  | 2451 | mPipeSink.clear(); | 
|  | 2452 | mNormalSink.clear(); | 
|  | 2453 | return output; | 
|  | 2454 | } | 
|  | 2455 |  | 
|  | 2456 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
|  | 2457 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const | 
|  | 2458 | { | 
|  | 2459 | if (mOutput == NULL) { | 
|  | 2460 | return NULL; | 
|  | 2461 | } | 
|  | 2462 | return &mOutput->stream->common; | 
|  | 2463 | } | 
|  | 2464 |  | 
|  | 2465 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const | 
|  | 2466 | { | 
|  | 2467 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 2468 | } | 
|  | 2469 |  | 
|  | 2470 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
|  | 2471 | { | 
|  | 2472 | if (!isValidSyncEvent(event)) { | 
|  | 2473 | return BAD_VALUE; | 
|  | 2474 | } | 
|  | 2475 |  | 
|  | 2476 | Mutex::Autolock _l(mLock); | 
|  | 2477 |  | 
|  | 2478 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2479 | sp<Track> track = mTracks[i]; | 
|  | 2480 | if (event->triggerSession() == track->sessionId()) { | 
|  | 2481 | (void) track->setSyncEvent(event); | 
|  | 2482 | return NO_ERROR; | 
|  | 2483 | } | 
|  | 2484 | } | 
|  | 2485 |  | 
|  | 2486 | return NAME_NOT_FOUND; | 
|  | 2487 | } | 
|  | 2488 |  | 
|  | 2489 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
|  | 2490 | { | 
|  | 2491 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
|  | 2492 | } | 
|  | 2493 |  | 
|  | 2494 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( | 
|  | 2495 | const Vector< sp<Track> >& tracksToRemove) | 
|  | 2496 | { | 
|  | 2497 | size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2498 | if (count > 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2499 | for (size_t i = 0 ; i < count ; i++) { | 
|  | 2500 | const sp<Track>& track = tracksToRemove.itemAt(i); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2501 | if (track->isExternalTrack()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2502 | AudioSystem::stopOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2503 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2504 | #ifdef ADD_BATTERY_DATA | 
|  | 2505 | // to track the speaker usage | 
|  | 2506 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
|  | 2507 | #endif | 
|  | 2508 | if (track->isTerminated()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2509 | AudioSystem::releaseOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2510 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2511 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2512 | } | 
|  | 2513 | } | 
|  | 2514 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2515 | } | 
|  | 2516 |  | 
|  | 2517 | void AudioFlinger::PlaybackThread::checkSilentMode_l() | 
|  | 2518 | { | 
|  | 2519 | if (!mMasterMute) { | 
|  | 2520 | char value[PROPERTY_VALUE_MAX]; | 
| Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2521 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { | 
|  | 2522 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); | 
|  | 2523 | return; | 
|  | 2524 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2525 | if (property_get("ro.audio.silent", value, "0") > 0) { | 
|  | 2526 | char *endptr; | 
|  | 2527 | unsigned long ul = strtoul(value, &endptr, 0); | 
|  | 2528 | if (*endptr == '\0' && ul != 0) { | 
|  | 2529 | ALOGD("Silence is golden"); | 
|  | 2530 | // The setprop command will not allow a property to be changed after | 
|  | 2531 | // the first time it is set, so we don't have to worry about un-muting. | 
|  | 2532 | setMasterMute_l(true); | 
|  | 2533 | } | 
|  | 2534 | } | 
|  | 2535 | } | 
|  | 2536 | } | 
|  | 2537 |  | 
|  | 2538 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2539 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2540 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2541 | mInWrite = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2542 | ssize_t bytesWritten; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2543 | const size_t offset = mCurrentWriteLength - mBytesRemaining; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2544 |  | 
|  | 2545 | // If an NBAIO sink is present, use it to write the normal mixer's submix | 
|  | 2546 | if (mNormalSink != 0) { | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2547 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2548 | const size_t count = mBytesRemaining / mFrameSize; | 
|  | 2549 |  | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2550 | ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2551 | // update the setpoint when AudioFlinger::mScreenState changes | 
|  | 2552 | uint32_t screenState = AudioFlinger::mScreenState; | 
|  | 2553 | if (screenState != mScreenState) { | 
|  | 2554 | mScreenState = screenState; | 
|  | 2555 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
|  | 2556 | if (pipe != NULL) { | 
|  | 2557 | pipe->setAvgFrames((mScreenState & 1) ? | 
|  | 2558 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
|  | 2559 | } | 
|  | 2560 | } | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2561 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2562 | ATRACE_END(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2563 | if (framesWritten > 0) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2564 | bytesWritten = framesWritten * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2565 | } else { | 
|  | 2566 | bytesWritten = framesWritten; | 
|  | 2567 | } | 
|  | 2568 | // otherwise use the HAL / AudioStreamOut directly | 
|  | 2569 | } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2570 | // Direct output and offload threads | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2571 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2572 | if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2573 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); | 
|  | 2574 | mWriteAckSequence += 2; | 
|  | 2575 | mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2576 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2577 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2578 | } | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2579 | // FIXME We should have an implementation of timestamps for direct output threads. | 
|  | 2580 | // They are used e.g for multichannel PCM playback over HDMI. | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2581 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2582 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2583 | if (mUseAsyncWrite && | 
|  | 2584 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { | 
|  | 2585 | // 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] | 2586 | mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2587 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2588 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2589 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2590 | } | 
|  | 2591 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2592 | mNumWrites++; | 
|  | 2593 | mInWrite = false; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2594 | mStandby = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2595 | return bytesWritten; | 
|  | 2596 | } | 
|  | 2597 |  | 
|  | 2598 | void AudioFlinger::PlaybackThread::threadLoop_drain() | 
|  | 2599 | { | 
|  | 2600 | if (mOutput->stream->drain) { | 
|  | 2601 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); | 
|  | 2602 | if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2603 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); | 
|  | 2604 | mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2605 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2606 | mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2607 | } | 
|  | 2608 | mOutput->stream->drain(mOutput->stream, | 
|  | 2609 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY | 
|  | 2610 | : AUDIO_DRAIN_ALL); | 
|  | 2611 | } | 
|  | 2612 | } | 
|  | 2613 |  | 
|  | 2614 | void AudioFlinger::PlaybackThread::threadLoop_exit() | 
|  | 2615 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2616 | { | 
|  | 2617 | Mutex::Autolock _l(mLock); | 
|  | 2618 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 2619 | sp<Track> track = mTracks[i]; | 
|  | 2620 | track->invalidate(); | 
|  | 2621 | } | 
|  | 2622 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2623 | } | 
|  | 2624 |  | 
|  | 2625 | /* | 
|  | 2626 | The derived values that are cached: | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2627 | - mSinkBufferSize from frame count * frame size | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2628 | - mActiveSleepTimeUs from activeSleepTimeUs() | 
|  | 2629 | - mIdleSleepTimeUs from idleSleepTimeUs() | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2630 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least | 
|  | 2631 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2632 | - maxPeriod from frame count and sample rate (MIXER only) | 
|  | 2633 |  | 
|  | 2634 | The parameters that affect these derived values are: | 
|  | 2635 | - frame count | 
|  | 2636 | - frame size | 
|  | 2637 | - sample rate | 
|  | 2638 | - device type: A2DP or not | 
|  | 2639 | - device latency | 
|  | 2640 | - format: PCM or not | 
|  | 2641 | - active sleep time | 
|  | 2642 | - idle sleep time | 
|  | 2643 | */ | 
|  | 2644 |  | 
|  | 2645 | void AudioFlinger::PlaybackThread::cacheParameters_l() | 
|  | 2646 | { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2647 | mSinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2648 | mActiveSleepTimeUs = activeSleepTimeUs(); | 
|  | 2649 | mIdleSleepTimeUs = idleSleepTimeUs(); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2650 |  | 
|  | 2651 | // make sure standby delay is not too short when connected to an A2DP sink to avoid | 
|  | 2652 | // truncating audio when going to standby. | 
|  | 2653 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; | 
|  | 2654 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { | 
|  | 2655 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { | 
|  | 2656 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; | 
|  | 2657 | } | 
|  | 2658 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2659 | } | 
|  | 2660 |  | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2661 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2662 | { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2663 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2664 | this,  streamType, mTracks.size()); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2665 | bool trackMatch = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2666 | size_t size = mTracks.size(); | 
|  | 2667 | for (size_t i = 0; i < size; i++) { | 
|  | 2668 | sp<Track> t = mTracks[i]; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2669 | if (t->streamType() == streamType && t->isExternalTrack()) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2670 | t->invalidate(); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2671 | trackMatch = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2672 | } | 
|  | 2673 | } | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2674 | return trackMatch; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2675 | } | 
|  | 2676 |  | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2677 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
|  | 2678 | { | 
|  | 2679 | Mutex::Autolock _l(mLock); | 
|  | 2680 | invalidateTracks_l(streamType); | 
|  | 2681 | } | 
|  | 2682 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2683 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 2684 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2685 | audio_session_t session = chain->sessionId(); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2686 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled | 
|  | 2687 | ? mEffectBuffer : mSinkBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2688 | bool ownsBuffer = false; | 
|  | 2689 |  | 
|  | 2690 | 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] | 2691 | if (session > AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2692 | // 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] | 2693 | // the sink buffer as input | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2694 | if (mType != DIRECT) { | 
|  | 2695 | size_t numSamples = mNormalFrameCount * mChannelCount; | 
|  | 2696 | buffer = new int16_t[numSamples]; | 
|  | 2697 | memset(buffer, 0, numSamples * sizeof(int16_t)); | 
|  | 2698 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); | 
|  | 2699 | ownsBuffer = true; | 
|  | 2700 | } | 
|  | 2701 |  | 
|  | 2702 | // Attach all tracks with same session ID to this chain. | 
|  | 2703 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2704 | sp<Track> track = mTracks[i]; | 
|  | 2705 | if (session == track->sessionId()) { | 
|  | 2706 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), | 
|  | 2707 | buffer); | 
|  | 2708 | track->setMainBuffer(buffer); | 
|  | 2709 | chain->incTrackCnt(); | 
|  | 2710 | } | 
|  | 2711 | } | 
|  | 2712 |  | 
|  | 2713 | // indicate all active tracks in the chain | 
|  | 2714 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
|  | 2715 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 2716 | if (track == 0) { | 
|  | 2717 | continue; | 
|  | 2718 | } | 
|  | 2719 | if (session == track->sessionId()) { | 
|  | 2720 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
|  | 2721 | chain->incActiveTrackCnt(); | 
|  | 2722 | } | 
|  | 2723 | } | 
|  | 2724 | } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2725 | chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2726 | chain->setInBuffer(buffer, ownsBuffer); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2727 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled | 
|  | 2728 | ? mEffectBuffer : mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2729 | // 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] | 2730 | // 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] | 2731 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
|  | 2732 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2733 | // after track specific effects and before output stage. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2734 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2735 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2736 | // Effect chain for other sessions are inserted at beginning of effect | 
|  | 2737 | // 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] | 2738 | // sessions is not important. | 
|  | 2739 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && | 
|  | 2740 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, | 
|  | 2741 | "audio_session_t constants misdefined"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2742 | size_t size = mEffectChains.size(); | 
|  | 2743 | size_t i = 0; | 
|  | 2744 | for (i = 0; i < size; i++) { | 
|  | 2745 | if (mEffectChains[i]->sessionId() < session) { | 
|  | 2746 | break; | 
|  | 2747 | } | 
|  | 2748 | } | 
|  | 2749 | mEffectChains.insertAt(chain, i); | 
|  | 2750 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 2751 |  | 
|  | 2752 | return NO_ERROR; | 
|  | 2753 | } | 
|  | 2754 |  | 
|  | 2755 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 2756 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2757 | audio_session_t session = chain->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2758 |  | 
|  | 2759 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
|  | 2760 |  | 
|  | 2761 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 2762 | if (chain == mEffectChains[i]) { | 
|  | 2763 | mEffectChains.removeAt(i); | 
|  | 2764 | // detach all active tracks from the chain | 
|  | 2765 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
|  | 2766 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 2767 | if (track == 0) { | 
|  | 2768 | continue; | 
|  | 2769 | } | 
|  | 2770 | if (session == track->sessionId()) { | 
|  | 2771 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
|  | 2772 | chain.get(), session); | 
|  | 2773 | chain->decActiveTrackCnt(); | 
|  | 2774 | } | 
|  | 2775 | } | 
|  | 2776 |  | 
|  | 2777 | // detach all tracks with same session ID from this chain | 
|  | 2778 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2779 | sp<Track> track = mTracks[i]; | 
|  | 2780 | if (session == track->sessionId()) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2781 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2782 | chain->decTrackCnt(); | 
|  | 2783 | } | 
|  | 2784 | } | 
|  | 2785 | break; | 
|  | 2786 | } | 
|  | 2787 | } | 
|  | 2788 | return mEffectChains.size(); | 
|  | 2789 | } | 
|  | 2790 |  | 
|  | 2791 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
|  | 2792 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
|  | 2793 | { | 
|  | 2794 | Mutex::Autolock _l(mLock); | 
|  | 2795 | return attachAuxEffect_l(track, EffectId); | 
|  | 2796 | } | 
|  | 2797 |  | 
|  | 2798 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
|  | 2799 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
|  | 2800 | { | 
|  | 2801 | status_t status = NO_ERROR; | 
|  | 2802 |  | 
|  | 2803 | if (EffectId == 0) { | 
|  | 2804 | track->setAuxBuffer(0, NULL); | 
|  | 2805 | } else { | 
|  | 2806 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
|  | 2807 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
|  | 2808 | if (effect != 0) { | 
|  | 2809 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 2810 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
|  | 2811 | } else { | 
|  | 2812 | status = INVALID_OPERATION; | 
|  | 2813 | } | 
|  | 2814 | } else { | 
|  | 2815 | status = BAD_VALUE; | 
|  | 2816 | } | 
|  | 2817 | } | 
|  | 2818 | return status; | 
|  | 2819 | } | 
|  | 2820 |  | 
|  | 2821 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
|  | 2822 | { | 
|  | 2823 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2824 | sp<Track> track = mTracks[i]; | 
|  | 2825 | if (track->auxEffectId() == effectId) { | 
|  | 2826 | attachAuxEffect_l(track, 0); | 
|  | 2827 | } | 
|  | 2828 | } | 
|  | 2829 | } | 
|  | 2830 |  | 
|  | 2831 | bool AudioFlinger::PlaybackThread::threadLoop() | 
|  | 2832 | { | 
|  | 2833 | Vector< sp<Track> > tracksToRemove; | 
|  | 2834 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2835 | mStandbyTimeNs = systemTime(); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2836 | nsecs_t lastWriteFinished = -1; // time last server write completed | 
|  | 2837 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2838 |  | 
|  | 2839 | // MIXER | 
|  | 2840 | nsecs_t lastWarning = 0; | 
|  | 2841 |  | 
|  | 2842 | // DUPLICATING | 
|  | 2843 | // FIXME could this be made local to while loop? | 
|  | 2844 | writeFrames = 0; | 
|  | 2845 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2846 | int lastGeneration = 0; | 
|  | 2847 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2848 | cacheParameters_l(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2849 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2850 |  | 
|  | 2851 | if (mType == MIXER) { | 
|  | 2852 | sleepTimeShift = 0; | 
|  | 2853 | } | 
|  | 2854 |  | 
|  | 2855 | CpuStats cpuStats; | 
|  | 2856 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
|  | 2857 |  | 
|  | 2858 | acquireWakeLock(); | 
|  | 2859 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2860 | // mNBLogWriter->log can only be called while thread mutex mLock is held. | 
|  | 2861 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
|  | 2862 | // and then that string will be logged at the next convenient opportunity. | 
|  | 2863 | const char *logString = NULL; | 
|  | 2864 |  | 
| Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2865 | checkSilentMode_l(); | 
|  | 2866 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2867 | while (!exitPending()) | 
|  | 2868 | { | 
|  | 2869 | cpuStats.sample(myName); | 
|  | 2870 |  | 
|  | 2871 | Vector< sp<EffectChain> > effectChains; | 
|  | 2872 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2873 | { // scope for mLock | 
|  | 2874 |  | 
|  | 2875 | Mutex::Autolock _l(mLock); | 
|  | 2876 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2877 | processConfigEvents_l(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2878 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2879 | if (logString != NULL) { | 
|  | 2880 | mNBLogWriter->logTimestamp(); | 
|  | 2881 | mNBLogWriter->log(logString); | 
|  | 2882 | logString = NULL; | 
|  | 2883 | } | 
|  | 2884 |  | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2885 | // Gather the framesReleased counters for all active tracks, | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2886 | // and associate with the sink frames written out.  We need | 
|  | 2887 | // this to convert the sink timestamp to the track timestamp. | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2888 | bool kernelLocationUpdate = false; | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2889 | if (mNormalSink != 0) { | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2890 | // Note: The DuplicatingThread may not have a mNormalSink. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2891 | // We always fetch the timestamp here because often the downstream | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2892 | // sink will block while writing. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2893 | ExtendedTimestamp timestamp; // use private copy to fetch | 
|  | 2894 | (void) mNormalSink->getTimestamp(timestamp); | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 2895 |  | 
|  | 2896 | // We keep track of the last valid kernel position in case we are in underrun | 
|  | 2897 | // and the normal mixer period is the same as the fast mixer period, or there | 
|  | 2898 | // is some error from the HAL. | 
|  | 2899 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
|  | 2900 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
|  | 2901 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; | 
|  | 2902 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
|  | 2903 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
|  | 2904 |  | 
|  | 2905 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
|  | 2906 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; | 
|  | 2907 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
|  | 2908 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2909 | } | 
|  | 2910 |  | 
|  | 2911 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
|  | 2912 | kernelLocationUpdate = true; | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 2913 | } else { | 
|  | 2914 | ALOGV("getTimestamp error - no valid kernel position"); | 
|  | 2915 | } | 
|  | 2916 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2917 | // copy over kernel info | 
|  | 2918 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = | 
| Andy Hung | 9ebe29b | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 2919 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
|  | 2920 | + mSuspendedFrames; // add frames discarded when suspended | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2921 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = | 
|  | 2922 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2923 | } | 
|  | 2924 | // mFramesWritten for non-offloaded tracks are contiguous | 
|  | 2925 | // even after standby() is called. This is useful for the track frame | 
|  | 2926 | // to sink frame mapping. | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2927 | bool serverLocationUpdate = false; | 
|  | 2928 | if (mFramesWritten != lastFramesWritten) { | 
|  | 2929 | serverLocationUpdate = true; | 
|  | 2930 | lastFramesWritten = mFramesWritten; | 
|  | 2931 | } | 
|  | 2932 | // Only update timestamps if there is a meaningful change. | 
|  | 2933 | // Either the kernel timestamp must be valid or we have written something. | 
|  | 2934 | if (kernelLocationUpdate || serverLocationUpdate) { | 
|  | 2935 | if (serverLocationUpdate) { | 
|  | 2936 | // use the time before we called the HAL write - it is a bit more accurate | 
|  | 2937 | // to when the server last read data than the current time here. | 
|  | 2938 | // | 
|  | 2939 | // If we haven't written anything, mLastWriteTime will be -1 | 
|  | 2940 | // and we use systemTime(). | 
|  | 2941 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; | 
|  | 2942 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 | 
|  | 2943 | ? systemTime() : mLastWriteTime; | 
|  | 2944 | } | 
|  | 2945 | const size_t size = mActiveTracks.size(); | 
|  | 2946 | for (size_t i = 0; i < size; ++i) { | 
|  | 2947 | sp<Track> t = mActiveTracks[i].promote(); | 
|  | 2948 | if (t != 0 && !t->isFastTrack()) { | 
|  | 2949 | t->updateTrackFrameInfo( | 
|  | 2950 | t->mAudioTrackServerProxy->framesReleased(), | 
|  | 2951 | mFramesWritten, | 
|  | 2952 | mTimestamp); | 
|  | 2953 | } | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2954 | } | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2955 | } | 
|  | 2956 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2957 | saveOutputTracks(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2958 | if (mSignalPending) { | 
|  | 2959 | // A signal was raised while we were unlocked | 
|  | 2960 | mSignalPending = false; | 
|  | 2961 | } else if (waitingAsyncCallback_l()) { | 
|  | 2962 | if (exitPending()) { | 
|  | 2963 | break; | 
|  | 2964 | } | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2965 | bool released = false; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 2966 | if (!keepWakeLock()) { | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2967 | releaseWakeLock_l(); | 
|  | 2968 | released = true; | 
|  | 2969 | } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2970 | mWakeLockUids.clear(); | 
|  | 2971 | mActiveTracksGeneration++; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2972 | ALOGV("wait async completion"); | 
|  | 2973 | mWaitWorkCV.wait(mLock); | 
|  | 2974 | ALOGV("async completion/wake"); | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2975 | if (released) { | 
|  | 2976 | acquireWakeLock_l(); | 
|  | 2977 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2978 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
|  | 2979 | mSleepTimeUs = 0; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2980 |  | 
|  | 2981 | continue; | 
|  | 2982 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2983 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2984 | isSuspended()) { | 
|  | 2985 | // put audio hardware into standby after short delay | 
|  | 2986 | if (shouldStandby_l()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2987 |  | 
|  | 2988 | threadLoop_standby(); | 
|  | 2989 |  | 
|  | 2990 | mStandby = true; | 
|  | 2991 | } | 
|  | 2992 |  | 
|  | 2993 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 2994 | // we're about to wait, flush the binder command buffer | 
|  | 2995 | IPCThreadState::self()->flushCommands(); | 
|  | 2996 |  | 
|  | 2997 | clearOutputTracks(); | 
|  | 2998 |  | 
|  | 2999 | if (exitPending()) { | 
|  | 3000 | break; | 
|  | 3001 | } | 
|  | 3002 |  | 
|  | 3003 | releaseWakeLock_l(); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3004 | mWakeLockUids.clear(); | 
|  | 3005 | mActiveTracksGeneration++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3006 | // wait until we have something to do... | 
|  | 3007 | ALOGV("%s going to sleep", myName.string()); | 
|  | 3008 | mWaitWorkCV.wait(mLock); | 
|  | 3009 | ALOGV("%s waking up", myName.string()); | 
|  | 3010 | acquireWakeLock_l(); | 
|  | 3011 |  | 
|  | 3012 | mMixerStatus = MIXER_IDLE; | 
|  | 3013 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
|  | 3014 | mBytesWritten = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3015 | mBytesRemaining = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3016 | checkSilentMode_l(); | 
|  | 3017 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3018 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
|  | 3019 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3020 | if (mType == MIXER) { | 
|  | 3021 | sleepTimeShift = 0; | 
|  | 3022 | } | 
|  | 3023 |  | 
|  | 3024 | continue; | 
|  | 3025 | } | 
|  | 3026 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3027 | // mMixerStatusIgnoringFastTracks is also updated internally | 
|  | 3028 | mMixerStatus = prepareTracks_l(&tracksToRemove); | 
|  | 3029 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3030 | // compare with previously applied list | 
|  | 3031 | if (lastGeneration != mActiveTracksGeneration) { | 
|  | 3032 | // update wakelock | 
|  | 3033 | updateWakeLockUids_l(mWakeLockUids); | 
|  | 3034 | lastGeneration = mActiveTracksGeneration; | 
|  | 3035 | } | 
|  | 3036 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3037 | // prevent any changes in effect chain list and in each effect chain | 
|  | 3038 | // during mixing and effect process as the audio buffers could be deleted | 
|  | 3039 | // or modified if an effect is created or deleted | 
|  | 3040 | lockEffectChains_l(effectChains); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3041 | } // mLock scope ends | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3042 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3043 | if (mBytesRemaining == 0) { | 
|  | 3044 | mCurrentWriteLength = 0; | 
|  | 3045 | if (mMixerStatus == MIXER_TRACKS_READY) { | 
|  | 3046 | // threadLoop_mix() sets mCurrentWriteLength | 
|  | 3047 | threadLoop_mix(); | 
|  | 3048 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) | 
|  | 3049 | && (mMixerStatus != MIXER_DRAIN_ALL)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3050 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3051 | // must be written to HAL | 
|  | 3052 | threadLoop_sleepTime(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3053 | if (mSleepTimeUs == 0) { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3054 | mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3055 | } | 
|  | 3056 | } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3057 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3058 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3059 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) | 
|  | 3060 | // or mSinkBuffer (if there are no effects). | 
|  | 3061 | // | 
|  | 3062 | // This is done pre-effects computation; if effects change to | 
|  | 3063 | // support higher precision, this needs to move. | 
|  | 3064 | // | 
|  | 3065 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3066 | // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3067 | if (mMixerBufferValid) { | 
|  | 3068 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; | 
|  | 3069 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; | 
|  | 3070 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3071 | // mono blend occurs for mixer threads only (not direct or offloaded) | 
|  | 3072 | // and is handled here if we're going directly to the sink. | 
|  | 3073 | if (requireMonoBlend() && !mEffectBufferValid) { | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3074 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, | 
|  | 3075 | true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3076 | } | 
|  | 3077 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3078 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, | 
|  | 3079 | mNormalFrameCount * mChannelCount); | 
|  | 3080 | } | 
|  | 3081 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3082 | mBytesRemaining = mCurrentWriteLength; | 
|  | 3083 | if (isSuspended()) { | 
| Andy Hung | 9ebe29b | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3084 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). | 
|  | 3085 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. | 
|  | 3086 | const size_t framesRemaining = mBytesRemaining / mFrameSize; | 
|  | 3087 | mBytesWritten += mBytesRemaining; | 
|  | 3088 | mFramesWritten += framesRemaining; | 
|  | 3089 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3090 | mBytesRemaining = 0; | 
|  | 3091 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3092 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3093 | // only process effects if we're going to write | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3094 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3095 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 3096 | effectChains[i]->process_l(); | 
|  | 3097 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3098 | } | 
|  | 3099 | } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3100 | // Process effect chains for offloaded thread even if no audio | 
|  | 3101 | // was read from audio track: process only updates effect state | 
|  | 3102 | // and thus does have to be synchronized with audio writes but may have | 
|  | 3103 | // to be called while waiting for async write callback | 
|  | 3104 | if (mType == OFFLOAD) { | 
|  | 3105 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 3106 | effectChains[i]->process_l(); | 
|  | 3107 | } | 
|  | 3108 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3109 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3110 | // Only if the Effects buffer is enabled and there is data in the | 
|  | 3111 | // Effects buffer (buffer valid), we need to | 
|  | 3112 | // copy into the sink buffer. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3113 | // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3114 | if (mEffectBufferValid) { | 
|  | 3115 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3116 |  | 
|  | 3117 | if (requireMonoBlend()) { | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3118 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, | 
|  | 3119 | true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3120 | } | 
|  | 3121 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3122 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, | 
|  | 3123 | mNormalFrameCount * mChannelCount); | 
|  | 3124 | } | 
|  | 3125 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3126 | // enable changes in effect chain | 
|  | 3127 | unlockEffectChains(effectChains); | 
|  | 3128 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3129 | if (!waitingAsyncCallback()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3130 | // mSleepTimeUs == 0 means we must write to audio hardware | 
|  | 3131 | if (mSleepTimeUs == 0) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3132 | ssize_t ret = 0; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3133 | // We save lastWriteFinished here, as previousLastWriteFinished, | 
|  | 3134 | // for throttling. On thread start, previousLastWriteFinished will be | 
|  | 3135 | // set to -1, which properly results in no throttling after the first write. | 
|  | 3136 | nsecs_t previousLastWriteFinished = lastWriteFinished; | 
|  | 3137 | nsecs_t delta = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3138 | if (mBytesRemaining) { | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3139 | // FIXME rewrite to reduce number of system calls | 
|  | 3140 | mLastWriteTime = systemTime();  // also used for dumpsys | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3141 | ret = threadLoop_write(); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3142 | lastWriteFinished = systemTime(); | 
|  | 3143 | delta = lastWriteFinished - mLastWriteTime; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3144 | if (ret < 0) { | 
|  | 3145 | mBytesRemaining = 0; | 
|  | 3146 | } else { | 
|  | 3147 | mBytesWritten += ret; | 
|  | 3148 | mBytesRemaining -= ret; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3149 | mFramesWritten += ret / mFrameSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3150 | } | 
|  | 3151 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || | 
|  | 3152 | (mMixerStatus == MIXER_DRAIN_ALL)) { | 
|  | 3153 | threadLoop_drain(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3154 | } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3155 | if (mType == MIXER && !mStandby) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3156 | // write blocked detection | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3157 | if (delta > maxPeriod) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3158 | mNumDelayedWrites++; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3159 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3160 | ATRACE_NAME("underrun"); | 
|  | 3161 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3162 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3163 | lastWarning = lastWriteFinished; | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3164 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3165 | } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3166 |  | 
|  | 3167 | if (mThreadThrottle | 
|  | 3168 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) | 
|  | 3169 | && ret > 0) {                         // we wrote something | 
|  | 3170 | // Limit MixerThread data processing to no more than twice the | 
|  | 3171 | // expected processing rate. | 
|  | 3172 | // | 
|  | 3173 | // This helps prevent underruns with NuPlayer and other applications | 
|  | 3174 | // which may set up buffers that are close to the minimum size, or use | 
|  | 3175 | // deep buffers, and rely on a double-buffering sleep strategy to fill. | 
|  | 3176 | // | 
|  | 3177 | // The throttle smooths out sudden large data drains from the device, | 
|  | 3178 | // e.g. when it comes out of standby, which often causes problems with | 
|  | 3179 | // (1) mixer threads without a fast mixer (which has its own warm-up) | 
|  | 3180 | // (2) minimum buffer sized tracks (even if the track is full, | 
|  | 3181 | //     the app won't fill fast enough to handle the sudden draw). | 
|  | 3182 |  | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3183 | // it's OK if deltaMs is an overestimate. | 
|  | 3184 | const int32_t deltaMs = | 
|  | 3185 | (lastWriteFinished - previousLastWriteFinished) / 1000000; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3186 | const int32_t throttleMs = mHalfBufferMs - deltaMs; | 
|  | 3187 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { | 
|  | 3188 | usleep(throttleMs * 1000); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3189 | // notify of throttle start on verbose log | 
|  | 3190 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, | 
|  | 3191 | "mixer(%p) throttle begin:" | 
|  | 3192 | " ret(%zd) deltaMs(%d) requires sleep %d ms", | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3193 | this, ret, deltaMs, throttleMs); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3194 | mThreadThrottleTimeMs += throttleMs; | 
|  | 3195 | } else { | 
|  | 3196 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; | 
|  | 3197 | if (diff > 0) { | 
|  | 3198 | // notify of throttle end on debug log | 
| Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3199 | // but prevent spamming for bluetooth | 
|  | 3200 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()), | 
|  | 3201 | "mixer(%p) throttle end: throttle time(%u)", this, diff); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3202 | mThreadThrottleEndMs = mThreadThrottleTimeMs; | 
|  | 3203 | } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3204 | } | 
|  | 3205 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3206 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3207 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3208 | } else { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3209 | ATRACE_BEGIN("sleep"); | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3210 | Mutex::Autolock _l(mLock); | 
|  | 3211 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { | 
|  | 3212 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3213 | } | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3214 | ATRACE_END(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3215 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3216 | } | 
|  | 3217 |  | 
|  | 3218 | // Finally let go of removed track(s), without the lock held | 
|  | 3219 | // since we can't guarantee the destructors won't acquire that | 
|  | 3220 | // same lock.  This will also mutate and push a new fast mixer state. | 
|  | 3221 | threadLoop_removeTracks(tracksToRemove); | 
|  | 3222 | tracksToRemove.clear(); | 
|  | 3223 |  | 
|  | 3224 | // FIXME I don't understand the need for this here; | 
|  | 3225 | //       it was in the original code but maybe the | 
|  | 3226 | //       assignment in saveOutputTracks() makes this unnecessary? | 
|  | 3227 | clearOutputTracks(); | 
|  | 3228 |  | 
|  | 3229 | // Effect chains will be actually deleted here if they were removed from | 
|  | 3230 | // mEffectChains list during mixing or effects processing | 
|  | 3231 | effectChains.clear(); | 
|  | 3232 |  | 
|  | 3233 | // FIXME Note that the above .clear() is no longer necessary since effectChains | 
|  | 3234 | // is now local to this block, but will keep it for now (at least until merge done). | 
|  | 3235 | } | 
|  | 3236 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3237 | threadLoop_exit(); | 
|  | 3238 |  | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3239 | if (!mStandby) { | 
|  | 3240 | threadLoop_standby(); | 
|  | 3241 | mStandby = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3242 | } | 
|  | 3243 |  | 
|  | 3244 | releaseWakeLock(); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3245 | mWakeLockUids.clear(); | 
|  | 3246 | mActiveTracksGeneration++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3247 |  | 
|  | 3248 | ALOGV("Thread %p type %d exiting", this, mType); | 
|  | 3249 | return false; | 
|  | 3250 | } | 
|  | 3251 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3252 | // removeTracks_l() must be called with ThreadBase::mLock held | 
|  | 3253 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) | 
|  | 3254 | { | 
|  | 3255 | size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3256 | if (count > 0) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3257 | for (size_t i=0 ; i<count ; i++) { | 
|  | 3258 | const sp<Track>& track = tracksToRemove.itemAt(i); | 
|  | 3259 | mActiveTracks.remove(track); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3260 | mWakeLockUids.remove(track->uid()); | 
|  | 3261 | mActiveTracksGeneration++; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3262 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); | 
|  | 3263 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 3264 | if (chain != 0) { | 
|  | 3265 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), | 
|  | 3266 | track->sessionId()); | 
|  | 3267 | chain->decActiveTrackCnt(); | 
|  | 3268 | } | 
|  | 3269 | if (track->isTerminated()) { | 
|  | 3270 | removeTrack_l(track); | 
|  | 3271 | } | 
|  | 3272 | } | 
|  | 3273 | } | 
|  | 3274 |  | 
|  | 3275 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3276 |  | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3277 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) | 
|  | 3278 | { | 
|  | 3279 | if (mNormalSink != 0) { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3280 | ExtendedTimestamp ets; | 
|  | 3281 | status_t status = mNormalSink->getTimestamp(ets); | 
|  | 3282 | if (status == NO_ERROR) { | 
|  | 3283 | status = ets.getBestTimestamp(×tamp); | 
|  | 3284 | } | 
|  | 3285 | return status; | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3286 | } | 
| Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3287 | if ((mType == OFFLOAD || mType == DIRECT) | 
|  | 3288 | && mOutput != NULL && mOutput->stream->get_presentation_position) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3289 | uint64_t position64; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3290 | int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3291 | if (ret == 0) { | 
|  | 3292 | timestamp.mPosition = (uint32_t)position64; | 
|  | 3293 | return NO_ERROR; | 
|  | 3294 | } | 
|  | 3295 | } | 
|  | 3296 | return INVALID_OPERATION; | 
|  | 3297 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3298 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3299 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 3300 | audio_patch_handle_t *handle) | 
|  | 3301 | { | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3302 | AutoPark<FastMixer> park(mFastMixer); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3303 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3304 | status_t status = PlaybackThread::createAudioPatch_l(patch, handle); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3305 |  | 
|  | 3306 | return status; | 
|  | 3307 | } | 
|  | 3308 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3309 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 3310 | audio_patch_handle_t *handle) | 
|  | 3311 | { | 
|  | 3312 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3313 |  | 
|  | 3314 | // store new device and send to effects | 
|  | 3315 | audio_devices_t type = AUDIO_DEVICE_NONE; | 
|  | 3316 | for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
|  | 3317 | type |= patch->sinks[i].ext.device.type; | 
|  | 3318 | } | 
|  | 3319 |  | 
|  | 3320 | #ifdef ADD_BATTERY_DATA | 
|  | 3321 | // when changing the audio output device, call addBatteryData to notify | 
|  | 3322 | // the change | 
|  | 3323 | if (mOutDevice != type) { | 
|  | 3324 | uint32_t params = 0; | 
|  | 3325 | // check whether speaker is on | 
|  | 3326 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { | 
|  | 3327 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3328 | } | 
|  | 3329 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3330 | audio_devices_t deviceWithoutSpeaker | 
|  | 3331 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 3332 | // check if any other device (except speaker) is on | 
|  | 3333 | if (type & deviceWithoutSpeaker) { | 
|  | 3334 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
|  | 3335 | } | 
|  | 3336 |  | 
|  | 3337 | if (params != 0) { | 
|  | 3338 | addBatteryData(params); | 
|  | 3339 | } | 
|  | 3340 | } | 
|  | 3341 | #endif | 
|  | 3342 |  | 
|  | 3343 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 3344 | mEffectChains[i]->setDevice_l(type); | 
|  | 3345 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3346 |  | 
|  | 3347 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when | 
|  | 3348 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback | 
|  | 3349 | bool configChanged = mPrevOutDevice != type; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3350 | mOutDevice = type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3351 | mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3352 |  | 
|  | 3353 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3354 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); | 
|  | 3355 | status = hwDevice->create_audio_patch(hwDevice, | 
|  | 3356 | patch->num_sources, | 
|  | 3357 | patch->sources, | 
|  | 3358 | patch->num_sinks, | 
|  | 3359 | patch->sinks, | 
|  | 3360 | handle); | 
|  | 3361 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3362 | char *address; | 
|  | 3363 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
|  | 3364 | //FIXME: we only support address on first sink with HAL version < 3.0 | 
|  | 3365 | address = audio_device_address_to_parameter( | 
|  | 3366 | patch->sinks[0].ext.device.type, | 
|  | 3367 | patch->sinks[0].ext.device.address); | 
|  | 3368 | } else { | 
|  | 3369 | address = (char *)calloc(1, 1); | 
|  | 3370 | } | 
|  | 3371 | AudioParameter param = AudioParameter(String8(address)); | 
|  | 3372 | free(address); | 
|  | 3373 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); | 
|  | 3374 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
|  | 3375 | param.toString().string()); | 
|  | 3376 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3377 | } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3378 | if (configChanged) { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3379 | mPrevOutDevice = type; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3380 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
|  | 3381 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3382 | return status; | 
|  | 3383 | } | 
|  | 3384 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3385 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 3386 | { | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 3387 | AutoPark<FastMixer> park(mFastMixer); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3388 |  | 
|  | 3389 | status_t status = PlaybackThread::releaseAudioPatch_l(handle); | 
|  | 3390 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3391 | return status; | 
|  | 3392 | } | 
|  | 3393 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3394 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 3395 | { | 
|  | 3396 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3397 |  | 
|  | 3398 | mOutDevice = AUDIO_DEVICE_NONE; | 
|  | 3399 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3400 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
|  | 3401 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); | 
|  | 3402 | status = hwDevice->release_audio_patch(hwDevice, handle); | 
|  | 3403 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3404 | AudioParameter param; | 
|  | 3405 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); | 
|  | 3406 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
|  | 3407 | param.toString().string()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3408 | } | 
|  | 3409 | return status; | 
|  | 3410 | } | 
|  | 3411 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3412 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) | 
|  | 3413 | { | 
|  | 3414 | Mutex::Autolock _l(mLock); | 
|  | 3415 | mTracks.add(track); | 
|  | 3416 | } | 
|  | 3417 |  | 
|  | 3418 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) | 
|  | 3419 | { | 
|  | 3420 | Mutex::Autolock _l(mLock); | 
|  | 3421 | destroyTrack_l(track); | 
|  | 3422 | } | 
|  | 3423 |  | 
|  | 3424 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) | 
|  | 3425 | { | 
|  | 3426 | ThreadBase::getAudioPortConfig(config); | 
|  | 3427 | config->role = AUDIO_PORT_ROLE_SOURCE; | 
|  | 3428 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); | 
|  | 3429 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
|  | 3430 | } | 
|  | 3431 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3432 | // ---------------------------------------------------------------------------- | 
|  | 3433 |  | 
|  | 3434 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3435 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) | 
|  | 3436 | :   PlaybackThread(audioFlinger, output, id, device, type, systemReady), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3437 | // mAudioMixer below | 
|  | 3438 | // mFastMixer below | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3439 | mFastMixerFutex(0), | 
|  | 3440 | mMasterMono(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3441 | // mOutputSink below | 
|  | 3442 | // mPipeSink below | 
|  | 3443 | // mNormalSink below | 
|  | 3444 | { | 
|  | 3445 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3446 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%zu, " | 
|  | 3447 | "mFrameCount=%zu, mNormalFrameCount=%zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3448 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
|  | 3449 | mNormalFrameCount); | 
|  | 3450 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
|  | 3451 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3452 | if (type == DUPLICATING) { | 
|  | 3453 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks | 
|  | 3454 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). | 
|  | 3455 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. | 
|  | 3456 | return; | 
|  | 3457 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3458 | // create an NBAIO sink for the HAL output stream, and negotiate | 
|  | 3459 | mOutputSink = new AudioStreamOutSink(output->stream); | 
|  | 3460 | size_t numCounterOffers = 0; | 
| Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3461 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3462 | #if !LOG_NDEBUG | 
|  | 3463 | ssize_t index = | 
|  | 3464 | #else | 
|  | 3465 | (void) | 
|  | 3466 | #endif | 
|  | 3467 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3468 | ALOG_ASSERT(index == 0); | 
|  | 3469 |  | 
|  | 3470 | // initialize fast mixer depending on configuration | 
|  | 3471 | bool initFastMixer; | 
|  | 3472 | switch (kUseFastMixer) { | 
|  | 3473 | case FastMixer_Never: | 
|  | 3474 | initFastMixer = false; | 
|  | 3475 | break; | 
|  | 3476 | case FastMixer_Always: | 
|  | 3477 | initFastMixer = true; | 
|  | 3478 | break; | 
|  | 3479 | case FastMixer_Static: | 
|  | 3480 | case FastMixer_Dynamic: | 
|  | 3481 | initFastMixer = mFrameCount < mNormalFrameCount; | 
|  | 3482 | break; | 
|  | 3483 | } | 
|  | 3484 | if (initFastMixer) { | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3485 | audio_format_t fastMixerFormat; | 
|  | 3486 | if (mMixerBufferEnabled && mEffectBufferEnabled) { | 
|  | 3487 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; | 
|  | 3488 | } else { | 
|  | 3489 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 3490 | } | 
|  | 3491 | if (mFormat != fastMixerFormat) { | 
|  | 3492 | // change our Sink format to accept our intermediate precision | 
|  | 3493 | mFormat = fastMixerFormat; | 
|  | 3494 | free(mSinkBuffer); | 
|  | 3495 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
|  | 3496 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
|  | 3497 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
|  | 3498 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3499 |  | 
|  | 3500 | // create a MonoPipe to connect our submix to FastMixer | 
|  | 3501 | NBAIO_Format format = mOutputSink->format(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3502 | #ifdef TEE_SINK | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3503 | NBAIO_Format origformat = format; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3504 | #endif | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3505 | // adjust format to match that of the Fast Mixer | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3506 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3507 | format.mFormat = fastMixerFormat; | 
|  | 3508 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; | 
|  | 3509 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3510 | // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
|  | 3511 | // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
|  | 3512 | // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
|  | 3513 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
|  | 3514 | const NBAIO_Format offers[1] = {format}; | 
|  | 3515 | size_t numCounterOffers = 0; | 
| Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3516 | #if !LOG_NDEBUG || defined(TEE_SINK) | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3517 | ssize_t index = | 
|  | 3518 | #else | 
|  | 3519 | (void) | 
|  | 3520 | #endif | 
|  | 3521 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3522 | ALOG_ASSERT(index == 0); | 
|  | 3523 | monoPipe->setAvgFrames((mScreenState & 1) ? | 
|  | 3524 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
|  | 3525 | mPipeSink = monoPipe; | 
|  | 3526 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3527 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3528 | if (mTeeSinkOutputEnabled) { | 
|  | 3529 | // 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] | 3530 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); | 
|  | 3531 | const NBAIO_Format offers2[1] = {origformat}; | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3532 | numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3533 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3534 | ALOG_ASSERT(index == 0); | 
|  | 3535 | mTeeSink = teeSink; | 
|  | 3536 | PipeReader *teeSource = new PipeReader(*teeSink); | 
|  | 3537 | numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3538 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3539 | ALOG_ASSERT(index == 0); | 
|  | 3540 | mTeeSource = teeSource; | 
|  | 3541 | } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3542 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3543 |  | 
|  | 3544 | // create fast mixer and configure it initially with just one fast track for our submix | 
|  | 3545 | mFastMixer = new FastMixer(); | 
|  | 3546 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3547 | #ifdef STATE_QUEUE_DUMP | 
|  | 3548 | sq->setObserverDump(&mStateQueueObserverDump); | 
|  | 3549 | sq->setMutatorDump(&mStateQueueMutatorDump); | 
|  | 3550 | #endif | 
|  | 3551 | FastMixerState *state = sq->begin(); | 
|  | 3552 | FastTrack *fastTrack = &state->mFastTracks[0]; | 
|  | 3553 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
|  | 3554 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
|  | 3555 | fastTrack->mVolumeProvider = NULL; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3556 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer | 
|  | 3557 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3558 | fastTrack->mGeneration++; | 
|  | 3559 | state->mFastTracksGen++; | 
|  | 3560 | state->mTrackMask = 1; | 
|  | 3561 | // fast mixer will use the HAL output sink | 
|  | 3562 | state->mOutputSink = mOutputSink.get(); | 
|  | 3563 | state->mOutputSinkGen++; | 
|  | 3564 | state->mFrameCount = mFrameCount; | 
|  | 3565 | state->mCommand = FastMixerState::COLD_IDLE; | 
|  | 3566 | // already done in constructor initialization list | 
|  | 3567 | //mFastMixerFutex = 0; | 
|  | 3568 | state->mColdFutexAddr = &mFastMixerFutex; | 
|  | 3569 | state->mColdGen++; | 
|  | 3570 | state->mDumpState = &mFastMixerDumpState; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3571 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3572 | state->mTeeSink = mTeeSink.get(); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3573 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3574 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
|  | 3575 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3576 | sq->end(); | 
|  | 3577 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 3578 |  | 
|  | 3579 | // start the fast mixer | 
|  | 3580 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
|  | 3581 | pid_t tid = mFastMixer->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3582 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3583 |  | 
|  | 3584 | #ifdef AUDIO_WATCHDOG | 
|  | 3585 | // create and start the watchdog | 
|  | 3586 | mAudioWatchdog = new AudioWatchdog(); | 
|  | 3587 | mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
|  | 3588 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
|  | 3589 | tid = mAudioWatchdog->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3590 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3591 | #endif | 
|  | 3592 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3593 | } | 
|  | 3594 |  | 
|  | 3595 | switch (kUseFastMixer) { | 
|  | 3596 | case FastMixer_Never: | 
|  | 3597 | case FastMixer_Dynamic: | 
|  | 3598 | mNormalSink = mOutputSink; | 
|  | 3599 | break; | 
|  | 3600 | case FastMixer_Always: | 
|  | 3601 | mNormalSink = mPipeSink; | 
|  | 3602 | break; | 
|  | 3603 | case FastMixer_Static: | 
|  | 3604 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
|  | 3605 | break; | 
|  | 3606 | } | 
|  | 3607 | } | 
|  | 3608 |  | 
|  | 3609 | AudioFlinger::MixerThread::~MixerThread() | 
|  | 3610 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3611 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3613 | FastMixerState *state = sq->begin(); | 
|  | 3614 | if (state->mCommand == FastMixerState::COLD_IDLE) { | 
|  | 3615 | int32_t old = android_atomic_inc(&mFastMixerFutex); | 
|  | 3616 | if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3617 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3618 | } | 
|  | 3619 | } | 
|  | 3620 | state->mCommand = FastMixerState::EXIT; | 
|  | 3621 | sq->end(); | 
|  | 3622 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 3623 | mFastMixer->join(); | 
|  | 3624 | // Though the fast mixer thread has exited, it's state queue is still valid. | 
|  | 3625 | // We'll use that extract the final state which contains one remaining fast track | 
|  | 3626 | // corresponding to our sub-mix. | 
|  | 3627 | state = sq->begin(); | 
|  | 3628 | ALOG_ASSERT(state->mTrackMask == 1); | 
|  | 3629 | FastTrack *fastTrack = &state->mFastTracks[0]; | 
|  | 3630 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
|  | 3631 | delete fastTrack->mBufferProvider; | 
|  | 3632 | sq->end(false /*didModify*/); | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3633 | mFastMixer.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3634 | #ifdef AUDIO_WATCHDOG | 
|  | 3635 | if (mAudioWatchdog != 0) { | 
|  | 3636 | mAudioWatchdog->requestExit(); | 
|  | 3637 | mAudioWatchdog->requestExitAndWait(); | 
|  | 3638 | mAudioWatchdog.clear(); | 
|  | 3639 | } | 
|  | 3640 | #endif | 
|  | 3641 | } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3642 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3643 | delete mAudioMixer; | 
|  | 3644 | } | 
|  | 3645 |  | 
|  | 3646 |  | 
|  | 3647 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const | 
|  | 3648 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3649 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3650 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
|  | 3651 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
|  | 3652 | } | 
|  | 3653 | return latency; | 
|  | 3654 | } | 
|  | 3655 |  | 
|  | 3656 |  | 
|  | 3657 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) | 
|  | 3658 | { | 
|  | 3659 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); | 
|  | 3660 | } | 
|  | 3661 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3662 | ssize_t AudioFlinger::MixerThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3663 | { | 
|  | 3664 | // FIXME we should only do one push per cycle; confirm this is true | 
|  | 3665 | // Start the fast mixer if it's not already running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3666 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3667 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3668 | FastMixerState *state = sq->begin(); | 
|  | 3669 | if (state->mCommand != FastMixerState::MIX_WRITE && | 
|  | 3670 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
|  | 3671 | if (state->mCommand == FastMixerState::COLD_IDLE) { | 
| Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3672 |  | 
|  | 3673 | // FIXME workaround for first HAL write being CPU bound on some devices | 
|  | 3674 | ATRACE_BEGIN("write"); | 
|  | 3675 | mOutput->write((char *)mSinkBuffer, 0); | 
|  | 3676 | ATRACE_END(); | 
|  | 3677 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3678 | int32_t old = android_atomic_inc(&mFastMixerFutex); | 
|  | 3679 | if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3680 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3681 | } | 
|  | 3682 | #ifdef AUDIO_WATCHDOG | 
|  | 3683 | if (mAudioWatchdog != 0) { | 
|  | 3684 | mAudioWatchdog->resume(); | 
|  | 3685 | } | 
|  | 3686 | #endif | 
|  | 3687 | } | 
|  | 3688 | state->mCommand = FastMixerState::MIX_WRITE; | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3689 | #ifdef FAST_THREAD_STATISTICS | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3690 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3691 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3692 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3693 | sq->end(); | 
|  | 3694 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 3695 | if (kUseFastMixer == FastMixer_Dynamic) { | 
|  | 3696 | mNormalSink = mPipeSink; | 
|  | 3697 | } | 
|  | 3698 | } else { | 
|  | 3699 | sq->end(false /*didModify*/); | 
|  | 3700 | } | 
|  | 3701 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3702 | return PlaybackThread::threadLoop_write(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3703 | } | 
|  | 3704 |  | 
|  | 3705 | void AudioFlinger::MixerThread::threadLoop_standby() | 
|  | 3706 | { | 
|  | 3707 | // Idle the fast mixer if it's currently running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3708 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3709 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3710 | FastMixerState *state = sq->begin(); | 
|  | 3711 | if (!(state->mCommand & FastMixerState::IDLE)) { | 
|  | 3712 | state->mCommand = FastMixerState::COLD_IDLE; | 
|  | 3713 | state->mColdFutexAddr = &mFastMixerFutex; | 
|  | 3714 | state->mColdGen++; | 
|  | 3715 | mFastMixerFutex = 0; | 
|  | 3716 | sq->end(); | 
|  | 3717 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
|  | 3718 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
|  | 3719 | if (kUseFastMixer == FastMixer_Dynamic) { | 
|  | 3720 | mNormalSink = mOutputSink; | 
|  | 3721 | } | 
|  | 3722 | #ifdef AUDIO_WATCHDOG | 
|  | 3723 | if (mAudioWatchdog != 0) { | 
|  | 3724 | mAudioWatchdog->pause(); | 
|  | 3725 | } | 
|  | 3726 | #endif | 
|  | 3727 | } else { | 
|  | 3728 | sq->end(false /*didModify*/); | 
|  | 3729 | } | 
|  | 3730 | } | 
|  | 3731 | PlaybackThread::threadLoop_standby(); | 
|  | 3732 | } | 
|  | 3733 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3734 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() | 
|  | 3735 | { | 
|  | 3736 | return false; | 
|  | 3737 | } | 
|  | 3738 |  | 
|  | 3739 | bool AudioFlinger::PlaybackThread::shouldStandby_l() | 
|  | 3740 | { | 
|  | 3741 | return !mStandby; | 
|  | 3742 | } | 
|  | 3743 |  | 
|  | 3744 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() | 
|  | 3745 | { | 
|  | 3746 | Mutex::Autolock _l(mLock); | 
|  | 3747 | return waitingAsyncCallback_l(); | 
|  | 3748 | } | 
|  | 3749 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3750 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
|  | 3751 | void AudioFlinger::PlaybackThread::threadLoop_standby() | 
|  | 3752 | { | 
|  | 3753 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3754 | mOutput->standby(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3755 | if (mUseAsyncWrite != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3756 | // discard any pending drain or write ack by incrementing sequence | 
|  | 3757 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
|  | 3758 | mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3759 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3760 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
|  | 3761 | mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3762 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3763 | mHwPaused = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3764 | } | 
|  | 3765 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3766 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() | 
|  | 3767 | { | 
|  | 3768 | ALOGV("signal playback thread"); | 
|  | 3769 | broadcast_l(); | 
|  | 3770 | } | 
|  | 3771 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3772 | void AudioFlinger::MixerThread::threadLoop_mix() | 
|  | 3773 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3774 | // mix buffers... | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3775 | mAudioMixer->process(); | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3776 | mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3777 | // increase sleep time progressively when application underrun condition clears. | 
|  | 3778 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
|  | 3779 | // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
|  | 3780 | // such that we would underrun the audio HAL. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3781 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3782 | sleepTimeShift--; | 
|  | 3783 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3784 | mSleepTimeUs = 0; | 
|  | 3785 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3786 | //TODO: delay standby when effects have a tail | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3787 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3788 | } | 
|  | 3789 |  | 
|  | 3790 | void AudioFlinger::MixerThread::threadLoop_sleepTime() | 
|  | 3791 | { | 
|  | 3792 | // If no tracks are ready, sleep once for the duration of an output | 
|  | 3793 | // buffer size, then write 0s to the output | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3794 | if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3795 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3796 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; | 
|  | 3797 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { | 
|  | 3798 | mSleepTimeUs = kMinThreadSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3799 | } | 
|  | 3800 | // reduce sleep time in case of consecutive application underruns to avoid | 
|  | 3801 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
|  | 3802 | // duration we would end up writing less data than needed by the audio HAL if | 
|  | 3803 | // the condition persists. | 
|  | 3804 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
|  | 3805 | sleepTimeShift++; | 
|  | 3806 | } | 
|  | 3807 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3808 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3809 | } | 
|  | 3810 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3811 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared | 
|  | 3812 | // before effects processing or output. | 
|  | 3813 | if (mMixerBufferValid) { | 
|  | 3814 | memset(mMixerBuffer, 0, mMixerBufferSize); | 
|  | 3815 | } else { | 
|  | 3816 | memset(mSinkBuffer, 0, mSinkBufferSize); | 
|  | 3817 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3818 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3819 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
|  | 3820 | "anticipated start"); | 
|  | 3821 | } | 
|  | 3822 | // TODO add standby time extension fct of effect tail | 
|  | 3823 | } | 
|  | 3824 |  | 
|  | 3825 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
|  | 3826 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( | 
|  | 3827 | Vector< sp<Track> > *tracksToRemove) | 
|  | 3828 | { | 
|  | 3829 |  | 
|  | 3830 | mixer_state mixerStatus = MIXER_IDLE; | 
|  | 3831 | // find out which tracks need to be processed | 
|  | 3832 | size_t count = mActiveTracks.size(); | 
|  | 3833 | size_t mixedTracks = 0; | 
|  | 3834 | size_t tracksWithEffect = 0; | 
|  | 3835 | // counts only _active_ fast tracks | 
|  | 3836 | size_t fastTracks = 0; | 
|  | 3837 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
|  | 3838 |  | 
|  | 3839 | float masterVolume = mMasterVolume; | 
|  | 3840 | bool masterMute = mMasterMute; | 
|  | 3841 |  | 
|  | 3842 | if (masterMute) { | 
|  | 3843 | masterVolume = 0; | 
|  | 3844 | } | 
|  | 3845 | // Delegate master volume control to effect in output mix effect chain if needed | 
|  | 3846 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
|  | 3847 | if (chain != 0) { | 
|  | 3848 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
|  | 3849 | chain->setVolume_l(&v, &v); | 
|  | 3850 | masterVolume = (float)((v + (1 << 23)) >> 24); | 
|  | 3851 | chain.clear(); | 
|  | 3852 | } | 
|  | 3853 |  | 
|  | 3854 | // prepare a new state to push | 
|  | 3855 | FastMixerStateQueue *sq = NULL; | 
|  | 3856 | FastMixerState *state = NULL; | 
|  | 3857 | bool didModify = false; | 
|  | 3858 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3859 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3860 | sq = mFastMixer->sq(); | 
|  | 3861 | state = sq->begin(); | 
|  | 3862 | } | 
|  | 3863 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3864 | mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3865 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3866 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3867 | for (size_t i=0 ; i<count ; i++) { | 
| Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3868 | const sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3869 | if (t == 0) { | 
|  | 3870 | continue; | 
|  | 3871 | } | 
|  | 3872 |  | 
|  | 3873 | // this const just means the local variable doesn't change | 
|  | 3874 | Track* const track = t.get(); | 
|  | 3875 |  | 
|  | 3876 | // process fast tracks | 
|  | 3877 | if (track->isFastTrack()) { | 
|  | 3878 |  | 
|  | 3879 | // It's theoretically possible (though unlikely) for a fast track to be created | 
|  | 3880 | // and then removed within the same normal mix cycle.  This is not a problem, as | 
|  | 3881 | // the track never becomes active so it's fast mixer slot is never touched. | 
|  | 3882 | // The converse, of removing an (active) track and then creating a new track | 
|  | 3883 | // at the identical fast mixer slot within the same normal mix cycle, | 
|  | 3884 | // is impossible because the slot isn't marked available until the end of each cycle. | 
|  | 3885 | int j = track->mFastIndex; | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 3886 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3887 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
|  | 3888 | FastTrack *fastTrack = &state->mFastTracks[j]; | 
|  | 3889 |  | 
|  | 3890 | // Determine whether the track is currently in underrun condition, | 
|  | 3891 | // and whether it had a recent underrun. | 
|  | 3892 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
|  | 3893 | FastTrackUnderruns underruns = ftDump->mUnderruns; | 
|  | 3894 | uint32_t recentFull = (underruns.mBitFields.mFull - | 
|  | 3895 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; | 
|  | 3896 | uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
|  | 3897 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; | 
|  | 3898 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
|  | 3899 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; | 
|  | 3900 | uint32_t recentUnderruns = recentPartial + recentEmpty; | 
|  | 3901 | track->mObservedUnderruns = underruns; | 
|  | 3902 | // don't count underruns that occur while stopping or pausing | 
|  | 3903 | // or stopped which can occur when flush() is called while active | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3904 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && | 
|  | 3905 | recentUnderruns > 0) { | 
|  | 3906 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun | 
|  | 3907 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 3908 | } else { | 
|  | 3909 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3910 | } | 
|  | 3911 |  | 
|  | 3912 | // This is similar to the state machine for normal tracks, | 
|  | 3913 | // with a few modifications for fast tracks. | 
|  | 3914 | bool isActive = true; | 
|  | 3915 | switch (track->mState) { | 
|  | 3916 | case TrackBase::STOPPING_1: | 
|  | 3917 | // track stays active in STOPPING_1 state until first underrun | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3918 | if (recentUnderruns > 0 || track->isTerminated()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3919 | track->mState = TrackBase::STOPPING_2; | 
|  | 3920 | } | 
|  | 3921 | break; | 
|  | 3922 | case TrackBase::PAUSING: | 
|  | 3923 | // ramp down is not yet implemented | 
|  | 3924 | track->setPaused(); | 
|  | 3925 | break; | 
|  | 3926 | case TrackBase::RESUMING: | 
|  | 3927 | // ramp up is not yet implemented | 
|  | 3928 | track->mState = TrackBase::ACTIVE; | 
|  | 3929 | break; | 
|  | 3930 | case TrackBase::ACTIVE: | 
|  | 3931 | if (recentFull > 0 || recentPartial > 0) { | 
|  | 3932 | // track has provided at least some frames recently: reset retry count | 
|  | 3933 | track->mRetryCount = kMaxTrackRetries; | 
|  | 3934 | } | 
|  | 3935 | if (recentUnderruns == 0) { | 
|  | 3936 | // no recent underruns: stay active | 
|  | 3937 | break; | 
|  | 3938 | } | 
|  | 3939 | // there has recently been an underrun of some kind | 
|  | 3940 | if (track->sharedBuffer() == 0) { | 
|  | 3941 | // were any of the recent underruns "empty" (no frames available)? | 
|  | 3942 | if (recentEmpty == 0) { | 
|  | 3943 | // no, then ignore the partial underruns as they are allowed indefinitely | 
|  | 3944 | break; | 
|  | 3945 | } | 
|  | 3946 | // there has recently been an "empty" underrun: decrement the retry counter | 
|  | 3947 | if (--(track->mRetryCount) > 0) { | 
|  | 3948 | break; | 
|  | 3949 | } | 
|  | 3950 | // indicate to client process that the track was disabled because of underrun; | 
|  | 3951 | // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 3952 | track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3953 | // remove from active list, but state remains ACTIVE [confusing but true] | 
|  | 3954 | isActive = false; | 
|  | 3955 | break; | 
|  | 3956 | } | 
|  | 3957 | // fall through | 
|  | 3958 | case TrackBase::STOPPING_2: | 
|  | 3959 | case TrackBase::PAUSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3960 | case TrackBase::STOPPED: | 
|  | 3961 | case TrackBase::FLUSHED:   // flush() while active | 
|  | 3962 | // Check for presentation complete if track is inactive | 
|  | 3963 | // We have consumed all the buffers of this track. | 
|  | 3964 | // This would be incomplete if we auto-paused on underrun | 
|  | 3965 | { | 
|  | 3966 | size_t audioHALFrames = | 
|  | 3967 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3968 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3969 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
|  | 3970 | // track stays in active list until presentation is complete | 
|  | 3971 | break; | 
|  | 3972 | } | 
|  | 3973 | } | 
|  | 3974 | if (track->isStopping_2()) { | 
|  | 3975 | track->mState = TrackBase::STOPPED; | 
|  | 3976 | } | 
|  | 3977 | if (track->isStopped()) { | 
|  | 3978 | // Can't reset directly, as fast mixer is still polling this track | 
|  | 3979 | //   track->reset(); | 
|  | 3980 | // So instead mark this track as needing to be reset after push with ack | 
|  | 3981 | resetMask |= 1 << i; | 
|  | 3982 | } | 
|  | 3983 | isActive = false; | 
|  | 3984 | break; | 
|  | 3985 | case TrackBase::IDLE: | 
|  | 3986 | default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3987 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3988 | } | 
|  | 3989 |  | 
|  | 3990 | if (isActive) { | 
|  | 3991 | // was it previously inactive? | 
|  | 3992 | if (!(state->mTrackMask & (1 << j))) { | 
|  | 3993 | ExtendedAudioBufferProvider *eabp = track; | 
|  | 3994 | VolumeProvider *vp = track; | 
|  | 3995 | fastTrack->mBufferProvider = eabp; | 
|  | 3996 | fastTrack->mVolumeProvider = vp; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3997 | fastTrack->mChannelMask = track->mChannelMask; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3998 | fastTrack->mFormat = track->mFormat; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3999 | fastTrack->mGeneration++; | 
|  | 4000 | state->mTrackMask |= 1 << j; | 
|  | 4001 | didModify = true; | 
|  | 4002 | // no acknowledgement required for newly active tracks | 
|  | 4003 | } | 
|  | 4004 | // cache the combined master volume and stream type volume for fast mixer; this | 
|  | 4005 | // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4006 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4007 | ++fastTracks; | 
|  | 4008 | } else { | 
|  | 4009 | // was it previously active? | 
|  | 4010 | if (state->mTrackMask & (1 << j)) { | 
|  | 4011 | fastTrack->mBufferProvider = NULL; | 
|  | 4012 | fastTrack->mGeneration++; | 
|  | 4013 | state->mTrackMask &= ~(1 << j); | 
|  | 4014 | didModify = true; | 
|  | 4015 | // If any fast tracks were removed, we must wait for acknowledgement | 
|  | 4016 | // because we're about to decrement the last sp<> on those tracks. | 
|  | 4017 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
|  | 4018 | } else { | 
| Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4019 | LOG_ALWAYS_FATAL("fast track %d should have been active; " | 
|  | 4020 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", | 
|  | 4021 | j, track->mState, state->mTrackMask, recentUnderruns, | 
|  | 4022 | track->sharedBuffer() != 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4023 | } | 
|  | 4024 | tracksToRemove->add(track); | 
|  | 4025 | // Avoids a misleading display in dumpsys | 
|  | 4026 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; | 
|  | 4027 | } | 
|  | 4028 | continue; | 
|  | 4029 | } | 
|  | 4030 |  | 
|  | 4031 | {   // local variable scope to avoid goto warning | 
|  | 4032 |  | 
|  | 4033 | audio_track_cblk_t* cblk = track->cblk(); | 
|  | 4034 |  | 
|  | 4035 | // The first time a track is added we wait | 
|  | 4036 | // for all its buffers to be filled before processing it | 
|  | 4037 | int name = track->name(); | 
|  | 4038 | // make sure that we have enough frames to mix one full buffer. | 
|  | 4039 | // enforce this condition only once to enable draining the buffer in case the client | 
|  | 4040 | // app does not call stop() and relies on underrun to stop: | 
|  | 4041 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
|  | 4042 | // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4043 | size_t desiredFrames; | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4044 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4045 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4046 |  | 
|  | 4047 | desiredFrames = sourceFramesNeededWithTimestretch( | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4048 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4049 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. | 
|  | 4050 | // add frames already consumed but not yet released by the resampler | 
|  | 4051 | // because mAudioTrackServerProxy->framesReady() will include these frames | 
|  | 4052 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
|  | 4053 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4054 | uint32_t minFrames = 1; | 
|  | 4055 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
|  | 4056 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4057 | minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4058 | } | 
| Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4059 |  | 
|  | 4060 | size_t framesReady = track->framesReady(); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4061 | if (ATRACE_ENABLED()) { | 
|  | 4062 | // I wish we had formatted trace names | 
|  | 4063 | char traceName[16]; | 
|  | 4064 | strcpy(traceName, "nRdy"); | 
|  | 4065 | int name = track->name(); | 
|  | 4066 | if (AudioMixer::TRACK0 <= name && | 
|  | 4067 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { | 
|  | 4068 | name -= AudioMixer::TRACK0; | 
|  | 4069 | traceName[4] = (name / 10) + '0'; | 
|  | 4070 | traceName[5] = (name % 10) + '0'; | 
|  | 4071 | } else { | 
|  | 4072 | traceName[4] = '?'; | 
|  | 4073 | traceName[5] = '?'; | 
|  | 4074 | } | 
|  | 4075 | traceName[6] = '\0'; | 
|  | 4076 | ATRACE_INT(traceName, framesReady); | 
|  | 4077 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4078 | if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4079 | !track->isPaused() && !track->isTerminated()) | 
|  | 4080 | { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4081 | 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] | 4082 |  | 
|  | 4083 | mixedTracks++; | 
|  | 4084 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4085 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means | 
|  | 4086 | // there is an effect chain connected to the track | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4087 | chain.clear(); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4088 | if (track->mainBuffer() != mSinkBuffer && | 
|  | 4089 | track->mainBuffer() != mMixerBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4090 | if (mEffectBufferEnabled) { | 
|  | 4091 | mEffectBufferValid = true; // Later can set directly. | 
|  | 4092 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4093 | chain = getEffectChain_l(track->sessionId()); | 
|  | 4094 | // Delegate volume control to effect in track effect chain if needed | 
|  | 4095 | if (chain != 0) { | 
|  | 4096 | tracksWithEffect++; | 
|  | 4097 | } else { | 
|  | 4098 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " | 
|  | 4099 | "session %d", | 
|  | 4100 | name, track->sessionId()); | 
|  | 4101 | } | 
|  | 4102 | } | 
|  | 4103 |  | 
|  | 4104 |  | 
|  | 4105 | int param = AudioMixer::VOLUME; | 
|  | 4106 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 4107 | // no ramp for the first volume setting | 
|  | 4108 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
|  | 4109 | if (track->mState == TrackBase::RESUMING) { | 
|  | 4110 | track->mState = TrackBase::ACTIVE; | 
|  | 4111 | param = AudioMixer::RAMP_VOLUME; | 
|  | 4112 | } | 
|  | 4113 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4114 | // FIXME should not make a decision based on mServer | 
|  | 4115 | } else if (cblk->mServer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4116 | // If the track is stopped before the first frame was mixed, | 
|  | 4117 | // do not apply ramp | 
|  | 4118 | param = AudioMixer::RAMP_VOLUME; | 
|  | 4119 | } | 
|  | 4120 |  | 
|  | 4121 | // compute volume for this track | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4122 | uint32_t vl, vr;       // in U8.24 integer format | 
|  | 4123 | float vlf, vrf, vaf;   // in [0.0, 1.0] float format | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4124 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4125 | vl = vr = 0; | 
|  | 4126 | vlf = vrf = vaf = 0.; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4127 | if (track->isPausing()) { | 
|  | 4128 | track->setPaused(); | 
|  | 4129 | } | 
|  | 4130 | } else { | 
|  | 4131 |  | 
|  | 4132 | // read original volumes with volume control | 
|  | 4133 | float typeVolume = mStreamTypes[track->streamType()].volume; | 
|  | 4134 | float v = masterVolume * typeVolume; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4135 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4136 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4137 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
|  | 4138 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4139 | // 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] | 4140 | if (vlf > GAIN_FLOAT_UNITY) { | 
|  | 4141 | ALOGV("Track left volume out of range: %.3g", vlf); | 
|  | 4142 | vlf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4143 | } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4144 | if (vrf > GAIN_FLOAT_UNITY) { | 
|  | 4145 | ALOGV("Track right volume out of range: %.3g", vrf); | 
|  | 4146 | vrf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4147 | } | 
|  | 4148 | // now apply the master volume and stream type volume | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4149 | vlf *= v; | 
|  | 4150 | vrf *= v; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4151 | // 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] | 4152 | // then derive vl and vr as U8.24 versions for the effect chain | 
|  | 4153 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; | 
|  | 4154 | vl = (uint32_t) (scaleto8_24 * vlf); | 
|  | 4155 | vr = (uint32_t) (scaleto8_24 * vrf); | 
|  | 4156 | // vl and vr are now in U8.24 format | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4157 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4158 | // send level comes from shared memory and so may be corrupt | 
|  | 4159 | if (sendLevel > MAX_GAIN_INT) { | 
|  | 4160 | ALOGV("Track send level out of range: %04X", sendLevel); | 
|  | 4161 | sendLevel = MAX_GAIN_INT; | 
|  | 4162 | } | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4163 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel | 
|  | 4164 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4165 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4166 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4167 | // Delegate volume control to effect in track effect chain if needed | 
|  | 4168 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
|  | 4169 | // Do not ramp volume if volume is controlled by effect | 
|  | 4170 | param = AudioMixer::VOLUME; | 
| Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4171 | // Update remaining floating point volume levels | 
|  | 4172 | vlf = (float)vl / (1 << 24); | 
|  | 4173 | vrf = (float)vr / (1 << 24); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4174 | track->mHasVolumeController = true; | 
|  | 4175 | } else { | 
|  | 4176 | // force no volume ramp when volume controller was just disabled or removed | 
|  | 4177 | // from effect chain to avoid volume spike | 
|  | 4178 | if (track->mHasVolumeController) { | 
|  | 4179 | param = AudioMixer::VOLUME; | 
|  | 4180 | } | 
|  | 4181 | track->mHasVolumeController = false; | 
|  | 4182 | } | 
|  | 4183 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4184 | // XXX: these things DON'T need to be done each time | 
|  | 4185 | mAudioMixer->setBufferProvider(name, track); | 
|  | 4186 | mAudioMixer->enable(name); | 
|  | 4187 |  | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4188 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); | 
|  | 4189 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); | 
|  | 4190 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4191 | mAudioMixer->setParameter( | 
|  | 4192 | name, | 
|  | 4193 | AudioMixer::TRACK, | 
|  | 4194 | AudioMixer::FORMAT, (void *)track->format()); | 
|  | 4195 | mAudioMixer->setParameter( | 
|  | 4196 | name, | 
|  | 4197 | AudioMixer::TRACK, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4198 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4199 | mAudioMixer->setParameter( | 
|  | 4200 | name, | 
|  | 4201 | AudioMixer::TRACK, | 
|  | 4202 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4203 | // 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] | 4204 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4205 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4206 | if (reqSampleRate == 0) { | 
|  | 4207 | reqSampleRate = mSampleRate; | 
|  | 4208 | } else if (reqSampleRate > maxSampleRate) { | 
|  | 4209 | reqSampleRate = maxSampleRate; | 
|  | 4210 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4211 | mAudioMixer->setParameter( | 
|  | 4212 | name, | 
|  | 4213 | AudioMixer::RESAMPLE, | 
|  | 4214 | AudioMixer::SAMPLE_RATE, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4215 | (void *)(uintptr_t)reqSampleRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4216 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4217 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4218 | mAudioMixer->setParameter( | 
|  | 4219 | name, | 
|  | 4220 | AudioMixer::TIMESTRETCH, | 
|  | 4221 | AudioMixer::PLAYBACK_RATE, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4222 | &playbackRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4223 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4224 | /* | 
|  | 4225 | * Select the appropriate output buffer for the track. | 
|  | 4226 | * | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4227 | * Tracks with effects go into their own effects chain buffer | 
|  | 4228 | * and from there into either mEffectBuffer or mSinkBuffer. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4229 | * | 
|  | 4230 | * Other tracks can use mMixerBuffer for higher precision | 
|  | 4231 | * channel accumulation.  If this buffer is enabled | 
|  | 4232 | * (mMixerBufferEnabled true), then selected tracks will accumulate | 
|  | 4233 | * into it. | 
|  | 4234 | * | 
|  | 4235 | */ | 
|  | 4236 | if (mMixerBufferEnabled | 
|  | 4237 | && (track->mainBuffer() == mSinkBuffer | 
|  | 4238 | || track->mainBuffer() == mMixerBuffer)) { | 
|  | 4239 | mAudioMixer->setParameter( | 
|  | 4240 | name, | 
|  | 4241 | AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4242 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4243 | mAudioMixer->setParameter( | 
|  | 4244 | name, | 
|  | 4245 | AudioMixer::TRACK, | 
|  | 4246 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); | 
|  | 4247 | // TODO: override track->mainBuffer()? | 
|  | 4248 | mMixerBufferValid = true; | 
|  | 4249 | } else { | 
|  | 4250 | mAudioMixer->setParameter( | 
|  | 4251 | name, | 
|  | 4252 | AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4253 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4254 | mAudioMixer->setParameter( | 
|  | 4255 | name, | 
|  | 4256 | AudioMixer::TRACK, | 
|  | 4257 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
|  | 4258 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4259 | mAudioMixer->setParameter( | 
|  | 4260 | name, | 
|  | 4261 | AudioMixer::TRACK, | 
|  | 4262 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
|  | 4263 |  | 
|  | 4264 | // reset retry count | 
|  | 4265 | track->mRetryCount = kMaxTrackRetries; | 
|  | 4266 |  | 
|  | 4267 | // If one track is ready, set the mixer ready if: | 
|  | 4268 | //  - the mixer was not ready during previous round OR | 
|  | 4269 | //  - no other track is not ready | 
|  | 4270 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
|  | 4271 | mixerStatus != MIXER_TRACKS_ENABLED) { | 
|  | 4272 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 4273 | } | 
|  | 4274 | } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4275 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4276 | ALOGV("track(%p) underrun,  framesReady(%zu) < framesDesired(%zd)", | 
|  | 4277 | track, framesReady, desiredFrames); | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4278 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4279 | } else { | 
|  | 4280 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4281 | } | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4282 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4283 | // clear effect chain input buffer if an active track underruns to avoid sending | 
|  | 4284 | // previous audio buffer again to effects | 
|  | 4285 | chain = getEffectChain_l(track->sessionId()); | 
|  | 4286 | if (chain != 0) { | 
|  | 4287 | chain->clearInputBuffer(); | 
|  | 4288 | } | 
|  | 4289 |  | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4290 | 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] | 4291 | if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
|  | 4292 | track->isStopped() || track->isPaused()) { | 
|  | 4293 | // We have consumed all the buffers of this track. | 
|  | 4294 | // Remove it from the list of active tracks. | 
|  | 4295 | // TODO: use actual buffer filling status instead of latency when available from | 
|  | 4296 | // audio HAL | 
|  | 4297 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4298 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4299 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
|  | 4300 | if (track->isStopped()) { | 
|  | 4301 | track->reset(); | 
|  | 4302 | } | 
|  | 4303 | tracksToRemove->add(track); | 
|  | 4304 | } | 
|  | 4305 | } else { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4306 | // No buffers for this track. Give it a few chances to | 
|  | 4307 | // fill a buffer, then remove it from active list. | 
|  | 4308 | if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4309 | 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] | 4310 | tracksToRemove->add(track); | 
|  | 4311 | // indicate to client process that the track was disabled because of underrun; | 
|  | 4312 | // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4313 | track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4314 | // If one track is not ready, mark the mixer also not ready if: | 
|  | 4315 | //  - the mixer was ready during previous round OR | 
|  | 4316 | //  - no other track is ready | 
|  | 4317 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
|  | 4318 | mixerStatus != MIXER_TRACKS_READY) { | 
|  | 4319 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 4320 | } | 
|  | 4321 | } | 
|  | 4322 | mAudioMixer->disable(name); | 
|  | 4323 | } | 
|  | 4324 |  | 
|  | 4325 | }   // local variable scope to avoid goto warning | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4326 |  | 
|  | 4327 | } | 
|  | 4328 |  | 
|  | 4329 | // Push the new FastMixer state if necessary | 
|  | 4330 | bool pauseAudioWatchdog = false; | 
|  | 4331 | if (didModify) { | 
|  | 4332 | state->mFastTracksGen++; | 
|  | 4333 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
|  | 4334 | if (kUseFastMixer == FastMixer_Dynamic && | 
|  | 4335 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
|  | 4336 | state->mCommand = FastMixerState::COLD_IDLE; | 
|  | 4337 | state->mColdFutexAddr = &mFastMixerFutex; | 
|  | 4338 | state->mColdGen++; | 
|  | 4339 | mFastMixerFutex = 0; | 
|  | 4340 | if (kUseFastMixer == FastMixer_Dynamic) { | 
|  | 4341 | mNormalSink = mOutputSink; | 
|  | 4342 | } | 
|  | 4343 | // If we go into cold idle, need to wait for acknowledgement | 
|  | 4344 | // so that fast mixer stops doing I/O. | 
|  | 4345 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
|  | 4346 | pauseAudioWatchdog = true; | 
|  | 4347 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4348 | } | 
|  | 4349 | if (sq != NULL) { | 
|  | 4350 | sq->end(didModify); | 
|  | 4351 | sq->push(block); | 
|  | 4352 | } | 
|  | 4353 | #ifdef AUDIO_WATCHDOG | 
|  | 4354 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
|  | 4355 | mAudioWatchdog->pause(); | 
|  | 4356 | } | 
|  | 4357 | #endif | 
|  | 4358 |  | 
|  | 4359 | // Now perform the deferred reset on fast tracks that have stopped | 
|  | 4360 | while (resetMask != 0) { | 
|  | 4361 | size_t i = __builtin_ctz(resetMask); | 
|  | 4362 | ALOG_ASSERT(i < count); | 
|  | 4363 | resetMask &= ~(1 << i); | 
|  | 4364 | sp<Track> t = mActiveTracks[i].promote(); | 
|  | 4365 | if (t == 0) { | 
|  | 4366 | continue; | 
|  | 4367 | } | 
|  | 4368 | Track* track = t.get(); | 
|  | 4369 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
|  | 4370 | track->reset(); | 
|  | 4371 | } | 
|  | 4372 |  | 
|  | 4373 | // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4374 | removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4375 |  | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4376 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { | 
|  | 4377 | mEffectBufferValid = true; | 
| Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4378 | } | 
|  | 4379 |  | 
|  | 4380 | if (mEffectBufferValid) { | 
| Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4381 | // as long as there are effects we should clear the effects buffer, to avoid | 
|  | 4382 | // passing a non-clean buffer to the effect chain | 
|  | 4383 | memset(mEffectBuffer, 0, mEffectBufferSize); | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4384 | } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4385 | // sink or mix buffer must be cleared if all tracks are connected to an | 
|  | 4386 | // effect chain as in this case the mixer will not write to the sink or mix buffer | 
|  | 4387 | // and track effects will accumulate into it | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4388 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
|  | 4389 | (mixedTracks == 0 && fastTracks > 0))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4390 | // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4391 | if (mMixerBufferValid) { | 
|  | 4392 | memset(mMixerBuffer, 0, mMixerBufferSize); | 
|  | 4393 | // TODO: In testing, mSinkBuffer below need not be cleared because | 
|  | 4394 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer | 
|  | 4395 | // after mixing. | 
|  | 4396 | // | 
|  | 4397 | // To enforce this guarantee: | 
|  | 4398 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
|  | 4399 | // (mixedTracks == 0 && fastTracks > 0)) | 
|  | 4400 | // must imply MIXER_TRACKS_READY. | 
|  | 4401 | // Later, we may clear buffers regardless, and skip much of this logic. | 
|  | 4402 | } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4403 | // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4404 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4405 | } | 
|  | 4406 |  | 
|  | 4407 | // if any fast tracks, then status is ready | 
|  | 4408 | mMixerStatusIgnoringFastTracks = mixerStatus; | 
|  | 4409 | if (fastTracks > 0) { | 
|  | 4410 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 4411 | } | 
|  | 4412 | return mixerStatus; | 
|  | 4413 | } | 
|  | 4414 |  | 
|  | 4415 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4416 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 4417 | audio_format_t format, audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4418 | { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4419 | return mAudioMixer->getTrackName(channelMask, format, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4420 | } | 
|  | 4421 |  | 
|  | 4422 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
|  | 4423 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
|  | 4424 | { | 
|  | 4425 | ALOGV("remove track (%d) and delete from mixer", name); | 
|  | 4426 | mAudioMixer->deleteTrackName(name); | 
|  | 4427 | } | 
|  | 4428 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4429 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
|  | 4430 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 4431 | status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4432 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4433 | bool reconfig = false; | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4434 | bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4435 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4436 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4437 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4438 | AutoPark<FastMixer> park(mFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4439 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4440 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 4441 | int value; | 
|  | 4442 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 4443 | reconfig = true; | 
|  | 4444 | } | 
|  | 4445 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4446 | if (!isValidPcmSinkFormat((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4447 | status = BAD_VALUE; | 
|  | 4448 | } else { | 
|  | 4449 | // no need to save value, since it's constant | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4450 | reconfig = true; | 
|  | 4451 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4452 | } | 
|  | 4453 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4454 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4455 | status = BAD_VALUE; | 
|  | 4456 | } else { | 
|  | 4457 | // no need to save value, since it's constant | 
|  | 4458 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4459 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4460 | } | 
|  | 4461 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 4462 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 4463 | // size depends on frame count and correct behavior would not be guaranteed | 
|  | 4464 | // if frame count is changed after track creation | 
|  | 4465 | if (!mTracks.isEmpty()) { | 
|  | 4466 | status = INVALID_OPERATION; | 
|  | 4467 | } else { | 
|  | 4468 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4469 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4470 | } | 
|  | 4471 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4472 | #ifdef ADD_BATTERY_DATA | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4473 | // when changing the audio output device, call addBatteryData to notify | 
|  | 4474 | // the change | 
|  | 4475 | if (mOutDevice != value) { | 
|  | 4476 | uint32_t params = 0; | 
|  | 4477 | // check whether speaker is on | 
|  | 4478 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
|  | 4479 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4480 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4481 |  | 
|  | 4482 | audio_devices_t deviceWithoutSpeaker | 
|  | 4483 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4484 | // check if any other device (except speaker) is on | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4485 | if (value & deviceWithoutSpeaker) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4486 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
|  | 4487 | } | 
|  | 4488 |  | 
|  | 4489 | if (params != 0) { | 
|  | 4490 | addBatteryData(params); | 
|  | 4491 | } | 
|  | 4492 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4493 | #endif | 
|  | 4494 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4495 | // forward device change to effects that have requested to be | 
|  | 4496 | // aware of attached audio device. | 
|  | 4497 | if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4498 | a2dpDeviceChanged = | 
|  | 4499 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4500 | mOutDevice = value; | 
|  | 4501 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 4502 | mEffectChains[i]->setDevice_l(mOutDevice); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4503 | } | 
|  | 4504 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4505 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4506 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4507 | if (status == NO_ERROR) { | 
|  | 4508 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
|  | 4509 | keyValuePair.string()); | 
|  | 4510 | if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4511 | mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4512 | mStandby = true; | 
|  | 4513 | mBytesWritten = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4514 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4515 | keyValuePair.string()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4516 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4517 | if (status == NO_ERROR && reconfig) { | 
|  | 4518 | readOutputParameters_l(); | 
|  | 4519 | delete mAudioMixer; | 
|  | 4520 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
|  | 4521 | for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4522 | int name = getTrackName_l(mTracks[i]->mChannelMask, | 
|  | 4523 | mTracks[i]->mFormat, mTracks[i]->mSessionId); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4524 | if (name < 0) { | 
|  | 4525 | break; | 
|  | 4526 | } | 
|  | 4527 | mTracks[i]->mName = name; | 
|  | 4528 | } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4529 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4530 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4531 | } | 
|  | 4532 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4533 | return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4534 | } | 
|  | 4535 |  | 
|  | 4536 |  | 
|  | 4537 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 4538 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4539 | PlaybackThread::dumpInternals(fd, args); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4540 | dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4541 | dprintf(fd, "  AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4542 | dprintf(fd, "  Master mono: %s\n", mMasterMono ? "on" : "off"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4543 |  | 
|  | 4544 | // 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] | 4545 | // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
|  | 4546 | // This is a large object so we place it on the heap. | 
|  | 4547 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
|  | 4548 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); | 
|  | 4549 | copy->dump(fd); | 
|  | 4550 | delete copy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4551 |  | 
|  | 4552 | #ifdef STATE_QUEUE_DUMP | 
|  | 4553 | // Similar for state queue | 
|  | 4554 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
|  | 4555 | observerCopy.dump(fd); | 
|  | 4556 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
|  | 4557 | mutatorCopy.dump(fd); | 
|  | 4558 | #endif | 
|  | 4559 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4560 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4561 | // Write the tee output to a .wav file | 
|  | 4562 | dumpTee(fd, mTeeSource, mId); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4563 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4564 |  | 
|  | 4565 | #ifdef AUDIO_WATCHDOG | 
|  | 4566 | if (mAudioWatchdog != 0) { | 
|  | 4567 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
|  | 4568 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
|  | 4569 | wdCopy.dump(fd); | 
|  | 4570 | } | 
|  | 4571 | #endif | 
|  | 4572 | } | 
|  | 4573 |  | 
|  | 4574 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const | 
|  | 4575 | { | 
|  | 4576 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
|  | 4577 | } | 
|  | 4578 |  | 
|  | 4579 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const | 
|  | 4580 | { | 
|  | 4581 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 4582 | } | 
|  | 4583 |  | 
|  | 4584 | void AudioFlinger::MixerThread::cacheParameters_l() | 
|  | 4585 | { | 
|  | 4586 | PlaybackThread::cacheParameters_l(); | 
|  | 4587 |  | 
|  | 4588 | // FIXME: Relaxed timing because of a certain device that can't meet latency | 
|  | 4589 | // Should be reduced to 2x after the vendor fixes the driver issue | 
|  | 4590 | // increase threshold again due to low power audio mode. The way this warning | 
|  | 4591 | // threshold is calculated and its usefulness should be reconsidered anyway. | 
|  | 4592 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
|  | 4593 | } | 
|  | 4594 |  | 
|  | 4595 | // ---------------------------------------------------------------------------- | 
|  | 4596 |  | 
|  | 4597 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4598 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) | 
|  | 4599 | :   PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4600 | // mLeftVolFloat, mRightVolFloat | 
|  | 4601 | { | 
|  | 4602 | } | 
|  | 4603 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4604 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 4605 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4606 | ThreadBase::type_t type, bool systemReady) | 
|  | 4607 | :   PlaybackThread(audioFlinger, output, id, device, type, systemReady) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4608 | // mLeftVolFloat, mRightVolFloat | 
|  | 4609 | { | 
|  | 4610 | } | 
|  | 4611 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4612 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
|  | 4613 | { | 
|  | 4614 | } | 
|  | 4615 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4616 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) | 
|  | 4617 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4618 | float left, right; | 
|  | 4619 |  | 
|  | 4620 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { | 
|  | 4621 | left = right = 0; | 
|  | 4622 | } else { | 
|  | 4623 | float typeVolume = mStreamTypes[track->streamType()].volume; | 
|  | 4624 | float v = mMasterVolume * typeVolume; | 
|  | 4625 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4626 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
|  | 4627 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
|  | 4628 | if (left > GAIN_FLOAT_UNITY) { | 
|  | 4629 | left = GAIN_FLOAT_UNITY; | 
|  | 4630 | } | 
|  | 4631 | left *= v; | 
|  | 4632 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
|  | 4633 | if (right > GAIN_FLOAT_UNITY) { | 
|  | 4634 | right = GAIN_FLOAT_UNITY; | 
|  | 4635 | } | 
|  | 4636 | right *= v; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4637 | } | 
|  | 4638 |  | 
|  | 4639 | if (lastTrack) { | 
|  | 4640 | if (left != mLeftVolFloat || right != mRightVolFloat) { | 
|  | 4641 | mLeftVolFloat = left; | 
|  | 4642 | mRightVolFloat = right; | 
|  | 4643 |  | 
|  | 4644 | // Convert volumes from float to 8.24 | 
|  | 4645 | uint32_t vl = (uint32_t)(left * (1 << 24)); | 
|  | 4646 | uint32_t vr = (uint32_t)(right * (1 << 24)); | 
|  | 4647 |  | 
|  | 4648 | // Delegate volume control to effect in track effect chain if needed | 
|  | 4649 | // only one effect chain can be present on DirectOutputThread, so if | 
|  | 4650 | // there is one, the track is connected to it | 
|  | 4651 | if (!mEffectChains.isEmpty()) { | 
|  | 4652 | mEffectChains[0]->setVolume_l(&vl, &vr); | 
|  | 4653 | left = (float)vl / (1 << 24); | 
|  | 4654 | right = (float)vr / (1 << 24); | 
|  | 4655 | } | 
|  | 4656 | if (mOutput->stream->set_volume) { | 
|  | 4657 | mOutput->stream->set_volume(mOutput->stream, left, right); | 
|  | 4658 | } | 
|  | 4659 | } | 
|  | 4660 | } | 
|  | 4661 | } | 
|  | 4662 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4663 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() | 
|  | 4664 | { | 
|  | 4665 | sp<Track> previousTrack = mPreviousTrack.promote(); | 
|  | 4666 | sp<Track> latestTrack = mLatestActiveTrack.promote(); | 
|  | 4667 |  | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4668 | if (previousTrack != 0 && latestTrack != 0) { | 
|  | 4669 | if (mType == DIRECT) { | 
|  | 4670 | if (previousTrack.get() != latestTrack.get()) { | 
|  | 4671 | mFlushPending = true; | 
|  | 4672 | } | 
|  | 4673 | } else /* mType == OFFLOAD */ { | 
|  | 4674 | if (previousTrack->sessionId() != latestTrack->sessionId()) { | 
|  | 4675 | mFlushPending = true; | 
|  | 4676 | } | 
|  | 4677 | } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4678 | } | 
|  | 4679 | PlaybackThread::onAddNewTrack_l(); | 
|  | 4680 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4681 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4682 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( | 
|  | 4683 | Vector< sp<Track> > *tracksToRemove | 
|  | 4684 | ) | 
|  | 4685 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4686 | size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4687 | mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4688 | bool doHwPause = false; | 
|  | 4689 | bool doHwResume = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4690 |  | 
|  | 4691 | // find out which tracks need to be processed | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4692 | for (size_t i = 0; i < count; i++) { | 
|  | 4693 | sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4694 | // The track died recently | 
|  | 4695 | if (t == 0) { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4696 | continue; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4697 | } | 
|  | 4698 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4699 | if (t->isInvalid()) { | 
|  | 4700 | ALOGW("An invalidated track shouldn't be in active list"); | 
|  | 4701 | tracksToRemove->add(t); | 
|  | 4702 | continue; | 
|  | 4703 | } | 
|  | 4704 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4705 | Track* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4706 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4707 | audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4708 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4709 | // Only consider last track started for volume and mixer state control. | 
|  | 4710 | // In theory an older track could underrun and restart after the new one starts | 
|  | 4711 | // but as we only care about the transition phase between two tracks on a | 
|  | 4712 | // direct output, it is not a problem to ignore the underrun case. | 
|  | 4713 | sp<Track> l = mLatestActiveTrack.promote(); | 
|  | 4714 | bool last = l.get() == track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4715 |  | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4716 | if (track->isPausing()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4717 | track->setPaused(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4718 | if (mHwSupportsPause && last && !mHwPaused) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4719 | doHwPause = true; | 
|  | 4720 | mHwPaused = true; | 
|  | 4721 | } | 
|  | 4722 | tracksToRemove->add(track); | 
|  | 4723 | } else if (track->isFlushPending()) { | 
|  | 4724 | track->flushAck(); | 
|  | 4725 | if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4726 | mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4727 | } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4728 | } else if (track->isResumePending()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4729 | track->resumeAck(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4730 | if (last && mHwPaused) { | 
|  | 4731 | doHwResume = true; | 
|  | 4732 | mHwPaused = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4733 | } | 
|  | 4734 | } | 
|  | 4735 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4736 | // The first time a track is added we wait | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4737 | // for all its buffers to be filled before processing it. | 
|  | 4738 | // Allow draining the buffer in case the client | 
|  | 4739 | // app does not call stop() and relies on underrun to stop: | 
|  | 4740 | // hence the test on (track->mRetryCount > 1). | 
|  | 4741 | // If retryCount<=1 then track is about to underrun and be removed. | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4742 | // Do not use a high threshold for compressed audio. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4743 | uint32_t minFrames; | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4744 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4745 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4746 | minFrames = mNormalFrameCount; | 
|  | 4747 | } else { | 
|  | 4748 | minFrames = 1; | 
|  | 4749 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4750 |  | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4751 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && | 
|  | 4752 | !track->isStopping_2() && !track->isStopped()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4753 | { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4754 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4755 |  | 
|  | 4756 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 4757 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4758 | // make sure processVolume_l() will apply new volume even if 0 | 
|  | 4759 | mLeftVolFloat = mRightVolFloat = -1.0; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4760 | if (!mHwSupportsPause) { | 
|  | 4761 | track->resumeAck(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4762 | } | 
|  | 4763 | } | 
|  | 4764 |  | 
|  | 4765 | // compute volume for this track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4766 | processVolume_l(track, last); | 
|  | 4767 | if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4768 | sp<Track> previousTrack = mPreviousTrack.promote(); | 
|  | 4769 | if (previousTrack != 0) { | 
|  | 4770 | if (track != previousTrack.get()) { | 
|  | 4771 | // Flush any data still being written from last track | 
|  | 4772 | mBytesRemaining = 0; | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4773 | // Invalidate previous track to force a seek when resuming. | 
|  | 4774 | previousTrack->invalidate(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4775 | } | 
|  | 4776 | } | 
|  | 4777 | mPreviousTrack = track; | 
|  | 4778 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4779 | // reset retry count | 
|  | 4780 | track->mRetryCount = kMaxTrackRetriesDirect; | 
|  | 4781 | mActiveTrack = t; | 
|  | 4782 | mixerStatus = MIXER_TRACKS_READY; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4783 | if (mHwPaused) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4784 | doHwResume = true; | 
|  | 4785 | mHwPaused = false; | 
|  | 4786 | } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4787 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4788 | } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4789 | // clear effect chain input buffer if the last active track started underruns | 
|  | 4790 | // to avoid sending previous audio buffer again to effects | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4791 | if (!mEffectChains.isEmpty() && last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4792 | mEffectChains[0]->clearInputBuffer(); | 
|  | 4793 | } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4794 | if (track->isStopping_1()) { | 
|  | 4795 | track->mState = TrackBase::STOPPING_2; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4796 | if (last && mHwPaused) { | 
|  | 4797 | doHwResume = true; | 
|  | 4798 | mHwPaused = false; | 
|  | 4799 | } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4800 | } | 
|  | 4801 | if ((track->sharedBuffer() != 0) || track->isStopped() || | 
|  | 4802 | track->isStopping_2() || track->isPaused()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4803 | // We have consumed all the buffers of this track. | 
|  | 4804 | // Remove it from the list of active tracks. | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4805 | size_t audioHALFrames; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4806 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4807 | audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
|  | 4808 | } else { | 
|  | 4809 | audioHALFrames = 0; | 
|  | 4810 | } | 
|  | 4811 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4812 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4813 | if (mStandby || !last || | 
|  | 4814 | track->presentationComplete(framesWritten, audioHALFrames)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4815 | if (track->isStopping_2()) { | 
|  | 4816 | track->mState = TrackBase::STOPPED; | 
|  | 4817 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4818 | if (track->isStopped()) { | 
|  | 4819 | track->reset(); | 
|  | 4820 | } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4821 | tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4822 | } | 
|  | 4823 | } else { | 
|  | 4824 | // No buffers for this track. Give it a few chances to | 
|  | 4825 | // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4826 | // Only consider last track started for mixer state control | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4827 | if (--(track->mRetryCount) <= 0) { | 
|  | 4828 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4829 | tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4830 | // indicate to client process that the track was disabled because of underrun; | 
|  | 4831 | // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4832 | track->disable(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4833 | } else if (last) { | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4834 | ALOGW("pause because of UNDERRUN, framesReady = %zu," | 
|  | 4835 | "minFrames = %u, mFormat = %#x", | 
|  | 4836 | track->framesReady(), minFrames, mFormat); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4837 | mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4838 | if (mHwSupportsPause && !mHwPaused && !mStandby) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4839 | doHwPause = true; | 
|  | 4840 | mHwPaused = true; | 
|  | 4841 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4842 | } | 
|  | 4843 | } | 
|  | 4844 | } | 
|  | 4845 | } | 
|  | 4846 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4847 | // 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] | 4848 | if (!mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4849 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 4850 | if (mTracks[i]->isFlushPending()) { | 
|  | 4851 | mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4852 | mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4853 | } | 
|  | 4854 | } | 
|  | 4855 | } | 
|  | 4856 |  | 
|  | 4857 | // make sure the pause/flush/resume sequence is executed in the right order. | 
|  | 4858 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
|  | 4859 | // before flush and then resume HW. This can happen in case of pause/flush/resume | 
|  | 4860 | // if resume is received before pause is executed. | 
|  | 4861 | if (mHwSupportsPause && !mStandby && | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4862 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4863 | mOutput->stream->pause(mOutput->stream); | 
|  | 4864 | } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4865 | if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4866 | flushHw_l(); | 
|  | 4867 | } | 
|  | 4868 | if (mHwSupportsPause && !mStandby && doHwResume) { | 
|  | 4869 | mOutput->stream->resume(mOutput->stream); | 
|  | 4870 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4871 | // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4872 | removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4873 |  | 
|  | 4874 | return mixerStatus; | 
|  | 4875 | } | 
|  | 4876 |  | 
|  | 4877 | void AudioFlinger::DirectOutputThread::threadLoop_mix() | 
|  | 4878 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4879 | size_t frameCount = mFrameCount; | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4880 | int8_t *curBuf = (int8_t *)mSinkBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4881 | // output audio to hardware | 
|  | 4882 | while (frameCount) { | 
| Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4883 | AudioBufferProvider::Buffer buffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4884 | buffer.frameCount = frameCount; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4885 | status_t status = mActiveTrack->getNextBuffer(&buffer); | 
|  | 4886 | if (status != NO_ERROR || buffer.raw == NULL) { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4887 | // no need to pad with 0 for compressed audio | 
|  | 4888 | if (audio_has_proportional_frames(mFormat)) { | 
|  | 4889 | memset(curBuf, 0, frameCount * mFrameSize); | 
|  | 4890 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4891 | break; | 
|  | 4892 | } | 
|  | 4893 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
|  | 4894 | frameCount -= buffer.frameCount; | 
|  | 4895 | curBuf += buffer.frameCount * mFrameSize; | 
|  | 4896 | mActiveTrack->releaseBuffer(&buffer); | 
|  | 4897 | } | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4898 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4899 | mSleepTimeUs = 0; | 
|  | 4900 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4901 | mActiveTrack.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4902 | } | 
|  | 4903 |  | 
|  | 4904 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() | 
|  | 4905 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4906 | // do not write to HAL when paused | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4907 | if (mHwPaused || (usesHwAvSync() && mStandby)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4908 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4909 | return; | 
|  | 4910 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4911 | if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4912 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4913 | mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4914 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4915 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4916 | } | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4917 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4918 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4919 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4920 | } | 
|  | 4921 | } | 
|  | 4922 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4923 | void AudioFlinger::DirectOutputThread::threadLoop_exit() | 
|  | 4924 | { | 
|  | 4925 | { | 
|  | 4926 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4927 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 4928 | if (mTracks[i]->isFlushPending()) { | 
|  | 4929 | mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4930 | mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4931 | } | 
|  | 4932 | } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4933 | if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4934 | flushHw_l(); | 
|  | 4935 | } | 
|  | 4936 | } | 
|  | 4937 | PlaybackThread::threadLoop_exit(); | 
|  | 4938 | } | 
|  | 4939 |  | 
|  | 4940 | // must be called with thread mutex locked | 
|  | 4941 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() | 
|  | 4942 | { | 
|  | 4943 | bool trackPaused = false; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4944 | bool trackStopped = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4945 |  | 
| vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 4946 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { | 
|  | 4947 | return !mStandby; | 
|  | 4948 | } | 
|  | 4949 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4950 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack | 
|  | 4951 | // after a timeout and we will enter standby then. | 
|  | 4952 | if (mTracks.size() > 0) { | 
|  | 4953 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4954 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || | 
|  | 4955 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4956 | } | 
|  | 4957 |  | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4958 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4959 | } | 
|  | 4960 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4961 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4962 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 4963 | audio_format_t format __unused, audio_session_t sessionId __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4964 | { | 
|  | 4965 | return 0; | 
|  | 4966 | } | 
|  | 4967 |  | 
|  | 4968 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4969 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4970 | { | 
|  | 4971 | } | 
|  | 4972 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4973 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
|  | 4974 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 4975 | status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4976 | { | 
|  | 4977 | bool reconfig = false; | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4978 | bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4979 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4980 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4981 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4982 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 4983 | int value; | 
|  | 4984 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 4985 | // forward device change to effects that have requested to be | 
|  | 4986 | // aware of attached audio device. | 
|  | 4987 | if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4988 | a2dpDeviceChanged = | 
|  | 4989 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4990 | mOutDevice = value; | 
|  | 4991 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 4992 | mEffectChains[i]->setDevice_l(mOutDevice); | 
| Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4993 | } | 
|  | 4994 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4995 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4996 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 4997 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 4998 | // size depends on frame count and correct behavior would not be garantied | 
|  | 4999 | // if frame count is changed after track creation | 
|  | 5000 | if (!mTracks.isEmpty()) { | 
|  | 5001 | status = INVALID_OPERATION; | 
|  | 5002 | } else { | 
|  | 5003 | reconfig = true; | 
|  | 5004 | } | 
|  | 5005 | } | 
|  | 5006 | if (status == NO_ERROR) { | 
|  | 5007 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
|  | 5008 | keyValuePair.string()); | 
|  | 5009 | if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5010 | mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5011 | mStandby = true; | 
|  | 5012 | mBytesWritten = 0; | 
|  | 5013 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
|  | 5014 | keyValuePair.string()); | 
|  | 5015 | } | 
|  | 5016 | if (status == NO_ERROR && reconfig) { | 
|  | 5017 | readOutputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5018 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5019 | } | 
|  | 5020 | } | 
|  | 5021 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5022 | return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5023 | } | 
|  | 5024 |  | 
|  | 5025 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const | 
|  | 5026 | { | 
|  | 5027 | uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5028 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5029 | time = PlaybackThread::activeSleepTimeUs(); | 
|  | 5030 | } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5031 | time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5032 | } | 
|  | 5033 | return time; | 
|  | 5034 | } | 
|  | 5035 |  | 
|  | 5036 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const | 
|  | 5037 | { | 
|  | 5038 | uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5039 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5040 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
|  | 5041 | } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5042 | time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5043 | } | 
|  | 5044 | return time; | 
|  | 5045 | } | 
|  | 5046 |  | 
|  | 5047 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const | 
|  | 5048 | { | 
|  | 5049 | uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5050 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5051 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 5052 | } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5053 | time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5054 | } | 
|  | 5055 | return time; | 
|  | 5056 | } | 
|  | 5057 |  | 
|  | 5058 | void AudioFlinger::DirectOutputThread::cacheParameters_l() | 
|  | 5059 | { | 
|  | 5060 | PlaybackThread::cacheParameters_l(); | 
|  | 5061 |  | 
|  | 5062 | // use shorter standby delay as on normal output to release | 
|  | 5063 | // hardware resources as soon as possible | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5064 | // no delay on outputs with HW A/V sync | 
|  | 5065 | if (usesHwAvSync()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5066 | mStandbyDelayNs = 0; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5067 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5068 | mStandbyDelayNs = kOffloadStandbyDelayNs; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5069 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5070 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5071 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5072 | } | 
|  | 5073 |  | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5074 | void AudioFlinger::DirectOutputThread::flushHw_l() | 
|  | 5075 | { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5076 | mOutput->flush(); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5077 | mHwPaused = false; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5078 | mFlushPending = false; | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5079 | } | 
|  | 5080 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5081 | // ---------------------------------------------------------------------------- | 
|  | 5082 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5083 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5084 | const wp<AudioFlinger::PlaybackThread>& playbackThread) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5085 | :   Thread(false /*canCallJava*/), | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5086 | mPlaybackThread(playbackThread), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5087 | mWriteAckSequence(0), | 
|  | 5088 | mDrainSequence(0) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5089 | { | 
|  | 5090 | } | 
|  | 5091 |  | 
|  | 5092 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() | 
|  | 5093 | { | 
|  | 5094 | } | 
|  | 5095 |  | 
|  | 5096 | void AudioFlinger::AsyncCallbackThread::onFirstRef() | 
|  | 5097 | { | 
|  | 5098 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); | 
|  | 5099 | } | 
|  | 5100 |  | 
|  | 5101 | bool AudioFlinger::AsyncCallbackThread::threadLoop() | 
|  | 5102 | { | 
|  | 5103 | while (!exitPending()) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5104 | uint32_t writeAckSequence; | 
|  | 5105 | uint32_t drainSequence; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5106 |  | 
|  | 5107 | { | 
|  | 5108 | Mutex::Autolock _l(mLock); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5109 | while (!((mWriteAckSequence & 1) || | 
|  | 5110 | (mDrainSequence & 1) || | 
|  | 5111 | exitPending())) { | 
|  | 5112 | mWaitWorkCV.wait(mLock); | 
|  | 5113 | } | 
|  | 5114 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5115 | if (exitPending()) { | 
|  | 5116 | break; | 
|  | 5117 | } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5118 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", | 
|  | 5119 | mWriteAckSequence, mDrainSequence); | 
|  | 5120 | writeAckSequence = mWriteAckSequence; | 
|  | 5121 | mWriteAckSequence &= ~1; | 
|  | 5122 | drainSequence = mDrainSequence; | 
|  | 5123 | mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5124 | } | 
|  | 5125 | { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5126 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); | 
|  | 5127 | if (playbackThread != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5128 | if (writeAckSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5129 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5130 | } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5131 | if (drainSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5132 | playbackThread->resetDraining(drainSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5133 | } | 
|  | 5134 | } | 
|  | 5135 | } | 
|  | 5136 | } | 
|  | 5137 | return false; | 
|  | 5138 | } | 
|  | 5139 |  | 
|  | 5140 | void AudioFlinger::AsyncCallbackThread::exit() | 
|  | 5141 | { | 
|  | 5142 | ALOGV("AsyncCallbackThread::exit"); | 
|  | 5143 | Mutex::Autolock _l(mLock); | 
|  | 5144 | requestExit(); | 
|  | 5145 | mWaitWorkCV.broadcast(); | 
|  | 5146 | } | 
|  | 5147 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5148 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5149 | { | 
|  | 5150 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5151 | // bit 0 is cleared | 
|  | 5152 | mWriteAckSequence = sequence << 1; | 
|  | 5153 | } | 
|  | 5154 |  | 
|  | 5155 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() | 
|  | 5156 | { | 
|  | 5157 | Mutex::Autolock _l(mLock); | 
|  | 5158 | // ignore unexpected callbacks | 
|  | 5159 | if (mWriteAckSequence & 2) { | 
|  | 5160 | mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5161 | mWaitWorkCV.signal(); | 
|  | 5162 | } | 
|  | 5163 | } | 
|  | 5164 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5165 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5166 | { | 
|  | 5167 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5168 | // bit 0 is cleared | 
|  | 5169 | mDrainSequence = sequence << 1; | 
|  | 5170 | } | 
|  | 5171 |  | 
|  | 5172 | void AudioFlinger::AsyncCallbackThread::resetDraining() | 
|  | 5173 | { | 
|  | 5174 | Mutex::Autolock _l(mLock); | 
|  | 5175 | // ignore unexpected callbacks | 
|  | 5176 | if (mDrainSequence & 2) { | 
|  | 5177 | mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5178 | mWaitWorkCV.signal(); | 
|  | 5179 | } | 
|  | 5180 | } | 
|  | 5181 |  | 
|  | 5182 |  | 
|  | 5183 | // ---------------------------------------------------------------------------- | 
|  | 5184 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5185 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) | 
|  | 5186 | :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), | 
| Andy Hung | 39ee5a4 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5187 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), | 
|  | 5188 | mOffloadUnderrunPosition(~0LL) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5189 | { | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5190 | //FIXME: mStandby should be set to true by ThreadBase constructor | 
|  | 5191 | mStandby = true; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5192 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5193 | } | 
|  | 5194 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5195 | void AudioFlinger::OffloadThread::threadLoop_exit() | 
|  | 5196 | { | 
|  | 5197 | if (mFlushPending || mHwPaused) { | 
|  | 5198 | // If a flush is pending or track was paused, just discard buffered data | 
|  | 5199 | flushHw_l(); | 
|  | 5200 | } else { | 
|  | 5201 | mMixerStatus = MIXER_DRAIN_ALL; | 
|  | 5202 | threadLoop_drain(); | 
|  | 5203 | } | 
| Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5204 | if (mUseAsyncWrite) { | 
|  | 5205 | ALOG_ASSERT(mCallbackThread != 0); | 
|  | 5206 | mCallbackThread->exit(); | 
|  | 5207 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5208 | PlaybackThread::threadLoop_exit(); | 
|  | 5209 | } | 
|  | 5210 |  | 
|  | 5211 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( | 
|  | 5212 | Vector< sp<Track> > *tracksToRemove | 
|  | 5213 | ) | 
|  | 5214 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5215 | size_t count = mActiveTracks.size(); | 
|  | 5216 |  | 
|  | 5217 | mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5218 | bool doHwPause = false; | 
|  | 5219 | bool doHwResume = false; | 
|  | 5220 |  | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5221 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5222 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5223 | // find out which tracks need to be processed | 
|  | 5224 | for (size_t i = 0; i < count; i++) { | 
|  | 5225 | sp<Track> t = mActiveTracks[i].promote(); | 
|  | 5226 | // The track died recently | 
|  | 5227 | if (t == 0) { | 
|  | 5228 | continue; | 
|  | 5229 | } | 
|  | 5230 | Track* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5231 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5232 | audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5233 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5234 | // Only consider last track started for volume and mixer state control. | 
|  | 5235 | // In theory an older track could underrun and restart after the new one starts | 
|  | 5236 | // but as we only care about the transition phase between two tracks on a | 
|  | 5237 | // direct output, it is not a problem to ignore the underrun case. | 
|  | 5238 | sp<Track> l = mLatestActiveTrack.promote(); | 
|  | 5239 | bool last = l.get() == track; | 
|  | 5240 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5241 | if (track->isInvalid()) { | 
|  | 5242 | ALOGW("An invalidated track shouldn't be in active list"); | 
|  | 5243 | tracksToRemove->add(track); | 
|  | 5244 | continue; | 
|  | 5245 | } | 
|  | 5246 |  | 
|  | 5247 | if (track->mState == TrackBase::IDLE) { | 
|  | 5248 | ALOGW("An idle track shouldn't be in active list"); | 
|  | 5249 | continue; | 
|  | 5250 | } | 
|  | 5251 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5252 | if (track->isPausing()) { | 
|  | 5253 | track->setPaused(); | 
|  | 5254 | if (last) { | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5255 | if (mHwSupportsPause && !mHwPaused) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5256 | doHwPause = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5257 | mHwPaused = true; | 
|  | 5258 | } | 
|  | 5259 | // If we were part way through writing the mixbuffer to | 
|  | 5260 | // the HAL we must save this until we resume | 
|  | 5261 | // BUG - this will be wrong if a different track is made active, | 
|  | 5262 | // in that case we want to discard the pending data in the | 
|  | 5263 | // mixbuffer and tell the client to present it again when the | 
|  | 5264 | // track is resumed | 
|  | 5265 | mPausedWriteLength = mCurrentWriteLength; | 
|  | 5266 | mPausedBytesRemaining = mBytesRemaining; | 
|  | 5267 | mBytesRemaining = 0;    // stop writing | 
|  | 5268 | } | 
|  | 5269 | tracksToRemove->add(track); | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5270 | } else if (track->isFlushPending()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5271 | if (track->isStopping_1()) { | 
|  | 5272 | track->mRetryCount = kMaxTrackStopRetriesOffload; | 
|  | 5273 | } else { | 
|  | 5274 | track->mRetryCount = kMaxTrackRetriesOffload; | 
|  | 5275 | } | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5276 | track->flushAck(); | 
|  | 5277 | if (last) { | 
|  | 5278 | mFlushPending = true; | 
|  | 5279 | } | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5280 | } else if (track->isResumePending()){ | 
|  | 5281 | track->resumeAck(); | 
|  | 5282 | if (last) { | 
|  | 5283 | if (mPausedBytesRemaining) { | 
|  | 5284 | // Need to continue write that was interrupted | 
|  | 5285 | mCurrentWriteLength = mPausedWriteLength; | 
|  | 5286 | mBytesRemaining = mPausedBytesRemaining; | 
|  | 5287 | mPausedBytesRemaining = 0; | 
|  | 5288 | } | 
|  | 5289 | if (mHwPaused) { | 
|  | 5290 | doHwResume = true; | 
|  | 5291 | mHwPaused = false; | 
|  | 5292 | // threadLoop_mix() will handle the case that we need to | 
|  | 5293 | // resume an interrupted write | 
|  | 5294 | } | 
|  | 5295 | // enable write to audio HAL | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5296 | mSleepTimeUs = 0; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5297 |  | 
|  | 5298 | // Do not handle new data in this iteration even if track->framesReady() | 
|  | 5299 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 5300 | } | 
|  | 5301 | }  else if (track->framesReady() && track->isReady() && | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5302 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5303 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5304 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 5305 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5306 | // make sure processVolume_l() will apply new volume even if 0 | 
|  | 5307 | mLeftVolFloat = mRightVolFloat = -1.0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5308 | } | 
|  | 5309 |  | 
|  | 5310 | if (last) { | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5311 | sp<Track> previousTrack = mPreviousTrack.promote(); | 
|  | 5312 | if (previousTrack != 0) { | 
|  | 5313 | if (track != previousTrack.get()) { | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5314 | // Flush any data still being written from last track | 
|  | 5315 | mBytesRemaining = 0; | 
|  | 5316 | if (mPausedBytesRemaining) { | 
|  | 5317 | // Last track was paused so we also need to flush saved | 
|  | 5318 | // mixbuffer state and invalidate track so that it will | 
|  | 5319 | // re-submit that unwritten data when it is next resumed | 
|  | 5320 | mPausedBytesRemaining = 0; | 
|  | 5321 | // Invalidate is a bit drastic - would be more efficient | 
|  | 5322 | // to have a flag to tell client that some of the | 
|  | 5323 | // previously written data was lost | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5324 | previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5325 | } | 
|  | 5326 | // flush data already sent to the DSP if changing audio session as audio | 
|  | 5327 | // comes from a different source. Also invalidate previous track to force a | 
|  | 5328 | // seek when resuming. | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5329 | if (previousTrack->sessionId() != track->sessionId()) { | 
|  | 5330 | previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5331 | } | 
|  | 5332 | } | 
|  | 5333 | } | 
|  | 5334 | mPreviousTrack = track; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5335 | // reset retry count | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5336 | if (track->isStopping_1()) { | 
|  | 5337 | track->mRetryCount = kMaxTrackStopRetriesOffload; | 
|  | 5338 | } else { | 
|  | 5339 | track->mRetryCount = kMaxTrackRetriesOffload; | 
|  | 5340 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5341 | mActiveTrack = t; | 
|  | 5342 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 5343 | } | 
|  | 5344 | } else { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5345 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5346 | if (track->isStopping_1()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5347 | if (--(track->mRetryCount) <= 0) { | 
|  | 5348 | // Hardware buffer can hold a large amount of audio so we must | 
|  | 5349 | // wait for all current track's data to drain before we say | 
|  | 5350 | // that the track is stopped. | 
|  | 5351 | if (mBytesRemaining == 0) { | 
|  | 5352 | // Only start draining when all data in mixbuffer | 
|  | 5353 | // has been written | 
|  | 5354 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); | 
|  | 5355 | track->mState = TrackBase::STOPPING_2; // so presentation completes after | 
|  | 5356 | // drain do not drain if no data was ever sent to HAL (mStandby == true) | 
|  | 5357 | if (last && !mStandby) { | 
|  | 5358 | // do not modify drain sequence if we are already draining. This happens | 
|  | 5359 | // when resuming from pause after drain. | 
|  | 5360 | if ((mDrainSequence & 1) == 0) { | 
|  | 5361 | mSleepTimeUs = 0; | 
|  | 5362 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
|  | 5363 | mixerStatus = MIXER_DRAIN_TRACK; | 
|  | 5364 | mDrainSequence += 2; | 
|  | 5365 | } | 
|  | 5366 | if (mHwPaused) { | 
|  | 5367 | // It is possible to move from PAUSED to STOPPING_1 without | 
|  | 5368 | // a resume so we must ensure hardware is running | 
|  | 5369 | doHwResume = true; | 
|  | 5370 | mHwPaused = false; | 
|  | 5371 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5372 | } | 
|  | 5373 | } | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5374 | } else if (last) { | 
|  | 5375 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); | 
|  | 5376 | mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5377 | } | 
|  | 5378 | } else if (track->isStopping_2()) { | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5379 | // Drain has completed or we are in standby, signal presentation complete | 
|  | 5380 | if (!(mDrainSequence & 1) || !last || mStandby) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5381 | track->mState = TrackBase::STOPPED; | 
|  | 5382 | size_t audioHALFrames = | 
|  | 5383 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5384 | int64_t framesWritten = | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5385 | mBytesWritten / mOutput->getFrameSize(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5386 | track->presentationComplete(framesWritten, audioHALFrames); | 
|  | 5387 | track->reset(); | 
|  | 5388 | tracksToRemove->add(track); | 
|  | 5389 | } | 
|  | 5390 | } else { | 
|  | 5391 | // No buffers for this track. Give it a few chances to | 
|  | 5392 | // fill a buffer, then remove it from active list. | 
|  | 5393 | if (--(track->mRetryCount) <= 0) { | 
| Andy Hung | 39ee5a4 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5394 | bool running = false; | 
|  | 5395 | if (mOutput->stream->get_presentation_position != nullptr) { | 
|  | 5396 | uint64_t position = 0; | 
|  | 5397 | struct timespec unused; | 
|  | 5398 | // The running check restarts the retry counter at least once. | 
|  | 5399 | int ret = mOutput->stream->get_presentation_position( | 
|  | 5400 | mOutput->stream, &position, &unused); | 
|  | 5401 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { | 
|  | 5402 | running = true; | 
|  | 5403 | mOffloadUnderrunPosition = position; | 
|  | 5404 | } | 
|  | 5405 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, | 
|  | 5406 | (long long)position, (long long)mOffloadUnderrunPosition); | 
|  | 5407 | } | 
|  | 5408 | if (running) { // still running, give us more time. | 
|  | 5409 | track->mRetryCount = kMaxTrackRetriesOffload; | 
|  | 5410 | } else { | 
|  | 5411 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", | 
|  | 5412 | track->name()); | 
|  | 5413 | tracksToRemove->add(track); | 
|  | 5414 | // indicate to client process that the track was disabled because of underrun; | 
|  | 5415 | // it will then automatically call start() when data is available | 
|  | 5416 | track->disable(); | 
|  | 5417 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5418 | } else if (last){ | 
|  | 5419 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 5420 | } | 
|  | 5421 | } | 
|  | 5422 | } | 
|  | 5423 | // compute volume for this track | 
|  | 5424 | processVolume_l(track, last); | 
|  | 5425 | } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5426 |  | 
| Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5427 | // make sure the pause/flush/resume sequence is executed in the right order. | 
|  | 5428 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
|  | 5429 | // before flush and then resume HW. This can happen in case of pause/flush/resume | 
|  | 5430 | // if resume is received before pause is executed. | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5431 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5432 | mOutput->stream->pause(mOutput->stream); | 
|  | 5433 | } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5434 | if (mFlushPending) { | 
|  | 5435 | flushHw_l(); | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5436 | } | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5437 | if (!mStandby && doHwResume) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5438 | mOutput->stream->resume(mOutput->stream); | 
|  | 5439 | } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5440 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5441 | // remove all the tracks that need to be... | 
|  | 5442 | removeTracks_l(*tracksToRemove); | 
|  | 5443 |  | 
|  | 5444 | return mixerStatus; | 
|  | 5445 | } | 
|  | 5446 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5447 | // must be called with thread mutex locked | 
|  | 5448 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() | 
|  | 5449 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5450 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", | 
|  | 5451 | mWriteAckSequence, mDrainSequence); | 
|  | 5452 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5453 | return true; | 
|  | 5454 | } | 
|  | 5455 | return false; | 
|  | 5456 | } | 
|  | 5457 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5458 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() | 
|  | 5459 | { | 
|  | 5460 | Mutex::Autolock _l(mLock); | 
|  | 5461 | return waitingAsyncCallback_l(); | 
|  | 5462 | } | 
|  | 5463 |  | 
|  | 5464 | void AudioFlinger::OffloadThread::flushHw_l() | 
|  | 5465 | { | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5466 | DirectOutputThread::flushHw_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5467 | // Flush anything still waiting in the mixbuffer | 
|  | 5468 | mCurrentWriteLength = 0; | 
|  | 5469 | mBytesRemaining = 0; | 
|  | 5470 | mPausedWriteLength = 0; | 
|  | 5471 | mPausedBytesRemaining = 0; | 
| Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5472 | // reset bytes written count to reflect that DSP buffers are empty after flush. | 
|  | 5473 | mBytesWritten = 0; | 
| Andy Hung | 39ee5a4 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5474 | mOffloadUnderrunPosition = ~0LL; | 
| Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5475 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5476 | if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5477 | // discard any pending drain or write ack by incrementing sequence | 
|  | 5478 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
|  | 5479 | mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5480 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5481 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
|  | 5482 | mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5483 | } | 
|  | 5484 | } | 
|  | 5485 |  | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5486 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) | 
|  | 5487 | { | 
|  | 5488 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5489 | if (PlaybackThread::invalidateTracks_l(streamType)) { | 
|  | 5490 | mFlushPending = true; | 
|  | 5491 | } | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5492 | } | 
|  | 5493 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5494 | // ---------------------------------------------------------------------------- | 
|  | 5495 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5496 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5497 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5498 | :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5499 | systemReady, DUPLICATING), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5500 | mWaitTimeMs(UINT_MAX) | 
|  | 5501 | { | 
|  | 5502 | addOutputTrack(mainThread); | 
|  | 5503 | } | 
|  | 5504 |  | 
|  | 5505 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
|  | 5506 | { | 
|  | 5507 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 5508 | mOutputTracks[i]->destroy(); | 
|  | 5509 | } | 
|  | 5510 | } | 
|  | 5511 |  | 
|  | 5512 | void AudioFlinger::DuplicatingThread::threadLoop_mix() | 
|  | 5513 | { | 
|  | 5514 | // mix buffers... | 
|  | 5515 | if (outputsReady(outputTracks)) { | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5516 | mAudioMixer->process(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5517 | } else { | 
| Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5518 | if (mMixerBufferValid) { | 
|  | 5519 | memset(mMixerBuffer, 0, mMixerBufferSize); | 
|  | 5520 | } else { | 
|  | 5521 | memset(mSinkBuffer, 0, mSinkBufferSize); | 
|  | 5522 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5523 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5524 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5525 | writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5526 | mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5527 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5528 | } | 
|  | 5529 |  | 
|  | 5530 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() | 
|  | 5531 | { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5532 | if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5533 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5534 | mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5535 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5536 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5537 | } | 
|  | 5538 | } else if (mBytesWritten != 0) { | 
|  | 5539 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
|  | 5540 | writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5541 | memset(mSinkBuffer, 0, mSinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5542 | } else { | 
|  | 5543 | // flush remaining overflow buffers in output tracks | 
|  | 5544 | writeFrames = 0; | 
|  | 5545 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5546 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5547 | } | 
|  | 5548 | } | 
|  | 5549 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5550 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5551 | { | 
|  | 5552 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5553 | outputTracks[i]->write(mSinkBuffer, writeFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5554 | } | 
| Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5555 | mStandby = false; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5556 | return (ssize_t)mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5557 | } | 
|  | 5558 |  | 
|  | 5559 | void AudioFlinger::DuplicatingThread::threadLoop_standby() | 
|  | 5560 | { | 
|  | 5561 | // DuplicatingThread implements standby by stopping all tracks | 
|  | 5562 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 5563 | outputTracks[i]->stop(); | 
|  | 5564 | } | 
|  | 5565 | } | 
|  | 5566 |  | 
|  | 5567 | void AudioFlinger::DuplicatingThread::saveOutputTracks() | 
|  | 5568 | { | 
|  | 5569 | outputTracks = mOutputTracks; | 
|  | 5570 | } | 
|  | 5571 |  | 
|  | 5572 | void AudioFlinger::DuplicatingThread::clearOutputTracks() | 
|  | 5573 | { | 
|  | 5574 | outputTracks.clear(); | 
|  | 5575 | } | 
|  | 5576 |  | 
|  | 5577 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
|  | 5578 | { | 
|  | 5579 | Mutex::Autolock _l(mLock); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5580 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. | 
|  | 5581 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. | 
|  | 5582 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. | 
|  | 5583 | const size_t frameCount = | 
|  | 5584 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); | 
|  | 5585 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity | 
|  | 5586 | // from different OutputTracks and their associated MixerThreads (e.g. one may | 
|  | 5587 | // nearly empty and the other may be dropping data). | 
|  | 5588 |  | 
|  | 5589 | sp<OutputTrack> outputTrack = new OutputTrack(thread, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5590 | this, | 
|  | 5591 | mSampleRate, | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5592 | mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5593 | mChannelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5594 | frameCount, | 
|  | 5595 | IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5596 | if (outputTrack->cblk() != NULL) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5597 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5598 | mOutputTracks.add(outputTrack); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5599 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5600 | updateWaitTime_l(); | 
|  | 5601 | } | 
|  | 5602 | } | 
|  | 5603 |  | 
|  | 5604 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
|  | 5605 | { | 
|  | 5606 | Mutex::Autolock _l(mLock); | 
|  | 5607 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 5608 | if (mOutputTracks[i]->thread() == thread) { | 
|  | 5609 | mOutputTracks[i]->destroy(); | 
|  | 5610 | mOutputTracks.removeAt(i); | 
|  | 5611 | updateWaitTime_l(); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5612 | if (thread->getOutput() == mOutput) { | 
|  | 5613 | mOutput = NULL; | 
|  | 5614 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5615 | return; | 
|  | 5616 | } | 
|  | 5617 | } | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5618 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5619 | } | 
|  | 5620 |  | 
|  | 5621 | // caller must hold mLock | 
|  | 5622 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() | 
|  | 5623 | { | 
|  | 5624 | mWaitTimeMs = UINT_MAX; | 
|  | 5625 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 5626 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
|  | 5627 | if (strong != 0) { | 
|  | 5628 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
|  | 5629 | if (waitTimeMs < mWaitTimeMs) { | 
|  | 5630 | mWaitTimeMs = waitTimeMs; | 
|  | 5631 | } | 
|  | 5632 | } | 
|  | 5633 | } | 
|  | 5634 | } | 
|  | 5635 |  | 
|  | 5636 |  | 
|  | 5637 | bool AudioFlinger::DuplicatingThread::outputsReady( | 
|  | 5638 | const SortedVector< sp<OutputTrack> > &outputTracks) | 
|  | 5639 | { | 
|  | 5640 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 5641 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); | 
|  | 5642 | if (thread == 0) { | 
|  | 5643 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
|  | 5644 | outputTracks[i].get()); | 
|  | 5645 | return false; | 
|  | 5646 | } | 
|  | 5647 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 5648 | // see note at standby() declaration | 
|  | 5649 | if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
|  | 5650 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
|  | 5651 | thread.get()); | 
|  | 5652 | return false; | 
|  | 5653 | } | 
|  | 5654 | } | 
|  | 5655 | return true; | 
|  | 5656 | } | 
|  | 5657 |  | 
|  | 5658 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const | 
|  | 5659 | { | 
|  | 5660 | return (mWaitTimeMs * 1000) / 2; | 
|  | 5661 | } | 
|  | 5662 |  | 
|  | 5663 | void AudioFlinger::DuplicatingThread::cacheParameters_l() | 
|  | 5664 | { | 
|  | 5665 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
|  | 5666 | updateWaitTime_l(); | 
|  | 5667 |  | 
|  | 5668 | MixerThread::cacheParameters_l(); | 
|  | 5669 | } | 
|  | 5670 |  | 
|  | 5671 | // ---------------------------------------------------------------------------- | 
|  | 5672 | //      Record | 
|  | 5673 | // ---------------------------------------------------------------------------- | 
|  | 5674 |  | 
|  | 5675 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 5676 | AudioStreamIn *input, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5677 | audio_io_handle_t id, | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5678 | audio_devices_t outDevice, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5679 | audio_devices_t inDevice, | 
|  | 5680 | bool systemReady | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5681 | #ifdef TEE_SINK | 
|  | 5682 | , const sp<NBAIO_Sink>& teeSink | 
|  | 5683 | #endif | 
|  | 5684 | ) : | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5685 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5686 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5687 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5688 | mRsmpInRear(0) | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5689 | #ifdef TEE_SINK | 
|  | 5690 | , mTeeSink(teeSink) | 
|  | 5691 | #endif | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5692 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, | 
|  | 5693 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5694 | // mFastCapture below | 
|  | 5695 | , mFastCaptureFutex(0) | 
|  | 5696 | // mInputSource | 
|  | 5697 | // mPipeSink | 
|  | 5698 | // mPipeSource | 
|  | 5699 | , mPipeFramesP2(0) | 
|  | 5700 | // mPipeMemory | 
|  | 5701 | // mFastCaptureNBLogWriter | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5702 | , mFastTrackAvail(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5703 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5704 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); | 
|  | 5705 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5706 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5707 | readInputParameters_l(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5708 |  | 
|  | 5709 | // create an NBAIO source for the HAL input stream, and negotiate | 
|  | 5710 | mInputSource = new AudioStreamInSource(input->stream); | 
|  | 5711 | size_t numCounterOffers = 0; | 
|  | 5712 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5713 | #if !LOG_NDEBUG | 
|  | 5714 | ssize_t index = | 
|  | 5715 | #else | 
|  | 5716 | (void) | 
|  | 5717 | #endif | 
|  | 5718 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5719 | ALOG_ASSERT(index == 0); | 
|  | 5720 |  | 
|  | 5721 | // initialize fast capture depending on configuration | 
|  | 5722 | bool initFastCapture; | 
|  | 5723 | switch (kUseFastCapture) { | 
|  | 5724 | case FastCapture_Never: | 
|  | 5725 | initFastCapture = false; | 
|  | 5726 | break; | 
|  | 5727 | case FastCapture_Always: | 
|  | 5728 | initFastCapture = true; | 
|  | 5729 | break; | 
|  | 5730 | case FastCapture_Static: | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5731 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5732 | break; | 
|  | 5733 | // case FastCapture_Dynamic: | 
|  | 5734 | } | 
|  | 5735 |  | 
|  | 5736 | if (initFastCapture) { | 
| Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5737 | // 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] | 5738 | NBAIO_Format format = mInputSource->format(); | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5739 | size_t pipeFramesP2 = roundup(mSampleRate / 25);    // double-buffering of 20 ms each | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5740 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); | 
|  | 5741 | void *pipeBuffer; | 
|  | 5742 | const sp<MemoryDealer> roHeap(readOnlyHeap()); | 
|  | 5743 | sp<IMemory> pipeMemory; | 
|  | 5744 | if ((roHeap == 0) || | 
|  | 5745 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || | 
|  | 5746 | (pipeBuffer = pipeMemory->pointer()) == NULL) { | 
|  | 5747 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); | 
|  | 5748 | goto failed; | 
|  | 5749 | } | 
|  | 5750 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information | 
|  | 5751 | memset(pipeBuffer, 0, pipeSize); | 
|  | 5752 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); | 
|  | 5753 | const NBAIO_Format offers[1] = {format}; | 
|  | 5754 | size_t numCounterOffers = 0; | 
|  | 5755 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); | 
|  | 5756 | ALOG_ASSERT(index == 0); | 
|  | 5757 | mPipeSink = pipe; | 
|  | 5758 | PipeReader *pipeReader = new PipeReader(*pipe); | 
|  | 5759 | numCounterOffers = 0; | 
|  | 5760 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); | 
|  | 5761 | ALOG_ASSERT(index == 0); | 
|  | 5762 | mPipeSource = pipeReader; | 
|  | 5763 | mPipeFramesP2 = pipeFramesP2; | 
|  | 5764 | mPipeMemory = pipeMemory; | 
|  | 5765 |  | 
|  | 5766 | // create fast capture | 
|  | 5767 | mFastCapture = new FastCapture(); | 
|  | 5768 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 5769 | #ifdef STATE_QUEUE_DUMP | 
|  | 5770 | // FIXME | 
|  | 5771 | #endif | 
|  | 5772 | FastCaptureState *state = sq->begin(); | 
|  | 5773 | state->mCblk = NULL; | 
|  | 5774 | state->mInputSource = mInputSource.get(); | 
|  | 5775 | state->mInputSourceGen++; | 
|  | 5776 | state->mPipeSink = pipe; | 
|  | 5777 | state->mPipeSinkGen++; | 
|  | 5778 | state->mFrameCount = mFrameCount; | 
|  | 5779 | state->mCommand = FastCaptureState::COLD_IDLE; | 
|  | 5780 | // already done in constructor initialization list | 
|  | 5781 | //mFastCaptureFutex = 0; | 
|  | 5782 | state->mColdFutexAddr = &mFastCaptureFutex; | 
|  | 5783 | state->mColdGen++; | 
|  | 5784 | state->mDumpState = &mFastCaptureDumpState; | 
|  | 5785 | #ifdef TEE_SINK | 
|  | 5786 | // FIXME | 
|  | 5787 | #endif | 
|  | 5788 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); | 
|  | 5789 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); | 
|  | 5790 | sq->end(); | 
|  | 5791 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 5792 |  | 
|  | 5793 | // start the fast capture | 
|  | 5794 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); | 
|  | 5795 | pid_t tid = mFastCapture->getTid(); | 
| Glenn Kasten | 8379b72 | 2016-03-18 14:54:17 -0700 | [diff] [blame] | 5796 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5797 | #ifdef AUDIO_WATCHDOG | 
|  | 5798 | // FIXME | 
|  | 5799 | #endif | 
|  | 5800 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5801 | mFastTrackAvail = true; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5802 | } | 
|  | 5803 | failed: ; | 
|  | 5804 |  | 
|  | 5805 | // FIXME mNormalSource | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5806 | } | 
|  | 5807 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5808 | AudioFlinger::RecordThread::~RecordThread() | 
|  | 5809 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5810 | if (mFastCapture != 0) { | 
|  | 5811 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 5812 | FastCaptureState *state = sq->begin(); | 
|  | 5813 | if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
|  | 5814 | int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
|  | 5815 | if (old == -1) { | 
|  | 5816 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
|  | 5817 | } | 
|  | 5818 | } | 
|  | 5819 | state->mCommand = FastCaptureState::EXIT; | 
|  | 5820 | sq->end(); | 
|  | 5821 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 5822 | mFastCapture->join(); | 
|  | 5823 | mFastCapture.clear(); | 
|  | 5824 | } | 
|  | 5825 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5826 | mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5827 | free(mRsmpInBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5828 | } | 
|  | 5829 |  | 
|  | 5830 | void AudioFlinger::RecordThread::onFirstRef() | 
|  | 5831 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5832 | run(mThreadName, PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5833 | } | 
|  | 5834 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5835 | bool AudioFlinger::RecordThread::threadLoop() | 
|  | 5836 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5837 | nsecs_t lastWarning = 0; | 
|  | 5838 |  | 
|  | 5839 | inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5840 |  | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5841 | reacquire_wakelock: | 
|  | 5842 | sp<RecordTrack> activeTrack; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5843 | int activeTracksGen; | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5844 | { | 
|  | 5845 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5846 | size_t size = mActiveTracks.size(); | 
|  | 5847 | activeTracksGen = mActiveTracksGen; | 
|  | 5848 | if (size > 0) { | 
|  | 5849 | // FIXME an arbitrary choice | 
|  | 5850 | activeTrack = mActiveTracks[0]; | 
|  | 5851 | acquireWakeLock_l(activeTrack->uid()); | 
|  | 5852 | if (size > 1) { | 
|  | 5853 | SortedVector<int> tmp; | 
|  | 5854 | for (size_t i = 0; i < size; i++) { | 
|  | 5855 | tmp.add(mActiveTracks[i]->uid()); | 
|  | 5856 | } | 
|  | 5857 | updateWakeLockUids_l(tmp); | 
|  | 5858 | } | 
|  | 5859 | } else { | 
|  | 5860 | acquireWakeLock_l(-1); | 
|  | 5861 | } | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5862 | } | 
|  | 5863 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5864 | // used to request a deferred sleep, to be executed later while mutex is unlocked | 
|  | 5865 | uint32_t sleepUs = 0; | 
|  | 5866 |  | 
|  | 5867 | // loop while there is work to do | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5868 | for (;;) { | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5869 | Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5870 |  | 
| Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5871 | // sleep with mutex unlocked | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5872 | if (sleepUs > 0) { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5873 | ATRACE_BEGIN("sleep"); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5874 | usleep(sleepUs); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5875 | ATRACE_END(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5876 | sleepUs = 0; | 
| Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5877 | } | 
|  | 5878 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5879 | // activeTracks accumulates a copy of a subset of mActiveTracks | 
|  | 5880 | Vector< sp<RecordTrack> > activeTracks; | 
|  | 5881 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5882 | // reference to the (first and only) active fast track | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5883 | sp<RecordTrack> fastTrack; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5884 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5885 | // reference to a fast track which is about to be removed | 
|  | 5886 | sp<RecordTrack> fastTrackToRemove; | 
|  | 5887 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5888 | { // scope for mLock | 
|  | 5889 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5890 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5891 | processConfigEvents_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5892 |  | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5893 | // check exitPending here because checkForNewParameters_l() and | 
|  | 5894 | // checkForNewParameters_l() can temporarily release mLock | 
|  | 5895 | if (exitPending()) { | 
|  | 5896 | break; | 
|  | 5897 | } | 
|  | 5898 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5899 | // if no active track(s), then standby and release wakelock | 
|  | 5900 | size_t size = mActiveTracks.size(); | 
|  | 5901 | if (size == 0) { | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5902 | standbyIfNotAlreadyInStandby(); | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5903 | // exitPending() can't become true here | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5904 | releaseWakeLock_l(); | 
|  | 5905 | ALOGV("RecordThread: loop stopping"); | 
|  | 5906 | // go to sleep | 
|  | 5907 | mWaitWorkCV.wait(mLock); | 
|  | 5908 | ALOGV("RecordThread: loop starting"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5909 | goto reacquire_wakelock; | 
|  | 5910 | } | 
|  | 5911 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5912 | if (mActiveTracksGen != activeTracksGen) { | 
|  | 5913 | activeTracksGen = mActiveTracksGen; | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5914 | SortedVector<int> tmp; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5915 | for (size_t i = 0; i < size; i++) { | 
|  | 5916 | tmp.add(mActiveTracks[i]->uid()); | 
|  | 5917 | } | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5918 | updateWakeLockUids_l(tmp); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5919 | } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5920 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5921 | bool doBroadcast = false; | 
|  | 5922 | for (size_t i = 0; i < size; ) { | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5923 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5924 | activeTrack = mActiveTracks[i]; | 
|  | 5925 | if (activeTrack->isTerminated()) { | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5926 | if (activeTrack->isFastTrack()) { | 
|  | 5927 | ALOG_ASSERT(fastTrackToRemove == 0); | 
|  | 5928 | fastTrackToRemove = activeTrack; | 
|  | 5929 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5930 | removeTrack_l(activeTrack); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5931 | mActiveTracks.remove(activeTrack); | 
|  | 5932 | mActiveTracksGen++; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5933 | size--; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5934 | continue; | 
|  | 5935 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5936 |  | 
|  | 5937 | TrackBase::track_state activeTrackState = activeTrack->mState; | 
|  | 5938 | switch (activeTrackState) { | 
|  | 5939 |  | 
|  | 5940 | case TrackBase::PAUSING: | 
|  | 5941 | mActiveTracks.remove(activeTrack); | 
|  | 5942 | mActiveTracksGen++; | 
|  | 5943 | doBroadcast = true; | 
|  | 5944 | size--; | 
|  | 5945 | continue; | 
|  | 5946 |  | 
|  | 5947 | case TrackBase::STARTING_1: | 
|  | 5948 | sleepUs = 10000; | 
|  | 5949 | i++; | 
|  | 5950 | continue; | 
|  | 5951 |  | 
|  | 5952 | case TrackBase::STARTING_2: | 
|  | 5953 | doBroadcast = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5954 | mStandby = false; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5955 | activeTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5956 | break; | 
|  | 5957 |  | 
|  | 5958 | case TrackBase::ACTIVE: | 
|  | 5959 | break; | 
|  | 5960 |  | 
|  | 5961 | case TrackBase::IDLE: | 
|  | 5962 | i++; | 
|  | 5963 | continue; | 
|  | 5964 |  | 
|  | 5965 | default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5966 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5967 | } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5968 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5969 | activeTracks.add(activeTrack); | 
|  | 5970 | i++; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5971 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5972 | if (activeTrack->isFastTrack()) { | 
|  | 5973 | ALOG_ASSERT(!mFastTrackAvail); | 
|  | 5974 | ALOG_ASSERT(fastTrack == 0); | 
|  | 5975 | fastTrack = activeTrack; | 
|  | 5976 | } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5977 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5978 | if (doBroadcast) { | 
|  | 5979 | mStartStopCond.broadcast(); | 
|  | 5980 | } | 
|  | 5981 |  | 
|  | 5982 | // sleep if there are no active tracks to process | 
|  | 5983 | if (activeTracks.size() == 0) { | 
|  | 5984 | if (sleepUs == 0) { | 
|  | 5985 | sleepUs = kRecordThreadSleepUs; | 
|  | 5986 | } | 
|  | 5987 | continue; | 
|  | 5988 | } | 
|  | 5989 | sleepUs = 0; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5990 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5991 | lockEffectChains_l(effectChains); | 
|  | 5992 | } | 
|  | 5993 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5994 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 | 
| Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5995 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5996 | size_t size = effectChains.size(); | 
|  | 5997 | for (size_t i = 0; i < size; i++) { | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5998 | // thread mutex is not locked, but effect chain is locked | 
|  | 5999 | effectChains[i]->process_l(); | 
|  | 6000 | } | 
|  | 6001 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6002 | // 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] | 6003 | if (mFastCapture != 0) { | 
|  | 6004 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 6005 | FastCaptureState *state = sq->begin(); | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6006 | bool didModify = false; | 
|  | 6007 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6008 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && | 
|  | 6009 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { | 
|  | 6010 | if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
|  | 6011 | int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
|  | 6012 | if (old == -1) { | 
|  | 6013 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
|  | 6014 | } | 
|  | 6015 | } | 
|  | 6016 | state->mCommand = FastCaptureState::READ_WRITE; | 
|  | 6017 | #if 0   // FIXME | 
|  | 6018 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6019 | FastThreadDumpState::kSamplingNforLowRamDevice : | 
|  | 6020 | FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6021 | #endif | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6022 | didModify = true; | 
|  | 6023 | } | 
|  | 6024 | audio_track_cblk_t *cblkOld = state->mCblk; | 
|  | 6025 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; | 
|  | 6026 | if (cblkNew != cblkOld) { | 
|  | 6027 | state->mCblk = cblkNew; | 
|  | 6028 | // block until acked if removing a fast track | 
|  | 6029 | if (cblkOld != NULL) { | 
|  | 6030 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; | 
|  | 6031 | } | 
|  | 6032 | didModify = true; | 
|  | 6033 | } | 
|  | 6034 | sq->end(didModify); | 
|  | 6035 | if (didModify) { | 
|  | 6036 | sq->push(block); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6037 | #if 0 | 
|  | 6038 | if (kUseFastCapture == FastCapture_Dynamic) { | 
|  | 6039 | mNormalSource = mPipeSource; | 
|  | 6040 | } | 
|  | 6041 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6042 | } | 
|  | 6043 | } | 
|  | 6044 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6045 | // now run the fast track destructor with thread mutex unlocked | 
|  | 6046 | fastTrackToRemove.clear(); | 
|  | 6047 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6048 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. | 
|  | 6049 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too | 
|  | 6050 | // slow, then this RecordThread will overrun by not calling HAL read often enough. | 
|  | 6051 | // If destination is non-contiguous, first read past the nominal end of buffer, then | 
|  | 6052 | // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated. | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6053 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6054 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6055 | ssize_t framesRead; | 
|  | 6056 |  | 
|  | 6057 | // If an NBAIO source is present, use it to read the normal capture's data | 
|  | 6058 | if (mPipeSource != 0) { | 
|  | 6059 | size_t framesToRead = mBufferSize / mFrameSize; | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6060 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6061 | framesToRead); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6062 | if (framesRead == 0) { | 
|  | 6063 | // since pipe is non-blocking, simulate blocking input | 
|  | 6064 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; | 
|  | 6065 | } | 
|  | 6066 | // otherwise use the HAL / AudioStreamIn directly | 
|  | 6067 | } else { | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6068 | ATRACE_BEGIN("read"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6069 | ssize_t bytesRead = mInput->stream->read(mInput->stream, | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6070 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6071 | ATRACE_END(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6072 | if (bytesRead < 0) { | 
|  | 6073 | framesRead = bytesRead; | 
|  | 6074 | } else { | 
|  | 6075 | framesRead = bytesRead / mFrameSize; | 
|  | 6076 | } | 
|  | 6077 | } | 
|  | 6078 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6079 | // Update server timestamp with server stats | 
|  | 6080 | // systemTime() is optional if the hardware supports timestamps. | 
|  | 6081 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; | 
|  | 6082 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); | 
|  | 6083 |  | 
|  | 6084 | // Update server timestamp with kernel stats | 
|  | 6085 | if (mInput->stream->get_capture_position != nullptr) { | 
|  | 6086 | int64_t position, time; | 
|  | 6087 | int ret = mInput->stream->get_capture_position(mInput->stream, &position, &time); | 
|  | 6088 | if (ret == NO_ERROR) { | 
|  | 6089 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; | 
|  | 6090 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; | 
|  | 6091 | // Note: In general record buffers should tend to be empty in | 
|  | 6092 | // a properly running pipeline. | 
|  | 6093 | // | 
|  | 6094 | // Also, it is not advantageous to call get_presentation_position during the read | 
|  | 6095 | // as the read obtains a lock, preventing the timestamp call from executing. | 
|  | 6096 | } | 
|  | 6097 | } | 
|  | 6098 | // Use this to track timestamp information | 
|  | 6099 | // ALOGD("%s", mTimestamp.toString().c_str()); | 
|  | 6100 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6101 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6102 | ALOGE("read failed: framesRead=%zd", framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6103 | // Force input into standby so that it tries to recover at next read attempt | 
|  | 6104 | inputStandBy(); | 
|  | 6105 | sleepUs = kRecordThreadSleepUs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6106 | } | 
|  | 6107 | if (framesRead <= 0) { | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6108 | goto unlock; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6109 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6110 | ALOG_ASSERT(framesRead > 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6111 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6112 | if (mTeeSink != 0) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6113 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6114 | } | 
|  | 6115 | // 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] | 6116 | { | 
|  | 6117 | size_t part1 = mRsmpInFramesP2 - rear; | 
|  | 6118 | if ((size_t) framesRead > part1) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6119 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6120 | (framesRead - part1) * mFrameSize); | 
|  | 6121 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6122 | } | 
|  | 6123 | rear = mRsmpInRear += framesRead; | 
|  | 6124 |  | 
|  | 6125 | size = activeTracks.size(); | 
|  | 6126 | // loop over each active track | 
|  | 6127 | for (size_t i = 0; i < size; i++) { | 
|  | 6128 | activeTrack = activeTracks[i]; | 
|  | 6129 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6130 | // skip fast tracks, as those are handled directly by FastCapture | 
|  | 6131 | if (activeTrack->isFastTrack()) { | 
|  | 6132 | continue; | 
|  | 6133 | } | 
|  | 6134 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6135 | // TODO: This code probably should be moved to RecordTrack. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6136 | // TODO: Update the activeTrack buffer converter in case of reconfigure. | 
|  | 6137 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6138 | enum { | 
|  | 6139 | OVERRUN_UNKNOWN, | 
|  | 6140 | OVERRUN_TRUE, | 
|  | 6141 | OVERRUN_FALSE | 
|  | 6142 | } overrun = OVERRUN_UNKNOWN; | 
|  | 6143 |  | 
|  | 6144 | // loop over getNextBuffer to handle circular sink | 
|  | 6145 | for (;;) { | 
|  | 6146 |  | 
|  | 6147 | activeTrack->mSink.frameCount = ~0; | 
|  | 6148 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); | 
|  | 6149 | size_t framesOut = activeTrack->mSink.frameCount; | 
|  | 6150 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); | 
|  | 6151 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6152 | // check available frames and handle overrun conditions | 
|  | 6153 | // if the record track isn't draining fast enough. | 
|  | 6154 | bool hasOverrun; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6155 | size_t framesIn; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6156 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); | 
|  | 6157 | if (hasOverrun) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6158 | overrun = OVERRUN_TRUE; | 
|  | 6159 | } | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6160 | if (framesOut == 0 || framesIn == 0) { | 
|  | 6161 | break; | 
|  | 6162 | } | 
|  | 6163 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6164 | // Don't allow framesOut to be larger than what is possible with resampling | 
|  | 6165 | // from framesIn. | 
|  | 6166 | // This isn't strictly necessary but helps limit buffer resizing in | 
|  | 6167 | // RecordBufferConverter.  TODO: remove when no longer needed. | 
|  | 6168 | framesOut = min(framesOut, | 
|  | 6169 | destinationFramesPossible( | 
|  | 6170 | framesIn, mSampleRate, activeTrack->mSampleRate)); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6171 | // process frames from the RecordThread buffer provider to the RecordTrack buffer | 
|  | 6172 | framesOut = activeTrack->mRecordBufferConverter->convert( | 
|  | 6173 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6174 |  | 
|  | 6175 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { | 
|  | 6176 | overrun = OVERRUN_FALSE; | 
|  | 6177 | } | 
|  | 6178 |  | 
|  | 6179 | if (activeTrack->mFramesToDrop == 0) { | 
|  | 6180 | if (framesOut > 0) { | 
|  | 6181 | activeTrack->mSink.frameCount = framesOut; | 
|  | 6182 | activeTrack->releaseBuffer(&activeTrack->mSink); | 
|  | 6183 | } | 
|  | 6184 | } else { | 
|  | 6185 | // FIXME could do a partial drop of framesOut | 
|  | 6186 | if (activeTrack->mFramesToDrop > 0) { | 
|  | 6187 | activeTrack->mFramesToDrop -= framesOut; | 
|  | 6188 | if (activeTrack->mFramesToDrop <= 0) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6189 | activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6190 | } | 
|  | 6191 | } else { | 
|  | 6192 | activeTrack->mFramesToDrop += framesOut; | 
|  | 6193 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || | 
|  | 6194 | activeTrack->mSyncStartEvent->isCancelled()) { | 
|  | 6195 | ALOGW("Synced record %s, session %d, trigger session %d", | 
|  | 6196 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", | 
|  | 6197 | activeTrack->sessionId(), | 
|  | 6198 | (activeTrack->mSyncStartEvent != 0) ? | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6199 | activeTrack->mSyncStartEvent->triggerSession() : | 
|  | 6200 | AUDIO_SESSION_NONE); | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6201 | activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6202 | } | 
|  | 6203 | } | 
|  | 6204 | } | 
|  | 6205 |  | 
|  | 6206 | if (framesOut == 0) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6207 | break; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6208 | } | 
|  | 6209 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6210 |  | 
|  | 6211 | switch (overrun) { | 
|  | 6212 | case OVERRUN_TRUE: | 
|  | 6213 | // client isn't retrieving buffers fast enough | 
|  | 6214 | if (!activeTrack->setOverflow()) { | 
|  | 6215 | nsecs_t now = systemTime(); | 
|  | 6216 | // FIXME should lastWarning per track? | 
|  | 6217 | if ((now - lastWarning) > kWarningThrottleNs) { | 
|  | 6218 | ALOGW("RecordThread: buffer overflow"); | 
|  | 6219 | lastWarning = now; | 
|  | 6220 | } | 
|  | 6221 | } | 
|  | 6222 | break; | 
|  | 6223 | case OVERRUN_FALSE: | 
|  | 6224 | activeTrack->clearOverflow(); | 
|  | 6225 | break; | 
|  | 6226 | case OVERRUN_UNKNOWN: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6227 | break; | 
|  | 6228 | } | 
|  | 6229 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6230 | // update frame information and push timestamp out | 
|  | 6231 | activeTrack->updateTrackFrameInfo( | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6232 | activeTrack->mServerProxy->framesReleased(), | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6233 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], | 
|  | 6234 | mSampleRate, mTimestamp); | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6235 | } | 
|  | 6236 |  | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6237 | unlock: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6238 | // enable changes in effect chain | 
|  | 6239 | unlockEffectChains(effectChains); | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6240 | // 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] | 6241 | } | 
|  | 6242 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6243 | standbyIfNotAlreadyInStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6244 |  | 
|  | 6245 | { | 
|  | 6246 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6247 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 6248 | sp<RecordTrack> track = mTracks[i]; | 
|  | 6249 | track->invalidate(); | 
|  | 6250 | } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6251 | mActiveTracks.clear(); | 
|  | 6252 | mActiveTracksGen++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6253 | mStartStopCond.broadcast(); | 
|  | 6254 | } | 
|  | 6255 |  | 
|  | 6256 | releaseWakeLock(); | 
|  | 6257 |  | 
|  | 6258 | ALOGV("RecordThread %p exiting", this); | 
|  | 6259 | return false; | 
|  | 6260 | } | 
|  | 6261 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6262 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6263 | { | 
|  | 6264 | if (!mStandby) { | 
|  | 6265 | inputStandBy(); | 
|  | 6266 | mStandby = true; | 
|  | 6267 | } | 
|  | 6268 | } | 
|  | 6269 |  | 
|  | 6270 | void AudioFlinger::RecordThread::inputStandBy() | 
|  | 6271 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6272 | // Idle the fast capture if it's currently running | 
|  | 6273 | if (mFastCapture != 0) { | 
|  | 6274 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 6275 | FastCaptureState *state = sq->begin(); | 
|  | 6276 | if (!(state->mCommand & FastCaptureState::IDLE)) { | 
|  | 6277 | state->mCommand = FastCaptureState::COLD_IDLE; | 
|  | 6278 | state->mColdFutexAddr = &mFastCaptureFutex; | 
|  | 6279 | state->mColdGen++; | 
|  | 6280 | mFastCaptureFutex = 0; | 
|  | 6281 | sq->end(); | 
|  | 6282 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
|  | 6283 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); | 
|  | 6284 | #if 0 | 
|  | 6285 | if (kUseFastCapture == FastCapture_Dynamic) { | 
|  | 6286 | // FIXME | 
|  | 6287 | } | 
|  | 6288 | #endif | 
|  | 6289 | #ifdef AUDIO_WATCHDOG | 
|  | 6290 | // FIXME | 
|  | 6291 | #endif | 
|  | 6292 | } else { | 
|  | 6293 | sq->end(false /*didModify*/); | 
|  | 6294 | } | 
|  | 6295 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6296 | mInput->stream->common.standby(&mInput->stream->common); | 
|  | 6297 | } | 
|  | 6298 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6299 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6300 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6301 | const sp<AudioFlinger::Client>& client, | 
|  | 6302 | uint32_t sampleRate, | 
|  | 6303 | audio_format_t format, | 
|  | 6304 | audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6305 | size_t *pFrameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6306 | audio_session_t sessionId, | 
| Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6307 | size_t *notificationFrames, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6308 | int uid, | 
| Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 6309 | IAudioFlinger::track_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6310 | pid_t tid, | 
|  | 6311 | status_t *status) | 
|  | 6312 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6313 | size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6314 | sp<RecordTrack> track; | 
|  | 6315 | status_t lStatus; | 
|  | 6316 |  | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6317 | // client expresses a preference for FAST, but we get the final say | 
|  | 6318 | if (*flags & IAudioFlinger::TRACK_FAST) { | 
|  | 6319 | if ( | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6320 | // we formerly checked for a callback handler (non-0 tid), | 
|  | 6321 | // but that is no longer required for TRANSFER_OBTAIN mode | 
|  | 6322 | // | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6323 | // frame count is not specified, or is exactly the pipe depth | 
|  | 6324 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6325 | // PCM data | 
|  | 6326 | audio_is_linear_pcm(format) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6327 | // hardware format | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6328 | (format == mFormat) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6329 | // hardware channel mask | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6330 | (channelMask == mChannelMask) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6331 | // hardware sample rate | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6332 | (sampleRate == mSampleRate) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6333 | // record thread has an associated fast capture | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6334 | hasFastCapture() && | 
|  | 6335 | // there are sufficient fast track slots available | 
|  | 6336 | mFastTrackAvail | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6337 | ) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6338 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6339 | frameCount, mFrameCount); | 
|  | 6340 | } else { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6341 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6342 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6343 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6344 | frameCount, mFrameCount, mPipeFramesP2, | 
|  | 6345 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, | 
|  | 6346 | hasFastCapture(), tid, mFastTrackAvail); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6347 | *flags &= ~IAudioFlinger::TRACK_FAST; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6348 | } | 
|  | 6349 | } | 
|  | 6350 |  | 
|  | 6351 | // compute track buffer size in frames, and suggest the notification frame count | 
|  | 6352 | if (*flags & IAudioFlinger::TRACK_FAST) { | 
|  | 6353 | // fast track: frame count is exactly the pipe depth | 
|  | 6354 | frameCount = mPipeFramesP2; | 
|  | 6355 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer | 
|  | 6356 | *notificationFrames = mFrameCount; | 
|  | 6357 | } else { | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6358 | // not fast track: max notification period is resampled equivalent of one HAL buffer time | 
|  | 6359 | //                 or 20 ms if there is a fast capture | 
|  | 6360 | // TODO This could be a roundupRatio inline, and const | 
|  | 6361 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) | 
|  | 6362 | * sampleRate + mSampleRate - 1) / mSampleRate; | 
|  | 6363 | // minimum number of notification periods is at least kMinNotifications, | 
|  | 6364 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) | 
|  | 6365 | static const size_t kMinNotifications = 3; | 
|  | 6366 | static const uint32_t kMinMs = 30; | 
|  | 6367 | // TODO This could be a roundupRatio inline | 
|  | 6368 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; | 
|  | 6369 | // TODO This could be a roundupRatio inline | 
|  | 6370 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / | 
|  | 6371 | maxNotificationFrames; | 
|  | 6372 | const size_t minFrameCount = maxNotificationFrames * | 
|  | 6373 | max(kMinNotifications, minNotificationsByMs); | 
|  | 6374 | frameCount = max(frameCount, minFrameCount); | 
|  | 6375 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { | 
|  | 6376 | *notificationFrames = maxNotificationFrames; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6377 | } | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6378 | } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6379 | *pFrameCount = frameCount; | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6380 |  | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6381 | lStatus = initCheck(); | 
|  | 6382 | if (lStatus != NO_ERROR) { | 
|  | 6383 | ALOGE("createRecordTrack_l() audio driver not initialized"); | 
|  | 6384 | goto Exit; | 
|  | 6385 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6386 |  | 
|  | 6387 | { // scope for mLock | 
|  | 6388 | Mutex::Autolock _l(mLock); | 
|  | 6389 |  | 
|  | 6390 | track = new RecordTrack(this, client, sampleRate, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6391 | format, channelMask, frameCount, NULL, sessionId, uid, | 
|  | 6392 | *flags, TrackBase::TYPE_DEFAULT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6393 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6394 | lStatus = track->initCheck(); | 
|  | 6395 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6396 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6397 | // 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] | 6398 | goto Exit; | 
|  | 6399 | } | 
|  | 6400 | mTracks.add(track); | 
|  | 6401 |  | 
|  | 6402 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 6403 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
|  | 6404 | mAudioFlinger->btNrecIsOff(); | 
|  | 6405 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
|  | 6406 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6407 |  | 
|  | 6408 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
|  | 6409 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
|  | 6410 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
|  | 6411 | // so ask activity manager to do this on our behalf | 
|  | 6412 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
|  | 6413 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6414 | } | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6415 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6416 | lStatus = NO_ERROR; | 
|  | 6417 |  | 
|  | 6418 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6419 | *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6420 | return track; | 
|  | 6421 | } | 
|  | 6422 |  | 
|  | 6423 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, | 
|  | 6424 | AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6425 | audio_session_t triggerSession) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6426 | { | 
|  | 6427 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
|  | 6428 | sp<ThreadBase> strongMe = this; | 
|  | 6429 | status_t status = NO_ERROR; | 
|  | 6430 |  | 
|  | 6431 | if (event == AudioSystem::SYNC_EVENT_NONE) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6432 | recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6433 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6434 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6435 | triggerSession, | 
|  | 6436 | recordTrack->sessionId(), | 
|  | 6437 | syncStartEventCallback, | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6438 | recordTrack); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6439 | // Sync event can be cancelled by the trigger session if the track is not in a | 
|  | 6440 | // compatible state in which case we start record immediately | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6441 | if (recordTrack->mSyncStartEvent->isCancelled()) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6442 | recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6443 | } else { | 
|  | 6444 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6445 | recordTrack->mFramesToDrop = - | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6446 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6447 | } | 
|  | 6448 | } | 
|  | 6449 |  | 
|  | 6450 | { | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6451 | // This section is a rendezvous between binder thread executing start() and RecordThread | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6452 | AutoMutex lock(mLock); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6453 | if (mActiveTracks.indexOf(recordTrack) >= 0) { | 
|  | 6454 | if (recordTrack->mState == TrackBase::PAUSING) { | 
|  | 6455 | ALOGV("active record track PAUSING -> ACTIVE"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6456 | recordTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6457 | } else { | 
|  | 6458 | ALOGV("active record track state %d", recordTrack->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6459 | } | 
|  | 6460 | return status; | 
|  | 6461 | } | 
|  | 6462 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6463 | // TODO consider other ways of handling this, such as changing the state to :STARTING and | 
|  | 6464 | //      adding the track to mActiveTracks after returning from AudioSystem::startInput(), | 
|  | 6465 | //      or using a separate command thread | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6466 | recordTrack->mState = TrackBase::STARTING_1; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6467 | mActiveTracks.add(recordTrack); | 
|  | 6468 | mActiveTracksGen++; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6469 | status_t status = NO_ERROR; | 
|  | 6470 | if (recordTrack->isExternalTrack()) { | 
|  | 6471 | mLock.unlock(); | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6472 | status = AudioSystem::startInput(mId, recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6473 | mLock.lock(); | 
|  | 6474 | // FIXME should verify that recordTrack is still in mActiveTracks | 
|  | 6475 | if (status != NO_ERROR) { | 
|  | 6476 | mActiveTracks.remove(recordTrack); | 
|  | 6477 | mActiveTracksGen++; | 
|  | 6478 | recordTrack->clearSyncStartEvent(); | 
|  | 6479 | ALOGV("RecordThread::start error %d", status); | 
|  | 6480 | return status; | 
|  | 6481 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6482 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6483 | // Catch up with current buffer indices if thread is already running. | 
|  | 6484 | // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront | 
|  | 6485 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could | 
|  | 6486 | // see previously buffered data before it called start(), but with greater risk of overrun. | 
|  | 6487 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6488 | recordTrack->mResamplerBufferProvider->reset(); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6489 | // clear any converter state as new data will be discontinuous | 
|  | 6490 | recordTrack->mRecordBufferConverter->reset(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6491 | recordTrack->mState = TrackBase::STARTING_2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6492 | // signal thread to start | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6493 | mWaitWorkCV.broadcast(); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6494 | if (mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6495 | ALOGV("Record failed to start"); | 
|  | 6496 | status = BAD_VALUE; | 
|  | 6497 | goto startError; | 
|  | 6498 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6499 | return status; | 
|  | 6500 | } | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6501 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6502 | startError: | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6503 | if (recordTrack->isExternalTrack()) { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6504 | AudioSystem::stopInput(mId, recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6505 | } | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6506 | recordTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6507 | // FIXME I wonder why we do not reset the state here? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6508 | return status; | 
|  | 6509 | } | 
|  | 6510 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6511 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
|  | 6512 | { | 
|  | 6513 | sp<SyncEvent> strongEvent = event.promote(); | 
|  | 6514 |  | 
|  | 6515 | if (strongEvent != 0) { | 
| Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6516 | sp<RefBase> ptr = strongEvent->cookie().promote(); | 
|  | 6517 | if (ptr != 0) { | 
|  | 6518 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); | 
|  | 6519 | recordTrack->handleSyncStartEvent(strongEvent); | 
|  | 6520 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6521 | } | 
|  | 6522 | } | 
|  | 6523 |  | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6524 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6525 | ALOGV("RecordThread::stop"); | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6526 | AutoMutex _l(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6527 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6528 | return false; | 
|  | 6529 | } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6530 | // 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] | 6531 | recordTrack->mState = TrackBase::PAUSING; | 
|  | 6532 | // do not wait for mStartStopCond if exiting | 
|  | 6533 | if (exitPending()) { | 
|  | 6534 | return true; | 
|  | 6535 | } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6536 | // FIXME incorrect usage of wait: no explicit predicate or loop | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6537 | mStartStopCond.wait(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6538 | // if we have been restarted, recordTrack is in mActiveTracks here | 
|  | 6539 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6540 | ALOGV("Record stopped OK"); | 
|  | 6541 | return true; | 
|  | 6542 | } | 
|  | 6543 | return false; | 
|  | 6544 | } | 
|  | 6545 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6546 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6547 | { | 
|  | 6548 | return false; | 
|  | 6549 | } | 
|  | 6550 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6551 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6552 | { | 
|  | 6553 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
|  | 6554 | if (!isValidSyncEvent(event)) { | 
|  | 6555 | return BAD_VALUE; | 
|  | 6556 | } | 
|  | 6557 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6558 | audio_session_t eventSession = event->triggerSession(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6559 | status_t ret = NAME_NOT_FOUND; | 
|  | 6560 |  | 
|  | 6561 | Mutex::Autolock _l(mLock); | 
|  | 6562 |  | 
|  | 6563 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 6564 | sp<RecordTrack> track = mTracks[i]; | 
|  | 6565 | if (eventSession == track->sessionId()) { | 
|  | 6566 | (void) track->setSyncEvent(event); | 
|  | 6567 | ret = NO_ERROR; | 
|  | 6568 | } | 
|  | 6569 | } | 
|  | 6570 | return ret; | 
|  | 6571 | #else | 
|  | 6572 | return BAD_VALUE; | 
|  | 6573 | #endif | 
|  | 6574 | } | 
|  | 6575 |  | 
|  | 6576 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
|  | 6577 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) | 
|  | 6578 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6579 | track->terminate(); | 
|  | 6580 | track->mState = TrackBase::STOPPED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6581 | // active tracks are removed by threadLoop() | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6582 | if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6583 | removeTrack_l(track); | 
|  | 6584 | } | 
|  | 6585 | } | 
|  | 6586 |  | 
|  | 6587 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) | 
|  | 6588 | { | 
|  | 6589 | mTracks.remove(track); | 
|  | 6590 | // need anything related to effects here? | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6591 | if (track->isFastTrack()) { | 
|  | 6592 | ALOG_ASSERT(!mFastTrackAvail); | 
|  | 6593 | mFastTrackAvail = true; | 
|  | 6594 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6595 | } | 
|  | 6596 |  | 
|  | 6597 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
|  | 6598 | { | 
|  | 6599 | dumpInternals(fd, args); | 
|  | 6600 | dumpTracks(fd, args); | 
|  | 6601 | dumpEffectChains(fd, args); | 
|  | 6602 | } | 
|  | 6603 |  | 
|  | 6604 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 6605 | { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6606 | dprintf(fd, "\nInput thread %p:\n", this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6607 |  | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6608 | dumpBase(fd, args); | 
|  | 6609 |  | 
|  | 6610 | if (mActiveTracks.size() == 0) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6611 | dprintf(fd, "  No active record clients\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6612 | } | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6613 | dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6614 | dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); | 
| Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6615 |  | 
| Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6616 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us | 
|  | 6617 | // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
|  | 6618 | // This is a large object so we place it on the heap. | 
|  | 6619 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
|  | 6620 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); | 
|  | 6621 | copy->dump(fd); | 
|  | 6622 | delete copy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6623 | } | 
|  | 6624 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6625 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6626 | { | 
|  | 6627 | const size_t SIZE = 256; | 
|  | 6628 | char buffer[SIZE]; | 
|  | 6629 | String8 result; | 
|  | 6630 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6631 | size_t numtracks = mTracks.size(); | 
|  | 6632 | size_t numactive = mActiveTracks.size(); | 
|  | 6633 | size_t numactiveseen = 0; | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6634 | dprintf(fd, "  %zu Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6635 | if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6636 | dprintf(fd, " of which %zu are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6637 | RecordTrack::appendDumpHeader(result); | 
|  | 6638 | for (size_t i = 0; i < numtracks ; ++i) { | 
|  | 6639 | sp<RecordTrack> track = mTracks[i]; | 
|  | 6640 | if (track != 0) { | 
|  | 6641 | bool active = mActiveTracks.indexOf(track) >= 0; | 
|  | 6642 | if (active) { | 
|  | 6643 | numactiveseen++; | 
|  | 6644 | } | 
|  | 6645 | track->dump(buffer, SIZE, active); | 
|  | 6646 | result.append(buffer); | 
|  | 6647 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6648 | } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6649 | } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6650 | dprintf(fd, "\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6651 | } | 
|  | 6652 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6653 | if (numactiveseen != numactive) { | 
|  | 6654 | snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
|  | 6655 | " not in the track list\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6656 | result.append(buffer); | 
|  | 6657 | RecordTrack::appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6658 | for (size_t i = 0; i < numactive; ++i) { | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6659 | sp<RecordTrack> track = mActiveTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6660 | if (mTracks.indexOf(track) < 0) { | 
|  | 6661 | track->dump(buffer, SIZE, true); | 
|  | 6662 | result.append(buffer); | 
|  | 6663 | } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6664 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6665 |  | 
|  | 6666 | } | 
|  | 6667 | write(fd, result.string(), result.size()); | 
|  | 6668 | } | 
|  | 6669 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6670 |  | 
|  | 6671 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() | 
|  | 6672 | { | 
|  | 6673 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
|  | 6674 | RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
|  | 6675 | mRsmpInFront = recordThread->mRsmpInRear; | 
|  | 6676 | mRsmpInUnrel = 0; | 
|  | 6677 | } | 
|  | 6678 |  | 
|  | 6679 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( | 
|  | 6680 | size_t *framesAvailable, bool *hasOverrun) | 
|  | 6681 | { | 
|  | 6682 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
|  | 6683 | RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
|  | 6684 | const int32_t rear = recordThread->mRsmpInRear; | 
|  | 6685 | const int32_t front = mRsmpInFront; | 
|  | 6686 | const ssize_t filled = rear - front; | 
|  | 6687 |  | 
|  | 6688 | size_t framesIn; | 
|  | 6689 | bool overrun = false; | 
|  | 6690 | if (filled < 0) { | 
|  | 6691 | // should not happen, but treat like a massive overrun and re-sync | 
|  | 6692 | framesIn = 0; | 
|  | 6693 | mRsmpInFront = rear; | 
|  | 6694 | overrun = true; | 
|  | 6695 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { | 
|  | 6696 | framesIn = (size_t) filled; | 
|  | 6697 | } else { | 
|  | 6698 | // client is not keeping up with server, but give it latest data | 
|  | 6699 | framesIn = recordThread->mRsmpInFrames; | 
|  | 6700 | mRsmpInFront = /* front = */ rear - framesIn; | 
|  | 6701 | overrun = true; | 
|  | 6702 | } | 
|  | 6703 | if (framesAvailable != NULL) { | 
|  | 6704 | *framesAvailable = framesIn; | 
|  | 6705 | } | 
|  | 6706 | if (hasOverrun != NULL) { | 
|  | 6707 | *hasOverrun = overrun; | 
|  | 6708 | } | 
|  | 6709 | } | 
|  | 6710 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6711 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6712 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6713 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6714 | { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6715 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6716 | if (threadBase == 0) { | 
|  | 6717 | buffer->frameCount = 0; | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6718 | buffer->raw = NULL; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6719 | return NOT_ENOUGH_DATA; | 
|  | 6720 | } | 
|  | 6721 | RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
|  | 6722 | int32_t rear = recordThread->mRsmpInRear; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6723 | int32_t front = mRsmpInFront; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6724 | ssize_t filled = rear - front; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6725 | // FIXME should not be P2 (don't want to increase latency) | 
|  | 6726 | // FIXME if client not keeping up, discard | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6727 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6728 | // 'filled' may be non-contiguous, so return only the first contiguous chunk | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6729 | front &= recordThread->mRsmpInFramesP2 - 1; | 
|  | 6730 | size_t part1 = recordThread->mRsmpInFramesP2 - front; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6731 | if (part1 > (size_t) filled) { | 
|  | 6732 | part1 = filled; | 
|  | 6733 | } | 
|  | 6734 | size_t ask = buffer->frameCount; | 
|  | 6735 | ALOG_ASSERT(ask > 0); | 
|  | 6736 | if (part1 > ask) { | 
|  | 6737 | part1 = ask; | 
|  | 6738 | } | 
|  | 6739 | if (part1 == 0) { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6740 | // 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] | 6741 | buffer->raw = NULL; | 
|  | 6742 | buffer->frameCount = 0; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6743 | mRsmpInUnrel = 0; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6744 | return NOT_ENOUGH_DATA; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6745 | } | 
|  | 6746 |  | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6747 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6748 | buffer->frameCount = part1; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6749 | mRsmpInUnrel = part1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6750 | return NO_ERROR; | 
|  | 6751 | } | 
|  | 6752 |  | 
|  | 6753 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6754 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( | 
|  | 6755 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6756 | { | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6757 | size_t stepCount = buffer->frameCount; | 
|  | 6758 | if (stepCount == 0) { | 
|  | 6759 | return; | 
|  | 6760 | } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6761 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); | 
|  | 6762 | mRsmpInUnrel -= stepCount; | 
|  | 6763 | mRsmpInFront += stepCount; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6764 | buffer->raw = NULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6765 | buffer->frameCount = 0; | 
|  | 6766 | } | 
|  | 6767 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6768 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( | 
|  | 6769 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, | 
|  | 6770 | uint32_t srcSampleRate, | 
|  | 6771 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, | 
|  | 6772 | uint32_t dstSampleRate) : | 
|  | 6773 | mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars | 
|  | 6774 | // mSrcFormat | 
|  | 6775 | // mSrcSampleRate | 
|  | 6776 | // mDstChannelMask | 
|  | 6777 | // mDstFormat | 
|  | 6778 | // mDstSampleRate | 
|  | 6779 | // mSrcChannelCount | 
|  | 6780 | // mDstChannelCount | 
|  | 6781 | // mDstFrameSize | 
|  | 6782 | mBuf(NULL), mBufFrames(0), mBufFrameSize(0), | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6783 | mResampler(NULL), | 
|  | 6784 | mIsLegacyDownmix(false), | 
|  | 6785 | mIsLegacyUpmix(false), | 
|  | 6786 | mRequiresFloat(false), | 
|  | 6787 | mInputConverterProvider(NULL) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6788 | { | 
|  | 6789 | (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, | 
|  | 6790 | dstChannelMask, dstFormat, dstSampleRate); | 
|  | 6791 | } | 
|  | 6792 |  | 
|  | 6793 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { | 
|  | 6794 | free(mBuf); | 
|  | 6795 | delete mResampler; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6796 | delete mInputConverterProvider; | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6797 | } | 
|  | 6798 |  | 
|  | 6799 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, | 
|  | 6800 | AudioBufferProvider *provider, size_t frames) | 
|  | 6801 | { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6802 | if (mInputConverterProvider != NULL) { | 
|  | 6803 | mInputConverterProvider->setBufferProvider(provider); | 
|  | 6804 | provider = mInputConverterProvider; | 
|  | 6805 | } | 
|  | 6806 |  | 
|  | 6807 | if (mResampler == NULL) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6808 | ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", | 
|  | 6809 | mSrcSampleRate, mSrcFormat, mDstFormat); | 
|  | 6810 |  | 
|  | 6811 | AudioBufferProvider::Buffer buffer; | 
|  | 6812 | for (size_t i = frames; i > 0; ) { | 
|  | 6813 | buffer.frameCount = i; | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6814 | status_t status = provider->getNextBuffer(&buffer); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6815 | if (status != OK || buffer.frameCount == 0) { | 
|  | 6816 | frames -= i; // cannot fill request. | 
|  | 6817 | break; | 
|  | 6818 | } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6819 | // format convert to destination buffer | 
|  | 6820 | convertNoResampler(dst, buffer.raw, buffer.frameCount); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6821 |  | 
|  | 6822 | dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; | 
|  | 6823 | i -= buffer.frameCount; | 
|  | 6824 | provider->releaseBuffer(&buffer); | 
|  | 6825 | } | 
|  | 6826 | } else { | 
|  | 6827 | ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", | 
|  | 6828 | mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); | 
|  | 6829 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6830 | // reallocate buffer if needed | 
|  | 6831 | if (mBufFrameSize != 0 && mBufFrames < frames) { | 
|  | 6832 | free(mBuf); | 
|  | 6833 | mBufFrames = frames; | 
|  | 6834 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); | 
|  | 6835 | } | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6836 | // resampler accumulates, but we only have one source track | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6837 | memset(mBuf, 0, frames * mBufFrameSize); | 
|  | 6838 | frames = mResampler->resample((int32_t*)mBuf, frames, provider); | 
|  | 6839 | // format convert to destination buffer | 
|  | 6840 | convertResampler(dst, mBuf, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6841 | } | 
|  | 6842 | return frames; | 
|  | 6843 | } | 
|  | 6844 |  | 
|  | 6845 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( | 
|  | 6846 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, | 
|  | 6847 | uint32_t srcSampleRate, | 
|  | 6848 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, | 
|  | 6849 | uint32_t dstSampleRate) | 
|  | 6850 | { | 
|  | 6851 | // quick evaluation if there is any change. | 
|  | 6852 | if (mSrcFormat == srcFormat | 
|  | 6853 | && mSrcChannelMask == srcChannelMask | 
|  | 6854 | && mSrcSampleRate == srcSampleRate | 
|  | 6855 | && mDstFormat == dstFormat | 
|  | 6856 | && mDstChannelMask == dstChannelMask | 
|  | 6857 | && mDstSampleRate == dstSampleRate) { | 
|  | 6858 | return NO_ERROR; | 
|  | 6859 | } | 
|  | 6860 |  | 
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 6861 | ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" | 
|  | 6862 | "  srcFormat:%#x dstFormat:%#x  srcRate:%u dstRate:%u", | 
|  | 6863 | srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6864 | const bool valid = | 
|  | 6865 | audio_is_input_channel(srcChannelMask) | 
|  | 6866 | && audio_is_input_channel(dstChannelMask) | 
|  | 6867 | && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) | 
|  | 6868 | && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) | 
|  | 6869 | && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) | 
|  | 6870 | ; // no upsampling checks for now | 
|  | 6871 | if (!valid) { | 
|  | 6872 | return BAD_VALUE; | 
|  | 6873 | } | 
|  | 6874 |  | 
|  | 6875 | mSrcFormat = srcFormat; | 
|  | 6876 | mSrcChannelMask = srcChannelMask; | 
|  | 6877 | mSrcSampleRate = srcSampleRate; | 
|  | 6878 | mDstFormat = dstFormat; | 
|  | 6879 | mDstChannelMask = dstChannelMask; | 
|  | 6880 | mDstSampleRate = dstSampleRate; | 
|  | 6881 |  | 
|  | 6882 | // compute derived parameters | 
|  | 6883 | mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); | 
|  | 6884 | mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); | 
|  | 6885 | mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); | 
|  | 6886 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6887 | // do we need to resample? | 
|  | 6888 | delete mResampler; | 
|  | 6889 | mResampler = NULL; | 
|  | 6890 | if (mSrcSampleRate != mDstSampleRate) { | 
|  | 6891 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, | 
|  | 6892 | mSrcChannelCount, mDstSampleRate); | 
|  | 6893 | mResampler->setSampleRate(mSrcSampleRate); | 
|  | 6894 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); | 
|  | 6895 | } | 
|  | 6896 |  | 
|  | 6897 | // are we running legacy channel conversion modes? | 
|  | 6898 | mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO | 
|  | 6899 | || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) | 
|  | 6900 | && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; | 
|  | 6901 | mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO | 
|  | 6902 | && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO | 
|  | 6903 | || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); | 
|  | 6904 |  | 
|  | 6905 | // do we need to process in float? | 
|  | 6906 | mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; | 
|  | 6907 |  | 
|  | 6908 | // do we need a staging buffer to convert for destination (we can still optimize this)? | 
|  | 6909 | // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity | 
|  | 6910 | if (mResampler != NULL) { | 
|  | 6911 | mBufFrameSize = max(mSrcChannelCount, FCC_2) | 
|  | 6912 | * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); | 
| Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame] | 6913 | } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6914 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); | 
|  | 6915 | } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6916 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); | 
|  | 6917 | } else { | 
|  | 6918 | mBufFrameSize = 0; | 
|  | 6919 | } | 
|  | 6920 | mBufFrames = 0; // force the buffer to be resized. | 
|  | 6921 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6922 | // do we need an input converter buffer provider to give us float? | 
|  | 6923 | delete mInputConverterProvider; | 
|  | 6924 | mInputConverterProvider = NULL; | 
|  | 6925 | if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
|  | 6926 | mInputConverterProvider = new ReformatBufferProvider( | 
|  | 6927 | audio_channel_count_from_in_mask(mSrcChannelMask), | 
|  | 6928 | mSrcFormat, | 
|  | 6929 | AUDIO_FORMAT_PCM_FLOAT, | 
|  | 6930 | 256 /* provider buffer frame count */); | 
|  | 6931 | } | 
|  | 6932 |  | 
|  | 6933 | // do we need a remixer to do channel mask conversion | 
|  | 6934 | if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { | 
|  | 6935 | (void) memcpy_by_index_array_initialization_from_channel_mask( | 
|  | 6936 | mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6937 | } | 
|  | 6938 | return NO_ERROR; | 
|  | 6939 | } | 
|  | 6940 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6941 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( | 
|  | 6942 | void *dst, const void *src, size_t frames) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6943 | { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6944 | // 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] | 6945 | if (mBufFrameSize != 0 && mBufFrames < frames) { | 
|  | 6946 | free(mBuf); | 
|  | 6947 | mBufFrames = frames; | 
|  | 6948 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); | 
|  | 6949 | } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6950 | // do we need to do legacy upmix and downmix? | 
|  | 6951 | if (mIsLegacyUpmix || mIsLegacyDownmix) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6952 | void *dstBuf = mBuf != NULL ? mBuf : dst; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6953 | if (mIsLegacyUpmix) { | 
|  | 6954 | upmix_to_stereo_float_from_mono_float((float *)dstBuf, | 
|  | 6955 | (const float *)src, frames); | 
|  | 6956 | } else /*mIsLegacyDownmix */ { | 
|  | 6957 | downmix_to_mono_float_from_stereo_float((float *)dstBuf, | 
|  | 6958 | (const float *)src, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6959 | } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6960 | if (mBuf != NULL) { | 
|  | 6961 | memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, | 
|  | 6962 | frames * mDstChannelCount); | 
|  | 6963 | } | 
|  | 6964 | return; | 
|  | 6965 | } | 
|  | 6966 | // do we need to do channel mask conversion? | 
|  | 6967 | if (mSrcChannelMask != mDstChannelMask) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6968 | void *dstBuf = mBuf != NULL ? mBuf : dst; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6969 | memcpy_by_index_array(dstBuf, mDstChannelCount, | 
|  | 6970 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); | 
|  | 6971 | if (dstBuf == dst) { | 
|  | 6972 | return; // format is the same | 
|  | 6973 | } | 
|  | 6974 | } | 
|  | 6975 | // convert to destination buffer | 
|  | 6976 | const void *convertBuf = mBuf != NULL ? mBuf : src; | 
|  | 6977 | memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, | 
|  | 6978 | frames * mDstChannelCount); | 
|  | 6979 | } | 
|  | 6980 |  | 
|  | 6981 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( | 
|  | 6982 | void *dst, /*not-a-const*/ void *src, size_t frames) | 
|  | 6983 | { | 
|  | 6984 | // src buffer format is ALWAYS float when entering this routine | 
|  | 6985 | if (mIsLegacyUpmix) { | 
|  | 6986 | ; // mono to stereo already handled by resampler | 
|  | 6987 | } else if (mIsLegacyDownmix | 
|  | 6988 | || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { | 
|  | 6989 | // the resampler outputs stereo for mono input channel (a feature?) | 
|  | 6990 | // must convert to mono | 
|  | 6991 | downmix_to_mono_float_from_stereo_float((float *)src, | 
|  | 6992 | (const float *)src, frames); | 
|  | 6993 | } else if (mSrcChannelMask != mDstChannelMask) { | 
|  | 6994 | // convert to mono channel again for channel mask conversion (could be skipped | 
|  | 6995 | // with further optimization). | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6996 | if (mSrcChannelCount == 1) { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6997 | downmix_to_mono_float_from_stereo_float((float *)src, | 
|  | 6998 | (const float *)src, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6999 | } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7000 | // convert to destination format (in place, OK as float is larger than other types) | 
|  | 7001 | if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
|  | 7002 | memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, | 
|  | 7003 | frames * mSrcChannelCount); | 
|  | 7004 | } | 
|  | 7005 | // channel convert and save to dst | 
|  | 7006 | memcpy_by_index_array(dst, mDstChannelCount, | 
|  | 7007 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); | 
|  | 7008 | return; | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7009 | } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7010 | // convert to destination format and save to dst | 
|  | 7011 | memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, | 
|  | 7012 | frames * mDstChannelCount); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7013 | } | 
|  | 7014 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7015 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 7016 | status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7017 | { | 
|  | 7018 | bool reconfig = false; | 
|  | 7019 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7020 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7021 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7022 | audio_format_t reqFormat = mFormat; | 
|  | 7023 | uint32_t samplingRate = mSampleRate; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7024 | // 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] | 7025 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); | 
|  | 7026 |  | 
|  | 7027 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 7028 | int value; | 
| Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7029 |  | 
|  | 7030 | // scope for AutoPark extends to end of method | 
|  | 7031 | AutoPark<FastCapture> park(mFastCapture); | 
|  | 7032 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7033 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and | 
|  | 7034 | //      channel count change can be requested. Do we mandate the first client defines the | 
|  | 7035 | //      HAL sampling rate and channel count or do we allow changes on the fly? | 
|  | 7036 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 7037 | samplingRate = value; | 
|  | 7038 | reconfig = true; | 
|  | 7039 | } | 
|  | 7040 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7041 | if (!audio_is_linear_pcm((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7042 | status = BAD_VALUE; | 
|  | 7043 | } else { | 
|  | 7044 | reqFormat = (audio_format_t) value; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7045 | reconfig = true; | 
|  | 7046 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7047 | } | 
|  | 7048 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
|  | 7049 | audio_channel_mask_t mask = (audio_channel_mask_t) value; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7050 | if (!audio_is_input_channel(mask) || | 
|  | 7051 | audio_channel_count_from_in_mask(mask) > FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7052 | status = BAD_VALUE; | 
|  | 7053 | } else { | 
|  | 7054 | channelMask = mask; | 
|  | 7055 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7056 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7057 | } | 
|  | 7058 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 7059 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 7060 | // size depends on frame count and correct behavior would not be guaranteed | 
|  | 7061 | // if frame count is changed after track creation | 
|  | 7062 | if (mActiveTracks.size() > 0) { | 
|  | 7063 | status = INVALID_OPERATION; | 
|  | 7064 | } else { | 
|  | 7065 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7066 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7067 | } | 
|  | 7068 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 7069 | // forward device change to effects that have requested to be | 
|  | 7070 | // aware of attached audio device. | 
|  | 7071 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7072 | mEffectChains[i]->setDevice_l(value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7073 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7074 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7075 | // store input device and output device but do not forward output device to audio HAL. | 
|  | 7076 | // Note that status is ignored by the caller for output device | 
|  | 7077 | // (see AudioFlinger::setParameters() | 
|  | 7078 | if (audio_is_output_devices(value)) { | 
|  | 7079 | mOutDevice = value; | 
|  | 7080 | status = BAD_VALUE; | 
|  | 7081 | } else { | 
|  | 7082 | mInDevice = value; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7083 | if (value != AUDIO_DEVICE_NONE) { | 
|  | 7084 | mPrevInDevice = value; | 
|  | 7085 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7086 | // disable AEC and NS if the device is a BT SCO headset supporting those | 
|  | 7087 | // pre processings | 
|  | 7088 | if (mTracks.size() > 0) { | 
|  | 7089 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
|  | 7090 | mAudioFlinger->btNrecIsOff(); | 
|  | 7091 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 7092 | sp<RecordTrack> track = mTracks[i]; | 
|  | 7093 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
|  | 7094 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7095 | } | 
|  | 7096 | } | 
|  | 7097 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7098 | } | 
|  | 7099 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
|  | 7100 | mAudioSource != (audio_source_t)value) { | 
|  | 7101 | // forward device change to effects that have requested to be | 
|  | 7102 | // aware of attached audio device. | 
|  | 7103 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7104 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7105 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7106 | mAudioSource = (audio_source_t)value; | 
|  | 7107 | } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7108 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7109 | if (status == NO_ERROR) { | 
|  | 7110 | status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
|  | 7111 | keyValuePair.string()); | 
|  | 7112 | if (status == INVALID_OPERATION) { | 
|  | 7113 | inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7114 | status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
|  | 7115 | keyValuePair.string()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7116 | } | 
|  | 7117 | if (reconfig) { | 
|  | 7118 | if (status == BAD_VALUE && | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7119 | audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && | 
|  | 7120 | audio_is_linear_pcm(reqFormat) && | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7121 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7122 | <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7123 | audio_channel_count_from_in_mask( | 
| Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 7124 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7125 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7126 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7127 | if (status == NO_ERROR) { | 
|  | 7128 | readInputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7129 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7130 | } | 
|  | 7131 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7132 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7133 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7134 | return reconfig; | 
|  | 7135 | } | 
|  | 7136 |  | 
|  | 7137 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
|  | 7138 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7139 | Mutex::Autolock _l(mLock); | 
|  | 7140 | if (initCheck() != NO_ERROR) { | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7141 | return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7142 | } | 
|  | 7143 |  | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7144 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); | 
|  | 7145 | const String8 out_s8(s); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7146 | free(s); | 
|  | 7147 | return out_s8; | 
|  | 7148 | } | 
|  | 7149 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7150 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7151 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
|  | 7152 |  | 
|  | 7153 | desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7154 |  | 
|  | 7155 | switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7156 | case AUDIO_INPUT_OPENED: | 
|  | 7157 | case AUDIO_INPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7158 | desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7159 | desc->mChannelMask = mChannelMask; | 
|  | 7160 | desc->mSamplingRate = mSampleRate; | 
|  | 7161 | desc->mFormat = mFormat; | 
|  | 7162 | desc->mFrameCount = mFrameCount; | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7163 | desc->mFrameCountHAL = mFrameCount; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7164 | desc->mLatency = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7165 | break; | 
|  | 7166 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7167 | case AUDIO_INPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7168 | default: | 
|  | 7169 | break; | 
|  | 7170 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7171 | mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7172 | } | 
|  | 7173 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7174 | void AudioFlinger::RecordThread::readInputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7175 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7176 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); | 
|  | 7177 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7178 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7179 | if (mChannelCount > FCC_8) { | 
|  | 7180 | ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); | 
|  | 7181 | } | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7182 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); | 
|  | 7183 | mFormat = mHALFormat; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7184 | if (!audio_is_linear_pcm(mFormat)) { | 
|  | 7185 | ALOGE("HAL format %#x is not linear pcm", mFormat); | 
| Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 7186 | } | 
| Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 7187 | mFrameSize = audio_stream_in_frame_size(mInput->stream); | 
| Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7188 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); | 
|  | 7189 | mFrameCount = mBufferSize / mFrameSize; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7190 | // This is the formula for calculating the temporary buffer size. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7191 | // 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] | 7192 | // 1 full output buffer, regardless of the alignment of the available input. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7193 | // The value is somewhat arbitrary, and could probably be even larger. | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7194 | // A larger value should allow more old data to be read after a track calls start(), | 
|  | 7195 | // without increasing latency. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7196 | // | 
|  | 7197 | // Note this is independent of the maximum downsampling ratio permitted for capture. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7198 | mRsmpInFrames = mFrameCount * 7; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7199 | mRsmpInFramesP2 = roundup(mRsmpInFrames); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7200 | free(mRsmpInBuffer); | 
| Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7201 | mRsmpInBuffer = NULL; | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7202 |  | 
|  | 7203 | // TODO optimize audio capture buffer sizes ... | 
|  | 7204 | // Here we calculate the size of the sliding buffer used as a source | 
|  | 7205 | // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7). | 
|  | 7206 | // For current HAL frame counts, this is usually 2048 = 40 ms.  It would | 
|  | 7207 | // be better to have it derived from the pipe depth in the long term. | 
|  | 7208 | // The current value is higher than necessary.  However it should not add to latency. | 
|  | 7209 |  | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7210 | // 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] | 7211 | size_t bufferSize = (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize; | 
|  | 7212 | (void)posix_memalign(&mRsmpInBuffer, 32, bufferSize); | 
|  | 7213 | memset(mRsmpInBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7214 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7215 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. | 
|  | 7216 | // 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] | 7217 | } | 
|  | 7218 |  | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7219 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7220 | { | 
|  | 7221 | Mutex::Autolock _l(mLock); | 
|  | 7222 | if (initCheck() != NO_ERROR) { | 
|  | 7223 | return 0; | 
|  | 7224 | } | 
|  | 7225 |  | 
|  | 7226 | return mInput->stream->get_input_frames_lost(mInput->stream); | 
|  | 7227 | } | 
|  | 7228 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7229 | uint32_t AudioFlinger::RecordThread::hasAudioSession(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7230 | { | 
|  | 7231 | Mutex::Autolock _l(mLock); | 
|  | 7232 | uint32_t result = 0; | 
|  | 7233 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 7234 | result = EFFECT_SESSION; | 
|  | 7235 | } | 
|  | 7236 |  | 
|  | 7237 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 7238 | if (sessionId == mTracks[i]->sessionId()) { | 
|  | 7239 | result |= TRACK_SESSION; | 
|  | 7240 | break; | 
|  | 7241 | } | 
|  | 7242 | } | 
|  | 7243 |  | 
|  | 7244 | return result; | 
|  | 7245 | } | 
|  | 7246 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7247 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7248 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7249 | KeyedVector<audio_session_t, bool> ids; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7250 | Mutex::Autolock _l(mLock); | 
|  | 7251 | for (size_t j = 0; j < mTracks.size(); ++j) { | 
|  | 7252 | sp<RecordThread::RecordTrack> track = mTracks[j]; | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7253 | audio_session_t sessionId = track->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7254 | if (ids.indexOfKey(sessionId) < 0) { | 
|  | 7255 | ids.add(sessionId, true); | 
|  | 7256 | } | 
|  | 7257 | } | 
|  | 7258 | return ids; | 
|  | 7259 | } | 
|  | 7260 |  | 
|  | 7261 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
|  | 7262 | { | 
|  | 7263 | Mutex::Autolock _l(mLock); | 
|  | 7264 | AudioStreamIn *input = mInput; | 
|  | 7265 | mInput = NULL; | 
|  | 7266 | return input; | 
|  | 7267 | } | 
|  | 7268 |  | 
|  | 7269 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
|  | 7270 | audio_stream_t* AudioFlinger::RecordThread::stream() const | 
|  | 7271 | { | 
|  | 7272 | if (mInput == NULL) { | 
|  | 7273 | return NULL; | 
|  | 7274 | } | 
|  | 7275 | return &mInput->stream->common; | 
|  | 7276 | } | 
|  | 7277 |  | 
|  | 7278 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 7279 | { | 
|  | 7280 | // only one chain per input thread | 
|  | 7281 | if (mEffectChains.size() != 0) { | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7282 | 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] | 7283 | return INVALID_OPERATION; | 
|  | 7284 | } | 
|  | 7285 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7286 | chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7287 | chain->setInBuffer(NULL); | 
|  | 7288 | chain->setOutBuffer(NULL); | 
|  | 7289 |  | 
|  | 7290 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 7291 |  | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7292 | // make sure enabled pre processing effects state is communicated to the HAL as we | 
|  | 7293 | // just moved them to a new input stream. | 
|  | 7294 | chain->syncHalEffectsState(); | 
|  | 7295 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7296 | mEffectChains.add(chain); | 
|  | 7297 |  | 
|  | 7298 | return NO_ERROR; | 
|  | 7299 | } | 
|  | 7300 |  | 
|  | 7301 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 7302 | { | 
|  | 7303 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
|  | 7304 | ALOGW_IF(mEffectChains.size() != 1, | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7305 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7306 | chain.get(), mEffectChains.size(), this); | 
|  | 7307 | if (mEffectChains.size() == 1) { | 
|  | 7308 | mEffectChains.removeAt(0); | 
|  | 7309 | } | 
|  | 7310 | return 0; | 
|  | 7311 | } | 
|  | 7312 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7313 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 7314 | audio_patch_handle_t *handle) | 
|  | 7315 | { | 
|  | 7316 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7317 |  | 
|  | 7318 | // store new device and send to effects | 
|  | 7319 | mInDevice = patch->sources[0].ext.device.type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7320 | mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7321 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7322 | mEffectChains[i]->setDevice_l(mInDevice); | 
|  | 7323 | } | 
|  | 7324 |  | 
|  | 7325 | // disable AEC and NS if the device is a BT SCO headset supporting those | 
|  | 7326 | // pre processings | 
|  | 7327 | if (mTracks.size() > 0) { | 
|  | 7328 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
|  | 7329 | mAudioFlinger->btNrecIsOff(); | 
|  | 7330 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 7331 | sp<RecordTrack> track = mTracks[i]; | 
|  | 7332 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
|  | 7333 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
|  | 7334 | } | 
|  | 7335 | } | 
|  | 7336 |  | 
|  | 7337 | // store new source and send to effects | 
|  | 7338 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
|  | 7339 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7340 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7341 | mEffectChains[i]->setAudioSource_l(mAudioSource); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7342 | } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7343 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7344 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7345 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7346 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); | 
|  | 7347 | status = hwDevice->create_audio_patch(hwDevice, | 
|  | 7348 | patch->num_sources, | 
|  | 7349 | patch->sources, | 
|  | 7350 | patch->num_sinks, | 
|  | 7351 | patch->sinks, | 
|  | 7352 | handle); | 
|  | 7353 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7354 | char *address; | 
|  | 7355 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { | 
|  | 7356 | address = audio_device_address_to_parameter( | 
|  | 7357 | patch->sources[0].ext.device.type, | 
|  | 7358 | patch->sources[0].ext.device.address); | 
|  | 7359 | } else { | 
|  | 7360 | address = (char *)calloc(1, 1); | 
|  | 7361 | } | 
|  | 7362 | AudioParameter param = AudioParameter(String8(address)); | 
|  | 7363 | free(address); | 
|  | 7364 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), | 
|  | 7365 | (int)patch->sources[0].ext.device.type); | 
|  | 7366 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), | 
|  | 7367 | (int)patch->sinks[0].ext.mix.usecase.source); | 
|  | 7368 | status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
|  | 7369 | param.toString().string()); | 
|  | 7370 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7371 | } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7372 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7373 | if (mInDevice != mPrevInDevice) { | 
|  | 7374 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
|  | 7375 | mPrevInDevice = mInDevice; | 
|  | 7376 | } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7377 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7378 | return status; | 
|  | 7379 | } | 
|  | 7380 |  | 
|  | 7381 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 7382 | { | 
|  | 7383 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7384 |  | 
|  | 7385 | mInDevice = AUDIO_DEVICE_NONE; | 
|  | 7386 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7387 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
|  | 7388 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); | 
|  | 7389 | status = hwDevice->release_audio_patch(hwDevice, handle); | 
|  | 7390 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7391 | AudioParameter param; | 
|  | 7392 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); | 
|  | 7393 | status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
|  | 7394 | param.toString().string()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7395 | } | 
|  | 7396 | return status; | 
|  | 7397 | } | 
|  | 7398 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7399 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) | 
|  | 7400 | { | 
|  | 7401 | Mutex::Autolock _l(mLock); | 
|  | 7402 | mTracks.add(record); | 
|  | 7403 | } | 
|  | 7404 |  | 
|  | 7405 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) | 
|  | 7406 | { | 
|  | 7407 | Mutex::Autolock _l(mLock); | 
|  | 7408 | destroyTrack_l(record); | 
|  | 7409 | } | 
|  | 7410 |  | 
|  | 7411 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) | 
|  | 7412 | { | 
|  | 7413 | ThreadBase::getAudioPortConfig(config); | 
|  | 7414 | config->role = AUDIO_PORT_ROLE_SINK; | 
|  | 7415 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); | 
|  | 7416 | config->ext.mix.usecase.source = mAudioSource; | 
|  | 7417 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7418 |  | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7419 | } // namespace android |