| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | ** | 
|  | 3 | ** Copyright 2012, The Android Open Source Project | 
|  | 4 | ** | 
|  | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | ** you may not use this file except in compliance with the License. | 
|  | 7 | ** You may obtain a copy of the License at | 
|  | 8 | ** | 
|  | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | ** | 
|  | 11 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | ** See the License for the specific language governing permissions and | 
|  | 15 | ** limitations under the License. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | #define LOG_TAG "AudioFlinger" | 
|  | 20 | //#define LOG_NDEBUG 0 | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 22 |  | 
| Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 23 | #include "Configuration.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 24 | #include <math.h> | 
|  | 25 | #include <fcntl.h> | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 26 | #include <linux/futex.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> | 
| Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/AudioParameter.h> | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 31 | #include <media/AudioResamplerPublic.h> | 
| Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 32 | #include <media/RecordBufferConverter.h> | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 33 | #include <media/TypeConverter.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 34 | #include <utils/Log.h> | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 35 | #include <utils/Trace.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | #include <private/media/AudioTrackShared.h> | 
| Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> | 
| Glenn Kasten | 6bf707f | 2017-02-23 16:53:58 -0800 | [diff] [blame] | 39 | #include <audio_utils/mono_blend.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | #include <audio_utils/primitives.h> | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 41 | #include <audio_utils/format.h> | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 42 | #include <audio_utils/minifloat.h> | 
| Mikhail Naganov | 9fe9401 | 2016-10-14 14:57:40 -0700 | [diff] [blame] | 43 | #include <system/audio_effects/effect_ns.h> | 
|  | 44 | #include <system/audio_effects/effect_aec.h> | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 45 | #include <system/audio.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | // NBAIO implementations | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 48 | #include <media/nbaio/AudioStreamInSource.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 49 | #include <media/nbaio/AudioStreamOutSink.h> | 
|  | 50 | #include <media/nbaio/MonoPipe.h> | 
|  | 51 | #include <media/nbaio/MonoPipeReader.h> | 
|  | 52 | #include <media/nbaio/Pipe.h> | 
|  | 53 | #include <media/nbaio/PipeReader.h> | 
|  | 54 | #include <media/nbaio/SourceAudioBufferProvider.h> | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 55 | #include <mediautils/BatteryNotifier.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 56 |  | 
|  | 57 | #include <powermanager/PowerManager.h> | 
|  | 58 |  | 
| Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 59 | #include <media/audiohal/EffectsFactoryHalInterface.h> | 
|  | 60 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 61 | #include "AudioFlinger.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "FastMixer.h" | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 63 | #include "FastCapture.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 | #include "ServiceUtilities.h" | 
| Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 65 | #include "mediautils/SchedulingPolicyService.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 66 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 67 | #ifdef ADD_BATTERY_DATA | 
|  | 68 | #include <media/IMediaPlayerService.h> | 
|  | 69 | #include <media/IMediaDeathNotifier.h> | 
|  | 70 | #endif | 
|  | 71 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 72 | #ifdef DEBUG_CPU_USAGE | 
|  | 73 | #include <cpustats/CentralTendencyStatistics.h> | 
|  | 74 | #include <cpustats/ThreadCpuUsage.h> | 
|  | 75 | #endif | 
|  | 76 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 77 | #include "AutoPark.h" | 
|  | 78 |  | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 79 | #include <pthread.h> | 
|  | 80 | #include "TypedLogger.h" | 
|  | 81 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 82 | // ---------------------------------------------------------------------------- | 
|  | 83 |  | 
|  | 84 | // Note: the following macro is used for extremely verbose logging message.  In | 
|  | 85 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
|  | 86 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
|  | 87 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
|  | 88 | // turned on.  Do not uncomment the #def below unless you really know what you | 
|  | 89 | // are doing and want to see all of the extremely verbose messages. | 
|  | 90 | //#define VERY_VERY_VERBOSE_LOGGING | 
|  | 91 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
|  | 92 | #define ALOGVV ALOGV | 
|  | 93 | #else | 
|  | 94 | #define ALOGVV(a...) do { } while(0) | 
|  | 95 | #endif | 
|  | 96 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 97 | // TODO: Move these macro/inlines to a header file. | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 98 | #define max(a, b) ((a) > (b) ? (a) : (b)) | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 99 | template <typename T> | 
|  | 100 | static inline T min(const T& a, const T& b) | 
|  | 101 | { | 
|  | 102 | return a < b ? a : b; | 
|  | 103 | } | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 104 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 105 | namespace android { | 
|  | 106 |  | 
|  | 107 | // retry counts for buffer fill timeout | 
|  | 108 | // 50 * ~20msecs = 1 second | 
|  | 109 | static const int8_t kMaxTrackRetries = 50; | 
|  | 110 | static const int8_t kMaxTrackStartupRetries = 50; | 
|  | 111 | // allow less retry attempts on direct output thread. | 
|  | 112 | // direct outputs can be a scarce resource in audio hardware and should | 
|  | 113 | // be released as quickly as possible. | 
|  | 114 | static const int8_t kMaxTrackRetriesDirect = 2; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 115 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 116 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 117 |  | 
|  | 118 | // don't warn about blocked writes or record buffer overflows more often than this | 
|  | 119 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
|  | 120 |  | 
|  | 121 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
|  | 122 | static const int kRecordThreadSleepUs = 5000; | 
|  | 123 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 124 | // maximum time to wait in sendConfigEvent_l() for a status to be received | 
|  | 125 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 126 |  | 
|  | 127 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
|  | 128 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
|  | 129 | // maximum divider applied to the active sleep time in the mixer thread loop | 
|  | 130 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
|  | 131 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 132 | // minimum normal sink buffer size, expressed in milliseconds rather than frames | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 133 | // FIXME This should be based on experimentally observed scheduling jitter | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 134 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; | 
|  | 135 | // maximum normal sink buffer size | 
|  | 136 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 137 |  | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 138 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread | 
|  | 139 | // FIXME This should be based on experimentally observed scheduling jitter | 
|  | 140 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; | 
|  | 141 |  | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 142 | // Offloaded output thread standby delay: allows track transition without going to standby | 
|  | 143 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); | 
|  | 144 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 145 | // Direct output thread minimum sleep time in idle or active(underrun) state | 
|  | 146 | static const nsecs_t kDirectMinSleepTimeUs = 10000; | 
|  | 147 |  | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 148 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good | 
|  | 149 | // balance between power consumption and latency, and allows threads to be scheduled reliably | 
|  | 150 | // by the CFS scheduler. | 
|  | 151 | // FIXME Express other hardcoded references to 20ms with references to this constant and move | 
|  | 152 | // it appropriately. | 
|  | 153 | #define FMS_20 20 | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 154 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 155 | // Whether to use fast mixer | 
|  | 156 | static const enum { | 
|  | 157 | FastMixer_Never,    // never initialize or use: for debugging only | 
|  | 158 | FastMixer_Always,   // always initialize and use, even if not needed: for debugging only | 
|  | 159 | // normal mixer multiplier is 1 | 
|  | 160 | FastMixer_Static,   // initialize if needed, then use all the time if initialized, | 
|  | 161 | // multiplier is calculated based on min & max normal mixer buffer size | 
|  | 162 | FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load, | 
|  | 163 | // multiplier is calculated based on min & max normal mixer buffer size | 
|  | 164 | // FIXME for FastMixer_Dynamic: | 
|  | 165 | //  Supporting this option will require fixing HALs that can't handle large writes. | 
|  | 166 | //  For example, one HAL implementation returns an error from a large write, | 
|  | 167 | //  and another HAL implementation corrupts memory, possibly in the sample rate converter. | 
|  | 168 | //  We could either fix the HAL implementations, or provide a wrapper that breaks | 
|  | 169 | //  up large writes into smaller ones, and the wrapper would need to deal with scheduler. | 
|  | 170 | } kUseFastMixer = FastMixer_Static; | 
|  | 171 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 172 | // Whether to use fast capture | 
|  | 173 | static const enum { | 
|  | 174 | FastCapture_Never,  // never initialize or use: for debugging only | 
|  | 175 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only | 
|  | 176 | FastCapture_Static, // initialize if needed, then use all the time if initialized | 
|  | 177 | } kUseFastCapture = FastCapture_Static; | 
|  | 178 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 179 | // Priorities for requestPriority | 
|  | 180 | static const int kPriorityAudioApp = 2; | 
|  | 181 | static const int kPriorityFastMixer = 3; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 182 | static const int kPriorityFastCapture = 3; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 183 |  | 
| Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 184 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the | 
|  | 185 | // track buffer in shared memory.  Zero on input means to use a default value.  For fast tracks, | 
|  | 186 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 187 |  | 
|  | 188 | // This is the default value, if not specified by property. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 189 | static const int kFastTrackMultiplier = 2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 190 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 191 | // The minimum and maximum allowed values | 
|  | 192 | static const int kFastTrackMultiplierMin = 1; | 
|  | 193 | static const int kFastTrackMultiplierMax = 2; | 
|  | 194 |  | 
|  | 195 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. | 
|  | 196 | static int sFastTrackMultiplier = kFastTrackMultiplier; | 
|  | 197 |  | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 198 | // See Thread::readOnlyHeap(). | 
|  | 199 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. | 
|  | 200 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), | 
|  | 201 | // and that all "fast" AudioRecord clients read from.  In either case, the size can be small. | 
| Glenn Kasten | 691b02a | 2017-10-03 10:12:20 -0700 | [diff] [blame] | 202 | static const size_t kRecordThreadReadOnlyHeapSize = 0x4000; | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 203 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 204 | // ---------------------------------------------------------------------------- | 
|  | 205 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 206 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; | 
|  | 207 |  | 
|  | 208 | static void sFastTrackMultiplierInit() | 
|  | 209 | { | 
|  | 210 | char value[PROPERTY_VALUE_MAX]; | 
|  | 211 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { | 
|  | 212 | char *endptr; | 
|  | 213 | unsigned long ul = strtoul(value, &endptr, 0); | 
|  | 214 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { | 
|  | 215 | sFastTrackMultiplier = (int) ul; | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | // ---------------------------------------------------------------------------- | 
|  | 221 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 222 | #ifdef ADD_BATTERY_DATA | 
|  | 223 | // To collect the amplifier usage | 
|  | 224 | static void addBatteryData(uint32_t params) { | 
|  | 225 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
|  | 226 | if (service == NULL) { | 
|  | 227 | // it already logged | 
|  | 228 | return; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | service->addBatteryData(params); | 
|  | 232 | } | 
|  | 233 | #endif | 
|  | 234 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 235 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset | 
|  | 236 | struct { | 
|  | 237 | // call when you acquire a partial wakelock | 
|  | 238 | void acquire(const sp<IBinder> &wakeLockToken) { | 
|  | 239 | pthread_mutex_lock(&mLock); | 
|  | 240 | if (wakeLockToken.get() == nullptr) { | 
|  | 241 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); | 
|  | 242 | } else { | 
|  | 243 | if (mCount == 0) { | 
|  | 244 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); | 
|  | 245 | } | 
|  | 246 | ++mCount; | 
|  | 247 | } | 
|  | 248 | pthread_mutex_unlock(&mLock); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | // call when you release a partial wakelock. | 
|  | 252 | void release(const sp<IBinder> &wakeLockToken) { | 
|  | 253 | if (wakeLockToken.get() == nullptr) { | 
|  | 254 | return; | 
|  | 255 | } | 
|  | 256 | pthread_mutex_lock(&mLock); | 
|  | 257 | if (--mCount < 0) { | 
|  | 258 | ALOGE("negative wakelock count"); | 
|  | 259 | mCount = 0; | 
|  | 260 | } | 
|  | 261 | pthread_mutex_unlock(&mLock); | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | // retrieves the boottime timebase offset from monotonic. | 
|  | 265 | int64_t getBoottimeOffset() { | 
|  | 266 | pthread_mutex_lock(&mLock); | 
|  | 267 | int64_t boottimeOffset = mBoottimeOffset; | 
|  | 268 | pthread_mutex_unlock(&mLock); | 
|  | 269 | return boottimeOffset; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC | 
|  | 273 | // and the selected timebase. | 
|  | 274 | // Currently only TIMEBASE_BOOTTIME is allowed. | 
|  | 275 | // | 
|  | 276 | // This only needs to be called upon acquiring the first partial wakelock | 
|  | 277 | // after all other partial wakelocks are released. | 
|  | 278 | // | 
|  | 279 | // We do an empirical measurement of the offset rather than parsing | 
|  | 280 | // /proc/timer_list since the latter is not a formal kernel ABI. | 
|  | 281 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { | 
|  | 282 | int clockbase; | 
|  | 283 | switch (timebase) { | 
|  | 284 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: | 
|  | 285 | clockbase = SYSTEM_TIME_BOOTTIME; | 
|  | 286 | break; | 
|  | 287 | default: | 
|  | 288 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); | 
|  | 289 | break; | 
|  | 290 | } | 
|  | 291 | // try three times to get the clock offset, choose the one | 
|  | 292 | // with the minimum gap in measurements. | 
|  | 293 | const int tries = 3; | 
|  | 294 | nsecs_t bestGap, measured; | 
|  | 295 | for (int i = 0; i < tries; ++i) { | 
|  | 296 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 297 | const nsecs_t tbase = systemTime(clockbase); | 
|  | 298 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 299 | const nsecs_t gap = tmono2 - tmono; | 
|  | 300 | if (i == 0 || gap < bestGap) { | 
|  | 301 | bestGap = gap; | 
|  | 302 | measured = tbase - ((tmono + tmono2) >> 1); | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | // to avoid micro-adjusting, we don't change the timebase | 
|  | 307 | // unless it is significantly different. | 
|  | 308 | // | 
|  | 309 | // Assumption: It probably takes more than toleranceNs to | 
|  | 310 | // suspend and resume the device. | 
|  | 311 | static int64_t toleranceNs = 10000; // 10 us | 
|  | 312 | if (llabs(*offset - measured) > toleranceNs) { | 
|  | 313 | ALOGV("Adjusting timebase offset old: %lld  new: %lld", | 
|  | 314 | (long long)*offset, (long long)measured); | 
|  | 315 | *offset = measured; | 
|  | 316 | } | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | pthread_mutex_t mLock; | 
|  | 320 | int32_t mCount; | 
|  | 321 | int64_t mBoottimeOffset; | 
|  | 322 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 323 |  | 
|  | 324 | // ---------------------------------------------------------------------------- | 
|  | 325 | //      CPU Stats | 
|  | 326 | // ---------------------------------------------------------------------------- | 
|  | 327 |  | 
|  | 328 | class CpuStats { | 
|  | 329 | public: | 
|  | 330 | CpuStats(); | 
|  | 331 | void sample(const String8 &title); | 
|  | 332 | #ifdef DEBUG_CPU_USAGE | 
|  | 333 | private: | 
|  | 334 | ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns | 
|  | 335 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns | 
|  | 336 |  | 
|  | 337 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles | 
|  | 338 |  | 
|  | 339 | int mCpuNum;                        // thread's current CPU number | 
|  | 340 | int mCpukHz;                        // frequency of thread's current CPU in kHz | 
|  | 341 | #endif | 
|  | 342 | }; | 
|  | 343 |  | 
|  | 344 | CpuStats::CpuStats() | 
|  | 345 | #ifdef DEBUG_CPU_USAGE | 
|  | 346 | : mCpuNum(-1), mCpukHz(-1) | 
|  | 347 | #endif | 
|  | 348 | { | 
|  | 349 | } | 
|  | 350 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 351 | void CpuStats::sample(const String8 &title | 
|  | 352 | #ifndef DEBUG_CPU_USAGE | 
|  | 353 | __unused | 
|  | 354 | #endif | 
|  | 355 | ) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 356 | #ifdef DEBUG_CPU_USAGE | 
|  | 357 | // get current thread's delta CPU time in wall clock ns | 
|  | 358 | double wcNs; | 
|  | 359 | bool valid = mCpuUsage.sampleAndEnable(wcNs); | 
|  | 360 |  | 
|  | 361 | // record sample for wall clock statistics | 
|  | 362 | if (valid) { | 
|  | 363 | mWcStats.sample(wcNs); | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | // get the current CPU number | 
|  | 367 | int cpuNum = sched_getcpu(); | 
|  | 368 |  | 
|  | 369 | // get the current CPU frequency in kHz | 
|  | 370 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); | 
|  | 371 |  | 
|  | 372 | // check if either CPU number or frequency changed | 
|  | 373 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { | 
|  | 374 | mCpuNum = cpuNum; | 
|  | 375 | mCpukHz = cpukHz; | 
|  | 376 | // ignore sample for purposes of cycles | 
|  | 377 | valid = false; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | // if no change in CPU number or frequency, then record sample for cycle statistics | 
|  | 381 | if (valid && mCpukHz > 0) { | 
|  | 382 | double cycles = wcNs * cpukHz * 0.000001; | 
|  | 383 | mHzStats.sample(cycles); | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | unsigned n = mWcStats.n(); | 
|  | 387 | // mCpuUsage.elapsed() is expensive, so don't call it every loop | 
|  | 388 | if ((n & 127) == 1) { | 
|  | 389 | long long elapsed = mCpuUsage.elapsed(); | 
|  | 390 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
|  | 391 | double perLoop = elapsed / (double) n; | 
|  | 392 | double perLoop100 = perLoop * 0.01; | 
|  | 393 | double perLoop1k = perLoop * 0.001; | 
|  | 394 | double mean = mWcStats.mean(); | 
|  | 395 | double stddev = mWcStats.stddev(); | 
|  | 396 | double minimum = mWcStats.minimum(); | 
|  | 397 | double maximum = mWcStats.maximum(); | 
|  | 398 | double meanCycles = mHzStats.mean(); | 
|  | 399 | double stddevCycles = mHzStats.stddev(); | 
|  | 400 | double minCycles = mHzStats.minimum(); | 
|  | 401 | double maxCycles = mHzStats.maximum(); | 
|  | 402 | mCpuUsage.resetElapsed(); | 
|  | 403 | mWcStats.reset(); | 
|  | 404 | mHzStats.reset(); | 
|  | 405 | ALOGD("CPU usage for %s over past %.1f secs\n" | 
|  | 406 | "  (%u mixer loops at %.1f mean ms per loop):\n" | 
|  | 407 | "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" | 
|  | 408 | "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" | 
|  | 409 | "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", | 
|  | 410 | title.string(), | 
|  | 411 | elapsed * .000000001, n, perLoop * .000001, | 
|  | 412 | mean * .001, | 
|  | 413 | stddev * .001, | 
|  | 414 | minimum * .001, | 
|  | 415 | maximum * .001, | 
|  | 416 | mean / perLoop100, | 
|  | 417 | stddev / perLoop100, | 
|  | 418 | minimum / perLoop100, | 
|  | 419 | maximum / perLoop100, | 
|  | 420 | meanCycles / perLoop1k, | 
|  | 421 | stddevCycles / perLoop1k, | 
|  | 422 | minCycles / perLoop1k, | 
|  | 423 | maxCycles / perLoop1k); | 
|  | 424 |  | 
|  | 425 | } | 
|  | 426 | } | 
|  | 427 | #endif | 
|  | 428 | }; | 
|  | 429 |  | 
|  | 430 | // ---------------------------------------------------------------------------- | 
|  | 431 | //      ThreadBase | 
|  | 432 | // ---------------------------------------------------------------------------- | 
|  | 433 |  | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 434 | // static | 
|  | 435 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) | 
|  | 436 | { | 
|  | 437 | switch (type) { | 
|  | 438 | case MIXER: | 
|  | 439 | return "MIXER"; | 
|  | 440 | case DIRECT: | 
|  | 441 | return "DIRECT"; | 
|  | 442 | case DUPLICATING: | 
|  | 443 | return "DUPLICATING"; | 
|  | 444 | case RECORD: | 
|  | 445 | return "RECORD"; | 
|  | 446 | case OFFLOAD: | 
|  | 447 | return "OFFLOAD"; | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 448 | case MMAP: | 
|  | 449 | return "MMAP"; | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 450 | default: | 
|  | 451 | return "unknown"; | 
|  | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 455 | std::string devicesToString(audio_devices_t devices) | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 456 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 457 | std::string result; | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 458 | if (devices & AUDIO_DEVICE_BIT_IN) { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 459 | InputDeviceConverter::maskToString(devices, result); | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 460 | } else { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 461 | OutputDeviceConverter::maskToString(devices, result); | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 462 | } | 
|  | 463 | return result; | 
|  | 464 | } | 
|  | 465 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 466 | std::string inputFlagsToString(audio_input_flags_t flags) | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 467 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 468 | std::string result; | 
|  | 469 | InputFlagConverter::maskToString(flags, result); | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 470 | return result; | 
|  | 471 | } | 
|  | 472 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 473 | std::string outputFlagsToString(audio_output_flags_t flags) | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 474 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 475 | std::string result; | 
|  | 476 | OutputFlagConverter::maskToString(flags, result); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 477 | return result; | 
|  | 478 | } | 
|  | 479 |  | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 480 | const char *sourceToString(audio_source_t source) | 
|  | 481 | { | 
|  | 482 | switch (source) { | 
|  | 483 | case AUDIO_SOURCE_DEFAULT:              return "default"; | 
|  | 484 | case AUDIO_SOURCE_MIC:                  return "mic"; | 
|  | 485 | case AUDIO_SOURCE_VOICE_UPLINK:         return "voice uplink"; | 
|  | 486 | case AUDIO_SOURCE_VOICE_DOWNLINK:       return "voice downlink"; | 
|  | 487 | case AUDIO_SOURCE_VOICE_CALL:           return "voice call"; | 
|  | 488 | case AUDIO_SOURCE_CAMCORDER:            return "camcorder"; | 
|  | 489 | case AUDIO_SOURCE_VOICE_RECOGNITION:    return "voice recognition"; | 
|  | 490 | case AUDIO_SOURCE_VOICE_COMMUNICATION:  return "voice communication"; | 
|  | 491 | case AUDIO_SOURCE_REMOTE_SUBMIX:        return "remote submix"; | 
| rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 492 | case AUDIO_SOURCE_UNPROCESSED:          return "unprocessed"; | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 493 | case AUDIO_SOURCE_FM_TUNER:             return "FM tuner"; | 
|  | 494 | case AUDIO_SOURCE_HOTWORD:              return "hotword"; | 
|  | 495 | default:                                return "unknown"; | 
|  | 496 | } | 
|  | 497 | } | 
|  | 498 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 499 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 500 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 501 | :   Thread(false /*canCallJava*/), | 
|  | 502 | mType(type), | 
| Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 503 | mAudioFlinger(audioFlinger), | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 504 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 505 | // are set by PlaybackThread::readOutputParameters_l() or | 
|  | 506 | // RecordThread::readInputParameters_l() | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 507 | //FIXME: mStandby should be true here. Is this some kind of hack? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 508 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 509 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), | 
|  | 510 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 511 | // mName will be set by concrete (non-virtual) subclass | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 512 | mDeathRecipient(new PMDeathRecipient(this)), | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 513 | mSystemReady(systemReady), | 
|  | 514 | mSignalPending(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 515 | { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 516 | memset(&mPatch, 0, sizeof(struct audio_patch)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 517 | } | 
|  | 518 |  | 
|  | 519 | AudioFlinger::ThreadBase::~ThreadBase() | 
|  | 520 | { | 
| Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 521 | // mConfigEvents should be empty, but just in case it isn't, free the memory it owns | 
| Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 522 | mConfigEvents.clear(); | 
|  | 523 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 524 | // do not lock the mutex in destructor | 
|  | 525 | releaseWakeLock_l(); | 
|  | 526 | if (mPowerManager != 0) { | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 527 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 528 | binder->unlinkToDeath(mDeathRecipient); | 
|  | 529 | } | 
|  | 530 | } | 
|  | 531 |  | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 532 | status_t AudioFlinger::ThreadBase::readyToRun() | 
|  | 533 | { | 
|  | 534 | status_t status = initCheck(); | 
|  | 535 | if (status == NO_ERROR) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 536 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 537 | } else { | 
|  | 538 | ALOGE("No working audio driver found."); | 
|  | 539 | } | 
|  | 540 | return status; | 
|  | 541 | } | 
|  | 542 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 543 | void AudioFlinger::ThreadBase::exit() | 
|  | 544 | { | 
|  | 545 | ALOGV("ThreadBase::exit"); | 
|  | 546 | // do any cleanup required for exit to succeed | 
|  | 547 | preExit(); | 
|  | 548 | { | 
|  | 549 | // This lock prevents the following race in thread (uniprocessor for illustration): | 
|  | 550 | //  if (!exitPending()) { | 
|  | 551 | //      // context switch from here to exit() | 
|  | 552 | //      // exit() calls requestExit(), what exitPending() observes | 
|  | 553 | //      // exit() calls signal(), which is dropped since no waiters | 
|  | 554 | //      // context switch back from exit() to here | 
|  | 555 | //      mWaitWorkCV.wait(...); | 
|  | 556 | //      // now thread is hung | 
|  | 557 | //  } | 
|  | 558 | AutoMutex lock(mLock); | 
|  | 559 | requestExit(); | 
|  | 560 | mWaitWorkCV.broadcast(); | 
|  | 561 | } | 
|  | 562 | // When Thread::requestExitAndWait is made virtual and this method is renamed to | 
|  | 563 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" | 
|  | 564 | requestExitAndWait(); | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
|  | 568 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 569 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
|  | 570 | Mutex::Autolock _l(mLock); | 
|  | 571 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 572 | return sendSetParameterConfigEvent_l(keyValuePairs); | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
|  | 576 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). | 
|  | 577 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) | 
|  | 578 | { | 
|  | 579 | status_t status = NO_ERROR; | 
|  | 580 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 581 | if (event->mRequiresSystemReady && !mSystemReady) { | 
|  | 582 | event->mWaitStatus = false; | 
|  | 583 | mPendingConfigEvents.add(event); | 
|  | 584 | return status; | 
|  | 585 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 586 | mConfigEvents.add(event); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 587 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 588 | mWaitWorkCV.signal(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 589 | mLock.unlock(); | 
|  | 590 | { | 
|  | 591 | Mutex::Autolock _l(event->mLock); | 
|  | 592 | while (event->mWaitStatus) { | 
|  | 593 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { | 
|  | 594 | event->mStatus = TIMED_OUT; | 
|  | 595 | event->mWaitStatus = false; | 
|  | 596 | } | 
|  | 597 | } | 
|  | 598 | status = event->mStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 599 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 600 | mLock.lock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 601 | return status; | 
|  | 602 | } | 
|  | 603 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 604 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 605 | { | 
|  | 606 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 607 | sendIoConfigEvent_l(event, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 608 | } | 
|  | 609 |  | 
|  | 610 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 611 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 612 | { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 613 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 614 | sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 615 | } | 
|  | 616 |  | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 617 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 618 | { | 
|  | 619 | Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 620 | sendPrioConfigEvent_l(pid, tid, prio, forApp); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 621 | } | 
|  | 622 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 623 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 624 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l( | 
|  | 625 | pid_t pid, pid_t tid, int32_t prio, bool forApp) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 626 | { | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 627 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 628 | sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 629 | } | 
|  | 630 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 631 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held | 
|  | 632 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 633 | { | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 634 | sp<ConfigEvent> configEvent; | 
|  | 635 | AudioParameter param(keyValuePair); | 
|  | 636 | int value; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 637 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 638 | setMasterMono_l(value != 0); | 
|  | 639 | if (param.size() == 1) { | 
|  | 640 | return NO_ERROR; // should be a solo parameter - we don't pass down | 
|  | 641 | } | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 642 | param.remove(String8(AudioParameter::keyMonoOutput)); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 643 | configEvent = new SetParameterConfigEvent(param.toString()); | 
|  | 644 | } else { | 
|  | 645 | configEvent = new SetParameterConfigEvent(keyValuePair); | 
|  | 646 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 647 | return sendConfigEvent_l(configEvent); | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 648 | } | 
|  | 649 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 650 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( | 
|  | 651 | const struct audio_patch *patch, | 
|  | 652 | audio_patch_handle_t *handle) | 
|  | 653 | { | 
|  | 654 | Mutex::Autolock _l(mLock); | 
|  | 655 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); | 
|  | 656 | status_t status = sendConfigEvent_l(configEvent); | 
|  | 657 | if (status == NO_ERROR) { | 
|  | 658 | CreateAudioPatchConfigEventData *data = | 
|  | 659 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); | 
|  | 660 | *handle = data->mHandle; | 
|  | 661 | } | 
|  | 662 | return status; | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( | 
|  | 666 | const audio_patch_handle_t handle) | 
|  | 667 | { | 
|  | 668 | Mutex::Autolock _l(mLock); | 
|  | 669 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); | 
|  | 670 | return sendConfigEvent_l(configEvent); | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 |  | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 674 | // post condition: mConfigEvents.isEmpty() | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 675 | void AudioFlinger::ThreadBase::processConfigEvents_l() | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 676 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 677 | bool configChanged = false; | 
|  | 678 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 679 | while (!mConfigEvents.isEmpty()) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 680 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 681 | sp<ConfigEvent> event = mConfigEvents[0]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 682 | mConfigEvents.removeAt(0); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 683 | switch (event->mType) { | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 684 | case CFG_EVENT_PRIO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 685 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); | 
|  | 686 | // FIXME Need to understand why this has to be done asynchronously | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 687 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 688 | true /*asynchronous*/); | 
|  | 689 | if (err != 0) { | 
|  | 690 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 691 | data->mPrio, data->mPid, data->mTid, err); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 692 | } | 
|  | 693 | } break; | 
|  | 694 | case CFG_EVENT_IO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 695 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 696 | ioConfigChanged(data->mEvent, data->mPid); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 697 | } break; | 
|  | 698 | case CFG_EVENT_SET_PARAMETER: { | 
|  | 699 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); | 
|  | 700 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { | 
|  | 701 | configChanged = true; | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 702 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", | 
|  | 703 | data->mKeyValuePairs.string()); | 
| Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 704 | } | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 705 | } break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 706 | case CFG_EVENT_CREATE_AUDIO_PATCH: { | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 707 | const audio_devices_t oldDevice = getDevice(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 708 | CreateAudioPatchConfigEventData *data = | 
|  | 709 | (CreateAudioPatchConfigEventData *)event->mData.get(); | 
|  | 710 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 711 | const audio_devices_t newDevice = getDevice(); | 
|  | 712 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", | 
|  | 713 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), | 
|  | 714 | (unsigned)newDevice, devicesToString(newDevice).c_str()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 715 | } break; | 
|  | 716 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 717 | const audio_devices_t oldDevice = getDevice(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 718 | ReleaseAudioPatchConfigEventData *data = | 
|  | 719 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); | 
|  | 720 | event->mStatus = releaseAudioPatch_l(data->mHandle); | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 721 | const audio_devices_t newDevice = getDevice(); | 
|  | 722 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", | 
|  | 723 | (unsigned)oldDevice, devicesToString(oldDevice).c_str(), | 
|  | 724 | (unsigned)newDevice, devicesToString(newDevice).c_str()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 725 | } break; | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 726 | default: | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 727 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 728 | break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 729 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 730 | { | 
|  | 731 | Mutex::Autolock _l(event->mLock); | 
|  | 732 | if (event->mWaitStatus) { | 
|  | 733 | event->mWaitStatus = false; | 
|  | 734 | event->mCond.signal(); | 
|  | 735 | } | 
|  | 736 | } | 
|  | 737 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 | if (configChanged) { | 
|  | 741 | cacheParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 742 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 | } | 
|  | 744 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 745 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { | 
|  | 746 | String8 s; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 747 | const audio_channel_representation_t representation = | 
|  | 748 | audio_channel_mask_get_representation(mask); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 749 |  | 
|  | 750 | switch (representation) { | 
|  | 751 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { | 
|  | 752 | if (output) { | 
|  | 753 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); | 
|  | 754 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); | 
|  | 755 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); | 
|  | 756 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); | 
|  | 757 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); | 
|  | 758 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); | 
|  | 759 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); | 
|  | 760 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); | 
|  | 761 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); | 
|  | 762 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); | 
|  | 763 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); | 
|  | 764 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); | 
|  | 765 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); | 
|  | 766 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); | 
|  | 767 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); | 
|  | 768 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); | 
|  | 769 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); | 
|  | 770 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); | 
|  | 771 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  "); | 
|  | 772 | } else { | 
|  | 773 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); | 
|  | 774 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); | 
|  | 775 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); | 
|  | 776 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); | 
|  | 777 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); | 
|  | 778 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); | 
|  | 779 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); | 
|  | 780 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); | 
|  | 781 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); | 
|  | 782 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); | 
|  | 783 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); | 
|  | 784 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); | 
|  | 785 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); | 
|  | 786 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); | 
|  | 787 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  "); | 
|  | 788 | } | 
|  | 789 | const int len = s.length(); | 
|  | 790 | if (len > 2) { | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 791 | (void) s.lockBuffer(len);      // needed? | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 792 | s.unlockBuffer(len - 2);       // remove trailing ", " | 
|  | 793 | } | 
|  | 794 | return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 795 | } | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 796 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: | 
|  | 797 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); | 
|  | 798 | return s; | 
|  | 799 | default: | 
|  | 800 | s.appendFormat("unknown mask, representation:%d  bits:%#x", | 
|  | 801 | representation, audio_channel_mask_get_bits(mask)); | 
|  | 802 | return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 803 | } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 804 | } | 
|  | 805 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 806 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 807 | { | 
|  | 808 | const size_t SIZE = 256; | 
|  | 809 | char buffer[SIZE]; | 
|  | 810 | String8 result; | 
|  | 811 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 812 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", | 
|  | 813 | this, mThreadName, getTid(), type(), threadTypeToString(type())); | 
|  | 814 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 815 | bool locked = AudioFlinger::dumpTryLock(mLock); | 
|  | 816 | if (!locked) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 817 | dprintf(fd, "  Thread may be deadlocked\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 820 | dprintf(fd, "  I/O handle: %d\n", mId); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 821 | dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no"); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 822 | dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 823 | dprintf(fd, "  HAL frame count: %zu\n", mFrameCount); | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 824 | dprintf(fd, "  HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat).c_str()); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 825 | dprintf(fd, "  HAL buffer size: %zu bytes\n", mBufferSize); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 826 | dprintf(fd, "  Channel count: %u\n", mChannelCount); | 
|  | 827 | dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask, | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 828 | channelMaskToString(mChannelMask, mType != RECORD).string()); | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 829 | dprintf(fd, "  Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat).c_str()); | 
| Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 830 | dprintf(fd, "  Processing frame size: %zu bytes\n", mFrameSize); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 831 | dprintf(fd, "  Pending config events:"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 832 | size_t numConfig = mConfigEvents.size(); | 
|  | 833 | if (numConfig) { | 
|  | 834 | for (size_t i = 0; i < numConfig; i++) { | 
|  | 835 | mConfigEvents[i]->dump(buffer, SIZE); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 836 | dprintf(fd, "\n    %s", buffer); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 837 | } | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 838 | dprintf(fd, "\n"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 839 | } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 840 | dprintf(fd, " none\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 841 | } | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 842 | // Note: output device may be used by capture threads for effects such as AEC. | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 843 | dprintf(fd, "  Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).c_str()); | 
|  | 844 | dprintf(fd, "  Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).c_str()); | 
| Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 845 | dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 846 |  | 
|  | 847 | if (locked) { | 
|  | 848 | mLock.unlock(); | 
|  | 849 | } | 
|  | 850 | } | 
|  | 851 |  | 
|  | 852 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
|  | 853 | { | 
|  | 854 | const size_t SIZE = 256; | 
|  | 855 | char buffer[SIZE]; | 
|  | 856 | String8 result; | 
|  | 857 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 858 | size_t numEffectChains = mEffectChains.size(); | 
| Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 859 | snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 860 | write(fd, buffer, strlen(buffer)); | 
|  | 861 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 862 | for (size_t i = 0; i < numEffectChains; ++i) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 863 | sp<EffectChain> chain = mEffectChains[i]; | 
|  | 864 | if (chain != 0) { | 
|  | 865 | chain->dump(fd, args); | 
|  | 866 | } | 
|  | 867 | } | 
|  | 868 | } | 
|  | 869 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 870 | void AudioFlinger::ThreadBase::acquireWakeLock() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 871 | { | 
|  | 872 | Mutex::Autolock _l(mLock); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 873 | acquireWakeLock_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 874 | } | 
|  | 875 |  | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 876 | String16 AudioFlinger::ThreadBase::getWakeLockTag() | 
|  | 877 | { | 
|  | 878 | switch (mType) { | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 879 | case MIXER: | 
|  | 880 | return String16("AudioMix"); | 
|  | 881 | case DIRECT: | 
|  | 882 | return String16("AudioDirectOut"); | 
|  | 883 | case DUPLICATING: | 
|  | 884 | return String16("AudioDup"); | 
|  | 885 | case RECORD: | 
|  | 886 | return String16("AudioIn"); | 
|  | 887 | case OFFLOAD: | 
|  | 888 | return String16("AudioOffload"); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 889 | case MMAP: | 
|  | 890 | return String16("Mmap"); | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 891 | default: | 
|  | 892 | ALOG_ASSERT(false); | 
|  | 893 | return String16("AudioUnknown"); | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 894 | } | 
|  | 895 | } | 
|  | 896 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 897 | void AudioFlinger::ThreadBase::acquireWakeLock_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 898 | { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 899 | getPowerManager_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 900 | if (mPowerManager != 0) { | 
|  | 901 | sp<IBinder> binder = new BBinder(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 902 | // Uses AID_AUDIOSERVER for wakelock.  updateWakeLockUids_l() updates with client uids. | 
|  | 903 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 904 | binder, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 905 | getWakeLockTag(), | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 906 | String16("audioserver"), | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 907 | true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 908 | if (status == NO_ERROR) { | 
|  | 909 | mWakeLockToken = binder; | 
|  | 910 | } | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 911 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 912 | } | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 913 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 914 | gBoottime.acquire(mWakeLockToken); | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 915 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = | 
|  | 916 | gBoottime.getBoottimeOffset(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 917 | } | 
|  | 918 |  | 
|  | 919 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
|  | 920 | { | 
|  | 921 | Mutex::Autolock _l(mLock); | 
|  | 922 | releaseWakeLock_l(); | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
|  | 926 | { | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 927 | gBoottime.release(mWakeLockToken); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 928 | if (mWakeLockToken != 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 929 | ALOGV("releaseWakeLock_l() %s", mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 930 | if (mPowerManager != 0) { | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 931 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, | 
|  | 932 | true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 933 | } | 
|  | 934 | mWakeLockToken.clear(); | 
|  | 935 | } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 936 | } | 
|  | 937 |  | 
|  | 938 | void AudioFlinger::ThreadBase::getPowerManager_l() { | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 939 | if (mSystemReady && mPowerManager == 0) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 940 | // use checkService() to avoid blocking if power service is not up yet | 
|  | 941 | sp<IBinder> binder = | 
|  | 942 | defaultServiceManager()->checkService(String16("power")); | 
|  | 943 | if (binder == 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 944 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 945 | } else { | 
|  | 946 | mPowerManager = interface_cast<IPowerManager>(binder); | 
|  | 947 | binder->linkToDeath(mDeathRecipient); | 
|  | 948 | } | 
|  | 949 | } | 
|  | 950 | } | 
|  | 951 |  | 
| Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 952 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 953 | getPowerManager_l(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 954 |  | 
|  | 955 | #if !LOG_NDEBUG | 
|  | 956 | std::stringstream s; | 
| Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 957 | for (uid_t uid : uids) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 958 | s << uid << " "; | 
|  | 959 | } | 
|  | 960 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); | 
|  | 961 | #endif | 
|  | 962 |  | 
| Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 963 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. | 
|  | 964 | if (mSystemReady) { | 
|  | 965 | ALOGE("no wake lock to update, but system ready!"); | 
|  | 966 | } else { | 
|  | 967 | ALOGW("no wake lock to update, system not ready yet"); | 
|  | 968 | } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 969 | return; | 
|  | 970 | } | 
|  | 971 | if (mPowerManager != 0) { | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 972 | std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints | 
|  | 973 | status_t status = mPowerManager->updateWakeLockUids( | 
|  | 974 | mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(), | 
|  | 975 | true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 976 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 977 | } | 
|  | 978 | } | 
|  | 979 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 980 | void AudioFlinger::ThreadBase::clearPowerManager() | 
|  | 981 | { | 
|  | 982 | Mutex::Autolock _l(mLock); | 
|  | 983 | releaseWakeLock_l(); | 
|  | 984 | mPowerManager.clear(); | 
|  | 985 | } | 
|  | 986 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 987 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 988 | { | 
|  | 989 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 990 | if (thread != 0) { | 
|  | 991 | thread->clearPowerManager(); | 
|  | 992 | } | 
|  | 993 | ALOGW("power manager service died !!!"); | 
|  | 994 | } | 
|  | 995 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 996 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 997 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 998 | { | 
|  | 999 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1000 | if (chain != 0) { | 
|  | 1001 | if (type != NULL) { | 
|  | 1002 | chain->setEffectSuspended_l(type, suspend); | 
|  | 1003 | } else { | 
|  | 1004 | chain->setEffectSuspendedAll_l(suspend); | 
|  | 1005 | } | 
|  | 1006 | } | 
|  | 1007 |  | 
|  | 1008 | updateSuspendedSessions_l(type, suspend, sessionId); | 
|  | 1009 | } | 
|  | 1010 |  | 
|  | 1011 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
|  | 1012 | { | 
|  | 1013 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
|  | 1014 | if (index < 0) { | 
|  | 1015 | return; | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = | 
|  | 1019 | mSuspendedSessions.valueAt(index); | 
|  | 1020 |  | 
|  | 1021 | for (size_t i = 0; i < sessionEffects.size(); i++) { | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1022 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1023 | for (int j = 0; j < desc->mRefCount; j++) { | 
|  | 1024 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
|  | 1025 | chain->setEffectSuspendedAll_l(true); | 
|  | 1026 | } else { | 
|  | 1027 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
|  | 1028 | desc->mType.timeLow); | 
|  | 1029 | chain->setEffectSuspended_l(&desc->mType, true); | 
|  | 1030 | } | 
|  | 1031 | } | 
|  | 1032 | } | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
|  | 1036 | bool suspend, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1037 | audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1038 | { | 
|  | 1039 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); | 
|  | 1040 |  | 
|  | 1041 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
|  | 1042 |  | 
|  | 1043 | if (suspend) { | 
|  | 1044 | if (index >= 0) { | 
|  | 1045 | sessionEffects = mSuspendedSessions.valueAt(index); | 
|  | 1046 | } else { | 
|  | 1047 | mSuspendedSessions.add(sessionId, sessionEffects); | 
|  | 1048 | } | 
|  | 1049 | } else { | 
|  | 1050 | if (index < 0) { | 
|  | 1051 | return; | 
|  | 1052 | } | 
|  | 1053 | sessionEffects = mSuspendedSessions.valueAt(index); | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 |  | 
|  | 1057 | int key = EffectChain::kKeyForSuspendAll; | 
|  | 1058 | if (type != NULL) { | 
|  | 1059 | key = type->timeLow; | 
|  | 1060 | } | 
|  | 1061 | index = sessionEffects.indexOfKey(key); | 
|  | 1062 |  | 
|  | 1063 | sp<SuspendedSessionDesc> desc; | 
|  | 1064 | if (suspend) { | 
|  | 1065 | if (index >= 0) { | 
|  | 1066 | desc = sessionEffects.valueAt(index); | 
|  | 1067 | } else { | 
|  | 1068 | desc = new SuspendedSessionDesc(); | 
|  | 1069 | if (type != NULL) { | 
|  | 1070 | desc->mType = *type; | 
|  | 1071 | } | 
|  | 1072 | sessionEffects.add(key, desc); | 
|  | 1073 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
|  | 1074 | } | 
|  | 1075 | desc->mRefCount++; | 
|  | 1076 | } else { | 
|  | 1077 | if (index < 0) { | 
|  | 1078 | return; | 
|  | 1079 | } | 
|  | 1080 | desc = sessionEffects.valueAt(index); | 
|  | 1081 | if (--desc->mRefCount == 0) { | 
|  | 1082 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
|  | 1083 | sessionEffects.removeItemsAt(index); | 
|  | 1084 | if (sessionEffects.isEmpty()) { | 
|  | 1085 | ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
|  | 1086 | sessionId); | 
|  | 1087 | mSuspendedSessions.removeItem(sessionId); | 
|  | 1088 | } | 
|  | 1089 | } | 
|  | 1090 | } | 
|  | 1091 | if (!sessionEffects.isEmpty()) { | 
|  | 1092 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
|  | 1093 | } | 
|  | 1094 | } | 
|  | 1095 |  | 
|  | 1096 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
|  | 1097 | bool enabled, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1098 | audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1099 | { | 
|  | 1100 | Mutex::Autolock _l(mLock); | 
|  | 1101 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
|  | 1102 | } | 
|  | 1103 |  | 
|  | 1104 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
|  | 1105 | bool enabled, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1106 | audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1107 | { | 
|  | 1108 | if (mType != RECORD) { | 
|  | 1109 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
|  | 1110 | // another session. This gives the priority to well behaved effect control panels | 
|  | 1111 | // and applications not using global effects. | 
|  | 1112 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect | 
|  | 1113 | // global effects | 
|  | 1114 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { | 
|  | 1115 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
|  | 1116 | } | 
|  | 1117 | } | 
|  | 1118 |  | 
|  | 1119 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1120 | if (chain != 0) { | 
|  | 1121 | chain->checkSuspendOnEffectEnabled(effect, enabled); | 
|  | 1122 | } | 
|  | 1123 | } | 
|  | 1124 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1125 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held | 
|  | 1126 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( | 
|  | 1127 | const effect_descriptor_t *desc, audio_session_t sessionId) | 
|  | 1128 | { | 
|  | 1129 | // No global effect sessions on record threads | 
|  | 1130 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
|  | 1131 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", | 
|  | 1132 | desc->name, mThreadName); | 
|  | 1133 | return BAD_VALUE; | 
|  | 1134 | } | 
|  | 1135 | // only pre processing effects on record thread | 
|  | 1136 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { | 
|  | 1137 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", | 
|  | 1138 | desc->name, mThreadName); | 
|  | 1139 | return BAD_VALUE; | 
|  | 1140 | } | 
| Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1141 |  | 
|  | 1142 | // always allow effects without processing load or latency | 
|  | 1143 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { | 
|  | 1144 | return NO_ERROR; | 
|  | 1145 | } | 
|  | 1146 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1147 | audio_input_flags_t flags = mInput->flags; | 
|  | 1148 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { | 
|  | 1149 | if (flags & AUDIO_INPUT_FLAG_RAW) { | 
|  | 1150 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", | 
|  | 1151 | desc->name, mThreadName); | 
|  | 1152 | return BAD_VALUE; | 
|  | 1153 | } | 
|  | 1154 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
|  | 1155 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", | 
|  | 1156 | desc->name, mThreadName); | 
|  | 1157 | return BAD_VALUE; | 
|  | 1158 | } | 
|  | 1159 | } | 
|  | 1160 | return NO_ERROR; | 
|  | 1161 | } | 
|  | 1162 |  | 
|  | 1163 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held | 
|  | 1164 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( | 
|  | 1165 | const effect_descriptor_t *desc, audio_session_t sessionId) | 
|  | 1166 | { | 
|  | 1167 | // no preprocessing on playback threads | 
|  | 1168 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { | 
|  | 1169 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" | 
|  | 1170 | " thread %s", desc->name, mThreadName); | 
|  | 1171 | return BAD_VALUE; | 
|  | 1172 | } | 
|  | 1173 |  | 
| Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1174 | // always allow effects without processing load or latency | 
|  | 1175 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { | 
|  | 1176 | return NO_ERROR; | 
|  | 1177 | } | 
|  | 1178 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1179 | switch (mType) { | 
|  | 1180 | case MIXER: { | 
| Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1181 | #ifndef MULTICHANNEL_EFFECT_CHAIN | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1182 | // Reject any effect on mixer multichannel sinks. | 
|  | 1183 | // TODO: fix both format and multichannel issues with effects. | 
|  | 1184 | if (mChannelCount != FCC_2) { | 
|  | 1185 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" | 
|  | 1186 | " thread %s", desc->name, mChannelCount, mThreadName); | 
|  | 1187 | return BAD_VALUE; | 
|  | 1188 | } | 
| Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1189 | #endif | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1190 | audio_output_flags_t flags = mOutput->flags; | 
|  | 1191 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { | 
|  | 1192 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 1193 | // global effects are applied only to non fast tracks if they are SW | 
|  | 1194 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
|  | 1195 | break; | 
|  | 1196 | } | 
|  | 1197 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
|  | 1198 | // only post processing on output stage session | 
|  | 1199 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { | 
|  | 1200 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" | 
|  | 1201 | " on output stage session", desc->name); | 
|  | 1202 | return BAD_VALUE; | 
|  | 1203 | } | 
|  | 1204 | } else { | 
|  | 1205 | // no restriction on effects applied on non fast tracks | 
|  | 1206 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { | 
|  | 1207 | break; | 
|  | 1208 | } | 
|  | 1209 | } | 
| Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1210 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1211 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { | 
|  | 1212 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", | 
|  | 1213 | desc->name); | 
|  | 1214 | return BAD_VALUE; | 
|  | 1215 | } | 
|  | 1216 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
|  | 1217 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" | 
|  | 1218 | " in fast mode", desc->name); | 
|  | 1219 | return BAD_VALUE; | 
|  | 1220 | } | 
|  | 1221 | } | 
|  | 1222 | } break; | 
|  | 1223 | case OFFLOAD: | 
| Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1224 | // nothing actionable on offload threads, if the effect: | 
|  | 1225 | //   - is offloadable: the effect can be created | 
|  | 1226 | //   - is NOT offloadable: the effect should still be created, but EffectHandle::enable() | 
|  | 1227 | //     will take care of invalidating the tracks of the thread | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1228 | break; | 
|  | 1229 | case DIRECT: | 
|  | 1230 | // Reject any effect on Direct output threads for now, since the format of | 
|  | 1231 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). | 
|  | 1232 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", | 
|  | 1233 | desc->name, mThreadName); | 
|  | 1234 | return BAD_VALUE; | 
|  | 1235 | case DUPLICATING: | 
| Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1236 | #ifndef MULTICHANNEL_EFFECT_CHAIN | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1237 | // Reject any effect on mixer multichannel sinks. | 
|  | 1238 | // TODO: fix both format and multichannel issues with effects. | 
|  | 1239 | if (mChannelCount != FCC_2) { | 
|  | 1240 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" | 
|  | 1241 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); | 
|  | 1242 | return BAD_VALUE; | 
|  | 1243 | } | 
| Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1244 | #endif | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1245 | if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) { | 
|  | 1246 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" | 
|  | 1247 | " thread %s", desc->name, mThreadName); | 
|  | 1248 | return BAD_VALUE; | 
|  | 1249 | } | 
|  | 1250 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { | 
|  | 1251 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" | 
|  | 1252 | " DUPLICATING thread %s", desc->name, mThreadName); | 
|  | 1253 | return BAD_VALUE; | 
|  | 1254 | } | 
|  | 1255 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { | 
|  | 1256 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" | 
|  | 1257 | " DUPLICATING thread %s", desc->name, mThreadName); | 
|  | 1258 | return BAD_VALUE; | 
|  | 1259 | } | 
|  | 1260 | break; | 
|  | 1261 | default: | 
|  | 1262 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | return NO_ERROR; | 
|  | 1266 | } | 
|  | 1267 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1268 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held | 
|  | 1269 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
|  | 1270 | const sp<AudioFlinger::Client>& client, | 
|  | 1271 | const sp<IEffectClient>& effectClient, | 
|  | 1272 | int32_t priority, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1273 | audio_session_t sessionId, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1274 | effect_descriptor_t *desc, | 
|  | 1275 | int *enabled, | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1276 | status_t *status, | 
|  | 1277 | bool pinned) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1278 | { | 
|  | 1279 | sp<EffectModule> effect; | 
|  | 1280 | sp<EffectHandle> handle; | 
|  | 1281 | status_t lStatus; | 
|  | 1282 | sp<EffectChain> chain; | 
|  | 1283 | bool chainCreated = false; | 
|  | 1284 | bool effectCreated = false; | 
|  | 1285 | bool effectRegistered = false; | 
| Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1286 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1287 |  | 
|  | 1288 | lStatus = initCheck(); | 
|  | 1289 | if (lStatus != NO_ERROR) { | 
|  | 1290 | ALOGW("createEffect_l() Audio driver not initialized."); | 
|  | 1291 | goto Exit; | 
|  | 1292 | } | 
|  | 1293 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1294 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
|  | 1295 |  | 
|  | 1296 | { // scope for mLock | 
|  | 1297 | Mutex::Autolock _l(mLock); | 
|  | 1298 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1299 | lStatus = checkEffectCompatibility_l(desc, sessionId); | 
|  | 1300 | if (lStatus != NO_ERROR) { | 
|  | 1301 | goto Exit; | 
|  | 1302 | } | 
|  | 1303 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1304 | // check for existing effect chain with the requested audio session | 
|  | 1305 | chain = getEffectChain_l(sessionId); | 
|  | 1306 | if (chain == 0) { | 
|  | 1307 | // create a new chain for this session | 
|  | 1308 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
|  | 1309 | chain = new EffectChain(this, sessionId); | 
|  | 1310 | addEffectChain_l(chain); | 
|  | 1311 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 1312 | chainCreated = true; | 
|  | 1313 | } else { | 
|  | 1314 | effect = chain->getEffectFromDesc_l(desc); | 
|  | 1315 | } | 
|  | 1316 |  | 
|  | 1317 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
|  | 1318 |  | 
|  | 1319 | if (effect == 0) { | 
| Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1320 | effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1321 | // Check CPU and memory usage | 
| Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1322 | lStatus = AudioSystem::registerEffect( | 
|  | 1323 | desc, mId, chain->strategy(), sessionId, effectId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 | if (lStatus != NO_ERROR) { | 
|  | 1325 | goto Exit; | 
|  | 1326 | } | 
|  | 1327 | effectRegistered = true; | 
|  | 1328 | // create a new effect module if none present in the chain | 
| Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1329 | lStatus = chain->createEffect_l(effect, this, desc, effectId, sessionId, pinned); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1330 | if (lStatus != NO_ERROR) { | 
|  | 1331 | goto Exit; | 
|  | 1332 | } | 
|  | 1333 | effectCreated = true; | 
|  | 1334 |  | 
|  | 1335 | effect->setDevice(mOutDevice); | 
|  | 1336 | effect->setDevice(mInDevice); | 
|  | 1337 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 1338 | effect->setAudioSource(mAudioSource); | 
|  | 1339 | } | 
|  | 1340 | // create effect handle and connect it to effect module | 
|  | 1341 | handle = new EffectHandle(effect, client, effectClient, priority); | 
| Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1342 | lStatus = handle->initCheck(); | 
|  | 1343 | if (lStatus == OK) { | 
|  | 1344 | lStatus = effect->addHandle(handle.get()); | 
|  | 1345 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1346 | if (enabled != NULL) { | 
|  | 1347 | *enabled = (int)effect->isEnabled(); | 
|  | 1348 | } | 
|  | 1349 | } | 
|  | 1350 |  | 
|  | 1351 | Exit: | 
|  | 1352 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
|  | 1353 | Mutex::Autolock _l(mLock); | 
|  | 1354 | if (effectCreated) { | 
|  | 1355 | chain->removeEffect_l(effect); | 
|  | 1356 | } | 
|  | 1357 | if (effectRegistered) { | 
| Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1358 | AudioSystem::unregisterEffect(effectId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1359 | } | 
|  | 1360 | if (chainCreated) { | 
|  | 1361 | removeEffectChain_l(chain); | 
|  | 1362 | } | 
|  | 1363 | handle.clear(); | 
|  | 1364 | } | 
|  | 1365 |  | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1366 | *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1367 | return handle; | 
|  | 1368 | } | 
|  | 1369 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1370 | void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle, | 
|  | 1371 | bool unpinIfLast) | 
|  | 1372 | { | 
|  | 1373 | bool remove = false; | 
|  | 1374 | sp<EffectModule> effect; | 
|  | 1375 | { | 
|  | 1376 | Mutex::Autolock _l(mLock); | 
|  | 1377 |  | 
|  | 1378 | effect = handle->effect().promote(); | 
|  | 1379 | if (effect == 0) { | 
|  | 1380 | return; | 
|  | 1381 | } | 
|  | 1382 | // restore suspended effects if the disconnected handle was enabled and the last one. | 
|  | 1383 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); | 
|  | 1384 | if (remove) { | 
|  | 1385 | removeEffect_l(effect, true); | 
|  | 1386 | } | 
|  | 1387 | } | 
|  | 1388 | if (remove) { | 
|  | 1389 | mAudioFlinger->updateOrphanEffectChains(effect); | 
|  | 1390 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 1391 | if (handle->enabled()) { | 
|  | 1392 | checkSuspendOnEffectEnabled(effect, false, effect->sessionId()); | 
|  | 1393 | } | 
|  | 1394 | } | 
|  | 1395 | } | 
|  | 1396 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1397 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, | 
|  | 1398 | int effectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1399 | { | 
|  | 1400 | Mutex::Autolock _l(mLock); | 
|  | 1401 | return getEffect_l(sessionId, effectId); | 
|  | 1402 | } | 
|  | 1403 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1404 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, | 
|  | 1405 | int effectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1406 | { | 
|  | 1407 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1408 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; | 
|  | 1409 | } | 
|  | 1410 |  | 
|  | 1411 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
|  | 1412 | // PlaybackThread::mLock held | 
|  | 1413 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
|  | 1414 | { | 
|  | 1415 | // check for existing effect chain with the requested audio session | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1416 | audio_session_t sessionId = effect->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1417 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1418 | bool chainCreated = false; | 
|  | 1419 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1420 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1421 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %#x", | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1422 | this, effect->desc().name, effect->desc().flags); | 
|  | 1423 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1424 | if (chain == 0) { | 
|  | 1425 | // create a new chain for this session | 
|  | 1426 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
|  | 1427 | chain = new EffectChain(this, sessionId); | 
|  | 1428 | addEffectChain_l(chain); | 
|  | 1429 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 1430 | chainCreated = true; | 
|  | 1431 | } | 
|  | 1432 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); | 
|  | 1433 |  | 
|  | 1434 | if (chain->getEffectFromId_l(effect->id()) != 0) { | 
|  | 1435 | ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
|  | 1436 | this, effect->desc().name, chain.get()); | 
|  | 1437 | return BAD_VALUE; | 
|  | 1438 | } | 
|  | 1439 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1440 | effect->setOffloaded(mType == OFFLOAD, mId); | 
|  | 1441 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1442 | status_t status = chain->addEffect_l(effect); | 
|  | 1443 | if (status != NO_ERROR) { | 
|  | 1444 | if (chainCreated) { | 
|  | 1445 | removeEffectChain_l(chain); | 
|  | 1446 | } | 
|  | 1447 | return status; | 
|  | 1448 | } | 
|  | 1449 |  | 
|  | 1450 | effect->setDevice(mOutDevice); | 
|  | 1451 | effect->setDevice(mInDevice); | 
|  | 1452 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 1453 | effect->setAudioSource(mAudioSource); | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1454 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1455 | return NO_ERROR; | 
|  | 1456 | } | 
|  | 1457 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1458 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1459 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1460 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | effect_descriptor_t desc = effect->desc(); | 
|  | 1462 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 1463 | detachAuxEffect_l(effect->id()); | 
|  | 1464 | } | 
|  | 1465 |  | 
|  | 1466 | sp<EffectChain> chain = effect->chain().promote(); | 
|  | 1467 | if (chain != 0) { | 
|  | 1468 | // remove effect chain if removing last effect | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1469 | if (chain->removeEffect_l(effect, release) == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1470 | removeEffectChain_l(chain); | 
|  | 1471 | } | 
|  | 1472 | } else { | 
|  | 1473 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
|  | 1474 | } | 
|  | 1475 | } | 
|  | 1476 |  | 
|  | 1477 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
|  | 1478 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
|  | 1479 | { | 
|  | 1480 | effectChains = mEffectChains; | 
|  | 1481 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 1482 | mEffectChains[i]->lock(); | 
|  | 1483 | } | 
|  | 1484 | } | 
|  | 1485 |  | 
|  | 1486 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
|  | 1487 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
|  | 1488 | { | 
|  | 1489 | for (size_t i = 0; i < effectChains.size(); i++) { | 
|  | 1490 | effectChains[i]->unlock(); | 
|  | 1491 | } | 
|  | 1492 | } | 
|  | 1493 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1494 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1495 | { | 
|  | 1496 | Mutex::Autolock _l(mLock); | 
|  | 1497 | return getEffectChain_l(sessionId); | 
|  | 1498 | } | 
|  | 1499 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1500 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) | 
|  | 1501 | const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1502 | { | 
|  | 1503 | size_t size = mEffectChains.size(); | 
|  | 1504 | for (size_t i = 0; i < size; i++) { | 
|  | 1505 | if (mEffectChains[i]->sessionId() == sessionId) { | 
|  | 1506 | return mEffectChains[i]; | 
|  | 1507 | } | 
|  | 1508 | } | 
|  | 1509 | return 0; | 
|  | 1510 | } | 
|  | 1511 |  | 
|  | 1512 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
|  | 1513 | { | 
|  | 1514 | Mutex::Autolock _l(mLock); | 
|  | 1515 | size_t size = mEffectChains.size(); | 
|  | 1516 | for (size_t i = 0; i < size; i++) { | 
|  | 1517 | mEffectChains[i]->setMode_l(mode); | 
|  | 1518 | } | 
|  | 1519 | } | 
|  | 1520 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1521 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) | 
|  | 1522 | { | 
|  | 1523 | config->type = AUDIO_PORT_TYPE_MIX; | 
|  | 1524 | config->ext.mix.handle = mId; | 
|  | 1525 | config->sample_rate = mSampleRate; | 
|  | 1526 | config->format = mFormat; | 
|  | 1527 | config->channel_mask = mChannelMask; | 
|  | 1528 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
|  | 1529 | AUDIO_PORT_CONFIG_FORMAT; | 
|  | 1530 | } | 
|  | 1531 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1532 | void AudioFlinger::ThreadBase::systemReady() | 
|  | 1533 | { | 
|  | 1534 | Mutex::Autolock _l(mLock); | 
|  | 1535 | if (mSystemReady) { | 
|  | 1536 | return; | 
|  | 1537 | } | 
|  | 1538 | mSystemReady = true; | 
|  | 1539 |  | 
|  | 1540 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { | 
|  | 1541 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); | 
|  | 1542 | } | 
|  | 1543 | mPendingConfigEvents.clear(); | 
|  | 1544 | } | 
|  | 1545 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1546 | template <typename T> | 
|  | 1547 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) { | 
|  | 1548 | ssize_t index = mActiveTracks.indexOf(track); | 
|  | 1549 | if (index >= 0) { | 
|  | 1550 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); | 
|  | 1551 | return index; | 
|  | 1552 | } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1553 | logTrack("add", track); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1554 | mActiveTracksGeneration++; | 
|  | 1555 | mLatestActiveTrack = track; | 
|  | 1556 | ++mBatteryCounter[track->uid()].second; | 
|  | 1557 | return mActiveTracks.add(track); | 
|  | 1558 | } | 
|  | 1559 |  | 
|  | 1560 | template <typename T> | 
|  | 1561 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) { | 
|  | 1562 | ssize_t index = mActiveTracks.remove(track); | 
|  | 1563 | if (index < 0) { | 
|  | 1564 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); | 
|  | 1565 | return index; | 
|  | 1566 | } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1567 | logTrack("remove", track); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1568 | mActiveTracksGeneration++; | 
|  | 1569 | --mBatteryCounter[track->uid()].second; | 
|  | 1570 | // mLatestActiveTrack is not cleared even if is the same as track. | 
|  | 1571 | return index; | 
|  | 1572 | } | 
|  | 1573 |  | 
|  | 1574 | template <typename T> | 
|  | 1575 | void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() { | 
|  | 1576 | for (const sp<T> &track : mActiveTracks) { | 
|  | 1577 | BatteryNotifier::getInstance().noteStopAudio(track->uid()); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1578 | logTrack("clear", track); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1579 | } | 
|  | 1580 | mLastActiveTracksGeneration = mActiveTracksGeneration; | 
|  | 1581 | mActiveTracks.clear(); | 
|  | 1582 | mLatestActiveTrack.clear(); | 
|  | 1583 | mBatteryCounter.clear(); | 
|  | 1584 | } | 
|  | 1585 |  | 
|  | 1586 | template <typename T> | 
|  | 1587 | void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState( | 
|  | 1588 | sp<ThreadBase> thread, bool force) { | 
|  | 1589 | // Updates ActiveTracks client uids to the thread wakelock. | 
|  | 1590 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { | 
|  | 1591 | thread->updateWakeLockUids_l(getWakeLockUids()); | 
|  | 1592 | mLastActiveTracksGeneration = mActiveTracksGeneration; | 
|  | 1593 | } | 
|  | 1594 |  | 
|  | 1595 | // Updates BatteryNotifier uids | 
|  | 1596 | for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) { | 
|  | 1597 | const uid_t uid = it->first; | 
|  | 1598 | ssize_t &previous = it->second.first; | 
|  | 1599 | ssize_t ¤t = it->second.second; | 
|  | 1600 | if (current > 0) { | 
|  | 1601 | if (previous == 0) { | 
|  | 1602 | BatteryNotifier::getInstance().noteStartAudio(uid); | 
|  | 1603 | } | 
|  | 1604 | previous = current; | 
|  | 1605 | ++it; | 
|  | 1606 | } else if (current == 0) { | 
|  | 1607 | if (previous > 0) { | 
|  | 1608 | BatteryNotifier::getInstance().noteStopAudio(uid); | 
|  | 1609 | } | 
|  | 1610 | it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase. | 
|  | 1611 | } else /* (current < 0) */ { | 
|  | 1612 | LOG_ALWAYS_FATAL("negative battery count %zd", current); | 
|  | 1613 | } | 
|  | 1614 | } | 
|  | 1615 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1616 |  | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1617 | template <typename T> | 
|  | 1618 | void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack( | 
|  | 1619 | const char *funcName, const sp<T> &track) const { | 
|  | 1620 | if (mLocalLog != nullptr) { | 
|  | 1621 | String8 result; | 
|  | 1622 | track->appendDump(result, false /* active */); | 
|  | 1623 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string()); | 
|  | 1624 | } | 
|  | 1625 | } | 
|  | 1626 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1627 | void AudioFlinger::ThreadBase::broadcast_l() | 
|  | 1628 | { | 
|  | 1629 | // Thread could be blocked waiting for async | 
|  | 1630 | // so signal it to handle state changes immediately | 
|  | 1631 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will | 
|  | 1632 | // be lost so we also flag to prevent it blocking on mWaitWorkCV | 
|  | 1633 | mSignalPending = true; | 
|  | 1634 | mWaitWorkCV.broadcast(); | 
|  | 1635 | } | 
|  | 1636 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1637 | // ---------------------------------------------------------------------------- | 
|  | 1638 | //      Playback | 
|  | 1639 | // ---------------------------------------------------------------------------- | 
|  | 1640 |  | 
|  | 1641 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 1642 | AudioStreamOut* output, | 
|  | 1643 | audio_io_handle_t id, | 
|  | 1644 | audio_devices_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1645 | type_t type, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1646 | bool systemReady) | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1647 | :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1648 | mNormalFrameCount(0), mSinkBuffer(NULL), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1649 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1650 | mMixerBuffer(NULL), | 
|  | 1651 | mMixerBufferSize(0), | 
|  | 1652 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), | 
|  | 1653 | mMixerBufferValid(false), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1654 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1655 | mEffectBuffer(NULL), | 
|  | 1656 | mEffectBufferSize(0), | 
|  | 1657 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), | 
|  | 1658 | mEffectBufferValid(false), | 
| Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1659 | mSuspended(0), mBytesWritten(0), | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1660 | mFramesWritten(0), | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1661 | mSuspendedFrames(0), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1662 | mActiveTracks(&this->mLocalLog), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 | // mStreamTypes[] initialized in constructor body | 
|  | 1664 | mOutput(output), | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1665 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1666 | mMixerStatus(MIXER_IDLE), | 
|  | 1667 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1668 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1669 | mBytesRemaining(0), | 
|  | 1670 | mCurrentWriteLength(0), | 
|  | 1671 | mUseAsyncWrite(false), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1672 | mWriteAckSequence(0), | 
|  | 1673 | mDrainSequence(0), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1674 | mScreenState(AudioFlinger::mScreenState), | 
|  | 1675 | // index 0 is reserved for normal mixer's submix | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1676 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 1677 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), | 
|  | 1678 | mLeftVolFloat(-1.0), mRightVolFloat(-1.0) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1679 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1680 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); | 
|  | 1681 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1682 |  | 
|  | 1683 | // Assumes constructor is called by AudioFlinger with it's mLock held, but | 
|  | 1684 | // it would be safer to explicitly pass initial masterVolume/masterMute as | 
|  | 1685 | // parameter. | 
|  | 1686 | // | 
|  | 1687 | // If the HAL we are using has support for master volume or master mute, | 
|  | 1688 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
|  | 1689 | // and the mute set to false). | 
|  | 1690 | mMasterVolume = audioFlinger->masterVolume_l(); | 
|  | 1691 | mMasterMute = audioFlinger->masterMute_l(); | 
|  | 1692 | if (mOutput && mOutput->audioHwDev) { | 
|  | 1693 | if (mOutput->audioHwDev->canSetMasterVolume()) { | 
|  | 1694 | mMasterVolume = 1.0; | 
|  | 1695 | } | 
|  | 1696 |  | 
|  | 1697 | if (mOutput->audioHwDev->canSetMasterMute()) { | 
|  | 1698 | mMasterMute = false; | 
|  | 1699 | } | 
|  | 1700 | } | 
|  | 1701 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1702 | readOutputParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1703 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1704 | // ++ operator does not compile | 
| Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1705 | 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] | 1706 | stream = (audio_stream_type_t) (stream + 1)) { | 
|  | 1707 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); | 
|  | 1708 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); | 
|  | 1709 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1710 | } | 
|  | 1711 |  | 
|  | 1712 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
|  | 1713 | { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1714 | mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1715 | free(mSinkBuffer); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1716 | free(mMixerBuffer); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1717 | free(mEffectBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1718 | } | 
|  | 1719 |  | 
|  | 1720 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
|  | 1721 | { | 
|  | 1722 | dumpInternals(fd, args); | 
|  | 1723 | dumpTracks(fd, args); | 
|  | 1724 | dumpEffectChains(fd, args); | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1725 | dprintf(fd, "  Local log:\n"); | 
|  | 1726 | mLocalLog.dump(fd, "   " /* prefix */, 40 /* lines */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1727 | } | 
|  | 1728 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1729 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1730 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1731 | String8 result; | 
|  | 1732 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1733 | result.appendFormat("  Stream volumes in dB: "); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
|  | 1735 | const stream_type_t *st = &mStreamTypes[i]; | 
|  | 1736 | if (i > 0) { | 
|  | 1737 | result.appendFormat(", "); | 
|  | 1738 | } | 
|  | 1739 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
|  | 1740 | if (st->mute) { | 
|  | 1741 | result.append("M"); | 
|  | 1742 | } | 
|  | 1743 | } | 
|  | 1744 | result.append("\n"); | 
|  | 1745 | write(fd, result.string(), result.length()); | 
|  | 1746 | result.clear(); | 
|  | 1747 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1748 | // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
|  | 1749 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1750 | dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1751 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1752 |  | 
|  | 1753 | size_t numtracks = mTracks.size(); | 
|  | 1754 | size_t numactive = mActiveTracks.size(); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1755 | dprintf(fd, "  %zu Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1756 | size_t numactiveseen = 0; | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1757 | const char *prefix = "    "; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1758 | if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1759 | dprintf(fd, " of which %zu are active\n", numactive); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1760 | result.append(prefix); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1761 | Track::appendDumpHeader(result); | 
|  | 1762 | for (size_t i = 0; i < numtracks; ++i) { | 
|  | 1763 | sp<Track> track = mTracks[i]; | 
|  | 1764 | if (track != 0) { | 
|  | 1765 | bool active = mActiveTracks.indexOf(track) >= 0; | 
|  | 1766 | if (active) { | 
|  | 1767 | numactiveseen++; | 
|  | 1768 | } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1769 | result.append(prefix); | 
|  | 1770 | track->appendDump(result, active); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1771 | } | 
|  | 1772 | } | 
|  | 1773 | } else { | 
|  | 1774 | result.append("\n"); | 
|  | 1775 | } | 
|  | 1776 | if (numactiveseen != numactive) { | 
|  | 1777 | // some tracks in the active list were not in the tracks list | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1778 | result.append("  The following tracks are in the active list but" | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1779 | " not in the track list\n"); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1780 | result.append(prefix); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1781 | Track::appendDumpHeader(result); | 
|  | 1782 | for (size_t i = 0; i < numactive; ++i) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1783 | sp<Track> track = mActiveTracks[i]; | 
|  | 1784 | if (mTracks.indexOf(track) < 0) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1785 | result.append(prefix); | 
|  | 1786 | track->appendDump(result, true /* active */); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1787 | } | 
|  | 1788 | } | 
|  | 1789 | } | 
|  | 1790 |  | 
|  | 1791 | write(fd, result.string(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1792 | } | 
|  | 1793 |  | 
|  | 1794 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 1795 | { | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1796 | dumpBase(fd, args); | 
|  | 1797 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1798 | dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1799 | dprintf(fd, "  Last write occurred (msecs): %llu\n", | 
|  | 1800 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1801 | dprintf(fd, "  Total writes: %d\n", mNumWrites); | 
|  | 1802 | dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites); | 
|  | 1803 | dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no"); | 
|  | 1804 | dprintf(fd, "  Suspend count: %d\n", mSuspended); | 
|  | 1805 | dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer); | 
|  | 1806 | dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer); | 
|  | 1807 | dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer); | 
|  | 1808 | dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1809 | dprintf(fd, "  Standby delay ns=%lld\n", (long long)mStandbyDelayNs); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1810 | AudioStreamOut *output = mOutput; | 
|  | 1811 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 1812 | dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", | 
|  | 1813 | output, flags, outputFlagsToString(flags).c_str()); | 
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 1814 | dprintf(fd, "  Frames written: %lld\n", (long long)mFramesWritten); | 
|  | 1815 | dprintf(fd, "  Suspended frames: %lld\n", (long long)mSuspendedFrames); | 
|  | 1816 | if (mPipeSink.get() != nullptr) { | 
|  | 1817 | dprintf(fd, "  PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); | 
|  | 1818 | } | 
|  | 1819 | if (output != nullptr) { | 
|  | 1820 | dprintf(fd, "  Hal stream dump:\n"); | 
|  | 1821 | (void)output->stream->dump(fd); | 
|  | 1822 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1823 | } | 
|  | 1824 |  | 
|  | 1825 | // Thread virtuals | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1826 |  | 
|  | 1827 | void AudioFlinger::PlaybackThread::onFirstRef() | 
|  | 1828 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1829 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1830 | } | 
|  | 1831 |  | 
|  | 1832 | // ThreadBase virtuals | 
|  | 1833 | void AudioFlinger::PlaybackThread::preExit() | 
|  | 1834 | { | 
|  | 1835 | ALOGV("  preExit()"); | 
|  | 1836 | // FIXME this is using hard-coded strings but in the future, this functionality will be | 
|  | 1837 | //       converted to use audio HAL extensions required to support tunneling | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1838 | status_t result = mOutput->stream->setParameters(String8("exiting=1")); | 
|  | 1839 | ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1840 | } | 
|  | 1841 |  | 
|  | 1842 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
|  | 1843 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( | 
|  | 1844 | const sp<AudioFlinger::Client>& client, | 
|  | 1845 | audio_stream_type_t streamType, | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1846 | uint32_t *pSampleRate, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1847 | audio_format_t format, | 
|  | 1848 | audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1849 | size_t *pFrameCount, | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1850 | size_t *pNotificationFrameCount, | 
|  | 1851 | uint32_t notificationsPerBuffer, | 
|  | 1852 | float speed, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1853 | const sp<IMemory>& sharedBuffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1854 | audio_session_t sessionId, | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1855 | audio_output_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1856 | pid_t tid, | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1857 | uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1858 | status_t *status, | 
|  | 1859 | audio_port_handle_t portId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1860 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1861 | size_t frameCount = *pFrameCount; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1862 | size_t notificationFrameCount = *pNotificationFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1863 | sp<Track> track; | 
|  | 1864 | status_t lStatus; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1865 | audio_output_flags_t outputFlags = mOutput->flags; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1866 | audio_output_flags_t requestedFlags = *flags; | 
|  | 1867 |  | 
|  | 1868 | if (*pSampleRate == 0) { | 
|  | 1869 | *pSampleRate = mSampleRate; | 
|  | 1870 | } | 
|  | 1871 | uint32_t sampleRate = *pSampleRate; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1872 |  | 
|  | 1873 | // special case for FAST flag considered OK if fast mixer is present | 
|  | 1874 | if (hasFastMixer()) { | 
|  | 1875 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); | 
|  | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | // Check if requested flags are compatible with output stream flags | 
|  | 1879 | if ((*flags & outputFlags) != *flags) { | 
|  | 1880 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", | 
|  | 1881 | *flags, outputFlags); | 
|  | 1882 | *flags = (audio_output_flags_t)(*flags & outputFlags); | 
|  | 1883 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1884 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1885 | // client expresses a preference for FAST, but we get the final say | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1886 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1887 | if ( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1888 | // PCM data | 
|  | 1889 | audio_is_linear_pcm(format) && | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1890 | // TODO: extract as a data library function that checks that a computationally | 
|  | 1891 | // expensive downmixer is not required: isFastOutputChannelConversion() | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1892 | (channelMask == mChannelMask || | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1893 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || | 
|  | 1894 | (channelMask == AUDIO_CHANNEL_OUT_MONO | 
|  | 1895 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1896 | // hardware sample rate | 
|  | 1897 | (sampleRate == mSampleRate) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1898 | // normal mixer has an associated fast mixer | 
|  | 1899 | hasFastMixer() && | 
|  | 1900 | // there are sufficient fast track slots available | 
|  | 1901 | (mFastTrackAvailMask != 0) | 
|  | 1902 | // FIXME test that MixerThread for this fast track has a capable output HAL | 
|  | 1903 | // FIXME add a permission test also? | 
|  | 1904 | ) { | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1905 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. | 
|  | 1906 | if (sharedBuffer == 0) { | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1907 | // read the fast track multiplier property the first time it is needed | 
|  | 1908 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); | 
|  | 1909 | if (ok != 0) { | 
|  | 1910 | ALOGE("%s pthread_once failed: %d", __func__, ok); | 
|  | 1911 | } | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1912 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1913 | } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1914 |  | 
|  | 1915 | // check compatibility with audio effects. | 
|  | 1916 | { // scope for mLock | 
|  | 1917 | Mutex::Autolock _l(mLock); | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 1918 | for (audio_session_t session : { | 
|  | 1919 | AUDIO_SESSION_OUTPUT_STAGE, | 
|  | 1920 | AUDIO_SESSION_OUTPUT_MIX, | 
|  | 1921 | sessionId, | 
|  | 1922 | }) { | 
|  | 1923 | sp<EffectChain> chain = getEffectChain_l(session); | 
|  | 1924 | if (chain.get() != nullptr) { | 
|  | 1925 | audio_output_flags_t old = *flags; | 
|  | 1926 | chain->checkOutputFlagCompatibility(flags); | 
|  | 1927 | if (old != *flags) { | 
|  | 1928 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", | 
|  | 1929 | (int)session, (int)old, (int)*flags); | 
|  | 1930 | } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1931 | } | 
|  | 1932 | } | 
|  | 1933 | } | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1934 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1935 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
|  | 1936 | frameCount, mFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1937 | } else { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1938 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " | 
|  | 1939 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1940 | "sampleRate=%u mSampleRate=%u " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1941 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1942 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1943 | audio_is_linear_pcm(format), | 
|  | 1944 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1945 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1946 | } | 
|  | 1947 | } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1948 |  | 
|  | 1949 | if (!audio_has_proportional_frames(format)) { | 
|  | 1950 | if (sharedBuffer != 0) { | 
|  | 1951 | // Same comment as below about ignoring frameCount parameter for set() | 
|  | 1952 | frameCount = sharedBuffer->size(); | 
|  | 1953 | } else if (frameCount == 0) { | 
|  | 1954 | frameCount = mNormalFrameCount; | 
|  | 1955 | } | 
|  | 1956 | if (notificationFrameCount != frameCount) { | 
|  | 1957 | notificationFrameCount = frameCount; | 
|  | 1958 | } | 
|  | 1959 | } else if (sharedBuffer != 0) { | 
|  | 1960 | // FIXME: Ensure client side memory buffers need | 
|  | 1961 | // not have additional alignment beyond sample | 
|  | 1962 | // (e.g. 16 bit stereo accessed as 32 bit frame). | 
|  | 1963 | size_t alignment = audio_bytes_per_sample(format); | 
|  | 1964 | if (alignment & 1) { | 
|  | 1965 | // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). | 
|  | 1966 | alignment = 1; | 
|  | 1967 | } | 
|  | 1968 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); | 
|  | 1969 | size_t frameSize = channelCount * audio_bytes_per_sample(format); | 
|  | 1970 | if (channelCount > 1) { | 
|  | 1971 | // More than 2 channels does not require stronger alignment than stereo | 
|  | 1972 | alignment <<= 1; | 
|  | 1973 | } | 
|  | 1974 | if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) { | 
|  | 1975 | ALOGE("Invalid buffer alignment: address %p, channel count %u", | 
|  | 1976 | sharedBuffer->pointer(), channelCount); | 
|  | 1977 | lStatus = BAD_VALUE; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1978 | goto Exit; | 
|  | 1979 | } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1980 |  | 
|  | 1981 | // When initializing a shared buffer AudioTrack via constructors, | 
|  | 1982 | // there's no frameCount parameter. | 
|  | 1983 | // But when initializing a shared buffer AudioTrack via set(), | 
|  | 1984 | // there _is_ a frameCount parameter.  We silently ignore it. | 
|  | 1985 | frameCount = sharedBuffer->size() / frameSize; | 
|  | 1986 | } else { | 
|  | 1987 | size_t minFrameCount = 0; | 
|  | 1988 | // For fast tracks we try to respect the application's request for notifications per buffer. | 
|  | 1989 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
|  | 1990 | if (notificationsPerBuffer > 0) { | 
|  | 1991 | // Avoid possible arithmetic overflow during multiplication. | 
|  | 1992 | if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { | 
|  | 1993 | ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", | 
|  | 1994 | notificationsPerBuffer, mFrameCount); | 
|  | 1995 | } else { | 
|  | 1996 | minFrameCount = mFrameCount * notificationsPerBuffer; | 
|  | 1997 | } | 
|  | 1998 | } | 
|  | 1999 | } else { | 
|  | 2000 | // For normal PCM streaming tracks, update minimum frame count. | 
|  | 2001 | // Buffer depth is forced to be at least 2 x the normal mixer frame count and | 
|  | 2002 | // cover audio hardware latency. | 
|  | 2003 | // This is probably too conservative, but legacy application code may depend on it. | 
|  | 2004 | // If you change this calculation, also review the start threshold which is related. | 
|  | 2005 | uint32_t latencyMs = latency_l(); | 
|  | 2006 | if (latencyMs == 0) { | 
|  | 2007 | ALOGE("Error when retrieving output stream latency"); | 
|  | 2008 | lStatus = UNKNOWN_ERROR; | 
|  | 2009 | goto Exit; | 
|  | 2010 | } | 
|  | 2011 |  | 
|  | 2012 | minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, | 
|  | 2013 | mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); | 
|  | 2014 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2015 | } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2016 | if (frameCount < minFrameCount) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2017 | frameCount = minFrameCount; | 
|  | 2018 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2019 | } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2020 |  | 
|  | 2021 | // Make sure that application is notified with sufficient margin before underrun. | 
|  | 2022 | // The client can divide the AudioTrack buffer into sub-buffers, | 
|  | 2023 | // and expresses its desire to server as the notification frame count. | 
|  | 2024 | if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { | 
|  | 2025 | size_t maxNotificationFrames; | 
|  | 2026 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
|  | 2027 | // notify every HAL buffer, regardless of the size of the track buffer | 
|  | 2028 | maxNotificationFrames = mFrameCount; | 
|  | 2029 | } else { | 
|  | 2030 | // For normal tracks, use at least double-buffering if no sample rate conversion, | 
|  | 2031 | // or at least triple-buffering if there is sample rate conversion | 
|  | 2032 | const int nBuffering = sampleRate == mSampleRate ? 2 : 3; | 
|  | 2033 | maxNotificationFrames = frameCount / nBuffering; | 
|  | 2034 | // If client requested a fast track but this was denied, then use the smaller maximum. | 
|  | 2035 | if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { | 
|  | 2036 | size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; | 
|  | 2037 | if (maxNotificationFrames > maxNotificationFramesFastDenied) { | 
|  | 2038 | maxNotificationFrames = maxNotificationFramesFastDenied; | 
|  | 2039 | } | 
|  | 2040 | } | 
|  | 2041 | } | 
|  | 2042 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { | 
|  | 2043 | if (notificationFrameCount == 0) { | 
|  | 2044 | ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", | 
|  | 2045 | maxNotificationFrames, frameCount); | 
|  | 2046 | } else { | 
|  | 2047 | ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", | 
|  | 2048 | notificationFrameCount, maxNotificationFrames, frameCount); | 
|  | 2049 | } | 
|  | 2050 | notificationFrameCount = maxNotificationFrames; | 
|  | 2051 | } | 
|  | 2052 | } | 
|  | 2053 |  | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2054 | *pFrameCount = frameCount; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2055 | *pNotificationFrameCount = notificationFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2056 |  | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2057 | switch (mType) { | 
|  | 2058 |  | 
|  | 2059 | case DIRECT: | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2060 | 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] | 2061 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2062 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " | 
|  | 2063 | "for output %p with format %#x", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2064 | sampleRate, format, channelMask, mOutput, mFormat); | 
|  | 2065 | lStatus = BAD_VALUE; | 
|  | 2066 | goto Exit; | 
|  | 2067 | } | 
|  | 2068 | } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2069 | break; | 
|  | 2070 |  | 
|  | 2071 | case OFFLOAD: | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2072 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2073 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" | 
|  | 2074 | "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2075 | sampleRate, format, channelMask, mOutput, mFormat); | 
|  | 2076 | lStatus = BAD_VALUE; | 
|  | 2077 | goto Exit; | 
|  | 2078 | } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2079 | break; | 
|  | 2080 |  | 
|  | 2081 | default: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2082 | if (!audio_is_linear_pcm(format)) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2083 | ALOGE("createTrack_l() Bad parameter: format %#x \"" | 
|  | 2084 | "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2085 | format, mOutput, mFormat); | 
|  | 2086 | lStatus = BAD_VALUE; | 
|  | 2087 | goto Exit; | 
|  | 2088 | } | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2089 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2090 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
|  | 2091 | lStatus = BAD_VALUE; | 
|  | 2092 | goto Exit; | 
|  | 2093 | } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2094 | break; | 
|  | 2095 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2096 | } | 
|  | 2097 |  | 
|  | 2098 | lStatus = initCheck(); | 
|  | 2099 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2100 | ALOGE("createTrack_l() audio driver not initialized"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2101 | goto Exit; | 
|  | 2102 | } | 
|  | 2103 |  | 
|  | 2104 | { // scope for mLock | 
|  | 2105 | Mutex::Autolock _l(mLock); | 
|  | 2106 |  | 
|  | 2107 | // all tracks in same audio session must share the same routing strategy otherwise | 
|  | 2108 | // conflicts will happen when tracks are moved from one output to another by audio policy | 
|  | 2109 | // manager | 
|  | 2110 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); | 
|  | 2111 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2112 | sp<Track> t = mTracks[i]; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2113 | if (t != 0 && t->isExternalTrack()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2114 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); | 
|  | 2115 | if (sessionId == t->sessionId() && strategy != actual) { | 
|  | 2116 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
|  | 2117 | strategy, actual); | 
|  | 2118 | lStatus = BAD_VALUE; | 
|  | 2119 | goto Exit; | 
|  | 2120 | } | 
|  | 2121 | } | 
|  | 2122 | } | 
|  | 2123 |  | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2124 | track = new Track(this, client, streamType, sampleRate, format, | 
| Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2125 | channelMask, frameCount, | 
|  | 2126 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2127 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT, portId); | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2128 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2129 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; | 
|  | 2130 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2131 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2132 | // 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] | 2133 | goto Exit; | 
|  | 2134 | } | 
|  | 2135 | mTracks.add(track); | 
|  | 2136 |  | 
|  | 2137 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 2138 | if (chain != 0) { | 
|  | 2139 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
|  | 2140 | track->setMainBuffer(chain->inBuffer()); | 
|  | 2141 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); | 
|  | 2142 | chain->incTrackCnt(); | 
|  | 2143 | } | 
|  | 2144 |  | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2145 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2146 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
|  | 2147 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
|  | 2148 | // so ask activity manager to do this on our behalf | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2149 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2150 | } | 
|  | 2151 | } | 
|  | 2152 |  | 
|  | 2153 | lStatus = NO_ERROR; | 
|  | 2154 |  | 
|  | 2155 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2156 | *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2157 | return track; | 
|  | 2158 | } | 
|  | 2159 |  | 
|  | 2160 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const | 
|  | 2161 | { | 
|  | 2162 | return latency; | 
|  | 2163 | } | 
|  | 2164 |  | 
|  | 2165 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
|  | 2166 | { | 
|  | 2167 | Mutex::Autolock _l(mLock); | 
|  | 2168 | return latency_l(); | 
|  | 2169 | } | 
|  | 2170 | uint32_t AudioFlinger::PlaybackThread::latency_l() const | 
|  | 2171 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2172 | uint32_t latency; | 
|  | 2173 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { | 
|  | 2174 | return correctLatency_l(latency); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2175 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2176 | return 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2177 | } | 
|  | 2178 |  | 
|  | 2179 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
|  | 2180 | { | 
|  | 2181 | Mutex::Autolock _l(mLock); | 
|  | 2182 | // Don't apply master volume in SW if our HAL can do it for us. | 
|  | 2183 | if (mOutput && mOutput->audioHwDev && | 
|  | 2184 | mOutput->audioHwDev->canSetMasterVolume()) { | 
|  | 2185 | mMasterVolume = 1.0; | 
|  | 2186 | } else { | 
|  | 2187 | mMasterVolume = value; | 
|  | 2188 | } | 
|  | 2189 | } | 
|  | 2190 |  | 
|  | 2191 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
|  | 2192 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2193 | if (isDuplicating()) { | 
|  | 2194 | return; | 
|  | 2195 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2196 | Mutex::Autolock _l(mLock); | 
|  | 2197 | // Don't apply master mute in SW if our HAL can do it for us. | 
|  | 2198 | if (mOutput && mOutput->audioHwDev && | 
|  | 2199 | mOutput->audioHwDev->canSetMasterMute()) { | 
|  | 2200 | mMasterMute = false; | 
|  | 2201 | } else { | 
|  | 2202 | mMasterMute = muted; | 
|  | 2203 | } | 
|  | 2204 | } | 
|  | 2205 |  | 
|  | 2206 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
|  | 2207 | { | 
|  | 2208 | Mutex::Autolock _l(mLock); | 
|  | 2209 | mStreamTypes[stream].volume = value; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2210 | broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2211 | } | 
|  | 2212 |  | 
|  | 2213 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
|  | 2214 | { | 
|  | 2215 | Mutex::Autolock _l(mLock); | 
|  | 2216 | mStreamTypes[stream].mute = muted; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2217 | broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2218 | } | 
|  | 2219 |  | 
|  | 2220 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
|  | 2221 | { | 
|  | 2222 | Mutex::Autolock _l(mLock); | 
|  | 2223 | return mStreamTypes[stream].volume; | 
|  | 2224 | } | 
|  | 2225 |  | 
|  | 2226 | // addTrack_l() must be called with ThreadBase::mLock held | 
|  | 2227 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
|  | 2228 | { | 
|  | 2229 | status_t status = ALREADY_EXISTS; | 
|  | 2230 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2231 | if (mActiveTracks.indexOf(track) < 0) { | 
|  | 2232 | // the track is newly added, make sure it fills up all its | 
|  | 2233 | // buffers before playing. This is to ensure the client will | 
|  | 2234 | // effectively get the latency it requested. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2235 | if (track->isExternalTrack()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2236 | TrackBase::track_state state = track->mState; | 
|  | 2237 | mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2238 | status = AudioSystem::startOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2239 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2240 | mLock.lock(); | 
|  | 2241 | // abort track was stopped/paused while we released the lock | 
|  | 2242 | if (state != track->mState) { | 
|  | 2243 | if (status == NO_ERROR) { | 
|  | 2244 | mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2245 | AudioSystem::stopOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2246 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2247 | mLock.lock(); | 
|  | 2248 | } | 
|  | 2249 | return INVALID_OPERATION; | 
|  | 2250 | } | 
|  | 2251 | // abort if start is rejected by audio policy manager | 
|  | 2252 | if (status != NO_ERROR) { | 
|  | 2253 | return PERMISSION_DENIED; | 
|  | 2254 | } | 
|  | 2255 | #ifdef ADD_BATTERY_DATA | 
|  | 2256 | // to track the speaker usage | 
|  | 2257 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
|  | 2258 | #endif | 
|  | 2259 | } | 
|  | 2260 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2261 | // set retry count for buffer fill | 
|  | 2262 | if (track->isOffloaded()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2263 | if (track->isStopping_1()) { | 
|  | 2264 | track->mRetryCount = kMaxTrackStopRetriesOffload; | 
|  | 2265 | } else { | 
|  | 2266 | track->mRetryCount = kMaxTrackStartupRetriesOffload; | 
|  | 2267 | } | 
|  | 2268 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2269 | } else { | 
|  | 2270 | track->mRetryCount = kMaxTrackStartupRetries; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2271 | track->mFillingUpStatus = | 
|  | 2272 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2273 | } | 
|  | 2274 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2275 | track->mResetDone = false; | 
|  | 2276 | track->mPresentationCompleteFrames = 0; | 
|  | 2277 | mActiveTracks.add(track); | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2278 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 2279 | if (chain != 0) { | 
|  | 2280 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
|  | 2281 | track->sessionId()); | 
|  | 2282 | chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2283 | } | 
|  | 2284 |  | 
|  | 2285 | status = NO_ERROR; | 
|  | 2286 | } | 
|  | 2287 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2288 | onAddNewTrack_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2289 | return status; | 
|  | 2290 | } | 
|  | 2291 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2292 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2293 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2294 | track->terminate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2295 | // active tracks are removed by threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2296 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); | 
|  | 2297 | track->mState = TrackBase::STOPPED; | 
|  | 2298 | if (!trackActive) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2299 | removeTrack_l(track); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2300 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2301 | track->mState = TrackBase::STOPPING_1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2302 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2303 |  | 
|  | 2304 | return trackActive; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2305 | } | 
|  | 2306 |  | 
|  | 2307 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
|  | 2308 | { | 
|  | 2309 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2310 |  | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2311 | String8 result; | 
|  | 2312 | track->appendDump(result, false /* active */); | 
|  | 2313 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2314 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2315 | mTracks.remove(track); | 
|  | 2316 | deleteTrackName_l(track->name()); | 
|  | 2317 | // redundant as track is about to be destroyed, for dumpsys only | 
|  | 2318 | track->mName = -1; | 
|  | 2319 | if (track->isFastTrack()) { | 
|  | 2320 | int index = track->mFastIndex; | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2321 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2322 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
|  | 2323 | mFastTrackAvailMask |= 1 << index; | 
|  | 2324 | // redundant as track is about to be destroyed, for dumpsys only | 
|  | 2325 | track->mFastIndex = -1; | 
|  | 2326 | } | 
|  | 2327 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 2328 | if (chain != 0) { | 
|  | 2329 | chain->decTrackCnt(); | 
|  | 2330 | } | 
|  | 2331 | } | 
|  | 2332 |  | 
|  | 2333 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
|  | 2334 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2335 | Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2336 | String8 out_s8; | 
|  | 2337 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { | 
|  | 2338 | return out_s8; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2339 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2340 | return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2341 | } | 
|  | 2342 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2343 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2344 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
|  | 2345 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2346 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2347 | desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2348 |  | 
|  | 2349 | switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2350 | case AUDIO_OUTPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2351 | case AUDIO_OUTPUT_REGISTERED: | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2352 | case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2353 | desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2354 | desc->mChannelMask = mChannelMask; | 
|  | 2355 | desc->mSamplingRate = mSampleRate; | 
|  | 2356 | desc->mFormat = mFormat; | 
|  | 2357 | desc->mFrameCount = mNormalFrameCount; // FIXME see | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2358 | // AudioFlinger::frameCount(audio_io_handle_t) | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2359 | desc->mFrameCountHAL = mFrameCount; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2360 | desc->mLatency = latency_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2361 | break; | 
|  | 2362 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2363 | case AUDIO_OUTPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2364 | default: | 
|  | 2365 | break; | 
|  | 2366 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2367 | mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2368 | } | 
|  | 2369 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2370 | void AudioFlinger::PlaybackThread::onWriteReady() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2371 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2372 | mCallbackThread->resetWriteBlocked(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2373 | } | 
|  | 2374 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2375 | void AudioFlinger::PlaybackThread::onDrainReady() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2376 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2377 | mCallbackThread->resetDraining(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2378 | } | 
|  | 2379 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2380 | void AudioFlinger::PlaybackThread::onError() | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2381 | { | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2382 | mCallbackThread->setAsyncError(); | 
|  | 2383 | } | 
|  | 2384 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2385 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2386 | { | 
|  | 2387 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2388 | // reject out of sequence requests | 
|  | 2389 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { | 
|  | 2390 | mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2391 | mWaitWorkCV.signal(); | 
|  | 2392 | } | 
|  | 2393 | } | 
|  | 2394 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2395 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2396 | { | 
|  | 2397 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2398 | // reject out of sequence requests | 
|  | 2399 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { | 
|  | 2400 | mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2401 | mWaitWorkCV.signal(); | 
|  | 2402 | } | 
|  | 2403 | } | 
|  | 2404 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2405 | void AudioFlinger::PlaybackThread::readOutputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2406 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2407 | // 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] | 2408 | mSampleRate = mOutput->getSampleRate(); | 
|  | 2409 | mChannelMask = mOutput->getChannelMask(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2410 | if (!audio_is_output_channel(mChannelMask)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2411 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2412 | } | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2413 | if ((mType == MIXER || mType == DUPLICATING) | 
|  | 2414 | && !isValidPcmSinkChannelMask(mChannelMask)) { | 
|  | 2415 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", | 
|  | 2416 | mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2417 | } | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2418 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2419 |  | 
|  | 2420 | // Get actual HAL format. | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2421 | status_t result = mOutput->stream->getFormat(&mHALFormat); | 
|  | 2422 | LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2423 | // Get format from the shim, which will be different than the HAL format | 
|  | 2424 | // if playing compressed audio over HDMI passthrough. | 
|  | 2425 | mFormat = mOutput->getFormat(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2426 | if (!audio_is_valid_format(mFormat)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2427 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2428 | } | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2429 | if ((mType == MIXER || mType == DUPLICATING) | 
|  | 2430 | && !isValidPcmSinkFormat(mFormat)) { | 
|  | 2431 | LOG_FATAL("HAL format %#x not supported for mixed output", | 
|  | 2432 | mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2433 | } | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2434 | mFrameSize = mOutput->getFrameSize(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2435 | result = mOutput->stream->getBufferSize(&mBufferSize); | 
|  | 2436 | LOG_ALWAYS_FATAL_IF(result != OK, | 
|  | 2437 | "Error when retrieving output stream buffer size: %d", result); | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2438 | mFrameCount = mBufferSize / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2439 | if (mFrameCount & 15) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2440 | 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] | 2441 | mFrameCount); | 
|  | 2442 | } | 
|  | 2443 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2444 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) { | 
|  | 2445 | if (mOutput->stream->setCallback(this) == OK) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2446 | mUseAsyncWrite = true; | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2447 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2448 | } | 
|  | 2449 | } | 
|  | 2450 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2451 | mHwSupportsPause = false; | 
|  | 2452 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2453 | bool supportsPause = false, supportsResume = false; | 
|  | 2454 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { | 
|  | 2455 | if (supportsPause && supportsResume) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2456 | mHwSupportsPause = true; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2457 | } else if (supportsPause) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2458 | ALOGW("direct output implements pause but not resume"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2459 | } else if (supportsResume) { | 
|  | 2460 | ALOGW("direct output implements resume but not pause"); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2461 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2462 | } | 
|  | 2463 | } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2464 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { | 
|  | 2465 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); | 
|  | 2466 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2467 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2468 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { | 
|  | 2469 | // For best precision, we use float instead of the associated output | 
|  | 2470 | // device format (typically PCM 16 bit). | 
|  | 2471 |  | 
|  | 2472 | mFormat = AUDIO_FORMAT_PCM_FLOAT; | 
|  | 2473 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
|  | 2474 | mBufferSize = mFrameSize * mFrameCount; | 
|  | 2475 |  | 
|  | 2476 | // TODO: We currently use the associated output device channel mask and sample rate. | 
|  | 2477 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads | 
|  | 2478 | // (if a valid mask) to avoid premature downmix. | 
|  | 2479 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads | 
|  | 2480 | // instead of the output device sample rate to avoid loss of high frequency information. | 
|  | 2481 | // This may need to be updated as MixerThread/OutputTracks are added and not here. | 
|  | 2482 | } | 
|  | 2483 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2484 | // 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] | 2485 | double multiplier = 1.0; | 
|  | 2486 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
|  | 2487 | kUseFastMixer == FastMixer_Dynamic)) { | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2488 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
|  | 2489 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
| Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2490 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2491 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
|  | 2492 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
|  | 2493 | maxNormalFrameCount = maxNormalFrameCount & ~15; | 
|  | 2494 | if (maxNormalFrameCount < minNormalFrameCount) { | 
|  | 2495 | maxNormalFrameCount = minNormalFrameCount; | 
|  | 2496 | } | 
|  | 2497 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
|  | 2498 | if (multiplier <= 1.0) { | 
|  | 2499 | multiplier = 1.0; | 
|  | 2500 | } else if (multiplier <= 2.0) { | 
|  | 2501 | if (2 * mFrameCount <= maxNormalFrameCount) { | 
|  | 2502 | multiplier = 2.0; | 
|  | 2503 | } else { | 
|  | 2504 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
|  | 2505 | } | 
|  | 2506 | } else { | 
| Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2507 | multiplier = floor(multiplier); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2508 | } | 
|  | 2509 | } | 
|  | 2510 | mNormalFrameCount = multiplier * mFrameCount; | 
|  | 2511 | // round up to nearest 16 frames to satisfy AudioMixer | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2512 | if (mType == MIXER || mType == DUPLICATING) { | 
|  | 2513 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
|  | 2514 | } | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2515 | 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] | 2516 | mNormalFrameCount); | 
|  | 2517 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2518 | // Check if we want to throttle the processing to no more than 2x normal rate | 
|  | 2519 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2520 | mThreadThrottleTimeMs = 0; | 
|  | 2521 | mThreadThrottleEndMs = 0; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2522 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); | 
|  | 2523 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2524 | // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames. | 
|  | 2525 | // Originally this was int16_t[] array, need to remove legacy implications. | 
|  | 2526 | free(mSinkBuffer); | 
|  | 2527 | mSinkBuffer = NULL; | 
| Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2528 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems | 
|  | 2529 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. | 
|  | 2530 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2531 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2532 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2533 | // We resize the mMixerBuffer according to the requirements of the sink buffer which | 
|  | 2534 | // drives the output. | 
|  | 2535 | free(mMixerBuffer); | 
|  | 2536 | mMixerBuffer = NULL; | 
|  | 2537 | if (mMixerBufferEnabled) { | 
|  | 2538 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. | 
|  | 2539 | mMixerBufferSize = mNormalFrameCount * mChannelCount | 
|  | 2540 | * audio_bytes_per_sample(mMixerBufferFormat); | 
|  | 2541 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); | 
|  | 2542 | } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2543 | free(mEffectBuffer); | 
|  | 2544 | mEffectBuffer = NULL; | 
|  | 2545 | if (mEffectBufferEnabled) { | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2546 | mEffectBufferFormat = EFFECT_BUFFER_FORMAT; | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2547 | mEffectBufferSize = mNormalFrameCount * mChannelCount | 
|  | 2548 | * audio_bytes_per_sample(mEffectBufferFormat); | 
|  | 2549 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); | 
|  | 2550 | } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2551 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2552 | // force reconfiguration of effect chains and engines to take new buffer size and audio | 
|  | 2553 | // parameters into account | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2554 | // 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] | 2555 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
|  | 2556 | // matter. | 
|  | 2557 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
|  | 2558 | Vector< sp<EffectChain> > effectChains = mEffectChains; | 
|  | 2559 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 2560 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
|  | 2561 | } | 
|  | 2562 | } | 
|  | 2563 |  | 
|  | 2564 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2565 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2566 | { | 
|  | 2567 | if (halFrames == NULL || dspFrames == NULL) { | 
|  | 2568 | return BAD_VALUE; | 
|  | 2569 | } | 
|  | 2570 | Mutex::Autolock _l(mLock); | 
|  | 2571 | if (initCheck() != NO_ERROR) { | 
|  | 2572 | return INVALID_OPERATION; | 
|  | 2573 | } | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2574 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2575 | *halFrames = framesWritten; | 
|  | 2576 |  | 
|  | 2577 | if (isSuspended()) { | 
|  | 2578 | // return an estimation of rendered frames when the output is suspended | 
|  | 2579 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2580 | *dspFrames = (uint32_t) | 
|  | 2581 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2582 | return NO_ERROR; | 
|  | 2583 | } else { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2584 | status_t status; | 
|  | 2585 | uint32_t frames; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2586 | status = mOutput->getRenderPosition(&frames); | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2587 | *dspFrames = (size_t)frames; | 
|  | 2588 | return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2589 | } | 
|  | 2590 | } | 
|  | 2591 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2592 | // hasAudioSession_l() must be called with ThreadBase::mLock held | 
|  | 2593 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2594 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2595 | uint32_t result = 0; | 
|  | 2596 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 2597 | result = EFFECT_SESSION; | 
|  | 2598 | } | 
|  | 2599 |  | 
|  | 2600 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2601 | sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2602 | if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2603 | result |= TRACK_SESSION; | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2604 | if (track->isFastTrack()) { | 
|  | 2605 | result |= FAST_SESSION; | 
|  | 2606 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2607 | break; | 
|  | 2608 | } | 
|  | 2609 | } | 
|  | 2610 |  | 
|  | 2611 | return result; | 
|  | 2612 | } | 
|  | 2613 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2614 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2615 | { | 
|  | 2616 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
|  | 2617 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
|  | 2618 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 2619 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
|  | 2620 | } | 
|  | 2621 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 2622 | sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2623 | if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2624 | return AudioSystem::getStrategyForStream(track->streamType()); | 
|  | 2625 | } | 
|  | 2626 | } | 
|  | 2627 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
|  | 2628 | } | 
|  | 2629 |  | 
|  | 2630 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2631 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2632 | { | 
|  | 2633 | Mutex::Autolock _l(mLock); | 
|  | 2634 | return mOutput; | 
|  | 2635 | } | 
|  | 2636 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2637 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2638 | { | 
|  | 2639 | Mutex::Autolock _l(mLock); | 
|  | 2640 | AudioStreamOut *output = mOutput; | 
|  | 2641 | mOutput = NULL; | 
|  | 2642 | // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
|  | 2643 | //       must push a NULL and wait for ack | 
|  | 2644 | mOutputSink.clear(); | 
|  | 2645 | mPipeSink.clear(); | 
|  | 2646 | mNormalSink.clear(); | 
|  | 2647 | return output; | 
|  | 2648 | } | 
|  | 2649 |  | 
|  | 2650 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2651 | sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2652 | { | 
|  | 2653 | if (mOutput == NULL) { | 
|  | 2654 | return NULL; | 
|  | 2655 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2656 | return mOutput->stream; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2657 | } | 
|  | 2658 |  | 
|  | 2659 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const | 
|  | 2660 | { | 
|  | 2661 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 2662 | } | 
|  | 2663 |  | 
|  | 2664 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
|  | 2665 | { | 
|  | 2666 | if (!isValidSyncEvent(event)) { | 
|  | 2667 | return BAD_VALUE; | 
|  | 2668 | } | 
|  | 2669 |  | 
|  | 2670 | Mutex::Autolock _l(mLock); | 
|  | 2671 |  | 
|  | 2672 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2673 | sp<Track> track = mTracks[i]; | 
|  | 2674 | if (event->triggerSession() == track->sessionId()) { | 
|  | 2675 | (void) track->setSyncEvent(event); | 
|  | 2676 | return NO_ERROR; | 
|  | 2677 | } | 
|  | 2678 | } | 
|  | 2679 |  | 
|  | 2680 | return NAME_NOT_FOUND; | 
|  | 2681 | } | 
|  | 2682 |  | 
|  | 2683 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
|  | 2684 | { | 
|  | 2685 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
|  | 2686 | } | 
|  | 2687 |  | 
|  | 2688 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( | 
|  | 2689 | const Vector< sp<Track> >& tracksToRemove) | 
|  | 2690 | { | 
|  | 2691 | size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2692 | if (count > 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2693 | for (size_t i = 0 ; i < count ; i++) { | 
|  | 2694 | const sp<Track>& track = tracksToRemove.itemAt(i); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2695 | if (track->isExternalTrack()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2696 | AudioSystem::stopOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2697 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2698 | #ifdef ADD_BATTERY_DATA | 
|  | 2699 | // to track the speaker usage | 
|  | 2700 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
|  | 2701 | #endif | 
|  | 2702 | if (track->isTerminated()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2703 | AudioSystem::releaseOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2704 | track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2705 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2706 | } | 
|  | 2707 | } | 
|  | 2708 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2709 | } | 
|  | 2710 |  | 
|  | 2711 | void AudioFlinger::PlaybackThread::checkSilentMode_l() | 
|  | 2712 | { | 
|  | 2713 | if (!mMasterMute) { | 
|  | 2714 | char value[PROPERTY_VALUE_MAX]; | 
| Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2715 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { | 
|  | 2716 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); | 
|  | 2717 | return; | 
|  | 2718 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2719 | if (property_get("ro.audio.silent", value, "0") > 0) { | 
|  | 2720 | char *endptr; | 
|  | 2721 | unsigned long ul = strtoul(value, &endptr, 0); | 
|  | 2722 | if (*endptr == '\0' && ul != 0) { | 
|  | 2723 | ALOGD("Silence is golden"); | 
|  | 2724 | // The setprop command will not allow a property to be changed after | 
|  | 2725 | // the first time it is set, so we don't have to worry about un-muting. | 
|  | 2726 | setMasterMute_l(true); | 
|  | 2727 | } | 
|  | 2728 | } | 
|  | 2729 | } | 
|  | 2730 | } | 
|  | 2731 |  | 
|  | 2732 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2733 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2734 | { | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 2735 | LOG_HIST_TS(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2736 | mInWrite = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2737 | ssize_t bytesWritten; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2738 | const size_t offset = mCurrentWriteLength - mBytesRemaining; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2739 |  | 
|  | 2740 | // If an NBAIO sink is present, use it to write the normal mixer's submix | 
|  | 2741 | if (mNormalSink != 0) { | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2742 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2743 | const size_t count = mBytesRemaining / mFrameSize; | 
|  | 2744 |  | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2745 | ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2746 | // update the setpoint when AudioFlinger::mScreenState changes | 
|  | 2747 | uint32_t screenState = AudioFlinger::mScreenState; | 
|  | 2748 | if (screenState != mScreenState) { | 
|  | 2749 | mScreenState = screenState; | 
|  | 2750 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
|  | 2751 | if (pipe != NULL) { | 
|  | 2752 | pipe->setAvgFrames((mScreenState & 1) ? | 
|  | 2753 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
|  | 2754 | } | 
|  | 2755 | } | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2756 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2757 | ATRACE_END(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2758 | if (framesWritten > 0) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2759 | bytesWritten = framesWritten * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2760 | } else { | 
|  | 2761 | bytesWritten = framesWritten; | 
|  | 2762 | } | 
|  | 2763 | // otherwise use the HAL / AudioStreamOut directly | 
|  | 2764 | } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2765 | // Direct output and offload threads | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2766 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2767 | if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2768 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); | 
|  | 2769 | mWriteAckSequence += 2; | 
|  | 2770 | mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2771 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2772 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2773 | } | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2774 | // FIXME We should have an implementation of timestamps for direct output threads. | 
|  | 2775 | // They are used e.g for multichannel PCM playback over HDMI. | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2776 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2777 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2778 | if (mUseAsyncWrite && | 
|  | 2779 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { | 
|  | 2780 | // 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] | 2781 | mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2782 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2783 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2784 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2785 | } | 
|  | 2786 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2787 | mNumWrites++; | 
|  | 2788 | mInWrite = false; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2789 | mStandby = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2790 | return bytesWritten; | 
|  | 2791 | } | 
|  | 2792 |  | 
|  | 2793 | void AudioFlinger::PlaybackThread::threadLoop_drain() | 
|  | 2794 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2795 | bool supportsDrain = false; | 
|  | 2796 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2797 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); | 
|  | 2798 | if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2799 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); | 
|  | 2800 | mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2801 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2802 | mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2803 | } | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 2804 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2805 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2806 | } | 
|  | 2807 | } | 
|  | 2808 |  | 
|  | 2809 | void AudioFlinger::PlaybackThread::threadLoop_exit() | 
|  | 2810 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2811 | { | 
|  | 2812 | Mutex::Autolock _l(mLock); | 
|  | 2813 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 2814 | sp<Track> track = mTracks[i]; | 
|  | 2815 | track->invalidate(); | 
|  | 2816 | } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2817 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. | 
|  | 2818 | // After we exit there are no more track changes sent to BatteryNotifier | 
|  | 2819 | // because that requires an active threadLoop. | 
|  | 2820 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? | 
|  | 2821 | mActiveTracks.clear(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2822 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2823 | } | 
|  | 2824 |  | 
|  | 2825 | /* | 
|  | 2826 | The derived values that are cached: | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2827 | - mSinkBufferSize from frame count * frame size | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2828 | - mActiveSleepTimeUs from activeSleepTimeUs() | 
|  | 2829 | - mIdleSleepTimeUs from idleSleepTimeUs() | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2830 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least | 
|  | 2831 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2832 | - maxPeriod from frame count and sample rate (MIXER only) | 
|  | 2833 |  | 
|  | 2834 | The parameters that affect these derived values are: | 
|  | 2835 | - frame count | 
|  | 2836 | - frame size | 
|  | 2837 | - sample rate | 
|  | 2838 | - device type: A2DP or not | 
|  | 2839 | - device latency | 
|  | 2840 | - format: PCM or not | 
|  | 2841 | - active sleep time | 
|  | 2842 | - idle sleep time | 
|  | 2843 | */ | 
|  | 2844 |  | 
|  | 2845 | void AudioFlinger::PlaybackThread::cacheParameters_l() | 
|  | 2846 | { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2847 | mSinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2848 | mActiveSleepTimeUs = activeSleepTimeUs(); | 
|  | 2849 | mIdleSleepTimeUs = idleSleepTimeUs(); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2850 |  | 
|  | 2851 | // make sure standby delay is not too short when connected to an A2DP sink to avoid | 
|  | 2852 | // truncating audio when going to standby. | 
|  | 2853 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; | 
|  | 2854 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { | 
|  | 2855 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { | 
|  | 2856 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; | 
|  | 2857 | } | 
|  | 2858 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2859 | } | 
|  | 2860 |  | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2861 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2862 | { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2863 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2864 | this,  streamType, mTracks.size()); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2865 | bool trackMatch = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2866 | size_t size = mTracks.size(); | 
|  | 2867 | for (size_t i = 0; i < size; i++) { | 
|  | 2868 | sp<Track> t = mTracks[i]; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2869 | if (t->streamType() == streamType && t->isExternalTrack()) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2870 | t->invalidate(); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2871 | trackMatch = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2872 | } | 
|  | 2873 | } | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2874 | return trackMatch; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2875 | } | 
|  | 2876 |  | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2877 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
|  | 2878 | { | 
|  | 2879 | Mutex::Autolock _l(mLock); | 
|  | 2880 | invalidateTracks_l(streamType); | 
|  | 2881 | } | 
|  | 2882 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2883 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 2884 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2885 | audio_session_t session = chain->sessionId(); | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2886 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; | 
| Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 2887 | status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer( | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2888 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, | 
|  | 2889 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, | 
|  | 2890 | &halInBuffer); | 
|  | 2891 | if (result != OK) return result; | 
|  | 2892 | halOutBuffer = halInBuffer; | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2893 | effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2894 | 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] | 2895 | if (session > AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2896 | // 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] | 2897 | // the sink buffer as input | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2898 | if (mType != DIRECT) { | 
|  | 2899 | size_t numSamples = mNormalFrameCount * mChannelCount; | 
| Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 2900 | status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer( | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2901 | numSamples * sizeof(effect_buffer_t), | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2902 | &halInBuffer); | 
|  | 2903 | if (result != OK) return result; | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2904 | #ifdef FLOAT_EFFECT_CHAIN | 
|  | 2905 | buffer = halInBuffer->audioBuffer()->f32; | 
|  | 2906 | #else | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2907 | buffer = halInBuffer->audioBuffer()->s16; | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2908 | #endif | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2909 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", | 
|  | 2910 | buffer, session); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2911 | } | 
|  | 2912 |  | 
|  | 2913 | // Attach all tracks with same session ID to this chain. | 
|  | 2914 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2915 | sp<Track> track = mTracks[i]; | 
|  | 2916 | if (session == track->sessionId()) { | 
|  | 2917 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), | 
|  | 2918 | buffer); | 
|  | 2919 | track->setMainBuffer(buffer); | 
|  | 2920 | chain->incTrackCnt(); | 
|  | 2921 | } | 
|  | 2922 | } | 
|  | 2923 |  | 
|  | 2924 | // indicate all active tracks in the chain | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2925 | for (const sp<Track> &track : mActiveTracks) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2926 | if (session == track->sessionId()) { | 
|  | 2927 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
|  | 2928 | chain->incActiveTrackCnt(); | 
|  | 2929 | } | 
|  | 2930 | } | 
|  | 2931 | } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2932 | chain->setThread(this); | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2933 | chain->setInBuffer(halInBuffer); | 
|  | 2934 | chain->setOutBuffer(halOutBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2935 | // 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] | 2936 | // 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] | 2937 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
|  | 2938 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2939 | // after track specific effects and before output stage. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2940 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2941 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2942 | // Effect chain for other sessions are inserted at beginning of effect | 
|  | 2943 | // 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] | 2944 | // sessions is not important. | 
|  | 2945 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && | 
|  | 2946 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, | 
|  | 2947 | "audio_session_t constants misdefined"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2948 | size_t size = mEffectChains.size(); | 
|  | 2949 | size_t i = 0; | 
|  | 2950 | for (i = 0; i < size; i++) { | 
|  | 2951 | if (mEffectChains[i]->sessionId() < session) { | 
|  | 2952 | break; | 
|  | 2953 | } | 
|  | 2954 | } | 
|  | 2955 | mEffectChains.insertAt(chain, i); | 
|  | 2956 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 2957 |  | 
|  | 2958 | return NO_ERROR; | 
|  | 2959 | } | 
|  | 2960 |  | 
|  | 2961 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 2962 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2963 | audio_session_t session = chain->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2964 |  | 
|  | 2965 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
|  | 2966 |  | 
|  | 2967 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 2968 | if (chain == mEffectChains[i]) { | 
|  | 2969 | mEffectChains.removeAt(i); | 
|  | 2970 | // detach all active tracks from the chain | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2971 | for (const sp<Track> &track : mActiveTracks) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2972 | if (session == track->sessionId()) { | 
|  | 2973 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
|  | 2974 | chain.get(), session); | 
|  | 2975 | chain->decActiveTrackCnt(); | 
|  | 2976 | } | 
|  | 2977 | } | 
|  | 2978 |  | 
|  | 2979 | // detach all tracks with same session ID from this chain | 
|  | 2980 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 2981 | sp<Track> track = mTracks[i]; | 
|  | 2982 | if (session == track->sessionId()) { | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2983 | track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2984 | chain->decTrackCnt(); | 
|  | 2985 | } | 
|  | 2986 | } | 
|  | 2987 | break; | 
|  | 2988 | } | 
|  | 2989 | } | 
|  | 2990 | return mEffectChains.size(); | 
|  | 2991 | } | 
|  | 2992 |  | 
|  | 2993 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2994 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2995 | { | 
|  | 2996 | Mutex::Autolock _l(mLock); | 
|  | 2997 | return attachAuxEffect_l(track, EffectId); | 
|  | 2998 | } | 
|  | 2999 |  | 
|  | 3000 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3001 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3002 | { | 
|  | 3003 | status_t status = NO_ERROR; | 
|  | 3004 |  | 
|  | 3005 | if (EffectId == 0) { | 
|  | 3006 | track->setAuxBuffer(0, NULL); | 
|  | 3007 | } else { | 
|  | 3008 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
|  | 3009 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
|  | 3010 | if (effect != 0) { | 
|  | 3011 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 3012 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
|  | 3013 | } else { | 
|  | 3014 | status = INVALID_OPERATION; | 
|  | 3015 | } | 
|  | 3016 | } else { | 
|  | 3017 | status = BAD_VALUE; | 
|  | 3018 | } | 
|  | 3019 | } | 
|  | 3020 | return status; | 
|  | 3021 | } | 
|  | 3022 |  | 
|  | 3023 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
|  | 3024 | { | 
|  | 3025 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 3026 | sp<Track> track = mTracks[i]; | 
|  | 3027 | if (track->auxEffectId() == effectId) { | 
|  | 3028 | attachAuxEffect_l(track, 0); | 
|  | 3029 | } | 
|  | 3030 | } | 
|  | 3031 | } | 
|  | 3032 |  | 
|  | 3033 | bool AudioFlinger::PlaybackThread::threadLoop() | 
|  | 3034 | { | 
| Glenn Kasten | 388d571 | 2017-04-07 14:38:41 -0700 | [diff] [blame] | 3035 | tlNBLogWriter = mNBLogWriter.get(); | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3036 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3037 | Vector< sp<Track> > tracksToRemove; | 
|  | 3038 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3039 | mStandbyTimeNs = systemTime(); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3040 | nsecs_t lastWriteFinished = -1; // time last server write completed | 
|  | 3041 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3042 |  | 
|  | 3043 | // MIXER | 
|  | 3044 | nsecs_t lastWarning = 0; | 
|  | 3045 |  | 
|  | 3046 | // DUPLICATING | 
|  | 3047 | // FIXME could this be made local to while loop? | 
|  | 3048 | writeFrames = 0; | 
|  | 3049 |  | 
|  | 3050 | cacheParameters_l(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3051 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3052 |  | 
|  | 3053 | if (mType == MIXER) { | 
|  | 3054 | sleepTimeShift = 0; | 
|  | 3055 | } | 
|  | 3056 |  | 
|  | 3057 | CpuStats cpuStats; | 
|  | 3058 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
|  | 3059 |  | 
|  | 3060 | acquireWakeLock(); | 
|  | 3061 |  | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3062 | // mNBLogWriter logging APIs can only be called by a single thread, typically the | 
|  | 3063 | // thread associated with this PlaybackThread. | 
|  | 3064 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) | 
|  | 3065 | // then all such threads must agree to hold a common mutex before logging. | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3066 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
|  | 3067 | // and then that string will be logged at the next convenient opportunity. | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3068 | // See reference to logString below. | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3069 | const char *logString = NULL; | 
|  | 3070 |  | 
| rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3071 | // Estimated time for next buffer to be written to hal. This is used only on | 
|  | 3072 | // suspended mode (for now) to help schedule the wait time until next iteration. | 
|  | 3073 | nsecs_t timeLoopNextNs = 0; | 
|  | 3074 |  | 
| Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 3075 | checkSilentMode_l(); | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3076 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3077 | while (!exitPending()) | 
|  | 3078 | { | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 3079 | // Log merge requests are performed during AudioFlinger binder transactions, but | 
|  | 3080 | // that does not cover audio playback. It's requested here for that reason. | 
|  | 3081 | mAudioFlinger->requestLogMerge(); | 
|  | 3082 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3083 | cpuStats.sample(myName); | 
|  | 3084 |  | 
|  | 3085 | Vector< sp<EffectChain> > effectChains; | 
|  | 3086 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3087 | { // scope for mLock | 
|  | 3088 |  | 
|  | 3089 | Mutex::Autolock _l(mLock); | 
|  | 3090 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3091 | processConfigEvents_l(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3092 |  | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3093 | // See comment at declaration of logString for why this is done under mLock | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3094 | if (logString != NULL) { | 
|  | 3095 | mNBLogWriter->logTimestamp(); | 
|  | 3096 | mNBLogWriter->log(logString); | 
|  | 3097 | logString = NULL; | 
|  | 3098 | } | 
|  | 3099 |  | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3100 | // Gather the framesReleased counters for all active tracks, | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3101 | // and associate with the sink frames written out.  We need | 
|  | 3102 | // this to convert the sink timestamp to the track timestamp. | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3103 | bool kernelLocationUpdate = false; | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3104 | if (mNormalSink != 0) { | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3105 | // Note: The DuplicatingThread may not have a mNormalSink. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3106 | // We always fetch the timestamp here because often the downstream | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3107 | // sink will block while writing. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3108 | ExtendedTimestamp timestamp; // use private copy to fetch | 
|  | 3109 | (void) mNormalSink->getTimestamp(timestamp); | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3110 |  | 
|  | 3111 | // We keep track of the last valid kernel position in case we are in underrun | 
|  | 3112 | // and the normal mixer period is the same as the fast mixer period, or there | 
|  | 3113 | // is some error from the HAL. | 
|  | 3114 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
|  | 3115 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
|  | 3116 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; | 
|  | 3117 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
|  | 3118 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
|  | 3119 |  | 
|  | 3120 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
|  | 3121 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; | 
|  | 3122 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
|  | 3123 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3124 | } | 
|  | 3125 |  | 
|  | 3126 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
|  | 3127 | kernelLocationUpdate = true; | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3128 | } else { | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3129 | ALOGVV("getTimestamp error - no valid kernel position"); | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3130 | } | 
|  | 3131 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3132 | // copy over kernel info | 
|  | 3133 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3134 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
|  | 3135 | + mSuspendedFrames; // add frames discarded when suspended | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3136 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = | 
|  | 3137 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3138 | } | 
|  | 3139 | // mFramesWritten for non-offloaded tracks are contiguous | 
|  | 3140 | // even after standby() is called. This is useful for the track frame | 
|  | 3141 | // to sink frame mapping. | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3142 | bool serverLocationUpdate = false; | 
|  | 3143 | if (mFramesWritten != lastFramesWritten) { | 
|  | 3144 | serverLocationUpdate = true; | 
|  | 3145 | lastFramesWritten = mFramesWritten; | 
|  | 3146 | } | 
|  | 3147 | // Only update timestamps if there is a meaningful change. | 
|  | 3148 | // Either the kernel timestamp must be valid or we have written something. | 
|  | 3149 | if (kernelLocationUpdate || serverLocationUpdate) { | 
|  | 3150 | if (serverLocationUpdate) { | 
|  | 3151 | // use the time before we called the HAL write - it is a bit more accurate | 
|  | 3152 | // to when the server last read data than the current time here. | 
|  | 3153 | // | 
|  | 3154 | // If we haven't written anything, mLastWriteTime will be -1 | 
|  | 3155 | // and we use systemTime(). | 
|  | 3156 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; | 
|  | 3157 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 | 
|  | 3158 | ? systemTime() : mLastWriteTime; | 
|  | 3159 | } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3160 |  | 
|  | 3161 | for (const sp<Track> &t : mActiveTracks) { | 
|  | 3162 | if (!t->isFastTrack()) { | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3163 | t->updateTrackFrameInfo( | 
|  | 3164 | t->mAudioTrackServerProxy->framesReleased(), | 
|  | 3165 | mFramesWritten, | 
|  | 3166 | mTimestamp); | 
|  | 3167 | } | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3168 | } | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3169 | } | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3170 | #if 0 | 
|  | 3171 | // logFormat example | 
| Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 3172 | if (z % 100 == 0) { | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3173 | timespec ts; | 
|  | 3174 | clock_gettime(CLOCK_MONOTONIC, &ts); | 
| Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3175 | LOGT("This is an integer %d, this is a float %f, this is my " | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3176 | "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts); | 
| Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3177 | LOGT("A deceptive null-terminated string %\0"); | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3178 | } | 
|  | 3179 | ++z; | 
|  | 3180 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3181 | saveOutputTracks(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3182 | if (mSignalPending) { | 
|  | 3183 | // A signal was raised while we were unlocked | 
|  | 3184 | mSignalPending = false; | 
|  | 3185 | } else if (waitingAsyncCallback_l()) { | 
|  | 3186 | if (exitPending()) { | 
|  | 3187 | break; | 
|  | 3188 | } | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3189 | bool released = false; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3190 | if (!keepWakeLock()) { | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3191 | releaseWakeLock_l(); | 
|  | 3192 | released = true; | 
|  | 3193 | } | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 3194 |  | 
|  | 3195 | const int64_t waitNs = computeWaitTimeNs_l(); | 
|  | 3196 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); | 
|  | 3197 | status_t status = mWaitWorkCV.waitRelative(mLock, waitNs); | 
|  | 3198 | if (status == TIMED_OUT) { | 
|  | 3199 | mSignalPending = true; // if timeout recheck everything | 
|  | 3200 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3201 | ALOGV("async completion/wake"); | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3202 | if (released) { | 
|  | 3203 | acquireWakeLock_l(); | 
|  | 3204 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3205 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
|  | 3206 | mSleepTimeUs = 0; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3207 |  | 
|  | 3208 | continue; | 
|  | 3209 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3210 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3211 | isSuspended()) { | 
|  | 3212 | // put audio hardware into standby after short delay | 
|  | 3213 | if (shouldStandby_l()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3214 |  | 
|  | 3215 | threadLoop_standby(); | 
|  | 3216 |  | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3217 | // This is where we go into standby | 
|  | 3218 | if (!mStandby) { | 
|  | 3219 | LOG_AUDIO_STATE(); | 
|  | 3220 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3221 | mStandby = true; | 
|  | 3222 | } | 
|  | 3223 |  | 
|  | 3224 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 3225 | // we're about to wait, flush the binder command buffer | 
|  | 3226 | IPCThreadState::self()->flushCommands(); | 
|  | 3227 |  | 
|  | 3228 | clearOutputTracks(); | 
|  | 3229 |  | 
|  | 3230 | if (exitPending()) { | 
|  | 3231 | break; | 
|  | 3232 | } | 
|  | 3233 |  | 
|  | 3234 | releaseWakeLock_l(); | 
|  | 3235 | // wait until we have something to do... | 
|  | 3236 | ALOGV("%s going to sleep", myName.string()); | 
|  | 3237 | mWaitWorkCV.wait(mLock); | 
|  | 3238 | ALOGV("%s waking up", myName.string()); | 
|  | 3239 | acquireWakeLock_l(); | 
|  | 3240 |  | 
|  | 3241 | mMixerStatus = MIXER_IDLE; | 
|  | 3242 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
|  | 3243 | mBytesWritten = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3244 | mBytesRemaining = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3245 | checkSilentMode_l(); | 
|  | 3246 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3247 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
|  | 3248 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3249 | if (mType == MIXER) { | 
|  | 3250 | sleepTimeShift = 0; | 
|  | 3251 | } | 
|  | 3252 |  | 
|  | 3253 | continue; | 
|  | 3254 | } | 
|  | 3255 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3256 | // mMixerStatusIgnoringFastTracks is also updated internally | 
|  | 3257 | mMixerStatus = prepareTracks_l(&tracksToRemove); | 
|  | 3258 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3259 | mActiveTracks.updatePowerState(this); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3260 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3261 | // prevent any changes in effect chain list and in each effect chain | 
|  | 3262 | // during mixing and effect process as the audio buffers could be deleted | 
|  | 3263 | // or modified if an effect is created or deleted | 
|  | 3264 | lockEffectChains_l(effectChains); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3265 | } // mLock scope ends | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3266 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3267 | if (mBytesRemaining == 0) { | 
|  | 3268 | mCurrentWriteLength = 0; | 
|  | 3269 | if (mMixerStatus == MIXER_TRACKS_READY) { | 
|  | 3270 | // threadLoop_mix() sets mCurrentWriteLength | 
|  | 3271 | threadLoop_mix(); | 
|  | 3272 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) | 
|  | 3273 | && (mMixerStatus != MIXER_DRAIN_ALL)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3274 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3275 | // must be written to HAL | 
|  | 3276 | threadLoop_sleepTime(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3277 | if (mSleepTimeUs == 0) { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3278 | mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3279 | } | 
|  | 3280 | } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3281 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3282 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3283 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) | 
|  | 3284 | // or mSinkBuffer (if there are no effects). | 
|  | 3285 | // | 
|  | 3286 | // This is done pre-effects computation; if effects change to | 
|  | 3287 | // support higher precision, this needs to move. | 
|  | 3288 | // | 
|  | 3289 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3290 | // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3291 | if (mMixerBufferValid) { | 
|  | 3292 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; | 
|  | 3293 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; | 
|  | 3294 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3295 | // mono blend occurs for mixer threads only (not direct or offloaded) | 
|  | 3296 | // and is handled here if we're going directly to the sink. | 
|  | 3297 | if (requireMonoBlend() && !mEffectBufferValid) { | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3298 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, | 
|  | 3299 | true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3300 | } | 
|  | 3301 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3302 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, | 
|  | 3303 | mNormalFrameCount * mChannelCount); | 
|  | 3304 | } | 
|  | 3305 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3306 | mBytesRemaining = mCurrentWriteLength; | 
|  | 3307 | if (isSuspended()) { | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3308 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). | 
|  | 3309 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. | 
|  | 3310 | const size_t framesRemaining = mBytesRemaining / mFrameSize; | 
|  | 3311 | mBytesWritten += mBytesRemaining; | 
|  | 3312 | mFramesWritten += framesRemaining; | 
|  | 3313 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3314 | mBytesRemaining = 0; | 
|  | 3315 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3316 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3317 | // only process effects if we're going to write | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3318 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3319 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 3320 | effectChains[i]->process_l(); | 
|  | 3321 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3322 | } | 
|  | 3323 | } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3324 | // Process effect chains for offloaded thread even if no audio | 
|  | 3325 | // was read from audio track: process only updates effect state | 
|  | 3326 | // and thus does have to be synchronized with audio writes but may have | 
|  | 3327 | // to be called while waiting for async write callback | 
|  | 3328 | if (mType == OFFLOAD) { | 
|  | 3329 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 3330 | effectChains[i]->process_l(); | 
|  | 3331 | } | 
|  | 3332 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3333 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3334 | // Only if the Effects buffer is enabled and there is data in the | 
|  | 3335 | // Effects buffer (buffer valid), we need to | 
|  | 3336 | // copy into the sink buffer. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3337 | // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3338 | if (mEffectBufferValid) { | 
|  | 3339 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3340 |  | 
|  | 3341 | if (requireMonoBlend()) { | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3342 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, | 
|  | 3343 | true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3344 | } | 
|  | 3345 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3346 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, | 
|  | 3347 | mNormalFrameCount * mChannelCount); | 
|  | 3348 | } | 
|  | 3349 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3350 | // enable changes in effect chain | 
|  | 3351 | unlockEffectChains(effectChains); | 
|  | 3352 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3353 | if (!waitingAsyncCallback()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3354 | // mSleepTimeUs == 0 means we must write to audio hardware | 
|  | 3355 | if (mSleepTimeUs == 0) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3356 | ssize_t ret = 0; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3357 | // We save lastWriteFinished here, as previousLastWriteFinished, | 
|  | 3358 | // for throttling. On thread start, previousLastWriteFinished will be | 
|  | 3359 | // set to -1, which properly results in no throttling after the first write. | 
|  | 3360 | nsecs_t previousLastWriteFinished = lastWriteFinished; | 
|  | 3361 | nsecs_t delta = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3362 | if (mBytesRemaining) { | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3363 | // FIXME rewrite to reduce number of system calls | 
|  | 3364 | mLastWriteTime = systemTime();  // also used for dumpsys | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3365 | ret = threadLoop_write(); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3366 | lastWriteFinished = systemTime(); | 
|  | 3367 | delta = lastWriteFinished - mLastWriteTime; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3368 | if (ret < 0) { | 
|  | 3369 | mBytesRemaining = 0; | 
|  | 3370 | } else { | 
|  | 3371 | mBytesWritten += ret; | 
|  | 3372 | mBytesRemaining -= ret; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3373 | mFramesWritten += ret / mFrameSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3374 | } | 
|  | 3375 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || | 
|  | 3376 | (mMixerStatus == MIXER_DRAIN_ALL)) { | 
|  | 3377 | threadLoop_drain(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3378 | } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3379 | if (mType == MIXER && !mStandby) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3380 | // write blocked detection | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3381 | if (delta > maxPeriod) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3382 | mNumDelayedWrites++; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3383 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3384 | ATRACE_NAME("underrun"); | 
|  | 3385 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3386 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3387 | lastWarning = lastWriteFinished; | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3388 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3389 | } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3390 |  | 
|  | 3391 | if (mThreadThrottle | 
|  | 3392 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) | 
|  | 3393 | && ret > 0) {                         // we wrote something | 
|  | 3394 | // Limit MixerThread data processing to no more than twice the | 
|  | 3395 | // expected processing rate. | 
|  | 3396 | // | 
|  | 3397 | // This helps prevent underruns with NuPlayer and other applications | 
|  | 3398 | // which may set up buffers that are close to the minimum size, or use | 
|  | 3399 | // deep buffers, and rely on a double-buffering sleep strategy to fill. | 
|  | 3400 | // | 
|  | 3401 | // The throttle smooths out sudden large data drains from the device, | 
|  | 3402 | // e.g. when it comes out of standby, which often causes problems with | 
|  | 3403 | // (1) mixer threads without a fast mixer (which has its own warm-up) | 
|  | 3404 | // (2) minimum buffer sized tracks (even if the track is full, | 
|  | 3405 | //     the app won't fill fast enough to handle the sudden draw). | 
| Haynes Mathew George | f92b217 | 2016-05-09 11:34:15 -0700 | [diff] [blame] | 3406 | // | 
|  | 3407 | // Total time spent in last processing cycle equals time spent in | 
|  | 3408 | // 1. threadLoop_write, as well as time spent in | 
|  | 3409 | // 2. threadLoop_mix (significant for heavy mixing, especially | 
|  | 3410 | //                    on low tier processors) | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3411 |  | 
| Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 3412 | // it's OK if deltaMs (and deltaNs) is an overestimate. | 
|  | 3413 | nsecs_t deltaNs; | 
|  | 3414 | // deltaNs = lastWriteFinished - previousLastWriteFinished; | 
|  | 3415 | __builtin_sub_overflow( | 
|  | 3416 | lastWriteFinished,previousLastWriteFinished, &deltaNs); | 
|  | 3417 | const int32_t deltaMs = deltaNs / 1000000; | 
|  | 3418 |  | 
| Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 3419 | const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3420 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { | 
|  | 3421 | usleep(throttleMs * 1000); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3422 | // notify of throttle start on verbose log | 
|  | 3423 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, | 
|  | 3424 | "mixer(%p) throttle begin:" | 
|  | 3425 | " ret(%zd) deltaMs(%d) requires sleep %d ms", | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3426 | this, ret, deltaMs, throttleMs); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3427 | mThreadThrottleTimeMs += throttleMs; | 
| Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3428 | // Throttle must be attributed to the previous mixer loop's write time | 
|  | 3429 | // to allow back-to-back throttling. | 
|  | 3430 | lastWriteFinished += throttleMs * 1000000; | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3431 | } else { | 
|  | 3432 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; | 
|  | 3433 | if (diff > 0) { | 
|  | 3434 | // notify of throttle end on debug log | 
| Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3435 | // but prevent spamming for bluetooth | 
|  | 3436 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()), | 
|  | 3437 | "mixer(%p) throttle end: throttle time(%u)", this, diff); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3438 | mThreadThrottleEndMs = mThreadThrottleTimeMs; | 
|  | 3439 | } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3440 | } | 
|  | 3441 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3442 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3443 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3444 | } else { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3445 | ATRACE_BEGIN("sleep"); | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3446 | Mutex::Autolock _l(mLock); | 
| rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3447 | // suspended requires accurate metering of sleep time. | 
|  | 3448 | if (isSuspended()) { | 
|  | 3449 | // advance by expected sleepTime | 
|  | 3450 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); | 
|  | 3451 | const nsecs_t nowNs = systemTime(); | 
|  | 3452 |  | 
|  | 3453 | // compute expected next time vs current time. | 
|  | 3454 | // (negative deltas are treated as delays). | 
|  | 3455 | nsecs_t deltaNs = timeLoopNextNs - nowNs; | 
|  | 3456 | if (deltaNs < -kMaxNextBufferDelayNs) { | 
|  | 3457 | // Delays longer than the max allowed trigger a reset. | 
|  | 3458 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); | 
|  | 3459 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); | 
|  | 3460 | timeLoopNextNs = nowNs + deltaNs; | 
|  | 3461 | } else if (deltaNs < 0) { | 
|  | 3462 | // Delays within the max delay allowed: zero the delta/sleepTime | 
|  | 3463 | // to help the system catch up in the next iteration(s) | 
|  | 3464 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); | 
|  | 3465 | deltaNs = 0; | 
|  | 3466 | } | 
|  | 3467 | // update sleep time (which is >= 0) | 
|  | 3468 | mSleepTimeUs = deltaNs / 1000; | 
|  | 3469 | } | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3470 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { | 
|  | 3471 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3472 | } | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3473 | ATRACE_END(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3474 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3475 | } | 
|  | 3476 |  | 
|  | 3477 | // Finally let go of removed track(s), without the lock held | 
|  | 3478 | // since we can't guarantee the destructors won't acquire that | 
|  | 3479 | // same lock.  This will also mutate and push a new fast mixer state. | 
|  | 3480 | threadLoop_removeTracks(tracksToRemove); | 
|  | 3481 | tracksToRemove.clear(); | 
|  | 3482 |  | 
|  | 3483 | // FIXME I don't understand the need for this here; | 
|  | 3484 | //       it was in the original code but maybe the | 
|  | 3485 | //       assignment in saveOutputTracks() makes this unnecessary? | 
|  | 3486 | clearOutputTracks(); | 
|  | 3487 |  | 
|  | 3488 | // Effect chains will be actually deleted here if they were removed from | 
|  | 3489 | // mEffectChains list during mixing or effects processing | 
|  | 3490 | effectChains.clear(); | 
|  | 3491 |  | 
|  | 3492 | // FIXME Note that the above .clear() is no longer necessary since effectChains | 
|  | 3493 | // is now local to this block, but will keep it for now (at least until merge done). | 
|  | 3494 | } | 
|  | 3495 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3496 | threadLoop_exit(); | 
|  | 3497 |  | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3498 | if (!mStandby) { | 
|  | 3499 | threadLoop_standby(); | 
|  | 3500 | mStandby = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3501 | } | 
|  | 3502 |  | 
|  | 3503 | releaseWakeLock(); | 
|  | 3504 |  | 
|  | 3505 | ALOGV("Thread %p type %d exiting", this, mType); | 
|  | 3506 | return false; | 
|  | 3507 | } | 
|  | 3508 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3509 | // removeTracks_l() must be called with ThreadBase::mLock held | 
|  | 3510 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) | 
|  | 3511 | { | 
|  | 3512 | size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3513 | if (count > 0) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3514 | for (size_t i=0 ; i<count ; i++) { | 
|  | 3515 | const sp<Track>& track = tracksToRemove.itemAt(i); | 
|  | 3516 | mActiveTracks.remove(track); | 
|  | 3517 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); | 
|  | 3518 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 3519 | if (chain != 0) { | 
|  | 3520 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), | 
|  | 3521 | track->sessionId()); | 
|  | 3522 | chain->decActiveTrackCnt(); | 
|  | 3523 | } | 
|  | 3524 | if (track->isTerminated()) { | 
|  | 3525 | removeTrack_l(track); | 
|  | 3526 | } | 
|  | 3527 | } | 
|  | 3528 | } | 
|  | 3529 |  | 
|  | 3530 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3531 |  | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3532 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) | 
|  | 3533 | { | 
|  | 3534 | if (mNormalSink != 0) { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3535 | ExtendedTimestamp ets; | 
|  | 3536 | status_t status = mNormalSink->getTimestamp(ets); | 
|  | 3537 | if (status == NO_ERROR) { | 
|  | 3538 | status = ets.getBestTimestamp(×tamp); | 
|  | 3539 | } | 
|  | 3540 | return status; | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3541 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3542 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3543 | uint64_t position64; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3544 | if (mOutput->getPresentationPosition(&position64, ×tamp.mTime) == OK) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3545 | timestamp.mPosition = (uint32_t)position64; | 
|  | 3546 | return NO_ERROR; | 
|  | 3547 | } | 
|  | 3548 | } | 
|  | 3549 | return INVALID_OPERATION; | 
|  | 3550 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3551 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3552 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 3553 | audio_patch_handle_t *handle) | 
|  | 3554 | { | 
| Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3555 | status_t status; | 
|  | 3556 | if (property_get_bool("af.patch_park", false /* default_value */)) { | 
|  | 3557 | // Park FastMixer to avoid potential DOS issues with writing to the HAL | 
|  | 3558 | // or if HAL does not properly lock against access. | 
|  | 3559 | AutoPark<FastMixer> park(mFastMixer); | 
|  | 3560 | status = PlaybackThread::createAudioPatch_l(patch, handle); | 
|  | 3561 | } else { | 
|  | 3562 | status = PlaybackThread::createAudioPatch_l(patch, handle); | 
|  | 3563 | } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3564 | return status; | 
|  | 3565 | } | 
|  | 3566 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3567 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 3568 | audio_patch_handle_t *handle) | 
|  | 3569 | { | 
|  | 3570 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3571 |  | 
|  | 3572 | // store new device and send to effects | 
|  | 3573 | audio_devices_t type = AUDIO_DEVICE_NONE; | 
|  | 3574 | for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
|  | 3575 | type |= patch->sinks[i].ext.device.type; | 
|  | 3576 | } | 
|  | 3577 |  | 
|  | 3578 | #ifdef ADD_BATTERY_DATA | 
|  | 3579 | // when changing the audio output device, call addBatteryData to notify | 
|  | 3580 | // the change | 
|  | 3581 | if (mOutDevice != type) { | 
|  | 3582 | uint32_t params = 0; | 
|  | 3583 | // check whether speaker is on | 
|  | 3584 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { | 
|  | 3585 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3586 | } | 
|  | 3587 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3588 | audio_devices_t deviceWithoutSpeaker | 
|  | 3589 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 3590 | // check if any other device (except speaker) is on | 
|  | 3591 | if (type & deviceWithoutSpeaker) { | 
|  | 3592 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
|  | 3593 | } | 
|  | 3594 |  | 
|  | 3595 | if (params != 0) { | 
|  | 3596 | addBatteryData(params); | 
|  | 3597 | } | 
|  | 3598 | } | 
|  | 3599 | #endif | 
|  | 3600 |  | 
|  | 3601 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 3602 | mEffectChains[i]->setDevice_l(type); | 
|  | 3603 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3604 |  | 
|  | 3605 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when | 
|  | 3606 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback | 
|  | 3607 | bool configChanged = mPrevOutDevice != type; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3608 | mOutDevice = type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3609 | mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3610 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3611 | if (mOutput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3612 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); | 
|  | 3613 | status = hwDevice->createAudioPatch(patch->num_sources, | 
|  | 3614 | patch->sources, | 
|  | 3615 | patch->num_sinks, | 
|  | 3616 | patch->sinks, | 
|  | 3617 | handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3618 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3619 | char *address; | 
|  | 3620 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
|  | 3621 | //FIXME: we only support address on first sink with HAL version < 3.0 | 
|  | 3622 | address = audio_device_address_to_parameter( | 
|  | 3623 | patch->sinks[0].ext.device.type, | 
|  | 3624 | patch->sinks[0].ext.device.address); | 
|  | 3625 | } else { | 
|  | 3626 | address = (char *)calloc(1, 1); | 
|  | 3627 | } | 
|  | 3628 | AudioParameter param = AudioParameter(String8(address)); | 
|  | 3629 | free(address); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3630 | param.addInt(String8(AudioParameter::keyRouting), (int)type); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3631 | status = mOutput->stream->setParameters(param.toString()); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3632 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3633 | } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3634 | if (configChanged) { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3635 | mPrevOutDevice = type; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3636 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
|  | 3637 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3638 | return status; | 
|  | 3639 | } | 
|  | 3640 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3641 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 3642 | { | 
| Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3643 | status_t status; | 
|  | 3644 | if (property_get_bool("af.patch_park", false /* default_value */)) { | 
|  | 3645 | // Park FastMixer to avoid potential DOS issues with writing to the HAL | 
|  | 3646 | // or if HAL does not properly lock against access. | 
|  | 3647 | AutoPark<FastMixer> park(mFastMixer); | 
|  | 3648 | status = PlaybackThread::releaseAudioPatch_l(handle); | 
|  | 3649 | } else { | 
|  | 3650 | status = PlaybackThread::releaseAudioPatch_l(handle); | 
|  | 3651 | } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3652 | return status; | 
|  | 3653 | } | 
|  | 3654 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3655 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 3656 | { | 
|  | 3657 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3658 |  | 
|  | 3659 | mOutDevice = AUDIO_DEVICE_NONE; | 
|  | 3660 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3661 | if (mOutput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3662 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); | 
|  | 3663 | status = hwDevice->releaseAudioPatch(handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3664 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3665 | AudioParameter param; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3666 | param.addInt(String8(AudioParameter::keyRouting), 0); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3667 | status = mOutput->stream->setParameters(param.toString()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3668 | } | 
|  | 3669 | return status; | 
|  | 3670 | } | 
|  | 3671 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3672 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) | 
|  | 3673 | { | 
|  | 3674 | Mutex::Autolock _l(mLock); | 
|  | 3675 | mTracks.add(track); | 
|  | 3676 | } | 
|  | 3677 |  | 
|  | 3678 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) | 
|  | 3679 | { | 
|  | 3680 | Mutex::Autolock _l(mLock); | 
|  | 3681 | destroyTrack_l(track); | 
|  | 3682 | } | 
|  | 3683 |  | 
|  | 3684 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) | 
|  | 3685 | { | 
|  | 3686 | ThreadBase::getAudioPortConfig(config); | 
|  | 3687 | config->role = AUDIO_PORT_ROLE_SOURCE; | 
|  | 3688 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); | 
|  | 3689 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
|  | 3690 | } | 
|  | 3691 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3692 | // ---------------------------------------------------------------------------- | 
|  | 3693 |  | 
|  | 3694 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3695 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) | 
|  | 3696 | :   PlaybackThread(audioFlinger, output, id, device, type, systemReady), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3697 | // mAudioMixer below | 
|  | 3698 | // mFastMixer below | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3699 | mFastMixerFutex(0), | 
|  | 3700 | mMasterMono(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3701 | // mOutputSink below | 
|  | 3702 | // mPipeSink below | 
|  | 3703 | // mNormalSink below | 
|  | 3704 | { | 
|  | 3705 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3706 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3707 | "mFrameCount=%zu, mNormalFrameCount=%zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3708 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
|  | 3709 | mNormalFrameCount); | 
|  | 3710 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
|  | 3711 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3712 | if (type == DUPLICATING) { | 
|  | 3713 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks | 
|  | 3714 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). | 
|  | 3715 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. | 
|  | 3716 | return; | 
|  | 3717 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3718 | // create an NBAIO sink for the HAL output stream, and negotiate | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 3719 | mOutputSink = new AudioStreamOutSink(output->stream); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3720 | size_t numCounterOffers = 0; | 
| Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3721 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3722 | #if !LOG_NDEBUG | 
|  | 3723 | ssize_t index = | 
|  | 3724 | #else | 
|  | 3725 | (void) | 
|  | 3726 | #endif | 
|  | 3727 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3728 | ALOG_ASSERT(index == 0); | 
|  | 3729 |  | 
|  | 3730 | // initialize fast mixer depending on configuration | 
|  | 3731 | bool initFastMixer; | 
|  | 3732 | switch (kUseFastMixer) { | 
|  | 3733 | case FastMixer_Never: | 
|  | 3734 | initFastMixer = false; | 
|  | 3735 | break; | 
|  | 3736 | case FastMixer_Always: | 
|  | 3737 | initFastMixer = true; | 
|  | 3738 | break; | 
|  | 3739 | case FastMixer_Static: | 
|  | 3740 | case FastMixer_Dynamic: | 
| Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3741 | // FastMixer was designed to operate with a HAL that pulls at a regular rate, | 
|  | 3742 | // where the period is less than an experimentally determined threshold that can be | 
|  | 3743 | // scheduled reliably with CFS. However, the BT A2DP HAL is | 
|  | 3744 | // bursty (does not pull at a regular rate) and so cannot operate with FastMixer. | 
|  | 3745 | initFastMixer = mFrameCount < mNormalFrameCount | 
|  | 3746 | && (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) == 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3747 | break; | 
|  | 3748 | } | 
| Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3749 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, | 
|  | 3750 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", | 
|  | 3751 | mFrameCount, mNormalFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3752 | if (initFastMixer) { | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3753 | audio_format_t fastMixerFormat; | 
|  | 3754 | if (mMixerBufferEnabled && mEffectBufferEnabled) { | 
|  | 3755 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; | 
|  | 3756 | } else { | 
|  | 3757 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 3758 | } | 
|  | 3759 | if (mFormat != fastMixerFormat) { | 
|  | 3760 | // change our Sink format to accept our intermediate precision | 
|  | 3761 | mFormat = fastMixerFormat; | 
|  | 3762 | free(mSinkBuffer); | 
|  | 3763 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
|  | 3764 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
|  | 3765 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
|  | 3766 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3767 |  | 
|  | 3768 | // create a MonoPipe to connect our submix to FastMixer | 
|  | 3769 | NBAIO_Format format = mOutputSink->format(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3770 | #ifdef TEE_SINK | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3771 | NBAIO_Format origformat = format; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3772 | #endif | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3773 | // adjust format to match that of the Fast Mixer | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 3774 | ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3775 | format.mFormat = fastMixerFormat; | 
|  | 3776 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; | 
|  | 3777 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3778 | // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
|  | 3779 | // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
|  | 3780 | // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
|  | 3781 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
|  | 3782 | const NBAIO_Format offers[1] = {format}; | 
|  | 3783 | size_t numCounterOffers = 0; | 
| Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3784 | #if !LOG_NDEBUG || defined(TEE_SINK) | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3785 | ssize_t index = | 
|  | 3786 | #else | 
|  | 3787 | (void) | 
|  | 3788 | #endif | 
|  | 3789 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3790 | ALOG_ASSERT(index == 0); | 
|  | 3791 | monoPipe->setAvgFrames((mScreenState & 1) ? | 
|  | 3792 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
|  | 3793 | mPipeSink = monoPipe; | 
|  | 3794 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3795 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3796 | if (mTeeSinkOutputEnabled) { | 
|  | 3797 | // 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] | 3798 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); | 
|  | 3799 | const NBAIO_Format offers2[1] = {origformat}; | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3800 | numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3801 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3802 | ALOG_ASSERT(index == 0); | 
|  | 3803 | mTeeSink = teeSink; | 
|  | 3804 | PipeReader *teeSource = new PipeReader(*teeSink); | 
|  | 3805 | numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3806 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3807 | ALOG_ASSERT(index == 0); | 
|  | 3808 | mTeeSource = teeSource; | 
|  | 3809 | } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3810 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3811 |  | 
|  | 3812 | // create fast mixer and configure it initially with just one fast track for our submix | 
|  | 3813 | mFastMixer = new FastMixer(); | 
|  | 3814 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3815 | #ifdef STATE_QUEUE_DUMP | 
|  | 3816 | sq->setObserverDump(&mStateQueueObserverDump); | 
|  | 3817 | sq->setMutatorDump(&mStateQueueMutatorDump); | 
|  | 3818 | #endif | 
|  | 3819 | FastMixerState *state = sq->begin(); | 
|  | 3820 | FastTrack *fastTrack = &state->mFastTracks[0]; | 
|  | 3821 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
|  | 3822 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
|  | 3823 | fastTrack->mVolumeProvider = NULL; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3824 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer | 
|  | 3825 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3826 | fastTrack->mGeneration++; | 
|  | 3827 | state->mFastTracksGen++; | 
|  | 3828 | state->mTrackMask = 1; | 
|  | 3829 | // fast mixer will use the HAL output sink | 
|  | 3830 | state->mOutputSink = mOutputSink.get(); | 
|  | 3831 | state->mOutputSinkGen++; | 
|  | 3832 | state->mFrameCount = mFrameCount; | 
|  | 3833 | state->mCommand = FastMixerState::COLD_IDLE; | 
|  | 3834 | // already done in constructor initialization list | 
|  | 3835 | //mFastMixerFutex = 0; | 
|  | 3836 | state->mColdFutexAddr = &mFastMixerFutex; | 
|  | 3837 | state->mColdGen++; | 
|  | 3838 | state->mDumpState = &mFastMixerDumpState; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3839 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3840 | state->mTeeSink = mTeeSink.get(); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3841 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3842 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
|  | 3843 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3844 | sq->end(); | 
|  | 3845 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 3846 |  | 
|  | 3847 | // start the fast mixer | 
|  | 3848 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
|  | 3849 | pid_t tid = mFastMixer->getTid(); | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3850 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); | 
| Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 3851 | stream()->setHalThreadPriority(kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3852 |  | 
|  | 3853 | #ifdef AUDIO_WATCHDOG | 
|  | 3854 | // create and start the watchdog | 
|  | 3855 | mAudioWatchdog = new AudioWatchdog(); | 
|  | 3856 | mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
|  | 3857 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
|  | 3858 | tid = mAudioWatchdog->getTid(); | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 3859 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false /*forApp*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3860 | #endif | 
|  | 3861 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3862 | } | 
|  | 3863 |  | 
|  | 3864 | switch (kUseFastMixer) { | 
|  | 3865 | case FastMixer_Never: | 
|  | 3866 | case FastMixer_Dynamic: | 
|  | 3867 | mNormalSink = mOutputSink; | 
|  | 3868 | break; | 
|  | 3869 | case FastMixer_Always: | 
|  | 3870 | mNormalSink = mPipeSink; | 
|  | 3871 | break; | 
|  | 3872 | case FastMixer_Static: | 
|  | 3873 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
|  | 3874 | break; | 
|  | 3875 | } | 
|  | 3876 | } | 
|  | 3877 |  | 
|  | 3878 | AudioFlinger::MixerThread::~MixerThread() | 
|  | 3879 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3880 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3881 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3882 | FastMixerState *state = sq->begin(); | 
|  | 3883 | if (state->mCommand == FastMixerState::COLD_IDLE) { | 
|  | 3884 | int32_t old = android_atomic_inc(&mFastMixerFutex); | 
|  | 3885 | if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3886 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3887 | } | 
|  | 3888 | } | 
|  | 3889 | state->mCommand = FastMixerState::EXIT; | 
|  | 3890 | sq->end(); | 
|  | 3891 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 3892 | mFastMixer->join(); | 
|  | 3893 | // Though the fast mixer thread has exited, it's state queue is still valid. | 
|  | 3894 | // We'll use that extract the final state which contains one remaining fast track | 
|  | 3895 | // corresponding to our sub-mix. | 
|  | 3896 | state = sq->begin(); | 
|  | 3897 | ALOG_ASSERT(state->mTrackMask == 1); | 
|  | 3898 | FastTrack *fastTrack = &state->mFastTracks[0]; | 
|  | 3899 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
|  | 3900 | delete fastTrack->mBufferProvider; | 
|  | 3901 | sq->end(false /*didModify*/); | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3902 | mFastMixer.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3903 | #ifdef AUDIO_WATCHDOG | 
|  | 3904 | if (mAudioWatchdog != 0) { | 
|  | 3905 | mAudioWatchdog->requestExit(); | 
|  | 3906 | mAudioWatchdog->requestExitAndWait(); | 
|  | 3907 | mAudioWatchdog.clear(); | 
|  | 3908 | } | 
|  | 3909 | #endif | 
|  | 3910 | } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3911 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3912 | delete mAudioMixer; | 
|  | 3913 | } | 
|  | 3914 |  | 
|  | 3915 |  | 
|  | 3916 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const | 
|  | 3917 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3918 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3919 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
|  | 3920 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
|  | 3921 | } | 
|  | 3922 | return latency; | 
|  | 3923 | } | 
|  | 3924 |  | 
|  | 3925 |  | 
|  | 3926 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) | 
|  | 3927 | { | 
|  | 3928 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); | 
|  | 3929 | } | 
|  | 3930 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3931 | ssize_t AudioFlinger::MixerThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3932 | { | 
|  | 3933 | // FIXME we should only do one push per cycle; confirm this is true | 
|  | 3934 | // Start the fast mixer if it's not already running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3935 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3936 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3937 | FastMixerState *state = sq->begin(); | 
|  | 3938 | if (state->mCommand != FastMixerState::MIX_WRITE && | 
|  | 3939 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
|  | 3940 | if (state->mCommand == FastMixerState::COLD_IDLE) { | 
| Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3941 |  | 
|  | 3942 | // FIXME workaround for first HAL write being CPU bound on some devices | 
|  | 3943 | ATRACE_BEGIN("write"); | 
|  | 3944 | mOutput->write((char *)mSinkBuffer, 0); | 
|  | 3945 | ATRACE_END(); | 
|  | 3946 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3947 | int32_t old = android_atomic_inc(&mFastMixerFutex); | 
|  | 3948 | if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3949 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3950 | } | 
|  | 3951 | #ifdef AUDIO_WATCHDOG | 
|  | 3952 | if (mAudioWatchdog != 0) { | 
|  | 3953 | mAudioWatchdog->resume(); | 
|  | 3954 | } | 
|  | 3955 | #endif | 
|  | 3956 | } | 
|  | 3957 | state->mCommand = FastMixerState::MIX_WRITE; | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3958 | #ifdef FAST_THREAD_STATISTICS | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3959 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3960 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3961 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3962 | sq->end(); | 
|  | 3963 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 3964 | if (kUseFastMixer == FastMixer_Dynamic) { | 
|  | 3965 | mNormalSink = mPipeSink; | 
|  | 3966 | } | 
|  | 3967 | } else { | 
|  | 3968 | sq->end(false /*didModify*/); | 
|  | 3969 | } | 
|  | 3970 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3971 | return PlaybackThread::threadLoop_write(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3972 | } | 
|  | 3973 |  | 
|  | 3974 | void AudioFlinger::MixerThread::threadLoop_standby() | 
|  | 3975 | { | 
|  | 3976 | // Idle the fast mixer if it's currently running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3977 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3978 | FastMixerStateQueue *sq = mFastMixer->sq(); | 
|  | 3979 | FastMixerState *state = sq->begin(); | 
|  | 3980 | if (!(state->mCommand & FastMixerState::IDLE)) { | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3981 | // Report any frames trapped in the Monopipe | 
|  | 3982 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); | 
|  | 3983 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); | 
|  | 3984 | mLocalLog.log("threadLoop_standby: framesWritten:%lld  suspendedFrames:%lld  " | 
|  | 3985 | "monoPipeWritten:%lld  monoPipeLeft:%lld", | 
|  | 3986 | (long long)mFramesWritten, (long long)mSuspendedFrames, | 
|  | 3987 | (long long)mPipeSink->framesWritten(), pipeFrames); | 
|  | 3988 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); | 
|  | 3989 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3990 | state->mCommand = FastMixerState::COLD_IDLE; | 
|  | 3991 | state->mColdFutexAddr = &mFastMixerFutex; | 
|  | 3992 | state->mColdGen++; | 
|  | 3993 | mFastMixerFutex = 0; | 
|  | 3994 | sq->end(); | 
|  | 3995 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
|  | 3996 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
|  | 3997 | if (kUseFastMixer == FastMixer_Dynamic) { | 
|  | 3998 | mNormalSink = mOutputSink; | 
|  | 3999 | } | 
|  | 4000 | #ifdef AUDIO_WATCHDOG | 
|  | 4001 | if (mAudioWatchdog != 0) { | 
|  | 4002 | mAudioWatchdog->pause(); | 
|  | 4003 | } | 
|  | 4004 | #endif | 
|  | 4005 | } else { | 
|  | 4006 | sq->end(false /*didModify*/); | 
|  | 4007 | } | 
|  | 4008 | } | 
|  | 4009 | PlaybackThread::threadLoop_standby(); | 
|  | 4010 | } | 
|  | 4011 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4012 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() | 
|  | 4013 | { | 
|  | 4014 | return false; | 
|  | 4015 | } | 
|  | 4016 |  | 
|  | 4017 | bool AudioFlinger::PlaybackThread::shouldStandby_l() | 
|  | 4018 | { | 
|  | 4019 | return !mStandby; | 
|  | 4020 | } | 
|  | 4021 |  | 
|  | 4022 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() | 
|  | 4023 | { | 
|  | 4024 | Mutex::Autolock _l(mLock); | 
|  | 4025 | return waitingAsyncCallback_l(); | 
|  | 4026 | } | 
|  | 4027 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4028 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
|  | 4029 | void AudioFlinger::PlaybackThread::threadLoop_standby() | 
|  | 4030 | { | 
|  | 4031 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4032 | mOutput->standby(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4033 | if (mUseAsyncWrite != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4034 | // discard any pending drain or write ack by incrementing sequence | 
|  | 4035 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
|  | 4036 | mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4037 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4038 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
|  | 4039 | mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4040 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4041 | mHwPaused = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4042 | } | 
|  | 4043 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4044 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() | 
|  | 4045 | { | 
|  | 4046 | ALOGV("signal playback thread"); | 
|  | 4047 | broadcast_l(); | 
|  | 4048 | } | 
|  | 4049 |  | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 4050 | void AudioFlinger::PlaybackThread::onAsyncError() | 
|  | 4051 | { | 
|  | 4052 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { | 
|  | 4053 | invalidateTracks((audio_stream_type_t)i); | 
|  | 4054 | } | 
|  | 4055 | } | 
|  | 4056 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4057 | void AudioFlinger::MixerThread::threadLoop_mix() | 
|  | 4058 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4059 | // mix buffers... | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 4060 | mAudioMixer->process(); | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4061 | mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4062 | // increase sleep time progressively when application underrun condition clears. | 
|  | 4063 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
|  | 4064 | // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
|  | 4065 | // such that we would underrun the audio HAL. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4066 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4067 | sleepTimeShift--; | 
|  | 4068 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4069 | mSleepTimeUs = 0; | 
|  | 4070 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4071 | //TODO: delay standby when effects have a tail | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 4072 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4073 | } | 
|  | 4074 |  | 
|  | 4075 | void AudioFlinger::MixerThread::threadLoop_sleepTime() | 
|  | 4076 | { | 
|  | 4077 | // If no tracks are ready, sleep once for the duration of an output | 
|  | 4078 | // buffer size, then write 0s to the output | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4079 | if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4080 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4081 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; | 
|  | 4082 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { | 
|  | 4083 | mSleepTimeUs = kMinThreadSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4084 | } | 
|  | 4085 | // reduce sleep time in case of consecutive application underruns to avoid | 
|  | 4086 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
|  | 4087 | // duration we would end up writing less data than needed by the audio HAL if | 
|  | 4088 | // the condition persists. | 
|  | 4089 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
|  | 4090 | sleepTimeShift++; | 
|  | 4091 | } | 
|  | 4092 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4093 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4094 | } | 
|  | 4095 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4096 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared | 
|  | 4097 | // before effects processing or output. | 
|  | 4098 | if (mMixerBufferValid) { | 
|  | 4099 | memset(mMixerBuffer, 0, mMixerBufferSize); | 
|  | 4100 | } else { | 
|  | 4101 | memset(mSinkBuffer, 0, mSinkBufferSize); | 
|  | 4102 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4103 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4104 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
|  | 4105 | "anticipated start"); | 
|  | 4106 | } | 
|  | 4107 | // TODO add standby time extension fct of effect tail | 
|  | 4108 | } | 
|  | 4109 |  | 
|  | 4110 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
|  | 4111 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( | 
|  | 4112 | Vector< sp<Track> > *tracksToRemove) | 
|  | 4113 | { | 
|  | 4114 |  | 
|  | 4115 | mixer_state mixerStatus = MIXER_IDLE; | 
|  | 4116 | // find out which tracks need to be processed | 
|  | 4117 | size_t count = mActiveTracks.size(); | 
|  | 4118 | size_t mixedTracks = 0; | 
|  | 4119 | size_t tracksWithEffect = 0; | 
|  | 4120 | // counts only _active_ fast tracks | 
|  | 4121 | size_t fastTracks = 0; | 
|  | 4122 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
|  | 4123 |  | 
|  | 4124 | float masterVolume = mMasterVolume; | 
|  | 4125 | bool masterMute = mMasterMute; | 
|  | 4126 |  | 
|  | 4127 | if (masterMute) { | 
|  | 4128 | masterVolume = 0; | 
|  | 4129 | } | 
|  | 4130 | // Delegate master volume control to effect in output mix effect chain if needed | 
|  | 4131 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
|  | 4132 | if (chain != 0) { | 
|  | 4133 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
|  | 4134 | chain->setVolume_l(&v, &v); | 
|  | 4135 | masterVolume = (float)((v + (1 << 23)) >> 24); | 
|  | 4136 | chain.clear(); | 
|  | 4137 | } | 
|  | 4138 |  | 
|  | 4139 | // prepare a new state to push | 
|  | 4140 | FastMixerStateQueue *sq = NULL; | 
|  | 4141 | FastMixerState *state = NULL; | 
|  | 4142 | bool didModify = false; | 
|  | 4143 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4144 | bool coldIdle = false; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4145 | if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4146 | sq = mFastMixer->sq(); | 
|  | 4147 | state = sq->begin(); | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4148 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4149 | } | 
|  | 4150 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4151 | mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4152 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4153 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4154 | for (size_t i=0 ; i<count ; i++) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4155 | const sp<Track> t = mActiveTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4156 |  | 
|  | 4157 | // this const just means the local variable doesn't change | 
|  | 4158 | Track* const track = t.get(); | 
|  | 4159 |  | 
|  | 4160 | // process fast tracks | 
|  | 4161 | if (track->isFastTrack()) { | 
|  | 4162 |  | 
|  | 4163 | // It's theoretically possible (though unlikely) for a fast track to be created | 
|  | 4164 | // and then removed within the same normal mix cycle.  This is not a problem, as | 
|  | 4165 | // the track never becomes active so it's fast mixer slot is never touched. | 
|  | 4166 | // The converse, of removing an (active) track and then creating a new track | 
|  | 4167 | // at the identical fast mixer slot within the same normal mix cycle, | 
|  | 4168 | // is impossible because the slot isn't marked available until the end of each cycle. | 
|  | 4169 | int j = track->mFastIndex; | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4170 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4171 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
|  | 4172 | FastTrack *fastTrack = &state->mFastTracks[j]; | 
|  | 4173 |  | 
|  | 4174 | // Determine whether the track is currently in underrun condition, | 
|  | 4175 | // and whether it had a recent underrun. | 
|  | 4176 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
|  | 4177 | FastTrackUnderruns underruns = ftDump->mUnderruns; | 
|  | 4178 | uint32_t recentFull = (underruns.mBitFields.mFull - | 
|  | 4179 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; | 
|  | 4180 | uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
|  | 4181 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; | 
|  | 4182 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
|  | 4183 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; | 
|  | 4184 | uint32_t recentUnderruns = recentPartial + recentEmpty; | 
|  | 4185 | track->mObservedUnderruns = underruns; | 
|  | 4186 | // don't count underruns that occur while stopping or pausing | 
|  | 4187 | // or stopped which can occur when flush() is called while active | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4188 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && | 
|  | 4189 | recentUnderruns > 0) { | 
|  | 4190 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun | 
|  | 4191 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4192 | } else { | 
|  | 4193 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4194 | } | 
|  | 4195 |  | 
|  | 4196 | // This is similar to the state machine for normal tracks, | 
|  | 4197 | // with a few modifications for fast tracks. | 
|  | 4198 | bool isActive = true; | 
|  | 4199 | switch (track->mState) { | 
|  | 4200 | case TrackBase::STOPPING_1: | 
|  | 4201 | // track stays active in STOPPING_1 state until first underrun | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4202 | if (recentUnderruns > 0 || track->isTerminated()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4203 | track->mState = TrackBase::STOPPING_2; | 
|  | 4204 | } | 
|  | 4205 | break; | 
|  | 4206 | case TrackBase::PAUSING: | 
|  | 4207 | // ramp down is not yet implemented | 
|  | 4208 | track->setPaused(); | 
|  | 4209 | break; | 
|  | 4210 | case TrackBase::RESUMING: | 
|  | 4211 | // ramp up is not yet implemented | 
|  | 4212 | track->mState = TrackBase::ACTIVE; | 
|  | 4213 | break; | 
|  | 4214 | case TrackBase::ACTIVE: | 
|  | 4215 | if (recentFull > 0 || recentPartial > 0) { | 
|  | 4216 | // track has provided at least some frames recently: reset retry count | 
|  | 4217 | track->mRetryCount = kMaxTrackRetries; | 
|  | 4218 | } | 
|  | 4219 | if (recentUnderruns == 0) { | 
|  | 4220 | // no recent underruns: stay active | 
|  | 4221 | break; | 
|  | 4222 | } | 
|  | 4223 | // there has recently been an underrun of some kind | 
|  | 4224 | if (track->sharedBuffer() == 0) { | 
|  | 4225 | // were any of the recent underruns "empty" (no frames available)? | 
|  | 4226 | if (recentEmpty == 0) { | 
|  | 4227 | // no, then ignore the partial underruns as they are allowed indefinitely | 
|  | 4228 | break; | 
|  | 4229 | } | 
|  | 4230 | // there has recently been an "empty" underrun: decrement the retry counter | 
|  | 4231 | if (--(track->mRetryCount) > 0) { | 
|  | 4232 | break; | 
|  | 4233 | } | 
|  | 4234 | // indicate to client process that the track was disabled because of underrun; | 
|  | 4235 | // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4236 | track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4237 | // remove from active list, but state remains ACTIVE [confusing but true] | 
|  | 4238 | isActive = false; | 
|  | 4239 | break; | 
|  | 4240 | } | 
|  | 4241 | // fall through | 
|  | 4242 | case TrackBase::STOPPING_2: | 
|  | 4243 | case TrackBase::PAUSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4244 | case TrackBase::STOPPED: | 
|  | 4245 | case TrackBase::FLUSHED:   // flush() while active | 
|  | 4246 | // Check for presentation complete if track is inactive | 
|  | 4247 | // We have consumed all the buffers of this track. | 
|  | 4248 | // This would be incomplete if we auto-paused on underrun | 
|  | 4249 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4250 | uint32_t latency = 0; | 
|  | 4251 | status_t result = mOutput->stream->getLatency(&latency); | 
|  | 4252 | ALOGE_IF(result != OK, | 
|  | 4253 | "Error when retrieving output stream latency: %d", result); | 
|  | 4254 | size_t audioHALFrames = (latency * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4255 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4256 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
|  | 4257 | // track stays in active list until presentation is complete | 
|  | 4258 | break; | 
|  | 4259 | } | 
|  | 4260 | } | 
|  | 4261 | if (track->isStopping_2()) { | 
|  | 4262 | track->mState = TrackBase::STOPPED; | 
|  | 4263 | } | 
|  | 4264 | if (track->isStopped()) { | 
|  | 4265 | // Can't reset directly, as fast mixer is still polling this track | 
|  | 4266 | //   track->reset(); | 
|  | 4267 | // So instead mark this track as needing to be reset after push with ack | 
|  | 4268 | resetMask |= 1 << i; | 
|  | 4269 | } | 
|  | 4270 | isActive = false; | 
|  | 4271 | break; | 
|  | 4272 | case TrackBase::IDLE: | 
|  | 4273 | default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4274 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4275 | } | 
|  | 4276 |  | 
|  | 4277 | if (isActive) { | 
|  | 4278 | // was it previously inactive? | 
|  | 4279 | if (!(state->mTrackMask & (1 << j))) { | 
|  | 4280 | ExtendedAudioBufferProvider *eabp = track; | 
|  | 4281 | VolumeProvider *vp = track; | 
|  | 4282 | fastTrack->mBufferProvider = eabp; | 
|  | 4283 | fastTrack->mVolumeProvider = vp; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4284 | fastTrack->mChannelMask = track->mChannelMask; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4285 | fastTrack->mFormat = track->mFormat; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4286 | fastTrack->mGeneration++; | 
|  | 4287 | state->mTrackMask |= 1 << j; | 
|  | 4288 | didModify = true; | 
|  | 4289 | // no acknowledgement required for newly active tracks | 
|  | 4290 | } | 
|  | 4291 | // cache the combined master volume and stream type volume for fast mixer; this | 
|  | 4292 | // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4293 | const float vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4294 | track->mAudioTrackServerProxy->framesReleased()).first; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4295 | track->mCachedVolume = masterVolume | 
|  | 4296 | * mStreamTypes[track->streamType()].volume | 
|  | 4297 | * vh; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4298 | ++fastTracks; | 
|  | 4299 | } else { | 
|  | 4300 | // was it previously active? | 
|  | 4301 | if (state->mTrackMask & (1 << j)) { | 
|  | 4302 | fastTrack->mBufferProvider = NULL; | 
|  | 4303 | fastTrack->mGeneration++; | 
|  | 4304 | state->mTrackMask &= ~(1 << j); | 
|  | 4305 | didModify = true; | 
|  | 4306 | // If any fast tracks were removed, we must wait for acknowledgement | 
|  | 4307 | // because we're about to decrement the last sp<> on those tracks. | 
|  | 4308 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
|  | 4309 | } else { | 
| Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 4310 | // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an | 
|  | 4311 | // AudioTrack may start (which may not be with a start() but with a write() | 
|  | 4312 | // after underrun) and immediately paused or released.  In that case the | 
|  | 4313 | // FastTrack state hasn't had time to update. | 
|  | 4314 | // TODO Remove the ALOGW when this theory is confirmed. | 
|  | 4315 | ALOGW("fast track %d should have been active; " | 
| Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4316 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", | 
|  | 4317 | j, track->mState, state->mTrackMask, recentUnderruns, | 
|  | 4318 | track->sharedBuffer() != 0); | 
| Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 4319 | // Since the FastMixer state already has the track inactive, do nothing here. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4320 | } | 
|  | 4321 | tracksToRemove->add(track); | 
|  | 4322 | // Avoids a misleading display in dumpsys | 
|  | 4323 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; | 
|  | 4324 | } | 
|  | 4325 | continue; | 
|  | 4326 | } | 
|  | 4327 |  | 
|  | 4328 | {   // local variable scope to avoid goto warning | 
|  | 4329 |  | 
|  | 4330 | audio_track_cblk_t* cblk = track->cblk(); | 
|  | 4331 |  | 
|  | 4332 | // The first time a track is added we wait | 
|  | 4333 | // for all its buffers to be filled before processing it | 
|  | 4334 | int name = track->name(); | 
|  | 4335 | // make sure that we have enough frames to mix one full buffer. | 
|  | 4336 | // enforce this condition only once to enable draining the buffer in case the client | 
|  | 4337 | // app does not call stop() and relies on underrun to stop: | 
|  | 4338 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
|  | 4339 | // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4340 | size_t desiredFrames; | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4341 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4342 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4343 |  | 
|  | 4344 | desiredFrames = sourceFramesNeededWithTimestretch( | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4345 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4346 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. | 
|  | 4347 | // add frames already consumed but not yet released by the resampler | 
|  | 4348 | // because mAudioTrackServerProxy->framesReady() will include these frames | 
|  | 4349 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
|  | 4350 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4351 | uint32_t minFrames = 1; | 
|  | 4352 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
|  | 4353 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4354 | minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4355 | } | 
| Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4356 |  | 
|  | 4357 | size_t framesReady = track->framesReady(); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4358 | if (ATRACE_ENABLED()) { | 
|  | 4359 | // I wish we had formatted trace names | 
|  | 4360 | char traceName[16]; | 
|  | 4361 | strcpy(traceName, "nRdy"); | 
|  | 4362 | int name = track->name(); | 
|  | 4363 | if (AudioMixer::TRACK0 <= name && | 
|  | 4364 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { | 
|  | 4365 | name -= AudioMixer::TRACK0; | 
|  | 4366 | traceName[4] = (name / 10) + '0'; | 
|  | 4367 | traceName[5] = (name % 10) + '0'; | 
|  | 4368 | } else { | 
|  | 4369 | traceName[4] = '?'; | 
|  | 4370 | traceName[5] = '?'; | 
|  | 4371 | } | 
|  | 4372 | traceName[6] = '\0'; | 
|  | 4373 | ATRACE_INT(traceName, framesReady); | 
|  | 4374 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4375 | if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4376 | !track->isPaused() && !track->isTerminated()) | 
|  | 4377 | { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4378 | 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] | 4379 |  | 
|  | 4380 | mixedTracks++; | 
|  | 4381 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4382 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means | 
|  | 4383 | // there is an effect chain connected to the track | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4384 | chain.clear(); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4385 | if (track->mainBuffer() != mSinkBuffer && | 
|  | 4386 | track->mainBuffer() != mMixerBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4387 | if (mEffectBufferEnabled) { | 
|  | 4388 | mEffectBufferValid = true; // Later can set directly. | 
|  | 4389 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4390 | chain = getEffectChain_l(track->sessionId()); | 
|  | 4391 | // Delegate volume control to effect in track effect chain if needed | 
|  | 4392 | if (chain != 0) { | 
|  | 4393 | tracksWithEffect++; | 
|  | 4394 | } else { | 
|  | 4395 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " | 
|  | 4396 | "session %d", | 
|  | 4397 | name, track->sessionId()); | 
|  | 4398 | } | 
|  | 4399 | } | 
|  | 4400 |  | 
|  | 4401 |  | 
|  | 4402 | int param = AudioMixer::VOLUME; | 
|  | 4403 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 4404 | // no ramp for the first volume setting | 
|  | 4405 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
|  | 4406 | if (track->mState == TrackBase::RESUMING) { | 
|  | 4407 | track->mState = TrackBase::ACTIVE; | 
|  | 4408 | param = AudioMixer::RAMP_VOLUME; | 
|  | 4409 | } | 
|  | 4410 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4411 | mLeftVolFloat = -1.0; | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4412 | // FIXME should not make a decision based on mServer | 
|  | 4413 | } else if (cblk->mServer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4414 | // If the track is stopped before the first frame was mixed, | 
|  | 4415 | // do not apply ramp | 
|  | 4416 | param = AudioMixer::RAMP_VOLUME; | 
|  | 4417 | } | 
|  | 4418 |  | 
|  | 4419 | // compute volume for this track | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4420 | uint32_t vl, vr;       // in U8.24 integer format | 
|  | 4421 | float vlf, vrf, vaf;   // in [0.0, 1.0] float format | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4422 | // read original volumes with volume control | 
|  | 4423 | float typeVolume = mStreamTypes[track->streamType()].volume; | 
|  | 4424 | float v = masterVolume * typeVolume; | 
|  | 4425 |  | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4426 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4427 | vl = vr = 0; | 
|  | 4428 | vlf = vrf = vaf = 0.; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4429 | if (track->isPausing()) { | 
|  | 4430 | track->setPaused(); | 
|  | 4431 | } | 
|  | 4432 | } else { | 
| Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4433 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4434 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4435 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
|  | 4436 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4437 | // 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] | 4438 | if (vlf > GAIN_FLOAT_UNITY) { | 
|  | 4439 | ALOGV("Track left volume out of range: %.3g", vlf); | 
|  | 4440 | vlf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4441 | } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4442 | if (vrf > GAIN_FLOAT_UNITY) { | 
|  | 4443 | ALOGV("Track right volume out of range: %.3g", vrf); | 
|  | 4444 | vrf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4445 | } | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4446 | const float vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4447 | track->mAudioTrackServerProxy->framesReleased()).first; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4448 | // now apply the master volume and stream type volume and shaper volume | 
|  | 4449 | vlf *= v * vh; | 
|  | 4450 | vrf *= v * vh; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4451 | // 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] | 4452 | // then derive vl and vr as U8.24 versions for the effect chain | 
|  | 4453 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; | 
|  | 4454 | vl = (uint32_t) (scaleto8_24 * vlf); | 
|  | 4455 | vr = (uint32_t) (scaleto8_24 * vrf); | 
|  | 4456 | // vl and vr are now in U8.24 format | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4457 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4458 | // send level comes from shared memory and so may be corrupt | 
|  | 4459 | if (sendLevel > MAX_GAIN_INT) { | 
|  | 4460 | ALOGV("Track send level out of range: %04X", sendLevel); | 
|  | 4461 | sendLevel = MAX_GAIN_INT; | 
|  | 4462 | } | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4463 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel | 
|  | 4464 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4465 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4466 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4467 | // Delegate volume control to effect in track effect chain if needed | 
|  | 4468 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
|  | 4469 | // Do not ramp volume if volume is controlled by effect | 
|  | 4470 | param = AudioMixer::VOLUME; | 
| Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4471 | // Update remaining floating point volume levels | 
|  | 4472 | vlf = (float)vl / (1 << 24); | 
|  | 4473 | vrf = (float)vr / (1 << 24); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4474 | track->mHasVolumeController = true; | 
|  | 4475 | } else { | 
|  | 4476 | // force no volume ramp when volume controller was just disabled or removed | 
|  | 4477 | // from effect chain to avoid volume spike | 
|  | 4478 | if (track->mHasVolumeController) { | 
|  | 4479 | param = AudioMixer::VOLUME; | 
|  | 4480 | } | 
|  | 4481 | track->mHasVolumeController = false; | 
|  | 4482 | } | 
|  | 4483 |  | 
| Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 4484 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is | 
|  | 4485 | // still applied by the mixer. | 
|  | 4486 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { | 
|  | 4487 | v = mStreamTypes[track->streamType()].mute ? 0.0f : v; | 
|  | 4488 | if (v != mLeftVolFloat) { | 
|  | 4489 | status_t result = mOutput->stream->setVolume(v, v); | 
|  | 4490 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); | 
|  | 4491 | if (result == OK) { | 
|  | 4492 | mLeftVolFloat = v; | 
|  | 4493 | } | 
|  | 4494 | } | 
|  | 4495 | // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we | 
|  | 4496 | // remove stream volume contribution from software volume. | 
|  | 4497 | if (v != 0.0f && mLeftVolFloat == v) { | 
|  | 4498 | vlf = min(1.0f, vlf / v); | 
|  | 4499 | vrf = min(1.0f, vrf / v); | 
|  | 4500 | vaf = min(1.0f, vaf / v); | 
|  | 4501 | } | 
|  | 4502 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4503 | // XXX: these things DON'T need to be done each time | 
|  | 4504 | mAudioMixer->setBufferProvider(name, track); | 
|  | 4505 | mAudioMixer->enable(name); | 
|  | 4506 |  | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4507 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); | 
|  | 4508 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); | 
|  | 4509 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4510 | mAudioMixer->setParameter( | 
|  | 4511 | name, | 
|  | 4512 | AudioMixer::TRACK, | 
|  | 4513 | AudioMixer::FORMAT, (void *)track->format()); | 
|  | 4514 | mAudioMixer->setParameter( | 
|  | 4515 | name, | 
|  | 4516 | AudioMixer::TRACK, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4517 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4518 | mAudioMixer->setParameter( | 
|  | 4519 | name, | 
|  | 4520 | AudioMixer::TRACK, | 
|  | 4521 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4522 | // 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] | 4523 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4524 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4525 | if (reqSampleRate == 0) { | 
|  | 4526 | reqSampleRate = mSampleRate; | 
|  | 4527 | } else if (reqSampleRate > maxSampleRate) { | 
|  | 4528 | reqSampleRate = maxSampleRate; | 
|  | 4529 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4530 | mAudioMixer->setParameter( | 
|  | 4531 | name, | 
|  | 4532 | AudioMixer::RESAMPLE, | 
|  | 4533 | AudioMixer::SAMPLE_RATE, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4534 | (void *)(uintptr_t)reqSampleRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4535 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4536 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4537 | mAudioMixer->setParameter( | 
|  | 4538 | name, | 
|  | 4539 | AudioMixer::TIMESTRETCH, | 
|  | 4540 | AudioMixer::PLAYBACK_RATE, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4541 | &playbackRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4542 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4543 | /* | 
|  | 4544 | * Select the appropriate output buffer for the track. | 
|  | 4545 | * | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4546 | * Tracks with effects go into their own effects chain buffer | 
|  | 4547 | * and from there into either mEffectBuffer or mSinkBuffer. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4548 | * | 
|  | 4549 | * Other tracks can use mMixerBuffer for higher precision | 
|  | 4550 | * channel accumulation.  If this buffer is enabled | 
|  | 4551 | * (mMixerBufferEnabled true), then selected tracks will accumulate | 
|  | 4552 | * into it. | 
|  | 4553 | * | 
|  | 4554 | */ | 
|  | 4555 | if (mMixerBufferEnabled | 
|  | 4556 | && (track->mainBuffer() == mSinkBuffer | 
|  | 4557 | || track->mainBuffer() == mMixerBuffer)) { | 
|  | 4558 | mAudioMixer->setParameter( | 
|  | 4559 | name, | 
|  | 4560 | AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4561 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4562 | mAudioMixer->setParameter( | 
|  | 4563 | name, | 
|  | 4564 | AudioMixer::TRACK, | 
|  | 4565 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); | 
|  | 4566 | // TODO: override track->mainBuffer()? | 
|  | 4567 | mMixerBufferValid = true; | 
|  | 4568 | } else { | 
|  | 4569 | mAudioMixer->setParameter( | 
|  | 4570 | name, | 
|  | 4571 | AudioMixer::TRACK, | 
| rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 4572 | AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4573 | mAudioMixer->setParameter( | 
|  | 4574 | name, | 
|  | 4575 | AudioMixer::TRACK, | 
|  | 4576 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
|  | 4577 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4578 | mAudioMixer->setParameter( | 
|  | 4579 | name, | 
|  | 4580 | AudioMixer::TRACK, | 
|  | 4581 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
|  | 4582 |  | 
|  | 4583 | // reset retry count | 
|  | 4584 | track->mRetryCount = kMaxTrackRetries; | 
|  | 4585 |  | 
|  | 4586 | // If one track is ready, set the mixer ready if: | 
|  | 4587 | //  - the mixer was not ready during previous round OR | 
|  | 4588 | //  - no other track is not ready | 
|  | 4589 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
|  | 4590 | mixerStatus != MIXER_TRACKS_ENABLED) { | 
|  | 4591 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 4592 | } | 
|  | 4593 | } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4594 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4595 | ALOGV("track(%p) underrun,  framesReady(%zu) < framesDesired(%zd)", | 
|  | 4596 | track, framesReady, desiredFrames); | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4597 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4598 | } else { | 
|  | 4599 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4600 | } | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4601 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4602 | // clear effect chain input buffer if an active track underruns to avoid sending | 
|  | 4603 | // previous audio buffer again to effects | 
|  | 4604 | chain = getEffectChain_l(track->sessionId()); | 
|  | 4605 | if (chain != 0) { | 
|  | 4606 | chain->clearInputBuffer(); | 
|  | 4607 | } | 
|  | 4608 |  | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4609 | 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] | 4610 | if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
|  | 4611 | track->isStopped() || track->isPaused()) { | 
|  | 4612 | // We have consumed all the buffers of this track. | 
|  | 4613 | // Remove it from the list of active tracks. | 
|  | 4614 | // TODO: use actual buffer filling status instead of latency when available from | 
|  | 4615 | // audio HAL | 
|  | 4616 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4617 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4618 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
|  | 4619 | if (track->isStopped()) { | 
|  | 4620 | track->reset(); | 
|  | 4621 | } | 
|  | 4622 | tracksToRemove->add(track); | 
|  | 4623 | } | 
|  | 4624 | } else { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4625 | // No buffers for this track. Give it a few chances to | 
|  | 4626 | // fill a buffer, then remove it from active list. | 
|  | 4627 | if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4628 | 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] | 4629 | tracksToRemove->add(track); | 
|  | 4630 | // indicate to client process that the track was disabled because of underrun; | 
|  | 4631 | // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4632 | track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4633 | // If one track is not ready, mark the mixer also not ready if: | 
|  | 4634 | //  - the mixer was ready during previous round OR | 
|  | 4635 | //  - no other track is ready | 
|  | 4636 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
|  | 4637 | mixerStatus != MIXER_TRACKS_READY) { | 
|  | 4638 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 4639 | } | 
|  | 4640 | } | 
|  | 4641 | mAudioMixer->disable(name); | 
|  | 4642 | } | 
|  | 4643 |  | 
|  | 4644 | }   // local variable scope to avoid goto warning | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4645 |  | 
|  | 4646 | } | 
|  | 4647 |  | 
|  | 4648 | // Push the new FastMixer state if necessary | 
|  | 4649 | bool pauseAudioWatchdog = false; | 
|  | 4650 | if (didModify) { | 
|  | 4651 | state->mFastTracksGen++; | 
|  | 4652 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
|  | 4653 | if (kUseFastMixer == FastMixer_Dynamic && | 
|  | 4654 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
|  | 4655 | state->mCommand = FastMixerState::COLD_IDLE; | 
|  | 4656 | state->mColdFutexAddr = &mFastMixerFutex; | 
|  | 4657 | state->mColdGen++; | 
|  | 4658 | mFastMixerFutex = 0; | 
|  | 4659 | if (kUseFastMixer == FastMixer_Dynamic) { | 
|  | 4660 | mNormalSink = mOutputSink; | 
|  | 4661 | } | 
|  | 4662 | // If we go into cold idle, need to wait for acknowledgement | 
|  | 4663 | // so that fast mixer stops doing I/O. | 
|  | 4664 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
|  | 4665 | pauseAudioWatchdog = true; | 
|  | 4666 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4667 | } | 
|  | 4668 | if (sq != NULL) { | 
|  | 4669 | sq->end(didModify); | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4670 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread | 
|  | 4671 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE | 
|  | 4672 | // when bringing the output sink into standby.) | 
|  | 4673 | // | 
|  | 4674 | // We will get the latest FastMixer state when we come out of COLD_IDLE. | 
|  | 4675 | // | 
|  | 4676 | // This occurs with BT suspend when we idle the FastMixer with | 
|  | 4677 | // active tracks, which may be added or removed. | 
|  | 4678 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4679 | } | 
|  | 4680 | #ifdef AUDIO_WATCHDOG | 
|  | 4681 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
|  | 4682 | mAudioWatchdog->pause(); | 
|  | 4683 | } | 
|  | 4684 | #endif | 
|  | 4685 |  | 
|  | 4686 | // Now perform the deferred reset on fast tracks that have stopped | 
|  | 4687 | while (resetMask != 0) { | 
|  | 4688 | size_t i = __builtin_ctz(resetMask); | 
|  | 4689 | ALOG_ASSERT(i < count); | 
|  | 4690 | resetMask &= ~(1 << i); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4691 | sp<Track> track = mActiveTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4692 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
|  | 4693 | track->reset(); | 
|  | 4694 | } | 
|  | 4695 |  | 
|  | 4696 | // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4697 | removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4698 |  | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4699 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { | 
|  | 4700 | mEffectBufferValid = true; | 
| Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4701 | } | 
|  | 4702 |  | 
|  | 4703 | if (mEffectBufferValid) { | 
| Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4704 | // as long as there are effects we should clear the effects buffer, to avoid | 
|  | 4705 | // passing a non-clean buffer to the effect chain | 
|  | 4706 | memset(mEffectBuffer, 0, mEffectBufferSize); | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4707 | } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4708 | // sink or mix buffer must be cleared if all tracks are connected to an | 
|  | 4709 | // effect chain as in this case the mixer will not write to the sink or mix buffer | 
|  | 4710 | // and track effects will accumulate into it | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4711 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
|  | 4712 | (mixedTracks == 0 && fastTracks > 0))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4713 | // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4714 | if (mMixerBufferValid) { | 
|  | 4715 | memset(mMixerBuffer, 0, mMixerBufferSize); | 
|  | 4716 | // TODO: In testing, mSinkBuffer below need not be cleared because | 
|  | 4717 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer | 
|  | 4718 | // after mixing. | 
|  | 4719 | // | 
|  | 4720 | // To enforce this guarantee: | 
|  | 4721 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
|  | 4722 | // (mixedTracks == 0 && fastTracks > 0)) | 
|  | 4723 | // must imply MIXER_TRACKS_READY. | 
|  | 4724 | // Later, we may clear buffers regardless, and skip much of this logic. | 
|  | 4725 | } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4726 | // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4727 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4728 | } | 
|  | 4729 |  | 
|  | 4730 | // if any fast tracks, then status is ready | 
|  | 4731 | mMixerStatusIgnoringFastTracks = mixerStatus; | 
|  | 4732 | if (fastTracks > 0) { | 
|  | 4733 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 4734 | } | 
|  | 4735 | return mixerStatus; | 
|  | 4736 | } | 
|  | 4737 |  | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4738 | // trackCountForUid_l() must be called with ThreadBase::mLock held | 
|  | 4739 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) | 
|  | 4740 | { | 
|  | 4741 | uint32_t trackCount = 0; | 
|  | 4742 | for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 4743 | if (mTracks[i]->uid() == uid) { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4744 | trackCount++; | 
|  | 4745 | } | 
|  | 4746 | } | 
|  | 4747 | return trackCount; | 
|  | 4748 | } | 
|  | 4749 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4750 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4751 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4752 | audio_format_t format, audio_session_t sessionId, uid_t uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4753 | { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4754 | if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { | 
|  | 4755 | return -1; | 
|  | 4756 | } | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4757 | return mAudioMixer->getTrackName(channelMask, format, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4758 | } | 
|  | 4759 |  | 
|  | 4760 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
|  | 4761 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
|  | 4762 | { | 
|  | 4763 | ALOGV("remove track (%d) and delete from mixer", name); | 
|  | 4764 | mAudioMixer->deleteTrackName(name); | 
|  | 4765 | } | 
|  | 4766 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4767 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
|  | 4768 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 4769 | status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4770 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4771 | bool reconfig = false; | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4772 | bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4773 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4774 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4775 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4776 | AutoPark<FastMixer> park(mFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4777 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4778 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 4779 | int value; | 
|  | 4780 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 4781 | reconfig = true; | 
|  | 4782 | } | 
|  | 4783 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4784 | if (!isValidPcmSinkFormat((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4785 | status = BAD_VALUE; | 
|  | 4786 | } else { | 
|  | 4787 | // no need to save value, since it's constant | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4788 | reconfig = true; | 
|  | 4789 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4790 | } | 
|  | 4791 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4792 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4793 | status = BAD_VALUE; | 
|  | 4794 | } else { | 
|  | 4795 | // no need to save value, since it's constant | 
|  | 4796 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4797 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4798 | } | 
|  | 4799 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 4800 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 4801 | // size depends on frame count and correct behavior would not be guaranteed | 
|  | 4802 | // if frame count is changed after track creation | 
|  | 4803 | if (!mTracks.isEmpty()) { | 
|  | 4804 | status = INVALID_OPERATION; | 
|  | 4805 | } else { | 
|  | 4806 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4807 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4808 | } | 
|  | 4809 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4810 | #ifdef ADD_BATTERY_DATA | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4811 | // when changing the audio output device, call addBatteryData to notify | 
|  | 4812 | // the change | 
|  | 4813 | if (mOutDevice != value) { | 
|  | 4814 | uint32_t params = 0; | 
|  | 4815 | // check whether speaker is on | 
|  | 4816 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
|  | 4817 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4818 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4819 |  | 
|  | 4820 | audio_devices_t deviceWithoutSpeaker | 
|  | 4821 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4822 | // check if any other device (except speaker) is on | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4823 | if (value & deviceWithoutSpeaker) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4824 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
|  | 4825 | } | 
|  | 4826 |  | 
|  | 4827 | if (params != 0) { | 
|  | 4828 | addBatteryData(params); | 
|  | 4829 | } | 
|  | 4830 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4831 | #endif | 
|  | 4832 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4833 | // forward device change to effects that have requested to be | 
|  | 4834 | // aware of attached audio device. | 
|  | 4835 | if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4836 | a2dpDeviceChanged = | 
|  | 4837 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4838 | mOutDevice = value; | 
|  | 4839 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 4840 | mEffectChains[i]->setDevice_l(mOutDevice); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4841 | } | 
|  | 4842 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4843 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4844 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4845 | if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4846 | status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4847 | if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4848 | mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4849 | mStandby = true; | 
|  | 4850 | mBytesWritten = 0; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4851 | status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4852 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4853 | if (status == NO_ERROR && reconfig) { | 
|  | 4854 | readOutputParameters_l(); | 
|  | 4855 | delete mAudioMixer; | 
|  | 4856 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
|  | 4857 | for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4858 | int name = getTrackName_l(mTracks[i]->mChannelMask, | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4859 | mTracks[i]->mFormat, mTracks[i]->mSessionId, mTracks[i]->uid()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4860 | if (name < 0) { | 
|  | 4861 | break; | 
|  | 4862 | } | 
|  | 4863 | mTracks[i]->mName = name; | 
|  | 4864 | } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4865 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4866 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4867 | } | 
|  | 4868 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4869 | return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4870 | } | 
|  | 4871 |  | 
|  | 4872 |  | 
|  | 4873 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 4874 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4875 | PlaybackThread::dumpInternals(fd, args); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4876 | dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); | 
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 4877 | dprintf(fd, "  AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4878 | dprintf(fd, "  Master mono: %s\n", mMasterMono ? "on" : "off"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4879 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4880 | if (hasFastMixer()) { | 
|  | 4881 | dprintf(fd, "  FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); | 
|  | 4882 |  | 
|  | 4883 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us | 
|  | 4884 | // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
|  | 4885 | // This is a large object so we place it on the heap. | 
|  | 4886 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
|  | 4887 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); | 
|  | 4888 | copy->dump(fd); | 
|  | 4889 | delete copy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4890 |  | 
|  | 4891 | #ifdef STATE_QUEUE_DUMP | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4892 | // Similar for state queue | 
|  | 4893 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
|  | 4894 | observerCopy.dump(fd); | 
|  | 4895 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
|  | 4896 | mutatorCopy.dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4897 | #endif | 
|  | 4898 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4899 | #ifdef AUDIO_WATCHDOG | 
|  | 4900 | if (mAudioWatchdog != 0) { | 
|  | 4901 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
|  | 4902 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
|  | 4903 | wdCopy.dump(fd); | 
|  | 4904 | } | 
|  | 4905 | #endif | 
|  | 4906 |  | 
|  | 4907 | } else { | 
|  | 4908 | dprintf(fd, "  No FastMixer\n"); | 
|  | 4909 | } | 
|  | 4910 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4911 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4912 | // Write the tee output to a .wav file | 
| Glenn Kasten | 5b2191a | 2016-08-19 11:44:47 -0700 | [diff] [blame] | 4913 | dumpTee(fd, mTeeSource, mId, 'M'); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4914 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4915 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4916 | } | 
|  | 4917 |  | 
|  | 4918 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const | 
|  | 4919 | { | 
|  | 4920 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
|  | 4921 | } | 
|  | 4922 |  | 
|  | 4923 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const | 
|  | 4924 | { | 
|  | 4925 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 4926 | } | 
|  | 4927 |  | 
|  | 4928 | void AudioFlinger::MixerThread::cacheParameters_l() | 
|  | 4929 | { | 
|  | 4930 | PlaybackThread::cacheParameters_l(); | 
|  | 4931 |  | 
|  | 4932 | // FIXME: Relaxed timing because of a certain device that can't meet latency | 
|  | 4933 | // Should be reduced to 2x after the vendor fixes the driver issue | 
|  | 4934 | // increase threshold again due to low power audio mode. The way this warning | 
|  | 4935 | // threshold is calculated and its usefulness should be reconsidered anyway. | 
|  | 4936 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
|  | 4937 | } | 
|  | 4938 |  | 
|  | 4939 | // ---------------------------------------------------------------------------- | 
|  | 4940 |  | 
|  | 4941 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4942 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) | 
|  | 4943 | :   PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4944 | { | 
|  | 4945 | } | 
|  | 4946 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4947 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 4948 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4949 | ThreadBase::type_t type, bool systemReady) | 
|  | 4950 | :   PlaybackThread(audioFlinger, output, id, device, type, systemReady) | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4951 | , mVolumeShaperActive(false) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4952 | { | 
|  | 4953 | } | 
|  | 4954 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4955 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
|  | 4956 | { | 
|  | 4957 | } | 
|  | 4958 |  | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4959 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4960 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4961 | float left, right; | 
|  | 4962 |  | 
|  | 4963 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { | 
|  | 4964 | left = right = 0; | 
|  | 4965 | } else { | 
|  | 4966 | float typeVolume = mStreamTypes[track->streamType()].volume; | 
|  | 4967 | float v = mMasterVolume * typeVolume; | 
| Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4968 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4969 |  | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4970 | // Get volumeshaper scaling | 
|  | 4971 | std::pair<float /* volume */, bool /* active */> | 
|  | 4972 | vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4973 | track->mAudioTrackServerProxy->framesReleased()); | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4974 | v *= vh.first; | 
|  | 4975 | mVolumeShaperActive = vh.second; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4976 |  | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4977 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
|  | 4978 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
|  | 4979 | if (left > GAIN_FLOAT_UNITY) { | 
|  | 4980 | left = GAIN_FLOAT_UNITY; | 
|  | 4981 | } | 
|  | 4982 | left *= v; | 
|  | 4983 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
|  | 4984 | if (right > GAIN_FLOAT_UNITY) { | 
|  | 4985 | right = GAIN_FLOAT_UNITY; | 
|  | 4986 | } | 
|  | 4987 | right *= v; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4988 | } | 
|  | 4989 |  | 
|  | 4990 | if (lastTrack) { | 
|  | 4991 | if (left != mLeftVolFloat || right != mRightVolFloat) { | 
|  | 4992 | mLeftVolFloat = left; | 
|  | 4993 | mRightVolFloat = right; | 
|  | 4994 |  | 
|  | 4995 | // Convert volumes from float to 8.24 | 
|  | 4996 | uint32_t vl = (uint32_t)(left * (1 << 24)); | 
|  | 4997 | uint32_t vr = (uint32_t)(right * (1 << 24)); | 
|  | 4998 |  | 
|  | 4999 | // Delegate volume control to effect in track effect chain if needed | 
|  | 5000 | // only one effect chain can be present on DirectOutputThread, so if | 
|  | 5001 | // there is one, the track is connected to it | 
|  | 5002 | if (!mEffectChains.isEmpty()) { | 
|  | 5003 | mEffectChains[0]->setVolume_l(&vl, &vr); | 
|  | 5004 | left = (float)vl / (1 << 24); | 
|  | 5005 | right = (float)vr / (1 << 24); | 
|  | 5006 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5007 | status_t result = mOutput->stream->setVolume(left, right); | 
|  | 5008 | ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5009 | } | 
|  | 5010 | } | 
|  | 5011 | } | 
|  | 5012 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5013 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() | 
|  | 5014 | { | 
|  | 5015 | sp<Track> previousTrack = mPreviousTrack.promote(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5016 | sp<Track> latestTrack = mActiveTracks.getLatest(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5017 |  | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5018 | if (previousTrack != 0 && latestTrack != 0) { | 
|  | 5019 | if (mType == DIRECT) { | 
|  | 5020 | if (previousTrack.get() != latestTrack.get()) { | 
|  | 5021 | mFlushPending = true; | 
|  | 5022 | } | 
|  | 5023 | } else /* mType == OFFLOAD */ { | 
|  | 5024 | if (previousTrack->sessionId() != latestTrack->sessionId()) { | 
|  | 5025 | mFlushPending = true; | 
|  | 5026 | } | 
|  | 5027 | } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5028 | } | 
|  | 5029 | PlaybackThread::onAddNewTrack_l(); | 
|  | 5030 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5031 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5032 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( | 
|  | 5033 | Vector< sp<Track> > *tracksToRemove | 
|  | 5034 | ) | 
|  | 5035 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5036 | size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5037 | mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5038 | bool doHwPause = false; | 
|  | 5039 | bool doHwResume = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5040 |  | 
|  | 5041 | // find out which tracks need to be processed | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5042 | for (const sp<Track> &t : mActiveTracks) { | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5043 | if (t->isInvalid()) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5044 | ALOGW("An invalidated track shouldn't be in active list"); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5045 | tracksToRemove->add(t); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5046 | continue; | 
|  | 5047 | } | 
|  | 5048 |  | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5049 | Track* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5050 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5051 | audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5052 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5053 | // Only consider last track started for volume and mixer state control. | 
|  | 5054 | // In theory an older track could underrun and restart after the new one starts | 
|  | 5055 | // but as we only care about the transition phase between two tracks on a | 
|  | 5056 | // direct output, it is not a problem to ignore the underrun case. | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5057 | sp<Track> l = mActiveTracks.getLatest(); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5058 | bool last = l.get() == track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5059 |  | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5060 | if (track->isPausing()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5061 | track->setPaused(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5062 | if (mHwSupportsPause && last && !mHwPaused) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5063 | doHwPause = true; | 
|  | 5064 | mHwPaused = true; | 
|  | 5065 | } | 
|  | 5066 | tracksToRemove->add(track); | 
|  | 5067 | } else if (track->isFlushPending()) { | 
|  | 5068 | track->flushAck(); | 
|  | 5069 | if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5070 | mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5071 | } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5072 | } else if (track->isResumePending()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5073 | track->resumeAck(); | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5074 | if (last) { | 
|  | 5075 | mLeftVolFloat = mRightVolFloat = -1.0; | 
|  | 5076 | if (mHwPaused) { | 
|  | 5077 | doHwResume = true; | 
|  | 5078 | mHwPaused = false; | 
|  | 5079 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5080 | } | 
|  | 5081 | } | 
|  | 5082 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5083 | // The first time a track is added we wait | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5084 | // for all its buffers to be filled before processing it. | 
|  | 5085 | // Allow draining the buffer in case the client | 
|  | 5086 | // app does not call stop() and relies on underrun to stop: | 
|  | 5087 | // hence the test on (track->mRetryCount > 1). | 
|  | 5088 | // If retryCount<=1 then track is about to underrun and be removed. | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5089 | // Do not use a high threshold for compressed audio. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5090 | uint32_t minFrames; | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5091 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5092 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5093 | minFrames = mNormalFrameCount; | 
|  | 5094 | } else { | 
|  | 5095 | minFrames = 1; | 
|  | 5096 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5097 |  | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5098 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && | 
|  | 5099 | !track->isStopping_2() && !track->isStopped()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5100 | { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5101 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5102 |  | 
|  | 5103 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 5104 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5105 | if (last) { | 
|  | 5106 | // make sure processVolume_l() will apply new volume even if 0 | 
|  | 5107 | mLeftVolFloat = mRightVolFloat = -1.0; | 
|  | 5108 | } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5109 | if (!mHwSupportsPause) { | 
|  | 5110 | track->resumeAck(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5111 | } | 
|  | 5112 | } | 
|  | 5113 |  | 
|  | 5114 | // compute volume for this track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5115 | processVolume_l(track, last); | 
|  | 5116 | if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5117 | sp<Track> previousTrack = mPreviousTrack.promote(); | 
|  | 5118 | if (previousTrack != 0) { | 
|  | 5119 | if (track != previousTrack.get()) { | 
|  | 5120 | // Flush any data still being written from last track | 
|  | 5121 | mBytesRemaining = 0; | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5122 | // Invalidate previous track to force a seek when resuming. | 
|  | 5123 | previousTrack->invalidate(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5124 | } | 
|  | 5125 | } | 
|  | 5126 | mPreviousTrack = track; | 
|  | 5127 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5128 | // reset retry count | 
|  | 5129 | track->mRetryCount = kMaxTrackRetriesDirect; | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5130 | mActiveTrack = t; | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5131 | mixerStatus = MIXER_TRACKS_READY; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5132 | if (mHwPaused) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5133 | doHwResume = true; | 
|  | 5134 | mHwPaused = false; | 
|  | 5135 | } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5136 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5137 | } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5138 | // clear effect chain input buffer if the last active track started underruns | 
|  | 5139 | // to avoid sending previous audio buffer again to effects | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5140 | if (!mEffectChains.isEmpty() && last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5141 | mEffectChains[0]->clearInputBuffer(); | 
|  | 5142 | } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5143 | if (track->isStopping_1()) { | 
|  | 5144 | track->mState = TrackBase::STOPPING_2; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5145 | if (last && mHwPaused) { | 
|  | 5146 | doHwResume = true; | 
|  | 5147 | mHwPaused = false; | 
|  | 5148 | } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5149 | } | 
|  | 5150 | if ((track->sharedBuffer() != 0) || track->isStopped() || | 
|  | 5151 | track->isStopping_2() || track->isPaused()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5152 | // We have consumed all the buffers of this track. | 
|  | 5153 | // Remove it from the list of active tracks. | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5154 | size_t audioHALFrames; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5155 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5156 | audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
|  | 5157 | } else { | 
|  | 5158 | audioHALFrames = 0; | 
|  | 5159 | } | 
|  | 5160 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5161 | int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5162 | if (mStandby || !last || | 
|  | 5163 | track->presentationComplete(framesWritten, audioHALFrames)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5164 | if (track->isStopping_2()) { | 
|  | 5165 | track->mState = TrackBase::STOPPED; | 
|  | 5166 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5167 | if (track->isStopped()) { | 
|  | 5168 | track->reset(); | 
|  | 5169 | } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5170 | tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5171 | } | 
|  | 5172 | } else { | 
|  | 5173 | // No buffers for this track. Give it a few chances to | 
|  | 5174 | // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5175 | // Only consider last track started for mixer state control | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5176 | if (--(track->mRetryCount) <= 0) { | 
|  | 5177 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5178 | tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5179 | // indicate to client process that the track was disabled because of underrun; | 
|  | 5180 | // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5181 | track->disable(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5182 | } else if (last) { | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5183 | ALOGW("pause because of UNDERRUN, framesReady = %zu," | 
|  | 5184 | "minFrames = %u, mFormat = %#x", | 
|  | 5185 | track->framesReady(), minFrames, mFormat); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5186 | mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5187 | if (mHwSupportsPause && !mHwPaused && !mStandby) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5188 | doHwPause = true; | 
|  | 5189 | mHwPaused = true; | 
|  | 5190 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5191 | } | 
|  | 5192 | } | 
|  | 5193 | } | 
|  | 5194 | } | 
|  | 5195 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5196 | // 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] | 5197 | if (!mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5198 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 5199 | if (mTracks[i]->isFlushPending()) { | 
|  | 5200 | mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5201 | mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5202 | } | 
|  | 5203 | } | 
|  | 5204 | } | 
|  | 5205 |  | 
|  | 5206 | // make sure the pause/flush/resume sequence is executed in the right order. | 
|  | 5207 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
|  | 5208 | // before flush and then resume HW. This can happen in case of pause/flush/resume | 
|  | 5209 | // if resume is received before pause is executed. | 
|  | 5210 | if (mHwSupportsPause && !mStandby && | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5211 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5212 | status_t result = mOutput->stream->pause(); | 
|  | 5213 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5214 | } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5215 | if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5216 | flushHw_l(); | 
|  | 5217 | } | 
|  | 5218 | if (mHwSupportsPause && !mStandby && doHwResume) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5219 | status_t result = mOutput->stream->resume(); | 
|  | 5220 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5221 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5222 | // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5223 | removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5224 |  | 
|  | 5225 | return mixerStatus; | 
|  | 5226 | } | 
|  | 5227 |  | 
|  | 5228 | void AudioFlinger::DirectOutputThread::threadLoop_mix() | 
|  | 5229 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5230 | size_t frameCount = mFrameCount; | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5231 | int8_t *curBuf = (int8_t *)mSinkBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5232 | // output audio to hardware | 
|  | 5233 | while (frameCount) { | 
| Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5234 | AudioBufferProvider::Buffer buffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5235 | buffer.frameCount = frameCount; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5236 | status_t status = mActiveTrack->getNextBuffer(&buffer); | 
|  | 5237 | if (status != NO_ERROR || buffer.raw == NULL) { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5238 | // no need to pad with 0 for compressed audio | 
|  | 5239 | if (audio_has_proportional_frames(mFormat)) { | 
|  | 5240 | memset(curBuf, 0, frameCount * mFrameSize); | 
|  | 5241 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5242 | break; | 
|  | 5243 | } | 
|  | 5244 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
|  | 5245 | frameCount -= buffer.frameCount; | 
|  | 5246 | curBuf += buffer.frameCount * mFrameSize; | 
|  | 5247 | mActiveTrack->releaseBuffer(&buffer); | 
|  | 5248 | } | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5249 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5250 | mSleepTimeUs = 0; | 
|  | 5251 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5252 | mActiveTrack.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5253 | } | 
|  | 5254 |  | 
|  | 5255 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() | 
|  | 5256 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5257 | // do not write to HAL when paused | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5258 | if (mHwPaused || (usesHwAvSync() && mStandby)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5259 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5260 | return; | 
|  | 5261 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5262 | if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5264 | mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5265 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5266 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5267 | } | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5268 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5269 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5270 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5271 | } | 
|  | 5272 | } | 
|  | 5273 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5274 | void AudioFlinger::DirectOutputThread::threadLoop_exit() | 
|  | 5275 | { | 
|  | 5276 | { | 
|  | 5277 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5278 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 5279 | if (mTracks[i]->isFlushPending()) { | 
|  | 5280 | mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5281 | mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5282 | } | 
|  | 5283 | } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5284 | if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5285 | flushHw_l(); | 
|  | 5286 | } | 
|  | 5287 | } | 
|  | 5288 | PlaybackThread::threadLoop_exit(); | 
|  | 5289 | } | 
|  | 5290 |  | 
|  | 5291 | // must be called with thread mutex locked | 
|  | 5292 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() | 
|  | 5293 | { | 
|  | 5294 | bool trackPaused = false; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5295 | bool trackStopped = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5296 |  | 
| vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5297 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { | 
|  | 5298 | return !mStandby; | 
|  | 5299 | } | 
|  | 5300 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5301 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack | 
|  | 5302 | // after a timeout and we will enter standby then. | 
|  | 5303 | if (mTracks.size() > 0) { | 
|  | 5304 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5305 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || | 
|  | 5306 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5307 | } | 
|  | 5308 |  | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5309 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5310 | } | 
|  | 5311 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5312 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5313 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5314 | audio_format_t format __unused, audio_session_t sessionId __unused, uid_t uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5315 | { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5316 | if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { | 
|  | 5317 | return -1; | 
|  | 5318 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5319 | return 0; | 
|  | 5320 | } | 
|  | 5321 |  | 
|  | 5322 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5323 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5324 | { | 
|  | 5325 | } | 
|  | 5326 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5327 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
|  | 5328 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 5329 | status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5330 | { | 
|  | 5331 | bool reconfig = false; | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5332 | bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5333 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5334 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5335 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5336 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 5337 | int value; | 
|  | 5338 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 5339 | // forward device change to effects that have requested to be | 
|  | 5340 | // aware of attached audio device. | 
|  | 5341 | if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5342 | a2dpDeviceChanged = | 
|  | 5343 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5344 | mOutDevice = value; | 
|  | 5345 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 5346 | mEffectChains[i]->setDevice_l(mOutDevice); | 
| Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5347 | } | 
|  | 5348 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5349 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5350 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 5351 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 5352 | // size depends on frame count and correct behavior would not be garantied | 
|  | 5353 | // if frame count is changed after track creation | 
|  | 5354 | if (!mTracks.isEmpty()) { | 
|  | 5355 | status = INVALID_OPERATION; | 
|  | 5356 | } else { | 
|  | 5357 | reconfig = true; | 
|  | 5358 | } | 
|  | 5359 | } | 
|  | 5360 | if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5361 | status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5362 | if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5363 | mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5364 | mStandby = true; | 
|  | 5365 | mBytesWritten = 0; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5366 | status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5367 | } | 
|  | 5368 | if (status == NO_ERROR && reconfig) { | 
|  | 5369 | readOutputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5370 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5371 | } | 
|  | 5372 | } | 
|  | 5373 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5374 | return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5375 | } | 
|  | 5376 |  | 
|  | 5377 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const | 
|  | 5378 | { | 
|  | 5379 | uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5380 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5381 | time = PlaybackThread::activeSleepTimeUs(); | 
|  | 5382 | } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5383 | time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5384 | } | 
|  | 5385 | return time; | 
|  | 5386 | } | 
|  | 5387 |  | 
|  | 5388 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const | 
|  | 5389 | { | 
|  | 5390 | uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5391 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5392 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
|  | 5393 | } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5394 | time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5395 | } | 
|  | 5396 | return time; | 
|  | 5397 | } | 
|  | 5398 |  | 
|  | 5399 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const | 
|  | 5400 | { | 
|  | 5401 | uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5402 | if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5403 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 5404 | } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5405 | time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5406 | } | 
|  | 5407 | return time; | 
|  | 5408 | } | 
|  | 5409 |  | 
|  | 5410 | void AudioFlinger::DirectOutputThread::cacheParameters_l() | 
|  | 5411 | { | 
|  | 5412 | PlaybackThread::cacheParameters_l(); | 
|  | 5413 |  | 
|  | 5414 | // use shorter standby delay as on normal output to release | 
|  | 5415 | // hardware resources as soon as possible | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5416 | // no delay on outputs with HW A/V sync | 
|  | 5417 | if (usesHwAvSync()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5418 | mStandbyDelayNs = 0; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5419 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5420 | mStandbyDelayNs = kOffloadStandbyDelayNs; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5421 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5422 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5423 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5424 | } | 
|  | 5425 |  | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5426 | void AudioFlinger::DirectOutputThread::flushHw_l() | 
|  | 5427 | { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5428 | mOutput->flush(); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5429 | mHwPaused = false; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5430 | mFlushPending = false; | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5431 | } | 
|  | 5432 |  | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5433 | int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const { | 
|  | 5434 | // If a VolumeShaper is active, we must wake up periodically to update volume. | 
|  | 5435 | const int64_t NS_PER_MS = 1000000; | 
|  | 5436 | return mVolumeShaperActive ? | 
|  | 5437 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); | 
|  | 5438 | } | 
|  | 5439 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5440 | // ---------------------------------------------------------------------------- | 
|  | 5441 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5442 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5443 | const wp<AudioFlinger::PlaybackThread>& playbackThread) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5444 | :   Thread(false /*canCallJava*/), | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5445 | mPlaybackThread(playbackThread), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5446 | mWriteAckSequence(0), | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5447 | mDrainSequence(0), | 
|  | 5448 | mAsyncError(false) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5449 | { | 
|  | 5450 | } | 
|  | 5451 |  | 
|  | 5452 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() | 
|  | 5453 | { | 
|  | 5454 | } | 
|  | 5455 |  | 
|  | 5456 | void AudioFlinger::AsyncCallbackThread::onFirstRef() | 
|  | 5457 | { | 
|  | 5458 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); | 
|  | 5459 | } | 
|  | 5460 |  | 
|  | 5461 | bool AudioFlinger::AsyncCallbackThread::threadLoop() | 
|  | 5462 | { | 
|  | 5463 | while (!exitPending()) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5464 | uint32_t writeAckSequence; | 
|  | 5465 | uint32_t drainSequence; | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5466 | bool asyncError; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5467 |  | 
|  | 5468 | { | 
|  | 5469 | Mutex::Autolock _l(mLock); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5470 | while (!((mWriteAckSequence & 1) || | 
|  | 5471 | (mDrainSequence & 1) || | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5472 | mAsyncError || | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5473 | exitPending())) { | 
|  | 5474 | mWaitWorkCV.wait(mLock); | 
|  | 5475 | } | 
|  | 5476 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5477 | if (exitPending()) { | 
|  | 5478 | break; | 
|  | 5479 | } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5480 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", | 
|  | 5481 | mWriteAckSequence, mDrainSequence); | 
|  | 5482 | writeAckSequence = mWriteAckSequence; | 
|  | 5483 | mWriteAckSequence &= ~1; | 
|  | 5484 | drainSequence = mDrainSequence; | 
|  | 5485 | mDrainSequence &= ~1; | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5486 | asyncError = mAsyncError; | 
|  | 5487 | mAsyncError = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5488 | } | 
|  | 5489 | { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5490 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); | 
|  | 5491 | if (playbackThread != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5492 | if (writeAckSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5493 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5494 | } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5495 | if (drainSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5496 | playbackThread->resetDraining(drainSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5497 | } | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5498 | if (asyncError) { | 
|  | 5499 | playbackThread->onAsyncError(); | 
|  | 5500 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5501 | } | 
|  | 5502 | } | 
|  | 5503 | } | 
|  | 5504 | return false; | 
|  | 5505 | } | 
|  | 5506 |  | 
|  | 5507 | void AudioFlinger::AsyncCallbackThread::exit() | 
|  | 5508 | { | 
|  | 5509 | ALOGV("AsyncCallbackThread::exit"); | 
|  | 5510 | Mutex::Autolock _l(mLock); | 
|  | 5511 | requestExit(); | 
|  | 5512 | mWaitWorkCV.broadcast(); | 
|  | 5513 | } | 
|  | 5514 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5515 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5516 | { | 
|  | 5517 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5518 | // bit 0 is cleared | 
|  | 5519 | mWriteAckSequence = sequence << 1; | 
|  | 5520 | } | 
|  | 5521 |  | 
|  | 5522 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() | 
|  | 5523 | { | 
|  | 5524 | Mutex::Autolock _l(mLock); | 
|  | 5525 | // ignore unexpected callbacks | 
|  | 5526 | if (mWriteAckSequence & 2) { | 
|  | 5527 | mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5528 | mWaitWorkCV.signal(); | 
|  | 5529 | } | 
|  | 5530 | } | 
|  | 5531 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5532 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5533 | { | 
|  | 5534 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5535 | // bit 0 is cleared | 
|  | 5536 | mDrainSequence = sequence << 1; | 
|  | 5537 | } | 
|  | 5538 |  | 
|  | 5539 | void AudioFlinger::AsyncCallbackThread::resetDraining() | 
|  | 5540 | { | 
|  | 5541 | Mutex::Autolock _l(mLock); | 
|  | 5542 | // ignore unexpected callbacks | 
|  | 5543 | if (mDrainSequence & 2) { | 
|  | 5544 | mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5545 | mWaitWorkCV.signal(); | 
|  | 5546 | } | 
|  | 5547 | } | 
|  | 5548 |  | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5549 | void AudioFlinger::AsyncCallbackThread::setAsyncError() | 
|  | 5550 | { | 
|  | 5551 | Mutex::Autolock _l(mLock); | 
|  | 5552 | mAsyncError = true; | 
|  | 5553 | mWaitWorkCV.signal(); | 
|  | 5554 | } | 
|  | 5555 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5556 |  | 
|  | 5557 | // ---------------------------------------------------------------------------- | 
|  | 5558 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5559 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) | 
|  | 5560 | :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5561 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), | 
|  | 5562 | mOffloadUnderrunPosition(~0LL) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5563 | { | 
| Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 5564 | //FIXME: mStandby should be set to true by ThreadBase constructo | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5565 | mStandby = true; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5566 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5567 | } | 
|  | 5568 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5569 | void AudioFlinger::OffloadThread::threadLoop_exit() | 
|  | 5570 | { | 
|  | 5571 | if (mFlushPending || mHwPaused) { | 
|  | 5572 | // If a flush is pending or track was paused, just discard buffered data | 
|  | 5573 | flushHw_l(); | 
|  | 5574 | } else { | 
|  | 5575 | mMixerStatus = MIXER_DRAIN_ALL; | 
|  | 5576 | threadLoop_drain(); | 
|  | 5577 | } | 
| Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5578 | if (mUseAsyncWrite) { | 
|  | 5579 | ALOG_ASSERT(mCallbackThread != 0); | 
|  | 5580 | mCallbackThread->exit(); | 
|  | 5581 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5582 | PlaybackThread::threadLoop_exit(); | 
|  | 5583 | } | 
|  | 5584 |  | 
|  | 5585 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( | 
|  | 5586 | Vector< sp<Track> > *tracksToRemove | 
|  | 5587 | ) | 
|  | 5588 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5589 | size_t count = mActiveTracks.size(); | 
|  | 5590 |  | 
|  | 5591 | mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5592 | bool doHwPause = false; | 
|  | 5593 | bool doHwResume = false; | 
|  | 5594 |  | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5595 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5596 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5597 | // find out which tracks need to be processed | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5598 | for (const sp<Track> &t : mActiveTracks) { | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5599 | Track* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5600 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5601 | audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5602 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5603 | // Only consider last track started for volume and mixer state control. | 
|  | 5604 | // In theory an older track could underrun and restart after the new one starts | 
|  | 5605 | // but as we only care about the transition phase between two tracks on a | 
|  | 5606 | // direct output, it is not a problem to ignore the underrun case. | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5607 | sp<Track> l = mActiveTracks.getLatest(); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5608 | bool last = l.get() == track; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5609 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5610 | if (track->isInvalid()) { | 
|  | 5611 | ALOGW("An invalidated track shouldn't be in active list"); | 
|  | 5612 | tracksToRemove->add(track); | 
|  | 5613 | continue; | 
|  | 5614 | } | 
|  | 5615 |  | 
|  | 5616 | if (track->mState == TrackBase::IDLE) { | 
|  | 5617 | ALOGW("An idle track shouldn't be in active list"); | 
|  | 5618 | continue; | 
|  | 5619 | } | 
|  | 5620 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5621 | if (track->isPausing()) { | 
|  | 5622 | track->setPaused(); | 
|  | 5623 | if (last) { | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5624 | if (mHwSupportsPause && !mHwPaused) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5625 | doHwPause = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5626 | mHwPaused = true; | 
|  | 5627 | } | 
|  | 5628 | // If we were part way through writing the mixbuffer to | 
|  | 5629 | // the HAL we must save this until we resume | 
|  | 5630 | // BUG - this will be wrong if a different track is made active, | 
|  | 5631 | // in that case we want to discard the pending data in the | 
|  | 5632 | // mixbuffer and tell the client to present it again when the | 
|  | 5633 | // track is resumed | 
|  | 5634 | mPausedWriteLength = mCurrentWriteLength; | 
|  | 5635 | mPausedBytesRemaining = mBytesRemaining; | 
|  | 5636 | mBytesRemaining = 0;    // stop writing | 
|  | 5637 | } | 
|  | 5638 | tracksToRemove->add(track); | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5639 | } else if (track->isFlushPending()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5640 | if (track->isStopping_1()) { | 
|  | 5641 | track->mRetryCount = kMaxTrackStopRetriesOffload; | 
|  | 5642 | } else { | 
|  | 5643 | track->mRetryCount = kMaxTrackRetriesOffload; | 
|  | 5644 | } | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5645 | track->flushAck(); | 
|  | 5646 | if (last) { | 
|  | 5647 | mFlushPending = true; | 
|  | 5648 | } | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5649 | } else if (track->isResumePending()){ | 
|  | 5650 | track->resumeAck(); | 
|  | 5651 | if (last) { | 
|  | 5652 | if (mPausedBytesRemaining) { | 
|  | 5653 | // Need to continue write that was interrupted | 
|  | 5654 | mCurrentWriteLength = mPausedWriteLength; | 
|  | 5655 | mBytesRemaining = mPausedBytesRemaining; | 
|  | 5656 | mPausedBytesRemaining = 0; | 
|  | 5657 | } | 
|  | 5658 | if (mHwPaused) { | 
|  | 5659 | doHwResume = true; | 
|  | 5660 | mHwPaused = false; | 
|  | 5661 | // threadLoop_mix() will handle the case that we need to | 
|  | 5662 | // resume an interrupted write | 
|  | 5663 | } | 
|  | 5664 | // enable write to audio HAL | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5665 | mSleepTimeUs = 0; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5666 |  | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5667 | mLeftVolFloat = mRightVolFloat = -1.0; | 
|  | 5668 |  | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5669 | // Do not handle new data in this iteration even if track->framesReady() | 
|  | 5670 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 5671 | } | 
|  | 5672 | }  else if (track->framesReady() && track->isReady() && | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5673 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5674 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5675 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 5676 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5677 | if (last) { | 
|  | 5678 | // make sure processVolume_l() will apply new volume even if 0 | 
|  | 5679 | mLeftVolFloat = mRightVolFloat = -1.0; | 
|  | 5680 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5681 | } | 
|  | 5682 |  | 
|  | 5683 | if (last) { | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5684 | sp<Track> previousTrack = mPreviousTrack.promote(); | 
|  | 5685 | if (previousTrack != 0) { | 
|  | 5686 | if (track != previousTrack.get()) { | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5687 | // Flush any data still being written from last track | 
|  | 5688 | mBytesRemaining = 0; | 
|  | 5689 | if (mPausedBytesRemaining) { | 
|  | 5690 | // Last track was paused so we also need to flush saved | 
|  | 5691 | // mixbuffer state and invalidate track so that it will | 
|  | 5692 | // re-submit that unwritten data when it is next resumed | 
|  | 5693 | mPausedBytesRemaining = 0; | 
|  | 5694 | // Invalidate is a bit drastic - would be more efficient | 
|  | 5695 | // to have a flag to tell client that some of the | 
|  | 5696 | // previously written data was lost | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5697 | previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5698 | } | 
|  | 5699 | // flush data already sent to the DSP if changing audio session as audio | 
|  | 5700 | // comes from a different source. Also invalidate previous track to force a | 
|  | 5701 | // seek when resuming. | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5702 | if (previousTrack->sessionId() != track->sessionId()) { | 
|  | 5703 | previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5704 | } | 
|  | 5705 | } | 
|  | 5706 | } | 
|  | 5707 | mPreviousTrack = track; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5708 | // reset retry count | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5709 | if (track->isStopping_1()) { | 
|  | 5710 | track->mRetryCount = kMaxTrackStopRetriesOffload; | 
|  | 5711 | } else { | 
|  | 5712 | track->mRetryCount = kMaxTrackRetriesOffload; | 
|  | 5713 | } | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5714 | mActiveTrack = t; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5715 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 5716 | } | 
|  | 5717 | } else { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5718 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5719 | if (track->isStopping_1()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5720 | if (--(track->mRetryCount) <= 0) { | 
|  | 5721 | // Hardware buffer can hold a large amount of audio so we must | 
|  | 5722 | // wait for all current track's data to drain before we say | 
|  | 5723 | // that the track is stopped. | 
|  | 5724 | if (mBytesRemaining == 0) { | 
|  | 5725 | // Only start draining when all data in mixbuffer | 
|  | 5726 | // has been written | 
|  | 5727 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); | 
|  | 5728 | track->mState = TrackBase::STOPPING_2; // so presentation completes after | 
|  | 5729 | // drain do not drain if no data was ever sent to HAL (mStandby == true) | 
|  | 5730 | if (last && !mStandby) { | 
|  | 5731 | // do not modify drain sequence if we are already draining. This happens | 
|  | 5732 | // when resuming from pause after drain. | 
|  | 5733 | if ((mDrainSequence & 1) == 0) { | 
|  | 5734 | mSleepTimeUs = 0; | 
|  | 5735 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
|  | 5736 | mixerStatus = MIXER_DRAIN_TRACK; | 
|  | 5737 | mDrainSequence += 2; | 
|  | 5738 | } | 
|  | 5739 | if (mHwPaused) { | 
|  | 5740 | // It is possible to move from PAUSED to STOPPING_1 without | 
|  | 5741 | // a resume so we must ensure hardware is running | 
|  | 5742 | doHwResume = true; | 
|  | 5743 | mHwPaused = false; | 
|  | 5744 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5745 | } | 
|  | 5746 | } | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5747 | } else if (last) { | 
|  | 5748 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); | 
|  | 5749 | mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5750 | } | 
|  | 5751 | } else if (track->isStopping_2()) { | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5752 | // Drain has completed or we are in standby, signal presentation complete | 
|  | 5753 | if (!(mDrainSequence & 1) || !last || mStandby) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5754 | track->mState = TrackBase::STOPPED; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5755 | uint32_t latency = 0; | 
|  | 5756 | status_t result = mOutput->stream->getLatency(&latency); | 
|  | 5757 | ALOGE_IF(result != OK, | 
|  | 5758 | "Error when retrieving output stream latency: %d", result); | 
|  | 5759 | size_t audioHALFrames = (latency * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5760 | int64_t framesWritten = | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5761 | mBytesWritten / mOutput->getFrameSize(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5762 | track->presentationComplete(framesWritten, audioHALFrames); | 
|  | 5763 | track->reset(); | 
|  | 5764 | tracksToRemove->add(track); | 
|  | 5765 | } | 
|  | 5766 | } else { | 
|  | 5767 | // No buffers for this track. Give it a few chances to | 
|  | 5768 | // fill a buffer, then remove it from active list. | 
|  | 5769 | if (--(track->mRetryCount) <= 0) { | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5770 | bool running = false; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5771 | uint64_t position = 0; | 
|  | 5772 | struct timespec unused; | 
|  | 5773 | // The running check restarts the retry counter at least once. | 
|  | 5774 | status_t ret = mOutput->stream->getPresentationPosition(&position, &unused); | 
|  | 5775 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { | 
|  | 5776 | running = true; | 
|  | 5777 | mOffloadUnderrunPosition = position; | 
|  | 5778 | } | 
|  | 5779 | if (ret == NO_ERROR) { | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5780 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, | 
|  | 5781 | (long long)position, (long long)mOffloadUnderrunPosition); | 
|  | 5782 | } | 
|  | 5783 | if (running) { // still running, give us more time. | 
|  | 5784 | track->mRetryCount = kMaxTrackRetriesOffload; | 
|  | 5785 | } else { | 
|  | 5786 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", | 
|  | 5787 | track->name()); | 
|  | 5788 | tracksToRemove->add(track); | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 5789 | // tell client process that the track was disabled because of underrun; | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5790 | // it will then automatically call start() when data is available | 
|  | 5791 | track->disable(); | 
|  | 5792 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5793 | } else if (last){ | 
|  | 5794 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 5795 | } | 
|  | 5796 | } | 
|  | 5797 | } | 
|  | 5798 | // compute volume for this track | 
|  | 5799 | processVolume_l(track, last); | 
|  | 5800 | } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5801 |  | 
| Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5802 | // make sure the pause/flush/resume sequence is executed in the right order. | 
|  | 5803 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
|  | 5804 | // before flush and then resume HW. This can happen in case of pause/flush/resume | 
|  | 5805 | // if resume is received before pause is executed. | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5806 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5807 | status_t result = mOutput->stream->pause(); | 
|  | 5808 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5809 | } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5810 | if (mFlushPending) { | 
|  | 5811 | flushHw_l(); | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5812 | } | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5813 | if (!mStandby && doHwResume) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5814 | status_t result = mOutput->stream->resume(); | 
|  | 5815 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5816 | } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5817 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5818 | // remove all the tracks that need to be... | 
|  | 5819 | removeTracks_l(*tracksToRemove); | 
|  | 5820 |  | 
|  | 5821 | return mixerStatus; | 
|  | 5822 | } | 
|  | 5823 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5824 | // must be called with thread mutex locked | 
|  | 5825 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() | 
|  | 5826 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5827 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", | 
|  | 5828 | mWriteAckSequence, mDrainSequence); | 
|  | 5829 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5830 | return true; | 
|  | 5831 | } | 
|  | 5832 | return false; | 
|  | 5833 | } | 
|  | 5834 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5835 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() | 
|  | 5836 | { | 
|  | 5837 | Mutex::Autolock _l(mLock); | 
|  | 5838 | return waitingAsyncCallback_l(); | 
|  | 5839 | } | 
|  | 5840 |  | 
|  | 5841 | void AudioFlinger::OffloadThread::flushHw_l() | 
|  | 5842 | { | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5843 | DirectOutputThread::flushHw_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5844 | // Flush anything still waiting in the mixbuffer | 
|  | 5845 | mCurrentWriteLength = 0; | 
|  | 5846 | mBytesRemaining = 0; | 
|  | 5847 | mPausedWriteLength = 0; | 
|  | 5848 | mPausedBytesRemaining = 0; | 
| Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5849 | // reset bytes written count to reflect that DSP buffers are empty after flush. | 
|  | 5850 | mBytesWritten = 0; | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5851 | mOffloadUnderrunPosition = ~0LL; | 
| Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5852 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5853 | if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5854 | // discard any pending drain or write ack by incrementing sequence | 
|  | 5855 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
|  | 5856 | mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5857 | ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5858 | mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
|  | 5859 | mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5860 | } | 
|  | 5861 | } | 
|  | 5862 |  | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5863 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) | 
|  | 5864 | { | 
|  | 5865 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5866 | if (PlaybackThread::invalidateTracks_l(streamType)) { | 
|  | 5867 | mFlushPending = true; | 
|  | 5868 | } | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5869 | } | 
|  | 5870 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5871 | // ---------------------------------------------------------------------------- | 
|  | 5872 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5873 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5874 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5875 | :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5876 | systemReady, DUPLICATING), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5877 | mWaitTimeMs(UINT_MAX) | 
|  | 5878 | { | 
|  | 5879 | addOutputTrack(mainThread); | 
|  | 5880 | } | 
|  | 5881 |  | 
|  | 5882 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
|  | 5883 | { | 
|  | 5884 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 5885 | mOutputTracks[i]->destroy(); | 
|  | 5886 | } | 
|  | 5887 | } | 
|  | 5888 |  | 
|  | 5889 | void AudioFlinger::DuplicatingThread::threadLoop_mix() | 
|  | 5890 | { | 
|  | 5891 | // mix buffers... | 
|  | 5892 | if (outputsReady(outputTracks)) { | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5893 | mAudioMixer->process(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5894 | } else { | 
| Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5895 | if (mMixerBufferValid) { | 
|  | 5896 | memset(mMixerBuffer, 0, mMixerBufferSize); | 
|  | 5897 | } else { | 
|  | 5898 | memset(mSinkBuffer, 0, mSinkBufferSize); | 
|  | 5899 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5900 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5901 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5902 | writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5903 | mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5904 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5905 | } | 
|  | 5906 |  | 
|  | 5907 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() | 
|  | 5908 | { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5909 | if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5910 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5911 | mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5912 | } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5913 | mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5914 | } | 
|  | 5915 | } else if (mBytesWritten != 0) { | 
|  | 5916 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
|  | 5917 | writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5918 | memset(mSinkBuffer, 0, mSinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5919 | } else { | 
|  | 5920 | // flush remaining overflow buffers in output tracks | 
|  | 5921 | writeFrames = 0; | 
|  | 5922 | } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5923 | mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5924 | } | 
|  | 5925 | } | 
|  | 5926 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5927 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5928 | { | 
|  | 5929 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5930 | outputTracks[i]->write(mSinkBuffer, writeFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5931 | } | 
| Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5932 | mStandby = false; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5933 | return (ssize_t)mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5934 | } | 
|  | 5935 |  | 
|  | 5936 | void AudioFlinger::DuplicatingThread::threadLoop_standby() | 
|  | 5937 | { | 
|  | 5938 | // DuplicatingThread implements standby by stopping all tracks | 
|  | 5939 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 5940 | outputTracks[i]->stop(); | 
|  | 5941 | } | 
|  | 5942 | } | 
|  | 5943 |  | 
|  | 5944 | void AudioFlinger::DuplicatingThread::saveOutputTracks() | 
|  | 5945 | { | 
|  | 5946 | outputTracks = mOutputTracks; | 
|  | 5947 | } | 
|  | 5948 |  | 
|  | 5949 | void AudioFlinger::DuplicatingThread::clearOutputTracks() | 
|  | 5950 | { | 
|  | 5951 | outputTracks.clear(); | 
|  | 5952 | } | 
|  | 5953 |  | 
|  | 5954 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
|  | 5955 | { | 
|  | 5956 | Mutex::Autolock _l(mLock); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5957 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. | 
|  | 5958 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. | 
|  | 5959 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. | 
|  | 5960 | const size_t frameCount = | 
|  | 5961 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); | 
|  | 5962 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity | 
|  | 5963 | // from different OutputTracks and their associated MixerThreads (e.g. one may | 
|  | 5964 | // nearly empty and the other may be dropping data). | 
|  | 5965 |  | 
|  | 5966 | sp<OutputTrack> outputTrack = new OutputTrack(thread, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5967 | this, | 
|  | 5968 | mSampleRate, | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5969 | mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5970 | mChannelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5971 | frameCount, | 
|  | 5972 | IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5973 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; | 
|  | 5974 | if (status != NO_ERROR) { | 
|  | 5975 | ALOGE("addOutputTrack() initCheck failed %d", status); | 
|  | 5976 | return; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5977 | } | 
| Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5978 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); | 
|  | 5979 | mOutputTracks.add(outputTrack); | 
|  | 5980 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); | 
|  | 5981 | updateWaitTime_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5982 | } | 
|  | 5983 |  | 
|  | 5984 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
|  | 5985 | { | 
|  | 5986 | Mutex::Autolock _l(mLock); | 
|  | 5987 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 5988 | if (mOutputTracks[i]->thread() == thread) { | 
|  | 5989 | mOutputTracks[i]->destroy(); | 
|  | 5990 | mOutputTracks.removeAt(i); | 
|  | 5991 | updateWaitTime_l(); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5992 | if (thread->getOutput() == mOutput) { | 
|  | 5993 | mOutput = NULL; | 
|  | 5994 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5995 | return; | 
|  | 5996 | } | 
|  | 5997 | } | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5998 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5999 | } | 
|  | 6000 |  | 
|  | 6001 | // caller must hold mLock | 
|  | 6002 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() | 
|  | 6003 | { | 
|  | 6004 | mWaitTimeMs = UINT_MAX; | 
|  | 6005 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 6006 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
|  | 6007 | if (strong != 0) { | 
|  | 6008 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
|  | 6009 | if (waitTimeMs < mWaitTimeMs) { | 
|  | 6010 | mWaitTimeMs = waitTimeMs; | 
|  | 6011 | } | 
|  | 6012 | } | 
|  | 6013 | } | 
|  | 6014 | } | 
|  | 6015 |  | 
|  | 6016 |  | 
|  | 6017 | bool AudioFlinger::DuplicatingThread::outputsReady( | 
|  | 6018 | const SortedVector< sp<OutputTrack> > &outputTracks) | 
|  | 6019 | { | 
|  | 6020 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 6021 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); | 
|  | 6022 | if (thread == 0) { | 
|  | 6023 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
|  | 6024 | outputTracks[i].get()); | 
|  | 6025 | return false; | 
|  | 6026 | } | 
|  | 6027 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 6028 | // see note at standby() declaration | 
|  | 6029 | if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
|  | 6030 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
|  | 6031 | thread.get()); | 
|  | 6032 | return false; | 
|  | 6033 | } | 
|  | 6034 | } | 
|  | 6035 | return true; | 
|  | 6036 | } | 
|  | 6037 |  | 
|  | 6038 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const | 
|  | 6039 | { | 
|  | 6040 | return (mWaitTimeMs * 1000) / 2; | 
|  | 6041 | } | 
|  | 6042 |  | 
|  | 6043 | void AudioFlinger::DuplicatingThread::cacheParameters_l() | 
|  | 6044 | { | 
|  | 6045 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
|  | 6046 | updateWaitTime_l(); | 
|  | 6047 |  | 
|  | 6048 | MixerThread::cacheParameters_l(); | 
|  | 6049 | } | 
|  | 6050 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 6051 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6052 | // ---------------------------------------------------------------------------- | 
|  | 6053 | //      Record | 
|  | 6054 | // ---------------------------------------------------------------------------- | 
|  | 6055 |  | 
|  | 6056 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 6057 | AudioStreamIn *input, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6058 | audio_io_handle_t id, | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 6059 | audio_devices_t outDevice, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6060 | audio_devices_t inDevice, | 
|  | 6061 | bool systemReady | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6062 | #ifdef TEE_SINK | 
|  | 6063 | , const sp<NBAIO_Sink>& teeSink | 
|  | 6064 | #endif | 
|  | 6065 | ) : | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6066 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6067 | mInput(input), | 
|  | 6068 | mActiveTracks(&this->mLocalLog), | 
|  | 6069 | mRsmpInBuffer(NULL), | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6070 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6071 | mRsmpInRear(0) | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6072 | #ifdef TEE_SINK | 
|  | 6073 | , mTeeSink(teeSink) | 
|  | 6074 | #endif | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 6075 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, | 
|  | 6076 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6077 | // mFastCapture below | 
|  | 6078 | , mFastCaptureFutex(0) | 
|  | 6079 | // mInputSource | 
|  | 6080 | // mPipeSink | 
|  | 6081 | // mPipeSource | 
|  | 6082 | , mPipeFramesP2(0) | 
|  | 6083 | // mPipeMemory | 
|  | 6084 | // mFastCaptureNBLogWriter | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6085 | , mFastTrackAvail(false) | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 6086 | , mBtNrecSuspended(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6087 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6088 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); | 
|  | 6089 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6090 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6091 | readInputParameters_l(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6092 |  | 
|  | 6093 | // create an NBAIO source for the HAL input stream, and negotiate | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 6094 | mInputSource = new AudioStreamInSource(input->stream); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6095 | size_t numCounterOffers = 0; | 
|  | 6096 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6097 | #if !LOG_NDEBUG | 
|  | 6098 | ssize_t index = | 
|  | 6099 | #else | 
|  | 6100 | (void) | 
|  | 6101 | #endif | 
|  | 6102 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6103 | ALOG_ASSERT(index == 0); | 
|  | 6104 |  | 
|  | 6105 | // initialize fast capture depending on configuration | 
|  | 6106 | bool initFastCapture; | 
|  | 6107 | switch (kUseFastCapture) { | 
|  | 6108 | case FastCapture_Never: | 
|  | 6109 | initFastCapture = false; | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6110 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6111 | break; | 
|  | 6112 | case FastCapture_Always: | 
|  | 6113 | initFastCapture = true; | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6114 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6115 | break; | 
|  | 6116 | case FastCapture_Static: | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 6117 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6118 | ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d", | 
|  | 6119 | this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs, | 
|  | 6120 | initFastCapture); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6121 | break; | 
|  | 6122 | // case FastCapture_Dynamic: | 
|  | 6123 | } | 
|  | 6124 |  | 
|  | 6125 | if (initFastCapture) { | 
| Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 6126 | // 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] | 6127 | NBAIO_Format format = mInputSource->format(); | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6128 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread | 
|  | 6129 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6130 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6131 | void *pipeBuffer = nullptr; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6132 | const sp<MemoryDealer> roHeap(readOnlyHeap()); | 
|  | 6133 | sp<IMemory> pipeMemory; | 
|  | 6134 | if ((roHeap == 0) || | 
|  | 6135 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6136 | (pipeBuffer = pipeMemory->pointer()) == nullptr) { | 
|  | 6137 | ALOGE("not enough memory for pipe buffer size=%zu; " | 
|  | 6138 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", | 
|  | 6139 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, | 
|  | 6140 | (long long)kRecordThreadReadOnlyHeapSize); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6141 | goto failed; | 
|  | 6142 | } | 
|  | 6143 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information | 
|  | 6144 | memset(pipeBuffer, 0, pipeSize); | 
|  | 6145 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); | 
|  | 6146 | const NBAIO_Format offers[1] = {format}; | 
|  | 6147 | size_t numCounterOffers = 0; | 
|  | 6148 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); | 
|  | 6149 | ALOG_ASSERT(index == 0); | 
|  | 6150 | mPipeSink = pipe; | 
|  | 6151 | PipeReader *pipeReader = new PipeReader(*pipe); | 
|  | 6152 | numCounterOffers = 0; | 
|  | 6153 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); | 
|  | 6154 | ALOG_ASSERT(index == 0); | 
|  | 6155 | mPipeSource = pipeReader; | 
|  | 6156 | mPipeFramesP2 = pipeFramesP2; | 
|  | 6157 | mPipeMemory = pipeMemory; | 
|  | 6158 |  | 
|  | 6159 | // create fast capture | 
|  | 6160 | mFastCapture = new FastCapture(); | 
|  | 6161 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 6162 | #ifdef STATE_QUEUE_DUMP | 
|  | 6163 | // FIXME | 
|  | 6164 | #endif | 
|  | 6165 | FastCaptureState *state = sq->begin(); | 
|  | 6166 | state->mCblk = NULL; | 
|  | 6167 | state->mInputSource = mInputSource.get(); | 
|  | 6168 | state->mInputSourceGen++; | 
|  | 6169 | state->mPipeSink = pipe; | 
|  | 6170 | state->mPipeSinkGen++; | 
|  | 6171 | state->mFrameCount = mFrameCount; | 
|  | 6172 | state->mCommand = FastCaptureState::COLD_IDLE; | 
|  | 6173 | // already done in constructor initialization list | 
|  | 6174 | //mFastCaptureFutex = 0; | 
|  | 6175 | state->mColdFutexAddr = &mFastCaptureFutex; | 
|  | 6176 | state->mColdGen++; | 
|  | 6177 | state->mDumpState = &mFastCaptureDumpState; | 
|  | 6178 | #ifdef TEE_SINK | 
|  | 6179 | // FIXME | 
|  | 6180 | #endif | 
|  | 6181 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); | 
|  | 6182 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); | 
|  | 6183 | sq->end(); | 
|  | 6184 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 6185 |  | 
|  | 6186 | // start the fast capture | 
|  | 6187 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); | 
|  | 6188 | pid_t tid = mFastCapture->getTid(); | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6189 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture, false /*forApp*/); | 
| Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 6190 | stream()->setHalThreadPriority(kPriorityFastCapture); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6191 | #ifdef AUDIO_WATCHDOG | 
|  | 6192 | // FIXME | 
|  | 6193 | #endif | 
|  | 6194 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6195 | mFastTrackAvail = true; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6196 | } | 
|  | 6197 | failed: ; | 
|  | 6198 |  | 
|  | 6199 | // FIXME mNormalSource | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6200 | } | 
|  | 6201 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6202 | AudioFlinger::RecordThread::~RecordThread() | 
|  | 6203 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6204 | if (mFastCapture != 0) { | 
|  | 6205 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 6206 | FastCaptureState *state = sq->begin(); | 
|  | 6207 | if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
|  | 6208 | int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
|  | 6209 | if (old == -1) { | 
|  | 6210 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
|  | 6211 | } | 
|  | 6212 | } | 
|  | 6213 | state->mCommand = FastCaptureState::EXIT; | 
|  | 6214 | sq->end(); | 
|  | 6215 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
|  | 6216 | mFastCapture->join(); | 
|  | 6217 | mFastCapture.clear(); | 
|  | 6218 | } | 
|  | 6219 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 6220 | mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6221 | free(mRsmpInBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6222 | } | 
|  | 6223 |  | 
|  | 6224 | void AudioFlinger::RecordThread::onFirstRef() | 
|  | 6225 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6226 | run(mThreadName, PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6227 | } | 
|  | 6228 |  | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 6229 | void AudioFlinger::RecordThread::preExit() | 
|  | 6230 | { | 
|  | 6231 | ALOGV("  preExit()"); | 
|  | 6232 | Mutex::Autolock _l(mLock); | 
|  | 6233 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 6234 | sp<RecordTrack> track = mTracks[i]; | 
|  | 6235 | track->invalidate(); | 
|  | 6236 | } | 
|  | 6237 | mActiveTracks.clear(); | 
|  | 6238 | mStartStopCond.broadcast(); | 
|  | 6239 | } | 
|  | 6240 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6241 | bool AudioFlinger::RecordThread::threadLoop() | 
|  | 6242 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6243 | nsecs_t lastWarning = 0; | 
|  | 6244 |  | 
|  | 6245 | inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6246 |  | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6247 | reacquire_wakelock: | 
|  | 6248 | sp<RecordTrack> activeTrack; | 
|  | 6249 | { | 
|  | 6250 | Mutex::Autolock _l(mLock); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6251 | acquireWakeLock_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6252 | } | 
|  | 6253 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6254 | // used to request a deferred sleep, to be executed later while mutex is unlocked | 
|  | 6255 | uint32_t sleepUs = 0; | 
|  | 6256 |  | 
|  | 6257 | // loop while there is work to do | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6258 | for (;;) { | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6259 | Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6260 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6261 | // activeTracks accumulates a copy of a subset of mActiveTracks | 
|  | 6262 | Vector< sp<RecordTrack> > activeTracks; | 
|  | 6263 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6264 | // reference to the (first and only) active fast track | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6265 | sp<RecordTrack> fastTrack; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6266 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6267 | // reference to a fast track which is about to be removed | 
|  | 6268 | sp<RecordTrack> fastTrackToRemove; | 
|  | 6269 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6270 | { // scope for mLock | 
|  | 6271 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6272 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6273 | processConfigEvents_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6274 |  | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6275 | // check exitPending here because checkForNewParameters_l() and | 
|  | 6276 | // checkForNewParameters_l() can temporarily release mLock | 
|  | 6277 | if (exitPending()) { | 
|  | 6278 | break; | 
|  | 6279 | } | 
|  | 6280 |  | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6281 | // sleep with mutex unlocked | 
|  | 6282 | if (sleepUs > 0) { | 
| Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 6283 | ATRACE_BEGIN("sleepC"); | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6284 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); | 
|  | 6285 | ATRACE_END(); | 
|  | 6286 | sleepUs = 0; | 
|  | 6287 | continue; | 
|  | 6288 | } | 
|  | 6289 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6290 | // if no active track(s), then standby and release wakelock | 
|  | 6291 | size_t size = mActiveTracks.size(); | 
|  | 6292 | if (size == 0) { | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6293 | standbyIfNotAlreadyInStandby(); | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6294 | // exitPending() can't become true here | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6295 | releaseWakeLock_l(); | 
|  | 6296 | ALOGV("RecordThread: loop stopping"); | 
|  | 6297 | // go to sleep | 
|  | 6298 | mWaitWorkCV.wait(mLock); | 
|  | 6299 | ALOGV("RecordThread: loop starting"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6300 | goto reacquire_wakelock; | 
|  | 6301 | } | 
|  | 6302 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6303 | bool doBroadcast = false; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6304 | bool allStopped = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6305 | for (size_t i = 0; i < size; ) { | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6306 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6307 | activeTrack = mActiveTracks[i]; | 
|  | 6308 | if (activeTrack->isTerminated()) { | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6309 | if (activeTrack->isFastTrack()) { | 
|  | 6310 | ALOG_ASSERT(fastTrackToRemove == 0); | 
|  | 6311 | fastTrackToRemove = activeTrack; | 
|  | 6312 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6313 | removeTrack_l(activeTrack); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6314 | mActiveTracks.remove(activeTrack); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6315 | size--; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6316 | continue; | 
|  | 6317 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6318 |  | 
|  | 6319 | TrackBase::track_state activeTrackState = activeTrack->mState; | 
|  | 6320 | switch (activeTrackState) { | 
|  | 6321 |  | 
|  | 6322 | case TrackBase::PAUSING: | 
|  | 6323 | mActiveTracks.remove(activeTrack); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6324 | doBroadcast = true; | 
|  | 6325 | size--; | 
|  | 6326 | continue; | 
|  | 6327 |  | 
|  | 6328 | case TrackBase::STARTING_1: | 
|  | 6329 | sleepUs = 10000; | 
|  | 6330 | i++; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6331 | allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6332 | continue; | 
|  | 6333 |  | 
|  | 6334 | case TrackBase::STARTING_2: | 
|  | 6335 | doBroadcast = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6336 | mStandby = false; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6337 | activeTrack->mState = TrackBase::ACTIVE; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6338 | allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6339 | break; | 
|  | 6340 |  | 
|  | 6341 | case TrackBase::ACTIVE: | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6342 | allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6343 | break; | 
|  | 6344 |  | 
|  | 6345 | case TrackBase::IDLE: | 
|  | 6346 | i++; | 
|  | 6347 | continue; | 
|  | 6348 |  | 
|  | 6349 | default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6350 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6351 | } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6352 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6353 | activeTracks.add(activeTrack); | 
|  | 6354 | i++; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6355 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6356 | if (activeTrack->isFastTrack()) { | 
|  | 6357 | ALOG_ASSERT(!mFastTrackAvail); | 
|  | 6358 | ALOG_ASSERT(fastTrack == 0); | 
|  | 6359 | fastTrack = activeTrack; | 
|  | 6360 | } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6361 | } | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6362 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6363 | mActiveTracks.updatePowerState(this); | 
|  | 6364 |  | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6365 | if (allStopped) { | 
|  | 6366 | standbyIfNotAlreadyInStandby(); | 
|  | 6367 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6368 | if (doBroadcast) { | 
|  | 6369 | mStartStopCond.broadcast(); | 
|  | 6370 | } | 
|  | 6371 |  | 
|  | 6372 | // sleep if there are no active tracks to process | 
|  | 6373 | if (activeTracks.size() == 0) { | 
|  | 6374 | if (sleepUs == 0) { | 
|  | 6375 | sleepUs = kRecordThreadSleepUs; | 
|  | 6376 | } | 
|  | 6377 | continue; | 
|  | 6378 | } | 
|  | 6379 | sleepUs = 0; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6380 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6381 | lockEffectChains_l(effectChains); | 
|  | 6382 | } | 
|  | 6383 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6384 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 | 
| Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6385 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6386 | size_t size = effectChains.size(); | 
|  | 6387 | for (size_t i = 0; i < size; i++) { | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6388 | // thread mutex is not locked, but effect chain is locked | 
|  | 6389 | effectChains[i]->process_l(); | 
|  | 6390 | } | 
|  | 6391 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6392 | // 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] | 6393 | if (mFastCapture != 0) { | 
|  | 6394 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 6395 | FastCaptureState *state = sq->begin(); | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6396 | bool didModify = false; | 
|  | 6397 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6398 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && | 
|  | 6399 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { | 
|  | 6400 | if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
|  | 6401 | int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
|  | 6402 | if (old == -1) { | 
|  | 6403 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
|  | 6404 | } | 
|  | 6405 | } | 
|  | 6406 | state->mCommand = FastCaptureState::READ_WRITE; | 
|  | 6407 | #if 0   // FIXME | 
|  | 6408 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6409 | FastThreadDumpState::kSamplingNforLowRamDevice : | 
|  | 6410 | FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6411 | #endif | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6412 | didModify = true; | 
|  | 6413 | } | 
|  | 6414 | audio_track_cblk_t *cblkOld = state->mCblk; | 
|  | 6415 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; | 
|  | 6416 | if (cblkNew != cblkOld) { | 
|  | 6417 | state->mCblk = cblkNew; | 
|  | 6418 | // block until acked if removing a fast track | 
|  | 6419 | if (cblkOld != NULL) { | 
|  | 6420 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; | 
|  | 6421 | } | 
|  | 6422 | didModify = true; | 
|  | 6423 | } | 
|  | 6424 | sq->end(didModify); | 
|  | 6425 | if (didModify) { | 
|  | 6426 | sq->push(block); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6427 | #if 0 | 
|  | 6428 | if (kUseFastCapture == FastCapture_Dynamic) { | 
|  | 6429 | mNormalSource = mPipeSource; | 
|  | 6430 | } | 
|  | 6431 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6432 | } | 
|  | 6433 | } | 
|  | 6434 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6435 | // now run the fast track destructor with thread mutex unlocked | 
|  | 6436 | fastTrackToRemove.clear(); | 
|  | 6437 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6438 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. | 
|  | 6439 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too | 
|  | 6440 | // slow, then this RecordThread will overrun by not calling HAL read often enough. | 
|  | 6441 | // If destination is non-contiguous, first read past the nominal end of buffer, then | 
|  | 6442 | // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated. | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6443 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6444 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6445 | ssize_t framesRead; | 
|  | 6446 |  | 
|  | 6447 | // If an NBAIO source is present, use it to read the normal capture's data | 
|  | 6448 | if (mPipeSource != 0) { | 
|  | 6449 | size_t framesToRead = mBufferSize / mFrameSize; | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6450 | framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6451 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6452 | framesToRead); | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6453 | // since pipe is non-blocking, simulate blocking input by waiting for 1/2 of | 
|  | 6454 | // buffer size or at least for 20ms. | 
|  | 6455 | size_t sleepFrames = max( | 
|  | 6456 | min(mPipeFramesP2, mRsmpInFramesP2) / 2, FMS_20 * mSampleRate / 1000); | 
|  | 6457 | if (framesRead <= (ssize_t) sleepFrames) { | 
|  | 6458 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; | 
|  | 6459 | } | 
|  | 6460 | if (framesRead < 0) { | 
|  | 6461 | status_t status = (status_t) framesRead; | 
|  | 6462 | switch (status) { | 
|  | 6463 | case OVERRUN: | 
|  | 6464 | ALOGW("overrun on read from pipe"); | 
|  | 6465 | framesRead = 0; | 
|  | 6466 | break; | 
|  | 6467 | case NEGOTIATE: | 
|  | 6468 | ALOGE("re-negotiation is needed"); | 
|  | 6469 | framesRead = -1;  // Will cause an attempt to recover. | 
|  | 6470 | break; | 
|  | 6471 | default: | 
|  | 6472 | ALOGE("unknown error %d on read from pipe", status); | 
|  | 6473 | break; | 
|  | 6474 | } | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6475 | } | 
|  | 6476 | // otherwise use the HAL / AudioStreamIn directly | 
|  | 6477 | } else { | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6478 | ATRACE_BEGIN("read"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6479 | size_t bytesRead; | 
|  | 6480 | status_t result = mInput->stream->read( | 
|  | 6481 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6482 | ATRACE_END(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6483 | if (result < 0) { | 
|  | 6484 | framesRead = result; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6485 | } else { | 
|  | 6486 | framesRead = bytesRead / mFrameSize; | 
|  | 6487 | } | 
|  | 6488 | } | 
|  | 6489 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6490 | // Update server timestamp with server stats | 
|  | 6491 | // systemTime() is optional if the hardware supports timestamps. | 
|  | 6492 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; | 
|  | 6493 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); | 
|  | 6494 |  | 
|  | 6495 | // Update server timestamp with kernel stats | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6496 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6497 | int64_t position, time; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6498 | int ret = mInput->stream->getCapturePosition(&position, &time); | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6499 | if (ret == NO_ERROR) { | 
|  | 6500 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; | 
|  | 6501 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; | 
|  | 6502 | // Note: In general record buffers should tend to be empty in | 
|  | 6503 | // a properly running pipeline. | 
|  | 6504 | // | 
|  | 6505 | // Also, it is not advantageous to call get_presentation_position during the read | 
|  | 6506 | // as the read obtains a lock, preventing the timestamp call from executing. | 
|  | 6507 | } | 
|  | 6508 | } | 
|  | 6509 | // Use this to track timestamp information | 
|  | 6510 | // ALOGD("%s", mTimestamp.toString().c_str()); | 
|  | 6511 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6512 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6513 | ALOGE("read failed: framesRead=%zd", framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6514 | // Force input into standby so that it tries to recover at next read attempt | 
|  | 6515 | inputStandBy(); | 
|  | 6516 | sleepUs = kRecordThreadSleepUs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6517 | } | 
|  | 6518 | if (framesRead <= 0) { | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6519 | goto unlock; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6520 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6521 | ALOG_ASSERT(framesRead > 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6522 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6523 | if (mTeeSink != 0) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6524 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6525 | } | 
|  | 6526 | // 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] | 6527 | { | 
|  | 6528 | size_t part1 = mRsmpInFramesP2 - rear; | 
|  | 6529 | if ((size_t) framesRead > part1) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6530 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6531 | (framesRead - part1) * mFrameSize); | 
|  | 6532 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6533 | } | 
|  | 6534 | rear = mRsmpInRear += framesRead; | 
|  | 6535 |  | 
|  | 6536 | size = activeTracks.size(); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6537 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6538 | // loop over each active track | 
|  | 6539 | for (size_t i = 0; i < size; i++) { | 
|  | 6540 | activeTrack = activeTracks[i]; | 
|  | 6541 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6542 | // skip fast tracks, as those are handled directly by FastCapture | 
|  | 6543 | if (activeTrack->isFastTrack()) { | 
|  | 6544 | continue; | 
|  | 6545 | } | 
|  | 6546 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6547 | // TODO: This code probably should be moved to RecordTrack. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6548 | // TODO: Update the activeTrack buffer converter in case of reconfigure. | 
|  | 6549 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6550 | enum { | 
|  | 6551 | OVERRUN_UNKNOWN, | 
|  | 6552 | OVERRUN_TRUE, | 
|  | 6553 | OVERRUN_FALSE | 
|  | 6554 | } overrun = OVERRUN_UNKNOWN; | 
|  | 6555 |  | 
|  | 6556 | // loop over getNextBuffer to handle circular sink | 
|  | 6557 | for (;;) { | 
|  | 6558 |  | 
|  | 6559 | activeTrack->mSink.frameCount = ~0; | 
|  | 6560 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); | 
|  | 6561 | size_t framesOut = activeTrack->mSink.frameCount; | 
|  | 6562 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); | 
|  | 6563 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6564 | // check available frames and handle overrun conditions | 
|  | 6565 | // if the record track isn't draining fast enough. | 
|  | 6566 | bool hasOverrun; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6567 | size_t framesIn; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6568 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); | 
|  | 6569 | if (hasOverrun) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6570 | overrun = OVERRUN_TRUE; | 
|  | 6571 | } | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6572 | if (framesOut == 0 || framesIn == 0) { | 
|  | 6573 | break; | 
|  | 6574 | } | 
|  | 6575 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6576 | // Don't allow framesOut to be larger than what is possible with resampling | 
|  | 6577 | // from framesIn. | 
|  | 6578 | // This isn't strictly necessary but helps limit buffer resizing in | 
|  | 6579 | // RecordBufferConverter.  TODO: remove when no longer needed. | 
|  | 6580 | framesOut = min(framesOut, | 
|  | 6581 | destinationFramesPossible( | 
|  | 6582 | framesIn, mSampleRate, activeTrack->mSampleRate)); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6583 | // process frames from the RecordThread buffer provider to the RecordTrack buffer | 
|  | 6584 | framesOut = activeTrack->mRecordBufferConverter->convert( | 
|  | 6585 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6586 |  | 
|  | 6587 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { | 
|  | 6588 | overrun = OVERRUN_FALSE; | 
|  | 6589 | } | 
|  | 6590 |  | 
|  | 6591 | if (activeTrack->mFramesToDrop == 0) { | 
|  | 6592 | if (framesOut > 0) { | 
|  | 6593 | activeTrack->mSink.frameCount = framesOut; | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6594 | // Sanitize before releasing if the track has no access to the source data | 
|  | 6595 | // An idle UID receives silence from non virtual devices until active | 
|  | 6596 | if (activeTrack->isSilenced()) { | 
|  | 6597 | memset(activeTrack->mSink.raw, 0, framesOut * mFrameSize); | 
|  | 6598 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6599 | activeTrack->releaseBuffer(&activeTrack->mSink); | 
|  | 6600 | } | 
|  | 6601 | } else { | 
|  | 6602 | // FIXME could do a partial drop of framesOut | 
|  | 6603 | if (activeTrack->mFramesToDrop > 0) { | 
|  | 6604 | activeTrack->mFramesToDrop -= framesOut; | 
|  | 6605 | if (activeTrack->mFramesToDrop <= 0) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6606 | activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6607 | } | 
|  | 6608 | } else { | 
|  | 6609 | activeTrack->mFramesToDrop += framesOut; | 
|  | 6610 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || | 
|  | 6611 | activeTrack->mSyncStartEvent->isCancelled()) { | 
|  | 6612 | ALOGW("Synced record %s, session %d, trigger session %d", | 
|  | 6613 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", | 
|  | 6614 | activeTrack->sessionId(), | 
|  | 6615 | (activeTrack->mSyncStartEvent != 0) ? | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6616 | activeTrack->mSyncStartEvent->triggerSession() : | 
|  | 6617 | AUDIO_SESSION_NONE); | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6618 | activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6619 | } | 
|  | 6620 | } | 
|  | 6621 | } | 
|  | 6622 |  | 
|  | 6623 | if (framesOut == 0) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6624 | break; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6625 | } | 
|  | 6626 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6627 |  | 
|  | 6628 | switch (overrun) { | 
|  | 6629 | case OVERRUN_TRUE: | 
|  | 6630 | // client isn't retrieving buffers fast enough | 
|  | 6631 | if (!activeTrack->setOverflow()) { | 
|  | 6632 | nsecs_t now = systemTime(); | 
|  | 6633 | // FIXME should lastWarning per track? | 
|  | 6634 | if ((now - lastWarning) > kWarningThrottleNs) { | 
|  | 6635 | ALOGW("RecordThread: buffer overflow"); | 
|  | 6636 | lastWarning = now; | 
|  | 6637 | } | 
|  | 6638 | } | 
|  | 6639 | break; | 
|  | 6640 | case OVERRUN_FALSE: | 
|  | 6641 | activeTrack->clearOverflow(); | 
|  | 6642 | break; | 
|  | 6643 | case OVERRUN_UNKNOWN: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6644 | break; | 
|  | 6645 | } | 
|  | 6646 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6647 | // update frame information and push timestamp out | 
|  | 6648 | activeTrack->updateTrackFrameInfo( | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6649 | activeTrack->mServerProxy->framesReleased(), | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6650 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], | 
|  | 6651 | mSampleRate, mTimestamp); | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6652 | } | 
|  | 6653 |  | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6654 | unlock: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6655 | // enable changes in effect chain | 
|  | 6656 | unlockEffectChains(effectChains); | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6657 | // 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] | 6658 | } | 
|  | 6659 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6660 | standbyIfNotAlreadyInStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6661 |  | 
|  | 6662 | { | 
|  | 6663 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6664 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 6665 | sp<RecordTrack> track = mTracks[i]; | 
|  | 6666 | track->invalidate(); | 
|  | 6667 | } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6668 | mActiveTracks.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6669 | mStartStopCond.broadcast(); | 
|  | 6670 | } | 
|  | 6671 |  | 
|  | 6672 | releaseWakeLock(); | 
|  | 6673 |  | 
|  | 6674 | ALOGV("RecordThread %p exiting", this); | 
|  | 6675 | return false; | 
|  | 6676 | } | 
|  | 6677 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6678 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6679 | { | 
|  | 6680 | if (!mStandby) { | 
|  | 6681 | inputStandBy(); | 
|  | 6682 | mStandby = true; | 
|  | 6683 | } | 
|  | 6684 | } | 
|  | 6685 |  | 
|  | 6686 | void AudioFlinger::RecordThread::inputStandBy() | 
|  | 6687 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6688 | // Idle the fast capture if it's currently running | 
|  | 6689 | if (mFastCapture != 0) { | 
|  | 6690 | FastCaptureStateQueue *sq = mFastCapture->sq(); | 
|  | 6691 | FastCaptureState *state = sq->begin(); | 
|  | 6692 | if (!(state->mCommand & FastCaptureState::IDLE)) { | 
|  | 6693 | state->mCommand = FastCaptureState::COLD_IDLE; | 
|  | 6694 | state->mColdFutexAddr = &mFastCaptureFutex; | 
|  | 6695 | state->mColdGen++; | 
|  | 6696 | mFastCaptureFutex = 0; | 
|  | 6697 | sq->end(); | 
|  | 6698 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
|  | 6699 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); | 
|  | 6700 | #if 0 | 
|  | 6701 | if (kUseFastCapture == FastCapture_Dynamic) { | 
|  | 6702 | // FIXME | 
|  | 6703 | } | 
|  | 6704 | #endif | 
|  | 6705 | #ifdef AUDIO_WATCHDOG | 
|  | 6706 | // FIXME | 
|  | 6707 | #endif | 
|  | 6708 | } else { | 
|  | 6709 | sq->end(false /*didModify*/); | 
|  | 6710 | } | 
|  | 6711 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6712 | status_t result = mInput->stream->standby(); | 
|  | 6713 | ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result); | 
| Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 6714 |  | 
|  | 6715 | // If going into standby, flush the pipe source. | 
|  | 6716 | if (mPipeSource.get() != nullptr) { | 
|  | 6717 | const ssize_t flushed = mPipeSource->flush(); | 
|  | 6718 | if (flushed > 0) { | 
|  | 6719 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); | 
|  | 6720 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; | 
|  | 6721 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); | 
|  | 6722 | } | 
|  | 6723 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6724 | } | 
|  | 6725 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6726 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6727 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6728 | const sp<AudioFlinger::Client>& client, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6729 | uint32_t *pSampleRate, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6730 | audio_format_t format, | 
|  | 6731 | audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6732 | size_t *pFrameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6733 | audio_session_t sessionId, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6734 | size_t *pNotificationFrameCount, | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6735 | uid_t uid, | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6736 | audio_input_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6737 | pid_t tid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6738 | status_t *status, | 
|  | 6739 | audio_port_handle_t portId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6740 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6741 | size_t frameCount = *pFrameCount; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6742 | size_t notificationFrameCount = *pNotificationFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6743 | sp<RecordTrack> track; | 
|  | 6744 | status_t lStatus; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6745 | audio_input_flags_t inputFlags = mInput->flags; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6746 | audio_input_flags_t requestedFlags = *flags; | 
|  | 6747 | uint32_t sampleRate; | 
|  | 6748 |  | 
|  | 6749 | lStatus = initCheck(); | 
|  | 6750 | if (lStatus != NO_ERROR) { | 
|  | 6751 | ALOGE("createRecordTrack_l() audio driver not initialized"); | 
|  | 6752 | goto Exit; | 
|  | 6753 | } | 
|  | 6754 |  | 
|  | 6755 | if (*pSampleRate == 0) { | 
|  | 6756 | *pSampleRate = mSampleRate; | 
|  | 6757 | } | 
|  | 6758 | sampleRate = *pSampleRate; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6759 |  | 
|  | 6760 | // special case for FAST flag considered OK if fast capture is present | 
|  | 6761 | if (hasFastCapture()) { | 
|  | 6762 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); | 
|  | 6763 | } | 
|  | 6764 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6765 | // Check if requested flags are compatible with input stream flags | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6766 | if ((*flags & inputFlags) != *flags) { | 
|  | 6767 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" | 
|  | 6768 | " input flags (%08x)", | 
|  | 6769 | *flags, inputFlags); | 
|  | 6770 | *flags = (audio_input_flags_t)(*flags & inputFlags); | 
|  | 6771 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6772 |  | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6773 | // client expresses a preference for FAST, but we get the final say | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6774 | if (*flags & AUDIO_INPUT_FLAG_FAST) { | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6775 | if ( | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6776 | // we formerly checked for a callback handler (non-0 tid), | 
|  | 6777 | // but that is no longer required for TRANSFER_OBTAIN mode | 
|  | 6778 | // | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6779 | // frame count is not specified, or is exactly the pipe depth | 
|  | 6780 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6781 | // PCM data | 
|  | 6782 | audio_is_linear_pcm(format) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6783 | // hardware format | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6784 | (format == mFormat) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6785 | // hardware channel mask | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6786 | (channelMask == mChannelMask) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6787 | // hardware sample rate | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6788 | (sampleRate == mSampleRate) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6789 | // record thread has an associated fast capture | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6790 | hasFastCapture() && | 
|  | 6791 | // there are sufficient fast track slots available | 
|  | 6792 | mFastTrackAvail | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6793 | ) { | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6794 | // check compatibility with audio effects. | 
|  | 6795 | Mutex::Autolock _l(mLock); | 
|  | 6796 | // Do not accept FAST flag if the session has software effects | 
|  | 6797 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 6798 | if (chain != 0) { | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 6799 | audio_input_flags_t old = *flags; | 
|  | 6800 | chain->checkInputFlagCompatibility(flags); | 
|  | 6801 | if (old != *flags) { | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6802 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", | 
|  | 6803 | this, (int)old, (int)*flags); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6804 | } | 
|  | 6805 | } | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6806 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6807 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
|  | 6808 | this, frameCount, mFrameCount); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6809 | } else { | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6810 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " | 
|  | 6811 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6812 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 6813 | this, frameCount, mFrameCount, mPipeFramesP2, | 
|  | 6814 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6815 | hasFastCapture(), tid, mFastTrackAvail); | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6816 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6817 | } | 
|  | 6818 | } | 
|  | 6819 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6820 | // If FAST or RAW flags were corrected, ask caller to request new input from audio policy | 
|  | 6821 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != | 
|  | 6822 | (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { | 
|  | 6823 | *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); | 
|  | 6824 | lStatus = BAD_TYPE; | 
|  | 6825 | goto Exit; | 
|  | 6826 | } | 
|  | 6827 |  | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6828 | // compute track buffer size in frames, and suggest the notification frame count | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6829 | if (*flags & AUDIO_INPUT_FLAG_FAST) { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6830 | // fast track: frame count is exactly the pipe depth | 
|  | 6831 | frameCount = mPipeFramesP2; | 
|  | 6832 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6833 | notificationFrameCount = mFrameCount; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6834 | } else { | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6835 | // not fast track: max notification period is resampled equivalent of one HAL buffer time | 
|  | 6836 | //                 or 20 ms if there is a fast capture | 
|  | 6837 | // TODO This could be a roundupRatio inline, and const | 
|  | 6838 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) | 
|  | 6839 | * sampleRate + mSampleRate - 1) / mSampleRate; | 
|  | 6840 | // minimum number of notification periods is at least kMinNotifications, | 
|  | 6841 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) | 
|  | 6842 | static const size_t kMinNotifications = 3; | 
|  | 6843 | static const uint32_t kMinMs = 30; | 
|  | 6844 | // TODO This could be a roundupRatio inline | 
|  | 6845 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; | 
|  | 6846 | // TODO This could be a roundupRatio inline | 
|  | 6847 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / | 
|  | 6848 | maxNotificationFrames; | 
|  | 6849 | const size_t minFrameCount = maxNotificationFrames * | 
|  | 6850 | max(kMinNotifications, minNotificationsByMs); | 
|  | 6851 | frameCount = max(frameCount, minFrameCount); | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6852 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { | 
|  | 6853 | notificationFrameCount = maxNotificationFrames; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6854 | } | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6855 | } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6856 | *pFrameCount = frameCount; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 6857 | *pNotificationFrameCount = notificationFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6858 |  | 
|  | 6859 | { // scope for mLock | 
|  | 6860 | Mutex::Autolock _l(mLock); | 
|  | 6861 |  | 
|  | 6862 | track = new RecordTrack(this, client, sampleRate, | 
| Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 6863 | format, channelMask, frameCount, | 
|  | 6864 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6865 | *flags, TrackBase::TYPE_DEFAULT, portId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6866 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6867 | lStatus = track->initCheck(); | 
|  | 6868 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6869 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6870 | // 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] | 6871 | goto Exit; | 
|  | 6872 | } | 
|  | 6873 | mTracks.add(track); | 
|  | 6874 |  | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6875 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6876 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
|  | 6877 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
|  | 6878 | // so ask activity manager to do this on our behalf | 
| Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 6879 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6880 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6881 | } | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6882 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6883 | lStatus = NO_ERROR; | 
|  | 6884 |  | 
|  | 6885 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6886 | *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6887 | return track; | 
|  | 6888 | } | 
|  | 6889 |  | 
|  | 6890 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, | 
|  | 6891 | AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6892 | audio_session_t triggerSession) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6893 | { | 
|  | 6894 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
|  | 6895 | sp<ThreadBase> strongMe = this; | 
|  | 6896 | status_t status = NO_ERROR; | 
|  | 6897 |  | 
|  | 6898 | if (event == AudioSystem::SYNC_EVENT_NONE) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6899 | recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6900 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6901 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6902 | triggerSession, | 
|  | 6903 | recordTrack->sessionId(), | 
|  | 6904 | syncStartEventCallback, | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6905 | recordTrack); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6906 | // Sync event can be cancelled by the trigger session if the track is not in a | 
|  | 6907 | // compatible state in which case we start record immediately | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6908 | if (recordTrack->mSyncStartEvent->isCancelled()) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6909 | recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6910 | } else { | 
|  | 6911 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs | 
| Ivan Lozano | 1b35dd6 | 2017-12-06 16:55:10 -0800 | [diff] [blame] | 6912 | recordTrack->mFramesToDrop = -(ssize_t) | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6913 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6914 | } | 
|  | 6915 | } | 
|  | 6916 |  | 
|  | 6917 | { | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6918 | // This section is a rendezvous between binder thread executing start() and RecordThread | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6919 | AutoMutex lock(mLock); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6920 | if (mActiveTracks.indexOf(recordTrack) >= 0) { | 
|  | 6921 | if (recordTrack->mState == TrackBase::PAUSING) { | 
|  | 6922 | ALOGV("active record track PAUSING -> ACTIVE"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6923 | recordTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6924 | } else { | 
|  | 6925 | ALOGV("active record track state %d", recordTrack->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6926 | } | 
|  | 6927 | return status; | 
|  | 6928 | } | 
|  | 6929 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6930 | // TODO consider other ways of handling this, such as changing the state to :STARTING and | 
|  | 6931 | //      adding the track to mActiveTracks after returning from AudioSystem::startInput(), | 
|  | 6932 | //      or using a separate command thread | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6933 | recordTrack->mState = TrackBase::STARTING_1; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6934 | mActiveTracks.add(recordTrack); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6935 | status_t status = NO_ERROR; | 
|  | 6936 | if (recordTrack->isExternalTrack()) { | 
|  | 6937 | mLock.unlock(); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6938 | bool silenced; | 
| Eric Laurent | e9ebcdb | 2018-01-29 14:27:18 -0800 | [diff] [blame] | 6939 | status = AudioSystem::startInput(mId, recordTrack->sessionId(), | 
|  | 6940 | mInDevice, recordTrack->uid(), &silenced); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6941 | mLock.lock(); | 
|  | 6942 | // FIXME should verify that recordTrack is still in mActiveTracks | 
|  | 6943 | if (status != NO_ERROR) { | 
|  | 6944 | mActiveTracks.remove(recordTrack); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6945 | recordTrack->clearSyncStartEvent(); | 
|  | 6946 | ALOGV("RecordThread::start error %d", status); | 
|  | 6947 | return status; | 
|  | 6948 | } | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 6949 | recordTrack->setSilenced(silenced); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6950 | } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6951 | // Catch up with current buffer indices if thread is already running. | 
|  | 6952 | // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront | 
|  | 6953 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could | 
|  | 6954 | // see previously buffered data before it called start(), but with greater risk of overrun. | 
|  | 6955 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6956 | recordTrack->mResamplerBufferProvider->reset(); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6957 | // clear any converter state as new data will be discontinuous | 
|  | 6958 | recordTrack->mRecordBufferConverter->reset(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6959 | recordTrack->mState = TrackBase::STARTING_2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6960 | // signal thread to start | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6961 | mWaitWorkCV.broadcast(); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6962 | if (mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6963 | ALOGV("Record failed to start"); | 
|  | 6964 | status = BAD_VALUE; | 
|  | 6965 | goto startError; | 
|  | 6966 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6967 | return status; | 
|  | 6968 | } | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6969 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6970 | startError: | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6971 | if (recordTrack->isExternalTrack()) { | 
| Eric Laurent | e9ebcdb | 2018-01-29 14:27:18 -0800 | [diff] [blame] | 6972 | AudioSystem::stopInput(mId, recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6973 | } | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6974 | recordTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6975 | // FIXME I wonder why we do not reset the state here? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6976 | return status; | 
|  | 6977 | } | 
|  | 6978 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6979 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
|  | 6980 | { | 
|  | 6981 | sp<SyncEvent> strongEvent = event.promote(); | 
|  | 6982 |  | 
|  | 6983 | if (strongEvent != 0) { | 
| Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6984 | sp<RefBase> ptr = strongEvent->cookie().promote(); | 
|  | 6985 | if (ptr != 0) { | 
|  | 6986 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); | 
|  | 6987 | recordTrack->handleSyncStartEvent(strongEvent); | 
|  | 6988 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6989 | } | 
|  | 6990 | } | 
|  | 6991 |  | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6992 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6993 | ALOGV("RecordThread::stop"); | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6994 | AutoMutex _l(mLock); | 
| Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 6995 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6996 | return false; | 
|  | 6997 | } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6998 | // 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] | 6999 | recordTrack->mState = TrackBase::PAUSING; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7000 | // signal thread to stop | 
|  | 7001 | mWaitWorkCV.broadcast(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7002 | // do not wait for mStartStopCond if exiting | 
|  | 7003 | if (exitPending()) { | 
|  | 7004 | return true; | 
|  | 7005 | } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 7006 | // FIXME incorrect usage of wait: no explicit predicate or loop | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7007 | mStartStopCond.wait(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7008 | // if we have been restarted, recordTrack is in mActiveTracks here | 
| Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 7009 | if (exitPending() || mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7010 | ALOGV("Record stopped OK"); | 
|  | 7011 | return true; | 
|  | 7012 | } | 
|  | 7013 | return false; | 
|  | 7014 | } | 
|  | 7015 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7016 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7017 | { | 
|  | 7018 | return false; | 
|  | 7019 | } | 
|  | 7020 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7021 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7022 | { | 
|  | 7023 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
|  | 7024 | if (!isValidSyncEvent(event)) { | 
|  | 7025 | return BAD_VALUE; | 
|  | 7026 | } | 
|  | 7027 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7028 | audio_session_t eventSession = event->triggerSession(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7029 | status_t ret = NAME_NOT_FOUND; | 
|  | 7030 |  | 
|  | 7031 | Mutex::Autolock _l(mLock); | 
|  | 7032 |  | 
|  | 7033 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 7034 | sp<RecordTrack> track = mTracks[i]; | 
|  | 7035 | if (eventSession == track->sessionId()) { | 
|  | 7036 | (void) track->setSyncEvent(event); | 
|  | 7037 | ret = NO_ERROR; | 
|  | 7038 | } | 
|  | 7039 | } | 
|  | 7040 | return ret; | 
|  | 7041 | #else | 
|  | 7042 | return BAD_VALUE; | 
|  | 7043 | #endif | 
|  | 7044 | } | 
|  | 7045 |  | 
| jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 7046 | status_t AudioFlinger::RecordThread::getActiveMicrophones( | 
|  | 7047 | std::vector<media::MicrophoneInfo>* activeMicrophones) | 
|  | 7048 | { | 
|  | 7049 | ALOGV("RecordThread::getActiveMicrophones"); | 
|  | 7050 | AutoMutex _l(mLock); | 
|  | 7051 | // Fake data | 
|  | 7052 | struct audio_microphone_characteristic_t characteristic; | 
|  | 7053 | sprintf(characteristic.device_id, "builtin_mic"); | 
| rago | 1de79cf | 2018-02-01 15:21:02 -0800 | [diff] [blame] | 7054 | characteristic.device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
| jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 7055 | sprintf(characteristic.address, ""); | 
|  | 7056 | characteristic.location = AUDIO_MICROPHONE_LOCATION_MAINBODY; | 
|  | 7057 | characteristic.group = 0; | 
|  | 7058 | characteristic.index_in_the_group = 0; | 
|  | 7059 | characteristic.sensitivity = 1.0f; | 
|  | 7060 | characteristic.max_spl = 100.0f; | 
|  | 7061 | characteristic.min_spl = 0.0f; | 
|  | 7062 | characteristic.directionality = AUDIO_MICROPHONE_DIRECTIONALITY_OMNI; | 
|  | 7063 | characteristic.num_frequency_responses = 5; | 
|  | 7064 | for (size_t i = 0; i < characteristic.num_frequency_responses; i++) { | 
|  | 7065 | characteristic.frequency_responses[0][i] = 100.0f - i; | 
|  | 7066 | characteristic.frequency_responses[1][i] = 100.0f + i; | 
|  | 7067 | } | 
|  | 7068 | for (size_t i = 0; i < AUDIO_CHANNEL_COUNT_MAX; i++) { | 
|  | 7069 | characteristic.channel_mapping[i] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED; | 
|  | 7070 | } | 
|  | 7071 | audio_microphone_channel_mapping_t channel_mappings[] = { | 
|  | 7072 | AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT, | 
|  | 7073 | AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED, | 
|  | 7074 | }; | 
|  | 7075 | for (size_t i = 0; i < mChannelCount; i++) { | 
|  | 7076 | characteristic.channel_mapping[i] = channel_mappings[i % 2]; | 
|  | 7077 | } | 
|  | 7078 | characteristic.geometric_location.x = 0.1f; | 
|  | 7079 | characteristic.geometric_location.y = 0.2f; | 
|  | 7080 | characteristic.geometric_location.z = 0.3f; | 
|  | 7081 | characteristic.orientation.x = 0.0f; | 
|  | 7082 | characteristic.orientation.y = 1.0f; | 
|  | 7083 | characteristic.orientation.z = 0.0f; | 
|  | 7084 | media::MicrophoneInfo microphoneInfo = media::MicrophoneInfo(characteristic); | 
|  | 7085 | activeMicrophones->push_back(microphoneInfo); | 
|  | 7086 | return NO_ERROR; | 
|  | 7087 | } | 
|  | 7088 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7089 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
|  | 7090 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) | 
|  | 7091 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7092 | track->terminate(); | 
|  | 7093 | track->mState = TrackBase::STOPPED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7094 | // active tracks are removed by threadLoop() | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7095 | if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7096 | removeTrack_l(track); | 
|  | 7097 | } | 
|  | 7098 | } | 
|  | 7099 |  | 
|  | 7100 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) | 
|  | 7101 | { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7102 | String8 result; | 
|  | 7103 | track->appendDump(result, false /* active */); | 
|  | 7104 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); | 
|  | 7105 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7106 | mTracks.remove(track); | 
|  | 7107 | // need anything related to effects here? | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7108 | if (track->isFastTrack()) { | 
|  | 7109 | ALOG_ASSERT(!mFastTrackAvail); | 
|  | 7110 | mFastTrackAvail = true; | 
|  | 7111 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7112 | } | 
|  | 7113 |  | 
|  | 7114 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
|  | 7115 | { | 
|  | 7116 | dumpInternals(fd, args); | 
|  | 7117 | dumpTracks(fd, args); | 
|  | 7118 | dumpEffectChains(fd, args); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7119 | dprintf(fd, "  Local log:\n"); | 
|  | 7120 | mLocalLog.dump(fd, "   " /* prefix */, 40 /* lines */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7121 | } | 
|  | 7122 |  | 
|  | 7123 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 7124 | { | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 7125 | dumpBase(fd, args); | 
|  | 7126 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 7127 | AudioStreamIn *input = mInput; | 
|  | 7128 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; | 
|  | 7129 | dprintf(fd, "  AudioStreamIn: %p flags %#x (%s)\n", | 
|  | 7130 | input, flags, inputFlagsToString(flags).c_str()); | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 7131 | if (mActiveTracks.size() == 0) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 7132 | dprintf(fd, "  No active record clients\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7133 | } | 
| Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 7134 |  | 
|  | 7135 | if (input != nullptr) { | 
|  | 7136 | dprintf(fd, "  Hal stream dump:\n"); | 
|  | 7137 | (void)input->stream->dump(fd); | 
|  | 7138 | } | 
|  | 7139 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 7140 | dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7141 | dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); | 
| Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 7142 |  | 
| Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 7143 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us | 
|  | 7144 | // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
|  | 7145 | // This is a large object so we place it on the heap. | 
|  | 7146 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
|  | 7147 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); | 
|  | 7148 | copy->dump(fd); | 
|  | 7149 | delete copy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7150 | } | 
|  | 7151 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 7152 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7153 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7154 | String8 result; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7155 | size_t numtracks = mTracks.size(); | 
|  | 7156 | size_t numactive = mActiveTracks.size(); | 
|  | 7157 | size_t numactiveseen = 0; | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7158 | dprintf(fd, "  %zu Tracks", numtracks); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7159 | const char *prefix = "    "; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7160 | if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7161 | dprintf(fd, " of which %zu are active\n", numactive); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7162 | result.append(prefix); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7163 | RecordTrack::appendDumpHeader(result); | 
|  | 7164 | for (size_t i = 0; i < numtracks ; ++i) { | 
|  | 7165 | sp<RecordTrack> track = mTracks[i]; | 
|  | 7166 | if (track != 0) { | 
|  | 7167 | bool active = mActiveTracks.indexOf(track) >= 0; | 
|  | 7168 | if (active) { | 
|  | 7169 | numactiveseen++; | 
|  | 7170 | } | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7171 | result.append(prefix); | 
|  | 7172 | track->appendDump(result, active); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7173 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7174 | } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7175 | } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 7176 | dprintf(fd, "\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7177 | } | 
|  | 7178 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7179 | if (numactiveseen != numactive) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7180 | result.append("  The following tracks are in the active list but" | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7181 | " not in the track list\n"); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7182 | result.append(prefix); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7183 | RecordTrack::appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7184 | for (size_t i = 0; i < numactive; ++i) { | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7185 | sp<RecordTrack> track = mActiveTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7186 | if (mTracks.indexOf(track) < 0) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7187 | result.append(prefix); | 
|  | 7188 | track->appendDump(result, true /* active */); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 7189 | } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7190 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7191 |  | 
|  | 7192 | } | 
|  | 7193 | write(fd, result.string(), result.size()); | 
|  | 7194 | } | 
|  | 7195 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7196 | void AudioFlinger::RecordThread::setRecordSilenced(uid_t uid, bool silenced) | 
|  | 7197 | { | 
|  | 7198 | Mutex::Autolock _l(mLock); | 
|  | 7199 | for (size_t i = 0; i < mTracks.size() ; i++) { | 
|  | 7200 | sp<RecordTrack> track = mTracks[i]; | 
|  | 7201 | if (track != 0 && track->uid() == uid) { | 
|  | 7202 | track->setSilenced(silenced); | 
|  | 7203 | } | 
|  | 7204 | } | 
|  | 7205 | } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7206 |  | 
|  | 7207 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() | 
|  | 7208 | { | 
|  | 7209 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
|  | 7210 | RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
|  | 7211 | mRsmpInFront = recordThread->mRsmpInRear; | 
|  | 7212 | mRsmpInUnrel = 0; | 
|  | 7213 | } | 
|  | 7214 |  | 
|  | 7215 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( | 
|  | 7216 | size_t *framesAvailable, bool *hasOverrun) | 
|  | 7217 | { | 
|  | 7218 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
|  | 7219 | RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
|  | 7220 | const int32_t rear = recordThread->mRsmpInRear; | 
|  | 7221 | const int32_t front = mRsmpInFront; | 
|  | 7222 | const ssize_t filled = rear - front; | 
|  | 7223 |  | 
|  | 7224 | size_t framesIn; | 
|  | 7225 | bool overrun = false; | 
|  | 7226 | if (filled < 0) { | 
|  | 7227 | // should not happen, but treat like a massive overrun and re-sync | 
|  | 7228 | framesIn = 0; | 
|  | 7229 | mRsmpInFront = rear; | 
|  | 7230 | overrun = true; | 
|  | 7231 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { | 
|  | 7232 | framesIn = (size_t) filled; | 
|  | 7233 | } else { | 
|  | 7234 | // client is not keeping up with server, but give it latest data | 
|  | 7235 | framesIn = recordThread->mRsmpInFrames; | 
|  | 7236 | mRsmpInFront = /* front = */ rear - framesIn; | 
|  | 7237 | overrun = true; | 
|  | 7238 | } | 
|  | 7239 | if (framesAvailable != NULL) { | 
|  | 7240 | *framesAvailable = framesIn; | 
|  | 7241 | } | 
|  | 7242 | if (hasOverrun != NULL) { | 
|  | 7243 | *hasOverrun = overrun; | 
|  | 7244 | } | 
|  | 7245 | } | 
|  | 7246 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7247 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7248 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7249 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7250 | { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7251 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7252 | if (threadBase == 0) { | 
|  | 7253 | buffer->frameCount = 0; | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7254 | buffer->raw = NULL; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7255 | return NOT_ENOUGH_DATA; | 
|  | 7256 | } | 
|  | 7257 | RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
|  | 7258 | int32_t rear = recordThread->mRsmpInRear; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7259 | int32_t front = mRsmpInFront; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7260 | ssize_t filled = rear - front; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7261 | // FIXME should not be P2 (don't want to increase latency) | 
|  | 7262 | // FIXME if client not keeping up, discard | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7263 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7264 | // 'filled' may be non-contiguous, so return only the first contiguous chunk | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7265 | front &= recordThread->mRsmpInFramesP2 - 1; | 
|  | 7266 | size_t part1 = recordThread->mRsmpInFramesP2 - front; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7267 | if (part1 > (size_t) filled) { | 
|  | 7268 | part1 = filled; | 
|  | 7269 | } | 
|  | 7270 | size_t ask = buffer->frameCount; | 
|  | 7271 | ALOG_ASSERT(ask > 0); | 
|  | 7272 | if (part1 > ask) { | 
|  | 7273 | part1 = ask; | 
|  | 7274 | } | 
|  | 7275 | if (part1 == 0) { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7276 | // 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] | 7277 | buffer->raw = NULL; | 
|  | 7278 | buffer->frameCount = 0; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7279 | mRsmpInUnrel = 0; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7280 | return NOT_ENOUGH_DATA; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7281 | } | 
|  | 7282 |  | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7283 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7284 | buffer->frameCount = part1; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7285 | mRsmpInUnrel = part1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7286 | return NO_ERROR; | 
|  | 7287 | } | 
|  | 7288 |  | 
|  | 7289 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7290 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( | 
|  | 7291 | AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7292 | { | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7293 | size_t stepCount = buffer->frameCount; | 
|  | 7294 | if (stepCount == 0) { | 
|  | 7295 | return; | 
|  | 7296 | } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7297 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); | 
|  | 7298 | mRsmpInUnrel -= stepCount; | 
|  | 7299 | mRsmpInFront += stepCount; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7300 | buffer->raw = NULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7301 | buffer->frameCount = 0; | 
|  | 7302 | } | 
|  | 7303 |  | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7304 | void AudioFlinger::RecordThread::checkBtNrec() | 
|  | 7305 | { | 
|  | 7306 | Mutex::Autolock _l(mLock); | 
|  | 7307 | checkBtNrec_l(); | 
|  | 7308 | } | 
|  | 7309 |  | 
|  | 7310 | void AudioFlinger::RecordThread::checkBtNrec_l() | 
|  | 7311 | { | 
|  | 7312 | // disable AEC and NS if the device is a BT SCO headset supporting those | 
|  | 7313 | // pre processings | 
|  | 7314 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
|  | 7315 | mAudioFlinger->btNrecIsOff(); | 
|  | 7316 | if (mBtNrecSuspended.exchange(suspend) != suspend) { | 
|  | 7317 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7318 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); | 
|  | 7319 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); | 
|  | 7320 | } | 
|  | 7321 | } | 
|  | 7322 | } | 
|  | 7323 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7324 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7325 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 7326 | status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7327 | { | 
|  | 7328 | bool reconfig = false; | 
|  | 7329 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7330 | status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7331 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7332 | audio_format_t reqFormat = mFormat; | 
|  | 7333 | uint32_t samplingRate = mSampleRate; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7334 | // 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] | 7335 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); | 
|  | 7336 |  | 
|  | 7337 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 7338 | int value; | 
| Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7339 |  | 
|  | 7340 | // scope for AutoPark extends to end of method | 
|  | 7341 | AutoPark<FastCapture> park(mFastCapture); | 
|  | 7342 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7343 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and | 
|  | 7344 | //      channel count change can be requested. Do we mandate the first client defines the | 
|  | 7345 | //      HAL sampling rate and channel count or do we allow changes on the fly? | 
|  | 7346 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 7347 | samplingRate = value; | 
|  | 7348 | reconfig = true; | 
|  | 7349 | } | 
|  | 7350 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7351 | if (!audio_is_linear_pcm((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7352 | status = BAD_VALUE; | 
|  | 7353 | } else { | 
|  | 7354 | reqFormat = (audio_format_t) value; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7355 | reconfig = true; | 
|  | 7356 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7357 | } | 
|  | 7358 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
|  | 7359 | audio_channel_mask_t mask = (audio_channel_mask_t) value; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7360 | if (!audio_is_input_channel(mask) || | 
|  | 7361 | audio_channel_count_from_in_mask(mask) > FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7362 | status = BAD_VALUE; | 
|  | 7363 | } else { | 
|  | 7364 | channelMask = mask; | 
|  | 7365 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7366 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7367 | } | 
|  | 7368 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 7369 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 7370 | // size depends on frame count and correct behavior would not be guaranteed | 
|  | 7371 | // if frame count is changed after track creation | 
|  | 7372 | if (mActiveTracks.size() > 0) { | 
|  | 7373 | status = INVALID_OPERATION; | 
|  | 7374 | } else { | 
|  | 7375 | reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7376 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7377 | } | 
|  | 7378 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 7379 | // forward device change to effects that have requested to be | 
|  | 7380 | // aware of attached audio device. | 
|  | 7381 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7382 | mEffectChains[i]->setDevice_l(value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7383 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7384 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7385 | // store input device and output device but do not forward output device to audio HAL. | 
|  | 7386 | // Note that status is ignored by the caller for output device | 
|  | 7387 | // (see AudioFlinger::setParameters() | 
|  | 7388 | if (audio_is_output_devices(value)) { | 
|  | 7389 | mOutDevice = value; | 
|  | 7390 | status = BAD_VALUE; | 
|  | 7391 | } else { | 
|  | 7392 | mInDevice = value; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7393 | if (value != AUDIO_DEVICE_NONE) { | 
|  | 7394 | mPrevInDevice = value; | 
|  | 7395 | } | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7396 | checkBtNrec_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7397 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7398 | } | 
|  | 7399 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
|  | 7400 | mAudioSource != (audio_source_t)value) { | 
|  | 7401 | // forward device change to effects that have requested to be | 
|  | 7402 | // aware of attached audio device. | 
|  | 7403 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7404 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7405 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7406 | mAudioSource = (audio_source_t)value; | 
|  | 7407 | } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7408 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7409 | if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7410 | status = mInput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7411 | if (status == INVALID_OPERATION) { | 
|  | 7412 | inputStandBy(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7413 | status = mInput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7414 | } | 
|  | 7415 | if (reconfig) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7416 | if (status == BAD_VALUE) { | 
|  | 7417 | uint32_t sRate; | 
|  | 7418 | audio_channel_mask_t channelMask; | 
|  | 7419 | audio_format_t format; | 
|  | 7420 | if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK && | 
|  | 7421 | audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) && | 
|  | 7422 | sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && | 
|  | 7423 | audio_channel_count_from_in_mask(channelMask) <= FCC_8) { | 
|  | 7424 | status = NO_ERROR; | 
|  | 7425 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7426 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7427 | if (status == NO_ERROR) { | 
|  | 7428 | readInputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7429 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7430 | } | 
|  | 7431 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7432 | } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7433 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7434 | return reconfig; | 
|  | 7435 | } | 
|  | 7436 |  | 
|  | 7437 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
|  | 7438 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7439 | Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7440 | if (initCheck() == NO_ERROR) { | 
|  | 7441 | String8 out_s8; | 
|  | 7442 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { | 
|  | 7443 | return out_s8; | 
|  | 7444 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7445 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7446 | return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7447 | } | 
|  | 7448 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7449 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7450 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
|  | 7451 |  | 
|  | 7452 | desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7453 |  | 
|  | 7454 | switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7455 | case AUDIO_INPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 7456 | case AUDIO_INPUT_REGISTERED: | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7457 | case AUDIO_INPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7458 | desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7459 | desc->mChannelMask = mChannelMask; | 
|  | 7460 | desc->mSamplingRate = mSampleRate; | 
|  | 7461 | desc->mFormat = mFormat; | 
|  | 7462 | desc->mFrameCount = mFrameCount; | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7463 | desc->mFrameCountHAL = mFrameCount; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7464 | desc->mLatency = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7465 | break; | 
|  | 7466 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7467 | case AUDIO_INPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7468 | default: | 
|  | 7469 | break; | 
|  | 7470 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7471 | mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7472 | } | 
|  | 7473 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7474 | void AudioFlinger::RecordThread::readInputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7475 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7476 | status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); | 
|  | 7477 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7478 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7479 | LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_8, "HAL channel count %d > %d", mChannelCount, FCC_8); | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7480 | mFormat = mHALFormat; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7481 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); | 
|  | 7482 | result = mInput->stream->getFrameSize(&mFrameSize); | 
|  | 7483 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); | 
|  | 7484 | result = mInput->stream->getBufferSize(&mBufferSize); | 
|  | 7485 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); | 
| Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7486 | mFrameCount = mBufferSize / mFrameSize; | 
| Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7487 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, " | 
|  | 7488 | "mBufferSize=%lld, mFrameCount=%lld", | 
|  | 7489 | this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize, | 
|  | 7490 | (long long)mFrameCount); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7491 | // This is the formula for calculating the temporary buffer size. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7492 | // 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] | 7493 | // 1 full output buffer, regardless of the alignment of the available input. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7494 | // The value is somewhat arbitrary, and could probably be even larger. | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7495 | // A larger value should allow more old data to be read after a track calls start(), | 
|  | 7496 | // without increasing latency. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7497 | // | 
|  | 7498 | // Note this is independent of the maximum downsampling ratio permitted for capture. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7499 | mRsmpInFrames = mFrameCount * 7; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7500 | mRsmpInFramesP2 = roundup(mRsmpInFrames); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7501 | free(mRsmpInBuffer); | 
| Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7502 | mRsmpInBuffer = NULL; | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7503 |  | 
|  | 7504 | // TODO optimize audio capture buffer sizes ... | 
|  | 7505 | // Here we calculate the size of the sliding buffer used as a source | 
|  | 7506 | // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7). | 
|  | 7507 | // For current HAL frame counts, this is usually 2048 = 40 ms.  It would | 
|  | 7508 | // be better to have it derived from the pipe depth in the long term. | 
|  | 7509 | // The current value is higher than necessary.  However it should not add to latency. | 
|  | 7510 |  | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7511 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 7512 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; | 
|  | 7513 | (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7514 | // if posix_memalign fails, will segv here. | 
|  | 7515 | memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7516 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7517 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. | 
|  | 7518 | // 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] | 7519 | } | 
|  | 7520 |  | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7521 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7522 | { | 
|  | 7523 | Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7524 | uint32_t result; | 
|  | 7525 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { | 
|  | 7526 | return result; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7527 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7528 | return 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7529 | } | 
|  | 7530 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7531 | // hasAudioSession_l() must be called with ThreadBase::mLock held | 
|  | 7532 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7533 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7534 | uint32_t result = 0; | 
|  | 7535 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 7536 | result = EFFECT_SESSION; | 
|  | 7537 | } | 
|  | 7538 |  | 
|  | 7539 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 7540 | if (sessionId == mTracks[i]->sessionId()) { | 
|  | 7541 | result |= TRACK_SESSION; | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7542 | if (mTracks[i]->isFastTrack()) { | 
|  | 7543 | result |= FAST_SESSION; | 
|  | 7544 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7545 | break; | 
|  | 7546 | } | 
|  | 7547 | } | 
|  | 7548 |  | 
|  | 7549 | return result; | 
|  | 7550 | } | 
|  | 7551 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7552 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7553 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7554 | KeyedVector<audio_session_t, bool> ids; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7555 | Mutex::Autolock _l(mLock); | 
|  | 7556 | for (size_t j = 0; j < mTracks.size(); ++j) { | 
|  | 7557 | sp<RecordThread::RecordTrack> track = mTracks[j]; | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7558 | audio_session_t sessionId = track->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7559 | if (ids.indexOfKey(sessionId) < 0) { | 
|  | 7560 | ids.add(sessionId, true); | 
|  | 7561 | } | 
|  | 7562 | } | 
|  | 7563 | return ids; | 
|  | 7564 | } | 
|  | 7565 |  | 
|  | 7566 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
|  | 7567 | { | 
|  | 7568 | Mutex::Autolock _l(mLock); | 
|  | 7569 | AudioStreamIn *input = mInput; | 
|  | 7570 | mInput = NULL; | 
|  | 7571 | return input; | 
|  | 7572 | } | 
|  | 7573 |  | 
|  | 7574 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7575 | sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7576 | { | 
|  | 7577 | if (mInput == NULL) { | 
|  | 7578 | return NULL; | 
|  | 7579 | } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7580 | return mInput->stream; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7581 | } | 
|  | 7582 |  | 
|  | 7583 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 7584 | { | 
|  | 7585 | // only one chain per input thread | 
|  | 7586 | if (mEffectChains.size() != 0) { | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7587 | 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] | 7588 | return INVALID_OPERATION; | 
|  | 7589 | } | 
|  | 7590 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7591 | chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7592 | chain->setInBuffer(NULL); | 
|  | 7593 | chain->setOutBuffer(NULL); | 
|  | 7594 |  | 
|  | 7595 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 7596 |  | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7597 | // make sure enabled pre processing effects state is communicated to the HAL as we | 
|  | 7598 | // just moved them to a new input stream. | 
|  | 7599 | chain->syncHalEffectsState(); | 
|  | 7600 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7601 | mEffectChains.add(chain); | 
|  | 7602 |  | 
|  | 7603 | return NO_ERROR; | 
|  | 7604 | } | 
|  | 7605 |  | 
|  | 7606 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 7607 | { | 
|  | 7608 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
|  | 7609 | ALOGW_IF(mEffectChains.size() != 1, | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7610 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7611 | chain.get(), mEffectChains.size(), this); | 
|  | 7612 | if (mEffectChains.size() == 1) { | 
|  | 7613 | mEffectChains.removeAt(0); | 
|  | 7614 | } | 
|  | 7615 | return 0; | 
|  | 7616 | } | 
|  | 7617 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7618 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 7619 | audio_patch_handle_t *handle) | 
|  | 7620 | { | 
|  | 7621 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7622 |  | 
|  | 7623 | // store new device and send to effects | 
|  | 7624 | mInDevice = patch->sources[0].ext.device.type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7625 | mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7626 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 7627 | mEffectChains[i]->setDevice_l(mInDevice); | 
|  | 7628 | } | 
|  | 7629 |  | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 7630 | checkBtNrec_l(); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7631 |  | 
|  | 7632 | // store new source and send to effects | 
|  | 7633 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
|  | 7634 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7635 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7636 | mEffectChains[i]->setAudioSource_l(mAudioSource); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7637 | } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7638 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7639 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7640 | if (mInput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7641 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); | 
|  | 7642 | status = hwDevice->createAudioPatch(patch->num_sources, | 
|  | 7643 | patch->sources, | 
|  | 7644 | patch->num_sinks, | 
|  | 7645 | patch->sinks, | 
|  | 7646 | handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7647 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7648 | char *address; | 
|  | 7649 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { | 
|  | 7650 | address = audio_device_address_to_parameter( | 
|  | 7651 | patch->sources[0].ext.device.type, | 
|  | 7652 | patch->sources[0].ext.device.address); | 
|  | 7653 | } else { | 
|  | 7654 | address = (char *)calloc(1, 1); | 
|  | 7655 | } | 
|  | 7656 | AudioParameter param = AudioParameter(String8(address)); | 
|  | 7657 | free(address); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7658 | param.addInt(String8(AudioParameter::keyRouting), | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7659 | (int)patch->sources[0].ext.device.type); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7660 | param.addInt(String8(AudioParameter::keyInputSource), | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7661 | (int)patch->sinks[0].ext.mix.usecase.source); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7662 | status = mInput->stream->setParameters(param.toString()); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7663 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7664 | } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7665 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7666 | if (mInDevice != mPrevInDevice) { | 
|  | 7667 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
|  | 7668 | mPrevInDevice = mInDevice; | 
|  | 7669 | } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7670 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7671 | return status; | 
|  | 7672 | } | 
|  | 7673 |  | 
|  | 7674 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 7675 | { | 
|  | 7676 | status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7677 |  | 
|  | 7678 | mInDevice = AUDIO_DEVICE_NONE; | 
|  | 7679 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7680 | if (mInput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7681 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); | 
|  | 7682 | status = hwDevice->releaseAudioPatch(handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7683 | } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7684 | AudioParameter param; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7685 | param.addInt(String8(AudioParameter::keyRouting), 0); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7686 | status = mInput->stream->setParameters(param.toString()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7687 | } | 
|  | 7688 | return status; | 
|  | 7689 | } | 
|  | 7690 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7691 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) | 
|  | 7692 | { | 
|  | 7693 | Mutex::Autolock _l(mLock); | 
|  | 7694 | mTracks.add(record); | 
|  | 7695 | } | 
|  | 7696 |  | 
|  | 7697 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) | 
|  | 7698 | { | 
|  | 7699 | Mutex::Autolock _l(mLock); | 
|  | 7700 | destroyTrack_l(record); | 
|  | 7701 | } | 
|  | 7702 |  | 
|  | 7703 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) | 
|  | 7704 | { | 
|  | 7705 | ThreadBase::getAudioPortConfig(config); | 
|  | 7706 | config->role = AUDIO_PORT_ROLE_SINK; | 
|  | 7707 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); | 
|  | 7708 | config->ext.mix.usecase.source = mAudioSource; | 
|  | 7709 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7710 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7711 | // ---------------------------------------------------------------------------- | 
|  | 7712 | //      Mmap | 
|  | 7713 | // ---------------------------------------------------------------------------- | 
|  | 7714 |  | 
|  | 7715 | AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread) | 
|  | 7716 | : mThread(thread) | 
|  | 7717 | { | 
| Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7718 | assert(thread != 0); // thread must start non-null and stay non-null | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7719 | } | 
|  | 7720 |  | 
|  | 7721 | AudioFlinger::MmapThreadHandle::~MmapThreadHandle() | 
|  | 7722 | { | 
| Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7723 | mThread->disconnect(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7724 | } | 
|  | 7725 |  | 
|  | 7726 | status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, | 
|  | 7727 | struct audio_mmap_buffer_info *info) | 
|  | 7728 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7729 | return mThread->createMmapBuffer(minSizeFrames, info); | 
|  | 7730 | } | 
|  | 7731 |  | 
|  | 7732 | status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position) | 
|  | 7733 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7734 | return mThread->getMmapPosition(position); | 
|  | 7735 | } | 
|  | 7736 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7737 | status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client, | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7738 | audio_port_handle_t *handle) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7739 |  | 
|  | 7740 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7741 | return mThread->start(client, handle); | 
|  | 7742 | } | 
|  | 7743 |  | 
|  | 7744 | status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle) | 
|  | 7745 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7746 | return mThread->stop(handle); | 
|  | 7747 | } | 
|  | 7748 |  | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7749 | status_t AudioFlinger::MmapThreadHandle::standby() | 
|  | 7750 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7751 | return mThread->standby(); | 
|  | 7752 | } | 
|  | 7753 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7754 |  | 
|  | 7755 | AudioFlinger::MmapThread::MmapThread( | 
|  | 7756 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
|  | 7757 | AudioHwDevice *hwDev, sp<StreamHalInterface> stream, | 
|  | 7758 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) | 
|  | 7759 | : ThreadBase(audioFlinger, id, outDevice, inDevice, MMAP, systemReady), | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7760 | mSessionId(AUDIO_SESSION_NONE), | 
|  | 7761 | mDeviceId(AUDIO_PORT_HANDLE_NONE), mPortId(AUDIO_PORT_HANDLE_NONE), | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 7762 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), | 
|  | 7763 | mActiveTracks(&this->mLocalLog) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7764 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7765 | mStandby = true; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7766 | readHalParameters_l(); | 
|  | 7767 | } | 
|  | 7768 |  | 
|  | 7769 | AudioFlinger::MmapThread::~MmapThread() | 
|  | 7770 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7771 | releaseWakeLock_l(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7772 | } | 
|  | 7773 |  | 
|  | 7774 | void AudioFlinger::MmapThread::onFirstRef() | 
|  | 7775 | { | 
|  | 7776 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
|  | 7777 | } | 
|  | 7778 |  | 
|  | 7779 | void AudioFlinger::MmapThread::disconnect() | 
|  | 7780 | { | 
|  | 7781 | for (const sp<MmapTrack> &t : mActiveTracks) { | 
|  | 7782 | stop(t->portId()); | 
|  | 7783 | } | 
| Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 7784 | // This will decrement references and may cause the destruction of this thread. | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7785 | if (isOutput()) { | 
|  | 7786 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); | 
|  | 7787 | } else { | 
| Eric Laurent | e9ebcdb | 2018-01-29 14:27:18 -0800 | [diff] [blame] | 7788 | AudioSystem::releaseInput(mId, mSessionId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7789 | } | 
|  | 7790 | } | 
|  | 7791 |  | 
|  | 7792 |  | 
|  | 7793 | void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr, | 
|  | 7794 | audio_stream_type_t streamType __unused, | 
|  | 7795 | audio_session_t sessionId, | 
|  | 7796 | const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7797 | audio_port_handle_t deviceId, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7798 | audio_port_handle_t portId) | 
|  | 7799 | { | 
|  | 7800 | mAttr = *attr; | 
|  | 7801 | mSessionId = sessionId; | 
|  | 7802 | mCallback = callback; | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7803 | mDeviceId = deviceId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7804 | mPortId = portId; | 
|  | 7805 | } | 
|  | 7806 |  | 
|  | 7807 | status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames, | 
|  | 7808 | struct audio_mmap_buffer_info *info) | 
|  | 7809 | { | 
|  | 7810 | if (mHalStream == 0) { | 
|  | 7811 | return NO_INIT; | 
|  | 7812 | } | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7813 | mStandby = true; | 
|  | 7814 | acquireWakeLock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7815 | return mHalStream->createMmapBuffer(minSizeFrames, info); | 
|  | 7816 | } | 
|  | 7817 |  | 
|  | 7818 | status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position) | 
|  | 7819 | { | 
|  | 7820 | if (mHalStream == 0) { | 
|  | 7821 | return NO_INIT; | 
|  | 7822 | } | 
|  | 7823 | return mHalStream->getMmapPosition(position); | 
|  | 7824 | } | 
|  | 7825 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7826 | status_t AudioFlinger::MmapThread::start(const AudioClient& client, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7827 | audio_port_handle_t *handle) | 
|  | 7828 | { | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7829 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, | 
|  | 7830 | client.clientUid, mStandby, mPortId, *handle); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7831 | if (mHalStream == 0) { | 
|  | 7832 | return NO_INIT; | 
|  | 7833 | } | 
|  | 7834 |  | 
|  | 7835 | status_t ret; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7836 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7837 | if (*handle == mPortId) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7838 | // for the first track, reuse portId and session allocated when the stream was opened | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7839 | ret = mHalStream->start(); | 
|  | 7840 | if (ret != NO_ERROR) { | 
|  | 7841 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); | 
|  | 7842 | return ret; | 
|  | 7843 | } | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7844 | mStandby = false; | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7845 | return NO_ERROR; | 
|  | 7846 | } | 
|  | 7847 |  | 
| Eric Laurent | 67651f9 | 2018-01-29 14:27:03 -0800 | [diff] [blame] | 7848 | if (!isOutput() && !recordingAllowed(client.packageName, client.clientPid, client.clientUid)) { | 
|  | 7849 | return PERMISSION_DENIED; | 
|  | 7850 | } | 
|  | 7851 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7852 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; | 
|  | 7853 |  | 
|  | 7854 | audio_io_handle_t io = mId; | 
|  | 7855 | if (isOutput()) { | 
|  | 7856 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 7857 | config.sample_rate = mSampleRate; | 
|  | 7858 | config.channel_mask = mChannelMask; | 
|  | 7859 | config.format = mFormat; | 
|  | 7860 | audio_stream_type_t stream = streamType(); | 
|  | 7861 | audio_output_flags_t flags = | 
|  | 7862 | (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7863 | audio_port_handle_t deviceId = mDeviceId; | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7864 | ret = AudioSystem::getOutputForAttr(&mAttr, &io, | 
|  | 7865 | mSessionId, | 
|  | 7866 | &stream, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 7867 | client.clientPid, | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7868 | client.clientUid, | 
|  | 7869 | &config, | 
|  | 7870 | flags, | 
|  | 7871 | &deviceId, | 
|  | 7872 | &portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7873 | } else { | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7874 | audio_config_base_t config; | 
|  | 7875 | config.sample_rate = mSampleRate; | 
|  | 7876 | config.channel_mask = mChannelMask; | 
|  | 7877 | config.format = mFormat; | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 7878 | audio_port_handle_t deviceId = mDeviceId; | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7879 | ret = AudioSystem::getInputForAttr(&mAttr, &io, | 
|  | 7880 | mSessionId, | 
|  | 7881 | client.clientPid, | 
|  | 7882 | client.clientUid, | 
|  | 7883 | &config, | 
|  | 7884 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, | 
|  | 7885 | &deviceId, | 
|  | 7886 | &portId); | 
|  | 7887 | } | 
|  | 7888 | // APM should not chose a different input or output stream for the same set of attributes | 
|  | 7889 | // and audo configuration | 
|  | 7890 | if (ret != NO_ERROR || io != mId) { | 
|  | 7891 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", | 
|  | 7892 | __FUNCTION__, ret, io, mId); | 
|  | 7893 | return BAD_VALUE; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7894 | } | 
|  | 7895 |  | 
|  | 7896 | if (isOutput()) { | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7897 | ret = AudioSystem::startOutput(mId, streamType(), mSessionId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7898 | } else { | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7899 | // TODO: Block recording for idle UIDs (b/72134552) | 
|  | 7900 | bool silenced; | 
| Eric Laurent | e9ebcdb | 2018-01-29 14:27:18 -0800 | [diff] [blame] | 7901 | ret = AudioSystem::startInput(mId, mSessionId, mInDevice, client.clientUid, &silenced); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7902 | } | 
|  | 7903 |  | 
|  | 7904 | // abort if start is rejected by audio policy manager | 
|  | 7905 | if (ret != NO_ERROR) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7906 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7907 | if (mActiveTracks.size() != 0) { | 
|  | 7908 | if (isOutput()) { | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7909 | AudioSystem::releaseOutput(mId, streamType(), mSessionId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7910 | } else { | 
| Eric Laurent | e9ebcdb | 2018-01-29 14:27:18 -0800 | [diff] [blame] | 7911 | AudioSystem::releaseInput(mId, mSessionId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7912 | } | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7913 | } else { | 
|  | 7914 | mHalStream->stop(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7915 | } | 
|  | 7916 | return PERMISSION_DENIED; | 
|  | 7917 | } | 
|  | 7918 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7919 | sp<MmapTrack> track = new MmapTrack(this, mSampleRate, mFormat, mChannelMask, mSessionId, | 
|  | 7920 | client.clientUid, client.clientPid, portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7921 |  | 
|  | 7922 | mActiveTracks.add(track); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7923 | sp<EffectChain> chain = getEffectChain_l(mSessionId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7924 | if (chain != 0) { | 
|  | 7925 | chain->setStrategy(AudioSystem::getStrategyForStream(streamType())); | 
|  | 7926 | chain->incTrackCnt(); | 
|  | 7927 | chain->incActiveTrackCnt(); | 
|  | 7928 | } | 
|  | 7929 |  | 
|  | 7930 | *handle = portId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7931 | broadcast_l(); | 
|  | 7932 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7933 | ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7934 |  | 
|  | 7935 | return NO_ERROR; | 
|  | 7936 | } | 
|  | 7937 |  | 
|  | 7938 | status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle) | 
|  | 7939 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7940 | ALOGV("%s handle %d", __FUNCTION__, handle); | 
|  | 7941 |  | 
|  | 7942 | if (mHalStream == 0) { | 
|  | 7943 | return NO_INIT; | 
|  | 7944 | } | 
|  | 7945 |  | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7946 | if (handle == mPortId) { | 
|  | 7947 | mHalStream->stop(); | 
|  | 7948 | return NO_ERROR; | 
|  | 7949 | } | 
|  | 7950 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7951 | sp<MmapTrack> track; | 
|  | 7952 | for (const sp<MmapTrack> &t : mActiveTracks) { | 
|  | 7953 | if (handle == t->portId()) { | 
|  | 7954 | track = t; | 
|  | 7955 | break; | 
|  | 7956 | } | 
|  | 7957 | } | 
|  | 7958 | if (track == 0) { | 
|  | 7959 | return BAD_VALUE; | 
|  | 7960 | } | 
|  | 7961 |  | 
|  | 7962 | mActiveTracks.remove(track); | 
|  | 7963 |  | 
|  | 7964 | if (isOutput()) { | 
|  | 7965 | AudioSystem::stopOutput(mId, streamType(), track->sessionId()); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 7966 | AudioSystem::releaseOutput(mId, streamType(), track->sessionId()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7967 | } else { | 
| Eric Laurent | e9ebcdb | 2018-01-29 14:27:18 -0800 | [diff] [blame] | 7968 | AudioSystem::stopInput(mId, track->sessionId()); | 
|  | 7969 | AudioSystem::releaseInput(mId, track->sessionId()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7970 | } | 
|  | 7971 |  | 
|  | 7972 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 7973 | if (chain != 0) { | 
|  | 7974 | chain->decActiveTrackCnt(); | 
|  | 7975 | chain->decTrackCnt(); | 
|  | 7976 | } | 
|  | 7977 |  | 
|  | 7978 | broadcast_l(); | 
|  | 7979 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7980 | return NO_ERROR; | 
|  | 7981 | } | 
|  | 7982 |  | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7983 | status_t AudioFlinger::MmapThread::standby() | 
|  | 7984 | { | 
|  | 7985 | ALOGV("%s", __FUNCTION__); | 
|  | 7986 |  | 
|  | 7987 | if (mHalStream == 0) { | 
|  | 7988 | return NO_INIT; | 
|  | 7989 | } | 
|  | 7990 | if (mActiveTracks.size() != 0) { | 
|  | 7991 | return INVALID_OPERATION; | 
|  | 7992 | } | 
|  | 7993 | mHalStream->standby(); | 
|  | 7994 | mStandby = true; | 
|  | 7995 | releaseWakeLock(); | 
|  | 7996 | return NO_ERROR; | 
|  | 7997 | } | 
|  | 7998 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7999 |  | 
|  | 8000 | void AudioFlinger::MmapThread::readHalParameters_l() | 
|  | 8001 | { | 
|  | 8002 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); | 
|  | 8003 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); | 
|  | 8004 | mFormat = mHALFormat; | 
|  | 8005 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); | 
|  | 8006 | result = mHalStream->getFrameSize(&mFrameSize); | 
|  | 8007 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); | 
|  | 8008 | result = mHalStream->getBufferSize(&mBufferSize); | 
|  | 8009 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); | 
|  | 8010 | mFrameCount = mBufferSize / mFrameSize; | 
|  | 8011 | } | 
|  | 8012 |  | 
|  | 8013 | bool AudioFlinger::MmapThread::threadLoop() | 
|  | 8014 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8015 | checkSilentMode_l(); | 
|  | 8016 |  | 
|  | 8017 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
|  | 8018 |  | 
|  | 8019 | while (!exitPending()) | 
|  | 8020 | { | 
|  | 8021 | Mutex::Autolock _l(mLock); | 
|  | 8022 | Vector< sp<EffectChain> > effectChains; | 
|  | 8023 |  | 
|  | 8024 | if (mSignalPending) { | 
|  | 8025 | // A signal was raised while we were unlocked | 
|  | 8026 | mSignalPending = false; | 
|  | 8027 | } else { | 
|  | 8028 | if (mConfigEvents.isEmpty()) { | 
|  | 8029 | // we're about to wait, flush the binder command buffer | 
|  | 8030 | IPCThreadState::self()->flushCommands(); | 
|  | 8031 |  | 
|  | 8032 | if (exitPending()) { | 
|  | 8033 | break; | 
|  | 8034 | } | 
|  | 8035 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8036 | // wait until we have something to do... | 
|  | 8037 | ALOGV("%s going to sleep", myName.string()); | 
|  | 8038 | mWaitWorkCV.wait(mLock); | 
|  | 8039 | ALOGV("%s waking up", myName.string()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8040 |  | 
|  | 8041 | checkSilentMode_l(); | 
|  | 8042 |  | 
|  | 8043 | continue; | 
|  | 8044 | } | 
|  | 8045 | } | 
|  | 8046 |  | 
|  | 8047 | processConfigEvents_l(); | 
|  | 8048 |  | 
|  | 8049 | processVolume_l(); | 
|  | 8050 |  | 
|  | 8051 | checkInvalidTracks_l(); | 
|  | 8052 |  | 
|  | 8053 | mActiveTracks.updatePowerState(this); | 
|  | 8054 |  | 
|  | 8055 | lockEffectChains_l(effectChains); | 
|  | 8056 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 8057 | effectChains[i]->process_l(); | 
|  | 8058 | } | 
|  | 8059 | // enable changes in effect chain | 
|  | 8060 | unlockEffectChains(effectChains); | 
|  | 8061 | // Effect chains will be actually deleted here if they were removed from | 
|  | 8062 | // mEffectChains list during mixing or effects processing | 
|  | 8063 | } | 
|  | 8064 |  | 
|  | 8065 | threadLoop_exit(); | 
|  | 8066 |  | 
|  | 8067 | if (!mStandby) { | 
|  | 8068 | threadLoop_standby(); | 
|  | 8069 | mStandby = true; | 
|  | 8070 | } | 
|  | 8071 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8072 | ALOGV("Thread %p type %d exiting", this, mType); | 
|  | 8073 | return false; | 
|  | 8074 | } | 
|  | 8075 |  | 
|  | 8076 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
|  | 8077 | bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair, | 
|  | 8078 | status_t& status) | 
|  | 8079 | { | 
|  | 8080 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 8081 | int value; | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8082 | bool sendToHal = true; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8083 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8084 | audio_devices_t device = (audio_devices_t)value; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8085 | // forward device change to effects that have requested to be | 
|  | 8086 | // aware of attached audio device. | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8087 | if (device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8088 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8089 | mEffectChains[i]->setDevice_l(device); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8090 | } | 
|  | 8091 | } | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8092 | if (audio_is_output_devices(device)) { | 
|  | 8093 | mOutDevice = device; | 
|  | 8094 | if (!isOutput()) { | 
|  | 8095 | sendToHal = false; | 
|  | 8096 | } | 
|  | 8097 | } else { | 
|  | 8098 | mInDevice = device; | 
|  | 8099 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 8100 | mPrevInDevice = value; | 
|  | 8101 | } | 
|  | 8102 | // TODO: implement and call checkBtNrec_l(); | 
|  | 8103 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8104 | } | 
| Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 8105 | if (sendToHal) { | 
|  | 8106 | status = mHalStream->setParameters(keyValuePair); | 
|  | 8107 | } else { | 
|  | 8108 | status = NO_ERROR; | 
|  | 8109 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8110 |  | 
|  | 8111 | return false; | 
|  | 8112 | } | 
|  | 8113 |  | 
|  | 8114 | String8 AudioFlinger::MmapThread::getParameters(const String8& keys) | 
|  | 8115 | { | 
|  | 8116 | Mutex::Autolock _l(mLock); | 
|  | 8117 | String8 out_s8; | 
|  | 8118 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { | 
|  | 8119 | return out_s8; | 
|  | 8120 | } | 
|  | 8121 | return String8(); | 
|  | 8122 | } | 
|  | 8123 |  | 
|  | 8124 | void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
|  | 8125 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
|  | 8126 |  | 
|  | 8127 | desc->mIoHandle = mId; | 
|  | 8128 |  | 
|  | 8129 | switch (event) { | 
|  | 8130 | case AUDIO_INPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8131 | case AUDIO_INPUT_REGISTERED: | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8132 | case AUDIO_INPUT_CONFIG_CHANGED: | 
|  | 8133 | case AUDIO_OUTPUT_OPENED: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8134 | case AUDIO_OUTPUT_REGISTERED: | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8135 | case AUDIO_OUTPUT_CONFIG_CHANGED: | 
|  | 8136 | desc->mPatch = mPatch; | 
|  | 8137 | desc->mChannelMask = mChannelMask; | 
|  | 8138 | desc->mSamplingRate = mSampleRate; | 
|  | 8139 | desc->mFormat = mFormat; | 
|  | 8140 | desc->mFrameCount = mFrameCount; | 
|  | 8141 | desc->mFrameCountHAL = mFrameCount; | 
|  | 8142 | desc->mLatency = 0; | 
|  | 8143 | break; | 
|  | 8144 |  | 
|  | 8145 | case AUDIO_INPUT_CLOSED: | 
|  | 8146 | case AUDIO_OUTPUT_CLOSED: | 
|  | 8147 | default: | 
|  | 8148 | break; | 
|  | 8149 | } | 
|  | 8150 | mAudioFlinger->ioConfigChanged(event, desc, pid); | 
|  | 8151 | } | 
|  | 8152 |  | 
|  | 8153 | status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch, | 
|  | 8154 | audio_patch_handle_t *handle) | 
|  | 8155 | { | 
|  | 8156 | status_t status = NO_ERROR; | 
|  | 8157 |  | 
|  | 8158 | // store new device and send to effects | 
|  | 8159 | audio_devices_t type = AUDIO_DEVICE_NONE; | 
|  | 8160 | audio_port_handle_t deviceId; | 
|  | 8161 | if (isOutput()) { | 
|  | 8162 | for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
|  | 8163 | type |= patch->sinks[i].ext.device.type; | 
|  | 8164 | } | 
|  | 8165 | deviceId = patch->sinks[0].id; | 
|  | 8166 | } else { | 
|  | 8167 | type = patch->sources[0].ext.device.type; | 
|  | 8168 | deviceId = patch->sources[0].id; | 
|  | 8169 | } | 
|  | 8170 |  | 
|  | 8171 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 8172 | mEffectChains[i]->setDevice_l(type); | 
|  | 8173 | } | 
|  | 8174 |  | 
|  | 8175 | if (isOutput()) { | 
|  | 8176 | mOutDevice = type; | 
|  | 8177 | } else { | 
|  | 8178 | mInDevice = type; | 
|  | 8179 | // store new source and send to effects | 
|  | 8180 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
|  | 8181 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
|  | 8182 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 8183 | mEffectChains[i]->setAudioSource_l(mAudioSource); | 
|  | 8184 | } | 
|  | 8185 | } | 
|  | 8186 | } | 
|  | 8187 |  | 
|  | 8188 | if (mAudioHwDev->supportsAudioPatches()) { | 
|  | 8189 | status = mHalDevice->createAudioPatch(patch->num_sources, | 
|  | 8190 | patch->sources, | 
|  | 8191 | patch->num_sinks, | 
|  | 8192 | patch->sinks, | 
|  | 8193 | handle); | 
|  | 8194 | } else { | 
|  | 8195 | char *address; | 
|  | 8196 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
|  | 8197 | //FIXME: we only support address on first sink with HAL version < 3.0 | 
|  | 8198 | address = audio_device_address_to_parameter( | 
|  | 8199 | patch->sinks[0].ext.device.type, | 
|  | 8200 | patch->sinks[0].ext.device.address); | 
|  | 8201 | } else { | 
|  | 8202 | address = (char *)calloc(1, 1); | 
|  | 8203 | } | 
|  | 8204 | AudioParameter param = AudioParameter(String8(address)); | 
|  | 8205 | free(address); | 
|  | 8206 | param.addInt(String8(AudioParameter::keyRouting), (int)type); | 
|  | 8207 | if (!isOutput()) { | 
|  | 8208 | param.addInt(String8(AudioParameter::keyInputSource), | 
|  | 8209 | (int)patch->sinks[0].ext.mix.usecase.source); | 
|  | 8210 | } | 
|  | 8211 | status = mHalStream->setParameters(param.toString()); | 
|  | 8212 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 8213 | } | 
|  | 8214 |  | 
|  | 8215 | if (isOutput() && mPrevOutDevice != mOutDevice) { | 
|  | 8216 | mPrevOutDevice = type; | 
|  | 8217 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8218 | sp<MmapStreamCallback> callback = mCallback.promote(); | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8219 | if (mDeviceId != deviceId && callback != 0) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8220 | callback->onRoutingChanged(deviceId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8221 | } | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8222 | mDeviceId = deviceId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8223 | } | 
|  | 8224 | if (!isOutput() && mPrevInDevice != mInDevice) { | 
|  | 8225 | mPrevInDevice = type; | 
|  | 8226 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8227 | sp<MmapStreamCallback> callback = mCallback.promote(); | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8228 | if (mDeviceId != deviceId && callback != 0) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8229 | callback->onRoutingChanged(deviceId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8230 | } | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8231 | mDeviceId = deviceId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8232 | } | 
|  | 8233 | return status; | 
|  | 8234 | } | 
|  | 8235 |  | 
|  | 8236 | status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
|  | 8237 | { | 
|  | 8238 | status_t status = NO_ERROR; | 
|  | 8239 |  | 
|  | 8240 | mInDevice = AUDIO_DEVICE_NONE; | 
|  | 8241 |  | 
|  | 8242 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? | 
|  | 8243 | supportsAudioPatches : false; | 
|  | 8244 |  | 
|  | 8245 | if (supportsAudioPatches) { | 
|  | 8246 | status = mHalDevice->releaseAudioPatch(handle); | 
|  | 8247 | } else { | 
|  | 8248 | AudioParameter param; | 
|  | 8249 | param.addInt(String8(AudioParameter::keyRouting), 0); | 
|  | 8250 | status = mHalStream->setParameters(param.toString()); | 
|  | 8251 | } | 
|  | 8252 | return status; | 
|  | 8253 | } | 
|  | 8254 |  | 
|  | 8255 | void AudioFlinger::MmapThread::getAudioPortConfig(struct audio_port_config *config) | 
|  | 8256 | { | 
|  | 8257 | ThreadBase::getAudioPortConfig(config); | 
|  | 8258 | if (isOutput()) { | 
|  | 8259 | config->role = AUDIO_PORT_ROLE_SOURCE; | 
|  | 8260 | config->ext.mix.hw_module = mAudioHwDev->handle(); | 
|  | 8261 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
|  | 8262 | } else { | 
|  | 8263 | config->role = AUDIO_PORT_ROLE_SINK; | 
|  | 8264 | config->ext.mix.hw_module = mAudioHwDev->handle(); | 
|  | 8265 | config->ext.mix.usecase.source = mAudioSource; | 
|  | 8266 | } | 
|  | 8267 | } | 
|  | 8268 |  | 
|  | 8269 | status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 8270 | { | 
|  | 8271 | audio_session_t session = chain->sessionId(); | 
|  | 8272 |  | 
|  | 8273 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
|  | 8274 | // Attach all tracks with same session ID to this chain. | 
|  | 8275 | // indicate all active tracks in the chain | 
|  | 8276 | for (const sp<MmapTrack> &track : mActiveTracks) { | 
|  | 8277 | if (session == track->sessionId()) { | 
|  | 8278 | chain->incTrackCnt(); | 
|  | 8279 | chain->incActiveTrackCnt(); | 
|  | 8280 | } | 
|  | 8281 | } | 
|  | 8282 |  | 
|  | 8283 | chain->setThread(this); | 
|  | 8284 | chain->setInBuffer(nullptr); | 
|  | 8285 | chain->setOutBuffer(nullptr); | 
|  | 8286 | chain->syncHalEffectsState(); | 
|  | 8287 |  | 
|  | 8288 | mEffectChains.add(chain); | 
|  | 8289 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 8290 | return NO_ERROR; | 
|  | 8291 | } | 
|  | 8292 |  | 
|  | 8293 | size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 8294 | { | 
|  | 8295 | audio_session_t session = chain->sessionId(); | 
|  | 8296 |  | 
|  | 8297 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
|  | 8298 |  | 
|  | 8299 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 8300 | if (chain == mEffectChains[i]) { | 
|  | 8301 | mEffectChains.removeAt(i); | 
|  | 8302 | // detach all active tracks from the chain | 
|  | 8303 | // detach all tracks with same session ID from this chain | 
|  | 8304 | for (const sp<MmapTrack> &track : mActiveTracks) { | 
|  | 8305 | if (session == track->sessionId()) { | 
|  | 8306 | chain->decActiveTrackCnt(); | 
|  | 8307 | chain->decTrackCnt(); | 
|  | 8308 | } | 
|  | 8309 | } | 
|  | 8310 | break; | 
|  | 8311 | } | 
|  | 8312 | } | 
|  | 8313 | return mEffectChains.size(); | 
|  | 8314 | } | 
|  | 8315 |  | 
|  | 8316 | // hasAudioSession_l() must be called with ThreadBase::mLock held | 
|  | 8317 | uint32_t AudioFlinger::MmapThread::hasAudioSession_l(audio_session_t sessionId) const | 
|  | 8318 | { | 
|  | 8319 | uint32_t result = 0; | 
|  | 8320 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 8321 | result = EFFECT_SESSION; | 
|  | 8322 | } | 
|  | 8323 |  | 
|  | 8324 | for (size_t i = 0; i < mActiveTracks.size(); i++) { | 
|  | 8325 | sp<MmapTrack> track = mActiveTracks[i]; | 
|  | 8326 | if (sessionId == track->sessionId()) { | 
|  | 8327 | result |= TRACK_SESSION; | 
|  | 8328 | if (track->isFastTrack()) { | 
|  | 8329 | result |= FAST_SESSION; | 
|  | 8330 | } | 
|  | 8331 | break; | 
|  | 8332 | } | 
|  | 8333 | } | 
|  | 8334 |  | 
|  | 8335 | return result; | 
|  | 8336 | } | 
|  | 8337 |  | 
|  | 8338 | void AudioFlinger::MmapThread::threadLoop_standby() | 
|  | 8339 | { | 
|  | 8340 | mHalStream->standby(); | 
|  | 8341 | } | 
|  | 8342 |  | 
|  | 8343 | void AudioFlinger::MmapThread::threadLoop_exit() | 
|  | 8344 | { | 
| Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 8345 | // Do not call callback->onTearDown() because it is redundant for thread exit | 
|  | 8346 | // and because it can cause a recursive mutex lock on stop(). | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8347 | } | 
|  | 8348 |  | 
|  | 8349 | status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
|  | 8350 | { | 
|  | 8351 | return BAD_VALUE; | 
|  | 8352 | } | 
|  | 8353 |  | 
|  | 8354 | bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
|  | 8355 | { | 
|  | 8356 | return false; | 
|  | 8357 | } | 
|  | 8358 |  | 
|  | 8359 | status_t AudioFlinger::MmapThread::checkEffectCompatibility_l( | 
|  | 8360 | const effect_descriptor_t *desc, audio_session_t sessionId) | 
|  | 8361 | { | 
|  | 8362 | // No global effect sessions on mmap threads | 
|  | 8363 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
|  | 8364 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", | 
|  | 8365 | desc->name, mThreadName); | 
|  | 8366 | return BAD_VALUE; | 
|  | 8367 | } | 
|  | 8368 |  | 
|  | 8369 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { | 
|  | 8370 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", | 
|  | 8371 | desc->name); | 
|  | 8372 | return BAD_VALUE; | 
|  | 8373 | } | 
|  | 8374 | if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8375 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " | 
|  | 8376 | "thread", desc->name); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8377 | return BAD_VALUE; | 
|  | 8378 | } | 
|  | 8379 |  | 
|  | 8380 | // Only allow effects without processing load or latency | 
|  | 8381 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { | 
|  | 8382 | return BAD_VALUE; | 
|  | 8383 | } | 
|  | 8384 |  | 
|  | 8385 | return NO_ERROR; | 
|  | 8386 |  | 
|  | 8387 | } | 
|  | 8388 |  | 
|  | 8389 | void AudioFlinger::MmapThread::checkInvalidTracks_l() | 
|  | 8390 | { | 
|  | 8391 | for (const sp<MmapTrack> &track : mActiveTracks) { | 
|  | 8392 | if (track->isInvalid()) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8393 | sp<MmapStreamCallback> callback = mCallback.promote(); | 
|  | 8394 | if (callback != 0) { | 
|  | 8395 | callback->onTearDown(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8396 | } | 
|  | 8397 | break; | 
|  | 8398 | } | 
|  | 8399 | } | 
|  | 8400 | } | 
|  | 8401 |  | 
|  | 8402 | void AudioFlinger::MmapThread::dump(int fd, const Vector<String16>& args) | 
|  | 8403 | { | 
|  | 8404 | dumpInternals(fd, args); | 
|  | 8405 | dumpTracks(fd, args); | 
|  | 8406 | dumpEffectChains(fd, args); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8407 | dprintf(fd, "  Local log:\n"); | 
|  | 8408 | mLocalLog.dump(fd, "   " /* prefix */, 40 /* lines */); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8409 | } | 
|  | 8410 |  | 
|  | 8411 | void AudioFlinger::MmapThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 8412 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8413 | dumpBase(fd, args); | 
|  | 8414 |  | 
|  | 8415 | dprintf(fd, "  Attributes: content type %d usage %d source %d\n", | 
|  | 8416 | mAttr.content_type, mAttr.usage, mAttr.source); | 
|  | 8417 | dprintf(fd, "  Session: %d port Id: %d\n", mSessionId, mPortId); | 
|  | 8418 | if (mActiveTracks.size() == 0) { | 
|  | 8419 | dprintf(fd, "  No active clients\n"); | 
|  | 8420 | } | 
|  | 8421 | } | 
|  | 8422 |  | 
|  | 8423 | void AudioFlinger::MmapThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
|  | 8424 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8425 | String8 result; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8426 | size_t numtracks = mActiveTracks.size(); | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8427 | dprintf(fd, "  %zu Tracks\n", numtracks); | 
|  | 8428 | const char *prefix = "    "; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8429 | if (numtracks) { | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8430 | result.append(prefix); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8431 | MmapTrack::appendDumpHeader(result); | 
|  | 8432 | for (size_t i = 0; i < numtracks ; ++i) { | 
|  | 8433 | sp<MmapTrack> track = mActiveTracks[i]; | 
| Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8434 | result.append(prefix); | 
|  | 8435 | track->appendDump(result, true /* active */); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8436 | } | 
|  | 8437 | } else { | 
|  | 8438 | dprintf(fd, "\n"); | 
|  | 8439 | } | 
|  | 8440 | write(fd, result.string(), result.size()); | 
|  | 8441 | } | 
|  | 8442 |  | 
|  | 8443 | AudioFlinger::MmapPlaybackThread::MmapPlaybackThread( | 
|  | 8444 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
|  | 8445 | AudioHwDevice *hwDev,  AudioStreamOut *output, | 
|  | 8446 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) | 
|  | 8447 | : MmapThread(audioFlinger, id, hwDev, output->stream, outDevice, inDevice, systemReady), | 
|  | 8448 | mStreamType(AUDIO_STREAM_MUSIC), | 
|  | 8449 | mStreamVolume(1.0), mStreamMute(false), mOutput(output) | 
|  | 8450 | { | 
|  | 8451 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); | 
|  | 8452 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
|  | 8453 | mMasterVolume = audioFlinger->masterVolume_l(); | 
|  | 8454 | mMasterMute = audioFlinger->masterMute_l(); | 
|  | 8455 | if (mAudioHwDev) { | 
|  | 8456 | if (mAudioHwDev->canSetMasterVolume()) { | 
|  | 8457 | mMasterVolume = 1.0; | 
|  | 8458 | } | 
|  | 8459 |  | 
|  | 8460 | if (mAudioHwDev->canSetMasterMute()) { | 
|  | 8461 | mMasterMute = false; | 
|  | 8462 | } | 
|  | 8463 | } | 
|  | 8464 | } | 
|  | 8465 |  | 
|  | 8466 | void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr, | 
|  | 8467 | audio_stream_type_t streamType, | 
|  | 8468 | audio_session_t sessionId, | 
|  | 8469 | const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8470 | audio_port_handle_t deviceId, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8471 | audio_port_handle_t portId) | 
|  | 8472 | { | 
| Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 8473 | MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8474 | mStreamType = streamType; | 
|  | 8475 | } | 
|  | 8476 |  | 
|  | 8477 | AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput() | 
|  | 8478 | { | 
|  | 8479 | Mutex::Autolock _l(mLock); | 
|  | 8480 | AudioStreamOut *output = mOutput; | 
|  | 8481 | mOutput = NULL; | 
|  | 8482 | return output; | 
|  | 8483 | } | 
|  | 8484 |  | 
|  | 8485 | void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value) | 
|  | 8486 | { | 
|  | 8487 | Mutex::Autolock _l(mLock); | 
|  | 8488 | // Don't apply master volume in SW if our HAL can do it for us. | 
|  | 8489 | if (mAudioHwDev && | 
|  | 8490 | mAudioHwDev->canSetMasterVolume()) { | 
|  | 8491 | mMasterVolume = 1.0; | 
|  | 8492 | } else { | 
|  | 8493 | mMasterVolume = value; | 
|  | 8494 | } | 
|  | 8495 | } | 
|  | 8496 |  | 
|  | 8497 | void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted) | 
|  | 8498 | { | 
|  | 8499 | Mutex::Autolock _l(mLock); | 
|  | 8500 | // Don't apply master mute in SW if our HAL can do it for us. | 
|  | 8501 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { | 
|  | 8502 | mMasterMute = false; | 
|  | 8503 | } else { | 
|  | 8504 | mMasterMute = muted; | 
|  | 8505 | } | 
|  | 8506 | } | 
|  | 8507 |  | 
|  | 8508 | void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
|  | 8509 | { | 
|  | 8510 | Mutex::Autolock _l(mLock); | 
|  | 8511 | if (stream == mStreamType) { | 
|  | 8512 | mStreamVolume = value; | 
|  | 8513 | broadcast_l(); | 
|  | 8514 | } | 
|  | 8515 | } | 
|  | 8516 |  | 
|  | 8517 | float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const | 
|  | 8518 | { | 
|  | 8519 | Mutex::Autolock _l(mLock); | 
|  | 8520 | if (stream == mStreamType) { | 
|  | 8521 | return mStreamVolume; | 
|  | 8522 | } | 
|  | 8523 | return 0.0f; | 
|  | 8524 | } | 
|  | 8525 |  | 
|  | 8526 | void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
|  | 8527 | { | 
|  | 8528 | Mutex::Autolock _l(mLock); | 
|  | 8529 | if (stream == mStreamType) { | 
|  | 8530 | mStreamMute= muted; | 
|  | 8531 | broadcast_l(); | 
|  | 8532 | } | 
|  | 8533 | } | 
|  | 8534 |  | 
|  | 8535 | void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
|  | 8536 | { | 
|  | 8537 | Mutex::Autolock _l(mLock); | 
|  | 8538 | if (streamType == mStreamType) { | 
|  | 8539 | for (const sp<MmapTrack> &track : mActiveTracks) { | 
|  | 8540 | track->invalidate(); | 
|  | 8541 | } | 
|  | 8542 | broadcast_l(); | 
|  | 8543 | } | 
|  | 8544 | } | 
|  | 8545 |  | 
|  | 8546 | void AudioFlinger::MmapPlaybackThread::processVolume_l() | 
|  | 8547 | { | 
|  | 8548 | float volume; | 
|  | 8549 |  | 
|  | 8550 | if (mMasterMute || mStreamMute) { | 
|  | 8551 | volume = 0; | 
|  | 8552 | } else { | 
|  | 8553 | volume = mMasterVolume * mStreamVolume; | 
|  | 8554 | } | 
|  | 8555 |  | 
|  | 8556 | if (volume != mHalVolFloat) { | 
|  | 8557 | mHalVolFloat = volume; | 
|  | 8558 |  | 
|  | 8559 | // Convert volumes from float to 8.24 | 
|  | 8560 | uint32_t vol = (uint32_t)(volume * (1 << 24)); | 
|  | 8561 |  | 
|  | 8562 | // Delegate volume control to effect in track effect chain if needed | 
|  | 8563 | // only one effect chain can be present on DirectOutputThread, so if | 
|  | 8564 | // there is one, the track is connected to it | 
|  | 8565 | if (!mEffectChains.isEmpty()) { | 
|  | 8566 | mEffectChains[0]->setVolume_l(&vol, &vol); | 
|  | 8567 | volume = (float)vol / (1 << 24); | 
|  | 8568 | } | 
| Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8569 | // Try to use HW volume control and fall back to SW control if not implemented | 
|  | 8570 | if (mOutput->stream->setVolume(volume, volume) != NO_ERROR) { | 
|  | 8571 | sp<MmapStreamCallback> callback = mCallback.promote(); | 
|  | 8572 | if (callback != 0) { | 
|  | 8573 | int channelCount; | 
|  | 8574 | if (isOutput()) { | 
|  | 8575 | channelCount = audio_channel_count_from_out_mask(mChannelMask); | 
|  | 8576 | } else { | 
|  | 8577 | channelCount = audio_channel_count_from_in_mask(mChannelMask); | 
|  | 8578 | } | 
|  | 8579 | Vector<float> values; | 
|  | 8580 | for (int i = 0; i < channelCount; i++) { | 
|  | 8581 | values.add(volume); | 
|  | 8582 | } | 
|  | 8583 | callback->onVolumeChanged(mChannelMask, values); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8584 | } else { | 
| Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 8585 | ALOGW("Could not set MMAP stream volume: no volume callback!"); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8586 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8587 | } | 
|  | 8588 | } | 
|  | 8589 | } | 
|  | 8590 |  | 
|  | 8591 | void AudioFlinger::MmapPlaybackThread::checkSilentMode_l() | 
|  | 8592 | { | 
|  | 8593 | if (!mMasterMute) { | 
|  | 8594 | char value[PROPERTY_VALUE_MAX]; | 
|  | 8595 | if (property_get("ro.audio.silent", value, "0") > 0) { | 
|  | 8596 | char *endptr; | 
|  | 8597 | unsigned long ul = strtoul(value, &endptr, 0); | 
|  | 8598 | if (*endptr == '\0' && ul != 0) { | 
|  | 8599 | ALOGD("Silence is golden"); | 
|  | 8600 | // The setprop command will not allow a property to be changed after | 
|  | 8601 | // the first time it is set, so we don't have to worry about un-muting. | 
|  | 8602 | setMasterMute_l(true); | 
|  | 8603 | } | 
|  | 8604 | } | 
|  | 8605 | } | 
|  | 8606 | } | 
|  | 8607 |  | 
|  | 8608 | void AudioFlinger::MmapPlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 8609 | { | 
|  | 8610 | MmapThread::dumpInternals(fd, args); | 
|  | 8611 |  | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8612 | dprintf(fd, "  Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", | 
|  | 8613 | mStreamType, mStreamVolume, mHalVolFloat, mStreamMute); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8614 | dprintf(fd, "  Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); | 
|  | 8615 | } | 
|  | 8616 |  | 
|  | 8617 | AudioFlinger::MmapCaptureThread::MmapCaptureThread( | 
|  | 8618 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
|  | 8619 | AudioHwDevice *hwDev,  AudioStreamIn *input, | 
|  | 8620 | audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) | 
|  | 8621 | : MmapThread(audioFlinger, id, hwDev, input->stream, outDevice, inDevice, systemReady), | 
|  | 8622 | mInput(input) | 
|  | 8623 | { | 
|  | 8624 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); | 
|  | 8625 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
|  | 8626 | } | 
|  | 8627 |  | 
|  | 8628 | AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput() | 
|  | 8629 | { | 
|  | 8630 | Mutex::Autolock _l(mLock); | 
|  | 8631 | AudioStreamIn *input = mInput; | 
|  | 8632 | mInput = NULL; | 
|  | 8633 | return input; | 
|  | 8634 | } | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 8635 | } // namespace android |