| 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 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | #include "AudioFlinger.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 60 | #include "FastMixer.h" | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 61 | #include "FastCapture.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "ServiceUtilities.h" | 
| Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 63 | #include "mediautils/SchedulingPolicyService.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 65 | #ifdef ADD_BATTERY_DATA | 
 | 66 | #include <media/IMediaPlayerService.h> | 
 | 67 | #include <media/IMediaDeathNotifier.h> | 
 | 68 | #endif | 
 | 69 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 70 | #ifdef DEBUG_CPU_USAGE | 
 | 71 | #include <cpustats/CentralTendencyStatistics.h> | 
 | 72 | #include <cpustats/ThreadCpuUsage.h> | 
 | 73 | #endif | 
 | 74 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 75 | #include "AutoPark.h" | 
 | 76 |  | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 77 | #include <pthread.h> | 
 | 78 | #include "TypedLogger.h" | 
 | 79 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 80 | // ---------------------------------------------------------------------------- | 
 | 81 |  | 
 | 82 | // Note: the following macro is used for extremely verbose logging message.  In | 
 | 83 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
 | 84 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
 | 85 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
 | 86 | // turned on.  Do not uncomment the #def below unless you really know what you | 
 | 87 | // are doing and want to see all of the extremely verbose messages. | 
 | 88 | //#define VERY_VERY_VERBOSE_LOGGING | 
 | 89 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
 | 90 | #define ALOGVV ALOGV | 
 | 91 | #else | 
 | 92 | #define ALOGVV(a...) do { } while(0) | 
 | 93 | #endif | 
 | 94 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 95 | // TODO: Move these macro/inlines to a header file. | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 96 | #define max(a, b) ((a) > (b) ? (a) : (b)) | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 97 | template <typename T> | 
 | 98 | static inline T min(const T& a, const T& b) | 
 | 99 | { | 
 | 100 |     return a < b ? a : b; | 
 | 101 | } | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 102 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 103 | namespace android { | 
 | 104 |  | 
 | 105 | // retry counts for buffer fill timeout | 
 | 106 | // 50 * ~20msecs = 1 second | 
 | 107 | static const int8_t kMaxTrackRetries = 50; | 
 | 108 | static const int8_t kMaxTrackStartupRetries = 50; | 
 | 109 | // allow less retry attempts on direct output thread. | 
 | 110 | // direct outputs can be a scarce resource in audio hardware and should | 
 | 111 | // be released as quickly as possible. | 
 | 112 | static const int8_t kMaxTrackRetriesDirect = 2; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 113 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 114 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 115 |  | 
 | 116 | // don't warn about blocked writes or record buffer overflows more often than this | 
 | 117 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
 | 118 |  | 
 | 119 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
 | 120 | static const int kRecordThreadSleepUs = 5000; | 
 | 121 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 122 | // maximum time to wait in sendConfigEvent_l() for a status to be received | 
 | 123 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 124 |  | 
 | 125 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
 | 126 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
 | 127 | // maximum divider applied to the active sleep time in the mixer thread loop | 
 | 128 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
 | 129 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 130 | // minimum normal sink buffer size, expressed in milliseconds rather than frames | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 131 | // FIXME This should be based on experimentally observed scheduling jitter | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 132 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; | 
 | 133 | // maximum normal sink buffer size | 
 | 134 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 135 |  | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 136 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread | 
 | 137 | // FIXME This should be based on experimentally observed scheduling jitter | 
 | 138 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; | 
 | 139 |  | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 140 | // Offloaded output thread standby delay: allows track transition without going to standby | 
 | 141 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); | 
 | 142 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 143 | // Direct output thread minimum sleep time in idle or active(underrun) state | 
 | 144 | static const nsecs_t kDirectMinSleepTimeUs = 10000; | 
 | 145 |  | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 146 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good | 
 | 147 | // balance between power consumption and latency, and allows threads to be scheduled reliably | 
 | 148 | // by the CFS scheduler. | 
 | 149 | // FIXME Express other hardcoded references to 20ms with references to this constant and move | 
 | 150 | // it appropriately. | 
 | 151 | #define FMS_20 20 | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 152 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 153 | // Whether to use fast mixer | 
 | 154 | static const enum { | 
 | 155 |     FastMixer_Never,    // never initialize or use: for debugging only | 
 | 156 |     FastMixer_Always,   // always initialize and use, even if not needed: for debugging only | 
 | 157 |                         // normal mixer multiplier is 1 | 
 | 158 |     FastMixer_Static,   // initialize if needed, then use all the time if initialized, | 
 | 159 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 160 |     FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load, | 
 | 161 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 162 |     // FIXME for FastMixer_Dynamic: | 
 | 163 |     //  Supporting this option will require fixing HALs that can't handle large writes. | 
 | 164 |     //  For example, one HAL implementation returns an error from a large write, | 
 | 165 |     //  and another HAL implementation corrupts memory, possibly in the sample rate converter. | 
 | 166 |     //  We could either fix the HAL implementations, or provide a wrapper that breaks | 
 | 167 |     //  up large writes into smaller ones, and the wrapper would need to deal with scheduler. | 
 | 168 | } kUseFastMixer = FastMixer_Static; | 
 | 169 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 170 | // Whether to use fast capture | 
 | 171 | static const enum { | 
 | 172 |     FastCapture_Never,  // never initialize or use: for debugging only | 
 | 173 |     FastCapture_Always, // always initialize and use, even if not needed: for debugging only | 
 | 174 |     FastCapture_Static, // initialize if needed, then use all the time if initialized | 
 | 175 | } kUseFastCapture = FastCapture_Static; | 
 | 176 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 177 | // Priorities for requestPriority | 
 | 178 | static const int kPriorityAudioApp = 2; | 
 | 179 | static const int kPriorityFastMixer = 3; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 180 | static const int kPriorityFastCapture = 3; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 181 |  | 
| Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 182 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the | 
 | 183 | // track buffer in shared memory.  Zero on input means to use a default value.  For fast tracks, | 
 | 184 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 185 |  | 
 | 186 | // This is the default value, if not specified by property. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 187 | static const int kFastTrackMultiplier = 2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 188 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 189 | // The minimum and maximum allowed values | 
 | 190 | static const int kFastTrackMultiplierMin = 1; | 
 | 191 | static const int kFastTrackMultiplierMax = 2; | 
 | 192 |  | 
 | 193 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. | 
 | 194 | static int sFastTrackMultiplier = kFastTrackMultiplier; | 
 | 195 |  | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 196 | // See Thread::readOnlyHeap(). | 
 | 197 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. | 
 | 198 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), | 
 | 199 | // and that all "fast" AudioRecord clients read from.  In either case, the size can be small. | 
| Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 200 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 201 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 202 | // ---------------------------------------------------------------------------- | 
 | 203 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 204 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; | 
 | 205 |  | 
 | 206 | static void sFastTrackMultiplierInit() | 
 | 207 | { | 
 | 208 |     char value[PROPERTY_VALUE_MAX]; | 
 | 209 |     if (property_get("af.fast_track_multiplier", value, NULL) > 0) { | 
 | 210 |         char *endptr; | 
 | 211 |         unsigned long ul = strtoul(value, &endptr, 0); | 
 | 212 |         if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { | 
 | 213 |             sFastTrackMultiplier = (int) ul; | 
 | 214 |         } | 
 | 215 |     } | 
 | 216 | } | 
 | 217 |  | 
 | 218 | // ---------------------------------------------------------------------------- | 
 | 219 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 220 | #ifdef ADD_BATTERY_DATA | 
 | 221 | // To collect the amplifier usage | 
 | 222 | static void addBatteryData(uint32_t params) { | 
 | 223 |     sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
 | 224 |     if (service == NULL) { | 
 | 225 |         // it already logged | 
 | 226 |         return; | 
 | 227 |     } | 
 | 228 |  | 
 | 229 |     service->addBatteryData(params); | 
 | 230 | } | 
 | 231 | #endif | 
 | 232 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 233 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset | 
 | 234 | struct { | 
 | 235 |     // call when you acquire a partial wakelock | 
 | 236 |     void acquire(const sp<IBinder> &wakeLockToken) { | 
 | 237 |         pthread_mutex_lock(&mLock); | 
 | 238 |         if (wakeLockToken.get() == nullptr) { | 
 | 239 |             adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); | 
 | 240 |         } else { | 
 | 241 |             if (mCount == 0) { | 
 | 242 |                 adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); | 
 | 243 |             } | 
 | 244 |             ++mCount; | 
 | 245 |         } | 
 | 246 |         pthread_mutex_unlock(&mLock); | 
 | 247 |     } | 
 | 248 |  | 
 | 249 |     // call when you release a partial wakelock. | 
 | 250 |     void release(const sp<IBinder> &wakeLockToken) { | 
 | 251 |         if (wakeLockToken.get() == nullptr) { | 
 | 252 |             return; | 
 | 253 |         } | 
 | 254 |         pthread_mutex_lock(&mLock); | 
 | 255 |         if (--mCount < 0) { | 
 | 256 |             ALOGE("negative wakelock count"); | 
 | 257 |             mCount = 0; | 
 | 258 |         } | 
 | 259 |         pthread_mutex_unlock(&mLock); | 
 | 260 |     } | 
 | 261 |  | 
 | 262 |     // retrieves the boottime timebase offset from monotonic. | 
 | 263 |     int64_t getBoottimeOffset() { | 
 | 264 |         pthread_mutex_lock(&mLock); | 
 | 265 |         int64_t boottimeOffset = mBoottimeOffset; | 
 | 266 |         pthread_mutex_unlock(&mLock); | 
 | 267 |         return boottimeOffset; | 
 | 268 |     } | 
 | 269 |  | 
 | 270 |     // Adjusts the timebase offset between TIMEBASE_MONOTONIC | 
 | 271 |     // and the selected timebase. | 
 | 272 |     // Currently only TIMEBASE_BOOTTIME is allowed. | 
 | 273 |     // | 
 | 274 |     // This only needs to be called upon acquiring the first partial wakelock | 
 | 275 |     // after all other partial wakelocks are released. | 
 | 276 |     // | 
 | 277 |     // We do an empirical measurement of the offset rather than parsing | 
 | 278 |     // /proc/timer_list since the latter is not a formal kernel ABI. | 
 | 279 |     static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { | 
 | 280 |         int clockbase; | 
 | 281 |         switch (timebase) { | 
 | 282 |         case ExtendedTimestamp::TIMEBASE_BOOTTIME: | 
 | 283 |             clockbase = SYSTEM_TIME_BOOTTIME; | 
 | 284 |             break; | 
 | 285 |         default: | 
 | 286 |             LOG_ALWAYS_FATAL("invalid timebase %d", timebase); | 
 | 287 |             break; | 
 | 288 |         } | 
 | 289 |         // try three times to get the clock offset, choose the one | 
 | 290 |         // with the minimum gap in measurements. | 
 | 291 |         const int tries = 3; | 
 | 292 |         nsecs_t bestGap, measured; | 
 | 293 |         for (int i = 0; i < tries; ++i) { | 
 | 294 |             const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 295 |             const nsecs_t tbase = systemTime(clockbase); | 
 | 296 |             const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 297 |             const nsecs_t gap = tmono2 - tmono; | 
 | 298 |             if (i == 0 || gap < bestGap) { | 
 | 299 |                 bestGap = gap; | 
 | 300 |                 measured = tbase - ((tmono + tmono2) >> 1); | 
 | 301 |             } | 
 | 302 |         } | 
 | 303 |  | 
 | 304 |         // to avoid micro-adjusting, we don't change the timebase | 
 | 305 |         // unless it is significantly different. | 
 | 306 |         // | 
 | 307 |         // Assumption: It probably takes more than toleranceNs to | 
 | 308 |         // suspend and resume the device. | 
 | 309 |         static int64_t toleranceNs = 10000; // 10 us | 
 | 310 |         if (llabs(*offset - measured) > toleranceNs) { | 
 | 311 |             ALOGV("Adjusting timebase offset old: %lld  new: %lld", | 
 | 312 |                     (long long)*offset, (long long)measured); | 
 | 313 |             *offset = measured; | 
 | 314 |         } | 
 | 315 |     } | 
 | 316 |  | 
 | 317 |     pthread_mutex_t mLock; | 
 | 318 |     int32_t mCount; | 
 | 319 |     int64_t mBoottimeOffset; | 
 | 320 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 321 |  | 
 | 322 | // ---------------------------------------------------------------------------- | 
 | 323 | //      CPU Stats | 
 | 324 | // ---------------------------------------------------------------------------- | 
 | 325 |  | 
 | 326 | class CpuStats { | 
 | 327 | public: | 
 | 328 |     CpuStats(); | 
 | 329 |     void sample(const String8 &title); | 
 | 330 | #ifdef DEBUG_CPU_USAGE | 
 | 331 | private: | 
 | 332 |     ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns | 
 | 333 |     CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns | 
 | 334 |  | 
 | 335 |     CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles | 
 | 336 |  | 
 | 337 |     int mCpuNum;                        // thread's current CPU number | 
 | 338 |     int mCpukHz;                        // frequency of thread's current CPU in kHz | 
 | 339 | #endif | 
 | 340 | }; | 
 | 341 |  | 
 | 342 | CpuStats::CpuStats() | 
 | 343 | #ifdef DEBUG_CPU_USAGE | 
 | 344 |     : mCpuNum(-1), mCpukHz(-1) | 
 | 345 | #endif | 
 | 346 | { | 
 | 347 | } | 
 | 348 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 349 | void CpuStats::sample(const String8 &title | 
 | 350 | #ifndef DEBUG_CPU_USAGE | 
 | 351 |                 __unused | 
 | 352 | #endif | 
 | 353 |         ) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 354 | #ifdef DEBUG_CPU_USAGE | 
 | 355 |     // get current thread's delta CPU time in wall clock ns | 
 | 356 |     double wcNs; | 
 | 357 |     bool valid = mCpuUsage.sampleAndEnable(wcNs); | 
 | 358 |  | 
 | 359 |     // record sample for wall clock statistics | 
 | 360 |     if (valid) { | 
 | 361 |         mWcStats.sample(wcNs); | 
 | 362 |     } | 
 | 363 |  | 
 | 364 |     // get the current CPU number | 
 | 365 |     int cpuNum = sched_getcpu(); | 
 | 366 |  | 
 | 367 |     // get the current CPU frequency in kHz | 
 | 368 |     int cpukHz = mCpuUsage.getCpukHz(cpuNum); | 
 | 369 |  | 
 | 370 |     // check if either CPU number or frequency changed | 
 | 371 |     if (cpuNum != mCpuNum || cpukHz != mCpukHz) { | 
 | 372 |         mCpuNum = cpuNum; | 
 | 373 |         mCpukHz = cpukHz; | 
 | 374 |         // ignore sample for purposes of cycles | 
 | 375 |         valid = false; | 
 | 376 |     } | 
 | 377 |  | 
 | 378 |     // if no change in CPU number or frequency, then record sample for cycle statistics | 
 | 379 |     if (valid && mCpukHz > 0) { | 
 | 380 |         double cycles = wcNs * cpukHz * 0.000001; | 
 | 381 |         mHzStats.sample(cycles); | 
 | 382 |     } | 
 | 383 |  | 
 | 384 |     unsigned n = mWcStats.n(); | 
 | 385 |     // mCpuUsage.elapsed() is expensive, so don't call it every loop | 
 | 386 |     if ((n & 127) == 1) { | 
 | 387 |         long long elapsed = mCpuUsage.elapsed(); | 
 | 388 |         if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
 | 389 |             double perLoop = elapsed / (double) n; | 
 | 390 |             double perLoop100 = perLoop * 0.01; | 
 | 391 |             double perLoop1k = perLoop * 0.001; | 
 | 392 |             double mean = mWcStats.mean(); | 
 | 393 |             double stddev = mWcStats.stddev(); | 
 | 394 |             double minimum = mWcStats.minimum(); | 
 | 395 |             double maximum = mWcStats.maximum(); | 
 | 396 |             double meanCycles = mHzStats.mean(); | 
 | 397 |             double stddevCycles = mHzStats.stddev(); | 
 | 398 |             double minCycles = mHzStats.minimum(); | 
 | 399 |             double maxCycles = mHzStats.maximum(); | 
 | 400 |             mCpuUsage.resetElapsed(); | 
 | 401 |             mWcStats.reset(); | 
 | 402 |             mHzStats.reset(); | 
 | 403 |             ALOGD("CPU usage for %s over past %.1f secs\n" | 
 | 404 |                 "  (%u mixer loops at %.1f mean ms per loop):\n" | 
 | 405 |                 "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" | 
 | 406 |                 "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" | 
 | 407 |                 "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", | 
 | 408 |                     title.string(), | 
 | 409 |                     elapsed * .000000001, n, perLoop * .000001, | 
 | 410 |                     mean * .001, | 
 | 411 |                     stddev * .001, | 
 | 412 |                     minimum * .001, | 
 | 413 |                     maximum * .001, | 
 | 414 |                     mean / perLoop100, | 
 | 415 |                     stddev / perLoop100, | 
 | 416 |                     minimum / perLoop100, | 
 | 417 |                     maximum / perLoop100, | 
 | 418 |                     meanCycles / perLoop1k, | 
 | 419 |                     stddevCycles / perLoop1k, | 
 | 420 |                     minCycles / perLoop1k, | 
 | 421 |                     maxCycles / perLoop1k); | 
 | 422 |  | 
 | 423 |         } | 
 | 424 |     } | 
 | 425 | #endif | 
 | 426 | }; | 
 | 427 |  | 
 | 428 | // ---------------------------------------------------------------------------- | 
 | 429 | //      ThreadBase | 
 | 430 | // ---------------------------------------------------------------------------- | 
 | 431 |  | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 432 | // static | 
 | 433 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) | 
 | 434 | { | 
 | 435 |     switch (type) { | 
 | 436 |     case MIXER: | 
 | 437 |         return "MIXER"; | 
 | 438 |     case DIRECT: | 
 | 439 |         return "DIRECT"; | 
 | 440 |     case DUPLICATING: | 
 | 441 |         return "DUPLICATING"; | 
 | 442 |     case RECORD: | 
 | 443 |         return "RECORD"; | 
 | 444 |     case OFFLOAD: | 
 | 445 |         return "OFFLOAD"; | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 446 |     case MMAP: | 
 | 447 |         return "MMAP"; | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 448 |     default: | 
 | 449 |         return "unknown"; | 
 | 450 |     } | 
 | 451 | } | 
 | 452 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 453 | std::string devicesToString(audio_devices_t devices) | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 454 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 455 |     std::string result; | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 456 |     if (devices & AUDIO_DEVICE_BIT_IN) { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 457 |         InputDeviceConverter::maskToString(devices, result); | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 458 |     } else { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 459 |         OutputDeviceConverter::maskToString(devices, result); | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 460 |     } | 
 | 461 |     return result; | 
 | 462 | } | 
 | 463 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 464 | std::string inputFlagsToString(audio_input_flags_t flags) | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 465 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 466 |     std::string result; | 
 | 467 |     InputFlagConverter::maskToString(flags, result); | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 468 |     return result; | 
 | 469 | } | 
 | 470 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 471 | std::string outputFlagsToString(audio_output_flags_t flags) | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 472 | { | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 473 |     std::string result; | 
 | 474 |     OutputFlagConverter::maskToString(flags, result); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 475 |     return result; | 
 | 476 | } | 
 | 477 |  | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 478 | const char *sourceToString(audio_source_t source) | 
 | 479 | { | 
 | 480 |     switch (source) { | 
 | 481 |     case AUDIO_SOURCE_DEFAULT:              return "default"; | 
 | 482 |     case AUDIO_SOURCE_MIC:                  return "mic"; | 
 | 483 |     case AUDIO_SOURCE_VOICE_UPLINK:         return "voice uplink"; | 
 | 484 |     case AUDIO_SOURCE_VOICE_DOWNLINK:       return "voice downlink"; | 
 | 485 |     case AUDIO_SOURCE_VOICE_CALL:           return "voice call"; | 
 | 486 |     case AUDIO_SOURCE_CAMCORDER:            return "camcorder"; | 
 | 487 |     case AUDIO_SOURCE_VOICE_RECOGNITION:    return "voice recognition"; | 
 | 488 |     case AUDIO_SOURCE_VOICE_COMMUNICATION:  return "voice communication"; | 
 | 489 |     case AUDIO_SOURCE_REMOTE_SUBMIX:        return "remote submix"; | 
| rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 490 |     case AUDIO_SOURCE_UNPROCESSED:          return "unprocessed"; | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 491 |     case AUDIO_SOURCE_FM_TUNER:             return "FM tuner"; | 
 | 492 |     case AUDIO_SOURCE_HOTWORD:              return "hotword"; | 
 | 493 |     default:                                return "unknown"; | 
 | 494 |     } | 
 | 495 | } | 
 | 496 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 497 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 498 |         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] | 499 |     :   Thread(false /*canCallJava*/), | 
 | 500 |         mType(type), | 
| Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 501 |         mAudioFlinger(audioFlinger), | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 502 |         // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 503 |         // are set by PlaybackThread::readOutputParameters_l() or | 
 | 504 |         // RecordThread::readInputParameters_l() | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 505 |         //FIXME: mStandby should be true here. Is this some kind of hack? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 506 |         mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 507 |         mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), | 
 | 508 |         mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 509 |         // mName will be set by concrete (non-virtual) subclass | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 510 |         mDeathRecipient(new PMDeathRecipient(this)), | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 511 |         mSystemReady(systemReady), | 
 | 512 |         mSignalPending(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 513 | { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 514 |     memset(&mPatch, 0, sizeof(struct audio_patch)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 515 | } | 
 | 516 |  | 
 | 517 | AudioFlinger::ThreadBase::~ThreadBase() | 
 | 518 | { | 
| Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 519 |     // 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] | 520 |     mConfigEvents.clear(); | 
 | 521 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 |     // do not lock the mutex in destructor | 
 | 523 |     releaseWakeLock_l(); | 
 | 524 |     if (mPowerManager != 0) { | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 525 |         sp<IBinder> binder = IInterface::asBinder(mPowerManager); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 526 |         binder->unlinkToDeath(mDeathRecipient); | 
 | 527 |     } | 
 | 528 | } | 
 | 529 |  | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 530 | status_t AudioFlinger::ThreadBase::readyToRun() | 
 | 531 | { | 
 | 532 |     status_t status = initCheck(); | 
 | 533 |     if (status == NO_ERROR) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 534 |         ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 535 |     } else { | 
 | 536 |         ALOGE("No working audio driver found."); | 
 | 537 |     } | 
 | 538 |     return status; | 
 | 539 | } | 
 | 540 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 541 | void AudioFlinger::ThreadBase::exit() | 
 | 542 | { | 
 | 543 |     ALOGV("ThreadBase::exit"); | 
 | 544 |     // do any cleanup required for exit to succeed | 
 | 545 |     preExit(); | 
 | 546 |     { | 
 | 547 |         // This lock prevents the following race in thread (uniprocessor for illustration): | 
 | 548 |         //  if (!exitPending()) { | 
 | 549 |         //      // context switch from here to exit() | 
 | 550 |         //      // exit() calls requestExit(), what exitPending() observes | 
 | 551 |         //      // exit() calls signal(), which is dropped since no waiters | 
 | 552 |         //      // context switch back from exit() to here | 
 | 553 |         //      mWaitWorkCV.wait(...); | 
 | 554 |         //      // now thread is hung | 
 | 555 |         //  } | 
 | 556 |         AutoMutex lock(mLock); | 
 | 557 |         requestExit(); | 
 | 558 |         mWaitWorkCV.broadcast(); | 
 | 559 |     } | 
 | 560 |     // When Thread::requestExitAndWait is made virtual and this method is renamed to | 
 | 561 |     // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" | 
 | 562 |     requestExitAndWait(); | 
 | 563 | } | 
 | 564 |  | 
 | 565 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
 | 566 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 567 |     ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
 | 568 |     Mutex::Autolock _l(mLock); | 
 | 569 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 570 |     return sendSetParameterConfigEvent_l(keyValuePairs); | 
 | 571 | } | 
 | 572 |  | 
 | 573 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 574 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). | 
 | 575 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) | 
 | 576 | { | 
 | 577 |     status_t status = NO_ERROR; | 
 | 578 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 579 |     if (event->mRequiresSystemReady && !mSystemReady) { | 
 | 580 |         event->mWaitStatus = false; | 
 | 581 |         mPendingConfigEvents.add(event); | 
 | 582 |         return status; | 
 | 583 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 584 |     mConfigEvents.add(event); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 585 |     ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 586 |     mWaitWorkCV.signal(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 587 |     mLock.unlock(); | 
 | 588 |     { | 
 | 589 |         Mutex::Autolock _l(event->mLock); | 
 | 590 |         while (event->mWaitStatus) { | 
 | 591 |             if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { | 
 | 592 |                 event->mStatus = TIMED_OUT; | 
 | 593 |                 event->mWaitStatus = false; | 
 | 594 |             } | 
 | 595 |         } | 
 | 596 |         status = event->mStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 597 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 598 |     mLock.lock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 599 |     return status; | 
 | 600 | } | 
 | 601 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 602 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 603 | { | 
 | 604 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 605 |     sendIoConfigEvent_l(event, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 606 | } | 
 | 607 |  | 
 | 608 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 609 | 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] | 610 | { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 611 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 612 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 613 | } | 
 | 614 |  | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 615 | 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] | 616 | { | 
 | 617 |     Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 618 |     sendPrioConfigEvent_l(pid, tid, prio, forApp); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 619 | } | 
 | 620 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 621 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 622 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l( | 
 | 623 |         pid_t pid, pid_t tid, int32_t prio, bool forApp) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 624 | { | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 625 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 626 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 627 | } | 
 | 628 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 629 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 630 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 631 | { | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 632 |     sp<ConfigEvent> configEvent; | 
 | 633 |     AudioParameter param(keyValuePair); | 
 | 634 |     int value; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 635 |     if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 636 |         setMasterMono_l(value != 0); | 
 | 637 |         if (param.size() == 1) { | 
 | 638 |             return NO_ERROR; // should be a solo parameter - we don't pass down | 
 | 639 |         } | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 640 |         param.remove(String8(AudioParameter::keyMonoOutput)); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 641 |         configEvent = new SetParameterConfigEvent(param.toString()); | 
 | 642 |     } else { | 
 | 643 |         configEvent = new SetParameterConfigEvent(keyValuePair); | 
 | 644 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 645 |     return sendConfigEvent_l(configEvent); | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 646 | } | 
 | 647 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 648 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( | 
 | 649 |                                                         const struct audio_patch *patch, | 
 | 650 |                                                         audio_patch_handle_t *handle) | 
 | 651 | { | 
 | 652 |     Mutex::Autolock _l(mLock); | 
 | 653 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); | 
 | 654 |     status_t status = sendConfigEvent_l(configEvent); | 
 | 655 |     if (status == NO_ERROR) { | 
 | 656 |         CreateAudioPatchConfigEventData *data = | 
 | 657 |                                         (CreateAudioPatchConfigEventData *)configEvent->mData.get(); | 
 | 658 |         *handle = data->mHandle; | 
 | 659 |     } | 
 | 660 |     return status; | 
 | 661 | } | 
 | 662 |  | 
 | 663 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( | 
 | 664 |                                                                 const audio_patch_handle_t handle) | 
 | 665 | { | 
 | 666 |     Mutex::Autolock _l(mLock); | 
 | 667 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); | 
 | 668 |     return sendConfigEvent_l(configEvent); | 
 | 669 | } | 
 | 670 |  | 
 | 671 |  | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 672 | // post condition: mConfigEvents.isEmpty() | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 673 | void AudioFlinger::ThreadBase::processConfigEvents_l() | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 674 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 675 |     bool configChanged = false; | 
 | 676 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 677 |     while (!mConfigEvents.isEmpty()) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 678 |         ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 679 |         sp<ConfigEvent> event = mConfigEvents[0]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 680 |         mConfigEvents.removeAt(0); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 681 |         switch (event->mType) { | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 682 |         case CFG_EVENT_PRIO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 683 |             PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); | 
 | 684 |             // FIXME Need to understand why this has to be done asynchronously | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 685 |             int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 686 |                     true /*asynchronous*/); | 
 | 687 |             if (err != 0) { | 
 | 688 |                 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] | 689 |                       data->mPrio, data->mPid, data->mTid, err); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 690 |             } | 
 | 691 |         } break; | 
 | 692 |         case CFG_EVENT_IO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 693 |             IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 694 |             ioConfigChanged(data->mEvent, data->mPid); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 695 |         } break; | 
 | 696 |         case CFG_EVENT_SET_PARAMETER: { | 
 | 697 |             SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); | 
 | 698 |             if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { | 
 | 699 |                 configChanged = true; | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 700 |                 mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", | 
 | 701 |                         data->mKeyValuePairs.string()); | 
| Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 702 |             } | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 703 |         } break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 704 |         case CFG_EVENT_CREATE_AUDIO_PATCH: { | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 705 |             const audio_devices_t oldDevice = getDevice(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 706 |             CreateAudioPatchConfigEventData *data = | 
 | 707 |                                             (CreateAudioPatchConfigEventData *)event->mData.get(); | 
 | 708 |             event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 709 |             const audio_devices_t newDevice = getDevice(); | 
 | 710 |             mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", | 
 | 711 |                     (unsigned)oldDevice, devicesToString(oldDevice).c_str(), | 
 | 712 |                     (unsigned)newDevice, devicesToString(newDevice).c_str()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 713 |         } break; | 
 | 714 |         case CFG_EVENT_RELEASE_AUDIO_PATCH: { | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 715 |             const audio_devices_t oldDevice = getDevice(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 716 |             ReleaseAudioPatchConfigEventData *data = | 
 | 717 |                                             (ReleaseAudioPatchConfigEventData *)event->mData.get(); | 
 | 718 |             event->mStatus = releaseAudioPatch_l(data->mHandle); | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 719 |             const audio_devices_t newDevice = getDevice(); | 
 | 720 |             mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)", | 
 | 721 |                     (unsigned)oldDevice, devicesToString(oldDevice).c_str(), | 
 | 722 |                     (unsigned)newDevice, devicesToString(newDevice).c_str()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 723 |         } break; | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 724 |         default: | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 725 |             ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 726 |             break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 727 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 728 |         { | 
 | 729 |             Mutex::Autolock _l(event->mLock); | 
 | 730 |             if (event->mWaitStatus) { | 
 | 731 |                 event->mWaitStatus = false; | 
 | 732 |                 event->mCond.signal(); | 
 | 733 |             } | 
 | 734 |         } | 
 | 735 |         ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); | 
 | 736 |     } | 
 | 737 |  | 
 | 738 |     if (configChanged) { | 
 | 739 |         cacheParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 740 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 741 | } | 
 | 742 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 743 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { | 
 | 744 |     String8 s; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 745 |     const audio_channel_representation_t representation = | 
 | 746 |             audio_channel_mask_get_representation(mask); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 747 |  | 
 | 748 |     switch (representation) { | 
 | 749 |     case AUDIO_CHANNEL_REPRESENTATION_POSITION: { | 
 | 750 |         if (output) { | 
 | 751 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); | 
 | 752 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); | 
 | 753 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); | 
 | 754 |             if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); | 
 | 755 |             if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); | 
 | 756 |             if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); | 
 | 757 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); | 
 | 758 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); | 
 | 759 |             if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); | 
 | 760 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); | 
 | 761 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); | 
 | 762 |             if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); | 
 | 763 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); | 
 | 764 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); | 
 | 765 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); | 
 | 766 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); | 
 | 767 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); | 
 | 768 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); | 
 | 769 |             if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  "); | 
 | 770 |         } else { | 
 | 771 |             if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); | 
 | 772 |             if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); | 
 | 773 |             if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); | 
 | 774 |             if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); | 
 | 775 |             if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); | 
 | 776 |             if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); | 
 | 777 |             if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); | 
 | 778 |             if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); | 
 | 779 |             if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); | 
 | 780 |             if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); | 
 | 781 |             if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); | 
 | 782 |             if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); | 
 | 783 |             if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); | 
 | 784 |             if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); | 
 | 785 |             if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  "); | 
 | 786 |         } | 
 | 787 |         const int len = s.length(); | 
 | 788 |         if (len > 2) { | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 789 |             (void) s.lockBuffer(len);      // needed? | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 790 |             s.unlockBuffer(len - 2);       // remove trailing ", " | 
 | 791 |         } | 
 | 792 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 793 |     } | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 794 |     case AUDIO_CHANNEL_REPRESENTATION_INDEX: | 
 | 795 |         s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); | 
 | 796 |         return s; | 
 | 797 |     default: | 
 | 798 |         s.appendFormat("unknown mask, representation:%d  bits:%#x", | 
 | 799 |                 representation, audio_channel_mask_get_bits(mask)); | 
 | 800 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 801 |     } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 802 | } | 
 | 803 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 804 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 805 | { | 
 | 806 |     const size_t SIZE = 256; | 
 | 807 |     char buffer[SIZE]; | 
 | 808 |     String8 result; | 
 | 809 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 810 |     dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", | 
 | 811 |             this, mThreadName, getTid(), type(), threadTypeToString(type())); | 
 | 812 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 813 |     bool locked = AudioFlinger::dumpTryLock(mLock); | 
 | 814 |     if (!locked) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 815 |         dprintf(fd, "  Thread may be deadlocked\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 816 |     } | 
 | 817 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 818 |     dprintf(fd, "  I/O handle: %d\n", mId); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 819 |     dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no"); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 820 |     dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 821 |     dprintf(fd, "  HAL frame count: %zu\n", mFrameCount); | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 822 |     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] | 823 |     dprintf(fd, "  HAL buffer size: %zu bytes\n", mBufferSize); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 824 |     dprintf(fd, "  Channel count: %u\n", mChannelCount); | 
 | 825 |     dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask, | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 826 |             channelMaskToString(mChannelMask, mType != RECORD).string()); | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 827 |     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] | 828 |     dprintf(fd, "  Processing frame size: %zu bytes\n", mFrameSize); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 829 |     dprintf(fd, "  Pending config events:"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 830 |     size_t numConfig = mConfigEvents.size(); | 
 | 831 |     if (numConfig) { | 
 | 832 |         for (size_t i = 0; i < numConfig; i++) { | 
 | 833 |             mConfigEvents[i]->dump(buffer, SIZE); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 834 |             dprintf(fd, "\n    %s", buffer); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 835 |         } | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 836 |         dprintf(fd, "\n"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 837 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 838 |         dprintf(fd, " none\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 839 |     } | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 840 |     // 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] | 841 |     dprintf(fd, "  Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).c_str()); | 
 | 842 |     dprintf(fd, "  Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).c_str()); | 
| Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 843 |     dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 844 |  | 
 | 845 |     if (locked) { | 
 | 846 |         mLock.unlock(); | 
 | 847 |     } | 
 | 848 | } | 
 | 849 |  | 
 | 850 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
 | 851 | { | 
 | 852 |     const size_t SIZE = 256; | 
 | 853 |     char buffer[SIZE]; | 
 | 854 |     String8 result; | 
 | 855 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 856 |     size_t numEffectChains = mEffectChains.size(); | 
| Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 857 |     snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 858 |     write(fd, buffer, strlen(buffer)); | 
 | 859 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 860 |     for (size_t i = 0; i < numEffectChains; ++i) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 861 |         sp<EffectChain> chain = mEffectChains[i]; | 
 | 862 |         if (chain != 0) { | 
 | 863 |             chain->dump(fd, args); | 
 | 864 |         } | 
 | 865 |     } | 
 | 866 | } | 
 | 867 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 868 | void AudioFlinger::ThreadBase::acquireWakeLock() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 869 | { | 
 | 870 |     Mutex::Autolock _l(mLock); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 871 |     acquireWakeLock_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 | } | 
 | 873 |  | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 874 | String16 AudioFlinger::ThreadBase::getWakeLockTag() | 
 | 875 | { | 
 | 876 |     switch (mType) { | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 877 |     case MIXER: | 
 | 878 |         return String16("AudioMix"); | 
 | 879 |     case DIRECT: | 
 | 880 |         return String16("AudioDirectOut"); | 
 | 881 |     case DUPLICATING: | 
 | 882 |         return String16("AudioDup"); | 
 | 883 |     case RECORD: | 
 | 884 |         return String16("AudioIn"); | 
 | 885 |     case OFFLOAD: | 
 | 886 |         return String16("AudioOffload"); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 887 |     case MMAP: | 
 | 888 |         return String16("Mmap"); | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 889 |     default: | 
 | 890 |         ALOG_ASSERT(false); | 
 | 891 |         return String16("AudioUnknown"); | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 892 |     } | 
 | 893 | } | 
 | 894 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 895 | void AudioFlinger::ThreadBase::acquireWakeLock_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 896 | { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 897 |     getPowerManager_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 898 |     if (mPowerManager != 0) { | 
 | 899 |         sp<IBinder> binder = new BBinder(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 900 |         // Uses AID_AUDIOSERVER for wakelock.  updateWakeLockUids_l() updates with client uids. | 
 | 901 |         status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 902 |                     binder, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 903 |                     getWakeLockTag(), | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 904 |                     String16("audioserver"), | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 905 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 906 |         if (status == NO_ERROR) { | 
 | 907 |             mWakeLockToken = binder; | 
 | 908 |         } | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 909 |         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 910 |     } | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 911 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 912 |     gBoottime.acquire(mWakeLockToken); | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 913 |     mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = | 
 | 914 |             gBoottime.getBoottimeOffset(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 915 | } | 
 | 916 |  | 
 | 917 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
 | 918 | { | 
 | 919 |     Mutex::Autolock _l(mLock); | 
 | 920 |     releaseWakeLock_l(); | 
 | 921 | } | 
 | 922 |  | 
 | 923 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
 | 924 | { | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 925 |     gBoottime.release(mWakeLockToken); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 926 |     if (mWakeLockToken != 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 927 |         ALOGV("releaseWakeLock_l() %s", mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 928 |         if (mPowerManager != 0) { | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 929 |             mPowerManager->releaseWakeLock(mWakeLockToken, 0, | 
 | 930 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 931 |         } | 
 | 932 |         mWakeLockToken.clear(); | 
 | 933 |     } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 934 | } | 
 | 935 |  | 
 | 936 | void AudioFlinger::ThreadBase::getPowerManager_l() { | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 937 |     if (mSystemReady && mPowerManager == 0) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 938 |         // use checkService() to avoid blocking if power service is not up yet | 
 | 939 |         sp<IBinder> binder = | 
 | 940 |             defaultServiceManager()->checkService(String16("power")); | 
 | 941 |         if (binder == 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 942 |             ALOGW("Thread %s cannot connect to the power manager service", mThreadName); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 943 |         } else { | 
 | 944 |             mPowerManager = interface_cast<IPowerManager>(binder); | 
 | 945 |             binder->linkToDeath(mDeathRecipient); | 
 | 946 |         } | 
 | 947 |     } | 
 | 948 | } | 
 | 949 |  | 
| Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 950 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 951 |     getPowerManager_l(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 952 |  | 
 | 953 | #if !LOG_NDEBUG | 
 | 954 |     std::stringstream s; | 
| Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 955 |     for (uid_t uid : uids) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 956 |         s << uid << " "; | 
 | 957 |     } | 
 | 958 |     ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); | 
 | 959 | #endif | 
 | 960 |  | 
| Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 961 |     if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. | 
 | 962 |         if (mSystemReady) { | 
 | 963 |             ALOGE("no wake lock to update, but system ready!"); | 
 | 964 |         } else { | 
 | 965 |             ALOGW("no wake lock to update, system not ready yet"); | 
 | 966 |         } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 967 |         return; | 
 | 968 |     } | 
 | 969 |     if (mPowerManager != 0) { | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 970 |         std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints | 
 | 971 |         status_t status = mPowerManager->updateWakeLockUids( | 
 | 972 |                 mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(), | 
 | 973 |                 true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 974 |         ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 975 |     } | 
 | 976 | } | 
 | 977 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 978 | void AudioFlinger::ThreadBase::clearPowerManager() | 
 | 979 | { | 
 | 980 |     Mutex::Autolock _l(mLock); | 
 | 981 |     releaseWakeLock_l(); | 
 | 982 |     mPowerManager.clear(); | 
 | 983 | } | 
 | 984 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 985 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 986 | { | 
 | 987 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 988 |     if (thread != 0) { | 
 | 989 |         thread->clearPowerManager(); | 
 | 990 |     } | 
 | 991 |     ALOGW("power manager service died !!!"); | 
 | 992 | } | 
 | 993 |  | 
 | 994 | void AudioFlinger::ThreadBase::setEffectSuspended( | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 995 |         const effect_uuid_t *type, bool suspend, audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 996 | { | 
 | 997 |     Mutex::Autolock _l(mLock); | 
 | 998 |     setEffectSuspended_l(type, suspend, sessionId); | 
 | 999 | } | 
 | 1000 |  | 
 | 1001 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1002 |         const effect_uuid_t *type, bool suspend, audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1003 | { | 
 | 1004 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1005 |     if (chain != 0) { | 
 | 1006 |         if (type != NULL) { | 
 | 1007 |             chain->setEffectSuspended_l(type, suspend); | 
 | 1008 |         } else { | 
 | 1009 |             chain->setEffectSuspendedAll_l(suspend); | 
 | 1010 |         } | 
 | 1011 |     } | 
 | 1012 |  | 
 | 1013 |     updateSuspendedSessions_l(type, suspend, sessionId); | 
 | 1014 | } | 
 | 1015 |  | 
 | 1016 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
 | 1017 | { | 
 | 1018 |     ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
 | 1019 |     if (index < 0) { | 
 | 1020 |         return; | 
 | 1021 |     } | 
 | 1022 |  | 
 | 1023 |     const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = | 
 | 1024 |             mSuspendedSessions.valueAt(index); | 
 | 1025 |  | 
 | 1026 |     for (size_t i = 0; i < sessionEffects.size(); i++) { | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1027 |         const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1028 |         for (int j = 0; j < desc->mRefCount; j++) { | 
 | 1029 |             if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
 | 1030 |                 chain->setEffectSuspendedAll_l(true); | 
 | 1031 |             } else { | 
 | 1032 |                 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
 | 1033 |                     desc->mType.timeLow); | 
 | 1034 |                 chain->setEffectSuspended_l(&desc->mType, true); | 
 | 1035 |             } | 
 | 1036 |         } | 
 | 1037 |     } | 
 | 1038 | } | 
 | 1039 |  | 
 | 1040 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
 | 1041 |                                                          bool suspend, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1042 |                                                          audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1043 | { | 
 | 1044 |     ssize_t index = mSuspendedSessions.indexOfKey(sessionId); | 
 | 1045 |  | 
 | 1046 |     KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
 | 1047 |  | 
 | 1048 |     if (suspend) { | 
 | 1049 |         if (index >= 0) { | 
 | 1050 |             sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1051 |         } else { | 
 | 1052 |             mSuspendedSessions.add(sessionId, sessionEffects); | 
 | 1053 |         } | 
 | 1054 |     } else { | 
 | 1055 |         if (index < 0) { | 
 | 1056 |             return; | 
 | 1057 |         } | 
 | 1058 |         sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1059 |     } | 
 | 1060 |  | 
 | 1061 |  | 
 | 1062 |     int key = EffectChain::kKeyForSuspendAll; | 
 | 1063 |     if (type != NULL) { | 
 | 1064 |         key = type->timeLow; | 
 | 1065 |     } | 
 | 1066 |     index = sessionEffects.indexOfKey(key); | 
 | 1067 |  | 
 | 1068 |     sp<SuspendedSessionDesc> desc; | 
 | 1069 |     if (suspend) { | 
 | 1070 |         if (index >= 0) { | 
 | 1071 |             desc = sessionEffects.valueAt(index); | 
 | 1072 |         } else { | 
 | 1073 |             desc = new SuspendedSessionDesc(); | 
 | 1074 |             if (type != NULL) { | 
 | 1075 |                 desc->mType = *type; | 
 | 1076 |             } | 
 | 1077 |             sessionEffects.add(key, desc); | 
 | 1078 |             ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
 | 1079 |         } | 
 | 1080 |         desc->mRefCount++; | 
 | 1081 |     } else { | 
 | 1082 |         if (index < 0) { | 
 | 1083 |             return; | 
 | 1084 |         } | 
 | 1085 |         desc = sessionEffects.valueAt(index); | 
 | 1086 |         if (--desc->mRefCount == 0) { | 
 | 1087 |             ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
 | 1088 |             sessionEffects.removeItemsAt(index); | 
 | 1089 |             if (sessionEffects.isEmpty()) { | 
 | 1090 |                 ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
 | 1091 |                                  sessionId); | 
 | 1092 |                 mSuspendedSessions.removeItem(sessionId); | 
 | 1093 |             } | 
 | 1094 |         } | 
 | 1095 |     } | 
 | 1096 |     if (!sessionEffects.isEmpty()) { | 
 | 1097 |         mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
 | 1098 |     } | 
 | 1099 | } | 
 | 1100 |  | 
 | 1101 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
 | 1102 |                                                             bool enabled, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1103 |                                                             audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1104 | { | 
 | 1105 |     Mutex::Autolock _l(mLock); | 
 | 1106 |     checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
 | 1107 | } | 
 | 1108 |  | 
 | 1109 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
 | 1110 |                                                             bool enabled, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1111 |                                                             audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1112 | { | 
 | 1113 |     if (mType != RECORD) { | 
 | 1114 |         // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
 | 1115 |         // another session. This gives the priority to well behaved effect control panels | 
 | 1116 |         // and applications not using global effects. | 
 | 1117 |         // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect | 
 | 1118 |         // global effects | 
 | 1119 |         if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { | 
 | 1120 |             setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
 | 1121 |         } | 
 | 1122 |     } | 
 | 1123 |  | 
 | 1124 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1125 |     if (chain != 0) { | 
 | 1126 |         chain->checkSuspendOnEffectEnabled(effect, enabled); | 
 | 1127 |     } | 
 | 1128 | } | 
 | 1129 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1130 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held | 
 | 1131 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( | 
 | 1132 |         const effect_descriptor_t *desc, audio_session_t sessionId) | 
 | 1133 | { | 
 | 1134 |     // No global effect sessions on record threads | 
 | 1135 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
 | 1136 |         ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", | 
 | 1137 |                 desc->name, mThreadName); | 
 | 1138 |         return BAD_VALUE; | 
 | 1139 |     } | 
 | 1140 |     // only pre processing effects on record thread | 
 | 1141 |     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { | 
 | 1142 |         ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", | 
 | 1143 |                 desc->name, mThreadName); | 
 | 1144 |         return BAD_VALUE; | 
 | 1145 |     } | 
| Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1146 |  | 
 | 1147 |     // always allow effects without processing load or latency | 
 | 1148 |     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { | 
 | 1149 |         return NO_ERROR; | 
 | 1150 |     } | 
 | 1151 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1152 |     audio_input_flags_t flags = mInput->flags; | 
 | 1153 |     if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { | 
 | 1154 |         if (flags & AUDIO_INPUT_FLAG_RAW) { | 
 | 1155 |             ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", | 
 | 1156 |                   desc->name, mThreadName); | 
 | 1157 |             return BAD_VALUE; | 
 | 1158 |         } | 
 | 1159 |         if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
 | 1160 |             ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", | 
 | 1161 |                   desc->name, mThreadName); | 
 | 1162 |             return BAD_VALUE; | 
 | 1163 |         } | 
 | 1164 |     } | 
 | 1165 |     return NO_ERROR; | 
 | 1166 | } | 
 | 1167 |  | 
 | 1168 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held | 
 | 1169 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( | 
 | 1170 |         const effect_descriptor_t *desc, audio_session_t sessionId) | 
 | 1171 | { | 
 | 1172 |     // no preprocessing on playback threads | 
 | 1173 |     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { | 
 | 1174 |         ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" | 
 | 1175 |                 " thread %s", desc->name, mThreadName); | 
 | 1176 |         return BAD_VALUE; | 
 | 1177 |     } | 
 | 1178 |  | 
 | 1179 |     switch (mType) { | 
 | 1180 |     case MIXER: { | 
 | 1181 |         // Reject any effect on mixer multichannel sinks. | 
 | 1182 |         // TODO: fix both format and multichannel issues with effects. | 
 | 1183 |         if (mChannelCount != FCC_2) { | 
 | 1184 |             ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" | 
 | 1185 |                     " thread %s", desc->name, mChannelCount, mThreadName); | 
 | 1186 |             return BAD_VALUE; | 
 | 1187 |         } | 
 | 1188 |         audio_output_flags_t flags = mOutput->flags; | 
 | 1189 |         if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { | 
 | 1190 |             if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1191 |                 // global effects are applied only to non fast tracks if they are SW | 
 | 1192 |                 if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
 | 1193 |                     break; | 
 | 1194 |                 } | 
 | 1195 |             } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
 | 1196 |                 // only post processing on output stage session | 
 | 1197 |                 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { | 
 | 1198 |                     ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" | 
 | 1199 |                             " on output stage session", desc->name); | 
 | 1200 |                     return BAD_VALUE; | 
 | 1201 |                 } | 
 | 1202 |             } else { | 
 | 1203 |                 // no restriction on effects applied on non fast tracks | 
 | 1204 |                 if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { | 
 | 1205 |                     break; | 
 | 1206 |                 } | 
 | 1207 |             } | 
| Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1208 |  | 
 | 1209 |             // always allow effects without processing load or latency | 
 | 1210 |             if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { | 
 | 1211 |                 break; | 
 | 1212 |             } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1213 |             if (flags & AUDIO_OUTPUT_FLAG_RAW) { | 
 | 1214 |                 ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", | 
 | 1215 |                       desc->name); | 
 | 1216 |                 return BAD_VALUE; | 
 | 1217 |             } | 
 | 1218 |             if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { | 
 | 1219 |                 ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" | 
 | 1220 |                         " in fast mode", desc->name); | 
 | 1221 |                 return BAD_VALUE; | 
 | 1222 |             } | 
 | 1223 |         } | 
 | 1224 |     } break; | 
 | 1225 |     case OFFLOAD: | 
| Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1226 |         // nothing actionable on offload threads, if the effect: | 
 | 1227 |         //   - is offloadable: the effect can be created | 
 | 1228 |         //   - is NOT offloadable: the effect should still be created, but EffectHandle::enable() | 
 | 1229 |         //     will take care of invalidating the tracks of the thread | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1230 |         break; | 
 | 1231 |     case DIRECT: | 
 | 1232 |         // Reject any effect on Direct output threads for now, since the format of | 
 | 1233 |         // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). | 
 | 1234 |         ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", | 
 | 1235 |                 desc->name, mThreadName); | 
 | 1236 |         return BAD_VALUE; | 
 | 1237 |     case DUPLICATING: | 
 | 1238 |         // Reject any effect on mixer multichannel sinks. | 
 | 1239 |         // TODO: fix both format and multichannel issues with effects. | 
 | 1240 |         if (mChannelCount != FCC_2) { | 
 | 1241 |             ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" | 
 | 1242 |                     " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); | 
 | 1243 |             return BAD_VALUE; | 
 | 1244 |         } | 
 | 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(), | 
 | 1421 |              "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", | 
 | 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); | 
 | 1454 |     return NO_ERROR; | 
 | 1455 | } | 
 | 1456 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1457 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1458 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1459 |     ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1460 |     effect_descriptor_t desc = effect->desc(); | 
 | 1461 |     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1462 |         detachAuxEffect_l(effect->id()); | 
 | 1463 |     } | 
 | 1464 |  | 
 | 1465 |     sp<EffectChain> chain = effect->chain().promote(); | 
 | 1466 |     if (chain != 0) { | 
 | 1467 |         // remove effect chain if removing last effect | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1468 |         if (chain->removeEffect_l(effect, release) == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1469 |             removeEffectChain_l(chain); | 
 | 1470 |         } | 
 | 1471 |     } else { | 
 | 1472 |         ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
 | 1473 |     } | 
 | 1474 | } | 
 | 1475 |  | 
 | 1476 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
 | 1477 |         Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 1478 | { | 
 | 1479 |     effectChains = mEffectChains; | 
 | 1480 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 1481 |         mEffectChains[i]->lock(); | 
 | 1482 |     } | 
 | 1483 | } | 
 | 1484 |  | 
 | 1485 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
 | 1486 |         const Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 1487 | { | 
 | 1488 |     for (size_t i = 0; i < effectChains.size(); i++) { | 
 | 1489 |         effectChains[i]->unlock(); | 
 | 1490 |     } | 
 | 1491 | } | 
 | 1492 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1493 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1494 | { | 
 | 1495 |     Mutex::Autolock _l(mLock); | 
 | 1496 |     return getEffectChain_l(sessionId); | 
 | 1497 | } | 
 | 1498 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1499 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) | 
 | 1500 |         const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1501 | { | 
 | 1502 |     size_t size = mEffectChains.size(); | 
 | 1503 |     for (size_t i = 0; i < size; i++) { | 
 | 1504 |         if (mEffectChains[i]->sessionId() == sessionId) { | 
 | 1505 |             return mEffectChains[i]; | 
 | 1506 |         } | 
 | 1507 |     } | 
 | 1508 |     return 0; | 
 | 1509 | } | 
 | 1510 |  | 
 | 1511 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
 | 1512 | { | 
 | 1513 |     Mutex::Autolock _l(mLock); | 
 | 1514 |     size_t size = mEffectChains.size(); | 
 | 1515 |     for (size_t i = 0; i < size; i++) { | 
 | 1516 |         mEffectChains[i]->setMode_l(mode); | 
 | 1517 |     } | 
 | 1518 | } | 
 | 1519 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1520 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) | 
 | 1521 | { | 
 | 1522 |     config->type = AUDIO_PORT_TYPE_MIX; | 
 | 1523 |     config->ext.mix.handle = mId; | 
 | 1524 |     config->sample_rate = mSampleRate; | 
 | 1525 |     config->format = mFormat; | 
 | 1526 |     config->channel_mask = mChannelMask; | 
 | 1527 |     config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
 | 1528 |                             AUDIO_PORT_CONFIG_FORMAT; | 
 | 1529 | } | 
 | 1530 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1531 | void AudioFlinger::ThreadBase::systemReady() | 
 | 1532 | { | 
 | 1533 |     Mutex::Autolock _l(mLock); | 
 | 1534 |     if (mSystemReady) { | 
 | 1535 |         return; | 
 | 1536 |     } | 
 | 1537 |     mSystemReady = true; | 
 | 1538 |  | 
 | 1539 |     for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { | 
 | 1540 |         sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); | 
 | 1541 |     } | 
 | 1542 |     mPendingConfigEvents.clear(); | 
 | 1543 | } | 
 | 1544 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1545 | template <typename T> | 
 | 1546 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) { | 
 | 1547 |     ssize_t index = mActiveTracks.indexOf(track); | 
 | 1548 |     if (index >= 0) { | 
 | 1549 |         ALOGW("ActiveTracks<T>::add track %p already there", track.get()); | 
 | 1550 |         return index; | 
 | 1551 |     } | 
 | 1552 |     mActiveTracksGeneration++; | 
 | 1553 |     mLatestActiveTrack = track; | 
 | 1554 |     ++mBatteryCounter[track->uid()].second; | 
 | 1555 |     return mActiveTracks.add(track); | 
 | 1556 | } | 
 | 1557 |  | 
 | 1558 | template <typename T> | 
 | 1559 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) { | 
 | 1560 |     ssize_t index = mActiveTracks.remove(track); | 
 | 1561 |     if (index < 0) { | 
 | 1562 |         ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); | 
 | 1563 |         return index; | 
 | 1564 |     } | 
 | 1565 |     mActiveTracksGeneration++; | 
 | 1566 |     --mBatteryCounter[track->uid()].second; | 
 | 1567 |     // mLatestActiveTrack is not cleared even if is the same as track. | 
 | 1568 |     return index; | 
 | 1569 | } | 
 | 1570 |  | 
 | 1571 | template <typename T> | 
 | 1572 | void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() { | 
 | 1573 |     for (const sp<T> &track : mActiveTracks) { | 
 | 1574 |         BatteryNotifier::getInstance().noteStopAudio(track->uid()); | 
 | 1575 |     } | 
 | 1576 |     mLastActiveTracksGeneration = mActiveTracksGeneration; | 
 | 1577 |     mActiveTracks.clear(); | 
 | 1578 |     mLatestActiveTrack.clear(); | 
 | 1579 |     mBatteryCounter.clear(); | 
 | 1580 | } | 
 | 1581 |  | 
 | 1582 | template <typename T> | 
 | 1583 | void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState( | 
 | 1584 |         sp<ThreadBase> thread, bool force) { | 
 | 1585 |     // Updates ActiveTracks client uids to the thread wakelock. | 
 | 1586 |     if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { | 
 | 1587 |         thread->updateWakeLockUids_l(getWakeLockUids()); | 
 | 1588 |         mLastActiveTracksGeneration = mActiveTracksGeneration; | 
 | 1589 |     } | 
 | 1590 |  | 
 | 1591 |     // Updates BatteryNotifier uids | 
 | 1592 |     for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) { | 
 | 1593 |         const uid_t uid = it->first; | 
 | 1594 |         ssize_t &previous = it->second.first; | 
 | 1595 |         ssize_t ¤t = it->second.second; | 
 | 1596 |         if (current > 0) { | 
 | 1597 |             if (previous == 0) { | 
 | 1598 |                 BatteryNotifier::getInstance().noteStartAudio(uid); | 
 | 1599 |             } | 
 | 1600 |             previous = current; | 
 | 1601 |             ++it; | 
 | 1602 |         } else if (current == 0) { | 
 | 1603 |             if (previous > 0) { | 
 | 1604 |                 BatteryNotifier::getInstance().noteStopAudio(uid); | 
 | 1605 |             } | 
 | 1606 |             it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase. | 
 | 1607 |         } else /* (current < 0) */ { | 
 | 1608 |             LOG_ALWAYS_FATAL("negative battery count %zd", current); | 
 | 1609 |         } | 
 | 1610 |     } | 
 | 1611 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1612 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1613 | void AudioFlinger::ThreadBase::broadcast_l() | 
 | 1614 | { | 
 | 1615 |     // Thread could be blocked waiting for async | 
 | 1616 |     // so signal it to handle state changes immediately | 
 | 1617 |     // If threadLoop is currently unlocked a signal of mWaitWorkCV will | 
 | 1618 |     // be lost so we also flag to prevent it blocking on mWaitWorkCV | 
 | 1619 |     mSignalPending = true; | 
 | 1620 |     mWaitWorkCV.broadcast(); | 
 | 1621 | } | 
 | 1622 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1623 | // ---------------------------------------------------------------------------- | 
 | 1624 | //      Playback | 
 | 1625 | // ---------------------------------------------------------------------------- | 
 | 1626 |  | 
 | 1627 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
 | 1628 |                                              AudioStreamOut* output, | 
 | 1629 |                                              audio_io_handle_t id, | 
 | 1630 |                                              audio_devices_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1631 |                                              type_t type, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1632 |                                              bool systemReady) | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1633 |     :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1634 |         mNormalFrameCount(0), mSinkBuffer(NULL), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1635 |         mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1636 |         mMixerBuffer(NULL), | 
 | 1637 |         mMixerBufferSize(0), | 
 | 1638 |         mMixerBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 1639 |         mMixerBufferValid(false), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1640 |         mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1641 |         mEffectBuffer(NULL), | 
 | 1642 |         mEffectBufferSize(0), | 
 | 1643 |         mEffectBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 1644 |         mEffectBufferValid(false), | 
| Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1645 |         mSuspended(0), mBytesWritten(0), | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1646 |         mFramesWritten(0), | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1647 |         mSuspendedFrames(0), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1648 |         // mStreamTypes[] initialized in constructor body | 
 | 1649 |         mOutput(output), | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1650 |         mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1651 |         mMixerStatus(MIXER_IDLE), | 
 | 1652 |         mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1653 |         mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1654 |         mBytesRemaining(0), | 
 | 1655 |         mCurrentWriteLength(0), | 
 | 1656 |         mUseAsyncWrite(false), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1657 |         mWriteAckSequence(0), | 
 | 1658 |         mDrainSequence(0), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 |         mScreenState(AudioFlinger::mScreenState), | 
 | 1660 |         // index 0 is reserved for normal mixer's submix | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1661 |         mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1662 |         mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1664 |     snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); | 
 | 1665 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1666 |  | 
 | 1667 |     // Assumes constructor is called by AudioFlinger with it's mLock held, but | 
 | 1668 |     // it would be safer to explicitly pass initial masterVolume/masterMute as | 
 | 1669 |     // parameter. | 
 | 1670 |     // | 
 | 1671 |     // If the HAL we are using has support for master volume or master mute, | 
 | 1672 |     // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
 | 1673 |     // and the mute set to false). | 
 | 1674 |     mMasterVolume = audioFlinger->masterVolume_l(); | 
 | 1675 |     mMasterMute = audioFlinger->masterMute_l(); | 
 | 1676 |     if (mOutput && mOutput->audioHwDev) { | 
 | 1677 |         if (mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 1678 |             mMasterVolume = 1.0; | 
 | 1679 |         } | 
 | 1680 |  | 
 | 1681 |         if (mOutput->audioHwDev->canSetMasterMute()) { | 
 | 1682 |             mMasterMute = false; | 
 | 1683 |         } | 
 | 1684 |     } | 
 | 1685 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1686 |     readOutputParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1687 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1688 |     // ++ operator does not compile | 
| Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1689 |     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] | 1690 |             stream = (audio_stream_type_t) (stream + 1)) { | 
 | 1691 |         mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); | 
 | 1692 |         mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); | 
 | 1693 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1694 | } | 
 | 1695 |  | 
 | 1696 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
 | 1697 | { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1698 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1699 |     free(mSinkBuffer); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1700 |     free(mMixerBuffer); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1701 |     free(mEffectBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1702 | } | 
 | 1703 |  | 
 | 1704 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
 | 1705 | { | 
 | 1706 |     dumpInternals(fd, args); | 
 | 1707 |     dumpTracks(fd, args); | 
 | 1708 |     dumpEffectChains(fd, args); | 
| Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1709 |     dprintf(fd, "  Local log:\n"); | 
 | 1710 |     mLocalLog.dump(fd, "   " /* prefix */, 40 /* lines */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1711 | } | 
 | 1712 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1713 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1714 | { | 
 | 1715 |     const size_t SIZE = 256; | 
 | 1716 |     char buffer[SIZE]; | 
 | 1717 |     String8 result; | 
 | 1718 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1719 |     result.appendFormat("  Stream volumes in dB: "); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1720 |     for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
 | 1721 |         const stream_type_t *st = &mStreamTypes[i]; | 
 | 1722 |         if (i > 0) { | 
 | 1723 |             result.appendFormat(", "); | 
 | 1724 |         } | 
 | 1725 |         result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
 | 1726 |         if (st->mute) { | 
 | 1727 |             result.append("M"); | 
 | 1728 |         } | 
 | 1729 |     } | 
 | 1730 |     result.append("\n"); | 
 | 1731 |     write(fd, result.string(), result.length()); | 
 | 1732 |     result.clear(); | 
 | 1733 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 |     // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
 | 1735 |     FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1736 |     dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1737 |             underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1738 |  | 
 | 1739 |     size_t numtracks = mTracks.size(); | 
 | 1740 |     size_t numactive = mActiveTracks.size(); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1741 |     dprintf(fd, "  %zu Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1742 |     size_t numactiveseen = 0; | 
 | 1743 |     if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1744 |         dprintf(fd, " of which %zu are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1745 |         Track::appendDumpHeader(result); | 
 | 1746 |         for (size_t i = 0; i < numtracks; ++i) { | 
 | 1747 |             sp<Track> track = mTracks[i]; | 
 | 1748 |             if (track != 0) { | 
 | 1749 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 1750 |                 if (active) { | 
 | 1751 |                     numactiveseen++; | 
 | 1752 |                 } | 
 | 1753 |                 track->dump(buffer, SIZE, active); | 
 | 1754 |                 result.append(buffer); | 
 | 1755 |             } | 
 | 1756 |         } | 
 | 1757 |     } else { | 
 | 1758 |         result.append("\n"); | 
 | 1759 |     } | 
 | 1760 |     if (numactiveseen != numactive) { | 
 | 1761 |         // some tracks in the active list were not in the tracks list | 
 | 1762 |         snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
 | 1763 |                 " not in the track list\n"); | 
 | 1764 |         result.append(buffer); | 
 | 1765 |         Track::appendDumpHeader(result); | 
 | 1766 |         for (size_t i = 0; i < numactive; ++i) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1767 |             sp<Track> track = mActiveTracks[i]; | 
 | 1768 |             if (mTracks.indexOf(track) < 0) { | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1769 |                 track->dump(buffer, SIZE, true); | 
 | 1770 |                 result.append(buffer); | 
 | 1771 |             } | 
 | 1772 |         } | 
 | 1773 |     } | 
 | 1774 |  | 
 | 1775 |     write(fd, result.string(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1776 | } | 
 | 1777 |  | 
 | 1778 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 1779 | { | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1780 |     dumpBase(fd, args); | 
 | 1781 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1782 |     dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1783 |     dprintf(fd, "  Last write occurred (msecs): %llu\n", | 
 | 1784 |             (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1785 |     dprintf(fd, "  Total writes: %d\n", mNumWrites); | 
 | 1786 |     dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites); | 
 | 1787 |     dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no"); | 
 | 1788 |     dprintf(fd, "  Suspend count: %d\n", mSuspended); | 
 | 1789 |     dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer); | 
 | 1790 |     dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer); | 
 | 1791 |     dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer); | 
 | 1792 |     dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1793 |     dprintf(fd, "  Standby delay ns=%lld\n", (long long)mStandbyDelayNs); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1794 |     AudioStreamOut *output = mOutput; | 
 | 1795 |     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] | 1796 |     dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", | 
 | 1797 |             output, flags, outputFlagsToString(flags).c_str()); | 
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 1798 |     dprintf(fd, "  Frames written: %lld\n", (long long)mFramesWritten); | 
 | 1799 |     dprintf(fd, "  Suspended frames: %lld\n", (long long)mSuspendedFrames); | 
 | 1800 |     if (mPipeSink.get() != nullptr) { | 
 | 1801 |         dprintf(fd, "  PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); | 
 | 1802 |     } | 
 | 1803 |     if (output != nullptr) { | 
 | 1804 |         dprintf(fd, "  Hal stream dump:\n"); | 
 | 1805 |         (void)output->stream->dump(fd); | 
 | 1806 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1807 | } | 
 | 1808 |  | 
 | 1809 | // Thread virtuals | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1810 |  | 
 | 1811 | void AudioFlinger::PlaybackThread::onFirstRef() | 
 | 1812 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1813 |     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1814 | } | 
 | 1815 |  | 
 | 1816 | // ThreadBase virtuals | 
 | 1817 | void AudioFlinger::PlaybackThread::preExit() | 
 | 1818 | { | 
 | 1819 |     ALOGV("  preExit()"); | 
 | 1820 |     // FIXME this is using hard-coded strings but in the future, this functionality will be | 
 | 1821 |     //       converted to use audio HAL extensions required to support tunneling | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1822 |     status_t result = mOutput->stream->setParameters(String8("exiting=1")); | 
 | 1823 |     ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1824 | } | 
 | 1825 |  | 
 | 1826 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
 | 1827 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( | 
 | 1828 |         const sp<AudioFlinger::Client>& client, | 
 | 1829 |         audio_stream_type_t streamType, | 
 | 1830 |         uint32_t sampleRate, | 
 | 1831 |         audio_format_t format, | 
 | 1832 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1833 |         size_t *pFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1834 |         const sp<IMemory>& sharedBuffer, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1835 |         audio_session_t sessionId, | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1836 |         audio_output_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1837 |         pid_t tid, | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1838 |         uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1839 |         status_t *status, | 
 | 1840 |         audio_port_handle_t portId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1841 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1842 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1843 |     sp<Track> track; | 
 | 1844 |     status_t lStatus; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1845 |     audio_output_flags_t outputFlags = mOutput->flags; | 
 | 1846 |  | 
 | 1847 |     // special case for FAST flag considered OK if fast mixer is present | 
 | 1848 |     if (hasFastMixer()) { | 
 | 1849 |         outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); | 
 | 1850 |     } | 
 | 1851 |  | 
 | 1852 |     // Check if requested flags are compatible with output stream flags | 
 | 1853 |     if ((*flags & outputFlags) != *flags) { | 
 | 1854 |         ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", | 
 | 1855 |               *flags, outputFlags); | 
 | 1856 |         *flags = (audio_output_flags_t)(*flags & outputFlags); | 
 | 1857 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1858 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1859 |     // client expresses a preference for FAST, but we get the final say | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1860 |     if (*flags & AUDIO_OUTPUT_FLAG_FAST) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1861 |       if ( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1862 |             // PCM data | 
 | 1863 |             audio_is_linear_pcm(format) && | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1864 |             // TODO: extract as a data library function that checks that a computationally | 
 | 1865 |             // expensive downmixer is not required: isFastOutputChannelConversion() | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1866 |             (channelMask == mChannelMask || | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1867 |                     mChannelMask != AUDIO_CHANNEL_OUT_STEREO || | 
 | 1868 |                     (channelMask == AUDIO_CHANNEL_OUT_MONO | 
 | 1869 |                             /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1870 |             // hardware sample rate | 
 | 1871 |             (sampleRate == mSampleRate) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1872 |             // normal mixer has an associated fast mixer | 
 | 1873 |             hasFastMixer() && | 
 | 1874 |             // there are sufficient fast track slots available | 
 | 1875 |             (mFastTrackAvailMask != 0) | 
 | 1876 |             // FIXME test that MixerThread for this fast track has a capable output HAL | 
 | 1877 |             // FIXME add a permission test also? | 
 | 1878 |         ) { | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1879 |         // static tracks can have any nonzero framecount, streaming tracks check against minimum. | 
 | 1880 |         if (sharedBuffer == 0) { | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1881 |             // read the fast track multiplier property the first time it is needed | 
 | 1882 |             int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); | 
 | 1883 |             if (ok != 0) { | 
 | 1884 |                 ALOGE("%s pthread_once failed: %d", __func__, ok); | 
 | 1885 |             } | 
| Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1886 |             frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1887 |         } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1888 |  | 
 | 1889 |         // check compatibility with audio effects. | 
 | 1890 |         { // scope for mLock | 
 | 1891 |             Mutex::Autolock _l(mLock); | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 1892 |             for (audio_session_t session : { | 
 | 1893 |                     AUDIO_SESSION_OUTPUT_STAGE, | 
 | 1894 |                     AUDIO_SESSION_OUTPUT_MIX, | 
 | 1895 |                     sessionId, | 
 | 1896 |                 }) { | 
 | 1897 |                 sp<EffectChain> chain = getEffectChain_l(session); | 
 | 1898 |                 if (chain.get() != nullptr) { | 
 | 1899 |                     audio_output_flags_t old = *flags; | 
 | 1900 |                     chain->checkOutputFlagCompatibility(flags); | 
 | 1901 |                     if (old != *flags) { | 
 | 1902 |                         ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", | 
 | 1903 |                                 (int)session, (int)old, (int)*flags); | 
 | 1904 |                     } | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1905 |                 } | 
 | 1906 |             } | 
 | 1907 |         } | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1908 |         ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1909 |                  "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
 | 1910 |                  frameCount, mFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1911 |       } else { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1912 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " | 
 | 1913 |                 "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1914 |                 "sampleRate=%u mSampleRate=%u " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1915 |                 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1916 |                 sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1917 |                 audio_is_linear_pcm(format), | 
 | 1918 |                 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1919 |         *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1920 |       } | 
 | 1921 |     } | 
 | 1922 |     // For normal PCM streaming tracks, update minimum frame count. | 
 | 1923 |     // For compatibility with AudioTrack calculation, buffer depth is forced | 
 | 1924 |     // to be at least 2 x the normal mixer frame count and cover audio hardware latency. | 
 | 1925 |     // This is probably too conservative, but legacy application code may depend on it. | 
 | 1926 |     // If you change this calculation, also review the start threshold which is related. | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1927 |     if (!(*flags & AUDIO_OUTPUT_FLAG_FAST) | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1928 |             && audio_has_proportional_frames(format) && sharedBuffer == 0) { | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1929 |         // this must match AudioTrack.cpp calculateMinFrameCount(). | 
 | 1930 |         // TODO: Move to a common library | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1931 |         uint32_t latencyMs = 0; | 
 | 1932 |         lStatus = mOutput->stream->getLatency(&latencyMs); | 
 | 1933 |         if (lStatus != OK) { | 
 | 1934 |             ALOGE("Error when retrieving output stream latency: %d", lStatus); | 
 | 1935 |             goto Exit; | 
 | 1936 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1937 |         uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); | 
 | 1938 |         if (minBufCount < 2) { | 
 | 1939 |             minBufCount = 2; | 
 | 1940 |         } | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1941 |         // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack | 
 | 1942 |         // or the client should compute and pass in a larger buffer request. | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1943 |         size_t minFrameCount = | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1944 |                 minBufCount * sourceFramesNeededWithTimestretch( | 
 | 1945 |                         sampleRate, mNormalFrameCount, | 
 | 1946 |                         mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1947 |         if (frameCount < minFrameCount) { // including frameCount == 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1948 |             frameCount = minFrameCount; | 
 | 1949 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1950 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1951 |     *pFrameCount = frameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1952 |  | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1953 |     switch (mType) { | 
 | 1954 |  | 
 | 1955 |     case DIRECT: | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1956 |         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] | 1957 |             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1958 |                 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " | 
 | 1959 |                         "for output %p with format %#x", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1960 |                         sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1961 |                 lStatus = BAD_VALUE; | 
 | 1962 |                 goto Exit; | 
 | 1963 |             } | 
 | 1964 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1965 |         break; | 
 | 1966 |  | 
 | 1967 |     case OFFLOAD: | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1968 |         if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1969 |             ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" | 
 | 1970 |                     "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1971 |                     sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1972 |             lStatus = BAD_VALUE; | 
 | 1973 |             goto Exit; | 
 | 1974 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1975 |         break; | 
 | 1976 |  | 
 | 1977 |     default: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1978 |         if (!audio_is_linear_pcm(format)) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1979 |                 ALOGE("createTrack_l() Bad parameter: format %#x \"" | 
 | 1980 |                         "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1981 |                         format, mOutput, mFormat); | 
 | 1982 |                 lStatus = BAD_VALUE; | 
 | 1983 |                 goto Exit; | 
 | 1984 |         } | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1985 |         if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1986 |             ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
 | 1987 |             lStatus = BAD_VALUE; | 
 | 1988 |             goto Exit; | 
 | 1989 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1990 |         break; | 
 | 1991 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1992 |     } | 
 | 1993 |  | 
 | 1994 |     lStatus = initCheck(); | 
 | 1995 |     if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1996 |         ALOGE("createTrack_l() audio driver not initialized"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1997 |         goto Exit; | 
 | 1998 |     } | 
 | 1999 |  | 
 | 2000 |     { // scope for mLock | 
 | 2001 |         Mutex::Autolock _l(mLock); | 
 | 2002 |  | 
 | 2003 |         // all tracks in same audio session must share the same routing strategy otherwise | 
 | 2004 |         // conflicts will happen when tracks are moved from one output to another by audio policy | 
 | 2005 |         // manager | 
 | 2006 |         uint32_t strategy = AudioSystem::getStrategyForStream(streamType); | 
 | 2007 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2008 |             sp<Track> t = mTracks[i]; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2009 |             if (t != 0 && t->isExternalTrack()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2010 |                 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); | 
 | 2011 |                 if (sessionId == t->sessionId() && strategy != actual) { | 
 | 2012 |                     ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
 | 2013 |                             strategy, actual); | 
 | 2014 |                     lStatus = BAD_VALUE; | 
 | 2015 |                     goto Exit; | 
 | 2016 |                 } | 
 | 2017 |             } | 
 | 2018 |         } | 
 | 2019 |  | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2020 |         track = new Track(this, client, streamType, sampleRate, format, | 
 | 2021 |                           channelMask, frameCount, NULL, sharedBuffer, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2022 |                           sessionId, uid, *flags, TrackBase::TYPE_DEFAULT, portId); | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2023 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2024 |         lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; | 
 | 2025 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2026 |             ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2027 |             // 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] | 2028 |             goto Exit; | 
 | 2029 |         } | 
 | 2030 |         mTracks.add(track); | 
 | 2031 |  | 
 | 2032 |         sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 2033 |         if (chain != 0) { | 
 | 2034 |             ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
 | 2035 |             track->setMainBuffer(chain->inBuffer()); | 
 | 2036 |             chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); | 
 | 2037 |             chain->incTrackCnt(); | 
 | 2038 |         } | 
 | 2039 |  | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2040 |         if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2041 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 2042 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 2043 |             // so ask activity manager to do this on our behalf | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 2044 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*isForApp*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2045 |         } | 
 | 2046 |     } | 
 | 2047 |  | 
 | 2048 |     lStatus = NO_ERROR; | 
 | 2049 |  | 
 | 2050 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2051 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2052 |     return track; | 
 | 2053 | } | 
 | 2054 |  | 
 | 2055 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const | 
 | 2056 | { | 
 | 2057 |     return latency; | 
 | 2058 | } | 
 | 2059 |  | 
 | 2060 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
 | 2061 | { | 
 | 2062 |     Mutex::Autolock _l(mLock); | 
 | 2063 |     return latency_l(); | 
 | 2064 | } | 
 | 2065 | uint32_t AudioFlinger::PlaybackThread::latency_l() const | 
 | 2066 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2067 |     uint32_t latency; | 
 | 2068 |     if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { | 
 | 2069 |         return correctLatency_l(latency); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2070 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2071 |     return 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2072 | } | 
 | 2073 |  | 
 | 2074 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
 | 2075 | { | 
 | 2076 |     Mutex::Autolock _l(mLock); | 
 | 2077 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 2078 |     if (mOutput && mOutput->audioHwDev && | 
 | 2079 |         mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 2080 |         mMasterVolume = 1.0; | 
 | 2081 |     } else { | 
 | 2082 |         mMasterVolume = value; | 
 | 2083 |     } | 
 | 2084 | } | 
 | 2085 |  | 
 | 2086 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
 | 2087 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2088 |     if (isDuplicating()) { | 
 | 2089 |         return; | 
 | 2090 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2091 |     Mutex::Autolock _l(mLock); | 
 | 2092 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 2093 |     if (mOutput && mOutput->audioHwDev && | 
 | 2094 |         mOutput->audioHwDev->canSetMasterMute()) { | 
 | 2095 |         mMasterMute = false; | 
 | 2096 |     } else { | 
 | 2097 |         mMasterMute = muted; | 
 | 2098 |     } | 
 | 2099 | } | 
 | 2100 |  | 
 | 2101 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
 | 2102 | { | 
 | 2103 |     Mutex::Autolock _l(mLock); | 
 | 2104 |     mStreamTypes[stream].volume = value; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2105 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2106 | } | 
 | 2107 |  | 
 | 2108 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
 | 2109 | { | 
 | 2110 |     Mutex::Autolock _l(mLock); | 
 | 2111 |     mStreamTypes[stream].mute = muted; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2112 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2113 | } | 
 | 2114 |  | 
 | 2115 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
 | 2116 | { | 
 | 2117 |     Mutex::Autolock _l(mLock); | 
 | 2118 |     return mStreamTypes[stream].volume; | 
 | 2119 | } | 
 | 2120 |  | 
 | 2121 | // addTrack_l() must be called with ThreadBase::mLock held | 
 | 2122 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
 | 2123 | { | 
 | 2124 |     status_t status = ALREADY_EXISTS; | 
 | 2125 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2126 |     if (mActiveTracks.indexOf(track) < 0) { | 
 | 2127 |         // the track is newly added, make sure it fills up all its | 
 | 2128 |         // buffers before playing. This is to ensure the client will | 
 | 2129 |         // effectively get the latency it requested. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2130 |         if (track->isExternalTrack()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2131 |             TrackBase::track_state state = track->mState; | 
 | 2132 |             mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2133 |             status = AudioSystem::startOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2134 |                                               track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2135 |             mLock.lock(); | 
 | 2136 |             // abort track was stopped/paused while we released the lock | 
 | 2137 |             if (state != track->mState) { | 
 | 2138 |                 if (status == NO_ERROR) { | 
 | 2139 |                     mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2140 |                     AudioSystem::stopOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2141 |                                             track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2142 |                     mLock.lock(); | 
 | 2143 |                 } | 
 | 2144 |                 return INVALID_OPERATION; | 
 | 2145 |             } | 
 | 2146 |             // abort if start is rejected by audio policy manager | 
 | 2147 |             if (status != NO_ERROR) { | 
 | 2148 |                 return PERMISSION_DENIED; | 
 | 2149 |             } | 
 | 2150 | #ifdef ADD_BATTERY_DATA | 
 | 2151 |             // to track the speaker usage | 
 | 2152 |             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
 | 2153 | #endif | 
 | 2154 |         } | 
 | 2155 |  | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2156 |         // set retry count for buffer fill | 
 | 2157 |         if (track->isOffloaded()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2158 |             if (track->isStopping_1()) { | 
 | 2159 |                 track->mRetryCount = kMaxTrackStopRetriesOffload; | 
 | 2160 |             } else { | 
 | 2161 |                 track->mRetryCount = kMaxTrackStartupRetriesOffload; | 
 | 2162 |             } | 
 | 2163 |             track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2164 |         } else { | 
 | 2165 |             track->mRetryCount = kMaxTrackStartupRetries; | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2166 |             track->mFillingUpStatus = | 
 | 2167 |                     track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2168 |         } | 
 | 2169 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2170 |         track->mResetDone = false; | 
 | 2171 |         track->mPresentationCompleteFrames = 0; | 
 | 2172 |         mActiveTracks.add(track); | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2173 |         sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 2174 |         if (chain != 0) { | 
 | 2175 |             ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
 | 2176 |                     track->sessionId()); | 
 | 2177 |             chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2178 |         } | 
 | 2179 |  | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2180 |         char buffer[256]; | 
| Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 2181 |         track->dump(buffer, arraysize(buffer), false /* active */); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2182 |         mLocalLog.log("addTrack_l    (%p) %s", track.get(), buffer + 4); // log for analysis | 
 | 2183 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2184 |         status = NO_ERROR; | 
 | 2185 |     } | 
 | 2186 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2187 |     onAddNewTrack_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2188 |     return status; | 
 | 2189 | } | 
 | 2190 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2191 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2192 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2193 |     track->terminate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2194 |     // active tracks are removed by threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2195 |     bool trackActive = (mActiveTracks.indexOf(track) >= 0); | 
 | 2196 |     track->mState = TrackBase::STOPPED; | 
 | 2197 |     if (!trackActive) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2198 |         removeTrack_l(track); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2199 |     } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2200 |         track->mState = TrackBase::STOPPING_1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2201 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2202 |  | 
 | 2203 |     return trackActive; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2204 | } | 
 | 2205 |  | 
 | 2206 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
 | 2207 | { | 
 | 2208 |     track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2209 |  | 
 | 2210 |     char buffer[256]; | 
| Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 2211 |     track->dump(buffer, arraysize(buffer), false /* active */); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2212 |     mLocalLog.log("removeTrack_l (%p) %s", track.get(), buffer + 4); // log for analysis | 
 | 2213 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2214 |     mTracks.remove(track); | 
 | 2215 |     deleteTrackName_l(track->name()); | 
 | 2216 |     // redundant as track is about to be destroyed, for dumpsys only | 
 | 2217 |     track->mName = -1; | 
 | 2218 |     if (track->isFastTrack()) { | 
 | 2219 |         int index = track->mFastIndex; | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2220 |         ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2221 |         ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
 | 2222 |         mFastTrackAvailMask |= 1 << index; | 
 | 2223 |         // redundant as track is about to be destroyed, for dumpsys only | 
 | 2224 |         track->mFastIndex = -1; | 
 | 2225 |     } | 
 | 2226 |     sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 2227 |     if (chain != 0) { | 
 | 2228 |         chain->decTrackCnt(); | 
 | 2229 |     } | 
 | 2230 | } | 
 | 2231 |  | 
 | 2232 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
 | 2233 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2234 |     Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2235 |     String8 out_s8; | 
 | 2236 |     if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { | 
 | 2237 |         return out_s8; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2238 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2239 |     return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2240 | } | 
 | 2241 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2242 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2243 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 2244 |     ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2245 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2246 |     desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2247 |  | 
 | 2248 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2249 |     case AUDIO_OUTPUT_OPENED: | 
 | 2250 |     case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2251 |         desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2252 |         desc->mChannelMask = mChannelMask; | 
 | 2253 |         desc->mSamplingRate = mSampleRate; | 
 | 2254 |         desc->mFormat = mFormat; | 
 | 2255 |         desc->mFrameCount = mNormalFrameCount; // FIXME see | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2256 |                                              // AudioFlinger::frameCount(audio_io_handle_t) | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2257 |         desc->mFrameCountHAL = mFrameCount; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2258 |         desc->mLatency = latency_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2259 |         break; | 
 | 2260 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2261 |     case AUDIO_OUTPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2262 |     default: | 
 | 2263 |         break; | 
 | 2264 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2265 |     mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2266 | } | 
 | 2267 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2268 | void AudioFlinger::PlaybackThread::onWriteReady() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2269 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2270 |     mCallbackThread->resetWriteBlocked(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2271 | } | 
 | 2272 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2273 | void AudioFlinger::PlaybackThread::onDrainReady() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2274 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2275 |     mCallbackThread->resetDraining(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2276 | } | 
 | 2277 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2278 | void AudioFlinger::PlaybackThread::onError() | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2279 | { | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2280 |     mCallbackThread->setAsyncError(); | 
 | 2281 | } | 
 | 2282 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2283 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2284 | { | 
 | 2285 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2286 |     // reject out of sequence requests | 
 | 2287 |     if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { | 
 | 2288 |         mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2289 |         mWaitWorkCV.signal(); | 
 | 2290 |     } | 
 | 2291 | } | 
 | 2292 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2293 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2294 | { | 
 | 2295 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2296 |     // reject out of sequence requests | 
 | 2297 |     if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { | 
 | 2298 |         mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2299 |         mWaitWorkCV.signal(); | 
 | 2300 |     } | 
 | 2301 | } | 
 | 2302 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2303 | void AudioFlinger::PlaybackThread::readOutputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2304 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2305 |     // 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] | 2306 |     mSampleRate = mOutput->getSampleRate(); | 
 | 2307 |     mChannelMask = mOutput->getChannelMask(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2308 |     if (!audio_is_output_channel(mChannelMask)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2309 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2310 |     } | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2311 |     if ((mType == MIXER || mType == DUPLICATING) | 
 | 2312 |             && !isValidPcmSinkChannelMask(mChannelMask)) { | 
 | 2313 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", | 
 | 2314 |                 mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2315 |     } | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2316 |     mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2317 |  | 
 | 2318 |     // Get actual HAL format. | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2319 |     status_t result = mOutput->stream->getFormat(&mHALFormat); | 
 | 2320 |     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] | 2321 |     // Get format from the shim, which will be different than the HAL format | 
 | 2322 |     // if playing compressed audio over HDMI passthrough. | 
 | 2323 |     mFormat = mOutput->getFormat(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2324 |     if (!audio_is_valid_format(mFormat)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2325 |         LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2326 |     } | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2327 |     if ((mType == MIXER || mType == DUPLICATING) | 
 | 2328 |             && !isValidPcmSinkFormat(mFormat)) { | 
 | 2329 |         LOG_FATAL("HAL format %#x not supported for mixed output", | 
 | 2330 |                 mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2331 |     } | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2332 |     mFrameSize = mOutput->getFrameSize(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2333 |     result = mOutput->stream->getBufferSize(&mBufferSize); | 
 | 2334 |     LOG_ALWAYS_FATAL_IF(result != OK, | 
 | 2335 |             "Error when retrieving output stream buffer size: %d", result); | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2336 |     mFrameCount = mBufferSize / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2337 |     if (mFrameCount & 15) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2338 |         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] | 2339 |                 mFrameCount); | 
 | 2340 |     } | 
 | 2341 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2342 |     if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) { | 
 | 2343 |         if (mOutput->stream->setCallback(this) == OK) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2344 |             mUseAsyncWrite = true; | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2345 |             mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2346 |         } | 
 | 2347 |     } | 
 | 2348 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2349 |     mHwSupportsPause = false; | 
 | 2350 |     if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2351 |         bool supportsPause = false, supportsResume = false; | 
 | 2352 |         if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { | 
 | 2353 |             if (supportsPause && supportsResume) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2354 |                 mHwSupportsPause = true; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2355 |             } else if (supportsPause) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2356 |                 ALOGW("direct output implements pause but not resume"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2357 |             } else if (supportsResume) { | 
 | 2358 |                 ALOGW("direct output implements resume but not pause"); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2359 |             } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2360 |         } | 
 | 2361 |     } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2362 |     if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { | 
 | 2363 |         LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); | 
 | 2364 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2365 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2366 |     if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 2367 |         // For best precision, we use float instead of the associated output | 
 | 2368 |         // device format (typically PCM 16 bit). | 
 | 2369 |  | 
 | 2370 |         mFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 2371 |         mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 2372 |         mBufferSize = mFrameSize * mFrameCount; | 
 | 2373 |  | 
 | 2374 |         // TODO: We currently use the associated output device channel mask and sample rate. | 
 | 2375 |         // (1) Perhaps use the ORed channel mask of all downstream MixerThreads | 
 | 2376 |         // (if a valid mask) to avoid premature downmix. | 
 | 2377 |         // (2) Perhaps use the maximum sample rate of all downstream MixerThreads | 
 | 2378 |         // instead of the output device sample rate to avoid loss of high frequency information. | 
 | 2379 |         // This may need to be updated as MixerThread/OutputTracks are added and not here. | 
 | 2380 |     } | 
 | 2381 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2382 |     // 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] | 2383 |     double multiplier = 1.0; | 
 | 2384 |     if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
 | 2385 |             kUseFastMixer == FastMixer_Dynamic)) { | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2386 |         size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
 | 2387 |         size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
| Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2388 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 |         // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
 | 2390 |         minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
 | 2391 |         maxNormalFrameCount = maxNormalFrameCount & ~15; | 
 | 2392 |         if (maxNormalFrameCount < minNormalFrameCount) { | 
 | 2393 |             maxNormalFrameCount = minNormalFrameCount; | 
 | 2394 |         } | 
 | 2395 |         multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
 | 2396 |         if (multiplier <= 1.0) { | 
 | 2397 |             multiplier = 1.0; | 
 | 2398 |         } else if (multiplier <= 2.0) { | 
 | 2399 |             if (2 * mFrameCount <= maxNormalFrameCount) { | 
 | 2400 |                 multiplier = 2.0; | 
 | 2401 |             } else { | 
 | 2402 |                 multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
 | 2403 |             } | 
 | 2404 |         } else { | 
| Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2405 |             multiplier = floor(multiplier); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2406 |         } | 
 | 2407 |     } | 
 | 2408 |     mNormalFrameCount = multiplier * mFrameCount; | 
 | 2409 |     // round up to nearest 16 frames to satisfy AudioMixer | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2410 |     if (mType == MIXER || mType == DUPLICATING) { | 
 | 2411 |         mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
 | 2412 |     } | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2413 |     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] | 2414 |             mNormalFrameCount); | 
 | 2415 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2416 |     // Check if we want to throttle the processing to no more than 2x normal rate | 
 | 2417 |     mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2418 |     mThreadThrottleTimeMs = 0; | 
 | 2419 |     mThreadThrottleEndMs = 0; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2420 |     mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); | 
 | 2421 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2422 |     // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames. | 
 | 2423 |     // Originally this was int16_t[] array, need to remove legacy implications. | 
 | 2424 |     free(mSinkBuffer); | 
 | 2425 |     mSinkBuffer = NULL; | 
| Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2426 |     // For sink buffer size, we use the frame size from the downstream sink to avoid problems | 
 | 2427 |     // with non PCM formats for compressed music, e.g. AAC, and Offload threads. | 
 | 2428 |     const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2429 |     (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2430 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2431 |     // We resize the mMixerBuffer according to the requirements of the sink buffer which | 
 | 2432 |     // drives the output. | 
 | 2433 |     free(mMixerBuffer); | 
 | 2434 |     mMixerBuffer = NULL; | 
 | 2435 |     if (mMixerBufferEnabled) { | 
 | 2436 |         mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. | 
 | 2437 |         mMixerBufferSize = mNormalFrameCount * mChannelCount | 
 | 2438 |                 * audio_bytes_per_sample(mMixerBufferFormat); | 
 | 2439 |         (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); | 
 | 2440 |     } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2441 |     free(mEffectBuffer); | 
 | 2442 |     mEffectBuffer = NULL; | 
 | 2443 |     if (mEffectBufferEnabled) { | 
 | 2444 |         mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only | 
 | 2445 |         mEffectBufferSize = mNormalFrameCount * mChannelCount | 
 | 2446 |                 * audio_bytes_per_sample(mEffectBufferFormat); | 
 | 2447 |         (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); | 
 | 2448 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2449 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2450 |     // force reconfiguration of effect chains and engines to take new buffer size and audio | 
 | 2451 |     // parameters into account | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2452 |     // 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] | 2453 |     // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
 | 2454 |     // matter. | 
 | 2455 |     // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
 | 2456 |     Vector< sp<EffectChain> > effectChains = mEffectChains; | 
 | 2457 |     for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2458 |         mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
 | 2459 |     } | 
 | 2460 | } | 
 | 2461 |  | 
 | 2462 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2463 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2464 | { | 
 | 2465 |     if (halFrames == NULL || dspFrames == NULL) { | 
 | 2466 |         return BAD_VALUE; | 
 | 2467 |     } | 
 | 2468 |     Mutex::Autolock _l(mLock); | 
 | 2469 |     if (initCheck() != NO_ERROR) { | 
 | 2470 |         return INVALID_OPERATION; | 
 | 2471 |     } | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2472 |     int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2473 |     *halFrames = framesWritten; | 
 | 2474 |  | 
 | 2475 |     if (isSuspended()) { | 
 | 2476 |         // return an estimation of rendered frames when the output is suspended | 
 | 2477 |         size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2478 |         *dspFrames = (uint32_t) | 
 | 2479 |                 (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2480 |         return NO_ERROR; | 
 | 2481 |     } else { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2482 |         status_t status; | 
 | 2483 |         uint32_t frames; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2484 |         status = mOutput->getRenderPosition(&frames); | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2485 |         *dspFrames = (size_t)frames; | 
 | 2486 |         return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2487 |     } | 
 | 2488 | } | 
 | 2489 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2490 | // hasAudioSession_l() must be called with ThreadBase::mLock held | 
 | 2491 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2492 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2493 |     uint32_t result = 0; | 
 | 2494 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 2495 |         result = EFFECT_SESSION; | 
 | 2496 |     } | 
 | 2497 |  | 
 | 2498 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2499 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2500 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2501 |             result |= TRACK_SESSION; | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2502 |             if (track->isFastTrack()) { | 
 | 2503 |                 result |= FAST_SESSION; | 
 | 2504 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2505 |             break; | 
 | 2506 |         } | 
 | 2507 |     } | 
 | 2508 |  | 
 | 2509 |     return result; | 
 | 2510 | } | 
 | 2511 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2512 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2513 | { | 
 | 2514 |     // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
 | 2515 |     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
 | 2516 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 2517 |         return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 2518 |     } | 
 | 2519 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 2520 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2521 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2522 |             return AudioSystem::getStrategyForStream(track->streamType()); | 
 | 2523 |         } | 
 | 2524 |     } | 
 | 2525 |     return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 2526 | } | 
 | 2527 |  | 
 | 2528 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2529 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2530 | { | 
 | 2531 |     Mutex::Autolock _l(mLock); | 
 | 2532 |     return mOutput; | 
 | 2533 | } | 
 | 2534 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2535 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2536 | { | 
 | 2537 |     Mutex::Autolock _l(mLock); | 
 | 2538 |     AudioStreamOut *output = mOutput; | 
 | 2539 |     mOutput = NULL; | 
 | 2540 |     // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
 | 2541 |     //       must push a NULL and wait for ack | 
 | 2542 |     mOutputSink.clear(); | 
 | 2543 |     mPipeSink.clear(); | 
 | 2544 |     mNormalSink.clear(); | 
 | 2545 |     return output; | 
 | 2546 | } | 
 | 2547 |  | 
 | 2548 | // 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] | 2549 | sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2550 | { | 
 | 2551 |     if (mOutput == NULL) { | 
 | 2552 |         return NULL; | 
 | 2553 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2554 |     return mOutput->stream; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2555 | } | 
 | 2556 |  | 
 | 2557 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const | 
 | 2558 | { | 
 | 2559 |     return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 2560 | } | 
 | 2561 |  | 
 | 2562 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
 | 2563 | { | 
 | 2564 |     if (!isValidSyncEvent(event)) { | 
 | 2565 |         return BAD_VALUE; | 
 | 2566 |     } | 
 | 2567 |  | 
 | 2568 |     Mutex::Autolock _l(mLock); | 
 | 2569 |  | 
 | 2570 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2571 |         sp<Track> track = mTracks[i]; | 
 | 2572 |         if (event->triggerSession() == track->sessionId()) { | 
 | 2573 |             (void) track->setSyncEvent(event); | 
 | 2574 |             return NO_ERROR; | 
 | 2575 |         } | 
 | 2576 |     } | 
 | 2577 |  | 
 | 2578 |     return NAME_NOT_FOUND; | 
 | 2579 | } | 
 | 2580 |  | 
 | 2581 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
 | 2582 | { | 
 | 2583 |     return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
 | 2584 | } | 
 | 2585 |  | 
 | 2586 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( | 
 | 2587 |         const Vector< sp<Track> >& tracksToRemove) | 
 | 2588 | { | 
 | 2589 |     size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2590 |     if (count > 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2591 |         for (size_t i = 0 ; i < count ; i++) { | 
 | 2592 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2593 |             if (track->isExternalTrack()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2594 |                 AudioSystem::stopOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2595 |                                         track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2596 | #ifdef ADD_BATTERY_DATA | 
 | 2597 |                 // to track the speaker usage | 
 | 2598 |                 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
 | 2599 | #endif | 
 | 2600 |                 if (track->isTerminated()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2601 |                     AudioSystem::releaseOutput(mId, track->streamType(), | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2602 |                                                track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2603 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2604 |             } | 
 | 2605 |         } | 
 | 2606 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2607 | } | 
 | 2608 |  | 
 | 2609 | void AudioFlinger::PlaybackThread::checkSilentMode_l() | 
 | 2610 | { | 
 | 2611 |     if (!mMasterMute) { | 
 | 2612 |         char value[PROPERTY_VALUE_MAX]; | 
| Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2613 |         if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { | 
 | 2614 |             ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); | 
 | 2615 |             return; | 
 | 2616 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2617 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 2618 |             char *endptr; | 
 | 2619 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 2620 |             if (*endptr == '\0' && ul != 0) { | 
 | 2621 |                 ALOGD("Silence is golden"); | 
 | 2622 |                 // The setprop command will not allow a property to be changed after | 
 | 2623 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 2624 |                 setMasterMute_l(true); | 
 | 2625 |             } | 
 | 2626 |         } | 
 | 2627 |     } | 
 | 2628 | } | 
 | 2629 |  | 
 | 2630 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2631 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2632 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2633 |     mInWrite = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2634 |     ssize_t bytesWritten; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2635 |     const size_t offset = mCurrentWriteLength - mBytesRemaining; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2636 |  | 
 | 2637 |     // If an NBAIO sink is present, use it to write the normal mixer's submix | 
 | 2638 |     if (mNormalSink != 0) { | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2639 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2640 |         const size_t count = mBytesRemaining / mFrameSize; | 
 | 2641 |  | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2642 |         ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2643 |         // update the setpoint when AudioFlinger::mScreenState changes | 
 | 2644 |         uint32_t screenState = AudioFlinger::mScreenState; | 
 | 2645 |         if (screenState != mScreenState) { | 
 | 2646 |             mScreenState = screenState; | 
 | 2647 |             MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 2648 |             if (pipe != NULL) { | 
 | 2649 |                 pipe->setAvgFrames((mScreenState & 1) ? | 
 | 2650 |                         (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 2651 |             } | 
 | 2652 |         } | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2653 |         ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2654 |         ATRACE_END(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2655 |         if (framesWritten > 0) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2656 |             bytesWritten = framesWritten * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2657 |         } else { | 
 | 2658 |             bytesWritten = framesWritten; | 
 | 2659 |         } | 
 | 2660 |     // otherwise use the HAL / AudioStreamOut directly | 
 | 2661 |     } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2662 |         // Direct output and offload threads | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2663 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2664 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2665 |             ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); | 
 | 2666 |             mWriteAckSequence += 2; | 
 | 2667 |             mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2668 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2669 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2670 |         } | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2671 |         // FIXME We should have an implementation of timestamps for direct output threads. | 
 | 2672 |         // They are used e.g for multichannel PCM playback over HDMI. | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2673 |         bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2674 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2675 |         if (mUseAsyncWrite && | 
 | 2676 |                 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { | 
 | 2677 |             // 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] | 2678 |             mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2679 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2680 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2681 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2682 |     } | 
 | 2683 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2684 |     mNumWrites++; | 
 | 2685 |     mInWrite = false; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2686 |     mStandby = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2687 |     return bytesWritten; | 
 | 2688 | } | 
 | 2689 |  | 
 | 2690 | void AudioFlinger::PlaybackThread::threadLoop_drain() | 
 | 2691 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2692 |     bool supportsDrain = false; | 
 | 2693 |     if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2694 |         ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); | 
 | 2695 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2696 |             ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); | 
 | 2697 |             mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2698 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2699 |             mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2700 |         } | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 2701 |         status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2702 |         ALOGE_IF(result != OK, "Error when draining stream: %d", result); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2703 |     } | 
 | 2704 | } | 
 | 2705 |  | 
 | 2706 | void AudioFlinger::PlaybackThread::threadLoop_exit() | 
 | 2707 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2708 |     { | 
 | 2709 |         Mutex::Autolock _l(mLock); | 
 | 2710 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 2711 |             sp<Track> track = mTracks[i]; | 
 | 2712 |             track->invalidate(); | 
 | 2713 |         } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2714 |         // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. | 
 | 2715 |         // After we exit there are no more track changes sent to BatteryNotifier | 
 | 2716 |         // because that requires an active threadLoop. | 
 | 2717 |         // TODO: should we decActiveTrackCnt() of the cleared track effect chain? | 
 | 2718 |         mActiveTracks.clear(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2719 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2720 | } | 
 | 2721 |  | 
 | 2722 | /* | 
 | 2723 | The derived values that are cached: | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2724 |  - mSinkBufferSize from frame count * frame size | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2725 |  - mActiveSleepTimeUs from activeSleepTimeUs() | 
 | 2726 |  - mIdleSleepTimeUs from idleSleepTimeUs() | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2727 |  - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least | 
 | 2728 |    kDefaultStandbyTimeInNsecs when connected to an A2DP device. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2729 |  - maxPeriod from frame count and sample rate (MIXER only) | 
 | 2730 |  | 
 | 2731 | The parameters that affect these derived values are: | 
 | 2732 |  - frame count | 
 | 2733 |  - frame size | 
 | 2734 |  - sample rate | 
 | 2735 |  - device type: A2DP or not | 
 | 2736 |  - device latency | 
 | 2737 |  - format: PCM or not | 
 | 2738 |  - active sleep time | 
 | 2739 |  - idle sleep time | 
 | 2740 | */ | 
 | 2741 |  | 
 | 2742 | void AudioFlinger::PlaybackThread::cacheParameters_l() | 
 | 2743 | { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2744 |     mSinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2745 |     mActiveSleepTimeUs = activeSleepTimeUs(); | 
 | 2746 |     mIdleSleepTimeUs = idleSleepTimeUs(); | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2747 |  | 
 | 2748 |     // make sure standby delay is not too short when connected to an A2DP sink to avoid | 
 | 2749 |     // truncating audio when going to standby. | 
 | 2750 |     mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; | 
 | 2751 |     if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { | 
 | 2752 |         if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { | 
 | 2753 |             mStandbyDelayNs = kDefaultStandbyTimeInNsecs; | 
 | 2754 |         } | 
 | 2755 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2756 | } | 
 | 2757 |  | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2758 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2759 | { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2760 |     ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2761 |             this,  streamType, mTracks.size()); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2762 |     bool trackMatch = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2763 |     size_t size = mTracks.size(); | 
 | 2764 |     for (size_t i = 0; i < size; i++) { | 
 | 2765 |         sp<Track> t = mTracks[i]; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2766 |         if (t->streamType() == streamType && t->isExternalTrack()) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2767 |             t->invalidate(); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2768 |             trackMatch = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2769 |         } | 
 | 2770 |     } | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2771 |     return trackMatch; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2772 | } | 
 | 2773 |  | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2774 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
 | 2775 | { | 
 | 2776 |     Mutex::Autolock _l(mLock); | 
 | 2777 |     invalidateTracks_l(streamType); | 
 | 2778 | } | 
 | 2779 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2780 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 2781 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2782 |     audio_session_t session = chain->sessionId(); | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2783 |     sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; | 
 | 2784 |     status_t result = EffectBufferHalInterface::mirror( | 
 | 2785 |             mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, | 
 | 2786 |             mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, | 
 | 2787 |             &halInBuffer); | 
 | 2788 |     if (result != OK) return result; | 
 | 2789 |     halOutBuffer = halInBuffer; | 
 | 2790 |     int16_t *buffer = reinterpret_cast<int16_t*>(halInBuffer->externalData()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2791 |  | 
 | 2792 |     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] | 2793 |     if (session > AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2794 |         // 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] | 2795 |         // the sink buffer as input | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2796 |         if (mType != DIRECT) { | 
 | 2797 |             size_t numSamples = mNormalFrameCount * mChannelCount; | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2798 |             status_t result = EffectBufferHalInterface::allocate( | 
 | 2799 |                     numSamples * sizeof(int16_t), | 
 | 2800 |                     &halInBuffer); | 
 | 2801 |             if (result != OK) return result; | 
 | 2802 |             buffer = halInBuffer->audioBuffer()->s16; | 
 | 2803 |             ALOGV("addEffectChain_l() creating new input buffer %p session %d", | 
 | 2804 |                     buffer, session); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2805 |         } | 
 | 2806 |  | 
 | 2807 |         // Attach all tracks with same session ID to this chain. | 
 | 2808 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2809 |             sp<Track> track = mTracks[i]; | 
 | 2810 |             if (session == track->sessionId()) { | 
 | 2811 |                 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), | 
 | 2812 |                         buffer); | 
 | 2813 |                 track->setMainBuffer(buffer); | 
 | 2814 |                 chain->incTrackCnt(); | 
 | 2815 |             } | 
 | 2816 |         } | 
 | 2817 |  | 
 | 2818 |         // indicate all active tracks in the chain | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2819 |         for (const sp<Track> &track : mActiveTracks) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2820 |             if (session == track->sessionId()) { | 
 | 2821 |                 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
 | 2822 |                 chain->incActiveTrackCnt(); | 
 | 2823 |             } | 
 | 2824 |         } | 
 | 2825 |     } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2826 |     chain->setThread(this); | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2827 |     chain->setInBuffer(halInBuffer); | 
 | 2828 |     chain->setOutBuffer(halOutBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2829 |     // 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] | 2830 |     // 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] | 2831 |     // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
 | 2832 |     // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2833 |     // after track specific effects and before output stage. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2834 |     // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2835 |     // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2836 |     // Effect chain for other sessions are inserted at beginning of effect | 
 | 2837 |     // 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] | 2838 |     // sessions is not important. | 
 | 2839 |     static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && | 
 | 2840 |             AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, | 
 | 2841 |             "audio_session_t constants misdefined"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2842 |     size_t size = mEffectChains.size(); | 
 | 2843 |     size_t i = 0; | 
 | 2844 |     for (i = 0; i < size; i++) { | 
 | 2845 |         if (mEffectChains[i]->sessionId() < session) { | 
 | 2846 |             break; | 
 | 2847 |         } | 
 | 2848 |     } | 
 | 2849 |     mEffectChains.insertAt(chain, i); | 
 | 2850 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 2851 |  | 
 | 2852 |     return NO_ERROR; | 
 | 2853 | } | 
 | 2854 |  | 
 | 2855 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 2856 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2857 |     audio_session_t session = chain->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2858 |  | 
 | 2859 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 2860 |  | 
 | 2861 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 2862 |         if (chain == mEffectChains[i]) { | 
 | 2863 |             mEffectChains.removeAt(i); | 
 | 2864 |             // detach all active tracks from the chain | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2865 |             for (const sp<Track> &track : mActiveTracks) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2866 |                 if (session == track->sessionId()) { | 
 | 2867 |                     ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
 | 2868 |                             chain.get(), session); | 
 | 2869 |                     chain->decActiveTrackCnt(); | 
 | 2870 |                 } | 
 | 2871 |             } | 
 | 2872 |  | 
 | 2873 |             // detach all tracks with same session ID from this chain | 
 | 2874 |             for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2875 |                 sp<Track> track = mTracks[i]; | 
 | 2876 |                 if (session == track->sessionId()) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2877 |                     track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2878 |                     chain->decTrackCnt(); | 
 | 2879 |                 } | 
 | 2880 |             } | 
 | 2881 |             break; | 
 | 2882 |         } | 
 | 2883 |     } | 
 | 2884 |     return mEffectChains.size(); | 
 | 2885 | } | 
 | 2886 |  | 
 | 2887 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2888 |         const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2889 | { | 
 | 2890 |     Mutex::Autolock _l(mLock); | 
 | 2891 |     return attachAuxEffect_l(track, EffectId); | 
 | 2892 | } | 
 | 2893 |  | 
 | 2894 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2895 |         const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2896 | { | 
 | 2897 |     status_t status = NO_ERROR; | 
 | 2898 |  | 
 | 2899 |     if (EffectId == 0) { | 
 | 2900 |         track->setAuxBuffer(0, NULL); | 
 | 2901 |     } else { | 
 | 2902 |         // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
 | 2903 |         sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
 | 2904 |         if (effect != 0) { | 
 | 2905 |             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 2906 |                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
 | 2907 |             } else { | 
 | 2908 |                 status = INVALID_OPERATION; | 
 | 2909 |             } | 
 | 2910 |         } else { | 
 | 2911 |             status = BAD_VALUE; | 
 | 2912 |         } | 
 | 2913 |     } | 
 | 2914 |     return status; | 
 | 2915 | } | 
 | 2916 |  | 
 | 2917 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
 | 2918 | { | 
 | 2919 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2920 |         sp<Track> track = mTracks[i]; | 
 | 2921 |         if (track->auxEffectId() == effectId) { | 
 | 2922 |             attachAuxEffect_l(track, 0); | 
 | 2923 |         } | 
 | 2924 |     } | 
 | 2925 | } | 
 | 2926 |  | 
 | 2927 | bool AudioFlinger::PlaybackThread::threadLoop() | 
 | 2928 | { | 
| Glenn Kasten | 388d571 | 2017-04-07 14:38:41 -0700 | [diff] [blame] | 2929 |     tlNBLogWriter = mNBLogWriter.get(); | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 2930 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2931 |     Vector< sp<Track> > tracksToRemove; | 
 | 2932 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2933 |     mStandbyTimeNs = systemTime(); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2934 |     nsecs_t lastWriteFinished = -1; // time last server write completed | 
 | 2935 |     int64_t lastFramesWritten = -1; // track changes in timestamp server frames written | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2936 |  | 
 | 2937 |     // MIXER | 
 | 2938 |     nsecs_t lastWarning = 0; | 
 | 2939 |  | 
 | 2940 |     // DUPLICATING | 
 | 2941 |     // FIXME could this be made local to while loop? | 
 | 2942 |     writeFrames = 0; | 
 | 2943 |  | 
 | 2944 |     cacheParameters_l(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2945 |     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2946 |  | 
 | 2947 |     if (mType == MIXER) { | 
 | 2948 |         sleepTimeShift = 0; | 
 | 2949 |     } | 
 | 2950 |  | 
 | 2951 |     CpuStats cpuStats; | 
 | 2952 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 2953 |  | 
 | 2954 |     acquireWakeLock(); | 
 | 2955 |  | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2956 |     // mNBLogWriter logging APIs can only be called by a single thread, typically the | 
 | 2957 |     // thread associated with this PlaybackThread. | 
 | 2958 |     // If you want to share the mNBLogWriter with other threads (for example, binder threads) | 
 | 2959 |     // 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] | 2960 |     // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
 | 2961 |     // and then that string will be logged at the next convenient opportunity. | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2962 |     // See reference to logString below. | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2963 |     const char *logString = NULL; | 
 | 2964 |  | 
| Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2965 |     checkSilentMode_l(); | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2966 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2967 |     while (!exitPending()) | 
 | 2968 |     { | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 2969 |         // Log merge requests are performed during AudioFlinger binder transactions, but | 
 | 2970 |         // that does not cover audio playback. It's requested here for that reason. | 
 | 2971 |         mAudioFlinger->requestLogMerge(); | 
 | 2972 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2973 |         cpuStats.sample(myName); | 
 | 2974 |  | 
 | 2975 |         Vector< sp<EffectChain> > effectChains; | 
 | 2976 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2977 |         { // scope for mLock | 
 | 2978 |  | 
 | 2979 |             Mutex::Autolock _l(mLock); | 
 | 2980 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2981 |             processConfigEvents_l(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2982 |  | 
| Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 2983 |             // 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] | 2984 |             if (logString != NULL) { | 
 | 2985 |                 mNBLogWriter->logTimestamp(); | 
 | 2986 |                 mNBLogWriter->log(logString); | 
 | 2987 |                 logString = NULL; | 
 | 2988 |             } | 
 | 2989 |  | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2990 |             // Gather the framesReleased counters for all active tracks, | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2991 |             // and associate with the sink frames written out.  We need | 
 | 2992 |             // this to convert the sink timestamp to the track timestamp. | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2993 |             bool kernelLocationUpdate = false; | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2994 |             if (mNormalSink != 0) { | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2995 |                 // Note: The DuplicatingThread may not have a mNormalSink. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2996 |                 // We always fetch the timestamp here because often the downstream | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2997 |                 // sink will block while writing. | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2998 |                 ExtendedTimestamp timestamp; // use private copy to fetch | 
 | 2999 |                 (void) mNormalSink->getTimestamp(timestamp); | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3000 |  | 
 | 3001 |                 // We keep track of the last valid kernel position in case we are in underrun | 
 | 3002 |                 // and the normal mixer period is the same as the fast mixer period, or there | 
 | 3003 |                 // is some error from the HAL. | 
 | 3004 |                 if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
 | 3005 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
 | 3006 |                             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 3007 |                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = | 
 | 3008 |                             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
 | 3009 |  | 
 | 3010 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
 | 3011 |                             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; | 
 | 3012 |                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = | 
 | 3013 |                             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3014 |                 } | 
 | 3015 |  | 
 | 3016 |                 if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { | 
 | 3017 |                     kernelLocationUpdate = true; | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3018 |                 } else { | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3019 |                     ALOGVV("getTimestamp error - no valid kernel position"); | 
| Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3020 |                 } | 
 | 3021 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3022 |                 // copy over kernel info | 
 | 3023 |                 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3024 |                         timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] | 
 | 3025 |                         + mSuspendedFrames; // add frames discarded when suspended | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3026 |                 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = | 
 | 3027 |                         timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3028 |             } | 
 | 3029 |             // mFramesWritten for non-offloaded tracks are contiguous | 
 | 3030 |             // even after standby() is called. This is useful for the track frame | 
 | 3031 |             // to sink frame mapping. | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3032 |             bool serverLocationUpdate = false; | 
 | 3033 |             if (mFramesWritten != lastFramesWritten) { | 
 | 3034 |                 serverLocationUpdate = true; | 
 | 3035 |                 lastFramesWritten = mFramesWritten; | 
 | 3036 |             } | 
 | 3037 |             // Only update timestamps if there is a meaningful change. | 
 | 3038 |             // Either the kernel timestamp must be valid or we have written something. | 
 | 3039 |             if (kernelLocationUpdate || serverLocationUpdate) { | 
 | 3040 |                 if (serverLocationUpdate) { | 
 | 3041 |                     // use the time before we called the HAL write - it is a bit more accurate | 
 | 3042 |                     // to when the server last read data than the current time here. | 
 | 3043 |                     // | 
 | 3044 |                     // If we haven't written anything, mLastWriteTime will be -1 | 
 | 3045 |                     // and we use systemTime(). | 
 | 3046 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; | 
 | 3047 |                     mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 | 
 | 3048 |                             ? systemTime() : mLastWriteTime; | 
 | 3049 |                 } | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3050 |  | 
 | 3051 |                 for (const sp<Track> &t : mActiveTracks) { | 
 | 3052 |                     if (!t->isFastTrack()) { | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3053 |                         t->updateTrackFrameInfo( | 
 | 3054 |                                 t->mAudioTrackServerProxy->framesReleased(), | 
 | 3055 |                                 mFramesWritten, | 
 | 3056 |                                 mTimestamp); | 
 | 3057 |                     } | 
| Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3058 |                 } | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3059 |             } | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3060 | #if 0 | 
 | 3061 |             // logFormat example | 
| Nicolas Roulet | c20cb50 | 2017-02-01 12:35:24 -0800 | [diff] [blame] | 3062 |             if (z % 100 == 0) { | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3063 |                 timespec ts; | 
 | 3064 |                 clock_gettime(CLOCK_MONOTONIC, &ts); | 
| Nicolas Roulet | 4da7820 | 2017-02-03 12:53:39 -0800 | [diff] [blame] | 3065 |                 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] | 3066 |                     "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] | 3067 |                 LOGT("A deceptive null-terminated string %\0"); | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3068 |             } | 
 | 3069 |             ++z; | 
 | 3070 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3071 |             saveOutputTracks(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3072 |             if (mSignalPending) { | 
 | 3073 |                 // A signal was raised while we were unlocked | 
 | 3074 |                 mSignalPending = false; | 
 | 3075 |             } else if (waitingAsyncCallback_l()) { | 
 | 3076 |                 if (exitPending()) { | 
 | 3077 |                     break; | 
 | 3078 |                 } | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3079 |                 bool released = false; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3080 |                 if (!keepWakeLock()) { | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3081 |                     releaseWakeLock_l(); | 
 | 3082 |                     released = true; | 
 | 3083 |                 } | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 3084 |  | 
 | 3085 |                 const int64_t waitNs = computeWaitTimeNs_l(); | 
 | 3086 |                 ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); | 
 | 3087 |                 status_t status = mWaitWorkCV.waitRelative(mLock, waitNs); | 
 | 3088 |                 if (status == TIMED_OUT) { | 
 | 3089 |                     mSignalPending = true; // if timeout recheck everything | 
 | 3090 |                 } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3091 |                 ALOGV("async completion/wake"); | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3092 |                 if (released) { | 
 | 3093 |                     acquireWakeLock_l(); | 
 | 3094 |                 } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3095 |                 mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 3096 |                 mSleepTimeUs = 0; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3097 |  | 
 | 3098 |                 continue; | 
 | 3099 |             } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3100 |             if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3101 |                                    isSuspended()) { | 
 | 3102 |                 // put audio hardware into standby after short delay | 
 | 3103 |                 if (shouldStandby_l()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3104 |  | 
 | 3105 |                     threadLoop_standby(); | 
 | 3106 |  | 
 | 3107 |                     mStandby = true; | 
 | 3108 |                 } | 
 | 3109 |  | 
 | 3110 |                 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
 | 3111 |                     // we're about to wait, flush the binder command buffer | 
 | 3112 |                     IPCThreadState::self()->flushCommands(); | 
 | 3113 |  | 
 | 3114 |                     clearOutputTracks(); | 
 | 3115 |  | 
 | 3116 |                     if (exitPending()) { | 
 | 3117 |                         break; | 
 | 3118 |                     } | 
 | 3119 |  | 
 | 3120 |                     releaseWakeLock_l(); | 
 | 3121 |                     // wait until we have something to do... | 
 | 3122 |                     ALOGV("%s going to sleep", myName.string()); | 
 | 3123 |                     mWaitWorkCV.wait(mLock); | 
 | 3124 |                     ALOGV("%s waking up", myName.string()); | 
 | 3125 |                     acquireWakeLock_l(); | 
 | 3126 |  | 
 | 3127 |                     mMixerStatus = MIXER_IDLE; | 
 | 3128 |                     mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
 | 3129 |                     mBytesWritten = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3130 |                     mBytesRemaining = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3131 |                     checkSilentMode_l(); | 
 | 3132 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3133 |                     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 3134 |                     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3135 |                     if (mType == MIXER) { | 
 | 3136 |                         sleepTimeShift = 0; | 
 | 3137 |                     } | 
 | 3138 |  | 
 | 3139 |                     continue; | 
 | 3140 |                 } | 
 | 3141 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3142 |             // mMixerStatusIgnoringFastTracks is also updated internally | 
 | 3143 |             mMixerStatus = prepareTracks_l(&tracksToRemove); | 
 | 3144 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3145 |             mActiveTracks.updatePowerState(this); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3146 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3147 |             // prevent any changes in effect chain list and in each effect chain | 
 | 3148 |             // during mixing and effect process as the audio buffers could be deleted | 
 | 3149 |             // or modified if an effect is created or deleted | 
 | 3150 |             lockEffectChains_l(effectChains); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3151 |         } // mLock scope ends | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3152 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3153 |         if (mBytesRemaining == 0) { | 
 | 3154 |             mCurrentWriteLength = 0; | 
 | 3155 |             if (mMixerStatus == MIXER_TRACKS_READY) { | 
 | 3156 |                 // threadLoop_mix() sets mCurrentWriteLength | 
 | 3157 |                 threadLoop_mix(); | 
 | 3158 |             } else if ((mMixerStatus != MIXER_DRAIN_TRACK) | 
 | 3159 |                         && (mMixerStatus != MIXER_DRAIN_ALL)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3160 |                 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3161 |                 // must be written to HAL | 
 | 3162 |                 threadLoop_sleepTime(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3163 |                 if (mSleepTimeUs == 0) { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3164 |                     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3165 |                 } | 
 | 3166 |             } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3167 |             // Either threadLoop_mix() or threadLoop_sleepTime() should have set | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3168 |             // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3169 |             // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) | 
 | 3170 |             // or mSinkBuffer (if there are no effects). | 
 | 3171 |             // | 
 | 3172 |             // This is done pre-effects computation; if effects change to | 
 | 3173 |             // support higher precision, this needs to move. | 
 | 3174 |             // | 
 | 3175 |             // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3176 |             // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3177 |             if (mMixerBufferValid) { | 
 | 3178 |                 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; | 
 | 3179 |                 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; | 
 | 3180 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3181 |                 // mono blend occurs for mixer threads only (not direct or offloaded) | 
 | 3182 |                 // and is handled here if we're going directly to the sink. | 
 | 3183 |                 if (requireMonoBlend() && !mEffectBufferValid) { | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3184 |                     mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, | 
 | 3185 |                                true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3186 |                 } | 
 | 3187 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3188 |                 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, | 
 | 3189 |                         mNormalFrameCount * mChannelCount); | 
 | 3190 |             } | 
 | 3191 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3192 |             mBytesRemaining = mCurrentWriteLength; | 
 | 3193 |             if (isSuspended()) { | 
| Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3194 |                 // Simulate write to HAL when suspended (e.g. BT SCO phone call). | 
 | 3195 |                 mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. | 
 | 3196 |                 const size_t framesRemaining = mBytesRemaining / mFrameSize; | 
 | 3197 |                 mBytesWritten += mBytesRemaining; | 
 | 3198 |                 mFramesWritten += framesRemaining; | 
 | 3199 |                 mSuspendedFrames += framesRemaining; // to adjust kernel HAL position | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3200 |                 mBytesRemaining = 0; | 
 | 3201 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3202 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3203 |             // only process effects if we're going to write | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3204 |             if (mSleepTimeUs == 0 && mType != OFFLOAD) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3205 |                 for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 3206 |                     effectChains[i]->process_l(); | 
 | 3207 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3208 |             } | 
 | 3209 |         } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3210 |         // Process effect chains for offloaded thread even if no audio | 
 | 3211 |         // was read from audio track: process only updates effect state | 
 | 3212 |         // and thus does have to be synchronized with audio writes but may have | 
 | 3213 |         // to be called while waiting for async write callback | 
 | 3214 |         if (mType == OFFLOAD) { | 
 | 3215 |             for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 3216 |                 effectChains[i]->process_l(); | 
 | 3217 |             } | 
 | 3218 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3219 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3220 |         // Only if the Effects buffer is enabled and there is data in the | 
 | 3221 |         // Effects buffer (buffer valid), we need to | 
 | 3222 |         // copy into the sink buffer. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3223 |         // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3224 |         if (mEffectBufferValid) { | 
 | 3225 |             //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3226 |  | 
 | 3227 |             if (requireMonoBlend()) { | 
| Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3228 |                 mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, | 
 | 3229 |                            true /*limit*/); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3230 |             } | 
 | 3231 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3232 |             memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, | 
 | 3233 |                     mNormalFrameCount * mChannelCount); | 
 | 3234 |         } | 
 | 3235 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3236 |         // enable changes in effect chain | 
 | 3237 |         unlockEffectChains(effectChains); | 
 | 3238 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3239 |         if (!waitingAsyncCallback()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3240 |             // mSleepTimeUs == 0 means we must write to audio hardware | 
 | 3241 |             if (mSleepTimeUs == 0) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3242 |                 ssize_t ret = 0; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3243 |                 // We save lastWriteFinished here, as previousLastWriteFinished, | 
 | 3244 |                 // for throttling. On thread start, previousLastWriteFinished will be | 
 | 3245 |                 // set to -1, which properly results in no throttling after the first write. | 
 | 3246 |                 nsecs_t previousLastWriteFinished = lastWriteFinished; | 
 | 3247 |                 nsecs_t delta = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3248 |                 if (mBytesRemaining) { | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3249 |                     // FIXME rewrite to reduce number of system calls | 
 | 3250 |                     mLastWriteTime = systemTime();  // also used for dumpsys | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3251 |                     ret = threadLoop_write(); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3252 |                     lastWriteFinished = systemTime(); | 
 | 3253 |                     delta = lastWriteFinished - mLastWriteTime; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3254 |                     if (ret < 0) { | 
 | 3255 |                         mBytesRemaining = 0; | 
 | 3256 |                     } else { | 
 | 3257 |                         mBytesWritten += ret; | 
 | 3258 |                         mBytesRemaining -= ret; | 
| Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3259 |                         mFramesWritten += ret / mFrameSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3260 |                     } | 
 | 3261 |                 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || | 
 | 3262 |                         (mMixerStatus == MIXER_DRAIN_ALL)) { | 
 | 3263 |                     threadLoop_drain(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3264 |                 } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3265 |                 if (mType == MIXER && !mStandby) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3266 |                     // write blocked detection | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3267 |                     if (delta > maxPeriod) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3268 |                         mNumDelayedWrites++; | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3269 |                         if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3270 |                             ATRACE_NAME("underrun"); | 
 | 3271 |                             ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3272 |                                     (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3273 |                             lastWarning = lastWriteFinished; | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3274 |                         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3275 |                     } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3276 |  | 
 | 3277 |                     if (mThreadThrottle | 
 | 3278 |                             && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) | 
 | 3279 |                             && ret > 0) {                         // we wrote something | 
 | 3280 |                         // Limit MixerThread data processing to no more than twice the | 
 | 3281 |                         // expected processing rate. | 
 | 3282 |                         // | 
 | 3283 |                         // This helps prevent underruns with NuPlayer and other applications | 
 | 3284 |                         // which may set up buffers that are close to the minimum size, or use | 
 | 3285 |                         // deep buffers, and rely on a double-buffering sleep strategy to fill. | 
 | 3286 |                         // | 
 | 3287 |                         // The throttle smooths out sudden large data drains from the device, | 
 | 3288 |                         // e.g. when it comes out of standby, which often causes problems with | 
 | 3289 |                         // (1) mixer threads without a fast mixer (which has its own warm-up) | 
 | 3290 |                         // (2) minimum buffer sized tracks (even if the track is full, | 
 | 3291 |                         //     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] | 3292 |                         // | 
 | 3293 |                         // Total time spent in last processing cycle equals time spent in | 
 | 3294 |                         // 1. threadLoop_write, as well as time spent in | 
 | 3295 |                         // 2. threadLoop_mix (significant for heavy mixing, especially | 
 | 3296 |                         //                    on low tier processors) | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3297 |  | 
| Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3298 |                         // it's OK if deltaMs is an overestimate. | 
 | 3299 |                         const int32_t deltaMs = | 
 | 3300 |                                 (lastWriteFinished - previousLastWriteFinished) / 1000000; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3301 |                         const int32_t throttleMs = mHalfBufferMs - deltaMs; | 
 | 3302 |                         if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { | 
 | 3303 |                             usleep(throttleMs * 1000); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3304 |                             // notify of throttle start on verbose log | 
 | 3305 |                             ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, | 
 | 3306 |                                     "mixer(%p) throttle begin:" | 
 | 3307 |                                     " ret(%zd) deltaMs(%d) requires sleep %d ms", | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3308 |                                     this, ret, deltaMs, throttleMs); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3309 |                             mThreadThrottleTimeMs += throttleMs; | 
| Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3310 |                             // Throttle must be attributed to the previous mixer loop's write time | 
 | 3311 |                             // to allow back-to-back throttling. | 
 | 3312 |                             lastWriteFinished += throttleMs * 1000000; | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3313 |                         } else { | 
 | 3314 |                             uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; | 
 | 3315 |                             if (diff > 0) { | 
 | 3316 |                                 // notify of throttle end on debug log | 
| Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3317 |                                 // but prevent spamming for bluetooth | 
 | 3318 |                                 ALOGD_IF(!audio_is_a2dp_out_device(outDevice()), | 
 | 3319 |                                         "mixer(%p) throttle end: throttle time(%u)", this, diff); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3320 |                                 mThreadThrottleEndMs = mThreadThrottleTimeMs; | 
 | 3321 |                             } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3322 |                         } | 
 | 3323 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3324 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3325 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3326 |             } else { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3327 |                 ATRACE_BEGIN("sleep"); | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3328 |                 Mutex::Autolock _l(mLock); | 
 | 3329 |                 if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { | 
 | 3330 |                     mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3331 |                 } | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3332 |                 ATRACE_END(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3333 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3334 |         } | 
 | 3335 |  | 
 | 3336 |         // Finally let go of removed track(s), without the lock held | 
 | 3337 |         // since we can't guarantee the destructors won't acquire that | 
 | 3338 |         // same lock.  This will also mutate and push a new fast mixer state. | 
 | 3339 |         threadLoop_removeTracks(tracksToRemove); | 
 | 3340 |         tracksToRemove.clear(); | 
 | 3341 |  | 
 | 3342 |         // FIXME I don't understand the need for this here; | 
 | 3343 |         //       it was in the original code but maybe the | 
 | 3344 |         //       assignment in saveOutputTracks() makes this unnecessary? | 
 | 3345 |         clearOutputTracks(); | 
 | 3346 |  | 
 | 3347 |         // Effect chains will be actually deleted here if they were removed from | 
 | 3348 |         // mEffectChains list during mixing or effects processing | 
 | 3349 |         effectChains.clear(); | 
 | 3350 |  | 
 | 3351 |         // FIXME Note that the above .clear() is no longer necessary since effectChains | 
 | 3352 |         // is now local to this block, but will keep it for now (at least until merge done). | 
 | 3353 |     } | 
 | 3354 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3355 |     threadLoop_exit(); | 
 | 3356 |  | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3357 |     if (!mStandby) { | 
 | 3358 |         threadLoop_standby(); | 
 | 3359 |         mStandby = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3360 |     } | 
 | 3361 |  | 
 | 3362 |     releaseWakeLock(); | 
 | 3363 |  | 
 | 3364 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 3365 |     return false; | 
 | 3366 | } | 
 | 3367 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3368 | // removeTracks_l() must be called with ThreadBase::mLock held | 
 | 3369 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) | 
 | 3370 | { | 
 | 3371 |     size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3372 |     if (count > 0) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3373 |         for (size_t i=0 ; i<count ; i++) { | 
 | 3374 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
 | 3375 |             mActiveTracks.remove(track); | 
 | 3376 |             ALOGV("removeTracks_l removing track on session %d", track->sessionId()); | 
 | 3377 |             sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 3378 |             if (chain != 0) { | 
 | 3379 |                 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), | 
 | 3380 |                         track->sessionId()); | 
 | 3381 |                 chain->decActiveTrackCnt(); | 
 | 3382 |             } | 
 | 3383 |             if (track->isTerminated()) { | 
 | 3384 |                 removeTrack_l(track); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3385 |             } else { // inactive but not terminated | 
 | 3386 |                 char buffer[256]; | 
| Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 3387 |                 track->dump(buffer, arraysize(buffer), false /* active */); | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3388 |                 mLocalLog.log("removeTracks_l(%p) %s", track.get(), buffer + 4); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3389 |             } | 
 | 3390 |         } | 
 | 3391 |     } | 
 | 3392 |  | 
 | 3393 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3394 |  | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3395 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) | 
 | 3396 | { | 
 | 3397 |     if (mNormalSink != 0) { | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3398 |         ExtendedTimestamp ets; | 
 | 3399 |         status_t status = mNormalSink->getTimestamp(ets); | 
 | 3400 |         if (status == NO_ERROR) { | 
 | 3401 |             status = ets.getBestTimestamp(×tamp); | 
 | 3402 |         } | 
 | 3403 |         return status; | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3404 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3405 |     if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3406 |         uint64_t position64; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3407 |         if (mOutput->getPresentationPosition(&position64, ×tamp.mTime) == OK) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3408 |             timestamp.mPosition = (uint32_t)position64; | 
 | 3409 |             return NO_ERROR; | 
 | 3410 |         } | 
 | 3411 |     } | 
 | 3412 |     return INVALID_OPERATION; | 
 | 3413 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3414 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3415 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 3416 |                                                           audio_patch_handle_t *handle) | 
 | 3417 | { | 
| Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3418 |     status_t status; | 
 | 3419 |     if (property_get_bool("af.patch_park", false /* default_value */)) { | 
 | 3420 |         // Park FastMixer to avoid potential DOS issues with writing to the HAL | 
 | 3421 |         // or if HAL does not properly lock against access. | 
 | 3422 |         AutoPark<FastMixer> park(mFastMixer); | 
 | 3423 |         status = PlaybackThread::createAudioPatch_l(patch, handle); | 
 | 3424 |     } else { | 
 | 3425 |         status = PlaybackThread::createAudioPatch_l(patch, handle); | 
 | 3426 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3427 |     return status; | 
 | 3428 | } | 
 | 3429 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3430 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 3431 |                                                           audio_patch_handle_t *handle) | 
 | 3432 | { | 
 | 3433 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3434 |  | 
 | 3435 |     // store new device and send to effects | 
 | 3436 |     audio_devices_t type = AUDIO_DEVICE_NONE; | 
 | 3437 |     for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
 | 3438 |         type |= patch->sinks[i].ext.device.type; | 
 | 3439 |     } | 
 | 3440 |  | 
 | 3441 | #ifdef ADD_BATTERY_DATA | 
 | 3442 |     // when changing the audio output device, call addBatteryData to notify | 
 | 3443 |     // the change | 
 | 3444 |     if (mOutDevice != type) { | 
 | 3445 |         uint32_t params = 0; | 
 | 3446 |         // check whether speaker is on | 
 | 3447 |         if (type & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 3448 |             params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3449 |         } | 
 | 3450 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3451 |         audio_devices_t deviceWithoutSpeaker | 
 | 3452 |             = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 3453 |         // check if any other device (except speaker) is on | 
 | 3454 |         if (type & deviceWithoutSpeaker) { | 
 | 3455 |             params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 3456 |         } | 
 | 3457 |  | 
 | 3458 |         if (params != 0) { | 
 | 3459 |             addBatteryData(params); | 
 | 3460 |         } | 
 | 3461 |     } | 
 | 3462 | #endif | 
 | 3463 |  | 
 | 3464 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 3465 |         mEffectChains[i]->setDevice_l(type); | 
 | 3466 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3467 |  | 
 | 3468 |     // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when | 
 | 3469 |     // the thread is created so that the first patch creation triggers an ioConfigChanged callback | 
 | 3470 |     bool configChanged = mPrevOutDevice != type; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3471 |     mOutDevice = type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3472 |     mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3473 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3474 |     if (mOutput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3475 |         sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 3476 |         status = hwDevice->createAudioPatch(patch->num_sources, | 
 | 3477 |                                             patch->sources, | 
 | 3478 |                                             patch->num_sinks, | 
 | 3479 |                                             patch->sinks, | 
 | 3480 |                                             handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3481 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3482 |         char *address; | 
 | 3483 |         if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
 | 3484 |             //FIXME: we only support address on first sink with HAL version < 3.0 | 
 | 3485 |             address = audio_device_address_to_parameter( | 
 | 3486 |                                                         patch->sinks[0].ext.device.type, | 
 | 3487 |                                                         patch->sinks[0].ext.device.address); | 
 | 3488 |         } else { | 
 | 3489 |             address = (char *)calloc(1, 1); | 
 | 3490 |         } | 
 | 3491 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 3492 |         free(address); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3493 |         param.addInt(String8(AudioParameter::keyRouting), (int)type); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3494 |         status = mOutput->stream->setParameters(param.toString()); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3495 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3496 |     } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3497 |     if (configChanged) { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3498 |         mPrevOutDevice = type; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3499 |         sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
 | 3500 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3501 |     return status; | 
 | 3502 | } | 
 | 3503 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3504 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 3505 | { | 
| Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3506 |     status_t status; | 
 | 3507 |     if (property_get_bool("af.patch_park", false /* default_value */)) { | 
 | 3508 |         // Park FastMixer to avoid potential DOS issues with writing to the HAL | 
 | 3509 |         // or if HAL does not properly lock against access. | 
 | 3510 |         AutoPark<FastMixer> park(mFastMixer); | 
 | 3511 |         status = PlaybackThread::releaseAudioPatch_l(handle); | 
 | 3512 |     } else { | 
 | 3513 |         status = PlaybackThread::releaseAudioPatch_l(handle); | 
 | 3514 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3515 |     return status; | 
 | 3516 | } | 
 | 3517 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3518 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 3519 | { | 
 | 3520 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3521 |  | 
 | 3522 |     mOutDevice = AUDIO_DEVICE_NONE; | 
 | 3523 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3524 |     if (mOutput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 3525 |         sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 3526 |         status = hwDevice->releaseAudioPatch(handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3527 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3528 |         AudioParameter param; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 3529 |         param.addInt(String8(AudioParameter::keyRouting), 0); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3530 |         status = mOutput->stream->setParameters(param.toString()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3531 |     } | 
 | 3532 |     return status; | 
 | 3533 | } | 
 | 3534 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3535 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) | 
 | 3536 | { | 
 | 3537 |     Mutex::Autolock _l(mLock); | 
 | 3538 |     mTracks.add(track); | 
 | 3539 | } | 
 | 3540 |  | 
 | 3541 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) | 
 | 3542 | { | 
 | 3543 |     Mutex::Autolock _l(mLock); | 
 | 3544 |     destroyTrack_l(track); | 
 | 3545 | } | 
 | 3546 |  | 
 | 3547 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 3548 | { | 
 | 3549 |     ThreadBase::getAudioPortConfig(config); | 
 | 3550 |     config->role = AUDIO_PORT_ROLE_SOURCE; | 
 | 3551 |     config->ext.mix.hw_module = mOutput->audioHwDev->handle(); | 
 | 3552 |     config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
 | 3553 | } | 
 | 3554 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3555 | // ---------------------------------------------------------------------------- | 
 | 3556 |  | 
 | 3557 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3558 |         audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) | 
 | 3559 |     :   PlaybackThread(audioFlinger, output, id, device, type, systemReady), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3560 |         // mAudioMixer below | 
 | 3561 |         // mFastMixer below | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3562 |         mFastMixerFutex(0), | 
 | 3563 |         mMasterMono(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3564 |         // mOutputSink below | 
 | 3565 |         // mPipeSink below | 
 | 3566 |         // mNormalSink below | 
 | 3567 | { | 
 | 3568 |     ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3569 |     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%zu, " | 
 | 3570 |             "mFrameCount=%zu, mNormalFrameCount=%zu", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3571 |             mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
 | 3572 |             mNormalFrameCount); | 
 | 3573 |     mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 3574 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3575 |     if (type == DUPLICATING) { | 
 | 3576 |         // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks | 
 | 3577 |         // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). | 
 | 3578 |         // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. | 
 | 3579 |         return; | 
 | 3580 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3581 |     // create an NBAIO sink for the HAL output stream, and negotiate | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 3582 |     mOutputSink = new AudioStreamOutSink(output->stream); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3583 |     size_t numCounterOffers = 0; | 
| Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3584 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3585 | #if !LOG_NDEBUG | 
 | 3586 |     ssize_t index = | 
 | 3587 | #else | 
 | 3588 |     (void) | 
 | 3589 | #endif | 
 | 3590 |             mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3591 |     ALOG_ASSERT(index == 0); | 
 | 3592 |  | 
 | 3593 |     // initialize fast mixer depending on configuration | 
 | 3594 |     bool initFastMixer; | 
 | 3595 |     switch (kUseFastMixer) { | 
 | 3596 |     case FastMixer_Never: | 
 | 3597 |         initFastMixer = false; | 
 | 3598 |         break; | 
 | 3599 |     case FastMixer_Always: | 
 | 3600 |         initFastMixer = true; | 
 | 3601 |         break; | 
 | 3602 |     case FastMixer_Static: | 
 | 3603 |     case FastMixer_Dynamic: | 
| Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3604 |         // FastMixer was designed to operate with a HAL that pulls at a regular rate, | 
 | 3605 |         // where the period is less than an experimentally determined threshold that can be | 
 | 3606 |         // scheduled reliably with CFS. However, the BT A2DP HAL is | 
 | 3607 |         // bursty (does not pull at a regular rate) and so cannot operate with FastMixer. | 
 | 3608 |         initFastMixer = mFrameCount < mNormalFrameCount | 
 | 3609 |                 && (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) == 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3610 |         break; | 
 | 3611 |     } | 
| Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 3612 |     ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, | 
 | 3613 |             "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", | 
 | 3614 |             mFrameCount, mNormalFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3615 |     if (initFastMixer) { | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3616 |         audio_format_t fastMixerFormat; | 
 | 3617 |         if (mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 3618 |             fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 3619 |         } else { | 
 | 3620 |             fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; | 
 | 3621 |         } | 
 | 3622 |         if (mFormat != fastMixerFormat) { | 
 | 3623 |             // change our Sink format to accept our intermediate precision | 
 | 3624 |             mFormat = fastMixerFormat; | 
 | 3625 |             free(mSinkBuffer); | 
 | 3626 |             mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 3627 |             const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
 | 3628 |             (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
 | 3629 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3630 |  | 
 | 3631 |         // create a MonoPipe to connect our submix to FastMixer | 
 | 3632 |         NBAIO_Format format = mOutputSink->format(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3633 | #ifdef TEE_SINK | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3634 |         NBAIO_Format origformat = format; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3635 | #endif | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3636 |         // adjust format to match that of the Fast Mixer | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3637 |         ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3638 |         format.mFormat = fastMixerFormat; | 
 | 3639 |         format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; | 
 | 3640 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3641 |         // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
 | 3642 |         // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
 | 3643 |         // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
 | 3644 |         MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
 | 3645 |         const NBAIO_Format offers[1] = {format}; | 
 | 3646 |         size_t numCounterOffers = 0; | 
| Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3647 | #if !LOG_NDEBUG || defined(TEE_SINK) | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3648 |         ssize_t index = | 
 | 3649 | #else | 
 | 3650 |         (void) | 
 | 3651 | #endif | 
 | 3652 |                 monoPipe->negotiate(offers, 1, NULL, numCounterOffers); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3653 |         ALOG_ASSERT(index == 0); | 
 | 3654 |         monoPipe->setAvgFrames((mScreenState & 1) ? | 
 | 3655 |                 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 3656 |         mPipeSink = monoPipe; | 
 | 3657 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3658 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3659 |         if (mTeeSinkOutputEnabled) { | 
 | 3660 |             // 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] | 3661 |             Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); | 
 | 3662 |             const NBAIO_Format offers2[1] = {origformat}; | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3663 |             numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3664 |             index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3665 |             ALOG_ASSERT(index == 0); | 
 | 3666 |             mTeeSink = teeSink; | 
 | 3667 |             PipeReader *teeSource = new PipeReader(*teeSink); | 
 | 3668 |             numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3669 |             index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3670 |             ALOG_ASSERT(index == 0); | 
 | 3671 |             mTeeSource = teeSource; | 
 | 3672 |         } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3673 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3674 |  | 
 | 3675 |         // create fast mixer and configure it initially with just one fast track for our submix | 
 | 3676 |         mFastMixer = new FastMixer(); | 
 | 3677 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3678 | #ifdef STATE_QUEUE_DUMP | 
 | 3679 |         sq->setObserverDump(&mStateQueueObserverDump); | 
 | 3680 |         sq->setMutatorDump(&mStateQueueMutatorDump); | 
 | 3681 | #endif | 
 | 3682 |         FastMixerState *state = sq->begin(); | 
 | 3683 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 3684 |         // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
 | 3685 |         fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
 | 3686 |         fastTrack->mVolumeProvider = NULL; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3687 |         fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer | 
 | 3688 |         fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3689 |         fastTrack->mGeneration++; | 
 | 3690 |         state->mFastTracksGen++; | 
 | 3691 |         state->mTrackMask = 1; | 
 | 3692 |         // fast mixer will use the HAL output sink | 
 | 3693 |         state->mOutputSink = mOutputSink.get(); | 
 | 3694 |         state->mOutputSinkGen++; | 
 | 3695 |         state->mFrameCount = mFrameCount; | 
 | 3696 |         state->mCommand = FastMixerState::COLD_IDLE; | 
 | 3697 |         // already done in constructor initialization list | 
 | 3698 |         //mFastMixerFutex = 0; | 
 | 3699 |         state->mColdFutexAddr = &mFastMixerFutex; | 
 | 3700 |         state->mColdGen++; | 
 | 3701 |         state->mDumpState = &mFastMixerDumpState; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3702 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3703 |         state->mTeeSink = mTeeSink.get(); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3704 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3705 |         mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
 | 3706 |         state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3707 |         sq->end(); | 
 | 3708 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3709 |  | 
 | 3710 |         // start the fast mixer | 
 | 3711 |         mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
 | 3712 |         pid_t tid = mFastMixer->getTid(); | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 3713 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer, false); | 
| Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 3714 |         stream()->setHalThreadPriority(kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3715 |  | 
 | 3716 | #ifdef AUDIO_WATCHDOG | 
 | 3717 |         // create and start the watchdog | 
 | 3718 |         mAudioWatchdog = new AudioWatchdog(); | 
 | 3719 |         mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
 | 3720 |         mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
 | 3721 |         tid = mAudioWatchdog->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3722 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3723 | #endif | 
 | 3724 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3725 |     } | 
 | 3726 |  | 
 | 3727 |     switch (kUseFastMixer) { | 
 | 3728 |     case FastMixer_Never: | 
 | 3729 |     case FastMixer_Dynamic: | 
 | 3730 |         mNormalSink = mOutputSink; | 
 | 3731 |         break; | 
 | 3732 |     case FastMixer_Always: | 
 | 3733 |         mNormalSink = mPipeSink; | 
 | 3734 |         break; | 
 | 3735 |     case FastMixer_Static: | 
 | 3736 |         mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
 | 3737 |         break; | 
 | 3738 |     } | 
 | 3739 | } | 
 | 3740 |  | 
 | 3741 | AudioFlinger::MixerThread::~MixerThread() | 
 | 3742 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3743 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3744 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3745 |         FastMixerState *state = sq->begin(); | 
 | 3746 |         if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 3747 |             int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 3748 |             if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3749 |                 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3750 |             } | 
 | 3751 |         } | 
 | 3752 |         state->mCommand = FastMixerState::EXIT; | 
 | 3753 |         sq->end(); | 
 | 3754 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3755 |         mFastMixer->join(); | 
 | 3756 |         // Though the fast mixer thread has exited, it's state queue is still valid. | 
 | 3757 |         // We'll use that extract the final state which contains one remaining fast track | 
 | 3758 |         // corresponding to our sub-mix. | 
 | 3759 |         state = sq->begin(); | 
 | 3760 |         ALOG_ASSERT(state->mTrackMask == 1); | 
 | 3761 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 3762 |         ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
 | 3763 |         delete fastTrack->mBufferProvider; | 
 | 3764 |         sq->end(false /*didModify*/); | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3765 |         mFastMixer.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3766 | #ifdef AUDIO_WATCHDOG | 
 | 3767 |         if (mAudioWatchdog != 0) { | 
 | 3768 |             mAudioWatchdog->requestExit(); | 
 | 3769 |             mAudioWatchdog->requestExitAndWait(); | 
 | 3770 |             mAudioWatchdog.clear(); | 
 | 3771 |         } | 
 | 3772 | #endif | 
 | 3773 |     } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3774 |     mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3775 |     delete mAudioMixer; | 
 | 3776 | } | 
 | 3777 |  | 
 | 3778 |  | 
 | 3779 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const | 
 | 3780 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3781 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3782 |         MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 3783 |         latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
 | 3784 |     } | 
 | 3785 |     return latency; | 
 | 3786 | } | 
 | 3787 |  | 
 | 3788 |  | 
 | 3789 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) | 
 | 3790 | { | 
 | 3791 |     PlaybackThread::threadLoop_removeTracks(tracksToRemove); | 
 | 3792 | } | 
 | 3793 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3794 | ssize_t AudioFlinger::MixerThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3795 | { | 
 | 3796 |     // FIXME we should only do one push per cycle; confirm this is true | 
 | 3797 |     // Start the fast mixer if it's not already running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3798 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3799 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3800 |         FastMixerState *state = sq->begin(); | 
 | 3801 |         if (state->mCommand != FastMixerState::MIX_WRITE && | 
 | 3802 |                 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
 | 3803 |             if (state->mCommand == FastMixerState::COLD_IDLE) { | 
| Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3804 |  | 
 | 3805 |                 // FIXME workaround for first HAL write being CPU bound on some devices | 
 | 3806 |                 ATRACE_BEGIN("write"); | 
 | 3807 |                 mOutput->write((char *)mSinkBuffer, 0); | 
 | 3808 |                 ATRACE_END(); | 
 | 3809 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3810 |                 int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 3811 |                 if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3812 |                     (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3813 |                 } | 
 | 3814 | #ifdef AUDIO_WATCHDOG | 
 | 3815 |                 if (mAudioWatchdog != 0) { | 
 | 3816 |                     mAudioWatchdog->resume(); | 
 | 3817 |                 } | 
 | 3818 | #endif | 
 | 3819 |             } | 
 | 3820 |             state->mCommand = FastMixerState::MIX_WRITE; | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3821 | #ifdef FAST_THREAD_STATISTICS | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3822 |             mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3823 |                 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3824 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3825 |             sq->end(); | 
 | 3826 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3827 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 3828 |                 mNormalSink = mPipeSink; | 
 | 3829 |             } | 
 | 3830 |         } else { | 
 | 3831 |             sq->end(false /*didModify*/); | 
 | 3832 |         } | 
 | 3833 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3834 |     return PlaybackThread::threadLoop_write(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3835 | } | 
 | 3836 |  | 
 | 3837 | void AudioFlinger::MixerThread::threadLoop_standby() | 
 | 3838 | { | 
 | 3839 |     // Idle the fast mixer if it's currently running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3840 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3841 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3842 |         FastMixerState *state = sq->begin(); | 
 | 3843 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
| Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 3844 |             // Report any frames trapped in the Monopipe | 
 | 3845 |             MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); | 
 | 3846 |             const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); | 
 | 3847 |             mLocalLog.log("threadLoop_standby: framesWritten:%lld  suspendedFrames:%lld  " | 
 | 3848 |                     "monoPipeWritten:%lld  monoPipeLeft:%lld", | 
 | 3849 |                     (long long)mFramesWritten, (long long)mSuspendedFrames, | 
 | 3850 |                     (long long)mPipeSink->framesWritten(), pipeFrames); | 
 | 3851 |             mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); | 
 | 3852 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3853 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 3854 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 3855 |             state->mColdGen++; | 
 | 3856 |             mFastMixerFutex = 0; | 
 | 3857 |             sq->end(); | 
 | 3858 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 3859 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3860 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 3861 |                 mNormalSink = mOutputSink; | 
 | 3862 |             } | 
 | 3863 | #ifdef AUDIO_WATCHDOG | 
 | 3864 |             if (mAudioWatchdog != 0) { | 
 | 3865 |                 mAudioWatchdog->pause(); | 
 | 3866 |             } | 
 | 3867 | #endif | 
 | 3868 |         } else { | 
 | 3869 |             sq->end(false /*didModify*/); | 
 | 3870 |         } | 
 | 3871 |     } | 
 | 3872 |     PlaybackThread::threadLoop_standby(); | 
 | 3873 | } | 
 | 3874 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3875 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() | 
 | 3876 | { | 
 | 3877 |     return false; | 
 | 3878 | } | 
 | 3879 |  | 
 | 3880 | bool AudioFlinger::PlaybackThread::shouldStandby_l() | 
 | 3881 | { | 
 | 3882 |     return !mStandby; | 
 | 3883 | } | 
 | 3884 |  | 
 | 3885 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() | 
 | 3886 | { | 
 | 3887 |     Mutex::Autolock _l(mLock); | 
 | 3888 |     return waitingAsyncCallback_l(); | 
 | 3889 | } | 
 | 3890 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3891 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
 | 3892 | void AudioFlinger::PlaybackThread::threadLoop_standby() | 
 | 3893 | { | 
 | 3894 |     ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3895 |     mOutput->standby(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3896 |     if (mUseAsyncWrite != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3897 |         // discard any pending drain or write ack by incrementing sequence | 
 | 3898 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 3899 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3900 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3901 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 3902 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3903 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3904 |     mHwPaused = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3905 | } | 
 | 3906 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3907 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() | 
 | 3908 | { | 
 | 3909 |     ALOGV("signal playback thread"); | 
 | 3910 |     broadcast_l(); | 
 | 3911 | } | 
 | 3912 |  | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3913 | void AudioFlinger::PlaybackThread::onAsyncError() | 
 | 3914 | { | 
 | 3915 |     for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { | 
 | 3916 |         invalidateTracks((audio_stream_type_t)i); | 
 | 3917 |     } | 
 | 3918 | } | 
 | 3919 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3920 | void AudioFlinger::MixerThread::threadLoop_mix() | 
 | 3921 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3922 |     // mix buffers... | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3923 |     mAudioMixer->process(); | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3924 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3925 |     // increase sleep time progressively when application underrun condition clears. | 
 | 3926 |     // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
 | 3927 |     // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
 | 3928 |     // such that we would underrun the audio HAL. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3929 |     if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3930 |         sleepTimeShift--; | 
 | 3931 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3932 |     mSleepTimeUs = 0; | 
 | 3933 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3934 |     //TODO: delay standby when effects have a tail | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3935 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3936 | } | 
 | 3937 |  | 
 | 3938 | void AudioFlinger::MixerThread::threadLoop_sleepTime() | 
 | 3939 | { | 
 | 3940 |     // If no tracks are ready, sleep once for the duration of an output | 
 | 3941 |     // buffer size, then write 0s to the output | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3942 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3943 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3944 |             mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; | 
 | 3945 |             if (mSleepTimeUs < kMinThreadSleepTimeUs) { | 
 | 3946 |                 mSleepTimeUs = kMinThreadSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3947 |             } | 
 | 3948 |             // reduce sleep time in case of consecutive application underruns to avoid | 
 | 3949 |             // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
 | 3950 |             // duration we would end up writing less data than needed by the audio HAL if | 
 | 3951 |             // the condition persists. | 
 | 3952 |             if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
 | 3953 |                 sleepTimeShift++; | 
 | 3954 |             } | 
 | 3955 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3956 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3957 |         } | 
 | 3958 |     } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3959 |         // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared | 
 | 3960 |         // before effects processing or output. | 
 | 3961 |         if (mMixerBufferValid) { | 
 | 3962 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 3963 |         } else { | 
 | 3964 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 3965 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3966 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3967 |         ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
 | 3968 |                 "anticipated start"); | 
 | 3969 |     } | 
 | 3970 |     // TODO add standby time extension fct of effect tail | 
 | 3971 | } | 
 | 3972 |  | 
 | 3973 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
 | 3974 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( | 
 | 3975 |         Vector< sp<Track> > *tracksToRemove) | 
 | 3976 | { | 
 | 3977 |  | 
 | 3978 |     mixer_state mixerStatus = MIXER_IDLE; | 
 | 3979 |     // find out which tracks need to be processed | 
 | 3980 |     size_t count = mActiveTracks.size(); | 
 | 3981 |     size_t mixedTracks = 0; | 
 | 3982 |     size_t tracksWithEffect = 0; | 
 | 3983 |     // counts only _active_ fast tracks | 
 | 3984 |     size_t fastTracks = 0; | 
 | 3985 |     uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
 | 3986 |  | 
 | 3987 |     float masterVolume = mMasterVolume; | 
 | 3988 |     bool masterMute = mMasterMute; | 
 | 3989 |  | 
 | 3990 |     if (masterMute) { | 
 | 3991 |         masterVolume = 0; | 
 | 3992 |     } | 
 | 3993 |     // Delegate master volume control to effect in output mix effect chain if needed | 
 | 3994 |     sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
 | 3995 |     if (chain != 0) { | 
 | 3996 |         uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
 | 3997 |         chain->setVolume_l(&v, &v); | 
 | 3998 |         masterVolume = (float)((v + (1 << 23)) >> 24); | 
 | 3999 |         chain.clear(); | 
 | 4000 |     } | 
 | 4001 |  | 
 | 4002 |     // prepare a new state to push | 
 | 4003 |     FastMixerStateQueue *sq = NULL; | 
 | 4004 |     FastMixerState *state = NULL; | 
 | 4005 |     bool didModify = false; | 
 | 4006 |     FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4007 |     bool coldIdle = false; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4008 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4009 |         sq = mFastMixer->sq(); | 
 | 4010 |         state = sq->begin(); | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4011 |         coldIdle = state->mCommand == FastMixerState::COLD_IDLE; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4012 |     } | 
 | 4013 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4014 |     mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4015 |     mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4016 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4017 |     for (size_t i=0 ; i<count ; i++) { | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4018 |         const sp<Track> t = mActiveTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4019 |  | 
 | 4020 |         // this const just means the local variable doesn't change | 
 | 4021 |         Track* const track = t.get(); | 
 | 4022 |  | 
 | 4023 |         // process fast tracks | 
 | 4024 |         if (track->isFastTrack()) { | 
 | 4025 |  | 
 | 4026 |             // It's theoretically possible (though unlikely) for a fast track to be created | 
 | 4027 |             // and then removed within the same normal mix cycle.  This is not a problem, as | 
 | 4028 |             // the track never becomes active so it's fast mixer slot is never touched. | 
 | 4029 |             // The converse, of removing an (active) track and then creating a new track | 
 | 4030 |             // at the identical fast mixer slot within the same normal mix cycle, | 
 | 4031 |             // is impossible because the slot isn't marked available until the end of each cycle. | 
 | 4032 |             int j = track->mFastIndex; | 
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4033 |             ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4034 |             ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
 | 4035 |             FastTrack *fastTrack = &state->mFastTracks[j]; | 
 | 4036 |  | 
 | 4037 |             // Determine whether the track is currently in underrun condition, | 
 | 4038 |             // and whether it had a recent underrun. | 
 | 4039 |             FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
 | 4040 |             FastTrackUnderruns underruns = ftDump->mUnderruns; | 
 | 4041 |             uint32_t recentFull = (underruns.mBitFields.mFull - | 
 | 4042 |                     track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; | 
 | 4043 |             uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
 | 4044 |                     track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; | 
 | 4045 |             uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
 | 4046 |                     track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; | 
 | 4047 |             uint32_t recentUnderruns = recentPartial + recentEmpty; | 
 | 4048 |             track->mObservedUnderruns = underruns; | 
 | 4049 |             // don't count underruns that occur while stopping or pausing | 
 | 4050 |             // or stopped which can occur when flush() is called while active | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4051 |             if (!(track->isStopping() || track->isPausing() || track->isStopped()) && | 
 | 4052 |                     recentUnderruns > 0) { | 
 | 4053 |                 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun | 
 | 4054 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4055 |             } else { | 
 | 4056 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4057 |             } | 
 | 4058 |  | 
 | 4059 |             // This is similar to the state machine for normal tracks, | 
 | 4060 |             // with a few modifications for fast tracks. | 
 | 4061 |             bool isActive = true; | 
 | 4062 |             switch (track->mState) { | 
 | 4063 |             case TrackBase::STOPPING_1: | 
 | 4064 |                 // track stays active in STOPPING_1 state until first underrun | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4065 |                 if (recentUnderruns > 0 || track->isTerminated()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4066 |                     track->mState = TrackBase::STOPPING_2; | 
 | 4067 |                 } | 
 | 4068 |                 break; | 
 | 4069 |             case TrackBase::PAUSING: | 
 | 4070 |                 // ramp down is not yet implemented | 
 | 4071 |                 track->setPaused(); | 
 | 4072 |                 break; | 
 | 4073 |             case TrackBase::RESUMING: | 
 | 4074 |                 // ramp up is not yet implemented | 
 | 4075 |                 track->mState = TrackBase::ACTIVE; | 
 | 4076 |                 break; | 
 | 4077 |             case TrackBase::ACTIVE: | 
 | 4078 |                 if (recentFull > 0 || recentPartial > 0) { | 
 | 4079 |                     // track has provided at least some frames recently: reset retry count | 
 | 4080 |                     track->mRetryCount = kMaxTrackRetries; | 
 | 4081 |                 } | 
 | 4082 |                 if (recentUnderruns == 0) { | 
 | 4083 |                     // no recent underruns: stay active | 
 | 4084 |                     break; | 
 | 4085 |                 } | 
 | 4086 |                 // there has recently been an underrun of some kind | 
 | 4087 |                 if (track->sharedBuffer() == 0) { | 
 | 4088 |                     // were any of the recent underruns "empty" (no frames available)? | 
 | 4089 |                     if (recentEmpty == 0) { | 
 | 4090 |                         // no, then ignore the partial underruns as they are allowed indefinitely | 
 | 4091 |                         break; | 
 | 4092 |                     } | 
 | 4093 |                     // there has recently been an "empty" underrun: decrement the retry counter | 
 | 4094 |                     if (--(track->mRetryCount) > 0) { | 
 | 4095 |                         break; | 
 | 4096 |                     } | 
 | 4097 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 4098 |                     // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4099 |                     track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4100 |                     // remove from active list, but state remains ACTIVE [confusing but true] | 
 | 4101 |                     isActive = false; | 
 | 4102 |                     break; | 
 | 4103 |                 } | 
 | 4104 |                 // fall through | 
 | 4105 |             case TrackBase::STOPPING_2: | 
 | 4106 |             case TrackBase::PAUSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4107 |             case TrackBase::STOPPED: | 
 | 4108 |             case TrackBase::FLUSHED:   // flush() while active | 
 | 4109 |                 // Check for presentation complete if track is inactive | 
 | 4110 |                 // We have consumed all the buffers of this track. | 
 | 4111 |                 // This would be incomplete if we auto-paused on underrun | 
 | 4112 |                 { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4113 |                     uint32_t latency = 0; | 
 | 4114 |                     status_t result = mOutput->stream->getLatency(&latency); | 
 | 4115 |                     ALOGE_IF(result != OK, | 
 | 4116 |                             "Error when retrieving output stream latency: %d", result); | 
 | 4117 |                     size_t audioHALFrames = (latency * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4118 |                     int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4119 |                     if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
 | 4120 |                         // track stays in active list until presentation is complete | 
 | 4121 |                         break; | 
 | 4122 |                     } | 
 | 4123 |                 } | 
 | 4124 |                 if (track->isStopping_2()) { | 
 | 4125 |                     track->mState = TrackBase::STOPPED; | 
 | 4126 |                 } | 
 | 4127 |                 if (track->isStopped()) { | 
 | 4128 |                     // Can't reset directly, as fast mixer is still polling this track | 
 | 4129 |                     //   track->reset(); | 
 | 4130 |                     // So instead mark this track as needing to be reset after push with ack | 
 | 4131 |                     resetMask |= 1 << i; | 
 | 4132 |                 } | 
 | 4133 |                 isActive = false; | 
 | 4134 |                 break; | 
 | 4135 |             case TrackBase::IDLE: | 
 | 4136 |             default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4137 |                 LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4138 |             } | 
 | 4139 |  | 
 | 4140 |             if (isActive) { | 
 | 4141 |                 // was it previously inactive? | 
 | 4142 |                 if (!(state->mTrackMask & (1 << j))) { | 
 | 4143 |                     ExtendedAudioBufferProvider *eabp = track; | 
 | 4144 |                     VolumeProvider *vp = track; | 
 | 4145 |                     fastTrack->mBufferProvider = eabp; | 
 | 4146 |                     fastTrack->mVolumeProvider = vp; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4147 |                     fastTrack->mChannelMask = track->mChannelMask; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4148 |                     fastTrack->mFormat = track->mFormat; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4149 |                     fastTrack->mGeneration++; | 
 | 4150 |                     state->mTrackMask |= 1 << j; | 
 | 4151 |                     didModify = true; | 
 | 4152 |                     // no acknowledgement required for newly active tracks | 
 | 4153 |                 } | 
 | 4154 |                 // cache the combined master volume and stream type volume for fast mixer; this | 
 | 4155 |                 // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4156 |                 const float vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4157 |                         track->mAudioTrackServerProxy->framesReleased()).first; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4158 |                 track->mCachedVolume = masterVolume | 
 | 4159 |                         * mStreamTypes[track->streamType()].volume | 
 | 4160 |                         * vh; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4161 |                 ++fastTracks; | 
 | 4162 |             } else { | 
 | 4163 |                 // was it previously active? | 
 | 4164 |                 if (state->mTrackMask & (1 << j)) { | 
 | 4165 |                     fastTrack->mBufferProvider = NULL; | 
 | 4166 |                     fastTrack->mGeneration++; | 
 | 4167 |                     state->mTrackMask &= ~(1 << j); | 
 | 4168 |                     didModify = true; | 
 | 4169 |                     // If any fast tracks were removed, we must wait for acknowledgement | 
 | 4170 |                     // because we're about to decrement the last sp<> on those tracks. | 
 | 4171 |                     block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 4172 |                 } else { | 
| Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4173 |                     LOG_ALWAYS_FATAL("fast track %d should have been active; " | 
 | 4174 |                             "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", | 
 | 4175 |                             j, track->mState, state->mTrackMask, recentUnderruns, | 
 | 4176 |                             track->sharedBuffer() != 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4177 |                 } | 
 | 4178 |                 tracksToRemove->add(track); | 
 | 4179 |                 // Avoids a misleading display in dumpsys | 
 | 4180 |                 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; | 
 | 4181 |             } | 
 | 4182 |             continue; | 
 | 4183 |         } | 
 | 4184 |  | 
 | 4185 |         {   // local variable scope to avoid goto warning | 
 | 4186 |  | 
 | 4187 |         audio_track_cblk_t* cblk = track->cblk(); | 
 | 4188 |  | 
 | 4189 |         // The first time a track is added we wait | 
 | 4190 |         // for all its buffers to be filled before processing it | 
 | 4191 |         int name = track->name(); | 
 | 4192 |         // make sure that we have enough frames to mix one full buffer. | 
 | 4193 |         // enforce this condition only once to enable draining the buffer in case the client | 
 | 4194 |         // app does not call stop() and relies on underrun to stop: | 
 | 4195 |         // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
 | 4196 |         // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4197 |         size_t desiredFrames; | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4198 |         const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4199 |         AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4200 |  | 
 | 4201 |         desiredFrames = sourceFramesNeededWithTimestretch( | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4202 |                 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4203 |         // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. | 
 | 4204 |         // add frames already consumed but not yet released by the resampler | 
 | 4205 |         // because mAudioTrackServerProxy->framesReady() will include these frames | 
 | 4206 |         desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
 | 4207 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4208 |         uint32_t minFrames = 1; | 
 | 4209 |         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
 | 4210 |                 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4211 |             minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4212 |         } | 
| Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4213 |  | 
 | 4214 |         size_t framesReady = track->framesReady(); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4215 |         if (ATRACE_ENABLED()) { | 
 | 4216 |             // I wish we had formatted trace names | 
 | 4217 |             char traceName[16]; | 
 | 4218 |             strcpy(traceName, "nRdy"); | 
 | 4219 |             int name = track->name(); | 
 | 4220 |             if (AudioMixer::TRACK0 <= name && | 
 | 4221 |                     name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { | 
 | 4222 |                 name -= AudioMixer::TRACK0; | 
 | 4223 |                 traceName[4] = (name / 10) + '0'; | 
 | 4224 |                 traceName[5] = (name % 10) + '0'; | 
 | 4225 |             } else { | 
 | 4226 |                 traceName[4] = '?'; | 
 | 4227 |                 traceName[5] = '?'; | 
 | 4228 |             } | 
 | 4229 |             traceName[6] = '\0'; | 
 | 4230 |             ATRACE_INT(traceName, framesReady); | 
 | 4231 |         } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4232 |         if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4233 |                 !track->isPaused() && !track->isTerminated()) | 
 | 4234 |         { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4235 |             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] | 4236 |  | 
 | 4237 |             mixedTracks++; | 
 | 4238 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4239 |             // track->mainBuffer() != mSinkBuffer or mMixerBuffer means | 
 | 4240 |             // there is an effect chain connected to the track | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4241 |             chain.clear(); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4242 |             if (track->mainBuffer() != mSinkBuffer && | 
 | 4243 |                     track->mainBuffer() != mMixerBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4244 |                 if (mEffectBufferEnabled) { | 
 | 4245 |                     mEffectBufferValid = true; // Later can set directly. | 
 | 4246 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4247 |                 chain = getEffectChain_l(track->sessionId()); | 
 | 4248 |                 // Delegate volume control to effect in track effect chain if needed | 
 | 4249 |                 if (chain != 0) { | 
 | 4250 |                     tracksWithEffect++; | 
 | 4251 |                 } else { | 
 | 4252 |                     ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " | 
 | 4253 |                             "session %d", | 
 | 4254 |                             name, track->sessionId()); | 
 | 4255 |                 } | 
 | 4256 |             } | 
 | 4257 |  | 
 | 4258 |  | 
 | 4259 |             int param = AudioMixer::VOLUME; | 
 | 4260 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 4261 |                 // no ramp for the first volume setting | 
 | 4262 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
 | 4263 |                 if (track->mState == TrackBase::RESUMING) { | 
 | 4264 |                     track->mState = TrackBase::ACTIVE; | 
 | 4265 |                     param = AudioMixer::RAMP_VOLUME; | 
 | 4266 |                 } | 
 | 4267 |                 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4268 |             // FIXME should not make a decision based on mServer | 
 | 4269 |             } else if (cblk->mServer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4270 |                 // If the track is stopped before the first frame was mixed, | 
 | 4271 |                 // do not apply ramp | 
 | 4272 |                 param = AudioMixer::RAMP_VOLUME; | 
 | 4273 |             } | 
 | 4274 |  | 
 | 4275 |             // compute volume for this track | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4276 |             uint32_t vl, vr;       // in U8.24 integer format | 
 | 4277 |             float vlf, vrf, vaf;   // in [0.0, 1.0] float format | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4278 |             if (track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4279 |                 vl = vr = 0; | 
 | 4280 |                 vlf = vrf = vaf = 0.; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4281 |                 if (track->isPausing()) { | 
 | 4282 |                     track->setPaused(); | 
 | 4283 |                 } | 
 | 4284 |             } else { | 
 | 4285 |  | 
 | 4286 |                 // read original volumes with volume control | 
 | 4287 |                 float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 4288 |                 float v = masterVolume * typeVolume; | 
| Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4289 |                 sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4290 |                 gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4291 |                 vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 4292 |                 vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4293 |                 // 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] | 4294 |                 if (vlf > GAIN_FLOAT_UNITY) { | 
 | 4295 |                     ALOGV("Track left volume out of range: %.3g", vlf); | 
 | 4296 |                     vlf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4297 |                 } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4298 |                 if (vrf > GAIN_FLOAT_UNITY) { | 
 | 4299 |                     ALOGV("Track right volume out of range: %.3g", vrf); | 
 | 4300 |                     vrf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4301 |                 } | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4302 |                 const float vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4303 |                         track->mAudioTrackServerProxy->framesReleased()).first; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4304 |                 // now apply the master volume and stream type volume and shaper volume | 
 | 4305 |                 vlf *= v * vh; | 
 | 4306 |                 vrf *= v * vh; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4307 |                 // 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] | 4308 |                 // then derive vl and vr as U8.24 versions for the effect chain | 
 | 4309 |                 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; | 
 | 4310 |                 vl = (uint32_t) (scaleto8_24 * vlf); | 
 | 4311 |                 vr = (uint32_t) (scaleto8_24 * vrf); | 
 | 4312 |                 // vl and vr are now in U8.24 format | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4313 |                 uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4314 |                 // send level comes from shared memory and so may be corrupt | 
 | 4315 |                 if (sendLevel > MAX_GAIN_INT) { | 
 | 4316 |                     ALOGV("Track send level out of range: %04X", sendLevel); | 
 | 4317 |                     sendLevel = MAX_GAIN_INT; | 
 | 4318 |                 } | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4319 |                 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel | 
 | 4320 |                 vaf = v * sendLevel * (1. / MAX_GAIN_INT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4321 |             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4322 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4323 |             // Delegate volume control to effect in track effect chain if needed | 
 | 4324 |             if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
 | 4325 |                 // Do not ramp volume if volume is controlled by effect | 
 | 4326 |                 param = AudioMixer::VOLUME; | 
| Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4327 |                 // Update remaining floating point volume levels | 
 | 4328 |                 vlf = (float)vl / (1 << 24); | 
 | 4329 |                 vrf = (float)vr / (1 << 24); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4330 |                 track->mHasVolumeController = true; | 
 | 4331 |             } else { | 
 | 4332 |                 // force no volume ramp when volume controller was just disabled or removed | 
 | 4333 |                 // from effect chain to avoid volume spike | 
 | 4334 |                 if (track->mHasVolumeController) { | 
 | 4335 |                     param = AudioMixer::VOLUME; | 
 | 4336 |                 } | 
 | 4337 |                 track->mHasVolumeController = false; | 
 | 4338 |             } | 
 | 4339 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4340 |             // XXX: these things DON'T need to be done each time | 
 | 4341 |             mAudioMixer->setBufferProvider(name, track); | 
 | 4342 |             mAudioMixer->enable(name); | 
 | 4343 |  | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4344 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); | 
 | 4345 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); | 
 | 4346 |             mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4347 |             mAudioMixer->setParameter( | 
 | 4348 |                 name, | 
 | 4349 |                 AudioMixer::TRACK, | 
 | 4350 |                 AudioMixer::FORMAT, (void *)track->format()); | 
 | 4351 |             mAudioMixer->setParameter( | 
 | 4352 |                 name, | 
 | 4353 |                 AudioMixer::TRACK, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4354 |                 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4355 |             mAudioMixer->setParameter( | 
 | 4356 |                 name, | 
 | 4357 |                 AudioMixer::TRACK, | 
 | 4358 |                 AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4359 |             // 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] | 4360 |             uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4361 |             uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4362 |             if (reqSampleRate == 0) { | 
 | 4363 |                 reqSampleRate = mSampleRate; | 
 | 4364 |             } else if (reqSampleRate > maxSampleRate) { | 
 | 4365 |                 reqSampleRate = maxSampleRate; | 
 | 4366 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4367 |             mAudioMixer->setParameter( | 
 | 4368 |                 name, | 
 | 4369 |                 AudioMixer::RESAMPLE, | 
 | 4370 |                 AudioMixer::SAMPLE_RATE, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4371 |                 (void *)(uintptr_t)reqSampleRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4372 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4373 |             AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4374 |             mAudioMixer->setParameter( | 
 | 4375 |                 name, | 
 | 4376 |                 AudioMixer::TIMESTRETCH, | 
 | 4377 |                 AudioMixer::PLAYBACK_RATE, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4378 |                 &playbackRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4379 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4380 |             /* | 
 | 4381 |              * Select the appropriate output buffer for the track. | 
 | 4382 |              * | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4383 |              * Tracks with effects go into their own effects chain buffer | 
 | 4384 |              * and from there into either mEffectBuffer or mSinkBuffer. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4385 |              * | 
 | 4386 |              * Other tracks can use mMixerBuffer for higher precision | 
 | 4387 |              * channel accumulation.  If this buffer is enabled | 
 | 4388 |              * (mMixerBufferEnabled true), then selected tracks will accumulate | 
 | 4389 |              * into it. | 
 | 4390 |              * | 
 | 4391 |              */ | 
 | 4392 |             if (mMixerBufferEnabled | 
 | 4393 |                     && (track->mainBuffer() == mSinkBuffer | 
 | 4394 |                             || track->mainBuffer() == mMixerBuffer)) { | 
 | 4395 |                 mAudioMixer->setParameter( | 
 | 4396 |                         name, | 
 | 4397 |                         AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4398 |                         AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4399 |                 mAudioMixer->setParameter( | 
 | 4400 |                         name, | 
 | 4401 |                         AudioMixer::TRACK, | 
 | 4402 |                         AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); | 
 | 4403 |                 // TODO: override track->mainBuffer()? | 
 | 4404 |                 mMixerBufferValid = true; | 
 | 4405 |             } else { | 
 | 4406 |                 mAudioMixer->setParameter( | 
 | 4407 |                         name, | 
 | 4408 |                         AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4409 |                         AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4410 |                 mAudioMixer->setParameter( | 
 | 4411 |                         name, | 
 | 4412 |                         AudioMixer::TRACK, | 
 | 4413 |                         AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
 | 4414 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4415 |             mAudioMixer->setParameter( | 
 | 4416 |                 name, | 
 | 4417 |                 AudioMixer::TRACK, | 
 | 4418 |                 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
 | 4419 |  | 
 | 4420 |             // reset retry count | 
 | 4421 |             track->mRetryCount = kMaxTrackRetries; | 
 | 4422 |  | 
 | 4423 |             // If one track is ready, set the mixer ready if: | 
 | 4424 |             //  - the mixer was not ready during previous round OR | 
 | 4425 |             //  - no other track is not ready | 
 | 4426 |             if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
 | 4427 |                     mixerStatus != MIXER_TRACKS_ENABLED) { | 
 | 4428 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 4429 |             } | 
 | 4430 |         } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4431 |             if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4432 |                 ALOGV("track(%p) underrun,  framesReady(%zu) < framesDesired(%zd)", | 
 | 4433 |                         track, framesReady, desiredFrames); | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4434 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4435 |             } else { | 
 | 4436 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(0); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4437 |             } | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4438 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4439 |             // clear effect chain input buffer if an active track underruns to avoid sending | 
 | 4440 |             // previous audio buffer again to effects | 
 | 4441 |             chain = getEffectChain_l(track->sessionId()); | 
 | 4442 |             if (chain != 0) { | 
 | 4443 |                 chain->clearInputBuffer(); | 
 | 4444 |             } | 
 | 4445 |  | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4446 |             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] | 4447 |             if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
 | 4448 |                     track->isStopped() || track->isPaused()) { | 
 | 4449 |                 // We have consumed all the buffers of this track. | 
 | 4450 |                 // Remove it from the list of active tracks. | 
 | 4451 |                 // TODO: use actual buffer filling status instead of latency when available from | 
 | 4452 |                 // audio HAL | 
 | 4453 |                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4454 |                 int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4455 |                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
 | 4456 |                     if (track->isStopped()) { | 
 | 4457 |                         track->reset(); | 
 | 4458 |                     } | 
 | 4459 |                     tracksToRemove->add(track); | 
 | 4460 |                 } | 
 | 4461 |             } else { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4462 |                 // No buffers for this track. Give it a few chances to | 
 | 4463 |                 // fill a buffer, then remove it from active list. | 
 | 4464 |                 if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4465 |                     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] | 4466 |                     tracksToRemove->add(track); | 
 | 4467 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 4468 |                     // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4469 |                     track->disable(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4470 |                 // If one track is not ready, mark the mixer also not ready if: | 
 | 4471 |                 //  - the mixer was ready during previous round OR | 
 | 4472 |                 //  - no other track is ready | 
 | 4473 |                 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
 | 4474 |                                 mixerStatus != MIXER_TRACKS_READY) { | 
 | 4475 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 4476 |                 } | 
 | 4477 |             } | 
 | 4478 |             mAudioMixer->disable(name); | 
 | 4479 |         } | 
 | 4480 |  | 
 | 4481 |         }   // local variable scope to avoid goto warning | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4482 |  | 
 | 4483 |     } | 
 | 4484 |  | 
 | 4485 |     // Push the new FastMixer state if necessary | 
 | 4486 |     bool pauseAudioWatchdog = false; | 
 | 4487 |     if (didModify) { | 
 | 4488 |         state->mFastTracksGen++; | 
 | 4489 |         // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
 | 4490 |         if (kUseFastMixer == FastMixer_Dynamic && | 
 | 4491 |                 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
 | 4492 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 4493 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 4494 |             state->mColdGen++; | 
 | 4495 |             mFastMixerFutex = 0; | 
 | 4496 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 4497 |                 mNormalSink = mOutputSink; | 
 | 4498 |             } | 
 | 4499 |             // If we go into cold idle, need to wait for acknowledgement | 
 | 4500 |             // so that fast mixer stops doing I/O. | 
 | 4501 |             block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 4502 |             pauseAudioWatchdog = true; | 
 | 4503 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4504 |     } | 
 | 4505 |     if (sq != NULL) { | 
 | 4506 |         sq->end(didModify); | 
| Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4507 |         // No need to block if the FastMixer is in COLD_IDLE as the FastThread | 
 | 4508 |         // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE | 
 | 4509 |         // when bringing the output sink into standby.) | 
 | 4510 |         // | 
 | 4511 |         // We will get the latest FastMixer state when we come out of COLD_IDLE. | 
 | 4512 |         // | 
 | 4513 |         // This occurs with BT suspend when we idle the FastMixer with | 
 | 4514 |         // active tracks, which may be added or removed. | 
 | 4515 |         sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4516 |     } | 
 | 4517 | #ifdef AUDIO_WATCHDOG | 
 | 4518 |     if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
 | 4519 |         mAudioWatchdog->pause(); | 
 | 4520 |     } | 
 | 4521 | #endif | 
 | 4522 |  | 
 | 4523 |     // Now perform the deferred reset on fast tracks that have stopped | 
 | 4524 |     while (resetMask != 0) { | 
 | 4525 |         size_t i = __builtin_ctz(resetMask); | 
 | 4526 |         ALOG_ASSERT(i < count); | 
 | 4527 |         resetMask &= ~(1 << i); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4528 |         sp<Track> track = mActiveTracks[i]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4529 |         ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
 | 4530 |         track->reset(); | 
 | 4531 |     } | 
 | 4532 |  | 
 | 4533 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4534 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4535 |  | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4536 |     if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { | 
 | 4537 |         mEffectBufferValid = true; | 
| Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4538 |     } | 
 | 4539 |  | 
 | 4540 |     if (mEffectBufferValid) { | 
| Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4541 |         // as long as there are effects we should clear the effects buffer, to avoid | 
 | 4542 |         // passing a non-clean buffer to the effect chain | 
 | 4543 |         memset(mEffectBuffer, 0, mEffectBufferSize); | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4544 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4545 |     // sink or mix buffer must be cleared if all tracks are connected to an | 
 | 4546 |     // effect chain as in this case the mixer will not write to the sink or mix buffer | 
 | 4547 |     // and track effects will accumulate into it | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4548 |     if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 4549 |             (mixedTracks == 0 && fastTracks > 0))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4550 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4551 |         if (mMixerBufferValid) { | 
 | 4552 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 4553 |             // TODO: In testing, mSinkBuffer below need not be cleared because | 
 | 4554 |             // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer | 
 | 4555 |             // after mixing. | 
 | 4556 |             // | 
 | 4557 |             // To enforce this guarantee: | 
 | 4558 |             // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 4559 |             // (mixedTracks == 0 && fastTracks > 0)) | 
 | 4560 |             // must imply MIXER_TRACKS_READY. | 
 | 4561 |             // Later, we may clear buffers regardless, and skip much of this logic. | 
 | 4562 |         } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4563 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4564 |         memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4565 |     } | 
 | 4566 |  | 
 | 4567 |     // if any fast tracks, then status is ready | 
 | 4568 |     mMixerStatusIgnoringFastTracks = mixerStatus; | 
 | 4569 |     if (fastTracks > 0) { | 
 | 4570 |         mixerStatus = MIXER_TRACKS_READY; | 
 | 4571 |     } | 
 | 4572 |     return mixerStatus; | 
 | 4573 | } | 
 | 4574 |  | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4575 | // trackCountForUid_l() must be called with ThreadBase::mLock held | 
 | 4576 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) | 
 | 4577 | { | 
 | 4578 |     uint32_t trackCount = 0; | 
 | 4579 |     for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 4580 |         if (mTracks[i]->uid() == uid) { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4581 |             trackCount++; | 
 | 4582 |         } | 
 | 4583 |     } | 
 | 4584 |     return trackCount; | 
 | 4585 | } | 
 | 4586 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4587 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4588 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4589 |         audio_format_t format, audio_session_t sessionId, uid_t uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4590 | { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4591 |     if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { | 
 | 4592 |         return -1; | 
 | 4593 |     } | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4594 |     return mAudioMixer->getTrackName(channelMask, format, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4595 | } | 
 | 4596 |  | 
 | 4597 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
 | 4598 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
 | 4599 | { | 
 | 4600 |     ALOGV("remove track (%d) and delete from mixer", name); | 
 | 4601 |     mAudioMixer->deleteTrackName(name); | 
 | 4602 | } | 
 | 4603 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4604 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 4605 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 4606 |                                                        status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4607 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4608 |     bool reconfig = false; | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4609 |     bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4610 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4611 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4612 |  | 
| Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4613 |     AutoPark<FastMixer> park(mFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4614 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4615 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 4616 |     int value; | 
 | 4617 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 4618 |         reconfig = true; | 
 | 4619 |     } | 
 | 4620 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4621 |         if (!isValidPcmSinkFormat((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4622 |             status = BAD_VALUE; | 
 | 4623 |         } else { | 
 | 4624 |             // no need to save value, since it's constant | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4625 |             reconfig = true; | 
 | 4626 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4627 |     } | 
 | 4628 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4629 |         if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4630 |             status = BAD_VALUE; | 
 | 4631 |         } else { | 
 | 4632 |             // no need to save value, since it's constant | 
 | 4633 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4634 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4635 |     } | 
 | 4636 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 4637 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 4638 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 4639 |         // if frame count is changed after track creation | 
 | 4640 |         if (!mTracks.isEmpty()) { | 
 | 4641 |             status = INVALID_OPERATION; | 
 | 4642 |         } else { | 
 | 4643 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4644 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4645 |     } | 
 | 4646 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4647 | #ifdef ADD_BATTERY_DATA | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4648 |         // when changing the audio output device, call addBatteryData to notify | 
 | 4649 |         // the change | 
 | 4650 |         if (mOutDevice != value) { | 
 | 4651 |             uint32_t params = 0; | 
 | 4652 |             // check whether speaker is on | 
 | 4653 |             if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 4654 |                 params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4655 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4656 |  | 
 | 4657 |             audio_devices_t deviceWithoutSpeaker | 
 | 4658 |                 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 4659 |             // check if any other device (except speaker) is on | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4660 |             if (value & deviceWithoutSpeaker) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4661 |                 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 4662 |             } | 
 | 4663 |  | 
 | 4664 |             if (params != 0) { | 
 | 4665 |                 addBatteryData(params); | 
 | 4666 |             } | 
 | 4667 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4668 | #endif | 
 | 4669 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4670 |         // forward device change to effects that have requested to be | 
 | 4671 |         // aware of attached audio device. | 
 | 4672 |         if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4673 |             a2dpDeviceChanged = | 
 | 4674 |                     (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4675 |             mOutDevice = value; | 
 | 4676 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4677 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4678 |             } | 
 | 4679 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4680 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4681 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4682 |     if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4683 |         status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4684 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4685 |             mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4686 |             mStandby = true; | 
 | 4687 |             mBytesWritten = 0; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4688 |             status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4689 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4690 |         if (status == NO_ERROR && reconfig) { | 
 | 4691 |             readOutputParameters_l(); | 
 | 4692 |             delete mAudioMixer; | 
 | 4693 |             mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 4694 |             for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4695 |                 int name = getTrackName_l(mTracks[i]->mChannelMask, | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4696 |                         mTracks[i]->mFormat, mTracks[i]->mSessionId, mTracks[i]->uid()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4697 |                 if (name < 0) { | 
 | 4698 |                     break; | 
 | 4699 |                 } | 
 | 4700 |                 mTracks[i]->mName = name; | 
 | 4701 |             } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4702 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4703 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4704 |     } | 
 | 4705 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4706 |     return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4707 | } | 
 | 4708 |  | 
 | 4709 |  | 
 | 4710 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 4711 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4712 |     PlaybackThread::dumpInternals(fd, args); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4713 |     dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4714 |     dprintf(fd, "  AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4715 |     dprintf(fd, "  Master mono: %s\n", mMasterMono ? "on" : "off"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4716 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4717 |     if (hasFastMixer()) { | 
 | 4718 |         dprintf(fd, "  FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); | 
 | 4719 |  | 
 | 4720 |         // Make a non-atomic copy of fast mixer dump state so it won't change underneath us | 
 | 4721 |         // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
 | 4722 |         // This is a large object so we place it on the heap. | 
 | 4723 |         // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
 | 4724 |         const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); | 
 | 4725 |         copy->dump(fd); | 
 | 4726 |         delete copy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4727 |  | 
 | 4728 | #ifdef STATE_QUEUE_DUMP | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4729 |         // Similar for state queue | 
 | 4730 |         StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
 | 4731 |         observerCopy.dump(fd); | 
 | 4732 |         StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
 | 4733 |         mutatorCopy.dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4734 | #endif | 
 | 4735 |  | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 4736 | #ifdef AUDIO_WATCHDOG | 
 | 4737 |         if (mAudioWatchdog != 0) { | 
 | 4738 |             // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
 | 4739 |             AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
 | 4740 |             wdCopy.dump(fd); | 
 | 4741 |         } | 
 | 4742 | #endif | 
 | 4743 |  | 
 | 4744 |     } else { | 
 | 4745 |         dprintf(fd, "  No FastMixer\n"); | 
 | 4746 |     } | 
 | 4747 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4748 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4749 |     // Write the tee output to a .wav file | 
| Glenn Kasten | 5b2191a | 2016-08-19 11:44:47 -0700 | [diff] [blame] | 4750 |     dumpTee(fd, mTeeSource, mId, 'M'); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4751 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4752 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4753 | } | 
 | 4754 |  | 
 | 4755 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const | 
 | 4756 | { | 
 | 4757 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 4758 | } | 
 | 4759 |  | 
 | 4760 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const | 
 | 4761 | { | 
 | 4762 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 4763 | } | 
 | 4764 |  | 
 | 4765 | void AudioFlinger::MixerThread::cacheParameters_l() | 
 | 4766 | { | 
 | 4767 |     PlaybackThread::cacheParameters_l(); | 
 | 4768 |  | 
 | 4769 |     // FIXME: Relaxed timing because of a certain device that can't meet latency | 
 | 4770 |     // Should be reduced to 2x after the vendor fixes the driver issue | 
 | 4771 |     // increase threshold again due to low power audio mode. The way this warning | 
 | 4772 |     // threshold is calculated and its usefulness should be reconsidered anyway. | 
 | 4773 |     maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
 | 4774 | } | 
 | 4775 |  | 
 | 4776 | // ---------------------------------------------------------------------------- | 
 | 4777 |  | 
 | 4778 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4779 |         AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) | 
 | 4780 |     :   PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4781 |         // mLeftVolFloat, mRightVolFloat | 
 | 4782 | { | 
 | 4783 | } | 
 | 4784 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4785 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
 | 4786 |         AudioStreamOut* output, audio_io_handle_t id, uint32_t device, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4787 |         ThreadBase::type_t type, bool systemReady) | 
 | 4788 |     :   PlaybackThread(audioFlinger, output, id, device, type, systemReady) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4789 |         // mLeftVolFloat, mRightVolFloat | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4790 |         , mVolumeShaperActive(false) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4791 | { | 
 | 4792 | } | 
 | 4793 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4794 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
 | 4795 | { | 
 | 4796 | } | 
 | 4797 |  | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4798 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4799 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4800 |     float left, right; | 
 | 4801 |  | 
 | 4802 |     if (mMasterMute || mStreamTypes[track->streamType()].mute) { | 
 | 4803 |         left = right = 0; | 
 | 4804 |     } else { | 
 | 4805 |         float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 4806 |         float v = mMasterVolume * typeVolume; | 
| Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 4807 |         sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4808 |  | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4809 |         // Get volumeshaper scaling | 
 | 4810 |         std::pair<float /* volume */, bool /* active */> | 
 | 4811 |             vh = track->getVolumeHandler()->getVolume( | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4812 |                     track->mAudioTrackServerProxy->framesReleased()); | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4813 |         v *= vh.first; | 
 | 4814 |         mVolumeShaperActive = vh.second; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 4815 |  | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4816 |         gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
 | 4817 |         left = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 4818 |         if (left > GAIN_FLOAT_UNITY) { | 
 | 4819 |             left = GAIN_FLOAT_UNITY; | 
 | 4820 |         } | 
 | 4821 |         left *= v; | 
 | 4822 |         right = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
 | 4823 |         if (right > GAIN_FLOAT_UNITY) { | 
 | 4824 |             right = GAIN_FLOAT_UNITY; | 
 | 4825 |         } | 
 | 4826 |         right *= v; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4827 |     } | 
 | 4828 |  | 
 | 4829 |     if (lastTrack) { | 
 | 4830 |         if (left != mLeftVolFloat || right != mRightVolFloat) { | 
 | 4831 |             mLeftVolFloat = left; | 
 | 4832 |             mRightVolFloat = right; | 
 | 4833 |  | 
 | 4834 |             // Convert volumes from float to 8.24 | 
 | 4835 |             uint32_t vl = (uint32_t)(left * (1 << 24)); | 
 | 4836 |             uint32_t vr = (uint32_t)(right * (1 << 24)); | 
 | 4837 |  | 
 | 4838 |             // Delegate volume control to effect in track effect chain if needed | 
 | 4839 |             // only one effect chain can be present on DirectOutputThread, so if | 
 | 4840 |             // there is one, the track is connected to it | 
 | 4841 |             if (!mEffectChains.isEmpty()) { | 
 | 4842 |                 mEffectChains[0]->setVolume_l(&vl, &vr); | 
 | 4843 |                 left = (float)vl / (1 << 24); | 
 | 4844 |                 right = (float)vr / (1 << 24); | 
 | 4845 |             } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4846 |             status_t result = mOutput->stream->setVolume(left, right); | 
 | 4847 |             ALOGE_IF(result != OK, "Error when setting output stream volume: %d", result); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4848 |         } | 
 | 4849 |     } | 
 | 4850 | } | 
 | 4851 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4852 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() | 
 | 4853 | { | 
 | 4854 |     sp<Track> previousTrack = mPreviousTrack.promote(); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4855 |     sp<Track> latestTrack = mActiveTracks.getLatest(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4856 |  | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4857 |     if (previousTrack != 0 && latestTrack != 0) { | 
 | 4858 |         if (mType == DIRECT) { | 
 | 4859 |             if (previousTrack.get() != latestTrack.get()) { | 
 | 4860 |                 mFlushPending = true; | 
 | 4861 |             } | 
 | 4862 |         } else /* mType == OFFLOAD */ { | 
 | 4863 |             if (previousTrack->sessionId() != latestTrack->sessionId()) { | 
 | 4864 |                 mFlushPending = true; | 
 | 4865 |             } | 
 | 4866 |         } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4867 |     } | 
 | 4868 |     PlaybackThread::onAddNewTrack_l(); | 
 | 4869 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4870 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4871 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( | 
 | 4872 |     Vector< sp<Track> > *tracksToRemove | 
 | 4873 | ) | 
 | 4874 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4875 |     size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4876 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4877 |     bool doHwPause = false; | 
 | 4878 |     bool doHwResume = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4879 |  | 
 | 4880 |     // find out which tracks need to be processed | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4881 |     for (const sp<Track> &t : mActiveTracks) { | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4882 |         if (t->isInvalid()) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4883 |             ALOGW("An invalidated track shouldn't be in active list"); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4884 |             tracksToRemove->add(t); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4885 |             continue; | 
 | 4886 |         } | 
 | 4887 |  | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4888 |         Track* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4889 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4890 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4891 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4892 |         // Only consider last track started for volume and mixer state control. | 
 | 4893 |         // In theory an older track could underrun and restart after the new one starts | 
 | 4894 |         // but as we only care about the transition phase between two tracks on a | 
 | 4895 |         // direct output, it is not a problem to ignore the underrun case. | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4896 |         sp<Track> l = mActiveTracks.getLatest(); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4897 |         bool last = l.get() == track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4898 |  | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4899 |         if (track->isPausing()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4900 |             track->setPaused(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4901 |             if (mHwSupportsPause && last && !mHwPaused) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4902 |                 doHwPause = true; | 
 | 4903 |                 mHwPaused = true; | 
 | 4904 |             } | 
 | 4905 |             tracksToRemove->add(track); | 
 | 4906 |         } else if (track->isFlushPending()) { | 
 | 4907 |             track->flushAck(); | 
 | 4908 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4909 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4910 |             } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4911 |         } else if (track->isResumePending()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4912 |             track->resumeAck(); | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 4913 |             if (last) { | 
 | 4914 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 4915 |                 if (mHwPaused) { | 
 | 4916 |                     doHwResume = true; | 
 | 4917 |                     mHwPaused = false; | 
 | 4918 |                 } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4919 |             } | 
 | 4920 |         } | 
 | 4921 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4922 |         // The first time a track is added we wait | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4923 |         // for all its buffers to be filled before processing it. | 
 | 4924 |         // Allow draining the buffer in case the client | 
 | 4925 |         // app does not call stop() and relies on underrun to stop: | 
 | 4926 |         // hence the test on (track->mRetryCount > 1). | 
 | 4927 |         // If retryCount<=1 then track is about to underrun and be removed. | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4928 |         // Do not use a high threshold for compressed audio. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4929 |         uint32_t minFrames; | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4930 |         if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4931 |             && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4932 |             minFrames = mNormalFrameCount; | 
 | 4933 |         } else { | 
 | 4934 |             minFrames = 1; | 
 | 4935 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4936 |  | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4937 |         if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && | 
 | 4938 |                 !track->isStopping_2() && !track->isStopped()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4939 |         { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4940 |             ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4941 |  | 
 | 4942 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 4943 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 4944 |                 if (last) { | 
 | 4945 |                     // make sure processVolume_l() will apply new volume even if 0 | 
 | 4946 |                     mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 4947 |                 } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4948 |                 if (!mHwSupportsPause) { | 
 | 4949 |                     track->resumeAck(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4950 |                 } | 
 | 4951 |             } | 
 | 4952 |  | 
 | 4953 |             // compute volume for this track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4954 |             processVolume_l(track, last); | 
 | 4955 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4956 |                 sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 4957 |                 if (previousTrack != 0) { | 
 | 4958 |                     if (track != previousTrack.get()) { | 
 | 4959 |                         // Flush any data still being written from last track | 
 | 4960 |                         mBytesRemaining = 0; | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4961 |                         // Invalidate previous track to force a seek when resuming. | 
 | 4962 |                         previousTrack->invalidate(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4963 |                     } | 
 | 4964 |                 } | 
 | 4965 |                 mPreviousTrack = track; | 
 | 4966 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4967 |                 // reset retry count | 
 | 4968 |                 track->mRetryCount = kMaxTrackRetriesDirect; | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 4969 |                 mActiveTrack = t; | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4970 |                 mixerStatus = MIXER_TRACKS_READY; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4971 |                 if (mHwPaused) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4972 |                     doHwResume = true; | 
 | 4973 |                     mHwPaused = false; | 
 | 4974 |                 } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4975 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4976 |         } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4977 |             // clear effect chain input buffer if the last active track started underruns | 
 | 4978 |             // to avoid sending previous audio buffer again to effects | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4979 |             if (!mEffectChains.isEmpty() && last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4980 |                 mEffectChains[0]->clearInputBuffer(); | 
 | 4981 |             } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4982 |             if (track->isStopping_1()) { | 
 | 4983 |                 track->mState = TrackBase::STOPPING_2; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4984 |                 if (last && mHwPaused) { | 
 | 4985 |                      doHwResume = true; | 
 | 4986 |                      mHwPaused = false; | 
 | 4987 |                  } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4988 |             } | 
 | 4989 |             if ((track->sharedBuffer() != 0) || track->isStopped() || | 
 | 4990 |                     track->isStopping_2() || track->isPaused()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4991 |                 // We have consumed all the buffers of this track. | 
 | 4992 |                 // Remove it from the list of active tracks. | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4993 |                 size_t audioHALFrames; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4994 |                 if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4995 |                     audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 4996 |                 } else { | 
 | 4997 |                     audioHALFrames = 0; | 
 | 4998 |                 } | 
 | 4999 |  | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5000 |                 int64_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5001 |                 if (mStandby || !last || | 
 | 5002 |                         track->presentationComplete(framesWritten, audioHALFrames)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5003 |                     if (track->isStopping_2()) { | 
 | 5004 |                         track->mState = TrackBase::STOPPED; | 
 | 5005 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5006 |                     if (track->isStopped()) { | 
 | 5007 |                         track->reset(); | 
 | 5008 |                     } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5009 |                     tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5010 |                 } | 
 | 5011 |             } else { | 
 | 5012 |                 // No buffers for this track. Give it a few chances to | 
 | 5013 |                 // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5014 |                 // Only consider last track started for mixer state control | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5015 |                 if (--(track->mRetryCount) <= 0) { | 
 | 5016 |                     ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5017 |                     tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5018 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 5019 |                     // it will then automatically call start() when data is available | 
| Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5020 |                     track->disable(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5021 |                 } else if (last) { | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5022 |                     ALOGW("pause because of UNDERRUN, framesReady = %zu," | 
 | 5023 |                             "minFrames = %u, mFormat = %#x", | 
 | 5024 |                             track->framesReady(), minFrames, mFormat); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5025 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5026 |                     if (mHwSupportsPause && !mHwPaused && !mStandby) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5027 |                         doHwPause = true; | 
 | 5028 |                         mHwPaused = true; | 
 | 5029 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5030 |                 } | 
 | 5031 |             } | 
 | 5032 |         } | 
 | 5033 |     } | 
 | 5034 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5035 |     // 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] | 5036 |     if (!mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5037 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 5038 |             if (mTracks[i]->isFlushPending()) { | 
 | 5039 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5040 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5041 |             } | 
 | 5042 |         } | 
 | 5043 |     } | 
 | 5044 |  | 
 | 5045 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 5046 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 5047 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 5048 |     // if resume is received before pause is executed. | 
 | 5049 |     if (mHwSupportsPause && !mStandby && | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5050 |             (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5051 |         status_t result = mOutput->stream->pause(); | 
 | 5052 |         ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5053 |     } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5054 |     if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5055 |         flushHw_l(); | 
 | 5056 |     } | 
 | 5057 |     if (mHwSupportsPause && !mStandby && doHwResume) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5058 |         status_t result = mOutput->stream->resume(); | 
 | 5059 |         ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5060 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5061 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5062 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5063 |  | 
 | 5064 |     return mixerStatus; | 
 | 5065 | } | 
 | 5066 |  | 
 | 5067 | void AudioFlinger::DirectOutputThread::threadLoop_mix() | 
 | 5068 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5069 |     size_t frameCount = mFrameCount; | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5070 |     int8_t *curBuf = (int8_t *)mSinkBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5071 |     // output audio to hardware | 
 | 5072 |     while (frameCount) { | 
| Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5073 |         AudioBufferProvider::Buffer buffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5074 |         buffer.frameCount = frameCount; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5075 |         status_t status = mActiveTrack->getNextBuffer(&buffer); | 
 | 5076 |         if (status != NO_ERROR || buffer.raw == NULL) { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5077 |             // no need to pad with 0 for compressed audio | 
 | 5078 |             if (audio_has_proportional_frames(mFormat)) { | 
 | 5079 |                 memset(curBuf, 0, frameCount * mFrameSize); | 
 | 5080 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5081 |             break; | 
 | 5082 |         } | 
 | 5083 |         memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
 | 5084 |         frameCount -= buffer.frameCount; | 
 | 5085 |         curBuf += buffer.frameCount * mFrameSize; | 
 | 5086 |         mActiveTrack->releaseBuffer(&buffer); | 
 | 5087 |     } | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5088 |     mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5089 |     mSleepTimeUs = 0; | 
 | 5090 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5091 |     mActiveTrack.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5092 | } | 
 | 5093 |  | 
 | 5094 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() | 
 | 5095 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5096 |     // do not write to HAL when paused | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5097 |     if (mHwPaused || (usesHwAvSync() && mStandby)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5098 |         mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5099 |         return; | 
 | 5100 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5101 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5102 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5103 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5104 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5105 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5106 |         } | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5107 |     } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5108 |         memset(mSinkBuffer, 0, mFrameCount * mFrameSize); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5109 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5110 |     } | 
 | 5111 | } | 
 | 5112 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5113 | void AudioFlinger::DirectOutputThread::threadLoop_exit() | 
 | 5114 | { | 
 | 5115 |     { | 
 | 5116 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5117 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 5118 |             if (mTracks[i]->isFlushPending()) { | 
 | 5119 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5120 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5121 |             } | 
 | 5122 |         } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5123 |         if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5124 |             flushHw_l(); | 
 | 5125 |         } | 
 | 5126 |     } | 
 | 5127 |     PlaybackThread::threadLoop_exit(); | 
 | 5128 | } | 
 | 5129 |  | 
 | 5130 | // must be called with thread mutex locked | 
 | 5131 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() | 
 | 5132 | { | 
 | 5133 |     bool trackPaused = false; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5134 |     bool trackStopped = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5135 |  | 
| vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5136 |     if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { | 
 | 5137 |         return !mStandby; | 
 | 5138 |     } | 
 | 5139 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5140 |     // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack | 
 | 5141 |     // after a timeout and we will enter standby then. | 
 | 5142 |     if (mTracks.size() > 0) { | 
 | 5143 |         trackPaused = mTracks[mTracks.size() - 1]->isPaused(); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5144 |         trackStopped = mTracks[mTracks.size() - 1]->isStopped() || | 
 | 5145 |                            mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5146 |     } | 
 | 5147 |  | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5148 |     return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5149 | } | 
 | 5150 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5151 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5152 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5153 |         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] | 5154 | { | 
| Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5155 |     if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { | 
 | 5156 |         return -1; | 
 | 5157 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5158 |     return 0; | 
 | 5159 | } | 
 | 5160 |  | 
 | 5161 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5162 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5163 | { | 
 | 5164 | } | 
 | 5165 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5166 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 5167 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 5168 |                                                               status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5169 | { | 
 | 5170 |     bool reconfig = false; | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5171 |     bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5172 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5173 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5174 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5175 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 5176 |     int value; | 
 | 5177 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 5178 |         // forward device change to effects that have requested to be | 
 | 5179 |         // aware of attached audio device. | 
 | 5180 |         if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5181 |             a2dpDeviceChanged = | 
 | 5182 |                     (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5183 |             mOutDevice = value; | 
 | 5184 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 5185 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
| Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5186 |             } | 
 | 5187 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5188 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5189 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 5190 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 5191 |         // size depends on frame count and correct behavior would not be garantied | 
 | 5192 |         // if frame count is changed after track creation | 
 | 5193 |         if (!mTracks.isEmpty()) { | 
 | 5194 |             status = INVALID_OPERATION; | 
 | 5195 |         } else { | 
 | 5196 |             reconfig = true; | 
 | 5197 |         } | 
 | 5198 |     } | 
 | 5199 |     if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5200 |         status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5201 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5202 |             mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5203 |             mStandby = true; | 
 | 5204 |             mBytesWritten = 0; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5205 |             status = mOutput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5206 |         } | 
 | 5207 |         if (status == NO_ERROR && reconfig) { | 
 | 5208 |             readOutputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5209 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5210 |         } | 
 | 5211 |     } | 
 | 5212 |  | 
| Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5213 |     return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5214 | } | 
 | 5215 |  | 
 | 5216 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const | 
 | 5217 | { | 
 | 5218 |     uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5219 |     if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5220 |         time = PlaybackThread::activeSleepTimeUs(); | 
 | 5221 |     } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5222 |         time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5223 |     } | 
 | 5224 |     return time; | 
 | 5225 | } | 
 | 5226 |  | 
 | 5227 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const | 
 | 5228 | { | 
 | 5229 |     uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5230 |     if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5231 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 5232 |     } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5233 |         time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5234 |     } | 
 | 5235 |     return time; | 
 | 5236 | } | 
 | 5237 |  | 
 | 5238 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const | 
 | 5239 | { | 
 | 5240 |     uint32_t time; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5241 |     if (audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5242 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
 | 5243 |     } else { | 
| Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5244 |         time = kDirectMinSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5245 |     } | 
 | 5246 |     return time; | 
 | 5247 | } | 
 | 5248 |  | 
 | 5249 | void AudioFlinger::DirectOutputThread::cacheParameters_l() | 
 | 5250 | { | 
 | 5251 |     PlaybackThread::cacheParameters_l(); | 
 | 5252 |  | 
 | 5253 |     // use shorter standby delay as on normal output to release | 
 | 5254 |     // hardware resources as soon as possible | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5255 |     // no delay on outputs with HW A/V sync | 
 | 5256 |     if (usesHwAvSync()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5257 |         mStandbyDelayNs = 0; | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5258 |     } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5259 |         mStandbyDelayNs = kOffloadStandbyDelayNs; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5260 |     } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5261 |         mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5262 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | } | 
 | 5264 |  | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5265 | void AudioFlinger::DirectOutputThread::flushHw_l() | 
 | 5266 | { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5267 |     mOutput->flush(); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5268 |     mHwPaused = false; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5269 |     mFlushPending = false; | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5270 | } | 
 | 5271 |  | 
| Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 5272 | int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const { | 
 | 5273 |     // If a VolumeShaper is active, we must wake up periodically to update volume. | 
 | 5274 |     const int64_t NS_PER_MS = 1000000; | 
 | 5275 |     return mVolumeShaperActive ? | 
 | 5276 |             kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); | 
 | 5277 | } | 
 | 5278 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5279 | // ---------------------------------------------------------------------------- | 
 | 5280 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5281 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5282 |         const wp<AudioFlinger::PlaybackThread>& playbackThread) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5283 |     :   Thread(false /*canCallJava*/), | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5284 |         mPlaybackThread(playbackThread), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5285 |         mWriteAckSequence(0), | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5286 |         mDrainSequence(0), | 
 | 5287 |         mAsyncError(false) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5288 | { | 
 | 5289 | } | 
 | 5290 |  | 
 | 5291 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() | 
 | 5292 | { | 
 | 5293 | } | 
 | 5294 |  | 
 | 5295 | void AudioFlinger::AsyncCallbackThread::onFirstRef() | 
 | 5296 | { | 
 | 5297 |     run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 5298 | } | 
 | 5299 |  | 
 | 5300 | bool AudioFlinger::AsyncCallbackThread::threadLoop() | 
 | 5301 | { | 
 | 5302 |     while (!exitPending()) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5303 |         uint32_t writeAckSequence; | 
 | 5304 |         uint32_t drainSequence; | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5305 |         bool asyncError; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5306 |  | 
 | 5307 |         { | 
 | 5308 |             Mutex::Autolock _l(mLock); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5309 |             while (!((mWriteAckSequence & 1) || | 
 | 5310 |                      (mDrainSequence & 1) || | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5311 |                      mAsyncError || | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5312 |                      exitPending())) { | 
 | 5313 |                 mWaitWorkCV.wait(mLock); | 
 | 5314 |             } | 
 | 5315 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5316 |             if (exitPending()) { | 
 | 5317 |                 break; | 
 | 5318 |             } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5319 |             ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", | 
 | 5320 |                   mWriteAckSequence, mDrainSequence); | 
 | 5321 |             writeAckSequence = mWriteAckSequence; | 
 | 5322 |             mWriteAckSequence &= ~1; | 
 | 5323 |             drainSequence = mDrainSequence; | 
 | 5324 |             mDrainSequence &= ~1; | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5325 |             asyncError = mAsyncError; | 
 | 5326 |             mAsyncError = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5327 |         } | 
 | 5328 |         { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5329 |             sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); | 
 | 5330 |             if (playbackThread != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5331 |                 if (writeAckSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5332 |                     playbackThread->resetWriteBlocked(writeAckSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5333 |                 } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5334 |                 if (drainSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5335 |                     playbackThread->resetDraining(drainSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5336 |                 } | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5337 |                 if (asyncError) { | 
 | 5338 |                     playbackThread->onAsyncError(); | 
 | 5339 |                 } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5340 |             } | 
 | 5341 |         } | 
 | 5342 |     } | 
 | 5343 |     return false; | 
 | 5344 | } | 
 | 5345 |  | 
 | 5346 | void AudioFlinger::AsyncCallbackThread::exit() | 
 | 5347 | { | 
 | 5348 |     ALOGV("AsyncCallbackThread::exit"); | 
 | 5349 |     Mutex::Autolock _l(mLock); | 
 | 5350 |     requestExit(); | 
 | 5351 |     mWaitWorkCV.broadcast(); | 
 | 5352 | } | 
 | 5353 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5354 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5355 | { | 
 | 5356 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5357 |     // bit 0 is cleared | 
 | 5358 |     mWriteAckSequence = sequence << 1; | 
 | 5359 | } | 
 | 5360 |  | 
 | 5361 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() | 
 | 5362 | { | 
 | 5363 |     Mutex::Autolock _l(mLock); | 
 | 5364 |     // ignore unexpected callbacks | 
 | 5365 |     if (mWriteAckSequence & 2) { | 
 | 5366 |         mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5367 |         mWaitWorkCV.signal(); | 
 | 5368 |     } | 
 | 5369 | } | 
 | 5370 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5371 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5372 | { | 
 | 5373 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5374 |     // bit 0 is cleared | 
 | 5375 |     mDrainSequence = sequence << 1; | 
 | 5376 | } | 
 | 5377 |  | 
 | 5378 | void AudioFlinger::AsyncCallbackThread::resetDraining() | 
 | 5379 | { | 
 | 5380 |     Mutex::Autolock _l(mLock); | 
 | 5381 |     // ignore unexpected callbacks | 
 | 5382 |     if (mDrainSequence & 2) { | 
 | 5383 |         mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5384 |         mWaitWorkCV.signal(); | 
 | 5385 |     } | 
 | 5386 | } | 
 | 5387 |  | 
| Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5388 | void AudioFlinger::AsyncCallbackThread::setAsyncError() | 
 | 5389 | { | 
 | 5390 |     Mutex::Autolock _l(mLock); | 
 | 5391 |     mAsyncError = true; | 
 | 5392 |     mWaitWorkCV.signal(); | 
 | 5393 | } | 
 | 5394 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5395 |  | 
 | 5396 | // ---------------------------------------------------------------------------- | 
 | 5397 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5398 |         AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) | 
 | 5399 |     :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5400 |         mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), | 
 | 5401 |         mOffloadUnderrunPosition(~0LL) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5402 | { | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5403 |     //FIXME: mStandby should be set to true by ThreadBase constructor | 
 | 5404 |     mStandby = true; | 
| Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5405 |     mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5406 | } | 
 | 5407 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5408 | void AudioFlinger::OffloadThread::threadLoop_exit() | 
 | 5409 | { | 
 | 5410 |     if (mFlushPending || mHwPaused) { | 
 | 5411 |         // If a flush is pending or track was paused, just discard buffered data | 
 | 5412 |         flushHw_l(); | 
 | 5413 |     } else { | 
 | 5414 |         mMixerStatus = MIXER_DRAIN_ALL; | 
 | 5415 |         threadLoop_drain(); | 
 | 5416 |     } | 
| Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5417 |     if (mUseAsyncWrite) { | 
 | 5418 |         ALOG_ASSERT(mCallbackThread != 0); | 
 | 5419 |         mCallbackThread->exit(); | 
 | 5420 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5421 |     PlaybackThread::threadLoop_exit(); | 
 | 5422 | } | 
 | 5423 |  | 
 | 5424 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( | 
 | 5425 |     Vector< sp<Track> > *tracksToRemove | 
 | 5426 | ) | 
 | 5427 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5428 |     size_t count = mActiveTracks.size(); | 
 | 5429 |  | 
 | 5430 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5431 |     bool doHwPause = false; | 
 | 5432 |     bool doHwResume = false; | 
 | 5433 |  | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5434 |     ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5435 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5436 |     // find out which tracks need to be processed | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5437 |     for (const sp<Track> &t : mActiveTracks) { | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5438 |         Track* const track = t.get(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5439 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5440 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5441 | #endif | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5442 |         // Only consider last track started for volume and mixer state control. | 
 | 5443 |         // In theory an older track could underrun and restart after the new one starts | 
 | 5444 |         // but as we only care about the transition phase between two tracks on a | 
 | 5445 |         // direct output, it is not a problem to ignore the underrun case. | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5446 |         sp<Track> l = mActiveTracks.getLatest(); | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5447 |         bool last = l.get() == track; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5448 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5449 |         if (track->isInvalid()) { | 
 | 5450 |             ALOGW("An invalidated track shouldn't be in active list"); | 
 | 5451 |             tracksToRemove->add(track); | 
 | 5452 |             continue; | 
 | 5453 |         } | 
 | 5454 |  | 
 | 5455 |         if (track->mState == TrackBase::IDLE) { | 
 | 5456 |             ALOGW("An idle track shouldn't be in active list"); | 
 | 5457 |             continue; | 
 | 5458 |         } | 
 | 5459 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5460 |         if (track->isPausing()) { | 
 | 5461 |             track->setPaused(); | 
 | 5462 |             if (last) { | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5463 |                 if (mHwSupportsPause && !mHwPaused) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5464 |                     doHwPause = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5465 |                     mHwPaused = true; | 
 | 5466 |                 } | 
 | 5467 |                 // If we were part way through writing the mixbuffer to | 
 | 5468 |                 // the HAL we must save this until we resume | 
 | 5469 |                 // BUG - this will be wrong if a different track is made active, | 
 | 5470 |                 // in that case we want to discard the pending data in the | 
 | 5471 |                 // mixbuffer and tell the client to present it again when the | 
 | 5472 |                 // track is resumed | 
 | 5473 |                 mPausedWriteLength = mCurrentWriteLength; | 
 | 5474 |                 mPausedBytesRemaining = mBytesRemaining; | 
 | 5475 |                 mBytesRemaining = 0;    // stop writing | 
 | 5476 |             } | 
 | 5477 |             tracksToRemove->add(track); | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5478 |         } else if (track->isFlushPending()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5479 |             if (track->isStopping_1()) { | 
 | 5480 |                 track->mRetryCount = kMaxTrackStopRetriesOffload; | 
 | 5481 |             } else { | 
 | 5482 |                 track->mRetryCount = kMaxTrackRetriesOffload; | 
 | 5483 |             } | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5484 |             track->flushAck(); | 
 | 5485 |             if (last) { | 
 | 5486 |                 mFlushPending = true; | 
 | 5487 |             } | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5488 |         } else if (track->isResumePending()){ | 
 | 5489 |             track->resumeAck(); | 
 | 5490 |             if (last) { | 
 | 5491 |                 if (mPausedBytesRemaining) { | 
 | 5492 |                     // Need to continue write that was interrupted | 
 | 5493 |                     mCurrentWriteLength = mPausedWriteLength; | 
 | 5494 |                     mBytesRemaining = mPausedBytesRemaining; | 
 | 5495 |                     mPausedBytesRemaining = 0; | 
 | 5496 |                 } | 
 | 5497 |                 if (mHwPaused) { | 
 | 5498 |                     doHwResume = true; | 
 | 5499 |                     mHwPaused = false; | 
 | 5500 |                     // threadLoop_mix() will handle the case that we need to | 
 | 5501 |                     // resume an interrupted write | 
 | 5502 |                 } | 
 | 5503 |                 // enable write to audio HAL | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5504 |                 mSleepTimeUs = 0; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5505 |  | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5506 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 5507 |  | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5508 |                 // Do not handle new data in this iteration even if track->framesReady() | 
 | 5509 |                 mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 5510 |             } | 
 | 5511 |         }  else if (track->framesReady() && track->isReady() && | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5512 |                 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5513 |             ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5514 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 5515 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5516 |                 if (last) { | 
 | 5517 |                     // make sure processVolume_l() will apply new volume even if 0 | 
 | 5518 |                     mLeftVolFloat = mRightVolFloat = -1.0; | 
 | 5519 |                 } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5520 |             } | 
 | 5521 |  | 
 | 5522 |             if (last) { | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5523 |                 sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 5524 |                 if (previousTrack != 0) { | 
 | 5525 |                     if (track != previousTrack.get()) { | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5526 |                         // Flush any data still being written from last track | 
 | 5527 |                         mBytesRemaining = 0; | 
 | 5528 |                         if (mPausedBytesRemaining) { | 
 | 5529 |                             // Last track was paused so we also need to flush saved | 
 | 5530 |                             // mixbuffer state and invalidate track so that it will | 
 | 5531 |                             // re-submit that unwritten data when it is next resumed | 
 | 5532 |                             mPausedBytesRemaining = 0; | 
 | 5533 |                             // Invalidate is a bit drastic - would be more efficient | 
 | 5534 |                             // to have a flag to tell client that some of the | 
 | 5535 |                             // previously written data was lost | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5536 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5537 |                         } | 
 | 5538 |                         // flush data already sent to the DSP if changing audio session as audio | 
 | 5539 |                         // comes from a different source. Also invalidate previous track to force a | 
 | 5540 |                         // seek when resuming. | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5541 |                         if (previousTrack->sessionId() != track->sessionId()) { | 
 | 5542 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5543 |                         } | 
 | 5544 |                     } | 
 | 5545 |                 } | 
 | 5546 |                 mPreviousTrack = track; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5547 |                 // reset retry count | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5548 |                 if (track->isStopping_1()) { | 
 | 5549 |                     track->mRetryCount = kMaxTrackStopRetriesOffload; | 
 | 5550 |                 } else { | 
 | 5551 |                     track->mRetryCount = kMaxTrackRetriesOffload; | 
 | 5552 |                 } | 
| Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5553 |                 mActiveTrack = t; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5554 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 5555 |             } | 
 | 5556 |         } else { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5557 |             ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5558 |             if (track->isStopping_1()) { | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5559 |                 if (--(track->mRetryCount) <= 0) { | 
 | 5560 |                     // Hardware buffer can hold a large amount of audio so we must | 
 | 5561 |                     // wait for all current track's data to drain before we say | 
 | 5562 |                     // that the track is stopped. | 
 | 5563 |                     if (mBytesRemaining == 0) { | 
 | 5564 |                         // Only start draining when all data in mixbuffer | 
 | 5565 |                         // has been written | 
 | 5566 |                         ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); | 
 | 5567 |                         track->mState = TrackBase::STOPPING_2; // so presentation completes after | 
 | 5568 |                         // drain do not drain if no data was ever sent to HAL (mStandby == true) | 
 | 5569 |                         if (last && !mStandby) { | 
 | 5570 |                             // do not modify drain sequence if we are already draining. This happens | 
 | 5571 |                             // when resuming from pause after drain. | 
 | 5572 |                             if ((mDrainSequence & 1) == 0) { | 
 | 5573 |                                 mSleepTimeUs = 0; | 
 | 5574 |                                 mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 5575 |                                 mixerStatus = MIXER_DRAIN_TRACK; | 
 | 5576 |                                 mDrainSequence += 2; | 
 | 5577 |                             } | 
 | 5578 |                             if (mHwPaused) { | 
 | 5579 |                                 // It is possible to move from PAUSED to STOPPING_1 without | 
 | 5580 |                                 // a resume so we must ensure hardware is running | 
 | 5581 |                                 doHwResume = true; | 
 | 5582 |                                 mHwPaused = false; | 
 | 5583 |                             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5584 |                         } | 
 | 5585 |                     } | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5586 |                 } else if (last) { | 
 | 5587 |                     ALOGV("stopping1 underrun retries left %d", track->mRetryCount); | 
 | 5588 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5589 |                 } | 
 | 5590 |             } else if (track->isStopping_2()) { | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5591 |                 // Drain has completed or we are in standby, signal presentation complete | 
 | 5592 |                 if (!(mDrainSequence & 1) || !last || mStandby) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5593 |                     track->mState = TrackBase::STOPPED; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5594 |                     uint32_t latency = 0; | 
 | 5595 |                     status_t result = mOutput->stream->getLatency(&latency); | 
 | 5596 |                     ALOGE_IF(result != OK, | 
 | 5597 |                             "Error when retrieving output stream latency: %d", result); | 
 | 5598 |                     size_t audioHALFrames = (latency * mSampleRate) / 1000; | 
| Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5599 |                     int64_t framesWritten = | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5600 |                             mBytesWritten / mOutput->getFrameSize(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5601 |                     track->presentationComplete(framesWritten, audioHALFrames); | 
 | 5602 |                     track->reset(); | 
 | 5603 |                     tracksToRemove->add(track); | 
 | 5604 |                 } | 
 | 5605 |             } else { | 
 | 5606 |                 // No buffers for this track. Give it a few chances to | 
 | 5607 |                 // fill a buffer, then remove it from active list. | 
 | 5608 |                 if (--(track->mRetryCount) <= 0) { | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5609 |                     bool running = false; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5610 |                     uint64_t position = 0; | 
 | 5611 |                     struct timespec unused; | 
 | 5612 |                     // The running check restarts the retry counter at least once. | 
 | 5613 |                     status_t ret = mOutput->stream->getPresentationPosition(&position, &unused); | 
 | 5614 |                     if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { | 
 | 5615 |                         running = true; | 
 | 5616 |                         mOffloadUnderrunPosition = position; | 
 | 5617 |                     } | 
 | 5618 |                     if (ret == NO_ERROR) { | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5619 |                         ALOGVV("underrun counter, running(%d): %lld vs %lld", running, | 
 | 5620 |                                 (long long)position, (long long)mOffloadUnderrunPosition); | 
 | 5621 |                     } | 
 | 5622 |                     if (running) { // still running, give us more time. | 
 | 5623 |                         track->mRetryCount = kMaxTrackRetriesOffload; | 
 | 5624 |                     } else { | 
 | 5625 |                         ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", | 
 | 5626 |                                 track->name()); | 
 | 5627 |                         tracksToRemove->add(track); | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 5628 |                         // tell client process that the track was disabled because of underrun; | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5629 |                         // it will then automatically call start() when data is available | 
 | 5630 |                         track->disable(); | 
 | 5631 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5632 |                 } else if (last){ | 
 | 5633 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 5634 |                 } | 
 | 5635 |             } | 
 | 5636 |         } | 
 | 5637 |         // compute volume for this track | 
 | 5638 |         processVolume_l(track, last); | 
 | 5639 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5640 |  | 
| Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5641 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 5642 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 5643 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 5644 |     // if resume is received before pause is executed. | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5645 |     if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5646 |         status_t result = mOutput->stream->pause(); | 
 | 5647 |         ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5648 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5649 |     if (mFlushPending) { | 
 | 5650 |         flushHw_l(); | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5651 |     } | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5652 |     if (!mStandby && doHwResume) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5653 |         status_t result = mOutput->stream->resume(); | 
 | 5654 |         ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5655 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5656 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5657 |     // remove all the tracks that need to be... | 
 | 5658 |     removeTracks_l(*tracksToRemove); | 
 | 5659 |  | 
 | 5660 |     return mixerStatus; | 
 | 5661 | } | 
 | 5662 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5663 | // must be called with thread mutex locked | 
 | 5664 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() | 
 | 5665 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5666 |     ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", | 
 | 5667 |           mWriteAckSequence, mDrainSequence); | 
 | 5668 |     if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5669 |         return true; | 
 | 5670 |     } | 
 | 5671 |     return false; | 
 | 5672 | } | 
 | 5673 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5674 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() | 
 | 5675 | { | 
 | 5676 |     Mutex::Autolock _l(mLock); | 
 | 5677 |     return waitingAsyncCallback_l(); | 
 | 5678 | } | 
 | 5679 |  | 
 | 5680 | void AudioFlinger::OffloadThread::flushHw_l() | 
 | 5681 | { | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5682 |     DirectOutputThread::flushHw_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5683 |     // Flush anything still waiting in the mixbuffer | 
 | 5684 |     mCurrentWriteLength = 0; | 
 | 5685 |     mBytesRemaining = 0; | 
 | 5686 |     mPausedWriteLength = 0; | 
 | 5687 |     mPausedBytesRemaining = 0; | 
| Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5688 |     // reset bytes written count to reflect that DSP buffers are empty after flush. | 
 | 5689 |     mBytesWritten = 0; | 
| Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5690 |     mOffloadUnderrunPosition = ~0LL; | 
| Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5691 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5692 |     if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5693 |         // discard any pending drain or write ack by incrementing sequence | 
 | 5694 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 5695 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5696 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5697 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 5698 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5699 |     } | 
 | 5700 | } | 
 | 5701 |  | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5702 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) | 
 | 5703 | { | 
 | 5704 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5705 |     if (PlaybackThread::invalidateTracks_l(streamType)) { | 
 | 5706 |         mFlushPending = true; | 
 | 5707 |     } | 
| Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5708 | } | 
 | 5709 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5710 | // ---------------------------------------------------------------------------- | 
 | 5711 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5712 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5713 |         AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5714 |     :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5715 |                     systemReady, DUPLICATING), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5716 |         mWaitTimeMs(UINT_MAX) | 
 | 5717 | { | 
 | 5718 |     addOutputTrack(mainThread); | 
 | 5719 | } | 
 | 5720 |  | 
 | 5721 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
 | 5722 | { | 
 | 5723 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5724 |         mOutputTracks[i]->destroy(); | 
 | 5725 |     } | 
 | 5726 | } | 
 | 5727 |  | 
 | 5728 | void AudioFlinger::DuplicatingThread::threadLoop_mix() | 
 | 5729 | { | 
 | 5730 |     // mix buffers... | 
 | 5731 |     if (outputsReady(outputTracks)) { | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5732 |         mAudioMixer->process(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5733 |     } else { | 
| Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5734 |         if (mMixerBufferValid) { | 
 | 5735 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 5736 |         } else { | 
 | 5737 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 5738 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5739 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5740 |     mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5741 |     writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5742 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5743 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5744 | } | 
 | 5745 |  | 
 | 5746 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() | 
 | 5747 | { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5748 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5749 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5750 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5751 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5752 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5753 |         } | 
 | 5754 |     } else if (mBytesWritten != 0) { | 
 | 5755 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 5756 |             writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5757 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5758 |         } else { | 
 | 5759 |             // flush remaining overflow buffers in output tracks | 
 | 5760 |             writeFrames = 0; | 
 | 5761 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5762 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5763 |     } | 
 | 5764 | } | 
 | 5765 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5766 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5767 | { | 
 | 5768 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5769 |         outputTracks[i]->write(mSinkBuffer, writeFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5770 |     } | 
| Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5771 |     mStandby = false; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5772 |     return (ssize_t)mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5773 | } | 
 | 5774 |  | 
 | 5775 | void AudioFlinger::DuplicatingThread::threadLoop_standby() | 
 | 5776 | { | 
 | 5777 |     // DuplicatingThread implements standby by stopping all tracks | 
 | 5778 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 5779 |         outputTracks[i]->stop(); | 
 | 5780 |     } | 
 | 5781 | } | 
 | 5782 |  | 
 | 5783 | void AudioFlinger::DuplicatingThread::saveOutputTracks() | 
 | 5784 | { | 
 | 5785 |     outputTracks = mOutputTracks; | 
 | 5786 | } | 
 | 5787 |  | 
 | 5788 | void AudioFlinger::DuplicatingThread::clearOutputTracks() | 
 | 5789 | { | 
 | 5790 |     outputTracks.clear(); | 
 | 5791 | } | 
 | 5792 |  | 
 | 5793 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
 | 5794 | { | 
 | 5795 |     Mutex::Autolock _l(mLock); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5796 |     // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. | 
 | 5797 |     // Adjust for thread->sampleRate() to determine minimum buffer frame count. | 
 | 5798 |     // Then triple buffer because Threads do not run synchronously and may not be clock locked. | 
 | 5799 |     const size_t frameCount = | 
 | 5800 |             3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); | 
 | 5801 |     // TODO: Consider asynchronous sample rate conversion to handle clock disparity | 
 | 5802 |     // from different OutputTracks and their associated MixerThreads (e.g. one may | 
 | 5803 |     // nearly empty and the other may be dropping data). | 
 | 5804 |  | 
 | 5805 |     sp<OutputTrack> outputTrack = new OutputTrack(thread, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5806 |                                             this, | 
 | 5807 |                                             mSampleRate, | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5808 |                                             mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5809 |                                             mChannelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5810 |                                             frameCount, | 
 | 5811 |                                             IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5812 |     status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; | 
 | 5813 |     if (status != NO_ERROR) { | 
 | 5814 |         ALOGE("addOutputTrack() initCheck failed %d", status); | 
 | 5815 |         return; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5816 |     } | 
| Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5817 |     thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); | 
 | 5818 |     mOutputTracks.add(outputTrack); | 
 | 5819 |     ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); | 
 | 5820 |     updateWaitTime_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5821 | } | 
 | 5822 |  | 
 | 5823 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
 | 5824 | { | 
 | 5825 |     Mutex::Autolock _l(mLock); | 
 | 5826 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5827 |         if (mOutputTracks[i]->thread() == thread) { | 
 | 5828 |             mOutputTracks[i]->destroy(); | 
 | 5829 |             mOutputTracks.removeAt(i); | 
 | 5830 |             updateWaitTime_l(); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5831 |             if (thread->getOutput() == mOutput) { | 
 | 5832 |                 mOutput = NULL; | 
 | 5833 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5834 |             return; | 
 | 5835 |         } | 
 | 5836 |     } | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5837 |     ALOGV("removeOutputTrack(): unknown thread: %p", thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5838 | } | 
 | 5839 |  | 
 | 5840 | // caller must hold mLock | 
 | 5841 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() | 
 | 5842 | { | 
 | 5843 |     mWaitTimeMs = UINT_MAX; | 
 | 5844 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5845 |         sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
 | 5846 |         if (strong != 0) { | 
 | 5847 |             uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
 | 5848 |             if (waitTimeMs < mWaitTimeMs) { | 
 | 5849 |                 mWaitTimeMs = waitTimeMs; | 
 | 5850 |             } | 
 | 5851 |         } | 
 | 5852 |     } | 
 | 5853 | } | 
 | 5854 |  | 
 | 5855 |  | 
 | 5856 | bool AudioFlinger::DuplicatingThread::outputsReady( | 
 | 5857 |         const SortedVector< sp<OutputTrack> > &outputTracks) | 
 | 5858 | { | 
 | 5859 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 5860 |         sp<ThreadBase> thread = outputTracks[i]->thread().promote(); | 
 | 5861 |         if (thread == 0) { | 
 | 5862 |             ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
 | 5863 |                     outputTracks[i].get()); | 
 | 5864 |             return false; | 
 | 5865 |         } | 
 | 5866 |         PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
 | 5867 |         // see note at standby() declaration | 
 | 5868 |         if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
 | 5869 |             ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
 | 5870 |                     thread.get()); | 
 | 5871 |             return false; | 
 | 5872 |         } | 
 | 5873 |     } | 
 | 5874 |     return true; | 
 | 5875 | } | 
 | 5876 |  | 
 | 5877 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const | 
 | 5878 | { | 
 | 5879 |     return (mWaitTimeMs * 1000) / 2; | 
 | 5880 | } | 
 | 5881 |  | 
 | 5882 | void AudioFlinger::DuplicatingThread::cacheParameters_l() | 
 | 5883 | { | 
 | 5884 |     // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
 | 5885 |     updateWaitTime_l(); | 
 | 5886 |  | 
 | 5887 |     MixerThread::cacheParameters_l(); | 
 | 5888 | } | 
 | 5889 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 5890 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5891 | // ---------------------------------------------------------------------------- | 
 | 5892 | //      Record | 
 | 5893 | // ---------------------------------------------------------------------------- | 
 | 5894 |  | 
 | 5895 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
 | 5896 |                                          AudioStreamIn *input, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5897 |                                          audio_io_handle_t id, | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5898 |                                          audio_devices_t outDevice, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5899 |                                          audio_devices_t inDevice, | 
 | 5900 |                                          bool systemReady | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5901 | #ifdef TEE_SINK | 
 | 5902 |                                          , const sp<NBAIO_Sink>& teeSink | 
 | 5903 | #endif | 
 | 5904 |                                          ) : | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5905 |     ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5906 |     mInput(input), mRsmpInBuffer(NULL), | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 5907 |     // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5908 |     mRsmpInRear(0) | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5909 | #ifdef TEE_SINK | 
 | 5910 |     , mTeeSink(teeSink) | 
 | 5911 | #endif | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5912 |     , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, | 
 | 5913 |             "RecordThreadRO", MemoryHeapBase::READ_ONLY)) | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5914 |     // mFastCapture below | 
 | 5915 |     , mFastCaptureFutex(0) | 
 | 5916 |     // mInputSource | 
 | 5917 |     // mPipeSink | 
 | 5918 |     // mPipeSource | 
 | 5919 |     , mPipeFramesP2(0) | 
 | 5920 |     // mPipeMemory | 
 | 5921 |     // mFastCaptureNBLogWriter | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5922 |     , mFastTrackAvail(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5923 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5924 |     snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); | 
 | 5925 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5926 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5927 |     readInputParameters_l(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5928 |  | 
 | 5929 |     // create an NBAIO source for the HAL input stream, and negotiate | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 5930 |     mInputSource = new AudioStreamInSource(input->stream); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5931 |     size_t numCounterOffers = 0; | 
 | 5932 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5933 | #if !LOG_NDEBUG | 
 | 5934 |     ssize_t index = | 
 | 5935 | #else | 
 | 5936 |     (void) | 
 | 5937 | #endif | 
 | 5938 |             mInputSource->negotiate(offers, 1, NULL, numCounterOffers); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5939 |     ALOG_ASSERT(index == 0); | 
 | 5940 |  | 
 | 5941 |     // initialize fast capture depending on configuration | 
 | 5942 |     bool initFastCapture; | 
 | 5943 |     switch (kUseFastCapture) { | 
 | 5944 |     case FastCapture_Never: | 
 | 5945 |         initFastCapture = false; | 
 | 5946 |         break; | 
 | 5947 |     case FastCapture_Always: | 
 | 5948 |         initFastCapture = true; | 
 | 5949 |         break; | 
 | 5950 |     case FastCapture_Static: | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5951 |         initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5952 |         break; | 
 | 5953 |     // case FastCapture_Dynamic: | 
 | 5954 |     } | 
 | 5955 |  | 
 | 5956 |     if (initFastCapture) { | 
| Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5957 |         // 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] | 5958 |         NBAIO_Format format = mInputSource->format(); | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 5959 |         // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread | 
 | 5960 |         size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5961 |         size_t pipeSize = pipeFramesP2 * Format_frameSize(format); | 
 | 5962 |         void *pipeBuffer; | 
 | 5963 |         const sp<MemoryDealer> roHeap(readOnlyHeap()); | 
 | 5964 |         sp<IMemory> pipeMemory; | 
 | 5965 |         if ((roHeap == 0) || | 
 | 5966 |                 (pipeMemory = roHeap->allocate(pipeSize)) == 0 || | 
 | 5967 |                 (pipeBuffer = pipeMemory->pointer()) == NULL) { | 
 | 5968 |             ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); | 
 | 5969 |             goto failed; | 
 | 5970 |         } | 
 | 5971 |         // pipe will be shared directly with fast clients, so clear to avoid leaking old information | 
 | 5972 |         memset(pipeBuffer, 0, pipeSize); | 
 | 5973 |         Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); | 
 | 5974 |         const NBAIO_Format offers[1] = {format}; | 
 | 5975 |         size_t numCounterOffers = 0; | 
 | 5976 |         ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5977 |         ALOG_ASSERT(index == 0); | 
 | 5978 |         mPipeSink = pipe; | 
 | 5979 |         PipeReader *pipeReader = new PipeReader(*pipe); | 
 | 5980 |         numCounterOffers = 0; | 
 | 5981 |         index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5982 |         ALOG_ASSERT(index == 0); | 
 | 5983 |         mPipeSource = pipeReader; | 
 | 5984 |         mPipeFramesP2 = pipeFramesP2; | 
 | 5985 |         mPipeMemory = pipeMemory; | 
 | 5986 |  | 
 | 5987 |         // create fast capture | 
 | 5988 |         mFastCapture = new FastCapture(); | 
 | 5989 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5990 | #ifdef STATE_QUEUE_DUMP | 
 | 5991 |         // FIXME | 
 | 5992 | #endif | 
 | 5993 |         FastCaptureState *state = sq->begin(); | 
 | 5994 |         state->mCblk = NULL; | 
 | 5995 |         state->mInputSource = mInputSource.get(); | 
 | 5996 |         state->mInputSourceGen++; | 
 | 5997 |         state->mPipeSink = pipe; | 
 | 5998 |         state->mPipeSinkGen++; | 
 | 5999 |         state->mFrameCount = mFrameCount; | 
 | 6000 |         state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 6001 |         // already done in constructor initialization list | 
 | 6002 |         //mFastCaptureFutex = 0; | 
 | 6003 |         state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 6004 |         state->mColdGen++; | 
 | 6005 |         state->mDumpState = &mFastCaptureDumpState; | 
 | 6006 | #ifdef TEE_SINK | 
 | 6007 |         // FIXME | 
 | 6008 | #endif | 
 | 6009 |         mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); | 
 | 6010 |         state->mNBLogWriter = mFastCaptureNBLogWriter.get(); | 
 | 6011 |         sq->end(); | 
 | 6012 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 6013 |  | 
 | 6014 |         // start the fast capture | 
 | 6015 |         mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 6016 |         pid_t tid = mFastCapture->getTid(); | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 6017 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture, false); | 
| Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 6018 |         stream()->setHalThreadPriority(kPriorityFastCapture); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6019 | #ifdef AUDIO_WATCHDOG | 
 | 6020 |         // FIXME | 
 | 6021 | #endif | 
 | 6022 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6023 |         mFastTrackAvail = true; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6024 |     } | 
 | 6025 | failed: ; | 
 | 6026 |  | 
 | 6027 |     // FIXME mNormalSource | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6028 | } | 
 | 6029 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6030 | AudioFlinger::RecordThread::~RecordThread() | 
 | 6031 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6032 |     if (mFastCapture != 0) { | 
 | 6033 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 6034 |         FastCaptureState *state = sq->begin(); | 
 | 6035 |         if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 6036 |             int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 6037 |             if (old == -1) { | 
 | 6038 |                 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 6039 |             } | 
 | 6040 |         } | 
 | 6041 |         state->mCommand = FastCaptureState::EXIT; | 
 | 6042 |         sq->end(); | 
 | 6043 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 6044 |         mFastCapture->join(); | 
 | 6045 |         mFastCapture.clear(); | 
 | 6046 |     } | 
 | 6047 |     mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 6048 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6049 |     free(mRsmpInBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6050 | } | 
 | 6051 |  | 
 | 6052 | void AudioFlinger::RecordThread::onFirstRef() | 
 | 6053 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6054 |     run(mThreadName, PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6055 | } | 
 | 6056 |  | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 6057 | void AudioFlinger::RecordThread::preExit() | 
 | 6058 | { | 
 | 6059 |     ALOGV("  preExit()"); | 
 | 6060 |     Mutex::Autolock _l(mLock); | 
 | 6061 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6062 |         sp<RecordTrack> track = mTracks[i]; | 
 | 6063 |         track->invalidate(); | 
 | 6064 |     } | 
 | 6065 |     mActiveTracks.clear(); | 
 | 6066 |     mStartStopCond.broadcast(); | 
 | 6067 | } | 
 | 6068 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6069 | bool AudioFlinger::RecordThread::threadLoop() | 
 | 6070 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6071 |     nsecs_t lastWarning = 0; | 
 | 6072 |  | 
 | 6073 |     inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6074 |  | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6075 | reacquire_wakelock: | 
 | 6076 |     sp<RecordTrack> activeTrack; | 
 | 6077 |     { | 
 | 6078 |         Mutex::Autolock _l(mLock); | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6079 |         acquireWakeLock_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6080 |     } | 
 | 6081 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6082 |     // used to request a deferred sleep, to be executed later while mutex is unlocked | 
 | 6083 |     uint32_t sleepUs = 0; | 
 | 6084 |  | 
 | 6085 |     // loop while there is work to do | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6086 |     for (;;) { | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6087 |         Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6088 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6089 |         // activeTracks accumulates a copy of a subset of mActiveTracks | 
 | 6090 |         Vector< sp<RecordTrack> > activeTracks; | 
 | 6091 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6092 |         // reference to the (first and only) active fast track | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6093 |         sp<RecordTrack> fastTrack; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6094 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6095 |         // reference to a fast track which is about to be removed | 
 | 6096 |         sp<RecordTrack> fastTrackToRemove; | 
 | 6097 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6098 |         { // scope for mLock | 
 | 6099 |             Mutex::Autolock _l(mLock); | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6100 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6101 |             processConfigEvents_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6102 |  | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6103 |             // check exitPending here because checkForNewParameters_l() and | 
 | 6104 |             // checkForNewParameters_l() can temporarily release mLock | 
 | 6105 |             if (exitPending()) { | 
 | 6106 |                 break; | 
 | 6107 |             } | 
 | 6108 |  | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6109 |             // sleep with mutex unlocked | 
 | 6110 |             if (sleepUs > 0) { | 
| Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 6111 |                 ATRACE_BEGIN("sleepC"); | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6112 |                 mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); | 
 | 6113 |                 ATRACE_END(); | 
 | 6114 |                 sleepUs = 0; | 
 | 6115 |                 continue; | 
 | 6116 |             } | 
 | 6117 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6118 |             // if no active track(s), then standby and release wakelock | 
 | 6119 |             size_t size = mActiveTracks.size(); | 
 | 6120 |             if (size == 0) { | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6121 |                 standbyIfNotAlreadyInStandby(); | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6122 |                 // exitPending() can't become true here | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6123 |                 releaseWakeLock_l(); | 
 | 6124 |                 ALOGV("RecordThread: loop stopping"); | 
 | 6125 |                 // go to sleep | 
 | 6126 |                 mWaitWorkCV.wait(mLock); | 
 | 6127 |                 ALOGV("RecordThread: loop starting"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6128 |                 goto reacquire_wakelock; | 
 | 6129 |             } | 
 | 6130 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6131 |             bool doBroadcast = false; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6132 |             bool allStopped = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6133 |             for (size_t i = 0; i < size; ) { | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6134 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6135 |                 activeTrack = mActiveTracks[i]; | 
 | 6136 |                 if (activeTrack->isTerminated()) { | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6137 |                     if (activeTrack->isFastTrack()) { | 
 | 6138 |                         ALOG_ASSERT(fastTrackToRemove == 0); | 
 | 6139 |                         fastTrackToRemove = activeTrack; | 
 | 6140 |                     } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6141 |                     removeTrack_l(activeTrack); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6142 |                     mActiveTracks.remove(activeTrack); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6143 |                     size--; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6144 |                     continue; | 
 | 6145 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6146 |  | 
 | 6147 |                 TrackBase::track_state activeTrackState = activeTrack->mState; | 
 | 6148 |                 switch (activeTrackState) { | 
 | 6149 |  | 
 | 6150 |                 case TrackBase::PAUSING: | 
 | 6151 |                     mActiveTracks.remove(activeTrack); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6152 |                     doBroadcast = true; | 
 | 6153 |                     size--; | 
 | 6154 |                     continue; | 
 | 6155 |  | 
 | 6156 |                 case TrackBase::STARTING_1: | 
 | 6157 |                     sleepUs = 10000; | 
 | 6158 |                     i++; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6159 |                     allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6160 |                     continue; | 
 | 6161 |  | 
 | 6162 |                 case TrackBase::STARTING_2: | 
 | 6163 |                     doBroadcast = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6164 |                     mStandby = false; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6165 |                     activeTrack->mState = TrackBase::ACTIVE; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6166 |                     allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6167 |                     break; | 
 | 6168 |  | 
 | 6169 |                 case TrackBase::ACTIVE: | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6170 |                     allStopped = false; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6171 |                     break; | 
 | 6172 |  | 
 | 6173 |                 case TrackBase::IDLE: | 
 | 6174 |                     i++; | 
 | 6175 |                     continue; | 
 | 6176 |  | 
 | 6177 |                 default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6178 |                     LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6179 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6180 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6181 |                 activeTracks.add(activeTrack); | 
 | 6182 |                 i++; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6183 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6184 |                 if (activeTrack->isFastTrack()) { | 
 | 6185 |                     ALOG_ASSERT(!mFastTrackAvail); | 
 | 6186 |                     ALOG_ASSERT(fastTrack == 0); | 
 | 6187 |                     fastTrack = activeTrack; | 
 | 6188 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6189 |             } | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6190 |  | 
| Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6191 |             mActiveTracks.updatePowerState(this); | 
 | 6192 |  | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6193 |             if (allStopped) { | 
 | 6194 |                 standbyIfNotAlreadyInStandby(); | 
 | 6195 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6196 |             if (doBroadcast) { | 
 | 6197 |                 mStartStopCond.broadcast(); | 
 | 6198 |             } | 
 | 6199 |  | 
 | 6200 |             // sleep if there are no active tracks to process | 
 | 6201 |             if (activeTracks.size() == 0) { | 
 | 6202 |                 if (sleepUs == 0) { | 
 | 6203 |                     sleepUs = kRecordThreadSleepUs; | 
 | 6204 |                 } | 
 | 6205 |                 continue; | 
 | 6206 |             } | 
 | 6207 |             sleepUs = 0; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6208 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6209 |             lockEffectChains_l(effectChains); | 
 | 6210 |         } | 
 | 6211 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6212 |         // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 | 
| Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6213 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6214 |         size_t size = effectChains.size(); | 
 | 6215 |         for (size_t i = 0; i < size; i++) { | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6216 |             // thread mutex is not locked, but effect chain is locked | 
 | 6217 |             effectChains[i]->process_l(); | 
 | 6218 |         } | 
 | 6219 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6220 |         // 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] | 6221 |         if (mFastCapture != 0) { | 
 | 6222 |             FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 6223 |             FastCaptureState *state = sq->begin(); | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6224 |             bool didModify = false; | 
 | 6225 |             FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6226 |             if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && | 
 | 6227 |                     (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { | 
 | 6228 |                 if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 6229 |                     int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 6230 |                     if (old == -1) { | 
 | 6231 |                         (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 6232 |                     } | 
 | 6233 |                 } | 
 | 6234 |                 state->mCommand = FastCaptureState::READ_WRITE; | 
 | 6235 | #if 0   // FIXME | 
 | 6236 |                 mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6237 |                         FastThreadDumpState::kSamplingNforLowRamDevice : | 
 | 6238 |                         FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6239 | #endif | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6240 |                 didModify = true; | 
 | 6241 |             } | 
 | 6242 |             audio_track_cblk_t *cblkOld = state->mCblk; | 
 | 6243 |             audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; | 
 | 6244 |             if (cblkNew != cblkOld) { | 
 | 6245 |                 state->mCblk = cblkNew; | 
 | 6246 |                 // block until acked if removing a fast track | 
 | 6247 |                 if (cblkOld != NULL) { | 
 | 6248 |                     block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; | 
 | 6249 |                 } | 
 | 6250 |                 didModify = true; | 
 | 6251 |             } | 
 | 6252 |             sq->end(didModify); | 
 | 6253 |             if (didModify) { | 
 | 6254 |                 sq->push(block); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6255 | #if 0 | 
 | 6256 |                 if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 6257 |                     mNormalSource = mPipeSource; | 
 | 6258 |                 } | 
 | 6259 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6260 |             } | 
 | 6261 |         } | 
 | 6262 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6263 |         // now run the fast track destructor with thread mutex unlocked | 
 | 6264 |         fastTrackToRemove.clear(); | 
 | 6265 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6266 |         // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. | 
 | 6267 |         // Only the client(s) that are too slow will overrun. But if even the fastest client is too | 
 | 6268 |         // slow, then this RecordThread will overrun by not calling HAL read often enough. | 
 | 6269 |         // If destination is non-contiguous, first read past the nominal end of buffer, then | 
 | 6270 |         // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated. | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6271 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6272 |         int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6273 |         ssize_t framesRead; | 
 | 6274 |  | 
 | 6275 |         // If an NBAIO source is present, use it to read the normal capture's data | 
 | 6276 |         if (mPipeSource != 0) { | 
 | 6277 |             size_t framesToRead = mBufferSize / mFrameSize; | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6278 |             framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6279 |             framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6280 |                     framesToRead); | 
| Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6281 |             // since pipe is non-blocking, simulate blocking input by waiting for 1/2 of | 
 | 6282 |             // buffer size or at least for 20ms. | 
 | 6283 |             size_t sleepFrames = max( | 
 | 6284 |                     min(mPipeFramesP2, mRsmpInFramesP2) / 2, FMS_20 * mSampleRate / 1000); | 
 | 6285 |             if (framesRead <= (ssize_t) sleepFrames) { | 
 | 6286 |                 sleepUs = (sleepFrames * 1000000LL) / mSampleRate; | 
 | 6287 |             } | 
 | 6288 |             if (framesRead < 0) { | 
 | 6289 |                 status_t status = (status_t) framesRead; | 
 | 6290 |                 switch (status) { | 
 | 6291 |                 case OVERRUN: | 
 | 6292 |                     ALOGW("overrun on read from pipe"); | 
 | 6293 |                     framesRead = 0; | 
 | 6294 |                     break; | 
 | 6295 |                 case NEGOTIATE: | 
 | 6296 |                     ALOGE("re-negotiation is needed"); | 
 | 6297 |                     framesRead = -1;  // Will cause an attempt to recover. | 
 | 6298 |                     break; | 
 | 6299 |                 default: | 
 | 6300 |                     ALOGE("unknown error %d on read from pipe", status); | 
 | 6301 |                     break; | 
 | 6302 |                 } | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6303 |             } | 
 | 6304 |         // otherwise use the HAL / AudioStreamIn directly | 
 | 6305 |         } else { | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6306 |             ATRACE_BEGIN("read"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6307 |             size_t bytesRead; | 
 | 6308 |             status_t result = mInput->stream->read( | 
 | 6309 |                     (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); | 
| Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6310 |             ATRACE_END(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6311 |             if (result < 0) { | 
 | 6312 |                 framesRead = result; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6313 |             } else { | 
 | 6314 |                 framesRead = bytesRead / mFrameSize; | 
 | 6315 |             } | 
 | 6316 |         } | 
 | 6317 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6318 |         // Update server timestamp with server stats | 
 | 6319 |         // systemTime() is optional if the hardware supports timestamps. | 
 | 6320 |         mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; | 
 | 6321 |         mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); | 
 | 6322 |  | 
 | 6323 |         // Update server timestamp with kernel stats | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6324 |         if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6325 |             int64_t position, time; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6326 |             int ret = mInput->stream->getCapturePosition(&position, &time); | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6327 |             if (ret == NO_ERROR) { | 
 | 6328 |                 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; | 
 | 6329 |                 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; | 
 | 6330 |                 // Note: In general record buffers should tend to be empty in | 
 | 6331 |                 // a properly running pipeline. | 
 | 6332 |                 // | 
 | 6333 |                 // Also, it is not advantageous to call get_presentation_position during the read | 
 | 6334 |                 // as the read obtains a lock, preventing the timestamp call from executing. | 
 | 6335 |             } | 
 | 6336 |         } | 
 | 6337 |         // Use this to track timestamp information | 
 | 6338 |         // ALOGD("%s", mTimestamp.toString().c_str()); | 
 | 6339 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6340 |         if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6341 |             ALOGE("read failed: framesRead=%zd", framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6342 |             // Force input into standby so that it tries to recover at next read attempt | 
 | 6343 |             inputStandBy(); | 
 | 6344 |             sleepUs = kRecordThreadSleepUs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6345 |         } | 
 | 6346 |         if (framesRead <= 0) { | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6347 |             goto unlock; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6348 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6349 |         ALOG_ASSERT(framesRead > 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6350 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6351 |         if (mTeeSink != 0) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6352 |             (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6353 |         } | 
 | 6354 |         // 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] | 6355 |         { | 
 | 6356 |             size_t part1 = mRsmpInFramesP2 - rear; | 
 | 6357 |             if ((size_t) framesRead > part1) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6358 |                 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6359 |                         (framesRead - part1) * mFrameSize); | 
 | 6360 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6361 |         } | 
 | 6362 |         rear = mRsmpInRear += framesRead; | 
 | 6363 |  | 
 | 6364 |         size = activeTracks.size(); | 
 | 6365 |         // loop over each active track | 
 | 6366 |         for (size_t i = 0; i < size; i++) { | 
 | 6367 |             activeTrack = activeTracks[i]; | 
 | 6368 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6369 |             // skip fast tracks, as those are handled directly by FastCapture | 
 | 6370 |             if (activeTrack->isFastTrack()) { | 
 | 6371 |                 continue; | 
 | 6372 |             } | 
 | 6373 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6374 |             // TODO: This code probably should be moved to RecordTrack. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6375 |             // TODO: Update the activeTrack buffer converter in case of reconfigure. | 
 | 6376 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6377 |             enum { | 
 | 6378 |                 OVERRUN_UNKNOWN, | 
 | 6379 |                 OVERRUN_TRUE, | 
 | 6380 |                 OVERRUN_FALSE | 
 | 6381 |             } overrun = OVERRUN_UNKNOWN; | 
 | 6382 |  | 
 | 6383 |             // loop over getNextBuffer to handle circular sink | 
 | 6384 |             for (;;) { | 
 | 6385 |  | 
 | 6386 |                 activeTrack->mSink.frameCount = ~0; | 
 | 6387 |                 status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); | 
 | 6388 |                 size_t framesOut = activeTrack->mSink.frameCount; | 
 | 6389 |                 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); | 
 | 6390 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6391 |                 // check available frames and handle overrun conditions | 
 | 6392 |                 // if the record track isn't draining fast enough. | 
 | 6393 |                 bool hasOverrun; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6394 |                 size_t framesIn; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6395 |                 activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); | 
 | 6396 |                 if (hasOverrun) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6397 |                     overrun = OVERRUN_TRUE; | 
 | 6398 |                 } | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6399 |                 if (framesOut == 0 || framesIn == 0) { | 
 | 6400 |                     break; | 
 | 6401 |                 } | 
 | 6402 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6403 |                 // Don't allow framesOut to be larger than what is possible with resampling | 
 | 6404 |                 // from framesIn. | 
 | 6405 |                 // This isn't strictly necessary but helps limit buffer resizing in | 
 | 6406 |                 // RecordBufferConverter.  TODO: remove when no longer needed. | 
 | 6407 |                 framesOut = min(framesOut, | 
 | 6408 |                         destinationFramesPossible( | 
 | 6409 |                                 framesIn, mSampleRate, activeTrack->mSampleRate)); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6410 |                 // process frames from the RecordThread buffer provider to the RecordTrack buffer | 
 | 6411 |                 framesOut = activeTrack->mRecordBufferConverter->convert( | 
 | 6412 |                         activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6413 |  | 
 | 6414 |                 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { | 
 | 6415 |                     overrun = OVERRUN_FALSE; | 
 | 6416 |                 } | 
 | 6417 |  | 
 | 6418 |                 if (activeTrack->mFramesToDrop == 0) { | 
 | 6419 |                     if (framesOut > 0) { | 
 | 6420 |                         activeTrack->mSink.frameCount = framesOut; | 
 | 6421 |                         activeTrack->releaseBuffer(&activeTrack->mSink); | 
 | 6422 |                     } | 
 | 6423 |                 } else { | 
 | 6424 |                     // FIXME could do a partial drop of framesOut | 
 | 6425 |                     if (activeTrack->mFramesToDrop > 0) { | 
 | 6426 |                         activeTrack->mFramesToDrop -= framesOut; | 
 | 6427 |                         if (activeTrack->mFramesToDrop <= 0) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6428 |                             activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6429 |                         } | 
 | 6430 |                     } else { | 
 | 6431 |                         activeTrack->mFramesToDrop += framesOut; | 
 | 6432 |                         if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || | 
 | 6433 |                                 activeTrack->mSyncStartEvent->isCancelled()) { | 
 | 6434 |                             ALOGW("Synced record %s, session %d, trigger session %d", | 
 | 6435 |                                   (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", | 
 | 6436 |                                   activeTrack->sessionId(), | 
 | 6437 |                                   (activeTrack->mSyncStartEvent != 0) ? | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6438 |                                           activeTrack->mSyncStartEvent->triggerSession() : | 
 | 6439 |                                           AUDIO_SESSION_NONE); | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6440 |                             activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6441 |                         } | 
 | 6442 |                     } | 
 | 6443 |                 } | 
 | 6444 |  | 
 | 6445 |                 if (framesOut == 0) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6446 |                     break; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6447 |                 } | 
 | 6448 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6449 |  | 
 | 6450 |             switch (overrun) { | 
 | 6451 |             case OVERRUN_TRUE: | 
 | 6452 |                 // client isn't retrieving buffers fast enough | 
 | 6453 |                 if (!activeTrack->setOverflow()) { | 
 | 6454 |                     nsecs_t now = systemTime(); | 
 | 6455 |                     // FIXME should lastWarning per track? | 
 | 6456 |                     if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 6457 |                         ALOGW("RecordThread: buffer overflow"); | 
 | 6458 |                         lastWarning = now; | 
 | 6459 |                     } | 
 | 6460 |                 } | 
 | 6461 |                 break; | 
 | 6462 |             case OVERRUN_FALSE: | 
 | 6463 |                 activeTrack->clearOverflow(); | 
 | 6464 |                 break; | 
 | 6465 |             case OVERRUN_UNKNOWN: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6466 |                 break; | 
 | 6467 |             } | 
 | 6468 |  | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6469 |             // update frame information and push timestamp out | 
 | 6470 |             activeTrack->updateTrackFrameInfo( | 
| Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6471 |                     activeTrack->mServerProxy->framesReleased(), | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6472 |                     mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], | 
 | 6473 |                     mSampleRate, mTimestamp); | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6474 |         } | 
 | 6475 |  | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6476 | unlock: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6477 |         // enable changes in effect chain | 
 | 6478 |         unlockEffectChains(effectChains); | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6479 |         // 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] | 6480 |     } | 
 | 6481 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6482 |     standbyIfNotAlreadyInStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6483 |  | 
 | 6484 |     { | 
 | 6485 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6486 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6487 |             sp<RecordTrack> track = mTracks[i]; | 
 | 6488 |             track->invalidate(); | 
 | 6489 |         } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6490 |         mActiveTracks.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6491 |         mStartStopCond.broadcast(); | 
 | 6492 |     } | 
 | 6493 |  | 
 | 6494 |     releaseWakeLock(); | 
 | 6495 |  | 
 | 6496 |     ALOGV("RecordThread %p exiting", this); | 
 | 6497 |     return false; | 
 | 6498 | } | 
 | 6499 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6500 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6501 | { | 
 | 6502 |     if (!mStandby) { | 
 | 6503 |         inputStandBy(); | 
 | 6504 |         mStandby = true; | 
 | 6505 |     } | 
 | 6506 | } | 
 | 6507 |  | 
 | 6508 | void AudioFlinger::RecordThread::inputStandBy() | 
 | 6509 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6510 |     // Idle the fast capture if it's currently running | 
 | 6511 |     if (mFastCapture != 0) { | 
 | 6512 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 6513 |         FastCaptureState *state = sq->begin(); | 
 | 6514 |         if (!(state->mCommand & FastCaptureState::IDLE)) { | 
 | 6515 |             state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 6516 |             state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 6517 |             state->mColdGen++; | 
 | 6518 |             mFastCaptureFutex = 0; | 
 | 6519 |             sq->end(); | 
 | 6520 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 6521 |             sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); | 
 | 6522 | #if 0 | 
 | 6523 |             if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 6524 |                 // FIXME | 
 | 6525 |             } | 
 | 6526 | #endif | 
 | 6527 | #ifdef AUDIO_WATCHDOG | 
 | 6528 |             // FIXME | 
 | 6529 | #endif | 
 | 6530 |         } else { | 
 | 6531 |             sq->end(false /*didModify*/); | 
 | 6532 |         } | 
 | 6533 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6534 |     status_t result = mInput->stream->standby(); | 
 | 6535 |     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] | 6536 |  | 
 | 6537 |     // If going into standby, flush the pipe source. | 
 | 6538 |     if (mPipeSource.get() != nullptr) { | 
 | 6539 |         const ssize_t flushed = mPipeSource->flush(); | 
 | 6540 |         if (flushed > 0) { | 
 | 6541 |             ALOGV("Input standby flushed PipeSource %zd frames", flushed); | 
 | 6542 |             mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; | 
 | 6543 |             mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); | 
 | 6544 |         } | 
 | 6545 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6546 | } | 
 | 6547 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6548 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6549 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6550 |         const sp<AudioFlinger::Client>& client, | 
 | 6551 |         uint32_t sampleRate, | 
 | 6552 |         audio_format_t format, | 
 | 6553 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6554 |         size_t *pFrameCount, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6555 |         audio_session_t sessionId, | 
| Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6556 |         size_t *notificationFrames, | 
| Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6557 |         uid_t uid, | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6558 |         audio_input_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6559 |         pid_t tid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6560 |         status_t *status, | 
 | 6561 |         audio_port_handle_t portId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6562 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6563 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6564 |     sp<RecordTrack> track; | 
 | 6565 |     status_t lStatus; | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6566 |     audio_input_flags_t inputFlags = mInput->flags; | 
 | 6567 |  | 
 | 6568 |     // special case for FAST flag considered OK if fast capture is present | 
 | 6569 |     if (hasFastCapture()) { | 
 | 6570 |         inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); | 
 | 6571 |     } | 
 | 6572 |  | 
 | 6573 |     // Check if requested flags are compatible with output stream flags | 
 | 6574 |     if ((*flags & inputFlags) != *flags) { | 
 | 6575 |         ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" | 
 | 6576 |                 " input flags (%08x)", | 
 | 6577 |               *flags, inputFlags); | 
 | 6578 |         *flags = (audio_input_flags_t)(*flags & inputFlags); | 
 | 6579 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6580 |  | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6581 |     // client expresses a preference for FAST, but we get the final say | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6582 |     if (*flags & AUDIO_INPUT_FLAG_FAST) { | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6583 |       if ( | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6584 |             // we formerly checked for a callback handler (non-0 tid), | 
 | 6585 |             // but that is no longer required for TRANSFER_OBTAIN mode | 
 | 6586 |             // | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6587 |             // frame count is not specified, or is exactly the pipe depth | 
 | 6588 |             ((frameCount == 0) || (frameCount == mPipeFramesP2)) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6589 |             // PCM data | 
 | 6590 |             audio_is_linear_pcm(format) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6591 |             // hardware format | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6592 |             (format == mFormat) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6593 |             // hardware channel mask | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6594 |             (channelMask == mChannelMask) && | 
| Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6595 |             // hardware sample rate | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6596 |             (sampleRate == mSampleRate) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6597 |             // record thread has an associated fast capture | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6598 |             hasFastCapture() && | 
 | 6599 |             // there are sufficient fast track slots available | 
 | 6600 |             mFastTrackAvail | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6601 |         ) { | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6602 |           // check compatibility with audio effects. | 
 | 6603 |           Mutex::Autolock _l(mLock); | 
 | 6604 |           // Do not accept FAST flag if the session has software effects | 
 | 6605 |           sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 6606 |           if (chain != 0) { | 
| Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 6607 |               audio_input_flags_t old = *flags; | 
 | 6608 |               chain->checkInputFlagCompatibility(flags); | 
 | 6609 |               if (old != *flags) { | 
 | 6610 |                   ALOGV("AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", | 
 | 6611 |                           (int)old, (int)*flags); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6612 |               } | 
 | 6613 |           } | 
| Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6614 |           ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6615 |                    "AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", | 
 | 6616 |                    frameCount, mFrameCount); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6617 |       } else { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6618 |         ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6619 |                 "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6620 |                 "hasFastCapture=%d tid=%d mFastTrackAvail=%d", | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6621 |                 frameCount, mFrameCount, mPipeFramesP2, | 
 | 6622 |                 format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, | 
 | 6623 |                 hasFastCapture(), tid, mFastTrackAvail); | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6624 |         *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6625 |       } | 
 | 6626 |     } | 
 | 6627 |  | 
 | 6628 |     // compute track buffer size in frames, and suggest the notification frame count | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6629 |     if (*flags & AUDIO_INPUT_FLAG_FAST) { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6630 |         // fast track: frame count is exactly the pipe depth | 
 | 6631 |         frameCount = mPipeFramesP2; | 
 | 6632 |         // ignore requested notificationFrames, and always notify exactly once every HAL buffer | 
 | 6633 |         *notificationFrames = mFrameCount; | 
 | 6634 |     } else { | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6635 |         // not fast track: max notification period is resampled equivalent of one HAL buffer time | 
 | 6636 |         //                 or 20 ms if there is a fast capture | 
 | 6637 |         // TODO This could be a roundupRatio inline, and const | 
 | 6638 |         size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) | 
 | 6639 |                 * sampleRate + mSampleRate - 1) / mSampleRate; | 
 | 6640 |         // minimum number of notification periods is at least kMinNotifications, | 
 | 6641 |         // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) | 
 | 6642 |         static const size_t kMinNotifications = 3; | 
 | 6643 |         static const uint32_t kMinMs = 30; | 
 | 6644 |         // TODO This could be a roundupRatio inline | 
 | 6645 |         const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; | 
 | 6646 |         // TODO This could be a roundupRatio inline | 
 | 6647 |         const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / | 
 | 6648 |                 maxNotificationFrames; | 
 | 6649 |         const size_t minFrameCount = maxNotificationFrames * | 
 | 6650 |                 max(kMinNotifications, minNotificationsByMs); | 
 | 6651 |         frameCount = max(frameCount, minFrameCount); | 
 | 6652 |         if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { | 
 | 6653 |             *notificationFrames = maxNotificationFrames; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6654 |         } | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6655 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6656 |     *pFrameCount = frameCount; | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6657 |  | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6658 |     lStatus = initCheck(); | 
 | 6659 |     if (lStatus != NO_ERROR) { | 
 | 6660 |         ALOGE("createRecordTrack_l() audio driver not initialized"); | 
 | 6661 |         goto Exit; | 
 | 6662 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6663 |  | 
 | 6664 |     { // scope for mLock | 
 | 6665 |         Mutex::Autolock _l(mLock); | 
 | 6666 |  | 
 | 6667 |         track = new RecordTrack(this, client, sampleRate, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6668 |                       format, channelMask, frameCount, NULL, sessionId, uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 6669 |                       *flags, TrackBase::TYPE_DEFAULT, portId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6670 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6671 |         lStatus = track->initCheck(); | 
 | 6672 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6673 |             ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6674 |             // 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] | 6675 |             goto Exit; | 
 | 6676 |         } | 
 | 6677 |         mTracks.add(track); | 
 | 6678 |  | 
 | 6679 |         // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
 | 6680 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 6681 |                         mAudioFlinger->btNrecIsOff(); | 
 | 6682 |         setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
 | 6683 |         setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6684 |  | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6685 |         if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6686 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 6687 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 6688 |             // so ask activity manager to do this on our behalf | 
| Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 6689 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6690 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6691 |     } | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6692 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6693 |     lStatus = NO_ERROR; | 
 | 6694 |  | 
 | 6695 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6696 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6697 |     return track; | 
 | 6698 | } | 
 | 6699 |  | 
 | 6700 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, | 
 | 6701 |                                            AudioSystem::sync_event_t event, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6702 |                                            audio_session_t triggerSession) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6703 | { | 
 | 6704 |     ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
 | 6705 |     sp<ThreadBase> strongMe = this; | 
 | 6706 |     status_t status = NO_ERROR; | 
 | 6707 |  | 
 | 6708 |     if (event == AudioSystem::SYNC_EVENT_NONE) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6709 |         recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6710 |     } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6711 |         recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6712 |                                        triggerSession, | 
 | 6713 |                                        recordTrack->sessionId(), | 
 | 6714 |                                        syncStartEventCallback, | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6715 |                                        recordTrack); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6716 |         // Sync event can be cancelled by the trigger session if the track is not in a | 
 | 6717 |         // compatible state in which case we start record immediately | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6718 |         if (recordTrack->mSyncStartEvent->isCancelled()) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6719 |             recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6720 |         } else { | 
 | 6721 |             // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6722 |             recordTrack->mFramesToDrop = - | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6723 |                     ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6724 |         } | 
 | 6725 |     } | 
 | 6726 |  | 
 | 6727 |     { | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6728 |         // This section is a rendezvous between binder thread executing start() and RecordThread | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6729 |         AutoMutex lock(mLock); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6730 |         if (mActiveTracks.indexOf(recordTrack) >= 0) { | 
 | 6731 |             if (recordTrack->mState == TrackBase::PAUSING) { | 
 | 6732 |                 ALOGV("active record track PAUSING -> ACTIVE"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6733 |                 recordTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6734 |             } else { | 
 | 6735 |                 ALOGV("active record track state %d", recordTrack->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6736 |             } | 
 | 6737 |             return status; | 
 | 6738 |         } | 
 | 6739 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6740 |         // TODO consider other ways of handling this, such as changing the state to :STARTING and | 
 | 6741 |         //      adding the track to mActiveTracks after returning from AudioSystem::startInput(), | 
 | 6742 |         //      or using a separate command thread | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6743 |         recordTrack->mState = TrackBase::STARTING_1; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6744 |         mActiveTracks.add(recordTrack); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6745 |         status_t status = NO_ERROR; | 
 | 6746 |         if (recordTrack->isExternalTrack()) { | 
 | 6747 |             mLock.unlock(); | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6748 |             status = AudioSystem::startInput(mId, recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6749 |             mLock.lock(); | 
 | 6750 |             // FIXME should verify that recordTrack is still in mActiveTracks | 
 | 6751 |             if (status != NO_ERROR) { | 
 | 6752 |                 mActiveTracks.remove(recordTrack); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6753 |                 recordTrack->clearSyncStartEvent(); | 
 | 6754 |                 ALOGV("RecordThread::start error %d", status); | 
 | 6755 |                 return status; | 
 | 6756 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6757 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6758 |         // Catch up with current buffer indices if thread is already running. | 
 | 6759 |         // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront | 
 | 6760 |         // was initialized to some value closer to the thread's mRsmpInFront, then the track could | 
 | 6761 |         // see previously buffered data before it called start(), but with greater risk of overrun. | 
 | 6762 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6763 |         recordTrack->mResamplerBufferProvider->reset(); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6764 |         // clear any converter state as new data will be discontinuous | 
 | 6765 |         recordTrack->mRecordBufferConverter->reset(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6766 |         recordTrack->mState = TrackBase::STARTING_2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6767 |         // signal thread to start | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6768 |         mWaitWorkCV.broadcast(); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6769 |         if (mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6770 |             ALOGV("Record failed to start"); | 
 | 6771 |             status = BAD_VALUE; | 
 | 6772 |             goto startError; | 
 | 6773 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6774 |         return status; | 
 | 6775 |     } | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6776 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6777 | startError: | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6778 |     if (recordTrack->isExternalTrack()) { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6779 |         AudioSystem::stopInput(mId, recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6780 |     } | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6781 |     recordTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6782 |     // FIXME I wonder why we do not reset the state here? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6783 |     return status; | 
 | 6784 | } | 
 | 6785 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6786 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
 | 6787 | { | 
 | 6788 |     sp<SyncEvent> strongEvent = event.promote(); | 
 | 6789 |  | 
 | 6790 |     if (strongEvent != 0) { | 
| Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6791 |         sp<RefBase> ptr = strongEvent->cookie().promote(); | 
 | 6792 |         if (ptr != 0) { | 
 | 6793 |             RecordTrack *recordTrack = (RecordTrack *)ptr.get(); | 
 | 6794 |             recordTrack->handleSyncStartEvent(strongEvent); | 
 | 6795 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6796 |     } | 
 | 6797 | } | 
 | 6798 |  | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6799 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6800 |     ALOGV("RecordThread::stop"); | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6801 |     AutoMutex _l(mLock); | 
| Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 6802 |     if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6803 |         return false; | 
 | 6804 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6805 |     // 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] | 6806 |     recordTrack->mState = TrackBase::PAUSING; | 
| Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6807 |     // signal thread to stop | 
 | 6808 |     mWaitWorkCV.broadcast(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6809 |     // do not wait for mStartStopCond if exiting | 
 | 6810 |     if (exitPending()) { | 
 | 6811 |         return true; | 
 | 6812 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6813 |     // FIXME incorrect usage of wait: no explicit predicate or loop | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6814 |     mStartStopCond.wait(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6815 |     // if we have been restarted, recordTrack is in mActiveTracks here | 
| Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 6816 |     if (exitPending() || mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6817 |         ALOGV("Record stopped OK"); | 
 | 6818 |         return true; | 
 | 6819 |     } | 
 | 6820 |     return false; | 
 | 6821 | } | 
 | 6822 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6823 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6824 | { | 
 | 6825 |     return false; | 
 | 6826 | } | 
 | 6827 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6828 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6829 | { | 
 | 6830 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
 | 6831 |     if (!isValidSyncEvent(event)) { | 
 | 6832 |         return BAD_VALUE; | 
 | 6833 |     } | 
 | 6834 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6835 |     audio_session_t eventSession = event->triggerSession(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6836 |     status_t ret = NAME_NOT_FOUND; | 
 | 6837 |  | 
 | 6838 |     Mutex::Autolock _l(mLock); | 
 | 6839 |  | 
 | 6840 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6841 |         sp<RecordTrack> track = mTracks[i]; | 
 | 6842 |         if (eventSession == track->sessionId()) { | 
 | 6843 |             (void) track->setSyncEvent(event); | 
 | 6844 |             ret = NO_ERROR; | 
 | 6845 |         } | 
 | 6846 |     } | 
 | 6847 |     return ret; | 
 | 6848 | #else | 
 | 6849 |     return BAD_VALUE; | 
 | 6850 | #endif | 
 | 6851 | } | 
 | 6852 |  | 
 | 6853 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
 | 6854 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) | 
 | 6855 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6856 |     track->terminate(); | 
 | 6857 |     track->mState = TrackBase::STOPPED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6858 |     // active tracks are removed by threadLoop() | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6859 |     if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6860 |         removeTrack_l(track); | 
 | 6861 |     } | 
 | 6862 | } | 
 | 6863 |  | 
 | 6864 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) | 
 | 6865 | { | 
 | 6866 |     mTracks.remove(track); | 
 | 6867 |     // need anything related to effects here? | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6868 |     if (track->isFastTrack()) { | 
 | 6869 |         ALOG_ASSERT(!mFastTrackAvail); | 
 | 6870 |         mFastTrackAvail = true; | 
 | 6871 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6872 | } | 
 | 6873 |  | 
 | 6874 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
 | 6875 | { | 
 | 6876 |     dumpInternals(fd, args); | 
 | 6877 |     dumpTracks(fd, args); | 
 | 6878 |     dumpEffectChains(fd, args); | 
 | 6879 | } | 
 | 6880 |  | 
 | 6881 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 6882 | { | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6883 |     dumpBase(fd, args); | 
 | 6884 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 6885 |     AudioStreamIn *input = mInput; | 
 | 6886 |     audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; | 
 | 6887 |     dprintf(fd, "  AudioStreamIn: %p flags %#x (%s)\n", | 
 | 6888 |             input, flags, inputFlagsToString(flags).c_str()); | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6889 |     if (mActiveTracks.size() == 0) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6890 |         dprintf(fd, "  No active record clients\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6891 |     } | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6892 |     dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6893 |     dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); | 
| Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6894 |  | 
| Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6895 |     // Make a non-atomic copy of fast capture dump state so it won't change underneath us | 
 | 6896 |     // while we are dumping it.  It may be inconsistent, but it won't mutate! | 
 | 6897 |     // This is a large object so we place it on the heap. | 
 | 6898 |     // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. | 
 | 6899 |     const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); | 
 | 6900 |     copy->dump(fd); | 
 | 6901 |     delete copy; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6902 | } | 
 | 6903 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6904 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6905 | { | 
 | 6906 |     const size_t SIZE = 256; | 
 | 6907 |     char buffer[SIZE]; | 
 | 6908 |     String8 result; | 
 | 6909 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6910 |     size_t numtracks = mTracks.size(); | 
 | 6911 |     size_t numactive = mActiveTracks.size(); | 
 | 6912 |     size_t numactiveseen = 0; | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6913 |     dprintf(fd, "  %zu Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6914 |     if (numtracks) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6915 |         dprintf(fd, " of which %zu are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6916 |         RecordTrack::appendDumpHeader(result); | 
 | 6917 |         for (size_t i = 0; i < numtracks ; ++i) { | 
 | 6918 |             sp<RecordTrack> track = mTracks[i]; | 
 | 6919 |             if (track != 0) { | 
 | 6920 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 6921 |                 if (active) { | 
 | 6922 |                     numactiveseen++; | 
 | 6923 |                 } | 
 | 6924 |                 track->dump(buffer, SIZE, active); | 
 | 6925 |                 result.append(buffer); | 
 | 6926 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6927 |         } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6928 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6929 |         dprintf(fd, "\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6930 |     } | 
 | 6931 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6932 |     if (numactiveseen != numactive) { | 
 | 6933 |         snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
 | 6934 |                 " not in the track list\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6935 |         result.append(buffer); | 
 | 6936 |         RecordTrack::appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6937 |         for (size_t i = 0; i < numactive; ++i) { | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6938 |             sp<RecordTrack> track = mActiveTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6939 |             if (mTracks.indexOf(track) < 0) { | 
 | 6940 |                 track->dump(buffer, SIZE, true); | 
 | 6941 |                 result.append(buffer); | 
 | 6942 |             } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6943 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6944 |  | 
 | 6945 |     } | 
 | 6946 |     write(fd, result.string(), result.size()); | 
 | 6947 | } | 
 | 6948 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6949 |  | 
 | 6950 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() | 
 | 6951 | { | 
 | 6952 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
 | 6953 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6954 |     mRsmpInFront = recordThread->mRsmpInRear; | 
 | 6955 |     mRsmpInUnrel = 0; | 
 | 6956 | } | 
 | 6957 |  | 
 | 6958 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( | 
 | 6959 |         size_t *framesAvailable, bool *hasOverrun) | 
 | 6960 | { | 
 | 6961 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
 | 6962 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6963 |     const int32_t rear = recordThread->mRsmpInRear; | 
 | 6964 |     const int32_t front = mRsmpInFront; | 
 | 6965 |     const ssize_t filled = rear - front; | 
 | 6966 |  | 
 | 6967 |     size_t framesIn; | 
 | 6968 |     bool overrun = false; | 
 | 6969 |     if (filled < 0) { | 
 | 6970 |         // should not happen, but treat like a massive overrun and re-sync | 
 | 6971 |         framesIn = 0; | 
 | 6972 |         mRsmpInFront = rear; | 
 | 6973 |         overrun = true; | 
 | 6974 |     } else if ((size_t) filled <= recordThread->mRsmpInFrames) { | 
 | 6975 |         framesIn = (size_t) filled; | 
 | 6976 |     } else { | 
 | 6977 |         // client is not keeping up with server, but give it latest data | 
 | 6978 |         framesIn = recordThread->mRsmpInFrames; | 
 | 6979 |         mRsmpInFront = /* front = */ rear - framesIn; | 
 | 6980 |         overrun = true; | 
 | 6981 |     } | 
 | 6982 |     if (framesAvailable != NULL) { | 
 | 6983 |         *framesAvailable = framesIn; | 
 | 6984 |     } | 
 | 6985 |     if (hasOverrun != NULL) { | 
 | 6986 |         *hasOverrun = overrun; | 
 | 6987 |     } | 
 | 6988 | } | 
 | 6989 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6990 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6991 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6992 |         AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6993 | { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6994 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6995 |     if (threadBase == 0) { | 
 | 6996 |         buffer->frameCount = 0; | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6997 |         buffer->raw = NULL; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6998 |         return NOT_ENOUGH_DATA; | 
 | 6999 |     } | 
 | 7000 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 7001 |     int32_t rear = recordThread->mRsmpInRear; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7002 |     int32_t front = mRsmpInFront; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7003 |     ssize_t filled = rear - front; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7004 |     // FIXME should not be P2 (don't want to increase latency) | 
 | 7005 |     // FIXME if client not keeping up, discard | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 7006 |     LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7007 |     // 'filled' may be non-contiguous, so return only the first contiguous chunk | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7008 |     front &= recordThread->mRsmpInFramesP2 - 1; | 
 | 7009 |     size_t part1 = recordThread->mRsmpInFramesP2 - front; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7010 |     if (part1 > (size_t) filled) { | 
 | 7011 |         part1 = filled; | 
 | 7012 |     } | 
 | 7013 |     size_t ask = buffer->frameCount; | 
 | 7014 |     ALOG_ASSERT(ask > 0); | 
 | 7015 |     if (part1 > ask) { | 
 | 7016 |         part1 = ask; | 
 | 7017 |     } | 
 | 7018 |     if (part1 == 0) { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7019 |         // 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] | 7020 |         buffer->raw = NULL; | 
 | 7021 |         buffer->frameCount = 0; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7022 |         mRsmpInUnrel = 0; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7023 |         return NOT_ENOUGH_DATA; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7024 |     } | 
 | 7025 |  | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7026 |     buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7027 |     buffer->frameCount = part1; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7028 |     mRsmpInUnrel = part1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7029 |     return NO_ERROR; | 
 | 7030 | } | 
 | 7031 |  | 
 | 7032 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7033 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( | 
 | 7034 |         AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7035 | { | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7036 |     size_t stepCount = buffer->frameCount; | 
 | 7037 |     if (stepCount == 0) { | 
 | 7038 |         return; | 
 | 7039 |     } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7040 |     ALOG_ASSERT(stepCount <= mRsmpInUnrel); | 
 | 7041 |     mRsmpInUnrel -= stepCount; | 
 | 7042 |     mRsmpInFront += stepCount; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7043 |     buffer->raw = NULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7044 |     buffer->frameCount = 0; | 
 | 7045 | } | 
 | 7046 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7047 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7048 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 7049 |                                                         status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7050 | { | 
 | 7051 |     bool reconfig = false; | 
 | 7052 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7053 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7054 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7055 |     audio_format_t reqFormat = mFormat; | 
 | 7056 |     uint32_t samplingRate = mSampleRate; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7057 |     // 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] | 7058 |     audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); | 
 | 7059 |  | 
 | 7060 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 7061 |     int value; | 
| Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7062 |  | 
 | 7063 |     // scope for AutoPark extends to end of method | 
 | 7064 |     AutoPark<FastCapture> park(mFastCapture); | 
 | 7065 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7066 |     // TODO Investigate when this code runs. Check with audio policy when a sample rate and | 
 | 7067 |     //      channel count change can be requested. Do we mandate the first client defines the | 
 | 7068 |     //      HAL sampling rate and channel count or do we allow changes on the fly? | 
 | 7069 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 7070 |         samplingRate = value; | 
 | 7071 |         reconfig = true; | 
 | 7072 |     } | 
 | 7073 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7074 |         if (!audio_is_linear_pcm((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7075 |             status = BAD_VALUE; | 
 | 7076 |         } else { | 
 | 7077 |             reqFormat = (audio_format_t) value; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7078 |             reconfig = true; | 
 | 7079 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7080 |     } | 
 | 7081 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
 | 7082 |         audio_channel_mask_t mask = (audio_channel_mask_t) value; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7083 |         if (!audio_is_input_channel(mask) || | 
 | 7084 |                 audio_channel_count_from_in_mask(mask) > FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7085 |             status = BAD_VALUE; | 
 | 7086 |         } else { | 
 | 7087 |             channelMask = mask; | 
 | 7088 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7089 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7090 |     } | 
 | 7091 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 7092 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 7093 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 7094 |         // if frame count is changed after track creation | 
 | 7095 |         if (mActiveTracks.size() > 0) { | 
 | 7096 |             status = INVALID_OPERATION; | 
 | 7097 |         } else { | 
 | 7098 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7099 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7100 |     } | 
 | 7101 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 7102 |         // forward device change to effects that have requested to be | 
 | 7103 |         // aware of attached audio device. | 
 | 7104 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7105 |             mEffectChains[i]->setDevice_l(value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7106 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7107 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7108 |         // store input device and output device but do not forward output device to audio HAL. | 
 | 7109 |         // Note that status is ignored by the caller for output device | 
 | 7110 |         // (see AudioFlinger::setParameters() | 
 | 7111 |         if (audio_is_output_devices(value)) { | 
 | 7112 |             mOutDevice = value; | 
 | 7113 |             status = BAD_VALUE; | 
 | 7114 |         } else { | 
 | 7115 |             mInDevice = value; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7116 |             if (value != AUDIO_DEVICE_NONE) { | 
 | 7117 |                 mPrevInDevice = value; | 
 | 7118 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7119 |             // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 7120 |             // pre processings | 
 | 7121 |             if (mTracks.size() > 0) { | 
 | 7122 |                 bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 7123 |                                     mAudioFlinger->btNrecIsOff(); | 
 | 7124 |                 for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 7125 |                     sp<RecordTrack> track = mTracks[i]; | 
 | 7126 |                     setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 7127 |                     setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7128 |                 } | 
 | 7129 |             } | 
 | 7130 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7131 |     } | 
 | 7132 |     if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
 | 7133 |             mAudioSource != (audio_source_t)value) { | 
 | 7134 |         // forward device change to effects that have requested to be | 
 | 7135 |         // aware of attached audio device. | 
 | 7136 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7137 |             mEffectChains[i]->setAudioSource_l((audio_source_t)value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7138 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7139 |         mAudioSource = (audio_source_t)value; | 
 | 7140 |     } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7141 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7142 |     if (status == NO_ERROR) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7143 |         status = mInput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7144 |         if (status == INVALID_OPERATION) { | 
 | 7145 |             inputStandBy(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7146 |             status = mInput->stream->setParameters(keyValuePair); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7147 |         } | 
 | 7148 |         if (reconfig) { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7149 |             if (status == BAD_VALUE) { | 
 | 7150 |                 uint32_t sRate; | 
 | 7151 |                 audio_channel_mask_t channelMask; | 
 | 7152 |                 audio_format_t format; | 
 | 7153 |                 if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK && | 
 | 7154 |                         audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) && | 
 | 7155 |                         sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && | 
 | 7156 |                         audio_channel_count_from_in_mask(channelMask) <= FCC_8) { | 
 | 7157 |                     status = NO_ERROR; | 
 | 7158 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7159 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7160 |             if (status == NO_ERROR) { | 
 | 7161 |                 readInputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7162 |                 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7163 |             } | 
 | 7164 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7165 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7166 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7167 |     return reconfig; | 
 | 7168 | } | 
 | 7169 |  | 
 | 7170 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
 | 7171 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7172 |     Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7173 |     if (initCheck() == NO_ERROR) { | 
 | 7174 |         String8 out_s8; | 
 | 7175 |         if (mInput->stream->getParameters(keys, &out_s8) == OK) { | 
 | 7176 |             return out_s8; | 
 | 7177 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7178 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7179 |     return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7180 | } | 
 | 7181 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7182 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7183 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 7184 |  | 
 | 7185 |     desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7186 |  | 
 | 7187 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7188 |     case AUDIO_INPUT_OPENED: | 
 | 7189 |     case AUDIO_INPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7190 |         desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7191 |         desc->mChannelMask = mChannelMask; | 
 | 7192 |         desc->mSamplingRate = mSampleRate; | 
 | 7193 |         desc->mFormat = mFormat; | 
 | 7194 |         desc->mFrameCount = mFrameCount; | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7195 |         desc->mFrameCountHAL = mFrameCount; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7196 |         desc->mLatency = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7197 |         break; | 
 | 7198 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7199 |     case AUDIO_INPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7200 |     default: | 
 | 7201 |         break; | 
 | 7202 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7203 |     mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7204 | } | 
 | 7205 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7206 | void AudioFlinger::RecordThread::readInputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7207 | { | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7208 |     status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); | 
 | 7209 |     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] | 7210 |     mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7211 |     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] | 7212 |     mFormat = mHALFormat; | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7213 |     LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); | 
 | 7214 |     result = mInput->stream->getFrameSize(&mFrameSize); | 
 | 7215 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); | 
 | 7216 |     result = mInput->stream->getBufferSize(&mBufferSize); | 
 | 7217 |     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] | 7218 |     mFrameCount = mBufferSize / mFrameSize; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7219 |     // This is the formula for calculating the temporary buffer size. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7220 |     // 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] | 7221 |     // 1 full output buffer, regardless of the alignment of the available input. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7222 |     // The value is somewhat arbitrary, and could probably be even larger. | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7223 |     // A larger value should allow more old data to be read after a track calls start(), | 
 | 7224 |     // without increasing latency. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7225 |     // | 
 | 7226 |     // Note this is independent of the maximum downsampling ratio permitted for capture. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7227 |     mRsmpInFrames = mFrameCount * 7; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7228 |     mRsmpInFramesP2 = roundup(mRsmpInFrames); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7229 |     free(mRsmpInBuffer); | 
| Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7230 |     mRsmpInBuffer = NULL; | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7231 |  | 
 | 7232 |     // TODO optimize audio capture buffer sizes ... | 
 | 7233 |     // Here we calculate the size of the sliding buffer used as a source | 
 | 7234 |     // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7). | 
 | 7235 |     // For current HAL frame counts, this is usually 2048 = 40 ms.  It would | 
 | 7236 |     // be better to have it derived from the pipe depth in the long term. | 
 | 7237 |     // The current value is higher than necessary.  However it should not add to latency. | 
 | 7238 |  | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7239 |     // 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] | 7240 |     mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; | 
 | 7241 |     (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7242 |     // if posix_memalign fails, will segv here. | 
 | 7243 |     memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7244 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7245 |     // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. | 
 | 7246 |     // 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] | 7247 | } | 
 | 7248 |  | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7249 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7250 | { | 
 | 7251 |     Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7252 |     uint32_t result; | 
 | 7253 |     if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { | 
 | 7254 |         return result; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7255 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7256 |     return 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7257 | } | 
 | 7258 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7259 | // hasAudioSession_l() must be called with ThreadBase::mLock held | 
 | 7260 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7261 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7262 |     uint32_t result = 0; | 
 | 7263 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 7264 |         result = EFFECT_SESSION; | 
 | 7265 |     } | 
 | 7266 |  | 
 | 7267 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 7268 |         if (sessionId == mTracks[i]->sessionId()) { | 
 | 7269 |             result |= TRACK_SESSION; | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7270 |             if (mTracks[i]->isFastTrack()) { | 
 | 7271 |                 result |= FAST_SESSION; | 
 | 7272 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7273 |             break; | 
 | 7274 |         } | 
 | 7275 |     } | 
 | 7276 |  | 
 | 7277 |     return result; | 
 | 7278 | } | 
 | 7279 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7280 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7281 | { | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7282 |     KeyedVector<audio_session_t, bool> ids; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7283 |     Mutex::Autolock _l(mLock); | 
 | 7284 |     for (size_t j = 0; j < mTracks.size(); ++j) { | 
 | 7285 |         sp<RecordThread::RecordTrack> track = mTracks[j]; | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7286 |         audio_session_t sessionId = track->sessionId(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7287 |         if (ids.indexOfKey(sessionId) < 0) { | 
 | 7288 |             ids.add(sessionId, true); | 
 | 7289 |         } | 
 | 7290 |     } | 
 | 7291 |     return ids; | 
 | 7292 | } | 
 | 7293 |  | 
 | 7294 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
 | 7295 | { | 
 | 7296 |     Mutex::Autolock _l(mLock); | 
 | 7297 |     AudioStreamIn *input = mInput; | 
 | 7298 |     mInput = NULL; | 
 | 7299 |     return input; | 
 | 7300 | } | 
 | 7301 |  | 
 | 7302 | // 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] | 7303 | sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7304 | { | 
 | 7305 |     if (mInput == NULL) { | 
 | 7306 |         return NULL; | 
 | 7307 |     } | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7308 |     return mInput->stream; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7309 | } | 
 | 7310 |  | 
 | 7311 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 7312 | { | 
 | 7313 |     // only one chain per input thread | 
 | 7314 |     if (mEffectChains.size() != 0) { | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7315 |         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] | 7316 |         return INVALID_OPERATION; | 
 | 7317 |     } | 
 | 7318 |     ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7319 |     chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7320 |     chain->setInBuffer(NULL); | 
 | 7321 |     chain->setOutBuffer(NULL); | 
 | 7322 |  | 
 | 7323 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 7324 |  | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7325 |     // make sure enabled pre processing effects state is communicated to the HAL as we | 
 | 7326 |     // just moved them to a new input stream. | 
 | 7327 |     chain->syncHalEffectsState(); | 
 | 7328 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7329 |     mEffectChains.add(chain); | 
 | 7330 |  | 
 | 7331 |     return NO_ERROR; | 
 | 7332 | } | 
 | 7333 |  | 
 | 7334 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 7335 | { | 
 | 7336 |     ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
 | 7337 |     ALOGW_IF(mEffectChains.size() != 1, | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7338 |             "removeEffectChain_l() %p invalid chain size %zu on thread %p", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7339 |             chain.get(), mEffectChains.size(), this); | 
 | 7340 |     if (mEffectChains.size() == 1) { | 
 | 7341 |         mEffectChains.removeAt(0); | 
 | 7342 |     } | 
 | 7343 |     return 0; | 
 | 7344 | } | 
 | 7345 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7346 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 7347 |                                                           audio_patch_handle_t *handle) | 
 | 7348 | { | 
 | 7349 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7350 |  | 
 | 7351 |     // store new device and send to effects | 
 | 7352 |     mInDevice = patch->sources[0].ext.device.type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7353 |     mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7354 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7355 |         mEffectChains[i]->setDevice_l(mInDevice); | 
 | 7356 |     } | 
 | 7357 |  | 
 | 7358 |     // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 7359 |     // pre processings | 
 | 7360 |     if (mTracks.size() > 0) { | 
 | 7361 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 7362 |                             mAudioFlinger->btNrecIsOff(); | 
 | 7363 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 7364 |             sp<RecordTrack> track = mTracks[i]; | 
 | 7365 |             setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 7366 |             setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
 | 7367 |         } | 
 | 7368 |     } | 
 | 7369 |  | 
 | 7370 |     // store new source and send to effects | 
 | 7371 |     if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
 | 7372 |         mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7373 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7374 |             mEffectChains[i]->setAudioSource_l(mAudioSource); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7375 |         } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7376 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7377 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7378 |     if (mInput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7379 |         sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 7380 |         status = hwDevice->createAudioPatch(patch->num_sources, | 
 | 7381 |                                             patch->sources, | 
 | 7382 |                                             patch->num_sinks, | 
 | 7383 |                                             patch->sinks, | 
 | 7384 |                                             handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7385 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7386 |         char *address; | 
 | 7387 |         if (strcmp(patch->sources[0].ext.device.address, "") != 0) { | 
 | 7388 |             address = audio_device_address_to_parameter( | 
 | 7389 |                                                 patch->sources[0].ext.device.type, | 
 | 7390 |                                                 patch->sources[0].ext.device.address); | 
 | 7391 |         } else { | 
 | 7392 |             address = (char *)calloc(1, 1); | 
 | 7393 |         } | 
 | 7394 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 7395 |         free(address); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7396 |         param.addInt(String8(AudioParameter::keyRouting), | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7397 |                      (int)patch->sources[0].ext.device.type); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7398 |         param.addInt(String8(AudioParameter::keyInputSource), | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7399 |                                          (int)patch->sinks[0].ext.mix.usecase.source); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7400 |         status = mInput->stream->setParameters(param.toString()); | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7401 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7402 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7403 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7404 |     if (mInDevice != mPrevInDevice) { | 
 | 7405 |         sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
 | 7406 |         mPrevInDevice = mInDevice; | 
 | 7407 |     } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7408 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7409 |     return status; | 
 | 7410 | } | 
 | 7411 |  | 
 | 7412 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 7413 | { | 
 | 7414 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7415 |  | 
 | 7416 |     mInDevice = AUDIO_DEVICE_NONE; | 
 | 7417 |  | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 7418 |     if (mInput->audioHwDev->supportsAudioPatches()) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 7419 |         sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 7420 |         status = hwDevice->releaseAudioPatch(handle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7421 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7422 |         AudioParameter param; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 7423 |         param.addInt(String8(AudioParameter::keyRouting), 0); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7424 |         status = mInput->stream->setParameters(param.toString()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7425 |     } | 
 | 7426 |     return status; | 
 | 7427 | } | 
 | 7428 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7429 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) | 
 | 7430 | { | 
 | 7431 |     Mutex::Autolock _l(mLock); | 
 | 7432 |     mTracks.add(record); | 
 | 7433 | } | 
 | 7434 |  | 
 | 7435 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) | 
 | 7436 | { | 
 | 7437 |     Mutex::Autolock _l(mLock); | 
 | 7438 |     destroyTrack_l(record); | 
 | 7439 | } | 
 | 7440 |  | 
 | 7441 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 7442 | { | 
 | 7443 |     ThreadBase::getAudioPortConfig(config); | 
 | 7444 |     config->role = AUDIO_PORT_ROLE_SINK; | 
 | 7445 |     config->ext.mix.hw_module = mInput->audioHwDev->handle(); | 
 | 7446 |     config->ext.mix.usecase.source = mAudioSource; | 
 | 7447 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7448 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7449 | // ---------------------------------------------------------------------------- | 
 | 7450 | //      Mmap | 
 | 7451 | // ---------------------------------------------------------------------------- | 
 | 7452 |  | 
 | 7453 | AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread) | 
 | 7454 |     : mThread(thread) | 
 | 7455 | { | 
 | 7456 | } | 
 | 7457 |  | 
 | 7458 | AudioFlinger::MmapThreadHandle::~MmapThreadHandle() | 
 | 7459 | { | 
 | 7460 |     MmapThread *thread = mThread.get(); | 
 | 7461 |     // clear our strong reference before disconnecting the thread: the last strong reference | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7462 |     // will be removed when closeInput/closeOutput is executed upon call from audio policy manager | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7463 |     // and the thread removed from mMMapThreads list causing the thread destruction. | 
 | 7464 |     mThread.clear(); | 
 | 7465 |     if (thread != nullptr) { | 
 | 7466 |         thread->disconnect(); | 
 | 7467 |     } | 
 | 7468 | } | 
 | 7469 |  | 
 | 7470 | status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, | 
 | 7471 |                                   struct audio_mmap_buffer_info *info) | 
 | 7472 | { | 
 | 7473 |     if (mThread == 0) { | 
 | 7474 |         return NO_INIT; | 
 | 7475 |     } | 
 | 7476 |     return mThread->createMmapBuffer(minSizeFrames, info); | 
 | 7477 | } | 
 | 7478 |  | 
 | 7479 | status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position) | 
 | 7480 | { | 
 | 7481 |     if (mThread == 0) { | 
 | 7482 |         return NO_INIT; | 
 | 7483 |     } | 
 | 7484 |     return mThread->getMmapPosition(position); | 
 | 7485 | } | 
 | 7486 |  | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7487 | status_t AudioFlinger::MmapThreadHandle::start(const MmapStreamInterface::Client& client, | 
 | 7488 |         audio_port_handle_t *handle) | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7489 |  | 
 | 7490 | { | 
 | 7491 |     if (mThread == 0) { | 
 | 7492 |         return NO_INIT; | 
 | 7493 |     } | 
 | 7494 |     return mThread->start(client, handle); | 
 | 7495 | } | 
 | 7496 |  | 
 | 7497 | status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle) | 
 | 7498 | { | 
 | 7499 |     if (mThread == 0) { | 
 | 7500 |         return NO_INIT; | 
 | 7501 |     } | 
 | 7502 |     return mThread->stop(handle); | 
 | 7503 | } | 
 | 7504 |  | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7505 | status_t AudioFlinger::MmapThreadHandle::standby() | 
 | 7506 | { | 
 | 7507 |     if (mThread == 0) { | 
 | 7508 |         return NO_INIT; | 
 | 7509 |     } | 
 | 7510 |     return mThread->standby(); | 
 | 7511 | } | 
 | 7512 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7513 |  | 
 | 7514 | AudioFlinger::MmapThread::MmapThread( | 
 | 7515 |         const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
 | 7516 |         AudioHwDevice *hwDev, sp<StreamHalInterface> stream, | 
 | 7517 |         audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) | 
 | 7518 |     : ThreadBase(audioFlinger, id, outDevice, inDevice, MMAP, systemReady), | 
 | 7519 |       mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev) | 
 | 7520 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7521 |     mStandby = true; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7522 |     readHalParameters_l(); | 
 | 7523 | } | 
 | 7524 |  | 
 | 7525 | AudioFlinger::MmapThread::~MmapThread() | 
 | 7526 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7527 |     releaseWakeLock_l(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7528 | } | 
 | 7529 |  | 
 | 7530 | void AudioFlinger::MmapThread::onFirstRef() | 
 | 7531 | { | 
 | 7532 |     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 7533 | } | 
 | 7534 |  | 
 | 7535 | void AudioFlinger::MmapThread::disconnect() | 
 | 7536 | { | 
 | 7537 |     for (const sp<MmapTrack> &t : mActiveTracks) { | 
 | 7538 |         stop(t->portId()); | 
 | 7539 |     } | 
 | 7540 |     // this will cause the destruction of this thread. | 
 | 7541 |     if (isOutput()) { | 
 | 7542 |         AudioSystem::releaseOutput(mId, streamType(), mSessionId); | 
 | 7543 |     } else { | 
 | 7544 |         AudioSystem::releaseInput(mId, mSessionId); | 
 | 7545 |     } | 
 | 7546 | } | 
 | 7547 |  | 
 | 7548 |  | 
 | 7549 | void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr, | 
 | 7550 |                                                 audio_stream_type_t streamType __unused, | 
 | 7551 |                                                 audio_session_t sessionId, | 
 | 7552 |                                                 const sp<MmapStreamCallback>& callback, | 
 | 7553 |                                                 audio_port_handle_t portId) | 
 | 7554 | { | 
 | 7555 |     mAttr = *attr; | 
 | 7556 |     mSessionId = sessionId; | 
 | 7557 |     mCallback = callback; | 
 | 7558 |     mPortId = portId; | 
 | 7559 | } | 
 | 7560 |  | 
 | 7561 | status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames, | 
 | 7562 |                                   struct audio_mmap_buffer_info *info) | 
 | 7563 | { | 
 | 7564 |     if (mHalStream == 0) { | 
 | 7565 |         return NO_INIT; | 
 | 7566 |     } | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7567 |     mStandby = true; | 
 | 7568 |     acquireWakeLock(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7569 |     return mHalStream->createMmapBuffer(minSizeFrames, info); | 
 | 7570 | } | 
 | 7571 |  | 
 | 7572 | status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position) | 
 | 7573 | { | 
 | 7574 |     if (mHalStream == 0) { | 
 | 7575 |         return NO_INIT; | 
 | 7576 |     } | 
 | 7577 |     return mHalStream->getMmapPosition(position); | 
 | 7578 | } | 
 | 7579 |  | 
 | 7580 | status_t AudioFlinger::MmapThread::start(const MmapStreamInterface::Client& client, | 
 | 7581 |                                          audio_port_handle_t *handle) | 
 | 7582 | { | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7583 |     ALOGV("%s clientUid %d mStandby %d", __FUNCTION__, client.clientUid, mStandby); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7584 |     if (mHalStream == 0) { | 
 | 7585 |         return NO_INIT; | 
 | 7586 |     } | 
 | 7587 |  | 
 | 7588 |     status_t ret; | 
 | 7589 |     audio_session_t sessionId; | 
 | 7590 |     audio_port_handle_t portId; | 
 | 7591 |  | 
 | 7592 |     if (mActiveTracks.size() == 0) { | 
 | 7593 |         // 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] | 7594 |         ret = mHalStream->start(); | 
 | 7595 |         if (ret != NO_ERROR) { | 
 | 7596 |             ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); | 
 | 7597 |             return ret; | 
 | 7598 |         } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7599 |         portId = mPortId; | 
 | 7600 |         sessionId = mSessionId; | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7601 |         mStandby = false; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7602 |     } else { | 
 | 7603 |         // for other tracks than first one, get a new port ID from APM. | 
 | 7604 |         sessionId = (audio_session_t)mAudioFlinger->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
 | 7605 |         audio_io_handle_t io; | 
 | 7606 |         if (isOutput()) { | 
 | 7607 |             audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 7608 |             config.sample_rate = mSampleRate; | 
 | 7609 |             config.channel_mask = mChannelMask; | 
 | 7610 |             config.format = mFormat; | 
 | 7611 |             audio_stream_type_t stream = streamType(); | 
 | 7612 |             audio_output_flags_t flags = | 
 | 7613 |                     (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); | 
 | 7614 |             ret = AudioSystem::getOutputForAttr(&mAttr, &io, | 
 | 7615 |                                                 sessionId, | 
 | 7616 |                                                 &stream, | 
 | 7617 |                                                 client.clientUid, | 
 | 7618 |                                                 &config, | 
 | 7619 |                                                 flags, | 
 | 7620 |                                                 AUDIO_PORT_HANDLE_NONE, | 
 | 7621 |                                                 &portId); | 
 | 7622 |         } else { | 
 | 7623 |             audio_config_base_t config; | 
 | 7624 |             config.sample_rate = mSampleRate; | 
 | 7625 |             config.channel_mask = mChannelMask; | 
 | 7626 |             config.format = mFormat; | 
 | 7627 |             ret = AudioSystem::getInputForAttr(&mAttr, &io, | 
 | 7628 |                                                   sessionId, | 
 | 7629 |                                                   client.clientPid, | 
 | 7630 |                                                   client.clientUid, | 
 | 7631 |                                                   &config, | 
 | 7632 |                                                   AUDIO_INPUT_FLAG_MMAP_NOIRQ, | 
 | 7633 |                                                   AUDIO_PORT_HANDLE_NONE, | 
 | 7634 |                                                   &portId); | 
 | 7635 |         } | 
 | 7636 |         // APM should not chose a different input or output stream for the same set of attributes | 
 | 7637 |         // and audo configuration | 
 | 7638 |         if (ret != NO_ERROR || io != mId) { | 
 | 7639 |             ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", | 
 | 7640 |                   __FUNCTION__, ret, io, mId); | 
 | 7641 |             return BAD_VALUE; | 
 | 7642 |         } | 
 | 7643 |     } | 
 | 7644 |  | 
 | 7645 |     if (isOutput()) { | 
 | 7646 |         ret = AudioSystem::startOutput(mId, streamType(), sessionId); | 
 | 7647 |     } else { | 
 | 7648 |         ret = AudioSystem::startInput(mId, sessionId); | 
 | 7649 |     } | 
 | 7650 |  | 
 | 7651 |     // abort if start is rejected by audio policy manager | 
 | 7652 |     if (ret != NO_ERROR) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7653 |         ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7654 |         if (mActiveTracks.size() != 0) { | 
 | 7655 |             if (isOutput()) { | 
 | 7656 |                 AudioSystem::releaseOutput(mId, streamType(), sessionId); | 
 | 7657 |             } else { | 
 | 7658 |                 AudioSystem::releaseInput(mId, sessionId); | 
 | 7659 |             } | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7660 |         } else { | 
 | 7661 |             mHalStream->stop(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7662 |         } | 
 | 7663 |         return PERMISSION_DENIED; | 
 | 7664 |     } | 
 | 7665 |  | 
 | 7666 |     sp<MmapTrack> track = new MmapTrack(this, mSampleRate, mFormat, mChannelMask, sessionId, | 
 | 7667 |                                         client.clientUid, portId); | 
 | 7668 |  | 
 | 7669 |     mActiveTracks.add(track); | 
 | 7670 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 7671 |     if (chain != 0) { | 
 | 7672 |         chain->setStrategy(AudioSystem::getStrategyForStream(streamType())); | 
 | 7673 |         chain->incTrackCnt(); | 
 | 7674 |         chain->incActiveTrackCnt(); | 
 | 7675 |     } | 
 | 7676 |  | 
 | 7677 |     *handle = portId; | 
 | 7678 |  | 
 | 7679 |     broadcast_l(); | 
 | 7680 |  | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7681 |     ALOGV("%s DONE handle %d stream %p", __FUNCTION__, portId, mHalStream.get()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7682 |  | 
 | 7683 |     return NO_ERROR; | 
 | 7684 | } | 
 | 7685 |  | 
 | 7686 | status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle) | 
 | 7687 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7688 |     ALOGV("%s handle %d", __FUNCTION__, handle); | 
 | 7689 |  | 
 | 7690 |     if (mHalStream == 0) { | 
 | 7691 |         return NO_INIT; | 
 | 7692 |     } | 
 | 7693 |  | 
 | 7694 |     sp<MmapTrack> track; | 
 | 7695 |     for (const sp<MmapTrack> &t : mActiveTracks) { | 
 | 7696 |         if (handle == t->portId()) { | 
 | 7697 |             track = t; | 
 | 7698 |             break; | 
 | 7699 |         } | 
 | 7700 |     } | 
 | 7701 |     if (track == 0) { | 
 | 7702 |         return BAD_VALUE; | 
 | 7703 |     } | 
 | 7704 |  | 
 | 7705 |     mActiveTracks.remove(track); | 
 | 7706 |  | 
 | 7707 |     if (isOutput()) { | 
 | 7708 |         AudioSystem::stopOutput(mId, streamType(), track->sessionId()); | 
 | 7709 |         if (mActiveTracks.size() != 0) { | 
 | 7710 |             AudioSystem::releaseOutput(mId, streamType(), track->sessionId()); | 
 | 7711 |         } | 
 | 7712 |     } else { | 
 | 7713 |         AudioSystem::stopInput(mId, track->sessionId()); | 
 | 7714 |         if (mActiveTracks.size() != 0) { | 
 | 7715 |             AudioSystem::releaseInput(mId, track->sessionId()); | 
 | 7716 |         } | 
 | 7717 |     } | 
 | 7718 |  | 
 | 7719 |     sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 7720 |     if (chain != 0) { | 
 | 7721 |         chain->decActiveTrackCnt(); | 
 | 7722 |         chain->decTrackCnt(); | 
 | 7723 |     } | 
 | 7724 |  | 
 | 7725 |     broadcast_l(); | 
 | 7726 |  | 
 | 7727 |     if (mActiveTracks.size() == 0) { | 
 | 7728 |         mHalStream->stop(); | 
 | 7729 |     } | 
 | 7730 |     return NO_ERROR; | 
 | 7731 | } | 
 | 7732 |  | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 7733 | status_t AudioFlinger::MmapThread::standby() | 
 | 7734 | { | 
 | 7735 |     ALOGV("%s", __FUNCTION__); | 
 | 7736 |  | 
 | 7737 |     if (mHalStream == 0) { | 
 | 7738 |         return NO_INIT; | 
 | 7739 |     } | 
 | 7740 |     if (mActiveTracks.size() != 0) { | 
 | 7741 |         return INVALID_OPERATION; | 
 | 7742 |     } | 
 | 7743 |     mHalStream->standby(); | 
 | 7744 |     mStandby = true; | 
 | 7745 |     releaseWakeLock(); | 
 | 7746 |     return NO_ERROR; | 
 | 7747 | } | 
 | 7748 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7749 |  | 
 | 7750 | void AudioFlinger::MmapThread::readHalParameters_l() | 
 | 7751 | { | 
 | 7752 |     status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); | 
 | 7753 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); | 
 | 7754 |     mFormat = mHALFormat; | 
 | 7755 |     LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); | 
 | 7756 |     result = mHalStream->getFrameSize(&mFrameSize); | 
 | 7757 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); | 
 | 7758 |     result = mHalStream->getBufferSize(&mBufferSize); | 
 | 7759 |     LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); | 
 | 7760 |     mFrameCount = mBufferSize / mFrameSize; | 
 | 7761 | } | 
 | 7762 |  | 
 | 7763 | bool AudioFlinger::MmapThread::threadLoop() | 
 | 7764 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7765 |     checkSilentMode_l(); | 
 | 7766 |  | 
 | 7767 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 7768 |  | 
 | 7769 |     while (!exitPending()) | 
 | 7770 |     { | 
 | 7771 |         Mutex::Autolock _l(mLock); | 
 | 7772 |         Vector< sp<EffectChain> > effectChains; | 
 | 7773 |  | 
 | 7774 |         if (mSignalPending) { | 
 | 7775 |             // A signal was raised while we were unlocked | 
 | 7776 |             mSignalPending = false; | 
 | 7777 |         } else { | 
 | 7778 |             if (mConfigEvents.isEmpty()) { | 
 | 7779 |                 // we're about to wait, flush the binder command buffer | 
 | 7780 |                 IPCThreadState::self()->flushCommands(); | 
 | 7781 |  | 
 | 7782 |                 if (exitPending()) { | 
 | 7783 |                     break; | 
 | 7784 |                 } | 
 | 7785 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7786 |                 // wait until we have something to do... | 
 | 7787 |                 ALOGV("%s going to sleep", myName.string()); | 
 | 7788 |                 mWaitWorkCV.wait(mLock); | 
 | 7789 |                 ALOGV("%s waking up", myName.string()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7790 |  | 
 | 7791 |                 checkSilentMode_l(); | 
 | 7792 |  | 
 | 7793 |                 continue; | 
 | 7794 |             } | 
 | 7795 |         } | 
 | 7796 |  | 
 | 7797 |         processConfigEvents_l(); | 
 | 7798 |  | 
 | 7799 |         processVolume_l(); | 
 | 7800 |  | 
 | 7801 |         checkInvalidTracks_l(); | 
 | 7802 |  | 
 | 7803 |         mActiveTracks.updatePowerState(this); | 
 | 7804 |  | 
 | 7805 |         lockEffectChains_l(effectChains); | 
 | 7806 |         for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 7807 |             effectChains[i]->process_l(); | 
 | 7808 |         } | 
 | 7809 |         // enable changes in effect chain | 
 | 7810 |         unlockEffectChains(effectChains); | 
 | 7811 |         // Effect chains will be actually deleted here if they were removed from | 
 | 7812 |         // mEffectChains list during mixing or effects processing | 
 | 7813 |     } | 
 | 7814 |  | 
 | 7815 |     threadLoop_exit(); | 
 | 7816 |  | 
 | 7817 |     if (!mStandby) { | 
 | 7818 |         threadLoop_standby(); | 
 | 7819 |         mStandby = true; | 
 | 7820 |     } | 
 | 7821 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7822 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 7823 |     return false; | 
 | 7824 | } | 
 | 7825 |  | 
 | 7826 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 7827 | bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 7828 |                                                               status_t& status) | 
 | 7829 | { | 
 | 7830 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 7831 |     int value; | 
 | 7832 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 7833 |         // forward device change to effects that have requested to be | 
 | 7834 |         // aware of attached audio device. | 
 | 7835 |         if (value != AUDIO_DEVICE_NONE) { | 
 | 7836 |             mOutDevice = value; | 
 | 7837 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7838 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
 | 7839 |             } | 
 | 7840 |         } | 
 | 7841 |     } | 
 | 7842 |     status = mHalStream->setParameters(keyValuePair); | 
 | 7843 |  | 
 | 7844 |     return false; | 
 | 7845 | } | 
 | 7846 |  | 
 | 7847 | String8 AudioFlinger::MmapThread::getParameters(const String8& keys) | 
 | 7848 | { | 
 | 7849 |     Mutex::Autolock _l(mLock); | 
 | 7850 |     String8 out_s8; | 
 | 7851 |     if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { | 
 | 7852 |         return out_s8; | 
 | 7853 |     } | 
 | 7854 |     return String8(); | 
 | 7855 | } | 
 | 7856 |  | 
 | 7857 | void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
 | 7858 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 7859 |  | 
 | 7860 |     desc->mIoHandle = mId; | 
 | 7861 |  | 
 | 7862 |     switch (event) { | 
 | 7863 |     case AUDIO_INPUT_OPENED: | 
 | 7864 |     case AUDIO_INPUT_CONFIG_CHANGED: | 
 | 7865 |     case AUDIO_OUTPUT_OPENED: | 
 | 7866 |     case AUDIO_OUTPUT_CONFIG_CHANGED: | 
 | 7867 |         desc->mPatch = mPatch; | 
 | 7868 |         desc->mChannelMask = mChannelMask; | 
 | 7869 |         desc->mSamplingRate = mSampleRate; | 
 | 7870 |         desc->mFormat = mFormat; | 
 | 7871 |         desc->mFrameCount = mFrameCount; | 
 | 7872 |         desc->mFrameCountHAL = mFrameCount; | 
 | 7873 |         desc->mLatency = 0; | 
 | 7874 |         break; | 
 | 7875 |  | 
 | 7876 |     case AUDIO_INPUT_CLOSED: | 
 | 7877 |     case AUDIO_OUTPUT_CLOSED: | 
 | 7878 |     default: | 
 | 7879 |         break; | 
 | 7880 |     } | 
 | 7881 |     mAudioFlinger->ioConfigChanged(event, desc, pid); | 
 | 7882 | } | 
 | 7883 |  | 
 | 7884 | status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 7885 |                                                           audio_patch_handle_t *handle) | 
 | 7886 | { | 
 | 7887 |     status_t status = NO_ERROR; | 
 | 7888 |  | 
 | 7889 |     // store new device and send to effects | 
 | 7890 |     audio_devices_t type = AUDIO_DEVICE_NONE; | 
 | 7891 |     audio_port_handle_t deviceId; | 
 | 7892 |     if (isOutput()) { | 
 | 7893 |         for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
 | 7894 |             type |= patch->sinks[i].ext.device.type; | 
 | 7895 |         } | 
 | 7896 |         deviceId = patch->sinks[0].id; | 
 | 7897 |     } else { | 
 | 7898 |         type = patch->sources[0].ext.device.type; | 
 | 7899 |         deviceId = patch->sources[0].id; | 
 | 7900 |     } | 
 | 7901 |  | 
 | 7902 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7903 |         mEffectChains[i]->setDevice_l(type); | 
 | 7904 |     } | 
 | 7905 |  | 
 | 7906 |     if (isOutput()) { | 
 | 7907 |         mOutDevice = type; | 
 | 7908 |     } else { | 
 | 7909 |         mInDevice = type; | 
 | 7910 |         // store new source and send to effects | 
 | 7911 |         if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
 | 7912 |             mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
 | 7913 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7914 |                 mEffectChains[i]->setAudioSource_l(mAudioSource); | 
 | 7915 |             } | 
 | 7916 |         } | 
 | 7917 |     } | 
 | 7918 |  | 
 | 7919 |     if (mAudioHwDev->supportsAudioPatches()) { | 
 | 7920 |         status = mHalDevice->createAudioPatch(patch->num_sources, | 
 | 7921 |                                             patch->sources, | 
 | 7922 |                                             patch->num_sinks, | 
 | 7923 |                                             patch->sinks, | 
 | 7924 |                                             handle); | 
 | 7925 |     } else { | 
 | 7926 |         char *address; | 
 | 7927 |         if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
 | 7928 |             //FIXME: we only support address on first sink with HAL version < 3.0 | 
 | 7929 |             address = audio_device_address_to_parameter( | 
 | 7930 |                                                         patch->sinks[0].ext.device.type, | 
 | 7931 |                                                         patch->sinks[0].ext.device.address); | 
 | 7932 |         } else { | 
 | 7933 |             address = (char *)calloc(1, 1); | 
 | 7934 |         } | 
 | 7935 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 7936 |         free(address); | 
 | 7937 |         param.addInt(String8(AudioParameter::keyRouting), (int)type); | 
 | 7938 |         if (!isOutput()) { | 
 | 7939 |             param.addInt(String8(AudioParameter::keyInputSource), | 
 | 7940 |                                          (int)patch->sinks[0].ext.mix.usecase.source); | 
 | 7941 |         } | 
 | 7942 |         status = mHalStream->setParameters(param.toString()); | 
 | 7943 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
 | 7944 |     } | 
 | 7945 |  | 
 | 7946 |     if (isOutput() && mPrevOutDevice != mOutDevice) { | 
 | 7947 |         mPrevOutDevice = type; | 
 | 7948 |         sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7949 |         sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 7950 |         if (callback != 0) { | 
 | 7951 |             callback->onRoutingChanged(deviceId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7952 |         } | 
 | 7953 |     } | 
 | 7954 |     if (!isOutput() && mPrevInDevice != mInDevice) { | 
 | 7955 |         mPrevInDevice = type; | 
 | 7956 |         sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 7957 |         sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 7958 |         if (callback != 0) { | 
 | 7959 |             callback->onRoutingChanged(deviceId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 7960 |         } | 
 | 7961 |     } | 
 | 7962 |     return status; | 
 | 7963 | } | 
 | 7964 |  | 
 | 7965 | status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 7966 | { | 
 | 7967 |     status_t status = NO_ERROR; | 
 | 7968 |  | 
 | 7969 |     mInDevice = AUDIO_DEVICE_NONE; | 
 | 7970 |  | 
 | 7971 |     bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? | 
 | 7972 |                                         supportsAudioPatches : false; | 
 | 7973 |  | 
 | 7974 |     if (supportsAudioPatches) { | 
 | 7975 |         status = mHalDevice->releaseAudioPatch(handle); | 
 | 7976 |     } else { | 
 | 7977 |         AudioParameter param; | 
 | 7978 |         param.addInt(String8(AudioParameter::keyRouting), 0); | 
 | 7979 |         status = mHalStream->setParameters(param.toString()); | 
 | 7980 |     } | 
 | 7981 |     return status; | 
 | 7982 | } | 
 | 7983 |  | 
 | 7984 | void AudioFlinger::MmapThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 7985 | { | 
 | 7986 |     ThreadBase::getAudioPortConfig(config); | 
 | 7987 |     if (isOutput()) { | 
 | 7988 |         config->role = AUDIO_PORT_ROLE_SOURCE; | 
 | 7989 |         config->ext.mix.hw_module = mAudioHwDev->handle(); | 
 | 7990 |         config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
 | 7991 |     } else { | 
 | 7992 |         config->role = AUDIO_PORT_ROLE_SINK; | 
 | 7993 |         config->ext.mix.hw_module = mAudioHwDev->handle(); | 
 | 7994 |         config->ext.mix.usecase.source = mAudioSource; | 
 | 7995 |     } | 
 | 7996 | } | 
 | 7997 |  | 
 | 7998 | status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 7999 | { | 
 | 8000 |     audio_session_t session = chain->sessionId(); | 
 | 8001 |  | 
 | 8002 |     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
 | 8003 |     // Attach all tracks with same session ID to this chain. | 
 | 8004 |     // indicate all active tracks in the chain | 
 | 8005 |     for (const sp<MmapTrack> &track : mActiveTracks) { | 
 | 8006 |         if (session == track->sessionId()) { | 
 | 8007 |             chain->incTrackCnt(); | 
 | 8008 |             chain->incActiveTrackCnt(); | 
 | 8009 |         } | 
 | 8010 |     } | 
 | 8011 |  | 
 | 8012 |     chain->setThread(this); | 
 | 8013 |     chain->setInBuffer(nullptr); | 
 | 8014 |     chain->setOutBuffer(nullptr); | 
 | 8015 |     chain->syncHalEffectsState(); | 
 | 8016 |  | 
 | 8017 |     mEffectChains.add(chain); | 
 | 8018 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 8019 |     return NO_ERROR; | 
 | 8020 | } | 
 | 8021 |  | 
 | 8022 | size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 8023 | { | 
 | 8024 |     audio_session_t session = chain->sessionId(); | 
 | 8025 |  | 
 | 8026 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 8027 |  | 
 | 8028 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 8029 |         if (chain == mEffectChains[i]) { | 
 | 8030 |             mEffectChains.removeAt(i); | 
 | 8031 |             // detach all active tracks from the chain | 
 | 8032 |             // detach all tracks with same session ID from this chain | 
 | 8033 |             for (const sp<MmapTrack> &track : mActiveTracks) { | 
 | 8034 |                 if (session == track->sessionId()) { | 
 | 8035 |                     chain->decActiveTrackCnt(); | 
 | 8036 |                     chain->decTrackCnt(); | 
 | 8037 |                 } | 
 | 8038 |             } | 
 | 8039 |             break; | 
 | 8040 |         } | 
 | 8041 |     } | 
 | 8042 |     return mEffectChains.size(); | 
 | 8043 | } | 
 | 8044 |  | 
 | 8045 | // hasAudioSession_l() must be called with ThreadBase::mLock held | 
 | 8046 | uint32_t AudioFlinger::MmapThread::hasAudioSession_l(audio_session_t sessionId) const | 
 | 8047 | { | 
 | 8048 |     uint32_t result = 0; | 
 | 8049 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 8050 |         result = EFFECT_SESSION; | 
 | 8051 |     } | 
 | 8052 |  | 
 | 8053 |     for (size_t i = 0; i < mActiveTracks.size(); i++) { | 
 | 8054 |         sp<MmapTrack> track = mActiveTracks[i]; | 
 | 8055 |         if (sessionId == track->sessionId()) { | 
 | 8056 |             result |= TRACK_SESSION; | 
 | 8057 |             if (track->isFastTrack()) { | 
 | 8058 |                 result |= FAST_SESSION; | 
 | 8059 |             } | 
 | 8060 |             break; | 
 | 8061 |         } | 
 | 8062 |     } | 
 | 8063 |  | 
 | 8064 |     return result; | 
 | 8065 | } | 
 | 8066 |  | 
 | 8067 | void AudioFlinger::MmapThread::threadLoop_standby() | 
 | 8068 | { | 
 | 8069 |     mHalStream->standby(); | 
 | 8070 | } | 
 | 8071 |  | 
 | 8072 | void AudioFlinger::MmapThread::threadLoop_exit() | 
 | 8073 | { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8074 |     sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 8075 |     if (callback != 0) { | 
 | 8076 |         callback->onTearDown(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8077 |     } | 
 | 8078 | } | 
 | 8079 |  | 
 | 8080 | status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
 | 8081 | { | 
 | 8082 |     return BAD_VALUE; | 
 | 8083 | } | 
 | 8084 |  | 
 | 8085 | bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
 | 8086 | { | 
 | 8087 |     return false; | 
 | 8088 | } | 
 | 8089 |  | 
 | 8090 | status_t AudioFlinger::MmapThread::checkEffectCompatibility_l( | 
 | 8091 |         const effect_descriptor_t *desc, audio_session_t sessionId) | 
 | 8092 | { | 
 | 8093 |     // No global effect sessions on mmap threads | 
 | 8094 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
 | 8095 |         ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", | 
 | 8096 |                 desc->name, mThreadName); | 
 | 8097 |         return BAD_VALUE; | 
 | 8098 |     } | 
 | 8099 |  | 
 | 8100 |     if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { | 
 | 8101 |         ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", | 
 | 8102 |                 desc->name); | 
 | 8103 |         return BAD_VALUE; | 
 | 8104 |     } | 
 | 8105 |     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] | 8106 |         ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " | 
 | 8107 |               "thread", desc->name); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8108 |         return BAD_VALUE; | 
 | 8109 |     } | 
 | 8110 |  | 
 | 8111 |     // Only allow effects without processing load or latency | 
 | 8112 |     if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { | 
 | 8113 |         return BAD_VALUE; | 
 | 8114 |     } | 
 | 8115 |  | 
 | 8116 |     return NO_ERROR; | 
 | 8117 |  | 
 | 8118 | } | 
 | 8119 |  | 
 | 8120 | void AudioFlinger::MmapThread::checkInvalidTracks_l() | 
 | 8121 | { | 
 | 8122 |     for (const sp<MmapTrack> &track : mActiveTracks) { | 
 | 8123 |         if (track->isInvalid()) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8124 |             sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 8125 |             if (callback != 0) { | 
 | 8126 |                 callback->onTearDown(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8127 |             } | 
 | 8128 |             break; | 
 | 8129 |         } | 
 | 8130 |     } | 
 | 8131 | } | 
 | 8132 |  | 
 | 8133 | void AudioFlinger::MmapThread::dump(int fd, const Vector<String16>& args) | 
 | 8134 | { | 
 | 8135 |     dumpInternals(fd, args); | 
 | 8136 |     dumpTracks(fd, args); | 
 | 8137 |     dumpEffectChains(fd, args); | 
 | 8138 | } | 
 | 8139 |  | 
 | 8140 | void AudioFlinger::MmapThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 8141 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8142 |     dumpBase(fd, args); | 
 | 8143 |  | 
 | 8144 |     dprintf(fd, "  Attributes: content type %d usage %d source %d\n", | 
 | 8145 |             mAttr.content_type, mAttr.usage, mAttr.source); | 
 | 8146 |     dprintf(fd, "  Session: %d port Id: %d\n", mSessionId, mPortId); | 
 | 8147 |     if (mActiveTracks.size() == 0) { | 
 | 8148 |         dprintf(fd, "  No active clients\n"); | 
 | 8149 |     } | 
 | 8150 | } | 
 | 8151 |  | 
 | 8152 | void AudioFlinger::MmapThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
 | 8153 | { | 
 | 8154 |     const size_t SIZE = 256; | 
 | 8155 |     char buffer[SIZE]; | 
 | 8156 |     String8 result; | 
 | 8157 |  | 
 | 8158 |     size_t numtracks = mActiveTracks.size(); | 
 | 8159 |     dprintf(fd, "  %zu Tracks", numtracks); | 
 | 8160 |     if (numtracks) { | 
 | 8161 |         MmapTrack::appendDumpHeader(result); | 
 | 8162 |         for (size_t i = 0; i < numtracks ; ++i) { | 
 | 8163 |             sp<MmapTrack> track = mActiveTracks[i]; | 
 | 8164 |             track->dump(buffer, SIZE); | 
 | 8165 |             result.append(buffer); | 
 | 8166 |         } | 
 | 8167 |     } else { | 
 | 8168 |         dprintf(fd, "\n"); | 
 | 8169 |     } | 
 | 8170 |     write(fd, result.string(), result.size()); | 
 | 8171 | } | 
 | 8172 |  | 
 | 8173 | AudioFlinger::MmapPlaybackThread::MmapPlaybackThread( | 
 | 8174 |         const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
 | 8175 |         AudioHwDevice *hwDev,  AudioStreamOut *output, | 
 | 8176 |         audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) | 
 | 8177 |     : MmapThread(audioFlinger, id, hwDev, output->stream, outDevice, inDevice, systemReady), | 
 | 8178 |       mStreamType(AUDIO_STREAM_MUSIC), | 
 | 8179 |       mStreamVolume(1.0), mStreamMute(false), mOutput(output) | 
 | 8180 | { | 
 | 8181 |     snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); | 
 | 8182 |     mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
 | 8183 |     mMasterVolume = audioFlinger->masterVolume_l(); | 
 | 8184 |     mMasterMute = audioFlinger->masterMute_l(); | 
 | 8185 |     if (mAudioHwDev) { | 
 | 8186 |         if (mAudioHwDev->canSetMasterVolume()) { | 
 | 8187 |             mMasterVolume = 1.0; | 
 | 8188 |         } | 
 | 8189 |  | 
 | 8190 |         if (mAudioHwDev->canSetMasterMute()) { | 
 | 8191 |             mMasterMute = false; | 
 | 8192 |         } | 
 | 8193 |     } | 
 | 8194 | } | 
 | 8195 |  | 
 | 8196 | void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr, | 
 | 8197 |                                                 audio_stream_type_t streamType, | 
 | 8198 |                                                 audio_session_t sessionId, | 
 | 8199 |                                                 const sp<MmapStreamCallback>& callback, | 
 | 8200 |                                                 audio_port_handle_t portId) | 
 | 8201 | { | 
 | 8202 |     MmapThread::configure(attr, streamType, sessionId, callback, portId); | 
 | 8203 |     mStreamType = streamType; | 
 | 8204 | } | 
 | 8205 |  | 
 | 8206 | AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput() | 
 | 8207 | { | 
 | 8208 |     Mutex::Autolock _l(mLock); | 
 | 8209 |     AudioStreamOut *output = mOutput; | 
 | 8210 |     mOutput = NULL; | 
 | 8211 |     return output; | 
 | 8212 | } | 
 | 8213 |  | 
 | 8214 | void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value) | 
 | 8215 | { | 
 | 8216 |     Mutex::Autolock _l(mLock); | 
 | 8217 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 8218 |     if (mAudioHwDev && | 
 | 8219 |             mAudioHwDev->canSetMasterVolume()) { | 
 | 8220 |         mMasterVolume = 1.0; | 
 | 8221 |     } else { | 
 | 8222 |         mMasterVolume = value; | 
 | 8223 |     } | 
 | 8224 | } | 
 | 8225 |  | 
 | 8226 | void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted) | 
 | 8227 | { | 
 | 8228 |     Mutex::Autolock _l(mLock); | 
 | 8229 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 8230 |     if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { | 
 | 8231 |         mMasterMute = false; | 
 | 8232 |     } else { | 
 | 8233 |         mMasterMute = muted; | 
 | 8234 |     } | 
 | 8235 | } | 
 | 8236 |  | 
 | 8237 | void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
 | 8238 | { | 
 | 8239 |     Mutex::Autolock _l(mLock); | 
 | 8240 |     if (stream == mStreamType) { | 
 | 8241 |         mStreamVolume = value; | 
 | 8242 |         broadcast_l(); | 
 | 8243 |     } | 
 | 8244 | } | 
 | 8245 |  | 
 | 8246 | float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const | 
 | 8247 | { | 
 | 8248 |     Mutex::Autolock _l(mLock); | 
 | 8249 |     if (stream == mStreamType) { | 
 | 8250 |         return mStreamVolume; | 
 | 8251 |     } | 
 | 8252 |     return 0.0f; | 
 | 8253 | } | 
 | 8254 |  | 
 | 8255 | void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
 | 8256 | { | 
 | 8257 |     Mutex::Autolock _l(mLock); | 
 | 8258 |     if (stream == mStreamType) { | 
 | 8259 |         mStreamMute= muted; | 
 | 8260 |         broadcast_l(); | 
 | 8261 |     } | 
 | 8262 | } | 
 | 8263 |  | 
 | 8264 | void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
 | 8265 | { | 
 | 8266 |     Mutex::Autolock _l(mLock); | 
 | 8267 |     if (streamType == mStreamType) { | 
 | 8268 |         for (const sp<MmapTrack> &track : mActiveTracks) { | 
 | 8269 |             track->invalidate(); | 
 | 8270 |         } | 
 | 8271 |         broadcast_l(); | 
 | 8272 |     } | 
 | 8273 | } | 
 | 8274 |  | 
 | 8275 | void AudioFlinger::MmapPlaybackThread::processVolume_l() | 
 | 8276 | { | 
 | 8277 |     float volume; | 
 | 8278 |  | 
 | 8279 |     if (mMasterMute || mStreamMute) { | 
 | 8280 |         volume = 0; | 
 | 8281 |     } else { | 
 | 8282 |         volume = mMasterVolume * mStreamVolume; | 
 | 8283 |     } | 
 | 8284 |  | 
 | 8285 |     if (volume != mHalVolFloat) { | 
 | 8286 |         mHalVolFloat = volume; | 
 | 8287 |  | 
 | 8288 |         // Convert volumes from float to 8.24 | 
 | 8289 |         uint32_t vol = (uint32_t)(volume * (1 << 24)); | 
 | 8290 |  | 
 | 8291 |         // Delegate volume control to effect in track effect chain if needed | 
 | 8292 |         // only one effect chain can be present on DirectOutputThread, so if | 
 | 8293 |         // there is one, the track is connected to it | 
 | 8294 |         if (!mEffectChains.isEmpty()) { | 
 | 8295 |             mEffectChains[0]->setVolume_l(&vol, &vol); | 
 | 8296 |             volume = (float)vol / (1 << 24); | 
 | 8297 |         } | 
 | 8298 |  | 
 | 8299 |         mOutput->stream->setVolume(volume, volume); | 
 | 8300 |  | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8301 |         sp<MmapStreamCallback> callback = mCallback.promote(); | 
 | 8302 |         if (callback != 0) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8303 |             int channelCount; | 
 | 8304 |             if (isOutput()) { | 
 | 8305 |                 channelCount = audio_channel_count_from_out_mask(mChannelMask); | 
 | 8306 |             } else { | 
 | 8307 |                 channelCount = audio_channel_count_from_in_mask(mChannelMask); | 
 | 8308 |             } | 
 | 8309 |             Vector<float> values; | 
 | 8310 |             for (int i = 0; i < channelCount; i++) { | 
 | 8311 |                 values.add(volume); | 
 | 8312 |             } | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 8313 |             callback->onVolumeChanged(mChannelMask, values); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8314 |         } | 
 | 8315 |     } | 
 | 8316 | } | 
 | 8317 |  | 
 | 8318 | void AudioFlinger::MmapPlaybackThread::checkSilentMode_l() | 
 | 8319 | { | 
 | 8320 |     if (!mMasterMute) { | 
 | 8321 |         char value[PROPERTY_VALUE_MAX]; | 
 | 8322 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 8323 |             char *endptr; | 
 | 8324 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 8325 |             if (*endptr == '\0' && ul != 0) { | 
 | 8326 |                 ALOGD("Silence is golden"); | 
 | 8327 |                 // The setprop command will not allow a property to be changed after | 
 | 8328 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 8329 |                 setMasterMute_l(true); | 
 | 8330 |             } | 
 | 8331 |         } | 
 | 8332 |     } | 
 | 8333 | } | 
 | 8334 |  | 
 | 8335 | void AudioFlinger::MmapPlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 8336 | { | 
 | 8337 |     MmapThread::dumpInternals(fd, args); | 
 | 8338 |  | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 8339 |     dprintf(fd, "  Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", | 
 | 8340 |             mStreamType, mStreamVolume, mHalVolFloat, mStreamMute); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8341 |     dprintf(fd, "  Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); | 
 | 8342 | } | 
 | 8343 |  | 
 | 8344 | AudioFlinger::MmapCaptureThread::MmapCaptureThread( | 
 | 8345 |         const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
 | 8346 |         AudioHwDevice *hwDev,  AudioStreamIn *input, | 
 | 8347 |         audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady) | 
 | 8348 |     : MmapThread(audioFlinger, id, hwDev, input->stream, outDevice, inDevice, systemReady), | 
 | 8349 |       mInput(input) | 
 | 8350 | { | 
 | 8351 |     snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); | 
 | 8352 |     mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
 | 8353 | } | 
 | 8354 |  | 
 | 8355 | AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput() | 
 | 8356 | { | 
 | 8357 |     Mutex::Autolock _l(mLock); | 
 | 8358 |     AudioStreamIn *input = mInput; | 
 | 8359 |     mInput = NULL; | 
 | 8360 |     return input; | 
 | 8361 | } | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 8362 | } // namespace android |