| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* | 
 | 2 | ** | 
 | 3 | ** Copyright 2012, The Android Open Source Project | 
 | 4 | ** | 
 | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 6 | ** you may not use this file except in compliance with the License. | 
 | 7 | ** You may obtain a copy of the License at | 
 | 8 | ** | 
 | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 10 | ** | 
 | 11 | ** Unless required by applicable law or agreed to in writing, software | 
 | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
 | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 14 | ** See the License for the specific language governing permissions and | 
 | 15 | ** limitations under the License. | 
 | 16 | */ | 
 | 17 |  | 
 | 18 |  | 
 | 19 | #define LOG_TAG "AudioFlinger" | 
 | 20 | //#define LOG_NDEBUG 0 | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 22 |  | 
| Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 23 | #include "Configuration.h" | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 24 | #include <math.h> | 
 | 25 | #include <fcntl.h> | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 26 | #include <linux/futex.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> | 
| Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> | 
| Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/AudioParameter.h> | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 31 | #include <media/AudioResamplerPublic.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 34 |  | 
 | 35 | #include <private/media/AudioTrackShared.h> | 
 | 36 | #include <hardware/audio.h> | 
 | 37 | #include <audio_effects/effect_ns.h> | 
 | 38 | #include <audio_effects/effect_aec.h> | 
 | 39 | #include <audio_utils/primitives.h> | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 40 | #include <audio_utils/format.h> | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 41 | #include <audio_utils/minifloat.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 42 |  | 
 | 43 | // NBAIO implementations | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 44 | #include <media/nbaio/AudioStreamInSource.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 45 | #include <media/nbaio/AudioStreamOutSink.h> | 
 | 46 | #include <media/nbaio/MonoPipe.h> | 
 | 47 | #include <media/nbaio/MonoPipeReader.h> | 
 | 48 | #include <media/nbaio/Pipe.h> | 
 | 49 | #include <media/nbaio/PipeReader.h> | 
 | 50 | #include <media/nbaio/SourceAudioBufferProvider.h> | 
 | 51 |  | 
 | 52 | #include <powermanager/PowerManager.h> | 
 | 53 |  | 
 | 54 | #include <common_time/cc_helper.h> | 
 | 55 | #include <common_time/local_clock.h> | 
 | 56 |  | 
 | 57 | #include "AudioFlinger.h" | 
 | 58 | #include "AudioMixer.h" | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 59 | #include "BufferProviders.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 |  | 
 | 75 | // ---------------------------------------------------------------------------- | 
 | 76 |  | 
 | 77 | // Note: the following macro is used for extremely verbose logging message.  In | 
 | 78 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
 | 79 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
 | 80 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
 | 81 | // turned on.  Do not uncomment the #def below unless you really know what you | 
 | 82 | // are doing and want to see all of the extremely verbose messages. | 
 | 83 | //#define VERY_VERY_VERBOSE_LOGGING | 
 | 84 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
 | 85 | #define ALOGVV ALOGV | 
 | 86 | #else | 
 | 87 | #define ALOGVV(a...) do { } while(0) | 
 | 88 | #endif | 
 | 89 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 90 | // TODO: Move these macro/inlines to a header file. | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 91 | #define max(a, b) ((a) > (b) ? (a) : (b)) | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 92 | template <typename T> | 
 | 93 | static inline T min(const T& a, const T& b) | 
 | 94 | { | 
 | 95 |     return a < b ? a : b; | 
 | 96 | } | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 97 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 98 | #ifndef ARRAY_SIZE | 
| Chih-Hung Hsieh | bf29173 | 2016-05-17 15:16:07 -0700 | [diff] [blame^] | 99 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 100 | #endif | 
 | 101 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 102 | namespace android { | 
 | 103 |  | 
 | 104 | // retry counts for buffer fill timeout | 
 | 105 | // 50 * ~20msecs = 1 second | 
 | 106 | static const int8_t kMaxTrackRetries = 50; | 
 | 107 | static const int8_t kMaxTrackStartupRetries = 50; | 
 | 108 | // allow less retry attempts on direct output thread. | 
 | 109 | // direct outputs can be a scarce resource in audio hardware and should | 
 | 110 | // be released as quickly as possible. | 
 | 111 | static const int8_t kMaxTrackRetriesDirect = 2; | 
 | 112 |  | 
 | 113 | // don't warn about blocked writes or record buffer overflows more often than this | 
 | 114 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
 | 115 |  | 
 | 116 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
 | 117 | static const int kRecordThreadSleepUs = 5000; | 
 | 118 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 119 | // maximum time to wait in sendConfigEvent_l() for a status to be received | 
 | 120 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 121 |  | 
 | 122 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
 | 123 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
 | 124 | // maximum divider applied to the active sleep time in the mixer thread loop | 
 | 125 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
 | 126 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 127 | // minimum normal sink buffer size, expressed in milliseconds rather than frames | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 128 | // FIXME This should be based on experimentally observed scheduling jitter | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 129 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; | 
 | 130 | // maximum normal sink buffer size | 
 | 131 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 132 |  | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 133 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread | 
 | 134 | // FIXME This should be based on experimentally observed scheduling jitter | 
 | 135 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; | 
 | 136 |  | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 137 | // Offloaded output thread standby delay: allows track transition without going to standby | 
 | 138 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); | 
 | 139 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 140 | // Whether to use fast mixer | 
 | 141 | static const enum { | 
 | 142 |     FastMixer_Never,    // never initialize or use: for debugging only | 
 | 143 |     FastMixer_Always,   // always initialize and use, even if not needed: for debugging only | 
 | 144 |                         // normal mixer multiplier is 1 | 
 | 145 |     FastMixer_Static,   // initialize if needed, then use all the time if initialized, | 
 | 146 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 147 |     FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load, | 
 | 148 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 149 |     // FIXME for FastMixer_Dynamic: | 
 | 150 |     //  Supporting this option will require fixing HALs that can't handle large writes. | 
 | 151 |     //  For example, one HAL implementation returns an error from a large write, | 
 | 152 |     //  and another HAL implementation corrupts memory, possibly in the sample rate converter. | 
 | 153 |     //  We could either fix the HAL implementations, or provide a wrapper that breaks | 
 | 154 |     //  up large writes into smaller ones, and the wrapper would need to deal with scheduler. | 
 | 155 | } kUseFastMixer = FastMixer_Static; | 
 | 156 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 157 | // Whether to use fast capture | 
 | 158 | static const enum { | 
 | 159 |     FastCapture_Never,  // never initialize or use: for debugging only | 
 | 160 |     FastCapture_Always, // always initialize and use, even if not needed: for debugging only | 
 | 161 |     FastCapture_Static, // initialize if needed, then use all the time if initialized | 
 | 162 | } kUseFastCapture = FastCapture_Static; | 
 | 163 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 164 | // Priorities for requestPriority | 
 | 165 | static const int kPriorityAudioApp = 2; | 
 | 166 | static const int kPriorityFastMixer = 3; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 167 | static const int kPriorityFastCapture = 3; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 168 |  | 
 | 169 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area | 
 | 170 | // for the track.  The client then sub-divides this into smaller buffers for its use. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 171 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. | 
 | 172 | // So for now we just assume that client is double-buffered for fast tracks. | 
 | 173 | // FIXME It would be better for client to tell AudioFlinger the value of N, | 
 | 174 | // so AudioFlinger could allocate the right amount of memory. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 175 | // See the client's minBufCount and mNotificationFramesAct calculations for details. | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 176 |  | 
 | 177 | // This is the default value, if not specified by property. | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 178 | static const int kFastTrackMultiplier = 2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 179 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 180 | // The minimum and maximum allowed values | 
 | 181 | static const int kFastTrackMultiplierMin = 1; | 
 | 182 | static const int kFastTrackMultiplierMax = 2; | 
 | 183 |  | 
 | 184 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. | 
 | 185 | static int sFastTrackMultiplier = kFastTrackMultiplier; | 
 | 186 |  | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 187 | // See Thread::readOnlyHeap(). | 
 | 188 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. | 
 | 189 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), | 
 | 190 | // 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] | 191 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 192 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 193 | // ---------------------------------------------------------------------------- | 
 | 194 |  | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 195 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; | 
 | 196 |  | 
 | 197 | static void sFastTrackMultiplierInit() | 
 | 198 | { | 
 | 199 |     char value[PROPERTY_VALUE_MAX]; | 
 | 200 |     if (property_get("af.fast_track_multiplier", value, NULL) > 0) { | 
 | 201 |         char *endptr; | 
 | 202 |         unsigned long ul = strtoul(value, &endptr, 0); | 
 | 203 |         if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { | 
 | 204 |             sFastTrackMultiplier = (int) ul; | 
 | 205 |         } | 
 | 206 |     } | 
 | 207 | } | 
 | 208 |  | 
 | 209 | // ---------------------------------------------------------------------------- | 
 | 210 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 211 | #ifdef ADD_BATTERY_DATA | 
 | 212 | // To collect the amplifier usage | 
 | 213 | static void addBatteryData(uint32_t params) { | 
 | 214 |     sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
 | 215 |     if (service == NULL) { | 
 | 216 |         // it already logged | 
 | 217 |         return; | 
 | 218 |     } | 
 | 219 |  | 
 | 220 |     service->addBatteryData(params); | 
 | 221 | } | 
 | 222 | #endif | 
 | 223 |  | 
 | 224 |  | 
 | 225 | // ---------------------------------------------------------------------------- | 
 | 226 | //      CPU Stats | 
 | 227 | // ---------------------------------------------------------------------------- | 
 | 228 |  | 
 | 229 | class CpuStats { | 
 | 230 | public: | 
 | 231 |     CpuStats(); | 
 | 232 |     void sample(const String8 &title); | 
 | 233 | #ifdef DEBUG_CPU_USAGE | 
 | 234 | private: | 
 | 235 |     ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns | 
 | 236 |     CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns | 
 | 237 |  | 
 | 238 |     CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles | 
 | 239 |  | 
 | 240 |     int mCpuNum;                        // thread's current CPU number | 
 | 241 |     int mCpukHz;                        // frequency of thread's current CPU in kHz | 
 | 242 | #endif | 
 | 243 | }; | 
 | 244 |  | 
 | 245 | CpuStats::CpuStats() | 
 | 246 | #ifdef DEBUG_CPU_USAGE | 
 | 247 |     : mCpuNum(-1), mCpukHz(-1) | 
 | 248 | #endif | 
 | 249 | { | 
 | 250 | } | 
 | 251 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 252 | void CpuStats::sample(const String8 &title | 
 | 253 | #ifndef DEBUG_CPU_USAGE | 
 | 254 |                 __unused | 
 | 255 | #endif | 
 | 256 |         ) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 257 | #ifdef DEBUG_CPU_USAGE | 
 | 258 |     // get current thread's delta CPU time in wall clock ns | 
 | 259 |     double wcNs; | 
 | 260 |     bool valid = mCpuUsage.sampleAndEnable(wcNs); | 
 | 261 |  | 
 | 262 |     // record sample for wall clock statistics | 
 | 263 |     if (valid) { | 
 | 264 |         mWcStats.sample(wcNs); | 
 | 265 |     } | 
 | 266 |  | 
 | 267 |     // get the current CPU number | 
 | 268 |     int cpuNum = sched_getcpu(); | 
 | 269 |  | 
 | 270 |     // get the current CPU frequency in kHz | 
 | 271 |     int cpukHz = mCpuUsage.getCpukHz(cpuNum); | 
 | 272 |  | 
 | 273 |     // check if either CPU number or frequency changed | 
 | 274 |     if (cpuNum != mCpuNum || cpukHz != mCpukHz) { | 
 | 275 |         mCpuNum = cpuNum; | 
 | 276 |         mCpukHz = cpukHz; | 
 | 277 |         // ignore sample for purposes of cycles | 
 | 278 |         valid = false; | 
 | 279 |     } | 
 | 280 |  | 
 | 281 |     // if no change in CPU number or frequency, then record sample for cycle statistics | 
 | 282 |     if (valid && mCpukHz > 0) { | 
 | 283 |         double cycles = wcNs * cpukHz * 0.000001; | 
 | 284 |         mHzStats.sample(cycles); | 
 | 285 |     } | 
 | 286 |  | 
 | 287 |     unsigned n = mWcStats.n(); | 
 | 288 |     // mCpuUsage.elapsed() is expensive, so don't call it every loop | 
 | 289 |     if ((n & 127) == 1) { | 
 | 290 |         long long elapsed = mCpuUsage.elapsed(); | 
 | 291 |         if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
 | 292 |             double perLoop = elapsed / (double) n; | 
 | 293 |             double perLoop100 = perLoop * 0.01; | 
 | 294 |             double perLoop1k = perLoop * 0.001; | 
 | 295 |             double mean = mWcStats.mean(); | 
 | 296 |             double stddev = mWcStats.stddev(); | 
 | 297 |             double minimum = mWcStats.minimum(); | 
 | 298 |             double maximum = mWcStats.maximum(); | 
 | 299 |             double meanCycles = mHzStats.mean(); | 
 | 300 |             double stddevCycles = mHzStats.stddev(); | 
 | 301 |             double minCycles = mHzStats.minimum(); | 
 | 302 |             double maxCycles = mHzStats.maximum(); | 
 | 303 |             mCpuUsage.resetElapsed(); | 
 | 304 |             mWcStats.reset(); | 
 | 305 |             mHzStats.reset(); | 
 | 306 |             ALOGD("CPU usage for %s over past %.1f secs\n" | 
 | 307 |                 "  (%u mixer loops at %.1f mean ms per loop):\n" | 
 | 308 |                 "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" | 
 | 309 |                 "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" | 
 | 310 |                 "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", | 
 | 311 |                     title.string(), | 
 | 312 |                     elapsed * .000000001, n, perLoop * .000001, | 
 | 313 |                     mean * .001, | 
 | 314 |                     stddev * .001, | 
 | 315 |                     minimum * .001, | 
 | 316 |                     maximum * .001, | 
 | 317 |                     mean / perLoop100, | 
 | 318 |                     stddev / perLoop100, | 
 | 319 |                     minimum / perLoop100, | 
 | 320 |                     maximum / perLoop100, | 
 | 321 |                     meanCycles / perLoop1k, | 
 | 322 |                     stddevCycles / perLoop1k, | 
 | 323 |                     minCycles / perLoop1k, | 
 | 324 |                     maxCycles / perLoop1k); | 
 | 325 |  | 
 | 326 |         } | 
 | 327 |     } | 
 | 328 | #endif | 
 | 329 | }; | 
 | 330 |  | 
 | 331 | // ---------------------------------------------------------------------------- | 
 | 332 | //      ThreadBase | 
 | 333 | // ---------------------------------------------------------------------------- | 
 | 334 |  | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 335 | // static | 
 | 336 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) | 
 | 337 | { | 
 | 338 |     switch (type) { | 
 | 339 |     case MIXER: | 
 | 340 |         return "MIXER"; | 
 | 341 |     case DIRECT: | 
 | 342 |         return "DIRECT"; | 
 | 343 |     case DUPLICATING: | 
 | 344 |         return "DUPLICATING"; | 
 | 345 |     case RECORD: | 
 | 346 |         return "RECORD"; | 
 | 347 |     case OFFLOAD: | 
 | 348 |         return "OFFLOAD"; | 
 | 349 |     default: | 
 | 350 |         return "unknown"; | 
 | 351 |     } | 
 | 352 | } | 
 | 353 |  | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 354 | String8 devicesToString(audio_devices_t devices) | 
 | 355 | { | 
 | 356 |     static const struct mapping { | 
 | 357 |         audio_devices_t mDevices; | 
 | 358 |         const char *    mString; | 
 | 359 |     } mappingsOut[] = { | 
 | 360 |         AUDIO_DEVICE_OUT_EARPIECE,          "EARPIECE", | 
 | 361 |         AUDIO_DEVICE_OUT_SPEAKER,           "SPEAKER", | 
 | 362 |         AUDIO_DEVICE_OUT_WIRED_HEADSET,     "WIRED_HEADSET", | 
 | 363 |         AUDIO_DEVICE_OUT_WIRED_HEADPHONE,   "WIRED_HEADPHONE", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 364 |         AUDIO_DEVICE_OUT_BLUETOOTH_SCO,     "BLUETOOTH_SCO", | 
 | 365 |         AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     "BLUETOOTH_SCO_HEADSET", | 
 | 366 |         AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      "BLUETOOTH_SCO_CARKIT", | 
 | 367 |         AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            "BLUETOOTH_A2DP", | 
 | 368 |         AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, "BLUETOOTH_A2DP_HEADPHONES", | 
 | 369 |         AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    "BLUETOOTH_A2DP_SPEAKER", | 
 | 370 |         AUDIO_DEVICE_OUT_AUX_DIGITAL,       "AUX_DIGITAL", | 
 | 371 |         AUDIO_DEVICE_OUT_HDMI,              "HDMI", | 
 | 372 |         AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET", | 
 | 373 |         AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET", | 
 | 374 |         AUDIO_DEVICE_OUT_USB_ACCESSORY,     "USB_ACCESSORY", | 
 | 375 |         AUDIO_DEVICE_OUT_USB_DEVICE,        "USB_DEVICE", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 376 |         AUDIO_DEVICE_OUT_TELEPHONY_TX,      "TELEPHONY_TX", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 377 |         AUDIO_DEVICE_OUT_LINE,              "LINE", | 
 | 378 |         AUDIO_DEVICE_OUT_HDMI_ARC,          "HDMI_ARC", | 
 | 379 |         AUDIO_DEVICE_OUT_SPDIF,             "SPDIF", | 
 | 380 |         AUDIO_DEVICE_OUT_FM,                "FM", | 
 | 381 |         AUDIO_DEVICE_OUT_AUX_LINE,          "AUX_LINE", | 
 | 382 |         AUDIO_DEVICE_OUT_SPEAKER_SAFE,      "SPEAKER_SAFE", | 
| Eric Laurent | b9d7333 | 2015-06-30 17:09:20 -0700 | [diff] [blame] | 383 |         AUDIO_DEVICE_OUT_IP,                "IP", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 384 |         AUDIO_DEVICE_NONE,                  "NONE",         // must be last | 
 | 385 |     }, mappingsIn[] = { | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 386 |         AUDIO_DEVICE_IN_COMMUNICATION,      "COMMUNICATION", | 
 | 387 |         AUDIO_DEVICE_IN_AMBIENT,            "AMBIENT", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 388 |         AUDIO_DEVICE_IN_BUILTIN_MIC,        "BUILTIN_MIC", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 389 |         AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,  "BLUETOOTH_SCO_HEADSET", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 390 |         AUDIO_DEVICE_IN_WIRED_HEADSET,      "WIRED_HEADSET", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 391 |         AUDIO_DEVICE_IN_AUX_DIGITAL,        "AUX_DIGITAL", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 392 |         AUDIO_DEVICE_IN_VOICE_CALL,         "VOICE_CALL", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 393 |         AUDIO_DEVICE_IN_TELEPHONY_RX,       "TELEPHONY_RX", | 
 | 394 |         AUDIO_DEVICE_IN_BACK_MIC,           "BACK_MIC", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 395 |         AUDIO_DEVICE_IN_REMOTE_SUBMIX,      "REMOTE_SUBMIX", | 
| Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 396 |         AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET,  "ANLG_DOCK_HEADSET", | 
 | 397 |         AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET,  "DGTL_DOCK_HEADSET", | 
 | 398 |         AUDIO_DEVICE_IN_USB_ACCESSORY,      "USB_ACCESSORY", | 
 | 399 |         AUDIO_DEVICE_IN_USB_DEVICE,         "USB_DEVICE", | 
 | 400 |         AUDIO_DEVICE_IN_FM_TUNER,           "FM_TUNER", | 
 | 401 |         AUDIO_DEVICE_IN_TV_TUNER,           "TV_TUNER", | 
 | 402 |         AUDIO_DEVICE_IN_LINE,               "LINE", | 
 | 403 |         AUDIO_DEVICE_IN_SPDIF,              "SPDIF", | 
 | 404 |         AUDIO_DEVICE_IN_BLUETOOTH_A2DP,     "BLUETOOTH_A2DP", | 
 | 405 |         AUDIO_DEVICE_IN_LOOPBACK,           "LOOPBACK", | 
| Eric Laurent | b9d7333 | 2015-06-30 17:09:20 -0700 | [diff] [blame] | 406 |         AUDIO_DEVICE_IN_IP,                 "IP", | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 407 |         AUDIO_DEVICE_NONE,                  "NONE",         // must be last | 
 | 408 |     }; | 
 | 409 |     String8 result; | 
 | 410 |     audio_devices_t allDevices = AUDIO_DEVICE_NONE; | 
 | 411 |     const mapping *entry; | 
 | 412 |     if (devices & AUDIO_DEVICE_BIT_IN) { | 
 | 413 |         devices &= ~AUDIO_DEVICE_BIT_IN; | 
 | 414 |         entry = mappingsIn; | 
 | 415 |     } else { | 
 | 416 |         entry = mappingsOut; | 
 | 417 |     } | 
 | 418 |     for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { | 
 | 419 |         allDevices = (audio_devices_t) (allDevices | entry->mDevices); | 
 | 420 |         if (devices & entry->mDevices) { | 
 | 421 |             if (!result.isEmpty()) { | 
 | 422 |                 result.append("|"); | 
 | 423 |             } | 
 | 424 |             result.append(entry->mString); | 
 | 425 |         } | 
 | 426 |     } | 
 | 427 |     if (devices & ~allDevices) { | 
 | 428 |         if (!result.isEmpty()) { | 
 | 429 |             result.append("|"); | 
 | 430 |         } | 
 | 431 |         result.appendFormat("0x%X", devices & ~allDevices); | 
 | 432 |     } | 
 | 433 |     if (result.isEmpty()) { | 
 | 434 |         result.append(entry->mString); | 
 | 435 |     } | 
 | 436 |     return result; | 
 | 437 | } | 
 | 438 |  | 
 | 439 | String8 inputFlagsToString(audio_input_flags_t flags) | 
 | 440 | { | 
 | 441 |     static const struct mapping { | 
 | 442 |         audio_input_flags_t     mFlag; | 
 | 443 |         const char *            mString; | 
 | 444 |     } mappings[] = { | 
 | 445 |         AUDIO_INPUT_FLAG_FAST,              "FAST", | 
 | 446 |         AUDIO_INPUT_FLAG_HW_HOTWORD,        "HW_HOTWORD", | 
 | 447 |         AUDIO_INPUT_FLAG_NONE,              "NONE",         // must be last | 
 | 448 |     }; | 
 | 449 |     String8 result; | 
 | 450 |     audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; | 
 | 451 |     const mapping *entry; | 
 | 452 |     for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { | 
 | 453 |         allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); | 
 | 454 |         if (flags & entry->mFlag) { | 
 | 455 |             if (!result.isEmpty()) { | 
 | 456 |                 result.append("|"); | 
 | 457 |             } | 
 | 458 |             result.append(entry->mString); | 
 | 459 |         } | 
 | 460 |     } | 
 | 461 |     if (flags & ~allFlags) { | 
 | 462 |         if (!result.isEmpty()) { | 
 | 463 |             result.append("|"); | 
 | 464 |         } | 
 | 465 |         result.appendFormat("0x%X", flags & ~allFlags); | 
 | 466 |     } | 
 | 467 |     if (result.isEmpty()) { | 
 | 468 |         result.append(entry->mString); | 
 | 469 |     } | 
 | 470 |     return result; | 
 | 471 | } | 
 | 472 |  | 
 | 473 | String8 outputFlagsToString(audio_output_flags_t flags) | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 474 | { | 
 | 475 |     static const struct mapping { | 
 | 476 |         audio_output_flags_t    mFlag; | 
 | 477 |         const char *            mString; | 
 | 478 |     } mappings[] = { | 
 | 479 |         AUDIO_OUTPUT_FLAG_DIRECT,           "DIRECT", | 
 | 480 |         AUDIO_OUTPUT_FLAG_PRIMARY,          "PRIMARY", | 
 | 481 |         AUDIO_OUTPUT_FLAG_FAST,             "FAST", | 
 | 482 |         AUDIO_OUTPUT_FLAG_DEEP_BUFFER,      "DEEP_BUFFER", | 
| Glenn Kasten | dfb0e11 | 2015-02-18 14:33:39 -0800 | [diff] [blame] | 483 |         AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD", | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 484 |         AUDIO_OUTPUT_FLAG_NON_BLOCKING,     "NON_BLOCKING", | 
 | 485 |         AUDIO_OUTPUT_FLAG_HW_AV_SYNC,       "HW_AV_SYNC", | 
 | 486 |         AUDIO_OUTPUT_FLAG_NONE,             "NONE",         // must be last | 
 | 487 |     }; | 
 | 488 |     String8 result; | 
 | 489 |     audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; | 
 | 490 |     const mapping *entry; | 
 | 491 |     for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { | 
 | 492 |         allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); | 
 | 493 |         if (flags & entry->mFlag) { | 
 | 494 |             if (!result.isEmpty()) { | 
 | 495 |                 result.append("|"); | 
 | 496 |             } | 
 | 497 |             result.append(entry->mString); | 
 | 498 |         } | 
 | 499 |     } | 
 | 500 |     if (flags & ~allFlags) { | 
 | 501 |         if (!result.isEmpty()) { | 
 | 502 |             result.append("|"); | 
 | 503 |         } | 
 | 504 |         result.appendFormat("0x%X", flags & ~allFlags); | 
 | 505 |     } | 
 | 506 |     if (result.isEmpty()) { | 
 | 507 |         result.append(entry->mString); | 
 | 508 |     } | 
 | 509 |     return result; | 
 | 510 | } | 
 | 511 |  | 
| Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 512 | const char *sourceToString(audio_source_t source) | 
 | 513 | { | 
 | 514 |     switch (source) { | 
 | 515 |     case AUDIO_SOURCE_DEFAULT:              return "default"; | 
 | 516 |     case AUDIO_SOURCE_MIC:                  return "mic"; | 
 | 517 |     case AUDIO_SOURCE_VOICE_UPLINK:         return "voice uplink"; | 
 | 518 |     case AUDIO_SOURCE_VOICE_DOWNLINK:       return "voice downlink"; | 
 | 519 |     case AUDIO_SOURCE_VOICE_CALL:           return "voice call"; | 
 | 520 |     case AUDIO_SOURCE_CAMCORDER:            return "camcorder"; | 
 | 521 |     case AUDIO_SOURCE_VOICE_RECOGNITION:    return "voice recognition"; | 
 | 522 |     case AUDIO_SOURCE_VOICE_COMMUNICATION:  return "voice communication"; | 
 | 523 |     case AUDIO_SOURCE_REMOTE_SUBMIX:        return "remote submix"; | 
 | 524 |     case AUDIO_SOURCE_FM_TUNER:             return "FM tuner"; | 
 | 525 |     case AUDIO_SOURCE_HOTWORD:              return "hotword"; | 
 | 526 |     default:                                return "unknown"; | 
 | 527 |     } | 
 | 528 | } | 
 | 529 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 530 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 531 |         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] | 532 |     :   Thread(false /*canCallJava*/), | 
 | 533 |         mType(type), | 
| Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 534 |         mAudioFlinger(audioFlinger), | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 535 |         // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 536 |         // are set by PlaybackThread::readOutputParameters_l() or | 
 | 537 |         // RecordThread::readInputParameters_l() | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 538 |         //FIXME: mStandby should be true here. Is this some kind of hack? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 539 |         mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 540 |         mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), | 
 | 541 |         mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 542 |         // mName will be set by concrete (non-virtual) subclass | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 543 |         mDeathRecipient(new PMDeathRecipient(this)), | 
 | 544 |         mSystemReady(systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 545 | { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 546 |     memset(&mPatch, 0, sizeof(struct audio_patch)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 547 | } | 
 | 548 |  | 
 | 549 | AudioFlinger::ThreadBase::~ThreadBase() | 
 | 550 | { | 
| Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 551 |     // 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] | 552 |     mConfigEvents.clear(); | 
 | 553 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 554 |     // do not lock the mutex in destructor | 
 | 555 |     releaseWakeLock_l(); | 
 | 556 |     if (mPowerManager != 0) { | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 557 |         sp<IBinder> binder = IInterface::asBinder(mPowerManager); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 558 |         binder->unlinkToDeath(mDeathRecipient); | 
 | 559 |     } | 
 | 560 | } | 
 | 561 |  | 
| Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 562 | status_t AudioFlinger::ThreadBase::readyToRun() | 
 | 563 | { | 
 | 564 |     status_t status = initCheck(); | 
 | 565 |     if (status == NO_ERROR) { | 
 | 566 |         ALOGI("AudioFlinger's thread %p ready to run", this); | 
 | 567 |     } else { | 
 | 568 |         ALOGE("No working audio driver found."); | 
 | 569 |     } | 
 | 570 |     return status; | 
 | 571 | } | 
 | 572 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 573 | void AudioFlinger::ThreadBase::exit() | 
 | 574 | { | 
 | 575 |     ALOGV("ThreadBase::exit"); | 
 | 576 |     // do any cleanup required for exit to succeed | 
 | 577 |     preExit(); | 
 | 578 |     { | 
 | 579 |         // This lock prevents the following race in thread (uniprocessor for illustration): | 
 | 580 |         //  if (!exitPending()) { | 
 | 581 |         //      // context switch from here to exit() | 
 | 582 |         //      // exit() calls requestExit(), what exitPending() observes | 
 | 583 |         //      // exit() calls signal(), which is dropped since no waiters | 
 | 584 |         //      // context switch back from exit() to here | 
 | 585 |         //      mWaitWorkCV.wait(...); | 
 | 586 |         //      // now thread is hung | 
 | 587 |         //  } | 
 | 588 |         AutoMutex lock(mLock); | 
 | 589 |         requestExit(); | 
 | 590 |         mWaitWorkCV.broadcast(); | 
 | 591 |     } | 
 | 592 |     // When Thread::requestExitAndWait is made virtual and this method is renamed to | 
 | 593 |     // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" | 
 | 594 |     requestExitAndWait(); | 
 | 595 | } | 
 | 596 |  | 
 | 597 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
 | 598 | { | 
 | 599 |     status_t status; | 
 | 600 |  | 
 | 601 |     ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
 | 602 |     Mutex::Autolock _l(mLock); | 
 | 603 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 604 |     return sendSetParameterConfigEvent_l(keyValuePairs); | 
 | 605 | } | 
 | 606 |  | 
 | 607 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 608 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). | 
 | 609 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) | 
 | 610 | { | 
 | 611 |     status_t status = NO_ERROR; | 
 | 612 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 613 |     if (event->mRequiresSystemReady && !mSystemReady) { | 
 | 614 |         event->mWaitStatus = false; | 
 | 615 |         mPendingConfigEvents.add(event); | 
 | 616 |         return status; | 
 | 617 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 618 |     mConfigEvents.add(event); | 
 | 619 |     ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 620 |     mWaitWorkCV.signal(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 621 |     mLock.unlock(); | 
 | 622 |     { | 
 | 623 |         Mutex::Autolock _l(event->mLock); | 
 | 624 |         while (event->mWaitStatus) { | 
 | 625 |             if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { | 
 | 626 |                 event->mStatus = TIMED_OUT; | 
 | 627 |                 event->mWaitStatus = false; | 
 | 628 |             } | 
 | 629 |         } | 
 | 630 |         status = event->mStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 631 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 632 |     mLock.lock(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 633 |     return status; | 
 | 634 | } | 
 | 635 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 636 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 637 | { | 
 | 638 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 639 |     sendIoConfigEvent_l(event, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 640 | } | 
 | 641 |  | 
 | 642 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 643 | 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] | 644 | { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 645 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 646 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 647 | } | 
 | 648 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 649 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) | 
 | 650 | { | 
 | 651 |     Mutex::Autolock _l(mLock); | 
 | 652 |     sendPrioConfigEvent_l(pid, tid, prio); | 
 | 653 | } | 
 | 654 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 655 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 656 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) | 
 | 657 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 658 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); | 
 | 659 |     sendConfigEvent_l(configEvent); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 660 | } | 
 | 661 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 662 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 663 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 664 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 665 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); | 
 | 666 |     return sendConfigEvent_l(configEvent); | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 667 | } | 
 | 668 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 669 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( | 
 | 670 |                                                         const struct audio_patch *patch, | 
 | 671 |                                                         audio_patch_handle_t *handle) | 
 | 672 | { | 
 | 673 |     Mutex::Autolock _l(mLock); | 
 | 674 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); | 
 | 675 |     status_t status = sendConfigEvent_l(configEvent); | 
 | 676 |     if (status == NO_ERROR) { | 
 | 677 |         CreateAudioPatchConfigEventData *data = | 
 | 678 |                                         (CreateAudioPatchConfigEventData *)configEvent->mData.get(); | 
 | 679 |         *handle = data->mHandle; | 
 | 680 |     } | 
 | 681 |     return status; | 
 | 682 | } | 
 | 683 |  | 
 | 684 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( | 
 | 685 |                                                                 const audio_patch_handle_t handle) | 
 | 686 | { | 
 | 687 |     Mutex::Autolock _l(mLock); | 
 | 688 |     sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); | 
 | 689 |     return sendConfigEvent_l(configEvent); | 
 | 690 | } | 
 | 691 |  | 
 | 692 |  | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 693 | // post condition: mConfigEvents.isEmpty() | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 694 | void AudioFlinger::ThreadBase::processConfigEvents_l() | 
| Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 695 | { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 696 |     bool configChanged = false; | 
 | 697 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 698 |     while (!mConfigEvents.isEmpty()) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 699 |         ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); | 
 | 700 |         sp<ConfigEvent> event = mConfigEvents[0]; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 701 |         mConfigEvents.removeAt(0); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 702 |         switch (event->mType) { | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 703 |         case CFG_EVENT_PRIO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 704 |             PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); | 
 | 705 |             // FIXME Need to understand why this has to be done asynchronously | 
 | 706 |             int err = requestPriority(data->mPid, data->mTid, data->mPrio, | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 707 |                     true /*asynchronous*/); | 
 | 708 |             if (err != 0) { | 
 | 709 |                 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] | 710 |                       data->mPrio, data->mPid, data->mTid, err); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 711 |             } | 
 | 712 |         } break; | 
 | 713 |         case CFG_EVENT_IO: { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 714 |             IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 715 |             ioConfigChanged(data->mEvent, data->mPid); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 716 |         } break; | 
 | 717 |         case CFG_EVENT_SET_PARAMETER: { | 
 | 718 |             SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); | 
 | 719 |             if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { | 
 | 720 |                 configChanged = true; | 
| Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 721 |             } | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 722 |         } break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 723 |         case CFG_EVENT_CREATE_AUDIO_PATCH: { | 
 | 724 |             CreateAudioPatchConfigEventData *data = | 
 | 725 |                                             (CreateAudioPatchConfigEventData *)event->mData.get(); | 
 | 726 |             event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); | 
 | 727 |         } break; | 
 | 728 |         case CFG_EVENT_RELEASE_AUDIO_PATCH: { | 
 | 729 |             ReleaseAudioPatchConfigEventData *data = | 
 | 730 |                                             (ReleaseAudioPatchConfigEventData *)event->mData.get(); | 
 | 731 |             event->mStatus = releaseAudioPatch_l(data->mHandle); | 
 | 732 |         } break; | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 733 |         default: | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 734 |             ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); | 
| Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 735 |             break; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 736 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 737 |         { | 
 | 738 |             Mutex::Autolock _l(event->mLock); | 
 | 739 |             if (event->mWaitStatus) { | 
 | 740 |                 event->mWaitStatus = false; | 
 | 741 |                 event->mCond.signal(); | 
 | 742 |             } | 
 | 743 |         } | 
 | 744 |         ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); | 
 | 745 |     } | 
 | 746 |  | 
 | 747 |     if (configChanged) { | 
 | 748 |         cacheParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 749 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 750 | } | 
 | 751 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 752 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { | 
 | 753 |     String8 s; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 754 |     const audio_channel_representation_t representation = | 
 | 755 |             audio_channel_mask_get_representation(mask); | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 756 |  | 
 | 757 |     switch (representation) { | 
 | 758 |     case AUDIO_CHANNEL_REPRESENTATION_POSITION: { | 
 | 759 |         if (output) { | 
 | 760 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); | 
 | 761 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); | 
 | 762 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); | 
 | 763 |             if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); | 
 | 764 |             if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); | 
 | 765 |             if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); | 
 | 766 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); | 
 | 767 |             if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); | 
 | 768 |             if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); | 
 | 769 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); | 
 | 770 |             if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); | 
 | 771 |             if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); | 
 | 772 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); | 
 | 773 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); | 
 | 774 |             if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); | 
 | 775 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); | 
 | 776 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); | 
 | 777 |             if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); | 
 | 778 |             if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  "); | 
 | 779 |         } else { | 
 | 780 |             if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); | 
 | 781 |             if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); | 
 | 782 |             if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); | 
 | 783 |             if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); | 
 | 784 |             if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); | 
 | 785 |             if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); | 
 | 786 |             if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); | 
 | 787 |             if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); | 
 | 788 |             if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); | 
 | 789 |             if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); | 
 | 790 |             if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); | 
 | 791 |             if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); | 
 | 792 |             if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); | 
 | 793 |             if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); | 
 | 794 |             if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  "); | 
 | 795 |         } | 
 | 796 |         const int len = s.length(); | 
 | 797 |         if (len > 2) { | 
 | 798 |             char *str = s.lockBuffer(len); // needed? | 
 | 799 |             s.unlockBuffer(len - 2);       // remove trailing ", " | 
 | 800 |         } | 
 | 801 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 802 |     } | 
| Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 803 |     case AUDIO_CHANNEL_REPRESENTATION_INDEX: | 
 | 804 |         s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); | 
 | 805 |         return s; | 
 | 806 |     default: | 
 | 807 |         s.appendFormat("unknown mask, representation:%d  bits:%#x", | 
 | 808 |                 representation, audio_channel_mask_get_bits(mask)); | 
 | 809 |         return s; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 810 |     } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 811 | } | 
 | 812 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 813 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 814 | { | 
 | 815 |     const size_t SIZE = 256; | 
 | 816 |     char buffer[SIZE]; | 
 | 817 |     String8 result; | 
 | 818 |  | 
 | 819 |     bool locked = AudioFlinger::dumpTryLock(mLock); | 
 | 820 |     if (!locked) { | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 821 |         dprintf(fd, "thread %p may be deadlocked\n", this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 822 |     } | 
 | 823 |  | 
| Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 824 |     dprintf(fd, "  Thread name: %s\n", mThreadName); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 825 |     dprintf(fd, "  I/O handle: %d\n", mId); | 
 | 826 |     dprintf(fd, "  TID: %d\n", getTid()); | 
 | 827 |     dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no"); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 828 |     dprintf(fd, "  Sample rate: %u Hz\n", mSampleRate); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 829 |     dprintf(fd, "  HAL frame count: %zu\n", mFrameCount); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 830 |     dprintf(fd, "  HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 831 |     dprintf(fd, "  HAL buffer size: %u bytes\n", mBufferSize); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 832 |     dprintf(fd, "  Channel count: %u\n", mChannelCount); | 
 | 833 |     dprintf(fd, "  Channel mask: 0x%08x (%s)\n", mChannelMask, | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 834 |             channelMaskToString(mChannelMask, mType != RECORD).string()); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 835 |     dprintf(fd, "  Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); | 
 | 836 |     dprintf(fd, "  Frame size: %zu bytes\n", mFrameSize); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 837 |     dprintf(fd, "  Pending config events:"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 838 |     size_t numConfig = mConfigEvents.size(); | 
 | 839 |     if (numConfig) { | 
 | 840 |         for (size_t i = 0; i < numConfig; i++) { | 
 | 841 |             mConfigEvents[i]->dump(buffer, SIZE); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 842 |             dprintf(fd, "\n    %s", buffer); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 843 |         } | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 844 |         dprintf(fd, "\n"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 845 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 846 |         dprintf(fd, " none\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 847 |     } | 
| Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 848 |     dprintf(fd, "  Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); | 
 | 849 |     dprintf(fd, "  Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); | 
 | 850 |     dprintf(fd, "  Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 851 |  | 
 | 852 |     if (locked) { | 
 | 853 |         mLock.unlock(); | 
 | 854 |     } | 
 | 855 | } | 
 | 856 |  | 
 | 857 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
 | 858 | { | 
 | 859 |     const size_t SIZE = 256; | 
 | 860 |     char buffer[SIZE]; | 
 | 861 |     String8 result; | 
 | 862 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 863 |     size_t numEffectChains = mEffectChains.size(); | 
| Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 864 |     snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 865 |     write(fd, buffer, strlen(buffer)); | 
 | 866 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 867 |     for (size_t i = 0; i < numEffectChains; ++i) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 868 |         sp<EffectChain> chain = mEffectChains[i]; | 
 | 869 |         if (chain != 0) { | 
 | 870 |             chain->dump(fd, args); | 
 | 871 |         } | 
 | 872 |     } | 
 | 873 | } | 
 | 874 |  | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 875 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 876 | { | 
 | 877 |     Mutex::Autolock _l(mLock); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 878 |     acquireWakeLock_l(uid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 879 | } | 
 | 880 |  | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 881 | String16 AudioFlinger::ThreadBase::getWakeLockTag() | 
 | 882 | { | 
 | 883 |     switch (mType) { | 
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 884 |     case MIXER: | 
 | 885 |         return String16("AudioMix"); | 
 | 886 |     case DIRECT: | 
 | 887 |         return String16("AudioDirectOut"); | 
 | 888 |     case DUPLICATING: | 
 | 889 |         return String16("AudioDup"); | 
 | 890 |     case RECORD: | 
 | 891 |         return String16("AudioIn"); | 
 | 892 |     case OFFLOAD: | 
 | 893 |         return String16("AudioOffload"); | 
 | 894 |     default: | 
 | 895 |         ALOG_ASSERT(false); | 
 | 896 |         return String16("AudioUnknown"); | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 897 |     } | 
 | 898 | } | 
 | 899 |  | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 900 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 901 | { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 902 |     getPowerManager_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 903 |     if (mPowerManager != 0) { | 
 | 904 |         sp<IBinder> binder = new BBinder(); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 905 |         status_t status; | 
 | 906 |         if (uid >= 0) { | 
| Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 907 |             status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 908 |                     binder, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 909 |                     getWakeLockTag(), | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 910 |                     String16("media"), | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 911 |                     uid, | 
 | 912 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 913 |         } else { | 
| Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 914 |             status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 915 |                     binder, | 
| Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 916 |                     getWakeLockTag(), | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 917 |                     String16("media"), | 
 | 918 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 919 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 920 |         if (status == NO_ERROR) { | 
 | 921 |             mWakeLockToken = binder; | 
 | 922 |         } | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 923 |         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 924 |     } | 
 | 925 | } | 
 | 926 |  | 
 | 927 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
 | 928 | { | 
 | 929 |     Mutex::Autolock _l(mLock); | 
 | 930 |     releaseWakeLock_l(); | 
 | 931 | } | 
 | 932 |  | 
 | 933 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
 | 934 | { | 
 | 935 |     if (mWakeLockToken != 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 936 |         ALOGV("releaseWakeLock_l() %s", mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 937 |         if (mPowerManager != 0) { | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 938 |             mPowerManager->releaseWakeLock(mWakeLockToken, 0, | 
 | 939 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 940 |         } | 
 | 941 |         mWakeLockToken.clear(); | 
 | 942 |     } | 
 | 943 | } | 
 | 944 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 945 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { | 
 | 946 |     Mutex::Autolock _l(mLock); | 
 | 947 |     updateWakeLockUids_l(uids); | 
 | 948 | } | 
 | 949 |  | 
 | 950 | void AudioFlinger::ThreadBase::getPowerManager_l() { | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 951 |     if (mSystemReady && mPowerManager == 0) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 952 |         // use checkService() to avoid blocking if power service is not up yet | 
 | 953 |         sp<IBinder> binder = | 
 | 954 |             defaultServiceManager()->checkService(String16("power")); | 
 | 955 |         if (binder == 0) { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 956 |             ALOGW("Thread %s cannot connect to the power manager service", mThreadName); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 957 |         } else { | 
 | 958 |             mPowerManager = interface_cast<IPowerManager>(binder); | 
 | 959 |             binder->linkToDeath(mDeathRecipient); | 
 | 960 |         } | 
 | 961 |     } | 
 | 962 | } | 
 | 963 |  | 
 | 964 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 965 |     getPowerManager_l(); | 
 | 966 |     if (mWakeLockToken == NULL) { | 
 | 967 |         ALOGE("no wake lock to update!"); | 
 | 968 |         return; | 
 | 969 |     } | 
 | 970 |     if (mPowerManager != 0) { | 
 | 971 |         sp<IBinder> binder = new BBinder(); | 
 | 972 |         status_t status; | 
| Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 973 |         status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), | 
 | 974 |                     true /* FIXME force oneway contrary to .aidl */); | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 975 |         ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 976 |     } | 
 | 977 | } | 
 | 978 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 979 | void AudioFlinger::ThreadBase::clearPowerManager() | 
 | 980 | { | 
 | 981 |     Mutex::Autolock _l(mLock); | 
 | 982 |     releaseWakeLock_l(); | 
 | 983 |     mPowerManager.clear(); | 
 | 984 | } | 
 | 985 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 986 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 987 | { | 
 | 988 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 989 |     if (thread != 0) { | 
 | 990 |         thread->clearPowerManager(); | 
 | 991 |     } | 
 | 992 |     ALOGW("power manager service died !!!"); | 
 | 993 | } | 
 | 994 |  | 
 | 995 | void AudioFlinger::ThreadBase::setEffectSuspended( | 
 | 996 |         const effect_uuid_t *type, bool suspend, int sessionId) | 
 | 997 | { | 
 | 998 |     Mutex::Autolock _l(mLock); | 
 | 999 |     setEffectSuspended_l(type, suspend, sessionId); | 
 | 1000 | } | 
 | 1001 |  | 
 | 1002 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
 | 1003 |         const effect_uuid_t *type, bool suspend, int sessionId) | 
 | 1004 | { | 
 | 1005 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1006 |     if (chain != 0) { | 
 | 1007 |         if (type != NULL) { | 
 | 1008 |             chain->setEffectSuspended_l(type, suspend); | 
 | 1009 |         } else { | 
 | 1010 |             chain->setEffectSuspendedAll_l(suspend); | 
 | 1011 |         } | 
 | 1012 |     } | 
 | 1013 |  | 
 | 1014 |     updateSuspendedSessions_l(type, suspend, sessionId); | 
 | 1015 | } | 
 | 1016 |  | 
 | 1017 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
 | 1018 | { | 
 | 1019 |     ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
 | 1020 |     if (index < 0) { | 
 | 1021 |         return; | 
 | 1022 |     } | 
 | 1023 |  | 
 | 1024 |     const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = | 
 | 1025 |             mSuspendedSessions.valueAt(index); | 
 | 1026 |  | 
 | 1027 |     for (size_t i = 0; i < sessionEffects.size(); i++) { | 
 | 1028 |         sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); | 
 | 1029 |         for (int j = 0; j < desc->mRefCount; j++) { | 
 | 1030 |             if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
 | 1031 |                 chain->setEffectSuspendedAll_l(true); | 
 | 1032 |             } else { | 
 | 1033 |                 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
 | 1034 |                     desc->mType.timeLow); | 
 | 1035 |                 chain->setEffectSuspended_l(&desc->mType, true); | 
 | 1036 |             } | 
 | 1037 |         } | 
 | 1038 |     } | 
 | 1039 | } | 
 | 1040 |  | 
 | 1041 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
 | 1042 |                                                          bool suspend, | 
 | 1043 |                                                          int sessionId) | 
 | 1044 | { | 
 | 1045 |     ssize_t index = mSuspendedSessions.indexOfKey(sessionId); | 
 | 1046 |  | 
 | 1047 |     KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
 | 1048 |  | 
 | 1049 |     if (suspend) { | 
 | 1050 |         if (index >= 0) { | 
 | 1051 |             sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1052 |         } else { | 
 | 1053 |             mSuspendedSessions.add(sessionId, sessionEffects); | 
 | 1054 |         } | 
 | 1055 |     } else { | 
 | 1056 |         if (index < 0) { | 
 | 1057 |             return; | 
 | 1058 |         } | 
 | 1059 |         sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 1060 |     } | 
 | 1061 |  | 
 | 1062 |  | 
 | 1063 |     int key = EffectChain::kKeyForSuspendAll; | 
 | 1064 |     if (type != NULL) { | 
 | 1065 |         key = type->timeLow; | 
 | 1066 |     } | 
 | 1067 |     index = sessionEffects.indexOfKey(key); | 
 | 1068 |  | 
 | 1069 |     sp<SuspendedSessionDesc> desc; | 
 | 1070 |     if (suspend) { | 
 | 1071 |         if (index >= 0) { | 
 | 1072 |             desc = sessionEffects.valueAt(index); | 
 | 1073 |         } else { | 
 | 1074 |             desc = new SuspendedSessionDesc(); | 
 | 1075 |             if (type != NULL) { | 
 | 1076 |                 desc->mType = *type; | 
 | 1077 |             } | 
 | 1078 |             sessionEffects.add(key, desc); | 
 | 1079 |             ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
 | 1080 |         } | 
 | 1081 |         desc->mRefCount++; | 
 | 1082 |     } else { | 
 | 1083 |         if (index < 0) { | 
 | 1084 |             return; | 
 | 1085 |         } | 
 | 1086 |         desc = sessionEffects.valueAt(index); | 
 | 1087 |         if (--desc->mRefCount == 0) { | 
 | 1088 |             ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
 | 1089 |             sessionEffects.removeItemsAt(index); | 
 | 1090 |             if (sessionEffects.isEmpty()) { | 
 | 1091 |                 ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
 | 1092 |                                  sessionId); | 
 | 1093 |                 mSuspendedSessions.removeItem(sessionId); | 
 | 1094 |             } | 
 | 1095 |         } | 
 | 1096 |     } | 
 | 1097 |     if (!sessionEffects.isEmpty()) { | 
 | 1098 |         mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
 | 1099 |     } | 
 | 1100 | } | 
 | 1101 |  | 
 | 1102 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
 | 1103 |                                                             bool enabled, | 
 | 1104 |                                                             int sessionId) | 
 | 1105 | { | 
 | 1106 |     Mutex::Autolock _l(mLock); | 
 | 1107 |     checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
 | 1108 | } | 
 | 1109 |  | 
 | 1110 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
 | 1111 |                                                             bool enabled, | 
 | 1112 |                                                             int sessionId) | 
 | 1113 | { | 
 | 1114 |     if (mType != RECORD) { | 
 | 1115 |         // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
 | 1116 |         // another session. This gives the priority to well behaved effect control panels | 
 | 1117 |         // and applications not using global effects. | 
 | 1118 |         // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect | 
 | 1119 |         // global effects | 
 | 1120 |         if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { | 
 | 1121 |             setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
 | 1122 |         } | 
 | 1123 |     } | 
 | 1124 |  | 
 | 1125 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1126 |     if (chain != 0) { | 
 | 1127 |         chain->checkSuspendOnEffectEnabled(effect, enabled); | 
 | 1128 |     } | 
 | 1129 | } | 
 | 1130 |  | 
 | 1131 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held | 
 | 1132 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
 | 1133 |         const sp<AudioFlinger::Client>& client, | 
 | 1134 |         const sp<IEffectClient>& effectClient, | 
 | 1135 |         int32_t priority, | 
 | 1136 |         int sessionId, | 
 | 1137 |         effect_descriptor_t *desc, | 
 | 1138 |         int *enabled, | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1139 |         status_t *status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1140 | { | 
 | 1141 |     sp<EffectModule> effect; | 
 | 1142 |     sp<EffectHandle> handle; | 
 | 1143 |     status_t lStatus; | 
 | 1144 |     sp<EffectChain> chain; | 
 | 1145 |     bool chainCreated = false; | 
 | 1146 |     bool effectCreated = false; | 
 | 1147 |     bool effectRegistered = false; | 
 | 1148 |  | 
 | 1149 |     lStatus = initCheck(); | 
 | 1150 |     if (lStatus != NO_ERROR) { | 
 | 1151 |         ALOGW("createEffect_l() Audio driver not initialized."); | 
 | 1152 |         goto Exit; | 
 | 1153 |     } | 
 | 1154 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1155 |     // Reject any effect on Direct output threads for now, since the format of | 
 | 1156 |     // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). | 
 | 1157 |     if (mType == DIRECT) { | 
 | 1158 |         ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1159 |                 desc->name, mThreadName); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1160 |         lStatus = BAD_VALUE; | 
 | 1161 |         goto Exit; | 
 | 1162 |     } | 
 | 1163 |  | 
| Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1164 |     // Reject any effect on mixer or duplicating multichannel sinks. | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1165 |     // TODO: fix both format and multichannel issues with effects. | 
| Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1166 |     if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { | 
 | 1167 |         ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", | 
 | 1168 |                 desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1169 |         lStatus = BAD_VALUE; | 
 | 1170 |         goto Exit; | 
 | 1171 |     } | 
 | 1172 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1173 |     // Allow global effects only on offloaded and mixer threads | 
 | 1174 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1175 |         switch (mType) { | 
 | 1176 |         case MIXER: | 
 | 1177 |         case OFFLOAD: | 
 | 1178 |             break; | 
 | 1179 |         case DIRECT: | 
 | 1180 |         case DUPLICATING: | 
 | 1181 |         case RECORD: | 
 | 1182 |         default: | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1183 |             ALOGW("createEffect_l() Cannot add global effect %s on thread %s", | 
 | 1184 |                     desc->name, mThreadName); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1185 |             lStatus = BAD_VALUE; | 
 | 1186 |             goto Exit; | 
 | 1187 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1188 |     } | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1189 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1190 |     // Only Pre processor effects are allowed on input threads and only on input threads | 
 | 1191 |     if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
 | 1192 |         ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", | 
 | 1193 |                 desc->name, desc->flags, mType); | 
 | 1194 |         lStatus = BAD_VALUE; | 
 | 1195 |         goto Exit; | 
 | 1196 |     } | 
 | 1197 |  | 
 | 1198 |     ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
 | 1199 |  | 
 | 1200 |     { // scope for mLock | 
 | 1201 |         Mutex::Autolock _l(mLock); | 
 | 1202 |  | 
 | 1203 |         // check for existing effect chain with the requested audio session | 
 | 1204 |         chain = getEffectChain_l(sessionId); | 
 | 1205 |         if (chain == 0) { | 
 | 1206 |             // create a new chain for this session | 
 | 1207 |             ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
 | 1208 |             chain = new EffectChain(this, sessionId); | 
 | 1209 |             addEffectChain_l(chain); | 
 | 1210 |             chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 1211 |             chainCreated = true; | 
 | 1212 |         } else { | 
 | 1213 |             effect = chain->getEffectFromDesc_l(desc); | 
 | 1214 |         } | 
 | 1215 |  | 
 | 1216 |         ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
 | 1217 |  | 
 | 1218 |         if (effect == 0) { | 
 | 1219 |             int id = mAudioFlinger->nextUniqueId(); | 
 | 1220 |             // Check CPU and memory usage | 
 | 1221 |             lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); | 
 | 1222 |             if (lStatus != NO_ERROR) { | 
 | 1223 |                 goto Exit; | 
 | 1224 |             } | 
 | 1225 |             effectRegistered = true; | 
 | 1226 |             // create a new effect module if none present in the chain | 
 | 1227 |             effect = new EffectModule(this, chain, desc, id, sessionId); | 
 | 1228 |             lStatus = effect->status(); | 
 | 1229 |             if (lStatus != NO_ERROR) { | 
 | 1230 |                 goto Exit; | 
 | 1231 |             } | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1232 |             effect->setOffloaded(mType == OFFLOAD, mId); | 
 | 1233 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1234 |             lStatus = chain->addEffect_l(effect); | 
 | 1235 |             if (lStatus != NO_ERROR) { | 
 | 1236 |                 goto Exit; | 
 | 1237 |             } | 
 | 1238 |             effectCreated = true; | 
 | 1239 |  | 
 | 1240 |             effect->setDevice(mOutDevice); | 
 | 1241 |             effect->setDevice(mInDevice); | 
 | 1242 |             effect->setMode(mAudioFlinger->getMode()); | 
 | 1243 |             effect->setAudioSource(mAudioSource); | 
 | 1244 |         } | 
 | 1245 |         // create effect handle and connect it to effect module | 
 | 1246 |         handle = new EffectHandle(effect, client, effectClient, priority); | 
| Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1247 |         lStatus = handle->initCheck(); | 
 | 1248 |         if (lStatus == OK) { | 
 | 1249 |             lStatus = effect->addHandle(handle.get()); | 
 | 1250 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1251 |         if (enabled != NULL) { | 
 | 1252 |             *enabled = (int)effect->isEnabled(); | 
 | 1253 |         } | 
 | 1254 |     } | 
 | 1255 |  | 
 | 1256 | Exit: | 
 | 1257 |     if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
 | 1258 |         Mutex::Autolock _l(mLock); | 
 | 1259 |         if (effectCreated) { | 
 | 1260 |             chain->removeEffect_l(effect); | 
 | 1261 |         } | 
 | 1262 |         if (effectRegistered) { | 
 | 1263 |             AudioSystem::unregisterEffect(effect->id()); | 
 | 1264 |         } | 
 | 1265 |         if (chainCreated) { | 
 | 1266 |             removeEffectChain_l(chain); | 
 | 1267 |         } | 
 | 1268 |         handle.clear(); | 
 | 1269 |     } | 
 | 1270 |  | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1271 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1272 |     return handle; | 
 | 1273 | } | 
 | 1274 |  | 
 | 1275 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) | 
 | 1276 | { | 
 | 1277 |     Mutex::Autolock _l(mLock); | 
 | 1278 |     return getEffect_l(sessionId, effectId); | 
 | 1279 | } | 
 | 1280 |  | 
 | 1281 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) | 
 | 1282 | { | 
 | 1283 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1284 |     return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; | 
 | 1285 | } | 
 | 1286 |  | 
 | 1287 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
 | 1288 | // PlaybackThread::mLock held | 
 | 1289 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
 | 1290 | { | 
 | 1291 |     // check for existing effect chain with the requested audio session | 
 | 1292 |     int sessionId = effect->sessionId(); | 
 | 1293 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1294 |     bool chainCreated = false; | 
 | 1295 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1296 |     ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), | 
 | 1297 |              "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", | 
 | 1298 |                     this, effect->desc().name, effect->desc().flags); | 
 | 1299 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1300 |     if (chain == 0) { | 
 | 1301 |         // create a new chain for this session | 
 | 1302 |         ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
 | 1303 |         chain = new EffectChain(this, sessionId); | 
 | 1304 |         addEffectChain_l(chain); | 
 | 1305 |         chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 1306 |         chainCreated = true; | 
 | 1307 |     } | 
 | 1308 |     ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); | 
 | 1309 |  | 
 | 1310 |     if (chain->getEffectFromId_l(effect->id()) != 0) { | 
 | 1311 |         ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
 | 1312 |                 this, effect->desc().name, chain.get()); | 
 | 1313 |         return BAD_VALUE; | 
 | 1314 |     } | 
 | 1315 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1316 |     effect->setOffloaded(mType == OFFLOAD, mId); | 
 | 1317 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1318 |     status_t status = chain->addEffect_l(effect); | 
 | 1319 |     if (status != NO_ERROR) { | 
 | 1320 |         if (chainCreated) { | 
 | 1321 |             removeEffectChain_l(chain); | 
 | 1322 |         } | 
 | 1323 |         return status; | 
 | 1324 |     } | 
 | 1325 |  | 
 | 1326 |     effect->setDevice(mOutDevice); | 
 | 1327 |     effect->setDevice(mInDevice); | 
 | 1328 |     effect->setMode(mAudioFlinger->getMode()); | 
 | 1329 |     effect->setAudioSource(mAudioSource); | 
 | 1330 |     return NO_ERROR; | 
 | 1331 | } | 
 | 1332 |  | 
 | 1333 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { | 
 | 1334 |  | 
 | 1335 |     ALOGV("removeEffect_l() %p effect %p", this, effect.get()); | 
 | 1336 |     effect_descriptor_t desc = effect->desc(); | 
 | 1337 |     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1338 |         detachAuxEffect_l(effect->id()); | 
 | 1339 |     } | 
 | 1340 |  | 
 | 1341 |     sp<EffectChain> chain = effect->chain().promote(); | 
 | 1342 |     if (chain != 0) { | 
 | 1343 |         // remove effect chain if removing last effect | 
 | 1344 |         if (chain->removeEffect_l(effect) == 0) { | 
 | 1345 |             removeEffectChain_l(chain); | 
 | 1346 |         } | 
 | 1347 |     } else { | 
 | 1348 |         ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
 | 1349 |     } | 
 | 1350 | } | 
 | 1351 |  | 
 | 1352 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
 | 1353 |         Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 1354 | { | 
 | 1355 |     effectChains = mEffectChains; | 
 | 1356 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 1357 |         mEffectChains[i]->lock(); | 
 | 1358 |     } | 
 | 1359 | } | 
 | 1360 |  | 
 | 1361 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
 | 1362 |         const Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 1363 | { | 
 | 1364 |     for (size_t i = 0; i < effectChains.size(); i++) { | 
 | 1365 |         effectChains[i]->unlock(); | 
 | 1366 |     } | 
 | 1367 | } | 
 | 1368 |  | 
 | 1369 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) | 
 | 1370 | { | 
 | 1371 |     Mutex::Autolock _l(mLock); | 
 | 1372 |     return getEffectChain_l(sessionId); | 
 | 1373 | } | 
 | 1374 |  | 
 | 1375 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const | 
 | 1376 | { | 
 | 1377 |     size_t size = mEffectChains.size(); | 
 | 1378 |     for (size_t i = 0; i < size; i++) { | 
 | 1379 |         if (mEffectChains[i]->sessionId() == sessionId) { | 
 | 1380 |             return mEffectChains[i]; | 
 | 1381 |         } | 
 | 1382 |     } | 
 | 1383 |     return 0; | 
 | 1384 | } | 
 | 1385 |  | 
 | 1386 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
 | 1387 | { | 
 | 1388 |     Mutex::Autolock _l(mLock); | 
 | 1389 |     size_t size = mEffectChains.size(); | 
 | 1390 |     for (size_t i = 0; i < size; i++) { | 
 | 1391 |         mEffectChains[i]->setMode_l(mode); | 
 | 1392 |     } | 
 | 1393 | } | 
 | 1394 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1395 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) | 
 | 1396 | { | 
 | 1397 |     config->type = AUDIO_PORT_TYPE_MIX; | 
 | 1398 |     config->ext.mix.handle = mId; | 
 | 1399 |     config->sample_rate = mSampleRate; | 
 | 1400 |     config->format = mFormat; | 
 | 1401 |     config->channel_mask = mChannelMask; | 
 | 1402 |     config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
 | 1403 |                             AUDIO_PORT_CONFIG_FORMAT; | 
 | 1404 | } | 
 | 1405 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1406 | void AudioFlinger::ThreadBase::systemReady() | 
 | 1407 | { | 
 | 1408 |     Mutex::Autolock _l(mLock); | 
 | 1409 |     if (mSystemReady) { | 
 | 1410 |         return; | 
 | 1411 |     } | 
 | 1412 |     mSystemReady = true; | 
 | 1413 |  | 
 | 1414 |     for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { | 
 | 1415 |         sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); | 
 | 1416 |     } | 
 | 1417 |     mPendingConfigEvents.clear(); | 
 | 1418 | } | 
 | 1419 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1420 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1421 | // ---------------------------------------------------------------------------- | 
 | 1422 | //      Playback | 
 | 1423 | // ---------------------------------------------------------------------------- | 
 | 1424 |  | 
 | 1425 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
 | 1426 |                                              AudioStreamOut* output, | 
 | 1427 |                                              audio_io_handle_t id, | 
 | 1428 |                                              audio_devices_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1429 |                                              type_t type, | 
 | 1430 |                                              bool systemReady) | 
 | 1431 |     :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1432 |         mNormalFrameCount(0), mSinkBuffer(NULL), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1433 |         mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1434 |         mMixerBuffer(NULL), | 
 | 1435 |         mMixerBufferSize(0), | 
 | 1436 |         mMixerBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 1437 |         mMixerBufferValid(false), | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1438 |         mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1439 |         mEffectBuffer(NULL), | 
 | 1440 |         mEffectBufferSize(0), | 
 | 1441 |         mEffectBufferFormat(AUDIO_FORMAT_INVALID), | 
 | 1442 |         mEffectBufferValid(false), | 
| Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1443 |         mSuspended(0), mBytesWritten(0), | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1444 |         mActiveTracksGeneration(0), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1445 |         // mStreamTypes[] initialized in constructor body | 
 | 1446 |         mOutput(output), | 
 | 1447 |         mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
 | 1448 |         mMixerStatus(MIXER_IDLE), | 
 | 1449 |         mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1450 |         mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1451 |         mBytesRemaining(0), | 
 | 1452 |         mCurrentWriteLength(0), | 
 | 1453 |         mUseAsyncWrite(false), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1454 |         mWriteAckSequence(0), | 
 | 1455 |         mDrainSequence(0), | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1456 |         mSignalPending(false), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1457 |         mScreenState(AudioFlinger::mScreenState), | 
 | 1458 |         // index 0 is reserved for normal mixer's submix | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1459 |         mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1460 |         mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1461 |         // mLatchD, mLatchQ, | 
 | 1462 |         mLatchDValid(false), mLatchQValid(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1463 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1464 |     snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); | 
 | 1465 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1466 |  | 
 | 1467 |     // Assumes constructor is called by AudioFlinger with it's mLock held, but | 
 | 1468 |     // it would be safer to explicitly pass initial masterVolume/masterMute as | 
 | 1469 |     // parameter. | 
 | 1470 |     // | 
 | 1471 |     // If the HAL we are using has support for master volume or master mute, | 
 | 1472 |     // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
 | 1473 |     // and the mute set to false). | 
 | 1474 |     mMasterVolume = audioFlinger->masterVolume_l(); | 
 | 1475 |     mMasterMute = audioFlinger->masterMute_l(); | 
 | 1476 |     if (mOutput && mOutput->audioHwDev) { | 
 | 1477 |         if (mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 1478 |             mMasterVolume = 1.0; | 
 | 1479 |         } | 
 | 1480 |  | 
 | 1481 |         if (mOutput->audioHwDev->canSetMasterMute()) { | 
 | 1482 |             mMasterMute = false; | 
 | 1483 |         } | 
 | 1484 |     } | 
 | 1485 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1486 |     readOutputParameters_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1487 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1488 |     // ++ operator does not compile | 
| Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1489 |     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] | 1490 |             stream = (audio_stream_type_t) (stream + 1)) { | 
 | 1491 |         mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); | 
 | 1492 |         mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); | 
 | 1493 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1494 | } | 
 | 1495 |  | 
 | 1496 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
 | 1497 | { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1498 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1499 |     free(mSinkBuffer); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1500 |     free(mMixerBuffer); | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1501 |     free(mEffectBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1502 | } | 
 | 1503 |  | 
 | 1504 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
 | 1505 | { | 
 | 1506 |     dumpInternals(fd, args); | 
 | 1507 |     dumpTracks(fd, args); | 
 | 1508 |     dumpEffectChains(fd, args); | 
 | 1509 | } | 
 | 1510 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1511 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1512 | { | 
 | 1513 |     const size_t SIZE = 256; | 
 | 1514 |     char buffer[SIZE]; | 
 | 1515 |     String8 result; | 
 | 1516 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1517 |     result.appendFormat("  Stream volumes in dB: "); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1518 |     for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
 | 1519 |         const stream_type_t *st = &mStreamTypes[i]; | 
 | 1520 |         if (i > 0) { | 
 | 1521 |             result.appendFormat(", "); | 
 | 1522 |         } | 
 | 1523 |         result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
 | 1524 |         if (st->mute) { | 
 | 1525 |             result.append("M"); | 
 | 1526 |         } | 
 | 1527 |     } | 
 | 1528 |     result.append("\n"); | 
 | 1529 |     write(fd, result.string(), result.length()); | 
 | 1530 |     result.clear(); | 
 | 1531 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1532 |     // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
 | 1533 |     FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1534 |     dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1535 |             underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1536 |  | 
 | 1537 |     size_t numtracks = mTracks.size(); | 
 | 1538 |     size_t numactive = mActiveTracks.size(); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1539 |     dprintf(fd, "  %d Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1540 |     size_t numactiveseen = 0; | 
 | 1541 |     if (numtracks) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1542 |         dprintf(fd, " of which %d are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1543 |         Track::appendDumpHeader(result); | 
 | 1544 |         for (size_t i = 0; i < numtracks; ++i) { | 
 | 1545 |             sp<Track> track = mTracks[i]; | 
 | 1546 |             if (track != 0) { | 
 | 1547 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 1548 |                 if (active) { | 
 | 1549 |                     numactiveseen++; | 
 | 1550 |                 } | 
 | 1551 |                 track->dump(buffer, SIZE, active); | 
 | 1552 |                 result.append(buffer); | 
 | 1553 |             } | 
 | 1554 |         } | 
 | 1555 |     } else { | 
 | 1556 |         result.append("\n"); | 
 | 1557 |     } | 
 | 1558 |     if (numactiveseen != numactive) { | 
 | 1559 |         // some tracks in the active list were not in the tracks list | 
 | 1560 |         snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
 | 1561 |                 " not in the track list\n"); | 
 | 1562 |         result.append(buffer); | 
 | 1563 |         Track::appendDumpHeader(result); | 
 | 1564 |         for (size_t i = 0; i < numactive; ++i) { | 
 | 1565 |             sp<Track> track = mActiveTracks[i].promote(); | 
 | 1566 |             if (track != 0 && mTracks.indexOf(track) < 0) { | 
 | 1567 |                 track->dump(buffer, SIZE, true); | 
 | 1568 |                 result.append(buffer); | 
 | 1569 |             } | 
 | 1570 |         } | 
 | 1571 |     } | 
 | 1572 |  | 
 | 1573 |     write(fd, result.string(), result.size()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1574 | } | 
 | 1575 |  | 
 | 1576 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 1577 | { | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1578 |     dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1579 |  | 
 | 1580 |     dumpBase(fd, args); | 
 | 1581 |  | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1582 |     dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount); | 
 | 1583 |     dprintf(fd, "  Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); | 
 | 1584 |     dprintf(fd, "  Total writes: %d\n", mNumWrites); | 
 | 1585 |     dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites); | 
 | 1586 |     dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no"); | 
 | 1587 |     dprintf(fd, "  Suspend count: %d\n", mSuspended); | 
 | 1588 |     dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer); | 
 | 1589 |     dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer); | 
 | 1590 |     dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer); | 
 | 1591 |     dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask); | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1592 |     dprintf(fd, "  Standby delay ns=%lld\n", (long long)mStandbyDelayNs); | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1593 |     AudioStreamOut *output = mOutput; | 
 | 1594 |     audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; | 
 | 1595 |     String8 flagsAsString = outputFlagsToString(flags); | 
 | 1596 |     dprintf(fd, "  AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1597 | } | 
 | 1598 |  | 
 | 1599 | // Thread virtuals | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1600 |  | 
 | 1601 | void AudioFlinger::PlaybackThread::onFirstRef() | 
 | 1602 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1603 |     run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1604 | } | 
 | 1605 |  | 
 | 1606 | // ThreadBase virtuals | 
 | 1607 | void AudioFlinger::PlaybackThread::preExit() | 
 | 1608 | { | 
 | 1609 |     ALOGV("  preExit()"); | 
 | 1610 |     // FIXME this is using hard-coded strings but in the future, this functionality will be | 
 | 1611 |     //       converted to use audio HAL extensions required to support tunneling | 
 | 1612 |     mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); | 
 | 1613 | } | 
 | 1614 |  | 
 | 1615 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
 | 1616 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( | 
 | 1617 |         const sp<AudioFlinger::Client>& client, | 
 | 1618 |         audio_stream_type_t streamType, | 
 | 1619 |         uint32_t sampleRate, | 
 | 1620 |         audio_format_t format, | 
 | 1621 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1622 |         size_t *pFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1623 |         const sp<IMemory>& sharedBuffer, | 
 | 1624 |         int sessionId, | 
 | 1625 |         IAudioFlinger::track_flags_t *flags, | 
 | 1626 |         pid_t tid, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1627 |         int uid, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1628 |         status_t *status) | 
 | 1629 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1630 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1631 |     sp<Track> track; | 
 | 1632 |     status_t lStatus; | 
 | 1633 |  | 
 | 1634 |     bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; | 
 | 1635 |  | 
 | 1636 |     // client expresses a preference for FAST, but we get the final say | 
 | 1637 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 1638 |       if ( | 
 | 1639 |             // not timed | 
 | 1640 |             (!isTimed) && | 
 | 1641 |             // either of these use cases: | 
 | 1642 |             ( | 
 | 1643 |               // use case 1: shared buffer with any frame count | 
 | 1644 |               ( | 
 | 1645 |                 (sharedBuffer != 0) | 
 | 1646 |               ) || | 
| Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1647 |               // use case 2: frame count is default or at least as large as HAL | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1648 |               ( | 
| Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1649 |                 // we formerly checked for a callback handler (non-0 tid), | 
 | 1650 |                 // but that is no longer required for TRANSFER_OBTAIN mode | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1651 |                 ((frameCount == 0) || | 
| Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1652 |                 (frameCount >= mFrameCount)) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1653 |               ) | 
 | 1654 |             ) && | 
 | 1655 |             // PCM data | 
 | 1656 |             audio_is_linear_pcm(format) && | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1657 |             // TODO: extract as a data library function that checks that a computationally | 
 | 1658 |             // expensive downmixer is not required: isFastOutputChannelConversion() | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1659 |             (channelMask == mChannelMask || | 
| Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1660 |                     mChannelMask != AUDIO_CHANNEL_OUT_STEREO || | 
 | 1661 |                     (channelMask == AUDIO_CHANNEL_OUT_MONO | 
 | 1662 |                             /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1663 |             // hardware sample rate | 
 | 1664 |             (sampleRate == mSampleRate) && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1665 |             // normal mixer has an associated fast mixer | 
 | 1666 |             hasFastMixer() && | 
 | 1667 |             // there are sufficient fast track slots available | 
 | 1668 |             (mFastTrackAvailMask != 0) | 
 | 1669 |             // FIXME test that MixerThread for this fast track has a capable output HAL | 
 | 1670 |             // FIXME add a permission test also? | 
 | 1671 |         ) { | 
 | 1672 |         // if frameCount not specified, then it defaults to fast mixer (HAL) frame count | 
 | 1673 |         if (frameCount == 0) { | 
| Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1674 |             // read the fast track multiplier property the first time it is needed | 
 | 1675 |             int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); | 
 | 1676 |             if (ok != 0) { | 
 | 1677 |                 ALOGE("%s pthread_once failed: %d", __func__, ok); | 
 | 1678 |             } | 
 | 1679 |             frameCount = mFrameCount * sFastTrackMultiplier; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1680 |         } | 
 | 1681 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", | 
 | 1682 |                 frameCount, mFrameCount); | 
 | 1683 |       } else { | 
 | 1684 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1685 |                 "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " | 
 | 1686 |                 "sampleRate=%u mSampleRate=%u " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1687 |                 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1688 |                 isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1689 |                 audio_is_linear_pcm(format), | 
 | 1690 |                 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
 | 1691 |         *flags &= ~IAudioFlinger::TRACK_FAST; | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1692 |       } | 
 | 1693 |     } | 
 | 1694 |     // For normal PCM streaming tracks, update minimum frame count. | 
 | 1695 |     // For compatibility with AudioTrack calculation, buffer depth is forced | 
 | 1696 |     // to be at least 2 x the normal mixer frame count and cover audio hardware latency. | 
 | 1697 |     // This is probably too conservative, but legacy application code may depend on it. | 
 | 1698 |     // If you change this calculation, also review the start threshold which is related. | 
 | 1699 |     if (!(*flags & IAudioFlinger::TRACK_FAST) | 
 | 1700 |             && audio_is_linear_pcm(format) && sharedBuffer == 0) { | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1701 |         // this must match AudioTrack.cpp calculateMinFrameCount(). | 
 | 1702 |         // TODO: Move to a common library | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1703 |         uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); | 
 | 1704 |         uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); | 
 | 1705 |         if (minBufCount < 2) { | 
 | 1706 |             minBufCount = 2; | 
 | 1707 |         } | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1708 |         // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack | 
 | 1709 |         // or the client should compute and pass in a larger buffer request. | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1710 |         size_t minFrameCount = | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1711 |                 minBufCount * sourceFramesNeededWithTimestretch( | 
 | 1712 |                         sampleRate, mNormalFrameCount, | 
 | 1713 |                         mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); | 
| Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1714 |         if (frameCount < minFrameCount) { // including frameCount == 0 | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1715 |             frameCount = minFrameCount; | 
 | 1716 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1717 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1718 |     *pFrameCount = frameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1719 |  | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1720 |     switch (mType) { | 
 | 1721 |  | 
 | 1722 |     case DIRECT: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1723 |         if (audio_is_linear_pcm(format)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1724 |             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1725 |                 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " | 
 | 1726 |                         "for output %p with format %#x", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1727 |                         sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1728 |                 lStatus = BAD_VALUE; | 
 | 1729 |                 goto Exit; | 
 | 1730 |             } | 
 | 1731 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1732 |         break; | 
 | 1733 |  | 
 | 1734 |     case OFFLOAD: | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1735 |         if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1736 |             ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" | 
 | 1737 |                     "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1738 |                     sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1739 |             lStatus = BAD_VALUE; | 
 | 1740 |             goto Exit; | 
 | 1741 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1742 |         break; | 
 | 1743 |  | 
 | 1744 |     default: | 
| Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1745 |         if (!audio_is_linear_pcm(format)) { | 
| Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1746 |                 ALOGE("createTrack_l() Bad parameter: format %#x \"" | 
 | 1747 |                         "for output %p with format %#x", | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1748 |                         format, mOutput, mFormat); | 
 | 1749 |                 lStatus = BAD_VALUE; | 
 | 1750 |                 goto Exit; | 
 | 1751 |         } | 
| Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1752 |         if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1753 |             ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
 | 1754 |             lStatus = BAD_VALUE; | 
 | 1755 |             goto Exit; | 
 | 1756 |         } | 
| Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1757 |         break; | 
 | 1758 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1759 |     } | 
 | 1760 |  | 
 | 1761 |     lStatus = initCheck(); | 
 | 1762 |     if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1763 |         ALOGE("createTrack_l() audio driver not initialized"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1764 |         goto Exit; | 
 | 1765 |     } | 
 | 1766 |  | 
 | 1767 |     { // scope for mLock | 
 | 1768 |         Mutex::Autolock _l(mLock); | 
 | 1769 |  | 
 | 1770 |         // all tracks in same audio session must share the same routing strategy otherwise | 
 | 1771 |         // conflicts will happen when tracks are moved from one output to another by audio policy | 
 | 1772 |         // manager | 
 | 1773 |         uint32_t strategy = AudioSystem::getStrategyForStream(streamType); | 
 | 1774 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1775 |             sp<Track> t = mTracks[i]; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1776 |             if (t != 0 && t->isExternalTrack()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1777 |                 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); | 
 | 1778 |                 if (sessionId == t->sessionId() && strategy != actual) { | 
 | 1779 |                     ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
 | 1780 |                             strategy, actual); | 
 | 1781 |                     lStatus = BAD_VALUE; | 
 | 1782 |                     goto Exit; | 
 | 1783 |                 } | 
 | 1784 |             } | 
 | 1785 |         } | 
 | 1786 |  | 
 | 1787 |         if (!isTimed) { | 
 | 1788 |             track = new Track(this, client, streamType, sampleRate, format, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1789 |                               channelMask, frameCount, NULL, sharedBuffer, | 
 | 1790 |                               sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1791 |         } else { | 
 | 1792 |             track = TimedTrack::create(this, client, streamType, sampleRate, format, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1793 |                     channelMask, frameCount, sharedBuffer, sessionId, uid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1794 |         } | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1795 |  | 
 | 1796 |         // new Track always returns non-NULL, | 
 | 1797 |         // but TimedTrack::create() is a factory that could fail by returning NULL | 
 | 1798 |         lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; | 
 | 1799 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1800 |             ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1801 |             // 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] | 1802 |             goto Exit; | 
 | 1803 |         } | 
 | 1804 |         mTracks.add(track); | 
 | 1805 |  | 
 | 1806 |         sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1807 |         if (chain != 0) { | 
 | 1808 |             ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
 | 1809 |             track->setMainBuffer(chain->inBuffer()); | 
 | 1810 |             chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); | 
 | 1811 |             chain->incTrackCnt(); | 
 | 1812 |         } | 
 | 1813 |  | 
 | 1814 |         if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
 | 1815 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 1816 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 1817 |             // so ask activity manager to do this on our behalf | 
 | 1818 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
 | 1819 |         } | 
 | 1820 |     } | 
 | 1821 |  | 
 | 1822 |     lStatus = NO_ERROR; | 
 | 1823 |  | 
 | 1824 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1825 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1826 |     return track; | 
 | 1827 | } | 
 | 1828 |  | 
 | 1829 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const | 
 | 1830 | { | 
 | 1831 |     return latency; | 
 | 1832 | } | 
 | 1833 |  | 
 | 1834 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
 | 1835 | { | 
 | 1836 |     Mutex::Autolock _l(mLock); | 
 | 1837 |     return latency_l(); | 
 | 1838 | } | 
 | 1839 | uint32_t AudioFlinger::PlaybackThread::latency_l() const | 
 | 1840 | { | 
 | 1841 |     if (initCheck() == NO_ERROR) { | 
 | 1842 |         return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); | 
 | 1843 |     } else { | 
 | 1844 |         return 0; | 
 | 1845 |     } | 
 | 1846 | } | 
 | 1847 |  | 
 | 1848 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
 | 1849 | { | 
 | 1850 |     Mutex::Autolock _l(mLock); | 
 | 1851 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 1852 |     if (mOutput && mOutput->audioHwDev && | 
 | 1853 |         mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 1854 |         mMasterVolume = 1.0; | 
 | 1855 |     } else { | 
 | 1856 |         mMasterVolume = value; | 
 | 1857 |     } | 
 | 1858 | } | 
 | 1859 |  | 
 | 1860 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
 | 1861 | { | 
 | 1862 |     Mutex::Autolock _l(mLock); | 
 | 1863 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 1864 |     if (mOutput && mOutput->audioHwDev && | 
 | 1865 |         mOutput->audioHwDev->canSetMasterMute()) { | 
 | 1866 |         mMasterMute = false; | 
 | 1867 |     } else { | 
 | 1868 |         mMasterMute = muted; | 
 | 1869 |     } | 
 | 1870 | } | 
 | 1871 |  | 
 | 1872 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
 | 1873 | { | 
 | 1874 |     Mutex::Autolock _l(mLock); | 
 | 1875 |     mStreamTypes[stream].volume = value; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1876 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1877 | } | 
 | 1878 |  | 
 | 1879 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
 | 1880 | { | 
 | 1881 |     Mutex::Autolock _l(mLock); | 
 | 1882 |     mStreamTypes[stream].mute = muted; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1883 |     broadcast_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1884 | } | 
 | 1885 |  | 
 | 1886 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
 | 1887 | { | 
 | 1888 |     Mutex::Autolock _l(mLock); | 
 | 1889 |     return mStreamTypes[stream].volume; | 
 | 1890 | } | 
 | 1891 |  | 
 | 1892 | // addTrack_l() must be called with ThreadBase::mLock held | 
 | 1893 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
 | 1894 | { | 
 | 1895 |     status_t status = ALREADY_EXISTS; | 
 | 1896 |  | 
 | 1897 |     // set retry count for buffer fill | 
 | 1898 |     track->mRetryCount = kMaxTrackStartupRetries; | 
 | 1899 |     if (mActiveTracks.indexOf(track) < 0) { | 
 | 1900 |         // the track is newly added, make sure it fills up all its | 
 | 1901 |         // buffers before playing. This is to ensure the client will | 
 | 1902 |         // effectively get the latency it requested. | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1903 |         if (track->isExternalTrack()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1904 |             TrackBase::track_state state = track->mState; | 
 | 1905 |             mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1906 |             status = AudioSystem::startOutput(mId, track->streamType(), | 
 | 1907 |                                               (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1908 |             mLock.lock(); | 
 | 1909 |             // abort track was stopped/paused while we released the lock | 
 | 1910 |             if (state != track->mState) { | 
 | 1911 |                 if (status == NO_ERROR) { | 
 | 1912 |                     mLock.unlock(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1913 |                     AudioSystem::stopOutput(mId, track->streamType(), | 
 | 1914 |                                             (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1915 |                     mLock.lock(); | 
 | 1916 |                 } | 
 | 1917 |                 return INVALID_OPERATION; | 
 | 1918 |             } | 
 | 1919 |             // abort if start is rejected by audio policy manager | 
 | 1920 |             if (status != NO_ERROR) { | 
 | 1921 |                 return PERMISSION_DENIED; | 
 | 1922 |             } | 
 | 1923 | #ifdef ADD_BATTERY_DATA | 
 | 1924 |             // to track the speaker usage | 
 | 1925 |             addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
 | 1926 | #endif | 
 | 1927 |         } | 
 | 1928 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1929 |         track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1930 |         track->mResetDone = false; | 
 | 1931 |         track->mPresentationCompleteFrames = 0; | 
 | 1932 |         mActiveTracks.add(track); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1933 |         mWakeLockUids.add(track->uid()); | 
 | 1934 |         mActiveTracksGeneration++; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 1935 |         mLatestActiveTrack = track; | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1936 |         sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 1937 |         if (chain != 0) { | 
 | 1938 |             ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
 | 1939 |                     track->sessionId()); | 
 | 1940 |             chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1941 |         } | 
 | 1942 |  | 
 | 1943 |         status = NO_ERROR; | 
 | 1944 |     } | 
 | 1945 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 1946 |     onAddNewTrack_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1947 |     return status; | 
 | 1948 | } | 
 | 1949 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1950 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1951 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1952 |     track->terminate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1953 |     // active tracks are removed by threadLoop() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1954 |     bool trackActive = (mActiveTracks.indexOf(track) >= 0); | 
 | 1955 |     track->mState = TrackBase::STOPPED; | 
 | 1956 |     if (!trackActive) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1957 |         removeTrack_l(track); | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1958 |     } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1959 |         track->mState = TrackBase::STOPPING_1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1960 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1961 |  | 
 | 1962 |     return trackActive; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1963 | } | 
 | 1964 |  | 
 | 1965 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
 | 1966 | { | 
 | 1967 |     track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
 | 1968 |     mTracks.remove(track); | 
 | 1969 |     deleteTrackName_l(track->name()); | 
 | 1970 |     // redundant as track is about to be destroyed, for dumpsys only | 
 | 1971 |     track->mName = -1; | 
 | 1972 |     if (track->isFastTrack()) { | 
 | 1973 |         int index = track->mFastIndex; | 
 | 1974 |         ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); | 
 | 1975 |         ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
 | 1976 |         mFastTrackAvailMask |= 1 << index; | 
 | 1977 |         // redundant as track is about to be destroyed, for dumpsys only | 
 | 1978 |         track->mFastIndex = -1; | 
 | 1979 |     } | 
 | 1980 |     sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 1981 |     if (chain != 0) { | 
 | 1982 |         chain->decTrackCnt(); | 
 | 1983 |     } | 
 | 1984 | } | 
 | 1985 |  | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1986 | void AudioFlinger::PlaybackThread::broadcast_l() | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1987 | { | 
 | 1988 |     // Thread could be blocked waiting for async | 
 | 1989 |     // so signal it to handle state changes immediately | 
 | 1990 |     // If threadLoop is currently unlocked a signal of mWaitWorkCV will | 
 | 1991 |     // be lost so we also flag to prevent it blocking on mWaitWorkCV | 
 | 1992 |     mSignalPending = true; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1993 |     mWaitWorkCV.broadcast(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1994 | } | 
 | 1995 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1996 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
 | 1997 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1998 |     Mutex::Autolock _l(mLock); | 
 | 1999 |     if (initCheck() != NO_ERROR) { | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2000 |         return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2001 |     } | 
 | 2002 |  | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2003 |     char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); | 
 | 2004 |     const String8 out_s8(s); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2005 |     free(s); | 
 | 2006 |     return out_s8; | 
 | 2007 | } | 
 | 2008 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2009 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2010 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 2011 |     ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2012 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2013 |     desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2014 |  | 
 | 2015 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2016 |     case AUDIO_OUTPUT_OPENED: | 
 | 2017 |     case AUDIO_OUTPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2018 |         desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2019 |         desc->mChannelMask = mChannelMask; | 
 | 2020 |         desc->mSamplingRate = mSampleRate; | 
 | 2021 |         desc->mFormat = mFormat; | 
 | 2022 |         desc->mFrameCount = mNormalFrameCount; // FIXME see | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2023 |                                              // AudioFlinger::frameCount(audio_io_handle_t) | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2024 |         desc->mLatency = latency_l(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2025 |         break; | 
 | 2026 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2027 |     case AUDIO_OUTPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2028 |     default: | 
 | 2029 |         break; | 
 | 2030 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2031 |     mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2032 | } | 
 | 2033 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2034 | void AudioFlinger::PlaybackThread::writeCallback() | 
 | 2035 | { | 
 | 2036 |     ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2037 |     mCallbackThread->resetWriteBlocked(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2038 | } | 
 | 2039 |  | 
 | 2040 | void AudioFlinger::PlaybackThread::drainCallback() | 
 | 2041 | { | 
 | 2042 |     ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2043 |     mCallbackThread->resetDraining(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2044 | } | 
 | 2045 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2046 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2047 | { | 
 | 2048 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2049 |     // reject out of sequence requests | 
 | 2050 |     if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { | 
 | 2051 |         mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2052 |         mWaitWorkCV.signal(); | 
 | 2053 |     } | 
 | 2054 | } | 
 | 2055 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2056 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2057 | { | 
 | 2058 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2059 |     // reject out of sequence requests | 
 | 2060 |     if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { | 
 | 2061 |         mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2062 |         mWaitWorkCV.signal(); | 
 | 2063 |     } | 
 | 2064 | } | 
 | 2065 |  | 
 | 2066 | // static | 
 | 2067 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2068 |                                                 void *param __unused, | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2069 |                                                 void *cookie) | 
 | 2070 | { | 
 | 2071 |     AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; | 
 | 2072 |     ALOGV("asyncCallback() event %d", event); | 
 | 2073 |     switch (event) { | 
 | 2074 |     case STREAM_CBK_EVENT_WRITE_READY: | 
 | 2075 |         me->writeCallback(); | 
 | 2076 |         break; | 
 | 2077 |     case STREAM_CBK_EVENT_DRAIN_READY: | 
 | 2078 |         me->drainCallback(); | 
 | 2079 |         break; | 
 | 2080 |     default: | 
 | 2081 |         ALOGW("asyncCallback() unknown event %d", event); | 
 | 2082 |         break; | 
 | 2083 |     } | 
 | 2084 |     return 0; | 
 | 2085 | } | 
 | 2086 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2087 | void AudioFlinger::PlaybackThread::readOutputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2088 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2089 |     // 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] | 2090 |     mSampleRate = mOutput->getSampleRate(); | 
 | 2091 |     mChannelMask = mOutput->getChannelMask(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2092 |     if (!audio_is_output_channel(mChannelMask)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2093 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2094 |     } | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2095 |     if ((mType == MIXER || mType == DUPLICATING) | 
 | 2096 |             && !isValidPcmSinkChannelMask(mChannelMask)) { | 
 | 2097 |         LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", | 
 | 2098 |                 mChannelMask); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2099 |     } | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2100 |     mChannelCount = audio_channel_count_from_out_mask(mChannelMask); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2101 |  | 
 | 2102 |     // Get actual HAL format. | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2103 |     mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2104 |     // Get format from the shim, which will be different than the HAL format | 
 | 2105 |     // if playing compressed audio over HDMI passthrough. | 
 | 2106 |     mFormat = mOutput->getFormat(); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2107 |     if (!audio_is_valid_format(mFormat)) { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2108 |         LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2109 |     } | 
| Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2110 |     if ((mType == MIXER || mType == DUPLICATING) | 
 | 2111 |             && !isValidPcmSinkFormat(mFormat)) { | 
 | 2112 |         LOG_FATAL("HAL format %#x not supported for mixed output", | 
 | 2113 |                 mFormat); | 
| Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2114 |     } | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2115 |     mFrameSize = mOutput->getFrameSize(); | 
| Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2116 |     mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); | 
 | 2117 |     mFrameCount = mBufferSize / mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2118 |     if (mFrameCount & 15) { | 
 | 2119 |         ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", | 
 | 2120 |                 mFrameCount); | 
 | 2121 |     } | 
 | 2122 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2123 |     if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && | 
 | 2124 |             (mOutput->stream->set_callback != NULL)) { | 
 | 2125 |         if (mOutput->stream->set_callback(mOutput->stream, | 
 | 2126 |                                       AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { | 
 | 2127 |             mUseAsyncWrite = true; | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2128 |             mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2129 |         } | 
 | 2130 |     } | 
 | 2131 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2132 |     mHwSupportsPause = false; | 
 | 2133 |     if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
 | 2134 |         if (mOutput->stream->pause != NULL) { | 
 | 2135 |             if (mOutput->stream->resume != NULL) { | 
 | 2136 |                 mHwSupportsPause = true; | 
 | 2137 |             } else { | 
 | 2138 |                 ALOGW("direct output implements pause but not resume"); | 
 | 2139 |             } | 
 | 2140 |         } else if (mOutput->stream->resume != NULL) { | 
 | 2141 |             ALOGW("direct output implements resume but not pause"); | 
 | 2142 |         } | 
 | 2143 |     } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2144 |     if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { | 
 | 2145 |         LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); | 
 | 2146 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2147 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2148 |     if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 2149 |         // For best precision, we use float instead of the associated output | 
 | 2150 |         // device format (typically PCM 16 bit). | 
 | 2151 |  | 
 | 2152 |         mFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 2153 |         mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 2154 |         mBufferSize = mFrameSize * mFrameCount; | 
 | 2155 |  | 
 | 2156 |         // TODO: We currently use the associated output device channel mask and sample rate. | 
 | 2157 |         // (1) Perhaps use the ORed channel mask of all downstream MixerThreads | 
 | 2158 |         // (if a valid mask) to avoid premature downmix. | 
 | 2159 |         // (2) Perhaps use the maximum sample rate of all downstream MixerThreads | 
 | 2160 |         // instead of the output device sample rate to avoid loss of high frequency information. | 
 | 2161 |         // This may need to be updated as MixerThread/OutputTracks are added and not here. | 
 | 2162 |     } | 
 | 2163 |  | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2164 |     // 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] | 2165 |     double multiplier = 1.0; | 
 | 2166 |     if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
 | 2167 |             kUseFastMixer == FastMixer_Dynamic)) { | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2168 |         size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
 | 2169 |         size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2170 |         // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
 | 2171 |         minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
 | 2172 |         maxNormalFrameCount = maxNormalFrameCount & ~15; | 
 | 2173 |         if (maxNormalFrameCount < minNormalFrameCount) { | 
 | 2174 |             maxNormalFrameCount = minNormalFrameCount; | 
 | 2175 |         } | 
 | 2176 |         multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
 | 2177 |         if (multiplier <= 1.0) { | 
 | 2178 |             multiplier = 1.0; | 
 | 2179 |         } else if (multiplier <= 2.0) { | 
 | 2180 |             if (2 * mFrameCount <= maxNormalFrameCount) { | 
 | 2181 |                 multiplier = 2.0; | 
 | 2182 |             } else { | 
 | 2183 |                 multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
 | 2184 |             } | 
 | 2185 |         } else { | 
 | 2186 |             // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2187 |             // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2188 |             // track, but we sometimes have to do this to satisfy the maximum frame count | 
 | 2189 |             // constraint) | 
 | 2190 |             // FIXME this rounding up should not be done if no HAL SRC | 
 | 2191 |             uint32_t truncMult = (uint32_t) multiplier; | 
 | 2192 |             if ((truncMult & 1)) { | 
 | 2193 |                 if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { | 
 | 2194 |                     ++truncMult; | 
 | 2195 |                 } | 
 | 2196 |             } | 
 | 2197 |             multiplier = (double) truncMult; | 
 | 2198 |         } | 
 | 2199 |     } | 
 | 2200 |     mNormalFrameCount = multiplier * mFrameCount; | 
 | 2201 |     // round up to nearest 16 frames to satisfy AudioMixer | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2202 |     if (mType == MIXER || mType == DUPLICATING) { | 
 | 2203 |         mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
 | 2204 |     } | 
| Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2205 |     ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2206 |             mNormalFrameCount); | 
 | 2207 |  | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2208 |     // Check if we want to throttle the processing to no more than 2x normal rate | 
 | 2209 |     mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2210 |     mThreadThrottleTimeMs = 0; | 
 | 2211 |     mThreadThrottleEndMs = 0; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2212 |     mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); | 
 | 2213 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2214 |     // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames. | 
 | 2215 |     // Originally this was int16_t[] array, need to remove legacy implications. | 
 | 2216 |     free(mSinkBuffer); | 
 | 2217 |     mSinkBuffer = NULL; | 
| Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2218 |     // For sink buffer size, we use the frame size from the downstream sink to avoid problems | 
 | 2219 |     // with non PCM formats for compressed music, e.g. AAC, and Offload threads. | 
 | 2220 |     const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2221 |     (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2222 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2223 |     // We resize the mMixerBuffer according to the requirements of the sink buffer which | 
 | 2224 |     // drives the output. | 
 | 2225 |     free(mMixerBuffer); | 
 | 2226 |     mMixerBuffer = NULL; | 
 | 2227 |     if (mMixerBufferEnabled) { | 
 | 2228 |         mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. | 
 | 2229 |         mMixerBufferSize = mNormalFrameCount * mChannelCount | 
 | 2230 |                 * audio_bytes_per_sample(mMixerBufferFormat); | 
 | 2231 |         (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); | 
 | 2232 |     } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2233 |     free(mEffectBuffer); | 
 | 2234 |     mEffectBuffer = NULL; | 
 | 2235 |     if (mEffectBufferEnabled) { | 
 | 2236 |         mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only | 
 | 2237 |         mEffectBufferSize = mNormalFrameCount * mChannelCount | 
 | 2238 |                 * audio_bytes_per_sample(mEffectBufferFormat); | 
 | 2239 |         (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); | 
 | 2240 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2241 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2242 |     // force reconfiguration of effect chains and engines to take new buffer size and audio | 
 | 2243 |     // parameters into account | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2244 |     // 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] | 2245 |     // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
 | 2246 |     // matter. | 
 | 2247 |     // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
 | 2248 |     Vector< sp<EffectChain> > effectChains = mEffectChains; | 
 | 2249 |     for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2250 |         mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
 | 2251 |     } | 
 | 2252 | } | 
 | 2253 |  | 
 | 2254 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2255 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2256 | { | 
 | 2257 |     if (halFrames == NULL || dspFrames == NULL) { | 
 | 2258 |         return BAD_VALUE; | 
 | 2259 |     } | 
 | 2260 |     Mutex::Autolock _l(mLock); | 
 | 2261 |     if (initCheck() != NO_ERROR) { | 
 | 2262 |         return INVALID_OPERATION; | 
 | 2263 |     } | 
 | 2264 |     size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 2265 |     *halFrames = framesWritten; | 
 | 2266 |  | 
 | 2267 |     if (isSuspended()) { | 
 | 2268 |         // return an estimation of rendered frames when the output is suspended | 
 | 2269 |         size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
 | 2270 |         *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; | 
 | 2271 |         return NO_ERROR; | 
 | 2272 |     } else { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2273 |         status_t status; | 
 | 2274 |         uint32_t frames; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2275 |         status = mOutput->getRenderPosition(&frames); | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2276 |         *dspFrames = (size_t)frames; | 
 | 2277 |         return status; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2278 |     } | 
 | 2279 | } | 
 | 2280 |  | 
 | 2281 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const | 
 | 2282 | { | 
 | 2283 |     Mutex::Autolock _l(mLock); | 
 | 2284 |     uint32_t result = 0; | 
 | 2285 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 2286 |         result = EFFECT_SESSION; | 
 | 2287 |     } | 
 | 2288 |  | 
 | 2289 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2290 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2291 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2292 |             result |= TRACK_SESSION; | 
 | 2293 |             break; | 
 | 2294 |         } | 
 | 2295 |     } | 
 | 2296 |  | 
 | 2297 |     return result; | 
 | 2298 | } | 
 | 2299 |  | 
 | 2300 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) | 
 | 2301 | { | 
 | 2302 |     // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
 | 2303 |     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
 | 2304 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 2305 |         return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 2306 |     } | 
 | 2307 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 2308 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2309 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2310 |             return AudioSystem::getStrategyForStream(track->streamType()); | 
 | 2311 |         } | 
 | 2312 |     } | 
 | 2313 |     return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 2314 | } | 
 | 2315 |  | 
 | 2316 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2317 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2318 | { | 
 | 2319 |     Mutex::Autolock _l(mLock); | 
 | 2320 |     return mOutput; | 
 | 2321 | } | 
 | 2322 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2323 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2324 | { | 
 | 2325 |     Mutex::Autolock _l(mLock); | 
 | 2326 |     AudioStreamOut *output = mOutput; | 
 | 2327 |     mOutput = NULL; | 
 | 2328 |     // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
 | 2329 |     //       must push a NULL and wait for ack | 
 | 2330 |     mOutputSink.clear(); | 
 | 2331 |     mPipeSink.clear(); | 
 | 2332 |     mNormalSink.clear(); | 
 | 2333 |     return output; | 
 | 2334 | } | 
 | 2335 |  | 
 | 2336 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
 | 2337 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const | 
 | 2338 | { | 
 | 2339 |     if (mOutput == NULL) { | 
 | 2340 |         return NULL; | 
 | 2341 |     } | 
 | 2342 |     return &mOutput->stream->common; | 
 | 2343 | } | 
 | 2344 |  | 
 | 2345 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const | 
 | 2346 | { | 
 | 2347 |     return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 2348 | } | 
 | 2349 |  | 
 | 2350 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
 | 2351 | { | 
 | 2352 |     if (!isValidSyncEvent(event)) { | 
 | 2353 |         return BAD_VALUE; | 
 | 2354 |     } | 
 | 2355 |  | 
 | 2356 |     Mutex::Autolock _l(mLock); | 
 | 2357 |  | 
 | 2358 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2359 |         sp<Track> track = mTracks[i]; | 
 | 2360 |         if (event->triggerSession() == track->sessionId()) { | 
 | 2361 |             (void) track->setSyncEvent(event); | 
 | 2362 |             return NO_ERROR; | 
 | 2363 |         } | 
 | 2364 |     } | 
 | 2365 |  | 
 | 2366 |     return NAME_NOT_FOUND; | 
 | 2367 | } | 
 | 2368 |  | 
 | 2369 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
 | 2370 | { | 
 | 2371 |     return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
 | 2372 | } | 
 | 2373 |  | 
 | 2374 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( | 
 | 2375 |         const Vector< sp<Track> >& tracksToRemove) | 
 | 2376 | { | 
 | 2377 |     size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2378 |     if (count > 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2379 |         for (size_t i = 0 ; i < count ; i++) { | 
 | 2380 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2381 |             if (track->isExternalTrack()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2382 |                 AudioSystem::stopOutput(mId, track->streamType(), | 
 | 2383 |                                         (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2384 | #ifdef ADD_BATTERY_DATA | 
 | 2385 |                 // to track the speaker usage | 
 | 2386 |                 addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
 | 2387 | #endif | 
 | 2388 |                 if (track->isTerminated()) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2389 |                     AudioSystem::releaseOutput(mId, track->streamType(), | 
 | 2390 |                                                (audio_session_t)track->sessionId()); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2391 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2392 |             } | 
 | 2393 |         } | 
 | 2394 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2395 | } | 
 | 2396 |  | 
 | 2397 | void AudioFlinger::PlaybackThread::checkSilentMode_l() | 
 | 2398 | { | 
 | 2399 |     if (!mMasterMute) { | 
 | 2400 |         char value[PROPERTY_VALUE_MAX]; | 
 | 2401 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 2402 |             char *endptr; | 
 | 2403 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 2404 |             if (*endptr == '\0' && ul != 0) { | 
 | 2405 |                 ALOGD("Silence is golden"); | 
 | 2406 |                 // The setprop command will not allow a property to be changed after | 
 | 2407 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 2408 |                 setMasterMute_l(true); | 
 | 2409 |             } | 
 | 2410 |         } | 
 | 2411 |     } | 
 | 2412 | } | 
 | 2413 |  | 
 | 2414 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2415 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2416 | { | 
 | 2417 |     // FIXME rewrite to reduce number of system calls | 
 | 2418 |     mLastWriteTime = systemTime(); | 
 | 2419 |     mInWrite = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2420 |     ssize_t bytesWritten; | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2421 |     const size_t offset = mCurrentWriteLength - mBytesRemaining; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2422 |  | 
 | 2423 |     // If an NBAIO sink is present, use it to write the normal mixer's submix | 
 | 2424 |     if (mNormalSink != 0) { | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2425 |  | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2426 |         const size_t count = mBytesRemaining / mFrameSize; | 
 | 2427 |  | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2428 |         ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2429 |         // update the setpoint when AudioFlinger::mScreenState changes | 
 | 2430 |         uint32_t screenState = AudioFlinger::mScreenState; | 
 | 2431 |         if (screenState != mScreenState) { | 
 | 2432 |             mScreenState = screenState; | 
 | 2433 |             MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 2434 |             if (pipe != NULL) { | 
 | 2435 |                 pipe->setAvgFrames((mScreenState & 1) ? | 
 | 2436 |                         (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 2437 |             } | 
 | 2438 |         } | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2439 |         ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2440 |         ATRACE_END(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2441 |         if (framesWritten > 0) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2442 |             bytesWritten = framesWritten * mFrameSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2443 |         } else { | 
 | 2444 |             bytesWritten = framesWritten; | 
 | 2445 |         } | 
| Glenn Kasten | efaa7ab | 2014-08-20 08:48:54 -0700 | [diff] [blame] | 2446 |         mLatchDValid = false; | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2447 |         status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2448 |         if (status == NO_ERROR) { | 
 | 2449 |             size_t totalFramesWritten = mNormalSink->framesWritten(); | 
 | 2450 |             if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { | 
 | 2451 |                 mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2452 |                 // mLatchD.mFramesReleased is set immediately before D is clocked into Q | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2453 |                 mLatchDValid = true; | 
 | 2454 |             } | 
 | 2455 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2456 |     // otherwise use the HAL / AudioStreamOut directly | 
 | 2457 |     } else { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2458 |         // Direct output and offload threads | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2459 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2460 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2461 |             ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); | 
 | 2462 |             mWriteAckSequence += 2; | 
 | 2463 |             mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2464 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2465 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2466 |         } | 
| Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2467 |         // FIXME We should have an implementation of timestamps for direct output threads. | 
 | 2468 |         // They are used e.g for multichannel PCM playback over HDMI. | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2469 |         bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2470 |         if (mUseAsyncWrite && | 
 | 2471 |                 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { | 
 | 2472 |             // 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] | 2473 |             mWriteAckSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2474 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2475 |             mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2476 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2477 |     } | 
 | 2478 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2479 |     mNumWrites++; | 
 | 2480 |     mInWrite = false; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2481 |     mStandby = false; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2482 |     return bytesWritten; | 
 | 2483 | } | 
 | 2484 |  | 
 | 2485 | void AudioFlinger::PlaybackThread::threadLoop_drain() | 
 | 2486 | { | 
 | 2487 |     if (mOutput->stream->drain) { | 
 | 2488 |         ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); | 
 | 2489 |         if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2490 |             ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); | 
 | 2491 |             mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2492 |             ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2493 |             mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2494 |         } | 
 | 2495 |         mOutput->stream->drain(mOutput->stream, | 
 | 2496 |             (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY | 
 | 2497 |                                                 : AUDIO_DRAIN_ALL); | 
 | 2498 |     } | 
 | 2499 | } | 
 | 2500 |  | 
 | 2501 | void AudioFlinger::PlaybackThread::threadLoop_exit() | 
 | 2502 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2503 |     { | 
 | 2504 |         Mutex::Autolock _l(mLock); | 
 | 2505 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 2506 |             sp<Track> track = mTracks[i]; | 
 | 2507 |             track->invalidate(); | 
 | 2508 |         } | 
 | 2509 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2510 | } | 
 | 2511 |  | 
 | 2512 | /* | 
 | 2513 | The derived values that are cached: | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2514 |  - mSinkBufferSize from frame count * frame size | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2515 |  - mActiveSleepTimeUs from activeSleepTimeUs() | 
 | 2516 |  - mIdleSleepTimeUs from idleSleepTimeUs() | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2517 |  - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least | 
 | 2518 |    kDefaultStandbyTimeInNsecs when connected to an A2DP device. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2519 |  - maxPeriod from frame count and sample rate (MIXER only) | 
 | 2520 |  | 
 | 2521 | The parameters that affect these derived values are: | 
 | 2522 |  - frame count | 
 | 2523 |  - frame size | 
 | 2524 |  - sample rate | 
 | 2525 |  - device type: A2DP or not | 
 | 2526 |  - device latency | 
 | 2527 |  - format: PCM or not | 
 | 2528 |  - active sleep time | 
 | 2529 |  - idle sleep time | 
 | 2530 | */ | 
 | 2531 |  | 
 | 2532 | void AudioFlinger::PlaybackThread::cacheParameters_l() | 
 | 2533 | { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2534 |     mSinkBufferSize = mNormalFrameCount * mFrameSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2535 |     mActiveSleepTimeUs = activeSleepTimeUs(); | 
 | 2536 |     mIdleSleepTimeUs = idleSleepTimeUs(); | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2537 |  | 
 | 2538 |     // make sure standby delay is not too short when connected to an A2DP sink to avoid | 
 | 2539 |     // truncating audio when going to standby. | 
 | 2540 |     mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; | 
 | 2541 |     if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { | 
 | 2542 |         if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { | 
 | 2543 |             mStandbyDelayNs = kDefaultStandbyTimeInNsecs; | 
 | 2544 |         } | 
 | 2545 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2546 | } | 
 | 2547 |  | 
 | 2548 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
 | 2549 | { | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2550 |     ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2551 |             this,  streamType, mTracks.size()); | 
 | 2552 |     Mutex::Autolock _l(mLock); | 
 | 2553 |  | 
 | 2554 |     size_t size = mTracks.size(); | 
 | 2555 |     for (size_t i = 0; i < size; i++) { | 
 | 2556 |         sp<Track> t = mTracks[i]; | 
 | 2557 |         if (t->streamType() == streamType) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2558 |             t->invalidate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2559 |         } | 
 | 2560 |     } | 
 | 2561 | } | 
 | 2562 |  | 
 | 2563 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 2564 | { | 
 | 2565 |     int session = chain->sessionId(); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2566 |     int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled | 
 | 2567 |             ? mEffectBuffer : mSinkBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2568 |     bool ownsBuffer = false; | 
 | 2569 |  | 
 | 2570 |     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
 | 2571 |     if (session > 0) { | 
 | 2572 |         // 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] | 2573 |         // the sink buffer as input | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2574 |         if (mType != DIRECT) { | 
 | 2575 |             size_t numSamples = mNormalFrameCount * mChannelCount; | 
 | 2576 |             buffer = new int16_t[numSamples]; | 
 | 2577 |             memset(buffer, 0, numSamples * sizeof(int16_t)); | 
 | 2578 |             ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); | 
 | 2579 |             ownsBuffer = true; | 
 | 2580 |         } | 
 | 2581 |  | 
 | 2582 |         // Attach all tracks with same session ID to this chain. | 
 | 2583 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2584 |             sp<Track> track = mTracks[i]; | 
 | 2585 |             if (session == track->sessionId()) { | 
 | 2586 |                 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), | 
 | 2587 |                         buffer); | 
 | 2588 |                 track->setMainBuffer(buffer); | 
 | 2589 |                 chain->incTrackCnt(); | 
 | 2590 |             } | 
 | 2591 |         } | 
 | 2592 |  | 
 | 2593 |         // indicate all active tracks in the chain | 
 | 2594 |         for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
 | 2595 |             sp<Track> track = mActiveTracks[i].promote(); | 
 | 2596 |             if (track == 0) { | 
 | 2597 |                 continue; | 
 | 2598 |             } | 
 | 2599 |             if (session == track->sessionId()) { | 
 | 2600 |                 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
 | 2601 |                 chain->incActiveTrackCnt(); | 
 | 2602 |             } | 
 | 2603 |         } | 
 | 2604 |     } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2605 |     chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2606 |     chain->setInBuffer(buffer, ownsBuffer); | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2607 |     chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled | 
 | 2608 |             ? mEffectBuffer : mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2609 |     // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect | 
 | 2610 |     // chains list in order to be processed last as it contains output stage effects | 
 | 2611 |     // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
 | 2612 |     // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
 | 2613 |     // after track specific effects and before output stage | 
 | 2614 |     // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
 | 2615 |     // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX | 
 | 2616 |     // Effect chain for other sessions are inserted at beginning of effect | 
 | 2617 |     // chains list to be processed before output mix effects. Relative order between other | 
 | 2618 |     // sessions is not important | 
 | 2619 |     size_t size = mEffectChains.size(); | 
 | 2620 |     size_t i = 0; | 
 | 2621 |     for (i = 0; i < size; i++) { | 
 | 2622 |         if (mEffectChains[i]->sessionId() < session) { | 
 | 2623 |             break; | 
 | 2624 |         } | 
 | 2625 |     } | 
 | 2626 |     mEffectChains.insertAt(chain, i); | 
 | 2627 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 2628 |  | 
 | 2629 |     return NO_ERROR; | 
 | 2630 | } | 
 | 2631 |  | 
 | 2632 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 2633 | { | 
 | 2634 |     int session = chain->sessionId(); | 
 | 2635 |  | 
 | 2636 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 2637 |  | 
 | 2638 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 2639 |         if (chain == mEffectChains[i]) { | 
 | 2640 |             mEffectChains.removeAt(i); | 
 | 2641 |             // detach all active tracks from the chain | 
 | 2642 |             for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
 | 2643 |                 sp<Track> track = mActiveTracks[i].promote(); | 
 | 2644 |                 if (track == 0) { | 
 | 2645 |                     continue; | 
 | 2646 |                 } | 
 | 2647 |                 if (session == track->sessionId()) { | 
 | 2648 |                     ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
 | 2649 |                             chain.get(), session); | 
 | 2650 |                     chain->decActiveTrackCnt(); | 
 | 2651 |                 } | 
 | 2652 |             } | 
 | 2653 |  | 
 | 2654 |             // detach all tracks with same session ID from this chain | 
 | 2655 |             for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2656 |                 sp<Track> track = mTracks[i]; | 
 | 2657 |                 if (session == track->sessionId()) { | 
| Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2658 |                     track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2659 |                     chain->decTrackCnt(); | 
 | 2660 |                 } | 
 | 2661 |             } | 
 | 2662 |             break; | 
 | 2663 |         } | 
 | 2664 |     } | 
 | 2665 |     return mEffectChains.size(); | 
 | 2666 | } | 
 | 2667 |  | 
 | 2668 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
 | 2669 |         const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
 | 2670 | { | 
 | 2671 |     Mutex::Autolock _l(mLock); | 
 | 2672 |     return attachAuxEffect_l(track, EffectId); | 
 | 2673 | } | 
 | 2674 |  | 
 | 2675 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
 | 2676 |         const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
 | 2677 | { | 
 | 2678 |     status_t status = NO_ERROR; | 
 | 2679 |  | 
 | 2680 |     if (EffectId == 0) { | 
 | 2681 |         track->setAuxBuffer(0, NULL); | 
 | 2682 |     } else { | 
 | 2683 |         // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
 | 2684 |         sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
 | 2685 |         if (effect != 0) { | 
 | 2686 |             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 2687 |                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
 | 2688 |             } else { | 
 | 2689 |                 status = INVALID_OPERATION; | 
 | 2690 |             } | 
 | 2691 |         } else { | 
 | 2692 |             status = BAD_VALUE; | 
 | 2693 |         } | 
 | 2694 |     } | 
 | 2695 |     return status; | 
 | 2696 | } | 
 | 2697 |  | 
 | 2698 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
 | 2699 | { | 
 | 2700 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 2701 |         sp<Track> track = mTracks[i]; | 
 | 2702 |         if (track->auxEffectId() == effectId) { | 
 | 2703 |             attachAuxEffect_l(track, 0); | 
 | 2704 |         } | 
 | 2705 |     } | 
 | 2706 | } | 
 | 2707 |  | 
 | 2708 | bool AudioFlinger::PlaybackThread::threadLoop() | 
 | 2709 | { | 
 | 2710 |     Vector< sp<Track> > tracksToRemove; | 
 | 2711 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2712 |     mStandbyTimeNs = systemTime(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2713 |  | 
 | 2714 |     // MIXER | 
 | 2715 |     nsecs_t lastWarning = 0; | 
 | 2716 |  | 
 | 2717 |     // DUPLICATING | 
 | 2718 |     // FIXME could this be made local to while loop? | 
 | 2719 |     writeFrames = 0; | 
 | 2720 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2721 |     int lastGeneration = 0; | 
 | 2722 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2723 |     cacheParameters_l(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2724 |     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2725 |  | 
 | 2726 |     if (mType == MIXER) { | 
 | 2727 |         sleepTimeShift = 0; | 
 | 2728 |     } | 
 | 2729 |  | 
 | 2730 |     CpuStats cpuStats; | 
 | 2731 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 2732 |  | 
 | 2733 |     acquireWakeLock(); | 
 | 2734 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2735 |     // mNBLogWriter->log can only be called while thread mutex mLock is held. | 
 | 2736 |     // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
 | 2737 |     // and then that string will be logged at the next convenient opportunity. | 
 | 2738 |     const char *logString = NULL; | 
 | 2739 |  | 
| Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2740 |     checkSilentMode_l(); | 
 | 2741 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2742 |     while (!exitPending()) | 
 | 2743 |     { | 
 | 2744 |         cpuStats.sample(myName); | 
 | 2745 |  | 
 | 2746 |         Vector< sp<EffectChain> > effectChains; | 
 | 2747 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2748 |         { // scope for mLock | 
 | 2749 |  | 
 | 2750 |             Mutex::Autolock _l(mLock); | 
 | 2751 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2752 |             processConfigEvents_l(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2753 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2754 |             if (logString != NULL) { | 
 | 2755 |                 mNBLogWriter->logTimestamp(); | 
 | 2756 |                 mNBLogWriter->log(logString); | 
 | 2757 |                 logString = NULL; | 
 | 2758 |             } | 
 | 2759 |  | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2760 |             // Gather the framesReleased counters for all active tracks, | 
 | 2761 |             // and latch them atomically with the timestamp. | 
 | 2762 |             // FIXME We're using raw pointers as indices. A unique track ID would be a better index. | 
 | 2763 |             mLatchD.mFramesReleased.clear(); | 
 | 2764 |             size_t size = mActiveTracks.size(); | 
 | 2765 |             for (size_t i = 0; i < size; i++) { | 
 | 2766 |                 sp<Track> t = mActiveTracks[i].promote(); | 
 | 2767 |                 if (t != 0) { | 
 | 2768 |                     mLatchD.mFramesReleased.add(t.get(), | 
 | 2769 |                             t->mAudioTrackServerProxy->framesReleased()); | 
 | 2770 |                 } | 
 | 2771 |             } | 
| Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2772 |             if (mLatchDValid) { | 
 | 2773 |                 mLatchQ = mLatchD; | 
 | 2774 |                 mLatchDValid = false; | 
 | 2775 |                 mLatchQValid = true; | 
 | 2776 |             } | 
 | 2777 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2778 |             saveOutputTracks(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2779 |             if (mSignalPending) { | 
 | 2780 |                 // A signal was raised while we were unlocked | 
 | 2781 |                 mSignalPending = false; | 
 | 2782 |             } else if (waitingAsyncCallback_l()) { | 
 | 2783 |                 if (exitPending()) { | 
 | 2784 |                     break; | 
 | 2785 |                 } | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2786 |                 bool released = false; | 
 | 2787 |                 // The following works around a bug in the offload driver. Ideally we would release | 
 | 2788 |                 // the wake lock every time, but that causes the last offload buffer(s) to be | 
 | 2789 |                 // dropped while the device is on battery, so we need to hold a wake lock during | 
 | 2790 |                 // the drain phase. | 
 | 2791 |                 if (mBytesRemaining && !(mDrainSequence & 1)) { | 
 | 2792 |                     releaseWakeLock_l(); | 
 | 2793 |                     released = true; | 
 | 2794 |                 } | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2795 |                 mWakeLockUids.clear(); | 
 | 2796 |                 mActiveTracksGeneration++; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2797 |                 ALOGV("wait async completion"); | 
 | 2798 |                 mWaitWorkCV.wait(mLock); | 
 | 2799 |                 ALOGV("async completion/wake"); | 
| Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2800 |                 if (released) { | 
 | 2801 |                     acquireWakeLock_l(); | 
 | 2802 |                 } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2803 |                 mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 2804 |                 mSleepTimeUs = 0; | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2805 |  | 
 | 2806 |                 continue; | 
 | 2807 |             } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2808 |             if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2809 |                                    isSuspended()) { | 
 | 2810 |                 // put audio hardware into standby after short delay | 
 | 2811 |                 if (shouldStandby_l()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2812 |  | 
 | 2813 |                     threadLoop_standby(); | 
 | 2814 |  | 
 | 2815 |                     mStandby = true; | 
 | 2816 |                 } | 
 | 2817 |  | 
 | 2818 |                 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
 | 2819 |                     // we're about to wait, flush the binder command buffer | 
 | 2820 |                     IPCThreadState::self()->flushCommands(); | 
 | 2821 |  | 
 | 2822 |                     clearOutputTracks(); | 
 | 2823 |  | 
 | 2824 |                     if (exitPending()) { | 
 | 2825 |                         break; | 
 | 2826 |                     } | 
 | 2827 |  | 
 | 2828 |                     releaseWakeLock_l(); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2829 |                     mWakeLockUids.clear(); | 
 | 2830 |                     mActiveTracksGeneration++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2831 |                     // wait until we have something to do... | 
 | 2832 |                     ALOGV("%s going to sleep", myName.string()); | 
 | 2833 |                     mWaitWorkCV.wait(mLock); | 
 | 2834 |                     ALOGV("%s waking up", myName.string()); | 
 | 2835 |                     acquireWakeLock_l(); | 
 | 2836 |  | 
 | 2837 |                     mMixerStatus = MIXER_IDLE; | 
 | 2838 |                     mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
 | 2839 |                     mBytesWritten = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2840 |                     mBytesRemaining = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2841 |                     checkSilentMode_l(); | 
 | 2842 |  | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2843 |                     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
 | 2844 |                     mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2845 |                     if (mType == MIXER) { | 
 | 2846 |                         sleepTimeShift = 0; | 
 | 2847 |                     } | 
 | 2848 |  | 
 | 2849 |                     continue; | 
 | 2850 |                 } | 
 | 2851 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2852 |             // mMixerStatusIgnoringFastTracks is also updated internally | 
 | 2853 |             mMixerStatus = prepareTracks_l(&tracksToRemove); | 
 | 2854 |  | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2855 |             // compare with previously applied list | 
 | 2856 |             if (lastGeneration != mActiveTracksGeneration) { | 
 | 2857 |                 // update wakelock | 
 | 2858 |                 updateWakeLockUids_l(mWakeLockUids); | 
 | 2859 |                 lastGeneration = mActiveTracksGeneration; | 
 | 2860 |             } | 
 | 2861 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2862 |             // prevent any changes in effect chain list and in each effect chain | 
 | 2863 |             // during mixing and effect process as the audio buffers could be deleted | 
 | 2864 |             // or modified if an effect is created or deleted | 
 | 2865 |             lockEffectChains_l(effectChains); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2866 |         } // mLock scope ends | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2867 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2868 |         if (mBytesRemaining == 0) { | 
 | 2869 |             mCurrentWriteLength = 0; | 
 | 2870 |             if (mMixerStatus == MIXER_TRACKS_READY) { | 
 | 2871 |                 // threadLoop_mix() sets mCurrentWriteLength | 
 | 2872 |                 threadLoop_mix(); | 
 | 2873 |             } else if ((mMixerStatus != MIXER_DRAIN_TRACK) | 
 | 2874 |                         && (mMixerStatus != MIXER_DRAIN_ALL)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2875 |                 // threadLoop_sleepTime sets mSleepTimeUs to 0 if data | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2876 |                 // must be written to HAL | 
 | 2877 |                 threadLoop_sleepTime(); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2878 |                 if (mSleepTimeUs == 0) { | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2879 |                     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2880 |                 } | 
 | 2881 |             } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2882 |             // Either threadLoop_mix() or threadLoop_sleepTime() should have set | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2883 |             // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2884 |             // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) | 
 | 2885 |             // or mSinkBuffer (if there are no effects). | 
 | 2886 |             // | 
 | 2887 |             // This is done pre-effects computation; if effects change to | 
 | 2888 |             // support higher precision, this needs to move. | 
 | 2889 |             // | 
 | 2890 |             // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2891 |             // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2892 |             if (mMixerBufferValid) { | 
 | 2893 |                 void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; | 
 | 2894 |                 audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; | 
 | 2895 |  | 
 | 2896 |                 memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, | 
 | 2897 |                         mNormalFrameCount * mChannelCount); | 
 | 2898 |             } | 
 | 2899 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2900 |             mBytesRemaining = mCurrentWriteLength; | 
 | 2901 |             if (isSuspended()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2902 |                 mSleepTimeUs = suspendSleepTimeUs(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2903 |                 // simulate write to HAL when suspended | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2904 |                 mBytesWritten += mSinkBufferSize; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2905 |                 mBytesRemaining = 0; | 
 | 2906 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2907 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2908 |             // only process effects if we're going to write | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2909 |             if (mSleepTimeUs == 0 && mType != OFFLOAD) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2910 |                 for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2911 |                     effectChains[i]->process_l(); | 
 | 2912 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2913 |             } | 
 | 2914 |         } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2915 |         // Process effect chains for offloaded thread even if no audio | 
 | 2916 |         // was read from audio track: process only updates effect state | 
 | 2917 |         // and thus does have to be synchronized with audio writes but may have | 
 | 2918 |         // to be called while waiting for async write callback | 
 | 2919 |         if (mType == OFFLOAD) { | 
 | 2920 |             for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 2921 |                 effectChains[i]->process_l(); | 
 | 2922 |             } | 
 | 2923 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2924 |  | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2925 |         // Only if the Effects buffer is enabled and there is data in the | 
 | 2926 |         // Effects buffer (buffer valid), we need to | 
 | 2927 |         // copy into the sink buffer. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2928 |         // TODO use mSleepTimeUs == 0 as an additional condition. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2929 |         if (mEffectBufferValid) { | 
 | 2930 |             //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); | 
 | 2931 |             memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, | 
 | 2932 |                     mNormalFrameCount * mChannelCount); | 
 | 2933 |         } | 
 | 2934 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2935 |         // enable changes in effect chain | 
 | 2936 |         unlockEffectChains(effectChains); | 
 | 2937 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2938 |         if (!waitingAsyncCallback()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2939 |             // mSleepTimeUs == 0 means we must write to audio hardware | 
 | 2940 |             if (mSleepTimeUs == 0) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2941 |                 ssize_t ret = 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2942 |                 if (mBytesRemaining) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2943 |                     ret = threadLoop_write(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2944 |                     if (ret < 0) { | 
 | 2945 |                         mBytesRemaining = 0; | 
 | 2946 |                     } else { | 
 | 2947 |                         mBytesWritten += ret; | 
 | 2948 |                         mBytesRemaining -= ret; | 
 | 2949 |                     } | 
 | 2950 |                 } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || | 
 | 2951 |                         (mMixerStatus == MIXER_DRAIN_ALL)) { | 
 | 2952 |                     threadLoop_drain(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2953 |                 } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2954 |                 if (mType == MIXER && !mStandby) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2955 |                     // write blocked detection | 
 | 2956 |                     nsecs_t now = systemTime(); | 
 | 2957 |                     nsecs_t delta = now - mLastWriteTime; | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2958 |                     if (delta > maxPeriod) { | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2959 |                         mNumDelayedWrites++; | 
 | 2960 |                         if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 2961 |                             ATRACE_NAME("underrun"); | 
 | 2962 |                             ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
 | 2963 |                                     ns2ms(delta), mNumDelayedWrites, this); | 
 | 2964 |                             lastWarning = now; | 
 | 2965 |                         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2966 |                     } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2967 |  | 
 | 2968 |                     if (mThreadThrottle | 
 | 2969 |                             && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) | 
 | 2970 |                             && ret > 0) {                         // we wrote something | 
 | 2971 |                         // Limit MixerThread data processing to no more than twice the | 
 | 2972 |                         // expected processing rate. | 
 | 2973 |                         // | 
 | 2974 |                         // This helps prevent underruns with NuPlayer and other applications | 
 | 2975 |                         // which may set up buffers that are close to the minimum size, or use | 
 | 2976 |                         // deep buffers, and rely on a double-buffering sleep strategy to fill. | 
 | 2977 |                         // | 
 | 2978 |                         // The throttle smooths out sudden large data drains from the device, | 
 | 2979 |                         // e.g. when it comes out of standby, which often causes problems with | 
 | 2980 |                         // (1) mixer threads without a fast mixer (which has its own warm-up) | 
 | 2981 |                         // (2) minimum buffer sized tracks (even if the track is full, | 
 | 2982 |                         //     the app won't fill fast enough to handle the sudden draw). | 
 | 2983 |  | 
 | 2984 |                         const int32_t deltaMs = delta / 1000000; | 
 | 2985 |                         const int32_t throttleMs = mHalfBufferMs - deltaMs; | 
 | 2986 |                         if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { | 
 | 2987 |                             usleep(throttleMs * 1000); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2988 |                             // notify of throttle start on verbose log | 
 | 2989 |                             ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, | 
 | 2990 |                                     "mixer(%p) throttle begin:" | 
 | 2991 |                                     " ret(%zd) deltaMs(%d) requires sleep %d ms", | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2992 |                                     this, ret, deltaMs, throttleMs); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2993 |                             mThreadThrottleTimeMs += throttleMs; | 
 | 2994 |                         } else { | 
 | 2995 |                             uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; | 
 | 2996 |                             if (diff > 0) { | 
 | 2997 |                                 // notify of throttle end on debug log | 
 | 2998 |                                 ALOGD("mixer(%p) throttle end: throttle time(%u)", this, diff); | 
 | 2999 |                                 mThreadThrottleEndMs = mThreadThrottleTimeMs; | 
 | 3000 |                             } | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3001 |                         } | 
 | 3002 |                     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3003 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3004 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3005 |             } else { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3006 |                 ATRACE_BEGIN("sleep"); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3007 |                 usleep(mSleepTimeUs); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3008 |                 ATRACE_END(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3009 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3010 |         } | 
 | 3011 |  | 
 | 3012 |         // Finally let go of removed track(s), without the lock held | 
 | 3013 |         // since we can't guarantee the destructors won't acquire that | 
 | 3014 |         // same lock.  This will also mutate and push a new fast mixer state. | 
 | 3015 |         threadLoop_removeTracks(tracksToRemove); | 
 | 3016 |         tracksToRemove.clear(); | 
 | 3017 |  | 
 | 3018 |         // FIXME I don't understand the need for this here; | 
 | 3019 |         //       it was in the original code but maybe the | 
 | 3020 |         //       assignment in saveOutputTracks() makes this unnecessary? | 
 | 3021 |         clearOutputTracks(); | 
 | 3022 |  | 
 | 3023 |         // Effect chains will be actually deleted here if they were removed from | 
 | 3024 |         // mEffectChains list during mixing or effects processing | 
 | 3025 |         effectChains.clear(); | 
 | 3026 |  | 
 | 3027 |         // FIXME Note that the above .clear() is no longer necessary since effectChains | 
 | 3028 |         // is now local to this block, but will keep it for now (at least until merge done). | 
 | 3029 |     } | 
 | 3030 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3031 |     threadLoop_exit(); | 
 | 3032 |  | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3033 |     if (!mStandby) { | 
 | 3034 |         threadLoop_standby(); | 
 | 3035 |         mStandby = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3036 |     } | 
 | 3037 |  | 
 | 3038 |     releaseWakeLock(); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3039 |     mWakeLockUids.clear(); | 
 | 3040 |     mActiveTracksGeneration++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3041 |  | 
 | 3042 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 3043 |     return false; | 
 | 3044 | } | 
 | 3045 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3046 | // removeTracks_l() must be called with ThreadBase::mLock held | 
 | 3047 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) | 
 | 3048 | { | 
 | 3049 |     size_t count = tracksToRemove.size(); | 
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3050 |     if (count > 0) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3051 |         for (size_t i=0 ; i<count ; i++) { | 
 | 3052 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
 | 3053 |             mActiveTracks.remove(track); | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3054 |             mWakeLockUids.remove(track->uid()); | 
 | 3055 |             mActiveTracksGeneration++; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3056 |             ALOGV("removeTracks_l removing track on session %d", track->sessionId()); | 
 | 3057 |             sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 3058 |             if (chain != 0) { | 
 | 3059 |                 ALOGV("stopping track on chain %p for session Id: %d", chain.get(), | 
 | 3060 |                         track->sessionId()); | 
 | 3061 |                 chain->decActiveTrackCnt(); | 
 | 3062 |             } | 
 | 3063 |             if (track->isTerminated()) { | 
 | 3064 |                 removeTrack_l(track); | 
 | 3065 |             } | 
 | 3066 |         } | 
 | 3067 |     } | 
 | 3068 |  | 
 | 3069 | } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3070 |  | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3071 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) | 
 | 3072 | { | 
 | 3073 |     if (mNormalSink != 0) { | 
 | 3074 |         return mNormalSink->getTimestamp(timestamp); | 
 | 3075 |     } | 
| Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3076 |     if ((mType == OFFLOAD || mType == DIRECT) | 
 | 3077 |             && mOutput != NULL && mOutput->stream->get_presentation_position) { | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3078 |         uint64_t position64; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3079 |         int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); | 
| Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3080 |         if (ret == 0) { | 
 | 3081 |             timestamp.mPosition = (uint32_t)position64; | 
 | 3082 |             return NO_ERROR; | 
 | 3083 |         } | 
 | 3084 |     } | 
 | 3085 |     return INVALID_OPERATION; | 
 | 3086 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3087 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3088 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 3089 |                                                           audio_patch_handle_t *handle) | 
 | 3090 | { | 
 | 3091 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 3092 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
 | 3093 |     if (mFastMixer != 0) { | 
 | 3094 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3095 |         FastMixerState *state = sq->begin(); | 
 | 3096 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 3097 |             previousCommand = state->mCommand; | 
 | 3098 |             state->mCommand = FastMixerState::HOT_IDLE; | 
 | 3099 |             sq->end(); | 
 | 3100 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3101 |         } else { | 
 | 3102 |             sq->end(false /*didModify*/); | 
 | 3103 |         } | 
 | 3104 |     } | 
 | 3105 |     status_t status = PlaybackThread::createAudioPatch_l(patch, handle); | 
 | 3106 |  | 
 | 3107 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
 | 3108 |         ALOG_ASSERT(mFastMixer != 0); | 
 | 3109 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3110 |         FastMixerState *state = sq->begin(); | 
 | 3111 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 3112 |         state->mCommand = previousCommand; | 
 | 3113 |         sq->end(); | 
 | 3114 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3115 |     } | 
 | 3116 |  | 
 | 3117 |     return status; | 
 | 3118 | } | 
 | 3119 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3120 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 3121 |                                                           audio_patch_handle_t *handle) | 
 | 3122 | { | 
 | 3123 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3124 |  | 
 | 3125 |     // store new device and send to effects | 
 | 3126 |     audio_devices_t type = AUDIO_DEVICE_NONE; | 
 | 3127 |     for (unsigned int i = 0; i < patch->num_sinks; i++) { | 
 | 3128 |         type |= patch->sinks[i].ext.device.type; | 
 | 3129 |     } | 
 | 3130 |  | 
 | 3131 | #ifdef ADD_BATTERY_DATA | 
 | 3132 |     // when changing the audio output device, call addBatteryData to notify | 
 | 3133 |     // the change | 
 | 3134 |     if (mOutDevice != type) { | 
 | 3135 |         uint32_t params = 0; | 
 | 3136 |         // check whether speaker is on | 
 | 3137 |         if (type & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 3138 |             params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3139 |         } | 
 | 3140 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3141 |         audio_devices_t deviceWithoutSpeaker | 
 | 3142 |             = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 3143 |         // check if any other device (except speaker) is on | 
 | 3144 |         if (type & deviceWithoutSpeaker) { | 
 | 3145 |             params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 3146 |         } | 
 | 3147 |  | 
 | 3148 |         if (params != 0) { | 
 | 3149 |             addBatteryData(params); | 
 | 3150 |         } | 
 | 3151 |     } | 
 | 3152 | #endif | 
 | 3153 |  | 
 | 3154 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 3155 |         mEffectChains[i]->setDevice_l(type); | 
 | 3156 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3157 |  | 
 | 3158 |     // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when | 
 | 3159 |     // the thread is created so that the first patch creation triggers an ioConfigChanged callback | 
 | 3160 |     bool configChanged = mPrevOutDevice != type; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3161 |     mOutDevice = type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3162 |     mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3163 |  | 
 | 3164 |     if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3165 |         audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 3166 |         status = hwDevice->create_audio_patch(hwDevice, | 
 | 3167 |                                                patch->num_sources, | 
 | 3168 |                                                patch->sources, | 
 | 3169 |                                                patch->num_sinks, | 
 | 3170 |                                                patch->sinks, | 
 | 3171 |                                                handle); | 
 | 3172 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3173 |         char *address; | 
 | 3174 |         if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { | 
 | 3175 |             //FIXME: we only support address on first sink with HAL version < 3.0 | 
 | 3176 |             address = audio_device_address_to_parameter( | 
 | 3177 |                                                         patch->sinks[0].ext.device.type, | 
 | 3178 |                                                         patch->sinks[0].ext.device.address); | 
 | 3179 |         } else { | 
 | 3180 |             address = (char *)calloc(1, 1); | 
 | 3181 |         } | 
 | 3182 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 3183 |         free(address); | 
 | 3184 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); | 
 | 3185 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3186 |                 param.toString().string()); | 
 | 3187 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3188 |     } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3189 |     if (configChanged) { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3190 |         mPrevOutDevice = type; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3191 |         sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
 | 3192 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3193 |     return status; | 
 | 3194 | } | 
 | 3195 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3196 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 3197 | { | 
 | 3198 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 3199 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
 | 3200 |     if (mFastMixer != 0) { | 
 | 3201 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3202 |         FastMixerState *state = sq->begin(); | 
 | 3203 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 3204 |             previousCommand = state->mCommand; | 
 | 3205 |             state->mCommand = FastMixerState::HOT_IDLE; | 
 | 3206 |             sq->end(); | 
 | 3207 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3208 |         } else { | 
 | 3209 |             sq->end(false /*didModify*/); | 
 | 3210 |         } | 
 | 3211 |     } | 
 | 3212 |  | 
 | 3213 |     status_t status = PlaybackThread::releaseAudioPatch_l(handle); | 
 | 3214 |  | 
 | 3215 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
 | 3216 |         ALOG_ASSERT(mFastMixer != 0); | 
 | 3217 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3218 |         FastMixerState *state = sq->begin(); | 
 | 3219 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 3220 |         state->mCommand = previousCommand; | 
 | 3221 |         sq->end(); | 
 | 3222 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3223 |     } | 
 | 3224 |  | 
 | 3225 |     return status; | 
 | 3226 | } | 
 | 3227 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3228 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 3229 | { | 
 | 3230 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3231 |  | 
 | 3232 |     mOutDevice = AUDIO_DEVICE_NONE; | 
 | 3233 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3234 |     if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
 | 3235 |         audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); | 
 | 3236 |         status = hwDevice->release_audio_patch(hwDevice, handle); | 
 | 3237 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3238 |         AudioParameter param; | 
 | 3239 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); | 
 | 3240 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3241 |                 param.toString().string()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3242 |     } | 
 | 3243 |     return status; | 
 | 3244 | } | 
 | 3245 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3246 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) | 
 | 3247 | { | 
 | 3248 |     Mutex::Autolock _l(mLock); | 
 | 3249 |     mTracks.add(track); | 
 | 3250 | } | 
 | 3251 |  | 
 | 3252 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) | 
 | 3253 | { | 
 | 3254 |     Mutex::Autolock _l(mLock); | 
 | 3255 |     destroyTrack_l(track); | 
 | 3256 | } | 
 | 3257 |  | 
 | 3258 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 3259 | { | 
 | 3260 |     ThreadBase::getAudioPortConfig(config); | 
 | 3261 |     config->role = AUDIO_PORT_ROLE_SOURCE; | 
 | 3262 |     config->ext.mix.hw_module = mOutput->audioHwDev->handle(); | 
 | 3263 |     config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
 | 3264 | } | 
 | 3265 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3266 | // ---------------------------------------------------------------------------- | 
 | 3267 |  | 
 | 3268 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3269 |         audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) | 
 | 3270 |     :   PlaybackThread(audioFlinger, output, id, device, type, systemReady), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3271 |         // mAudioMixer below | 
 | 3272 |         // mFastMixer below | 
 | 3273 |         mFastMixerFutex(0) | 
 | 3274 |         // mOutputSink below | 
 | 3275 |         // mPipeSink below | 
 | 3276 |         // mNormalSink below | 
 | 3277 | { | 
 | 3278 |     ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); | 
| Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 3279 |     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3280 |             "mFrameCount=%d, mNormalFrameCount=%d", | 
 | 3281 |             mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
 | 3282 |             mNormalFrameCount); | 
 | 3283 |     mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 3284 |  | 
| Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3285 |     if (type == DUPLICATING) { | 
 | 3286 |         // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks | 
 | 3287 |         // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). | 
 | 3288 |         // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. | 
 | 3289 |         return; | 
 | 3290 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3291 |     // create an NBAIO sink for the HAL output stream, and negotiate | 
 | 3292 |     mOutputSink = new AudioStreamOutSink(output->stream); | 
 | 3293 |     size_t numCounterOffers = 0; | 
| Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3294 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3295 |     ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 3296 |     ALOG_ASSERT(index == 0); | 
 | 3297 |  | 
 | 3298 |     // initialize fast mixer depending on configuration | 
 | 3299 |     bool initFastMixer; | 
 | 3300 |     switch (kUseFastMixer) { | 
 | 3301 |     case FastMixer_Never: | 
 | 3302 |         initFastMixer = false; | 
 | 3303 |         break; | 
 | 3304 |     case FastMixer_Always: | 
 | 3305 |         initFastMixer = true; | 
 | 3306 |         break; | 
 | 3307 |     case FastMixer_Static: | 
 | 3308 |     case FastMixer_Dynamic: | 
 | 3309 |         initFastMixer = mFrameCount < mNormalFrameCount; | 
 | 3310 |         break; | 
 | 3311 |     } | 
 | 3312 |     if (initFastMixer) { | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3313 |         audio_format_t fastMixerFormat; | 
 | 3314 |         if (mMixerBufferEnabled && mEffectBufferEnabled) { | 
 | 3315 |             fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; | 
 | 3316 |         } else { | 
 | 3317 |             fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; | 
 | 3318 |         } | 
 | 3319 |         if (mFormat != fastMixerFormat) { | 
 | 3320 |             // change our Sink format to accept our intermediate precision | 
 | 3321 |             mFormat = fastMixerFormat; | 
 | 3322 |             free(mSinkBuffer); | 
 | 3323 |             mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); | 
 | 3324 |             const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; | 
 | 3325 |             (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); | 
 | 3326 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3327 |  | 
 | 3328 |         // create a MonoPipe to connect our submix to FastMixer | 
 | 3329 |         NBAIO_Format format = mOutputSink->format(); | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3330 |         NBAIO_Format origformat = format; | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3331 |         // adjust format to match that of the Fast Mixer | 
| Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3332 |         ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); | 
| Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3333 |         format.mFormat = fastMixerFormat; | 
 | 3334 |         format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; | 
 | 3335 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3336 |         // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
 | 3337 |         // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
 | 3338 |         // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
 | 3339 |         MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
 | 3340 |         const NBAIO_Format offers[1] = {format}; | 
 | 3341 |         size_t numCounterOffers = 0; | 
 | 3342 |         ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 3343 |         ALOG_ASSERT(index == 0); | 
 | 3344 |         monoPipe->setAvgFrames((mScreenState & 1) ? | 
 | 3345 |                 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 3346 |         mPipeSink = monoPipe; | 
 | 3347 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3348 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3349 |         if (mTeeSinkOutputEnabled) { | 
 | 3350 |             // 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] | 3351 |             Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); | 
 | 3352 |             const NBAIO_Format offers2[1] = {origformat}; | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3353 |             numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3354 |             index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3355 |             ALOG_ASSERT(index == 0); | 
 | 3356 |             mTeeSink = teeSink; | 
 | 3357 |             PipeReader *teeSource = new PipeReader(*teeSink); | 
 | 3358 |             numCounterOffers = 0; | 
| Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3359 |             index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3360 |             ALOG_ASSERT(index == 0); | 
 | 3361 |             mTeeSource = teeSource; | 
 | 3362 |         } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3363 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3364 |  | 
 | 3365 |         // create fast mixer and configure it initially with just one fast track for our submix | 
 | 3366 |         mFastMixer = new FastMixer(); | 
 | 3367 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3368 | #ifdef STATE_QUEUE_DUMP | 
 | 3369 |         sq->setObserverDump(&mStateQueueObserverDump); | 
 | 3370 |         sq->setMutatorDump(&mStateQueueMutatorDump); | 
 | 3371 | #endif | 
 | 3372 |         FastMixerState *state = sq->begin(); | 
 | 3373 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 3374 |         // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
 | 3375 |         fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
 | 3376 |         fastTrack->mVolumeProvider = NULL; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3377 |         fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer | 
 | 3378 |         fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3379 |         fastTrack->mGeneration++; | 
 | 3380 |         state->mFastTracksGen++; | 
 | 3381 |         state->mTrackMask = 1; | 
 | 3382 |         // fast mixer will use the HAL output sink | 
 | 3383 |         state->mOutputSink = mOutputSink.get(); | 
 | 3384 |         state->mOutputSinkGen++; | 
 | 3385 |         state->mFrameCount = mFrameCount; | 
 | 3386 |         state->mCommand = FastMixerState::COLD_IDLE; | 
 | 3387 |         // already done in constructor initialization list | 
 | 3388 |         //mFastMixerFutex = 0; | 
 | 3389 |         state->mColdFutexAddr = &mFastMixerFutex; | 
 | 3390 |         state->mColdGen++; | 
 | 3391 |         state->mDumpState = &mFastMixerDumpState; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3392 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3393 |         state->mTeeSink = mTeeSink.get(); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3394 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3395 |         mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
 | 3396 |         state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3397 |         sq->end(); | 
 | 3398 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3399 |  | 
 | 3400 |         // start the fast mixer | 
 | 3401 |         mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
 | 3402 |         pid_t tid = mFastMixer->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3403 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3404 |  | 
 | 3405 | #ifdef AUDIO_WATCHDOG | 
 | 3406 |         // create and start the watchdog | 
 | 3407 |         mAudioWatchdog = new AudioWatchdog(); | 
 | 3408 |         mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
 | 3409 |         mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
 | 3410 |         tid = mAudioWatchdog->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3411 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3412 | #endif | 
 | 3413 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3414 |     } | 
 | 3415 |  | 
 | 3416 |     switch (kUseFastMixer) { | 
 | 3417 |     case FastMixer_Never: | 
 | 3418 |     case FastMixer_Dynamic: | 
 | 3419 |         mNormalSink = mOutputSink; | 
 | 3420 |         break; | 
 | 3421 |     case FastMixer_Always: | 
 | 3422 |         mNormalSink = mPipeSink; | 
 | 3423 |         break; | 
 | 3424 |     case FastMixer_Static: | 
 | 3425 |         mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
 | 3426 |         break; | 
 | 3427 |     } | 
 | 3428 | } | 
 | 3429 |  | 
 | 3430 | AudioFlinger::MixerThread::~MixerThread() | 
 | 3431 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3432 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3433 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3434 |         FastMixerState *state = sq->begin(); | 
 | 3435 |         if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 3436 |             int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 3437 |             if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3438 |                 (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3439 |             } | 
 | 3440 |         } | 
 | 3441 |         state->mCommand = FastMixerState::EXIT; | 
 | 3442 |         sq->end(); | 
 | 3443 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3444 |         mFastMixer->join(); | 
 | 3445 |         // Though the fast mixer thread has exited, it's state queue is still valid. | 
 | 3446 |         // We'll use that extract the final state which contains one remaining fast track | 
 | 3447 |         // corresponding to our sub-mix. | 
 | 3448 |         state = sq->begin(); | 
 | 3449 |         ALOG_ASSERT(state->mTrackMask == 1); | 
 | 3450 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 3451 |         ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
 | 3452 |         delete fastTrack->mBufferProvider; | 
 | 3453 |         sq->end(false /*didModify*/); | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3454 |         mFastMixer.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3455 | #ifdef AUDIO_WATCHDOG | 
 | 3456 |         if (mAudioWatchdog != 0) { | 
 | 3457 |             mAudioWatchdog->requestExit(); | 
 | 3458 |             mAudioWatchdog->requestExitAndWait(); | 
 | 3459 |             mAudioWatchdog.clear(); | 
 | 3460 |         } | 
 | 3461 | #endif | 
 | 3462 |     } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3463 |     mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3464 |     delete mAudioMixer; | 
 | 3465 | } | 
 | 3466 |  | 
 | 3467 |  | 
 | 3468 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const | 
 | 3469 | { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3470 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3471 |         MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 3472 |         latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
 | 3473 |     } | 
 | 3474 |     return latency; | 
 | 3475 | } | 
 | 3476 |  | 
 | 3477 |  | 
 | 3478 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) | 
 | 3479 | { | 
 | 3480 |     PlaybackThread::threadLoop_removeTracks(tracksToRemove); | 
 | 3481 | } | 
 | 3482 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3483 | ssize_t AudioFlinger::MixerThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3484 | { | 
 | 3485 |     // FIXME we should only do one push per cycle; confirm this is true | 
 | 3486 |     // Start the fast mixer if it's not already running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3487 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3488 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3489 |         FastMixerState *state = sq->begin(); | 
 | 3490 |         if (state->mCommand != FastMixerState::MIX_WRITE && | 
 | 3491 |                 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
 | 3492 |             if (state->mCommand == FastMixerState::COLD_IDLE) { | 
| Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3493 |  | 
 | 3494 |                 // FIXME workaround for first HAL write being CPU bound on some devices | 
 | 3495 |                 ATRACE_BEGIN("write"); | 
 | 3496 |                 mOutput->write((char *)mSinkBuffer, 0); | 
 | 3497 |                 ATRACE_END(); | 
 | 3498 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3499 |                 int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 3500 |                 if (old == -1) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3501 |                     (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3502 |                 } | 
 | 3503 | #ifdef AUDIO_WATCHDOG | 
 | 3504 |                 if (mAudioWatchdog != 0) { | 
 | 3505 |                     mAudioWatchdog->resume(); | 
 | 3506 |                 } | 
 | 3507 | #endif | 
 | 3508 |             } | 
 | 3509 |             state->mCommand = FastMixerState::MIX_WRITE; | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3510 | #ifdef FAST_THREAD_STATISTICS | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3511 |             mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3512 |                 FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3513 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3514 |             sq->end(); | 
 | 3515 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3516 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 3517 |                 mNormalSink = mPipeSink; | 
 | 3518 |             } | 
 | 3519 |         } else { | 
 | 3520 |             sq->end(false /*didModify*/); | 
 | 3521 |         } | 
 | 3522 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3523 |     return PlaybackThread::threadLoop_write(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3524 | } | 
 | 3525 |  | 
 | 3526 | void AudioFlinger::MixerThread::threadLoop_standby() | 
 | 3527 | { | 
 | 3528 |     // Idle the fast mixer if it's currently running | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3529 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3530 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3531 |         FastMixerState *state = sq->begin(); | 
 | 3532 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 3533 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 3534 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 3535 |             state->mColdGen++; | 
 | 3536 |             mFastMixerFutex = 0; | 
 | 3537 |             sq->end(); | 
 | 3538 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 3539 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 3540 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 3541 |                 mNormalSink = mOutputSink; | 
 | 3542 |             } | 
 | 3543 | #ifdef AUDIO_WATCHDOG | 
 | 3544 |             if (mAudioWatchdog != 0) { | 
 | 3545 |                 mAudioWatchdog->pause(); | 
 | 3546 |             } | 
 | 3547 | #endif | 
 | 3548 |         } else { | 
 | 3549 |             sq->end(false /*didModify*/); | 
 | 3550 |         } | 
 | 3551 |     } | 
 | 3552 |     PlaybackThread::threadLoop_standby(); | 
 | 3553 | } | 
 | 3554 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3555 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() | 
 | 3556 | { | 
 | 3557 |     return false; | 
 | 3558 | } | 
 | 3559 |  | 
 | 3560 | bool AudioFlinger::PlaybackThread::shouldStandby_l() | 
 | 3561 | { | 
 | 3562 |     return !mStandby; | 
 | 3563 | } | 
 | 3564 |  | 
 | 3565 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() | 
 | 3566 | { | 
 | 3567 |     Mutex::Autolock _l(mLock); | 
 | 3568 |     return waitingAsyncCallback_l(); | 
 | 3569 | } | 
 | 3570 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3571 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
 | 3572 | void AudioFlinger::PlaybackThread::threadLoop_standby() | 
 | 3573 | { | 
 | 3574 |     ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3575 |     mOutput->standby(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3576 |     if (mUseAsyncWrite != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3577 |         // discard any pending drain or write ack by incrementing sequence | 
 | 3578 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 3579 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3580 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3581 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 3582 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3583 |     } | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3584 |     mHwPaused = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3585 | } | 
 | 3586 |  | 
| Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3587 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() | 
 | 3588 | { | 
 | 3589 |     ALOGV("signal playback thread"); | 
 | 3590 |     broadcast_l(); | 
 | 3591 | } | 
 | 3592 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3593 | void AudioFlinger::MixerThread::threadLoop_mix() | 
 | 3594 | { | 
 | 3595 |     // obtain the presentation timestamp of the next output buffer | 
 | 3596 |     int64_t pts; | 
 | 3597 |     status_t status = INVALID_OPERATION; | 
 | 3598 |  | 
 | 3599 |     if (mNormalSink != 0) { | 
 | 3600 |         status = mNormalSink->getNextWriteTimestamp(&pts); | 
 | 3601 |     } else { | 
 | 3602 |         status = mOutputSink->getNextWriteTimestamp(&pts); | 
 | 3603 |     } | 
 | 3604 |  | 
 | 3605 |     if (status != NO_ERROR) { | 
 | 3606 |         pts = AudioBufferProvider::kInvalidPTS; | 
 | 3607 |     } | 
 | 3608 |  | 
 | 3609 |     // mix buffers... | 
 | 3610 |     mAudioMixer->process(pts); | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3611 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 |     // increase sleep time progressively when application underrun condition clears. | 
 | 3613 |     // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
 | 3614 |     // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
 | 3615 |     // such that we would underrun the audio HAL. | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3616 |     if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3617 |         sleepTimeShift--; | 
 | 3618 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3619 |     mSleepTimeUs = 0; | 
 | 3620 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3621 |     //TODO: delay standby when effects have a tail | 
| Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3622 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3623 | } | 
 | 3624 |  | 
 | 3625 | void AudioFlinger::MixerThread::threadLoop_sleepTime() | 
 | 3626 | { | 
 | 3627 |     // If no tracks are ready, sleep once for the duration of an output | 
 | 3628 |     // buffer size, then write 0s to the output | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3629 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3630 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3631 |             mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; | 
 | 3632 |             if (mSleepTimeUs < kMinThreadSleepTimeUs) { | 
 | 3633 |                 mSleepTimeUs = kMinThreadSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3634 |             } | 
 | 3635 |             // reduce sleep time in case of consecutive application underruns to avoid | 
 | 3636 |             // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
 | 3637 |             // duration we would end up writing less data than needed by the audio HAL if | 
 | 3638 |             // the condition persists. | 
 | 3639 |             if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
 | 3640 |                 sleepTimeShift++; | 
 | 3641 |             } | 
 | 3642 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3643 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3644 |         } | 
 | 3645 |     } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3646 |         // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared | 
 | 3647 |         // before effects processing or output. | 
 | 3648 |         if (mMixerBufferValid) { | 
 | 3649 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 3650 |         } else { | 
 | 3651 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 3652 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3653 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3654 |         ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
 | 3655 |                 "anticipated start"); | 
 | 3656 |     } | 
 | 3657 |     // TODO add standby time extension fct of effect tail | 
 | 3658 | } | 
 | 3659 |  | 
 | 3660 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
 | 3661 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( | 
 | 3662 |         Vector< sp<Track> > *tracksToRemove) | 
 | 3663 | { | 
 | 3664 |  | 
 | 3665 |     mixer_state mixerStatus = MIXER_IDLE; | 
 | 3666 |     // find out which tracks need to be processed | 
 | 3667 |     size_t count = mActiveTracks.size(); | 
 | 3668 |     size_t mixedTracks = 0; | 
 | 3669 |     size_t tracksWithEffect = 0; | 
 | 3670 |     // counts only _active_ fast tracks | 
 | 3671 |     size_t fastTracks = 0; | 
 | 3672 |     uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
 | 3673 |  | 
 | 3674 |     float masterVolume = mMasterVolume; | 
 | 3675 |     bool masterMute = mMasterMute; | 
 | 3676 |  | 
 | 3677 |     if (masterMute) { | 
 | 3678 |         masterVolume = 0; | 
 | 3679 |     } | 
 | 3680 |     // Delegate master volume control to effect in output mix effect chain if needed | 
 | 3681 |     sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
 | 3682 |     if (chain != 0) { | 
 | 3683 |         uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
 | 3684 |         chain->setVolume_l(&v, &v); | 
 | 3685 |         masterVolume = (float)((v + (1 << 23)) >> 24); | 
 | 3686 |         chain.clear(); | 
 | 3687 |     } | 
 | 3688 |  | 
 | 3689 |     // prepare a new state to push | 
 | 3690 |     FastMixerStateQueue *sq = NULL; | 
 | 3691 |     FastMixerState *state = NULL; | 
 | 3692 |     bool didModify = false; | 
 | 3693 |     FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3694 |     if (mFastMixer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3695 |         sq = mFastMixer->sq(); | 
 | 3696 |         state = sq->begin(); | 
 | 3697 |     } | 
 | 3698 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3699 |     mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found. | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3700 |     mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3701 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3702 |     for (size_t i=0 ; i<count ; i++) { | 
| Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3703 |         const sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3704 |         if (t == 0) { | 
 | 3705 |             continue; | 
 | 3706 |         } | 
 | 3707 |  | 
 | 3708 |         // this const just means the local variable doesn't change | 
 | 3709 |         Track* const track = t.get(); | 
 | 3710 |  | 
 | 3711 |         // process fast tracks | 
 | 3712 |         if (track->isFastTrack()) { | 
 | 3713 |  | 
 | 3714 |             // It's theoretically possible (though unlikely) for a fast track to be created | 
 | 3715 |             // and then removed within the same normal mix cycle.  This is not a problem, as | 
 | 3716 |             // the track never becomes active so it's fast mixer slot is never touched. | 
 | 3717 |             // The converse, of removing an (active) track and then creating a new track | 
 | 3718 |             // at the identical fast mixer slot within the same normal mix cycle, | 
 | 3719 |             // is impossible because the slot isn't marked available until the end of each cycle. | 
 | 3720 |             int j = track->mFastIndex; | 
 | 3721 |             ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); | 
 | 3722 |             ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
 | 3723 |             FastTrack *fastTrack = &state->mFastTracks[j]; | 
 | 3724 |  | 
 | 3725 |             // Determine whether the track is currently in underrun condition, | 
 | 3726 |             // and whether it had a recent underrun. | 
 | 3727 |             FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
 | 3728 |             FastTrackUnderruns underruns = ftDump->mUnderruns; | 
 | 3729 |             uint32_t recentFull = (underruns.mBitFields.mFull - | 
 | 3730 |                     track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; | 
 | 3731 |             uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
 | 3732 |                     track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; | 
 | 3733 |             uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
 | 3734 |                     track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; | 
 | 3735 |             uint32_t recentUnderruns = recentPartial + recentEmpty; | 
 | 3736 |             track->mObservedUnderruns = underruns; | 
 | 3737 |             // don't count underruns that occur while stopping or pausing | 
 | 3738 |             // or stopped which can occur when flush() is called while active | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3739 |             if (!(track->isStopping() || track->isPausing() || track->isStopped()) && | 
 | 3740 |                     recentUnderruns > 0) { | 
 | 3741 |                 // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun | 
 | 3742 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3743 |             } | 
 | 3744 |  | 
 | 3745 |             // This is similar to the state machine for normal tracks, | 
 | 3746 |             // with a few modifications for fast tracks. | 
 | 3747 |             bool isActive = true; | 
 | 3748 |             switch (track->mState) { | 
 | 3749 |             case TrackBase::STOPPING_1: | 
 | 3750 |                 // track stays active in STOPPING_1 state until first underrun | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3751 |                 if (recentUnderruns > 0 || track->isTerminated()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3752 |                     track->mState = TrackBase::STOPPING_2; | 
 | 3753 |                 } | 
 | 3754 |                 break; | 
 | 3755 |             case TrackBase::PAUSING: | 
 | 3756 |                 // ramp down is not yet implemented | 
 | 3757 |                 track->setPaused(); | 
 | 3758 |                 break; | 
 | 3759 |             case TrackBase::RESUMING: | 
 | 3760 |                 // ramp up is not yet implemented | 
 | 3761 |                 track->mState = TrackBase::ACTIVE; | 
 | 3762 |                 break; | 
 | 3763 |             case TrackBase::ACTIVE: | 
 | 3764 |                 if (recentFull > 0 || recentPartial > 0) { | 
 | 3765 |                     // track has provided at least some frames recently: reset retry count | 
 | 3766 |                     track->mRetryCount = kMaxTrackRetries; | 
 | 3767 |                 } | 
 | 3768 |                 if (recentUnderruns == 0) { | 
 | 3769 |                     // no recent underruns: stay active | 
 | 3770 |                     break; | 
 | 3771 |                 } | 
 | 3772 |                 // there has recently been an underrun of some kind | 
 | 3773 |                 if (track->sharedBuffer() == 0) { | 
 | 3774 |                     // were any of the recent underruns "empty" (no frames available)? | 
 | 3775 |                     if (recentEmpty == 0) { | 
 | 3776 |                         // no, then ignore the partial underruns as they are allowed indefinitely | 
 | 3777 |                         break; | 
 | 3778 |                     } | 
 | 3779 |                     // there has recently been an "empty" underrun: decrement the retry counter | 
 | 3780 |                     if (--(track->mRetryCount) > 0) { | 
 | 3781 |                         break; | 
 | 3782 |                     } | 
 | 3783 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 3784 |                     // it will then automatically call start() when data is available | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3785 |                     android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3786 |                     // remove from active list, but state remains ACTIVE [confusing but true] | 
 | 3787 |                     isActive = false; | 
 | 3788 |                     break; | 
 | 3789 |                 } | 
 | 3790 |                 // fall through | 
 | 3791 |             case TrackBase::STOPPING_2: | 
 | 3792 |             case TrackBase::PAUSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3793 |             case TrackBase::STOPPED: | 
 | 3794 |             case TrackBase::FLUSHED:   // flush() while active | 
 | 3795 |                 // Check for presentation complete if track is inactive | 
 | 3796 |                 // We have consumed all the buffers of this track. | 
 | 3797 |                 // This would be incomplete if we auto-paused on underrun | 
 | 3798 |                 { | 
 | 3799 |                     size_t audioHALFrames = | 
 | 3800 |                             (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
 | 3801 |                     size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 3802 |                     if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
 | 3803 |                         // track stays in active list until presentation is complete | 
 | 3804 |                         break; | 
 | 3805 |                     } | 
 | 3806 |                 } | 
 | 3807 |                 if (track->isStopping_2()) { | 
 | 3808 |                     track->mState = TrackBase::STOPPED; | 
 | 3809 |                 } | 
 | 3810 |                 if (track->isStopped()) { | 
 | 3811 |                     // Can't reset directly, as fast mixer is still polling this track | 
 | 3812 |                     //   track->reset(); | 
 | 3813 |                     // So instead mark this track as needing to be reset after push with ack | 
 | 3814 |                     resetMask |= 1 << i; | 
 | 3815 |                 } | 
 | 3816 |                 isActive = false; | 
 | 3817 |                 break; | 
 | 3818 |             case TrackBase::IDLE: | 
 | 3819 |             default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3820 |                 LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3821 |             } | 
 | 3822 |  | 
 | 3823 |             if (isActive) { | 
 | 3824 |                 // was it previously inactive? | 
 | 3825 |                 if (!(state->mTrackMask & (1 << j))) { | 
 | 3826 |                     ExtendedAudioBufferProvider *eabp = track; | 
 | 3827 |                     VolumeProvider *vp = track; | 
 | 3828 |                     fastTrack->mBufferProvider = eabp; | 
 | 3829 |                     fastTrack->mVolumeProvider = vp; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3830 |                     fastTrack->mChannelMask = track->mChannelMask; | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3831 |                     fastTrack->mFormat = track->mFormat; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3832 |                     fastTrack->mGeneration++; | 
 | 3833 |                     state->mTrackMask |= 1 << j; | 
 | 3834 |                     didModify = true; | 
 | 3835 |                     // no acknowledgement required for newly active tracks | 
 | 3836 |                 } | 
 | 3837 |                 // cache the combined master volume and stream type volume for fast mixer; this | 
 | 3838 |                 // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3839 |                 track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3840 |                 ++fastTracks; | 
 | 3841 |             } else { | 
 | 3842 |                 // was it previously active? | 
 | 3843 |                 if (state->mTrackMask & (1 << j)) { | 
 | 3844 |                     fastTrack->mBufferProvider = NULL; | 
 | 3845 |                     fastTrack->mGeneration++; | 
 | 3846 |                     state->mTrackMask &= ~(1 << j); | 
 | 3847 |                     didModify = true; | 
 | 3848 |                     // If any fast tracks were removed, we must wait for acknowledgement | 
 | 3849 |                     // because we're about to decrement the last sp<> on those tracks. | 
 | 3850 |                     block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 3851 |                 } else { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3852 |                     LOG_ALWAYS_FATAL("fast track %d should have been active", j); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3853 |                 } | 
 | 3854 |                 tracksToRemove->add(track); | 
 | 3855 |                 // Avoids a misleading display in dumpsys | 
 | 3856 |                 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; | 
 | 3857 |             } | 
 | 3858 |             continue; | 
 | 3859 |         } | 
 | 3860 |  | 
 | 3861 |         {   // local variable scope to avoid goto warning | 
 | 3862 |  | 
 | 3863 |         audio_track_cblk_t* cblk = track->cblk(); | 
 | 3864 |  | 
 | 3865 |         // The first time a track is added we wait | 
 | 3866 |         // for all its buffers to be filled before processing it | 
 | 3867 |         int name = track->name(); | 
 | 3868 |         // make sure that we have enough frames to mix one full buffer. | 
 | 3869 |         // enforce this condition only once to enable draining the buffer in case the client | 
 | 3870 |         // app does not call stop() and relies on underrun to stop: | 
 | 3871 |         // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
 | 3872 |         // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3873 |         size_t desiredFrames; | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3874 |         const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 3875 |         AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3876 |  | 
 | 3877 |         desiredFrames = sourceFramesNeededWithTimestretch( | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 3878 |                 sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3879 |         // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. | 
 | 3880 |         // add frames already consumed but not yet released by the resampler | 
 | 3881 |         // because mAudioTrackServerProxy->framesReady() will include these frames | 
 | 3882 |         desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
 | 3883 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3884 |         uint32_t minFrames = 1; | 
 | 3885 |         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
 | 3886 |                 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3887 |             minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3888 |         } | 
| Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 3889 |  | 
 | 3890 |         size_t framesReady = track->framesReady(); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3891 |         if (ATRACE_ENABLED()) { | 
 | 3892 |             // I wish we had formatted trace names | 
 | 3893 |             char traceName[16]; | 
 | 3894 |             strcpy(traceName, "nRdy"); | 
 | 3895 |             int name = track->name(); | 
 | 3896 |             if (AudioMixer::TRACK0 <= name && | 
 | 3897 |                     name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { | 
 | 3898 |                 name -= AudioMixer::TRACK0; | 
 | 3899 |                 traceName[4] = (name / 10) + '0'; | 
 | 3900 |                 traceName[5] = (name % 10) + '0'; | 
 | 3901 |             } else { | 
 | 3902 |                 traceName[4] = '?'; | 
 | 3903 |                 traceName[5] = '?'; | 
 | 3904 |             } | 
 | 3905 |             traceName[6] = '\0'; | 
 | 3906 |             ATRACE_INT(traceName, framesReady); | 
 | 3907 |         } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3908 |         if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3909 |                 !track->isPaused() && !track->isTerminated()) | 
 | 3910 |         { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3911 |             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] | 3912 |  | 
 | 3913 |             mixedTracks++; | 
 | 3914 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3915 |             // track->mainBuffer() != mSinkBuffer or mMixerBuffer means | 
 | 3916 |             // there is an effect chain connected to the track | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3917 |             chain.clear(); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3918 |             if (track->mainBuffer() != mSinkBuffer && | 
 | 3919 |                     track->mainBuffer() != mMixerBuffer) { | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3920 |                 if (mEffectBufferEnabled) { | 
 | 3921 |                     mEffectBufferValid = true; // Later can set directly. | 
 | 3922 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3923 |                 chain = getEffectChain_l(track->sessionId()); | 
 | 3924 |                 // Delegate volume control to effect in track effect chain if needed | 
 | 3925 |                 if (chain != 0) { | 
 | 3926 |                     tracksWithEffect++; | 
 | 3927 |                 } else { | 
 | 3928 |                     ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " | 
 | 3929 |                             "session %d", | 
 | 3930 |                             name, track->sessionId()); | 
 | 3931 |                 } | 
 | 3932 |             } | 
 | 3933 |  | 
 | 3934 |  | 
 | 3935 |             int param = AudioMixer::VOLUME; | 
 | 3936 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 3937 |                 // no ramp for the first volume setting | 
 | 3938 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
 | 3939 |                 if (track->mState == TrackBase::RESUMING) { | 
 | 3940 |                     track->mState = TrackBase::ACTIVE; | 
 | 3941 |                     param = AudioMixer::RAMP_VOLUME; | 
 | 3942 |                 } | 
 | 3943 |                 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3944 |             // FIXME should not make a decision based on mServer | 
 | 3945 |             } else if (cblk->mServer != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3946 |                 // If the track is stopped before the first frame was mixed, | 
 | 3947 |                 // do not apply ramp | 
 | 3948 |                 param = AudioMixer::RAMP_VOLUME; | 
 | 3949 |             } | 
 | 3950 |  | 
 | 3951 |             // compute volume for this track | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3952 |             uint32_t vl, vr;       // in U8.24 integer format | 
 | 3953 |             float vlf, vrf, vaf;   // in [0.0, 1.0] float format | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3954 |             if (track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3955 |                 vl = vr = 0; | 
 | 3956 |                 vlf = vrf = vaf = 0.; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3957 |                 if (track->isPausing()) { | 
 | 3958 |                     track->setPaused(); | 
 | 3959 |                 } | 
 | 3960 |             } else { | 
 | 3961 |  | 
 | 3962 |                 // read original volumes with volume control | 
 | 3963 |                 float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 3964 |                 float v = masterVolume * typeVolume; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3965 |                 AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3966 |                 gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3967 |                 vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 3968 |                 vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3969 |                 // 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] | 3970 |                 if (vlf > GAIN_FLOAT_UNITY) { | 
 | 3971 |                     ALOGV("Track left volume out of range: %.3g", vlf); | 
 | 3972 |                     vlf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3973 |                 } | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3974 |                 if (vrf > GAIN_FLOAT_UNITY) { | 
 | 3975 |                     ALOGV("Track right volume out of range: %.3g", vrf); | 
 | 3976 |                     vrf = GAIN_FLOAT_UNITY; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3977 |                 } | 
 | 3978 |                 // now apply the master volume and stream type volume | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3979 |                 vlf *= v; | 
 | 3980 |                 vrf *= v; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3981 |                 // 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] | 3982 |                 // then derive vl and vr as U8.24 versions for the effect chain | 
 | 3983 |                 const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; | 
 | 3984 |                 vl = (uint32_t) (scaleto8_24 * vlf); | 
 | 3985 |                 vr = (uint32_t) (scaleto8_24 * vrf); | 
 | 3986 |                 // vl and vr are now in U8.24 format | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3987 |                 uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3988 |                 // send level comes from shared memory and so may be corrupt | 
 | 3989 |                 if (sendLevel > MAX_GAIN_INT) { | 
 | 3990 |                     ALOGV("Track send level out of range: %04X", sendLevel); | 
 | 3991 |                     sendLevel = MAX_GAIN_INT; | 
 | 3992 |                 } | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3993 |                 // vaf is represented as [0.0, 1.0] float by rescaling sendLevel | 
 | 3994 |                 vaf = v * sendLevel * (1. / MAX_GAIN_INT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3995 |             } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3996 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3997 |             // Delegate volume control to effect in track effect chain if needed | 
 | 3998 |             if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
 | 3999 |                 // Do not ramp volume if volume is controlled by effect | 
 | 4000 |                 param = AudioMixer::VOLUME; | 
| Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4001 |                 // Update remaining floating point volume levels | 
 | 4002 |                 vlf = (float)vl / (1 << 24); | 
 | 4003 |                 vrf = (float)vr / (1 << 24); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4004 |                 track->mHasVolumeController = true; | 
 | 4005 |             } else { | 
 | 4006 |                 // force no volume ramp when volume controller was just disabled or removed | 
 | 4007 |                 // from effect chain to avoid volume spike | 
 | 4008 |                 if (track->mHasVolumeController) { | 
 | 4009 |                     param = AudioMixer::VOLUME; | 
 | 4010 |                 } | 
 | 4011 |                 track->mHasVolumeController = false; | 
 | 4012 |             } | 
 | 4013 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4014 |             // XXX: these things DON'T need to be done each time | 
 | 4015 |             mAudioMixer->setBufferProvider(name, track); | 
 | 4016 |             mAudioMixer->enable(name); | 
 | 4017 |  | 
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4018 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); | 
 | 4019 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); | 
 | 4020 |             mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4021 |             mAudioMixer->setParameter( | 
 | 4022 |                 name, | 
 | 4023 |                 AudioMixer::TRACK, | 
 | 4024 |                 AudioMixer::FORMAT, (void *)track->format()); | 
 | 4025 |             mAudioMixer->setParameter( | 
 | 4026 |                 name, | 
 | 4027 |                 AudioMixer::TRACK, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4028 |                 AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4029 |             mAudioMixer->setParameter( | 
 | 4030 |                 name, | 
 | 4031 |                 AudioMixer::TRACK, | 
 | 4032 |                 AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4033 |             // 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] | 4034 |             uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4035 |             uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4036 |             if (reqSampleRate == 0) { | 
 | 4037 |                 reqSampleRate = mSampleRate; | 
 | 4038 |             } else if (reqSampleRate > maxSampleRate) { | 
 | 4039 |                 reqSampleRate = maxSampleRate; | 
 | 4040 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4041 |             mAudioMixer->setParameter( | 
 | 4042 |                 name, | 
 | 4043 |                 AudioMixer::RESAMPLE, | 
 | 4044 |                 AudioMixer::SAMPLE_RATE, | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4045 |                 (void *)(uintptr_t)reqSampleRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4046 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4047 |             AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4048 |             mAudioMixer->setParameter( | 
 | 4049 |                 name, | 
 | 4050 |                 AudioMixer::TIMESTRETCH, | 
 | 4051 |                 AudioMixer::PLAYBACK_RATE, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4052 |                 &playbackRate); | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4053 |  | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4054 |             /* | 
 | 4055 |              * Select the appropriate output buffer for the track. | 
 | 4056 |              * | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4057 |              * Tracks with effects go into their own effects chain buffer | 
 | 4058 |              * and from there into either mEffectBuffer or mSinkBuffer. | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4059 |              * | 
 | 4060 |              * Other tracks can use mMixerBuffer for higher precision | 
 | 4061 |              * channel accumulation.  If this buffer is enabled | 
 | 4062 |              * (mMixerBufferEnabled true), then selected tracks will accumulate | 
 | 4063 |              * into it. | 
 | 4064 |              * | 
 | 4065 |              */ | 
 | 4066 |             if (mMixerBufferEnabled | 
 | 4067 |                     && (track->mainBuffer() == mSinkBuffer | 
 | 4068 |                             || track->mainBuffer() == mMixerBuffer)) { | 
 | 4069 |                 mAudioMixer->setParameter( | 
 | 4070 |                         name, | 
 | 4071 |                         AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4072 |                         AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4073 |                 mAudioMixer->setParameter( | 
 | 4074 |                         name, | 
 | 4075 |                         AudioMixer::TRACK, | 
 | 4076 |                         AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); | 
 | 4077 |                 // TODO: override track->mainBuffer()? | 
 | 4078 |                 mMixerBufferValid = true; | 
 | 4079 |             } else { | 
 | 4080 |                 mAudioMixer->setParameter( | 
 | 4081 |                         name, | 
 | 4082 |                         AudioMixer::TRACK, | 
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4083 |                         AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4084 |                 mAudioMixer->setParameter( | 
 | 4085 |                         name, | 
 | 4086 |                         AudioMixer::TRACK, | 
 | 4087 |                         AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
 | 4088 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4089 |             mAudioMixer->setParameter( | 
 | 4090 |                 name, | 
 | 4091 |                 AudioMixer::TRACK, | 
 | 4092 |                 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
 | 4093 |  | 
 | 4094 |             // reset retry count | 
 | 4095 |             track->mRetryCount = kMaxTrackRetries; | 
 | 4096 |  | 
 | 4097 |             // If one track is ready, set the mixer ready if: | 
 | 4098 |             //  - the mixer was not ready during previous round OR | 
 | 4099 |             //  - no other track is not ready | 
 | 4100 |             if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
 | 4101 |                     mixerStatus != MIXER_TRACKS_ENABLED) { | 
 | 4102 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 4103 |             } | 
 | 4104 |         } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4105 |             if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
| Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4106 |                 ALOGV("track(%p) underrun,  framesReady(%zu) < framesDesired(%zd)", | 
 | 4107 |                         track, framesReady, desiredFrames); | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4108 |                 track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4109 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4110 |             // clear effect chain input buffer if an active track underruns to avoid sending | 
 | 4111 |             // previous audio buffer again to effects | 
 | 4112 |             chain = getEffectChain_l(track->sessionId()); | 
 | 4113 |             if (chain != 0) { | 
 | 4114 |                 chain->clearInputBuffer(); | 
 | 4115 |             } | 
 | 4116 |  | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4117 |             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] | 4118 |             if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
 | 4119 |                     track->isStopped() || track->isPaused()) { | 
 | 4120 |                 // We have consumed all the buffers of this track. | 
 | 4121 |                 // Remove it from the list of active tracks. | 
 | 4122 |                 // TODO: use actual buffer filling status instead of latency when available from | 
 | 4123 |                 // audio HAL | 
 | 4124 |                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 4125 |                 size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 4126 |                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
 | 4127 |                     if (track->isStopped()) { | 
 | 4128 |                         track->reset(); | 
 | 4129 |                     } | 
 | 4130 |                     tracksToRemove->add(track); | 
 | 4131 |                 } | 
 | 4132 |             } else { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4133 |                 // No buffers for this track. Give it a few chances to | 
 | 4134 |                 // fill a buffer, then remove it from active list. | 
 | 4135 |                 if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4136 |                     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] | 4137 |                     tracksToRemove->add(track); | 
 | 4138 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 4139 |                     // it will then automatically call start() when data is available | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 4140 |                     android_atomic_or(CBLK_DISABLED, &cblk->mFlags); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4141 |                 // If one track is not ready, mark the mixer also not ready if: | 
 | 4142 |                 //  - the mixer was ready during previous round OR | 
 | 4143 |                 //  - no other track is ready | 
 | 4144 |                 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
 | 4145 |                                 mixerStatus != MIXER_TRACKS_READY) { | 
 | 4146 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 4147 |                 } | 
 | 4148 |             } | 
 | 4149 |             mAudioMixer->disable(name); | 
 | 4150 |         } | 
 | 4151 |  | 
 | 4152 |         }   // local variable scope to avoid goto warning | 
 | 4153 | track_is_ready: ; | 
 | 4154 |  | 
 | 4155 |     } | 
 | 4156 |  | 
 | 4157 |     // Push the new FastMixer state if necessary | 
 | 4158 |     bool pauseAudioWatchdog = false; | 
 | 4159 |     if (didModify) { | 
 | 4160 |         state->mFastTracksGen++; | 
 | 4161 |         // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
 | 4162 |         if (kUseFastMixer == FastMixer_Dynamic && | 
 | 4163 |                 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
 | 4164 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 4165 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 4166 |             state->mColdGen++; | 
 | 4167 |             mFastMixerFutex = 0; | 
 | 4168 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 4169 |                 mNormalSink = mOutputSink; | 
 | 4170 |             } | 
 | 4171 |             // If we go into cold idle, need to wait for acknowledgement | 
 | 4172 |             // so that fast mixer stops doing I/O. | 
 | 4173 |             block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 4174 |             pauseAudioWatchdog = true; | 
 | 4175 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4176 |     } | 
 | 4177 |     if (sq != NULL) { | 
 | 4178 |         sq->end(didModify); | 
 | 4179 |         sq->push(block); | 
 | 4180 |     } | 
 | 4181 | #ifdef AUDIO_WATCHDOG | 
 | 4182 |     if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
 | 4183 |         mAudioWatchdog->pause(); | 
 | 4184 |     } | 
 | 4185 | #endif | 
 | 4186 |  | 
 | 4187 |     // Now perform the deferred reset on fast tracks that have stopped | 
 | 4188 |     while (resetMask != 0) { | 
 | 4189 |         size_t i = __builtin_ctz(resetMask); | 
 | 4190 |         ALOG_ASSERT(i < count); | 
 | 4191 |         resetMask &= ~(1 << i); | 
 | 4192 |         sp<Track> t = mActiveTracks[i].promote(); | 
 | 4193 |         if (t == 0) { | 
 | 4194 |             continue; | 
 | 4195 |         } | 
 | 4196 |         Track* track = t.get(); | 
 | 4197 |         ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
 | 4198 |         track->reset(); | 
 | 4199 |     } | 
 | 4200 |  | 
 | 4201 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4202 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4203 |  | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4204 |     if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { | 
 | 4205 |         mEffectBufferValid = true; | 
| Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4206 |     } | 
 | 4207 |  | 
 | 4208 |     if (mEffectBufferValid) { | 
| Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4209 |         // as long as there are effects we should clear the effects buffer, to avoid | 
 | 4210 |         // passing a non-clean buffer to the effect chain | 
 | 4211 |         memset(mEffectBuffer, 0, mEffectBufferSize); | 
| Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4212 |     } | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4213 |     // sink or mix buffer must be cleared if all tracks are connected to an | 
 | 4214 |     // effect chain as in this case the mixer will not write to the sink or mix buffer | 
 | 4215 |     // and track effects will accumulate into it | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4216 |     if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 4217 |             (mixedTracks == 0 && fastTracks > 0))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4218 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4219 |         if (mMixerBufferValid) { | 
 | 4220 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 4221 |             // TODO: In testing, mSinkBuffer below need not be cleared because | 
 | 4222 |             // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer | 
 | 4223 |             // after mixing. | 
 | 4224 |             // | 
 | 4225 |             // To enforce this guarantee: | 
 | 4226 |             // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 4227 |             // (mixedTracks == 0 && fastTracks > 0)) | 
 | 4228 |             // must imply MIXER_TRACKS_READY. | 
 | 4229 |             // Later, we may clear buffers regardless, and skip much of this logic. | 
 | 4230 |         } | 
| Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4231 |         // FIXME as a performance optimization, should remember previous zero status | 
| Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4232 |         memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4233 |     } | 
 | 4234 |  | 
 | 4235 |     // if any fast tracks, then status is ready | 
 | 4236 |     mMixerStatusIgnoringFastTracks = mixerStatus; | 
 | 4237 |     if (fastTracks > 0) { | 
 | 4238 |         mixerStatus = MIXER_TRACKS_READY; | 
 | 4239 |     } | 
 | 4240 |     return mixerStatus; | 
 | 4241 | } | 
 | 4242 |  | 
 | 4243 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4244 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, | 
 | 4245 |         audio_format_t format, int sessionId) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4246 | { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4247 |     return mAudioMixer->getTrackName(channelMask, format, sessionId); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4248 | } | 
 | 4249 |  | 
 | 4250 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
 | 4251 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
 | 4252 | { | 
 | 4253 |     ALOGV("remove track (%d) and delete from mixer", name); | 
 | 4254 |     mAudioMixer->deleteTrackName(name); | 
 | 4255 | } | 
 | 4256 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4257 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 4258 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 4259 |                                                        status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4260 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4261 |     bool reconfig = false; | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4262 |     bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4263 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4264 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4265 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4266 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 4267 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4268 |     if (mFastMixer != 0) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4269 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 4270 |         FastMixerState *state = sq->begin(); | 
 | 4271 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 4272 |             previousCommand = state->mCommand; | 
 | 4273 |             state->mCommand = FastMixerState::HOT_IDLE; | 
 | 4274 |             sq->end(); | 
 | 4275 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 4276 |         } else { | 
 | 4277 |             sq->end(false /*didModify*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4278 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4279 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4280 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4281 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 4282 |     int value; | 
 | 4283 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 4284 |         reconfig = true; | 
 | 4285 |     } | 
 | 4286 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4287 |         if (!isValidPcmSinkFormat((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4288 |             status = BAD_VALUE; | 
 | 4289 |         } else { | 
 | 4290 |             // no need to save value, since it's constant | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4291 |             reconfig = true; | 
 | 4292 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4293 |     } | 
 | 4294 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4295 |         if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4296 |             status = BAD_VALUE; | 
 | 4297 |         } else { | 
 | 4298 |             // no need to save value, since it's constant | 
 | 4299 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4300 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4301 |     } | 
 | 4302 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 4303 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 4304 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 4305 |         // if frame count is changed after track creation | 
 | 4306 |         if (!mTracks.isEmpty()) { | 
 | 4307 |             status = INVALID_OPERATION; | 
 | 4308 |         } else { | 
 | 4309 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4310 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4311 |     } | 
 | 4312 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4313 | #ifdef ADD_BATTERY_DATA | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4314 |         // when changing the audio output device, call addBatteryData to notify | 
 | 4315 |         // the change | 
 | 4316 |         if (mOutDevice != value) { | 
 | 4317 |             uint32_t params = 0; | 
 | 4318 |             // check whether speaker is on | 
 | 4319 |             if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 4320 |                 params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4321 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4322 |  | 
 | 4323 |             audio_devices_t deviceWithoutSpeaker | 
 | 4324 |                 = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 4325 |             // check if any other device (except speaker) is on | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4326 |             if (value & deviceWithoutSpeaker) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4327 |                 params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 4328 |             } | 
 | 4329 |  | 
 | 4330 |             if (params != 0) { | 
 | 4331 |                 addBatteryData(params); | 
 | 4332 |             } | 
 | 4333 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4334 | #endif | 
 | 4335 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4336 |         // forward device change to effects that have requested to be | 
 | 4337 |         // aware of attached audio device. | 
 | 4338 |         if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4339 |             a2dpDeviceChanged = | 
 | 4340 |                     (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4341 |             mOutDevice = value; | 
 | 4342 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4343 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4344 |             } | 
 | 4345 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4346 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4347 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4348 |     if (status == NO_ERROR) { | 
 | 4349 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 4350 |                                                 keyValuePair.string()); | 
 | 4351 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4352 |             mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4353 |             mStandby = true; | 
 | 4354 |             mBytesWritten = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4355 |             status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4356 |                                                    keyValuePair.string()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4357 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4358 |         if (status == NO_ERROR && reconfig) { | 
 | 4359 |             readOutputParameters_l(); | 
 | 4360 |             delete mAudioMixer; | 
 | 4361 |             mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 4362 |             for (size_t i = 0; i < mTracks.size() ; i++) { | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4363 |                 int name = getTrackName_l(mTracks[i]->mChannelMask, | 
 | 4364 |                         mTracks[i]->mFormat, mTracks[i]->mSessionId); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4365 |                 if (name < 0) { | 
 | 4366 |                     break; | 
 | 4367 |                 } | 
 | 4368 |                 mTracks[i]->mName = name; | 
 | 4369 |             } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4370 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4371 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4372 |     } | 
 | 4373 |  | 
 | 4374 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
| Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4375 |         ALOG_ASSERT(mFastMixer != 0); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4376 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 4377 |         FastMixerState *state = sq->begin(); | 
 | 4378 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 4379 |         state->mCommand = previousCommand; | 
 | 4380 |         sq->end(); | 
 | 4381 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 4382 |     } | 
 | 4383 |  | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4384 |     return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4385 | } | 
 | 4386 |  | 
 | 4387 |  | 
 | 4388 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 4389 | { | 
 | 4390 |     const size_t SIZE = 256; | 
 | 4391 |     char buffer[SIZE]; | 
 | 4392 |     String8 result; | 
 | 4393 |  | 
 | 4394 |     PlaybackThread::dumpInternals(fd, args); | 
| Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4395 |     dprintf(fd, "  Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4396 |     dprintf(fd, "  AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4397 |  | 
 | 4398 |     // Make a non-atomic copy of fast mixer dump state so it won't change underneath us | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 4399 |     const FastMixerDumpState copy(mFastMixerDumpState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4400 |     copy.dump(fd); | 
 | 4401 |  | 
 | 4402 | #ifdef STATE_QUEUE_DUMP | 
 | 4403 |     // Similar for state queue | 
 | 4404 |     StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
 | 4405 |     observerCopy.dump(fd); | 
 | 4406 |     StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
 | 4407 |     mutatorCopy.dump(fd); | 
 | 4408 | #endif | 
 | 4409 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4410 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4411 |     // Write the tee output to a .wav file | 
 | 4412 |     dumpTee(fd, mTeeSource, mId); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4413 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4414 |  | 
 | 4415 | #ifdef AUDIO_WATCHDOG | 
 | 4416 |     if (mAudioWatchdog != 0) { | 
 | 4417 |         // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
 | 4418 |         AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
 | 4419 |         wdCopy.dump(fd); | 
 | 4420 |     } | 
 | 4421 | #endif | 
 | 4422 | } | 
 | 4423 |  | 
 | 4424 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const | 
 | 4425 | { | 
 | 4426 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 4427 | } | 
 | 4428 |  | 
 | 4429 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const | 
 | 4430 | { | 
 | 4431 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 4432 | } | 
 | 4433 |  | 
 | 4434 | void AudioFlinger::MixerThread::cacheParameters_l() | 
 | 4435 | { | 
 | 4436 |     PlaybackThread::cacheParameters_l(); | 
 | 4437 |  | 
 | 4438 |     // FIXME: Relaxed timing because of a certain device that can't meet latency | 
 | 4439 |     // Should be reduced to 2x after the vendor fixes the driver issue | 
 | 4440 |     // increase threshold again due to low power audio mode. The way this warning | 
 | 4441 |     // threshold is calculated and its usefulness should be reconsidered anyway. | 
 | 4442 |     maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
 | 4443 | } | 
 | 4444 |  | 
 | 4445 | // ---------------------------------------------------------------------------- | 
 | 4446 |  | 
 | 4447 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4448 |         AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) | 
 | 4449 |     :   PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4450 |         // mLeftVolFloat, mRightVolFloat | 
 | 4451 | { | 
 | 4452 | } | 
 | 4453 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4454 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
 | 4455 |         AudioStreamOut* output, audio_io_handle_t id, uint32_t device, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4456 |         ThreadBase::type_t type, bool systemReady) | 
 | 4457 |     :   PlaybackThread(audioFlinger, output, id, device, type, systemReady) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4458 |         // mLeftVolFloat, mRightVolFloat | 
 | 4459 | { | 
 | 4460 | } | 
 | 4461 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4462 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
 | 4463 | { | 
 | 4464 | } | 
 | 4465 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4466 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) | 
 | 4467 | { | 
 | 4468 |     audio_track_cblk_t* cblk = track->cblk(); | 
 | 4469 |     float left, right; | 
 | 4470 |  | 
 | 4471 |     if (mMasterMute || mStreamTypes[track->streamType()].mute) { | 
 | 4472 |         left = right = 0; | 
 | 4473 |     } else { | 
 | 4474 |         float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 4475 |         float v = mMasterVolume * typeVolume; | 
 | 4476 |         AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4477 |         gain_minifloat_packed_t vlr = proxy->getVolumeLR(); | 
 | 4478 |         left = float_from_gain(gain_minifloat_unpack_left(vlr)); | 
 | 4479 |         if (left > GAIN_FLOAT_UNITY) { | 
 | 4480 |             left = GAIN_FLOAT_UNITY; | 
 | 4481 |         } | 
 | 4482 |         left *= v; | 
 | 4483 |         right = float_from_gain(gain_minifloat_unpack_right(vlr)); | 
 | 4484 |         if (right > GAIN_FLOAT_UNITY) { | 
 | 4485 |             right = GAIN_FLOAT_UNITY; | 
 | 4486 |         } | 
 | 4487 |         right *= v; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4488 |     } | 
 | 4489 |  | 
 | 4490 |     if (lastTrack) { | 
 | 4491 |         if (left != mLeftVolFloat || right != mRightVolFloat) { | 
 | 4492 |             mLeftVolFloat = left; | 
 | 4493 |             mRightVolFloat = right; | 
 | 4494 |  | 
 | 4495 |             // Convert volumes from float to 8.24 | 
 | 4496 |             uint32_t vl = (uint32_t)(left * (1 << 24)); | 
 | 4497 |             uint32_t vr = (uint32_t)(right * (1 << 24)); | 
 | 4498 |  | 
 | 4499 |             // Delegate volume control to effect in track effect chain if needed | 
 | 4500 |             // only one effect chain can be present on DirectOutputThread, so if | 
 | 4501 |             // there is one, the track is connected to it | 
 | 4502 |             if (!mEffectChains.isEmpty()) { | 
 | 4503 |                 mEffectChains[0]->setVolume_l(&vl, &vr); | 
 | 4504 |                 left = (float)vl / (1 << 24); | 
 | 4505 |                 right = (float)vr / (1 << 24); | 
 | 4506 |             } | 
 | 4507 |             if (mOutput->stream->set_volume) { | 
 | 4508 |                 mOutput->stream->set_volume(mOutput->stream, left, right); | 
 | 4509 |             } | 
 | 4510 |         } | 
 | 4511 |     } | 
 | 4512 | } | 
 | 4513 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4514 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() | 
 | 4515 | { | 
 | 4516 |     sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 4517 |     sp<Track> latestTrack = mLatestActiveTrack.promote(); | 
 | 4518 |  | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4519 |     if (previousTrack != 0 && latestTrack != 0) { | 
 | 4520 |         if (mType == DIRECT) { | 
 | 4521 |             if (previousTrack.get() != latestTrack.get()) { | 
 | 4522 |                 mFlushPending = true; | 
 | 4523 |             } | 
 | 4524 |         } else /* mType == OFFLOAD */ { | 
 | 4525 |             if (previousTrack->sessionId() != latestTrack->sessionId()) { | 
 | 4526 |                 mFlushPending = true; | 
 | 4527 |             } | 
 | 4528 |         } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4529 |     } | 
 | 4530 |     PlaybackThread::onAddNewTrack_l(); | 
 | 4531 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4532 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4533 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( | 
 | 4534 |     Vector< sp<Track> > *tracksToRemove | 
 | 4535 | ) | 
 | 4536 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4537 |     size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4538 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4539 |     bool doHwPause = false; | 
 | 4540 |     bool doHwResume = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4541 |  | 
 | 4542 |     // find out which tracks need to be processed | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4543 |     for (size_t i = 0; i < count; i++) { | 
 | 4544 |         sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4545 |         // The track died recently | 
 | 4546 |         if (t == 0) { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4547 |             continue; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4548 |         } | 
 | 4549 |  | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4550 |         if (t->isInvalid()) { | 
 | 4551 |             ALOGW("An invalidated track shouldn't be in active list"); | 
 | 4552 |             tracksToRemove->add(t); | 
 | 4553 |             continue; | 
 | 4554 |         } | 
 | 4555 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4556 |         Track* const track = t.get(); | 
 | 4557 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4558 |         // Only consider last track started for volume and mixer state control. | 
 | 4559 |         // In theory an older track could underrun and restart after the new one starts | 
 | 4560 |         // but as we only care about the transition phase between two tracks on a | 
 | 4561 |         // direct output, it is not a problem to ignore the underrun case. | 
 | 4562 |         sp<Track> l = mLatestActiveTrack.promote(); | 
 | 4563 |         bool last = l.get() == track; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4564 |  | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4565 |         if (track->isPausing()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4566 |             track->setPaused(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4567 |             if (mHwSupportsPause && last && !mHwPaused) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4568 |                 doHwPause = true; | 
 | 4569 |                 mHwPaused = true; | 
 | 4570 |             } | 
 | 4571 |             tracksToRemove->add(track); | 
 | 4572 |         } else if (track->isFlushPending()) { | 
 | 4573 |             track->flushAck(); | 
 | 4574 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4575 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4576 |             } | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4577 |         } else if (track->isResumePending()) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4578 |             track->resumeAck(); | 
| Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4579 |             if (last && mHwPaused) { | 
 | 4580 |                 doHwResume = true; | 
 | 4581 |                 mHwPaused = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4582 |             } | 
 | 4583 |         } | 
 | 4584 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4585 |         // The first time a track is added we wait | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4586 |         // for all its buffers to be filled before processing it. | 
 | 4587 |         // Allow draining the buffer in case the client | 
 | 4588 |         // app does not call stop() and relies on underrun to stop: | 
 | 4589 |         // hence the test on (track->mRetryCount > 1). | 
 | 4590 |         // If retryCount<=1 then track is about to underrun and be removed. | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4591 |         // Do not use a high threshold for compressed audio. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4592 |         uint32_t minFrames; | 
| Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4593 |         if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4594 |             && (track->mRetryCount > 1) && audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4595 |             minFrames = mNormalFrameCount; | 
 | 4596 |         } else { | 
 | 4597 |             minFrames = 1; | 
 | 4598 |         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4599 |  | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4600 |         if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && | 
 | 4601 |                 !track->isStopping_2() && !track->isStopped()) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4602 |         { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4603 |             ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4604 |  | 
 | 4605 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 4606 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4607 |                 // make sure processVolume_l() will apply new volume even if 0 | 
 | 4608 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4609 |                 if (!mHwSupportsPause) { | 
 | 4610 |                     track->resumeAck(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4611 |                 } | 
 | 4612 |             } | 
 | 4613 |  | 
 | 4614 |             // compute volume for this track | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4615 |             processVolume_l(track, last); | 
 | 4616 |             if (last) { | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4617 |                 sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 4618 |                 if (previousTrack != 0) { | 
 | 4619 |                     if (track != previousTrack.get()) { | 
 | 4620 |                         // Flush any data still being written from last track | 
 | 4621 |                         mBytesRemaining = 0; | 
| Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4622 |                         // Invalidate previous track to force a seek when resuming. | 
 | 4623 |                         previousTrack->invalidate(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4624 |                     } | 
 | 4625 |                 } | 
 | 4626 |                 mPreviousTrack = track; | 
 | 4627 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4628 |                 // reset retry count | 
 | 4629 |                 track->mRetryCount = kMaxTrackRetriesDirect; | 
 | 4630 |                 mActiveTrack = t; | 
 | 4631 |                 mixerStatus = MIXER_TRACKS_READY; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4632 |                 if (mHwPaused) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4633 |                     doHwResume = true; | 
 | 4634 |                     mHwPaused = false; | 
 | 4635 |                 } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4636 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4637 |         } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4638 |             // clear effect chain input buffer if the last active track started underruns | 
 | 4639 |             // to avoid sending previous audio buffer again to effects | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4640 |             if (!mEffectChains.isEmpty() && last) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4641 |                 mEffectChains[0]->clearInputBuffer(); | 
 | 4642 |             } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4643 |             if (track->isStopping_1()) { | 
 | 4644 |                 track->mState = TrackBase::STOPPING_2; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4645 |                 if (last && mHwPaused) { | 
 | 4646 |                      doHwResume = true; | 
 | 4647 |                      mHwPaused = false; | 
 | 4648 |                  } | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4649 |             } | 
 | 4650 |             if ((track->sharedBuffer() != 0) || track->isStopped() || | 
 | 4651 |                     track->isStopping_2() || track->isPaused()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4652 |                 // We have consumed all the buffers of this track. | 
 | 4653 |                 // Remove it from the list of active tracks. | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4654 |                 size_t audioHALFrames; | 
 | 4655 |                 if (audio_is_linear_pcm(mFormat)) { | 
 | 4656 |                     audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 4657 |                 } else { | 
 | 4658 |                     audioHALFrames = 0; | 
 | 4659 |                 } | 
 | 4660 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4661 |                 size_t framesWritten = mBytesWritten / mFrameSize; | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4662 |                 if (mStandby || !last || | 
 | 4663 |                         track->presentationComplete(framesWritten, audioHALFrames)) { | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4664 |                     if (track->isStopping_2()) { | 
 | 4665 |                         track->mState = TrackBase::STOPPED; | 
 | 4666 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4667 |                     if (track->isStopped()) { | 
 | 4668 |                         track->reset(); | 
 | 4669 |                     } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4670 |                     tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4671 |                 } | 
 | 4672 |             } else { | 
 | 4673 |                 // No buffers for this track. Give it a few chances to | 
 | 4674 |                 // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4675 |                 // Only consider last track started for mixer state control | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4676 |                 if (--(track->mRetryCount) <= 0) { | 
 | 4677 |                     ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4678 |                     tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4679 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 4680 |                     // it will then automatically call start() when data is available | 
 | 4681 |                     android_atomic_or(CBLK_DISABLED, &cblk->mFlags); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4682 |                 } else if (last) { | 
| Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4683 |                     ALOGW("pause because of UNDERRUN, framesReady = %zu," | 
 | 4684 |                             "minFrames = %u, mFormat = %#x", | 
 | 4685 |                             track->framesReady(), minFrames, mFormat); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4686 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4687 |                     if (mHwSupportsPause && !mHwPaused && !mStandby) { | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4688 |                         doHwPause = true; | 
 | 4689 |                         mHwPaused = true; | 
 | 4690 |                     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4691 |                 } | 
 | 4692 |             } | 
 | 4693 |         } | 
 | 4694 |     } | 
 | 4695 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4696 |     // 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] | 4697 |     if (!mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4698 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 4699 |             if (mTracks[i]->isFlushPending()) { | 
 | 4700 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4701 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4702 |             } | 
 | 4703 |         } | 
 | 4704 |     } | 
 | 4705 |  | 
 | 4706 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 4707 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 4708 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 4709 |     // if resume is received before pause is executed. | 
 | 4710 |     if (mHwSupportsPause && !mStandby && | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4711 |             (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4712 |         mOutput->stream->pause(mOutput->stream); | 
 | 4713 |     } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4714 |     if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4715 |         flushHw_l(); | 
 | 4716 |     } | 
 | 4717 |     if (mHwSupportsPause && !mStandby && doHwResume) { | 
 | 4718 |         mOutput->stream->resume(mOutput->stream); | 
 | 4719 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4720 |     // remove all the tracks that need to be... | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4721 |     removeTracks_l(*tracksToRemove); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4722 |  | 
 | 4723 |     return mixerStatus; | 
 | 4724 | } | 
 | 4725 |  | 
 | 4726 | void AudioFlinger::DirectOutputThread::threadLoop_mix() | 
 | 4727 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4728 |     size_t frameCount = mFrameCount; | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4729 |     int8_t *curBuf = (int8_t *)mSinkBuffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4730 |     // output audio to hardware | 
 | 4731 |     while (frameCount) { | 
| Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4732 |         AudioBufferProvider::Buffer buffer; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4733 |         buffer.frameCount = frameCount; | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4734 |         status_t status = mActiveTrack->getNextBuffer(&buffer); | 
 | 4735 |         if (status != NO_ERROR || buffer.raw == NULL) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4736 |             memset(curBuf, 0, frameCount * mFrameSize); | 
 | 4737 |             break; | 
 | 4738 |         } | 
 | 4739 |         memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
 | 4740 |         frameCount -= buffer.frameCount; | 
 | 4741 |         curBuf += buffer.frameCount * mFrameSize; | 
 | 4742 |         mActiveTrack->releaseBuffer(&buffer); | 
 | 4743 |     } | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4744 |     mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4745 |     mSleepTimeUs = 0; | 
 | 4746 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4747 |     mActiveTrack.clear(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4748 | } | 
 | 4749 |  | 
 | 4750 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() | 
 | 4751 | { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4752 |     // do not write to HAL when paused | 
| Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4753 |     if (mHwPaused || (usesHwAvSync() && mStandby)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4754 |         mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4755 |         return; | 
 | 4756 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4757 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4758 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4759 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4760 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4761 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4762 |         } | 
 | 4763 |     } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { | 
| Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4764 |         memset(mSinkBuffer, 0, mFrameCount * mFrameSize); | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4765 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4766 |     } | 
 | 4767 | } | 
 | 4768 |  | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4769 | void AudioFlinger::DirectOutputThread::threadLoop_exit() | 
 | 4770 | { | 
 | 4771 |     { | 
 | 4772 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4773 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 4774 |             if (mTracks[i]->isFlushPending()) { | 
 | 4775 |                 mTracks[i]->flushAck(); | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4776 |                 mFlushPending = true; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4777 |             } | 
 | 4778 |         } | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4779 |         if (mFlushPending) { | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4780 |             flushHw_l(); | 
 | 4781 |         } | 
 | 4782 |     } | 
 | 4783 |     PlaybackThread::threadLoop_exit(); | 
 | 4784 | } | 
 | 4785 |  | 
 | 4786 | // must be called with thread mutex locked | 
 | 4787 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() | 
 | 4788 | { | 
 | 4789 |     bool trackPaused = false; | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4790 |     bool trackStopped = false; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4791 |  | 
 | 4792 |     // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack | 
 | 4793 |     // after a timeout and we will enter standby then. | 
 | 4794 |     if (mTracks.size() > 0) { | 
 | 4795 |         trackPaused = mTracks[mTracks.size() - 1]->isPaused(); | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4796 |         trackStopped = mTracks[mTracks.size() - 1]->isStopped() || | 
 | 4797 |                            mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4798 |     } | 
 | 4799 |  | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4800 |     return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4801 | } | 
 | 4802 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4803 | // getTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4804 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, | 
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4805 |         audio_format_t format __unused, int sessionId __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4806 | { | 
 | 4807 |     return 0; | 
 | 4808 | } | 
 | 4809 |  | 
 | 4810 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4811 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4812 | { | 
 | 4813 | } | 
 | 4814 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4815 | // checkForNewParameter_l() must be called with ThreadBase::mLock held | 
 | 4816 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 4817 |                                                               status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4818 | { | 
 | 4819 |     bool reconfig = false; | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4820 |     bool a2dpDeviceChanged = false; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4821 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4822 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4823 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4824 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 4825 |     int value; | 
 | 4826 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 4827 |         // forward device change to effects that have requested to be | 
 | 4828 |         // aware of attached audio device. | 
 | 4829 |         if (value != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4830 |             a2dpDeviceChanged = | 
 | 4831 |                     (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4832 |             mOutDevice = value; | 
 | 4833 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4834 |                 mEffectChains[i]->setDevice_l(mOutDevice); | 
| Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4835 |             } | 
 | 4836 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4837 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4838 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 4839 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 4840 |         // size depends on frame count and correct behavior would not be garantied | 
 | 4841 |         // if frame count is changed after track creation | 
 | 4842 |         if (!mTracks.isEmpty()) { | 
 | 4843 |             status = INVALID_OPERATION; | 
 | 4844 |         } else { | 
 | 4845 |             reconfig = true; | 
 | 4846 |         } | 
 | 4847 |     } | 
 | 4848 |     if (status == NO_ERROR) { | 
 | 4849 |         status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 4850 |                                                 keyValuePair.string()); | 
 | 4851 |         if (!mStandby && status == INVALID_OPERATION) { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4852 |             mOutput->standby(); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4853 |             mStandby = true; | 
 | 4854 |             mBytesWritten = 0; | 
 | 4855 |             status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 4856 |                                                    keyValuePair.string()); | 
 | 4857 |         } | 
 | 4858 |         if (status == NO_ERROR && reconfig) { | 
 | 4859 |             readOutputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4860 |             sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4861 |         } | 
 | 4862 |     } | 
 | 4863 |  | 
| Eric Laurent | 113efbb | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4864 |     return reconfig || a2dpDeviceChanged; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4865 | } | 
 | 4866 |  | 
 | 4867 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const | 
 | 4868 | { | 
 | 4869 |     uint32_t time; | 
 | 4870 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 4871 |         time = PlaybackThread::activeSleepTimeUs(); | 
 | 4872 |     } else { | 
 | 4873 |         time = 10000; | 
 | 4874 |     } | 
 | 4875 |     return time; | 
 | 4876 | } | 
 | 4877 |  | 
 | 4878 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const | 
 | 4879 | { | 
 | 4880 |     uint32_t time; | 
 | 4881 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 4882 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 4883 |     } else { | 
 | 4884 |         time = 10000; | 
 | 4885 |     } | 
 | 4886 |     return time; | 
 | 4887 | } | 
 | 4888 |  | 
 | 4889 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const | 
 | 4890 | { | 
 | 4891 |     uint32_t time; | 
 | 4892 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 4893 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
 | 4894 |     } else { | 
 | 4895 |         time = 10000; | 
 | 4896 |     } | 
 | 4897 |     return time; | 
 | 4898 | } | 
 | 4899 |  | 
 | 4900 | void AudioFlinger::DirectOutputThread::cacheParameters_l() | 
 | 4901 | { | 
 | 4902 |     PlaybackThread::cacheParameters_l(); | 
 | 4903 |  | 
 | 4904 |     // use shorter standby delay as on normal output to release | 
 | 4905 |     // hardware resources as soon as possible | 
| Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4906 |     // no delay on outputs with HW A/V sync | 
 | 4907 |     if (usesHwAvSync()) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4908 |         mStandbyDelayNs = 0; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4909 |     } else if ((mType == OFFLOAD) && !audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4910 |         mStandbyDelayNs = kOffloadStandbyDelayNs; | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4911 |     } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4912 |         mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4913 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4914 | } | 
 | 4915 |  | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4916 | void AudioFlinger::DirectOutputThread::flushHw_l() | 
 | 4917 | { | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4918 |     mOutput->flush(); | 
| Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4919 |     mHwPaused = false; | 
| Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4920 |     mFlushPending = false; | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4921 | } | 
 | 4922 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4923 | // ---------------------------------------------------------------------------- | 
 | 4924 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4925 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4926 |         const wp<AudioFlinger::PlaybackThread>& playbackThread) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4927 |     :   Thread(false /*canCallJava*/), | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4928 |         mPlaybackThread(playbackThread), | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4929 |         mWriteAckSequence(0), | 
 | 4930 |         mDrainSequence(0) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4931 | { | 
 | 4932 | } | 
 | 4933 |  | 
 | 4934 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() | 
 | 4935 | { | 
 | 4936 | } | 
 | 4937 |  | 
 | 4938 | void AudioFlinger::AsyncCallbackThread::onFirstRef() | 
 | 4939 | { | 
 | 4940 |     run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 4941 | } | 
 | 4942 |  | 
 | 4943 | bool AudioFlinger::AsyncCallbackThread::threadLoop() | 
 | 4944 | { | 
 | 4945 |     while (!exitPending()) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4946 |         uint32_t writeAckSequence; | 
 | 4947 |         uint32_t drainSequence; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4948 |  | 
 | 4949 |         { | 
 | 4950 |             Mutex::Autolock _l(mLock); | 
| Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 4951 |             while (!((mWriteAckSequence & 1) || | 
 | 4952 |                      (mDrainSequence & 1) || | 
 | 4953 |                      exitPending())) { | 
 | 4954 |                 mWaitWorkCV.wait(mLock); | 
 | 4955 |             } | 
 | 4956 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4957 |             if (exitPending()) { | 
 | 4958 |                 break; | 
 | 4959 |             } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4960 |             ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", | 
 | 4961 |                   mWriteAckSequence, mDrainSequence); | 
 | 4962 |             writeAckSequence = mWriteAckSequence; | 
 | 4963 |             mWriteAckSequence &= ~1; | 
 | 4964 |             drainSequence = mDrainSequence; | 
 | 4965 |             mDrainSequence &= ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4966 |         } | 
 | 4967 |         { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4968 |             sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); | 
 | 4969 |             if (playbackThread != 0) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4970 |                 if (writeAckSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4971 |                     playbackThread->resetWriteBlocked(writeAckSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4972 |                 } | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4973 |                 if (drainSequence & 1) { | 
| Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4974 |                     playbackThread->resetDraining(drainSequence >> 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4975 |                 } | 
 | 4976 |             } | 
 | 4977 |         } | 
 | 4978 |     } | 
 | 4979 |     return false; | 
 | 4980 | } | 
 | 4981 |  | 
 | 4982 | void AudioFlinger::AsyncCallbackThread::exit() | 
 | 4983 | { | 
 | 4984 |     ALOGV("AsyncCallbackThread::exit"); | 
 | 4985 |     Mutex::Autolock _l(mLock); | 
 | 4986 |     requestExit(); | 
 | 4987 |     mWaitWorkCV.broadcast(); | 
 | 4988 | } | 
 | 4989 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4990 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4991 | { | 
 | 4992 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4993 |     // bit 0 is cleared | 
 | 4994 |     mWriteAckSequence = sequence << 1; | 
 | 4995 | } | 
 | 4996 |  | 
 | 4997 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() | 
 | 4998 | { | 
 | 4999 |     Mutex::Autolock _l(mLock); | 
 | 5000 |     // ignore unexpected callbacks | 
 | 5001 |     if (mWriteAckSequence & 2) { | 
 | 5002 |         mWriteAckSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5003 |         mWaitWorkCV.signal(); | 
 | 5004 |     } | 
 | 5005 | } | 
 | 5006 |  | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5007 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5008 | { | 
 | 5009 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5010 |     // bit 0 is cleared | 
 | 5011 |     mDrainSequence = sequence << 1; | 
 | 5012 | } | 
 | 5013 |  | 
 | 5014 | void AudioFlinger::AsyncCallbackThread::resetDraining() | 
 | 5015 | { | 
 | 5016 |     Mutex::Autolock _l(mLock); | 
 | 5017 |     // ignore unexpected callbacks | 
 | 5018 |     if (mDrainSequence & 2) { | 
 | 5019 |         mDrainSequence |= 1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5020 |         mWaitWorkCV.signal(); | 
 | 5021 |     } | 
 | 5022 | } | 
 | 5023 |  | 
 | 5024 |  | 
 | 5025 | // ---------------------------------------------------------------------------- | 
 | 5026 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5027 |         AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) | 
 | 5028 |     :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5029 |         mPausedBytesRemaining(0) | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5030 | { | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5031 |     //FIXME: mStandby should be set to true by ThreadBase constructor | 
 | 5032 |     mStandby = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5033 | } | 
 | 5034 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5035 | void AudioFlinger::OffloadThread::threadLoop_exit() | 
 | 5036 | { | 
 | 5037 |     if (mFlushPending || mHwPaused) { | 
 | 5038 |         // If a flush is pending or track was paused, just discard buffered data | 
 | 5039 |         flushHw_l(); | 
 | 5040 |     } else { | 
 | 5041 |         mMixerStatus = MIXER_DRAIN_ALL; | 
 | 5042 |         threadLoop_drain(); | 
 | 5043 |     } | 
| Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5044 |     if (mUseAsyncWrite) { | 
 | 5045 |         ALOG_ASSERT(mCallbackThread != 0); | 
 | 5046 |         mCallbackThread->exit(); | 
 | 5047 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5048 |     PlaybackThread::threadLoop_exit(); | 
 | 5049 | } | 
 | 5050 |  | 
 | 5051 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( | 
 | 5052 |     Vector< sp<Track> > *tracksToRemove | 
 | 5053 | ) | 
 | 5054 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5055 |     size_t count = mActiveTracks.size(); | 
 | 5056 |  | 
 | 5057 |     mixer_state mixerStatus = MIXER_IDLE; | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5058 |     bool doHwPause = false; | 
 | 5059 |     bool doHwResume = false; | 
 | 5060 |  | 
| Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5061 |     ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); | 
 | 5062 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5063 |     // find out which tracks need to be processed | 
 | 5064 |     for (size_t i = 0; i < count; i++) { | 
 | 5065 |         sp<Track> t = mActiveTracks[i].promote(); | 
 | 5066 |         // The track died recently | 
 | 5067 |         if (t == 0) { | 
 | 5068 |             continue; | 
 | 5069 |         } | 
 | 5070 |         Track* const track = t.get(); | 
 | 5071 |         audio_track_cblk_t* cblk = track->cblk(); | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5072 |         // Only consider last track started for volume and mixer state control. | 
 | 5073 |         // In theory an older track could underrun and restart after the new one starts | 
 | 5074 |         // but as we only care about the transition phase between two tracks on a | 
 | 5075 |         // direct output, it is not a problem to ignore the underrun case. | 
 | 5076 |         sp<Track> l = mLatestActiveTrack.promote(); | 
 | 5077 |         bool last = l.get() == track; | 
 | 5078 |  | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5079 |         if (track->isInvalid()) { | 
 | 5080 |             ALOGW("An invalidated track shouldn't be in active list"); | 
 | 5081 |             tracksToRemove->add(track); | 
 | 5082 |             continue; | 
 | 5083 |         } | 
 | 5084 |  | 
 | 5085 |         if (track->mState == TrackBase::IDLE) { | 
 | 5086 |             ALOGW("An idle track shouldn't be in active list"); | 
 | 5087 |             continue; | 
 | 5088 |         } | 
 | 5089 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5090 |         if (track->isPausing()) { | 
 | 5091 |             track->setPaused(); | 
 | 5092 |             if (last) { | 
| Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5093 |                 if (mHwSupportsPause && !mHwPaused) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5094 |                     doHwPause = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5095 |                     mHwPaused = true; | 
 | 5096 |                 } | 
 | 5097 |                 // If we were part way through writing the mixbuffer to | 
 | 5098 |                 // the HAL we must save this until we resume | 
 | 5099 |                 // BUG - this will be wrong if a different track is made active, | 
 | 5100 |                 // in that case we want to discard the pending data in the | 
 | 5101 |                 // mixbuffer and tell the client to present it again when the | 
 | 5102 |                 // track is resumed | 
 | 5103 |                 mPausedWriteLength = mCurrentWriteLength; | 
 | 5104 |                 mPausedBytesRemaining = mBytesRemaining; | 
 | 5105 |                 mBytesRemaining = 0;    // stop writing | 
 | 5106 |             } | 
 | 5107 |             tracksToRemove->add(track); | 
| Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5108 |         } else if (track->isFlushPending()) { | 
 | 5109 |             track->flushAck(); | 
 | 5110 |             if (last) { | 
 | 5111 |                 mFlushPending = true; | 
 | 5112 |             } | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5113 |         } else if (track->isResumePending()){ | 
 | 5114 |             track->resumeAck(); | 
 | 5115 |             if (last) { | 
 | 5116 |                 if (mPausedBytesRemaining) { | 
 | 5117 |                     // Need to continue write that was interrupted | 
 | 5118 |                     mCurrentWriteLength = mPausedWriteLength; | 
 | 5119 |                     mBytesRemaining = mPausedBytesRemaining; | 
 | 5120 |                     mPausedBytesRemaining = 0; | 
 | 5121 |                 } | 
 | 5122 |                 if (mHwPaused) { | 
 | 5123 |                     doHwResume = true; | 
 | 5124 |                     mHwPaused = false; | 
 | 5125 |                     // threadLoop_mix() will handle the case that we need to | 
 | 5126 |                     // resume an interrupted write | 
 | 5127 |                 } | 
 | 5128 |                 // enable write to audio HAL | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5129 |                 mSleepTimeUs = 0; | 
| Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5130 |  | 
 | 5131 |                 // Do not handle new data in this iteration even if track->framesReady() | 
 | 5132 |                 mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 5133 |             } | 
 | 5134 |         }  else if (track->framesReady() && track->isReady() && | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5135 |                 !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5136 |             ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5137 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 5138 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
| Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5139 |                 // make sure processVolume_l() will apply new volume even if 0 | 
 | 5140 |                 mLeftVolFloat = mRightVolFloat = -1.0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5141 |             } | 
 | 5142 |  | 
 | 5143 |             if (last) { | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5144 |                 sp<Track> previousTrack = mPreviousTrack.promote(); | 
 | 5145 |                 if (previousTrack != 0) { | 
 | 5146 |                     if (track != previousTrack.get()) { | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5147 |                         // Flush any data still being written from last track | 
 | 5148 |                         mBytesRemaining = 0; | 
 | 5149 |                         if (mPausedBytesRemaining) { | 
 | 5150 |                             // Last track was paused so we also need to flush saved | 
 | 5151 |                             // mixbuffer state and invalidate track so that it will | 
 | 5152 |                             // re-submit that unwritten data when it is next resumed | 
 | 5153 |                             mPausedBytesRemaining = 0; | 
 | 5154 |                             // Invalidate is a bit drastic - would be more efficient | 
 | 5155 |                             // to have a flag to tell client that some of the | 
 | 5156 |                             // previously written data was lost | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5157 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5158 |                         } | 
 | 5159 |                         // flush data already sent to the DSP if changing audio session as audio | 
 | 5160 |                         // comes from a different source. Also invalidate previous track to force a | 
 | 5161 |                         // seek when resuming. | 
| Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5162 |                         if (previousTrack->sessionId() != track->sessionId()) { | 
 | 5163 |                             previousTrack->invalidate(); | 
| Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5164 |                         } | 
 | 5165 |                     } | 
 | 5166 |                 } | 
 | 5167 |                 mPreviousTrack = track; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5168 |                 // reset retry count | 
 | 5169 |                 track->mRetryCount = kMaxTrackRetriesOffload; | 
 | 5170 |                 mActiveTrack = t; | 
 | 5171 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 5172 |             } | 
 | 5173 |         } else { | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5174 |             ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5175 |             if (track->isStopping_1()) { | 
 | 5176 |                 // Hardware buffer can hold a large amount of audio so we must | 
 | 5177 |                 // wait for all current track's data to drain before we say | 
 | 5178 |                 // that the track is stopped. | 
 | 5179 |                 if (mBytesRemaining == 0) { | 
 | 5180 |                     // Only start draining when all data in mixbuffer | 
 | 5181 |                     // has been written | 
 | 5182 |                     ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); | 
 | 5183 |                     track->mState = TrackBase::STOPPING_2; // so presentation completes after drain | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5184 |                     // do not drain if no data was ever sent to HAL (mStandby == true) | 
 | 5185 |                     if (last && !mStandby) { | 
| Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5186 |                         // do not modify drain sequence if we are already draining. This happens | 
 | 5187 |                         // when resuming from pause after drain. | 
 | 5188 |                         if ((mDrainSequence & 1) == 0) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5189 |                             mSleepTimeUs = 0; | 
 | 5190 |                             mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5191 |                             mixerStatus = MIXER_DRAIN_TRACK; | 
 | 5192 |                             mDrainSequence += 2; | 
 | 5193 |                         } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5194 |                         if (mHwPaused) { | 
 | 5195 |                             // It is possible to move from PAUSED to STOPPING_1 without | 
 | 5196 |                             // a resume so we must ensure hardware is running | 
| Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5197 |                             doHwResume = true; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5198 |                             mHwPaused = false; | 
 | 5199 |                         } | 
 | 5200 |                     } | 
 | 5201 |                 } | 
 | 5202 |             } else if (track->isStopping_2()) { | 
| Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5203 |                 // Drain has completed or we are in standby, signal presentation complete | 
 | 5204 |                 if (!(mDrainSequence & 1) || !last || mStandby) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5205 |                     track->mState = TrackBase::STOPPED; | 
 | 5206 |                     size_t audioHALFrames = | 
 | 5207 |                             (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
 | 5208 |                     size_t framesWritten = | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5209 |                             mBytesWritten / mOutput->getFrameSize(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5210 |                     track->presentationComplete(framesWritten, audioHALFrames); | 
 | 5211 |                     track->reset(); | 
 | 5212 |                     tracksToRemove->add(track); | 
 | 5213 |                 } | 
 | 5214 |             } else { | 
 | 5215 |                 // No buffers for this track. Give it a few chances to | 
 | 5216 |                 // fill a buffer, then remove it from active list. | 
 | 5217 |                 if (--(track->mRetryCount) <= 0) { | 
 | 5218 |                     ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", | 
 | 5219 |                           track->name()); | 
 | 5220 |                     tracksToRemove->add(track); | 
| Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5221 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 5222 |                     // it will then automatically call start() when data is available | 
 | 5223 |                     android_atomic_or(CBLK_DISABLED, &cblk->mFlags); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5224 |                 } else if (last){ | 
 | 5225 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 5226 |                 } | 
 | 5227 |             } | 
 | 5228 |         } | 
 | 5229 |         // compute volume for this track | 
 | 5230 |         processVolume_l(track, last); | 
 | 5231 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5232 |  | 
| Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5233 |     // make sure the pause/flush/resume sequence is executed in the right order. | 
 | 5234 |     // If a flush is pending and a track is active but the HW is not paused, force a HW pause | 
 | 5235 |     // before flush and then resume HW. This can happen in case of pause/flush/resume | 
 | 5236 |     // if resume is received before pause is executed. | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5237 |     if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5238 |         mOutput->stream->pause(mOutput->stream); | 
 | 5239 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5240 |     if (mFlushPending) { | 
 | 5241 |         flushHw_l(); | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5242 |     } | 
| Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5243 |     if (!mStandby && doHwResume) { | 
| Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5244 |         mOutput->stream->resume(mOutput->stream); | 
 | 5245 |     } | 
| Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5246 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5247 |     // remove all the tracks that need to be... | 
 | 5248 |     removeTracks_l(*tracksToRemove); | 
 | 5249 |  | 
 | 5250 |     return mixerStatus; | 
 | 5251 | } | 
 | 5252 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5253 | // must be called with thread mutex locked | 
 | 5254 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() | 
 | 5255 | { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5256 |     ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", | 
 | 5257 |           mWriteAckSequence, mDrainSequence); | 
 | 5258 |     if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5259 |         return true; | 
 | 5260 |     } | 
 | 5261 |     return false; | 
 | 5262 | } | 
 | 5263 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5264 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() | 
 | 5265 | { | 
 | 5266 |     Mutex::Autolock _l(mLock); | 
 | 5267 |     return waitingAsyncCallback_l(); | 
 | 5268 | } | 
 | 5269 |  | 
 | 5270 | void AudioFlinger::OffloadThread::flushHw_l() | 
 | 5271 | { | 
| Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5272 |     DirectOutputThread::flushHw_l(); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5273 |     // Flush anything still waiting in the mixbuffer | 
 | 5274 |     mCurrentWriteLength = 0; | 
 | 5275 |     mBytesRemaining = 0; | 
 | 5276 |     mPausedWriteLength = 0; | 
 | 5277 |     mPausedBytesRemaining = 0; | 
| Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5278 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5279 |     if (mUseAsyncWrite) { | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5280 |         // discard any pending drain or write ack by incrementing sequence | 
 | 5281 |         mWriteAckSequence = (mWriteAckSequence + 2) & ~1; | 
 | 5282 |         mDrainSequence = (mDrainSequence + 2) & ~1; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5283 |         ALOG_ASSERT(mCallbackThread != 0); | 
| Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5284 |         mCallbackThread->setWriteBlocked(mWriteAckSequence); | 
 | 5285 |         mCallbackThread->setDraining(mDrainSequence); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5286 |     } | 
 | 5287 | } | 
 | 5288 |  | 
 | 5289 | // ---------------------------------------------------------------------------- | 
 | 5290 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5291 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5292 |         AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5293 |     :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5294 |                     systemReady, DUPLICATING), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5295 |         mWaitTimeMs(UINT_MAX) | 
 | 5296 | { | 
 | 5297 |     addOutputTrack(mainThread); | 
 | 5298 | } | 
 | 5299 |  | 
 | 5300 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
 | 5301 | { | 
 | 5302 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5303 |         mOutputTracks[i]->destroy(); | 
 | 5304 |     } | 
 | 5305 | } | 
 | 5306 |  | 
 | 5307 | void AudioFlinger::DuplicatingThread::threadLoop_mix() | 
 | 5308 | { | 
 | 5309 |     // mix buffers... | 
 | 5310 |     if (outputsReady(outputTracks)) { | 
 | 5311 |         mAudioMixer->process(AudioBufferProvider::kInvalidPTS); | 
 | 5312 |     } else { | 
| Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5313 |         if (mMixerBufferValid) { | 
 | 5314 |             memset(mMixerBuffer, 0, mMixerBufferSize); | 
 | 5315 |         } else { | 
 | 5316 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
 | 5317 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5318 |     } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5319 |     mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5320 |     writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5321 |     mCurrentWriteLength = mSinkBufferSize; | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5322 |     mStandbyTimeNs = systemTime() + mStandbyDelayNs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5323 | } | 
 | 5324 |  | 
 | 5325 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() | 
 | 5326 | { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5327 |     if (mSleepTimeUs == 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5328 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5329 |             mSleepTimeUs = mActiveSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5330 |         } else { | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5331 |             mSleepTimeUs = mIdleSleepTimeUs; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5332 |         } | 
 | 5333 |     } else if (mBytesWritten != 0) { | 
 | 5334 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 5335 |             writeFrames = mNormalFrameCount; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5336 |             memset(mSinkBuffer, 0, mSinkBufferSize); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5337 |         } else { | 
 | 5338 |             // flush remaining overflow buffers in output tracks | 
 | 5339 |             writeFrames = 0; | 
 | 5340 |         } | 
| Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5341 |         mSleepTimeUs = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5342 |     } | 
 | 5343 | } | 
 | 5344 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5345 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5346 | { | 
 | 5347 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5348 |         outputTracks[i]->write(mSinkBuffer, writeFrames); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5349 |     } | 
| Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5350 |     mStandby = false; | 
| Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5351 |     return (ssize_t)mSinkBufferSize; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5352 | } | 
 | 5353 |  | 
 | 5354 | void AudioFlinger::DuplicatingThread::threadLoop_standby() | 
 | 5355 | { | 
 | 5356 |     // DuplicatingThread implements standby by stopping all tracks | 
 | 5357 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 5358 |         outputTracks[i]->stop(); | 
 | 5359 |     } | 
 | 5360 | } | 
 | 5361 |  | 
 | 5362 | void AudioFlinger::DuplicatingThread::saveOutputTracks() | 
 | 5363 | { | 
 | 5364 |     outputTracks = mOutputTracks; | 
 | 5365 | } | 
 | 5366 |  | 
 | 5367 | void AudioFlinger::DuplicatingThread::clearOutputTracks() | 
 | 5368 | { | 
 | 5369 |     outputTracks.clear(); | 
 | 5370 | } | 
 | 5371 |  | 
 | 5372 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
 | 5373 | { | 
 | 5374 |     Mutex::Autolock _l(mLock); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5375 |     // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. | 
 | 5376 |     // Adjust for thread->sampleRate() to determine minimum buffer frame count. | 
 | 5377 |     // Then triple buffer because Threads do not run synchronously and may not be clock locked. | 
 | 5378 |     const size_t frameCount = | 
 | 5379 |             3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); | 
 | 5380 |     // TODO: Consider asynchronous sample rate conversion to handle clock disparity | 
 | 5381 |     // from different OutputTracks and their associated MixerThreads (e.g. one may | 
 | 5382 |     // nearly empty and the other may be dropping data). | 
 | 5383 |  | 
 | 5384 |     sp<OutputTrack> outputTrack = new OutputTrack(thread, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5385 |                                             this, | 
 | 5386 |                                             mSampleRate, | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5387 |                                             mFormat, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5388 |                                             mChannelMask, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5389 |                                             frameCount, | 
 | 5390 |                                             IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5391 |     if (outputTrack->cblk() != NULL) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5392 |         thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5393 |         mOutputTracks.add(outputTrack); | 
| Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5394 |         ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5395 |         updateWaitTime_l(); | 
 | 5396 |     } | 
 | 5397 | } | 
 | 5398 |  | 
 | 5399 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
 | 5400 | { | 
 | 5401 |     Mutex::Autolock _l(mLock); | 
 | 5402 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5403 |         if (mOutputTracks[i]->thread() == thread) { | 
 | 5404 |             mOutputTracks[i]->destroy(); | 
 | 5405 |             mOutputTracks.removeAt(i); | 
 | 5406 |             updateWaitTime_l(); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5407 |             if (thread->getOutput() == mOutput) { | 
 | 5408 |                 mOutput = NULL; | 
 | 5409 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5410 |             return; | 
 | 5411 |         } | 
 | 5412 |     } | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5413 |     ALOGV("removeOutputTrack(): unknown thread: %p", thread); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5414 | } | 
 | 5415 |  | 
 | 5416 | // caller must hold mLock | 
 | 5417 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() | 
 | 5418 | { | 
 | 5419 |     mWaitTimeMs = UINT_MAX; | 
 | 5420 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 5421 |         sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
 | 5422 |         if (strong != 0) { | 
 | 5423 |             uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
 | 5424 |             if (waitTimeMs < mWaitTimeMs) { | 
 | 5425 |                 mWaitTimeMs = waitTimeMs; | 
 | 5426 |             } | 
 | 5427 |         } | 
 | 5428 |     } | 
 | 5429 | } | 
 | 5430 |  | 
 | 5431 |  | 
 | 5432 | bool AudioFlinger::DuplicatingThread::outputsReady( | 
 | 5433 |         const SortedVector< sp<OutputTrack> > &outputTracks) | 
 | 5434 | { | 
 | 5435 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 5436 |         sp<ThreadBase> thread = outputTracks[i]->thread().promote(); | 
 | 5437 |         if (thread == 0) { | 
 | 5438 |             ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
 | 5439 |                     outputTracks[i].get()); | 
 | 5440 |             return false; | 
 | 5441 |         } | 
 | 5442 |         PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
 | 5443 |         // see note at standby() declaration | 
 | 5444 |         if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
 | 5445 |             ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
 | 5446 |                     thread.get()); | 
 | 5447 |             return false; | 
 | 5448 |         } | 
 | 5449 |     } | 
 | 5450 |     return true; | 
 | 5451 | } | 
 | 5452 |  | 
 | 5453 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const | 
 | 5454 | { | 
 | 5455 |     return (mWaitTimeMs * 1000) / 2; | 
 | 5456 | } | 
 | 5457 |  | 
 | 5458 | void AudioFlinger::DuplicatingThread::cacheParameters_l() | 
 | 5459 | { | 
 | 5460 |     // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
 | 5461 |     updateWaitTime_l(); | 
 | 5462 |  | 
 | 5463 |     MixerThread::cacheParameters_l(); | 
 | 5464 | } | 
 | 5465 |  | 
 | 5466 | // ---------------------------------------------------------------------------- | 
 | 5467 | //      Record | 
 | 5468 | // ---------------------------------------------------------------------------- | 
 | 5469 |  | 
 | 5470 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
 | 5471 |                                          AudioStreamIn *input, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5472 |                                          audio_io_handle_t id, | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5473 |                                          audio_devices_t outDevice, | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5474 |                                          audio_devices_t inDevice, | 
 | 5475 |                                          bool systemReady | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5476 | #ifdef TEE_SINK | 
 | 5477 |                                          , const sp<NBAIO_Sink>& teeSink | 
 | 5478 | #endif | 
 | 5479 |                                          ) : | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5480 |     ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5481 |     mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5482 |     // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5483 |     mRsmpInRear(0) | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5484 | #ifdef TEE_SINK | 
 | 5485 |     , mTeeSink(teeSink) | 
 | 5486 | #endif | 
| Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5487 |     , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, | 
 | 5488 |             "RecordThreadRO", MemoryHeapBase::READ_ONLY)) | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5489 |     // mFastCapture below | 
 | 5490 |     , mFastCaptureFutex(0) | 
 | 5491 |     // mInputSource | 
 | 5492 |     // mPipeSink | 
 | 5493 |     // mPipeSource | 
 | 5494 |     , mPipeFramesP2(0) | 
 | 5495 |     // mPipeMemory | 
 | 5496 |     // mFastCaptureNBLogWriter | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5497 |     , mFastTrackAvail(false) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5498 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5499 |     snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); | 
 | 5500 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5501 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5502 |     readInputParameters_l(); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5503 |  | 
 | 5504 |     // create an NBAIO source for the HAL input stream, and negotiate | 
 | 5505 |     mInputSource = new AudioStreamInSource(input->stream); | 
 | 5506 |     size_t numCounterOffers = 0; | 
 | 5507 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; | 
 | 5508 |     ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5509 |     ALOG_ASSERT(index == 0); | 
 | 5510 |  | 
 | 5511 |     // initialize fast capture depending on configuration | 
 | 5512 |     bool initFastCapture; | 
 | 5513 |     switch (kUseFastCapture) { | 
 | 5514 |     case FastCapture_Never: | 
 | 5515 |         initFastCapture = false; | 
 | 5516 |         break; | 
 | 5517 |     case FastCapture_Always: | 
 | 5518 |         initFastCapture = true; | 
 | 5519 |         break; | 
 | 5520 |     case FastCapture_Static: | 
| Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5521 |         initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5522 |         break; | 
 | 5523 |     // case FastCapture_Dynamic: | 
 | 5524 |     } | 
 | 5525 |  | 
 | 5526 |     if (initFastCapture) { | 
| Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5527 |         // 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] | 5528 |         NBAIO_Format format = mInputSource->format(); | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5529 |         size_t pipeFramesP2 = roundup(mSampleRate / 25);    // double-buffering of 20 ms each | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5530 |         size_t pipeSize = pipeFramesP2 * Format_frameSize(format); | 
 | 5531 |         void *pipeBuffer; | 
 | 5532 |         const sp<MemoryDealer> roHeap(readOnlyHeap()); | 
 | 5533 |         sp<IMemory> pipeMemory; | 
 | 5534 |         if ((roHeap == 0) || | 
 | 5535 |                 (pipeMemory = roHeap->allocate(pipeSize)) == 0 || | 
 | 5536 |                 (pipeBuffer = pipeMemory->pointer()) == NULL) { | 
 | 5537 |             ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); | 
 | 5538 |             goto failed; | 
 | 5539 |         } | 
 | 5540 |         // pipe will be shared directly with fast clients, so clear to avoid leaking old information | 
 | 5541 |         memset(pipeBuffer, 0, pipeSize); | 
 | 5542 |         Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); | 
 | 5543 |         const NBAIO_Format offers[1] = {format}; | 
 | 5544 |         size_t numCounterOffers = 0; | 
 | 5545 |         ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5546 |         ALOG_ASSERT(index == 0); | 
 | 5547 |         mPipeSink = pipe; | 
 | 5548 |         PipeReader *pipeReader = new PipeReader(*pipe); | 
 | 5549 |         numCounterOffers = 0; | 
 | 5550 |         index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 5551 |         ALOG_ASSERT(index == 0); | 
 | 5552 |         mPipeSource = pipeReader; | 
 | 5553 |         mPipeFramesP2 = pipeFramesP2; | 
 | 5554 |         mPipeMemory = pipeMemory; | 
 | 5555 |  | 
 | 5556 |         // create fast capture | 
 | 5557 |         mFastCapture = new FastCapture(); | 
 | 5558 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5559 | #ifdef STATE_QUEUE_DUMP | 
 | 5560 |         // FIXME | 
 | 5561 | #endif | 
 | 5562 |         FastCaptureState *state = sq->begin(); | 
 | 5563 |         state->mCblk = NULL; | 
 | 5564 |         state->mInputSource = mInputSource.get(); | 
 | 5565 |         state->mInputSourceGen++; | 
 | 5566 |         state->mPipeSink = pipe; | 
 | 5567 |         state->mPipeSinkGen++; | 
 | 5568 |         state->mFrameCount = mFrameCount; | 
 | 5569 |         state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 5570 |         // already done in constructor initialization list | 
 | 5571 |         //mFastCaptureFutex = 0; | 
 | 5572 |         state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 5573 |         state->mColdGen++; | 
 | 5574 |         state->mDumpState = &mFastCaptureDumpState; | 
 | 5575 | #ifdef TEE_SINK | 
 | 5576 |         // FIXME | 
 | 5577 | #endif | 
 | 5578 |         mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); | 
 | 5579 |         state->mNBLogWriter = mFastCaptureNBLogWriter.get(); | 
 | 5580 |         sq->end(); | 
 | 5581 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5582 |  | 
 | 5583 |         // start the fast capture | 
 | 5584 |         mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 5585 |         pid_t tid = mFastCapture->getTid(); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5586 |         sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5587 | #ifdef AUDIO_WATCHDOG | 
 | 5588 |         // FIXME | 
 | 5589 | #endif | 
 | 5590 |  | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5591 |         mFastTrackAvail = true; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5592 |     } | 
 | 5593 | failed: ; | 
 | 5594 |  | 
 | 5595 |     // FIXME mNormalSource | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5596 | } | 
 | 5597 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5598 | AudioFlinger::RecordThread::~RecordThread() | 
 | 5599 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5600 |     if (mFastCapture != 0) { | 
 | 5601 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5602 |         FastCaptureState *state = sq->begin(); | 
 | 5603 |         if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 5604 |             int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 5605 |             if (old == -1) { | 
 | 5606 |                 (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 5607 |             } | 
 | 5608 |         } | 
 | 5609 |         state->mCommand = FastCaptureState::EXIT; | 
 | 5610 |         sq->end(); | 
 | 5611 |         sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 5612 |         mFastCapture->join(); | 
 | 5613 |         mFastCapture.clear(); | 
 | 5614 |     } | 
 | 5615 |     mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5616 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5617 |     free(mRsmpInBuffer); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5618 | } | 
 | 5619 |  | 
 | 5620 | void AudioFlinger::RecordThread::onFirstRef() | 
 | 5621 | { | 
| Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5622 |     run(mThreadName, PRIORITY_URGENT_AUDIO); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5623 | } | 
 | 5624 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5625 | bool AudioFlinger::RecordThread::threadLoop() | 
 | 5626 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5627 |     nsecs_t lastWarning = 0; | 
 | 5628 |  | 
 | 5629 |     inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5630 |  | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5631 | reacquire_wakelock: | 
 | 5632 |     sp<RecordTrack> activeTrack; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5633 |     int activeTracksGen; | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5634 |     { | 
 | 5635 |         Mutex::Autolock _l(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5636 |         size_t size = mActiveTracks.size(); | 
 | 5637 |         activeTracksGen = mActiveTracksGen; | 
 | 5638 |         if (size > 0) { | 
 | 5639 |             // FIXME an arbitrary choice | 
 | 5640 |             activeTrack = mActiveTracks[0]; | 
 | 5641 |             acquireWakeLock_l(activeTrack->uid()); | 
 | 5642 |             if (size > 1) { | 
 | 5643 |                 SortedVector<int> tmp; | 
 | 5644 |                 for (size_t i = 0; i < size; i++) { | 
 | 5645 |                     tmp.add(mActiveTracks[i]->uid()); | 
 | 5646 |                 } | 
 | 5647 |                 updateWakeLockUids_l(tmp); | 
 | 5648 |             } | 
 | 5649 |         } else { | 
 | 5650 |             acquireWakeLock_l(-1); | 
 | 5651 |         } | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5652 |     } | 
 | 5653 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5654 |     // used to request a deferred sleep, to be executed later while mutex is unlocked | 
 | 5655 |     uint32_t sleepUs = 0; | 
 | 5656 |  | 
 | 5657 |     // loop while there is work to do | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5658 |     for (;;) { | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5659 |         Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5660 |  | 
| Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5661 |         // sleep with mutex unlocked | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5662 |         if (sleepUs > 0) { | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5663 |             ATRACE_BEGIN("sleep"); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5664 |             usleep(sleepUs); | 
| Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5665 |             ATRACE_END(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5666 |             sleepUs = 0; | 
| Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5667 |         } | 
 | 5668 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5669 |         // activeTracks accumulates a copy of a subset of mActiveTracks | 
 | 5670 |         Vector< sp<RecordTrack> > activeTracks; | 
 | 5671 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5672 |         // reference to the (first and only) active fast track | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5673 |         sp<RecordTrack> fastTrack; | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5674 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5675 |         // reference to a fast track which is about to be removed | 
 | 5676 |         sp<RecordTrack> fastTrackToRemove; | 
 | 5677 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5678 |         { // scope for mLock | 
 | 5679 |             Mutex::Autolock _l(mLock); | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5680 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5681 |             processConfigEvents_l(); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5682 |  | 
| Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5683 |             // check exitPending here because checkForNewParameters_l() and | 
 | 5684 |             // checkForNewParameters_l() can temporarily release mLock | 
 | 5685 |             if (exitPending()) { | 
 | 5686 |                 break; | 
 | 5687 |             } | 
 | 5688 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5689 |             // if no active track(s), then standby and release wakelock | 
 | 5690 |             size_t size = mActiveTracks.size(); | 
 | 5691 |             if (size == 0) { | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5692 |                 standbyIfNotAlreadyInStandby(); | 
| Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5693 |                 // exitPending() can't become true here | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5694 |                 releaseWakeLock_l(); | 
 | 5695 |                 ALOGV("RecordThread: loop stopping"); | 
 | 5696 |                 // go to sleep | 
 | 5697 |                 mWaitWorkCV.wait(mLock); | 
 | 5698 |                 ALOGV("RecordThread: loop starting"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5699 |                 goto reacquire_wakelock; | 
 | 5700 |             } | 
 | 5701 |  | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5702 |             if (mActiveTracksGen != activeTracksGen) { | 
 | 5703 |                 activeTracksGen = mActiveTracksGen; | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5704 |                 SortedVector<int> tmp; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5705 |                 for (size_t i = 0; i < size; i++) { | 
 | 5706 |                     tmp.add(mActiveTracks[i]->uid()); | 
 | 5707 |                 } | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5708 |                 updateWakeLockUids_l(tmp); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5709 |             } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5710 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5711 |             bool doBroadcast = false; | 
 | 5712 |             for (size_t i = 0; i < size; ) { | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5713 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5714 |                 activeTrack = mActiveTracks[i]; | 
 | 5715 |                 if (activeTrack->isTerminated()) { | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5716 |                     if (activeTrack->isFastTrack()) { | 
 | 5717 |                         ALOG_ASSERT(fastTrackToRemove == 0); | 
 | 5718 |                         fastTrackToRemove = activeTrack; | 
 | 5719 |                     } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5720 |                     removeTrack_l(activeTrack); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5721 |                     mActiveTracks.remove(activeTrack); | 
 | 5722 |                     mActiveTracksGen++; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5723 |                     size--; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5724 |                     continue; | 
 | 5725 |                 } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5726 |  | 
 | 5727 |                 TrackBase::track_state activeTrackState = activeTrack->mState; | 
 | 5728 |                 switch (activeTrackState) { | 
 | 5729 |  | 
 | 5730 |                 case TrackBase::PAUSING: | 
 | 5731 |                     mActiveTracks.remove(activeTrack); | 
 | 5732 |                     mActiveTracksGen++; | 
 | 5733 |                     doBroadcast = true; | 
 | 5734 |                     size--; | 
 | 5735 |                     continue; | 
 | 5736 |  | 
 | 5737 |                 case TrackBase::STARTING_1: | 
 | 5738 |                     sleepUs = 10000; | 
 | 5739 |                     i++; | 
 | 5740 |                     continue; | 
 | 5741 |  | 
 | 5742 |                 case TrackBase::STARTING_2: | 
 | 5743 |                     doBroadcast = true; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5744 |                     mStandby = false; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5745 |                     activeTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5746 |                     break; | 
 | 5747 |  | 
 | 5748 |                 case TrackBase::ACTIVE: | 
 | 5749 |                     break; | 
 | 5750 |  | 
 | 5751 |                 case TrackBase::IDLE: | 
 | 5752 |                     i++; | 
 | 5753 |                     continue; | 
 | 5754 |  | 
 | 5755 |                 default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5756 |                     LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5757 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5758 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5759 |                 activeTracks.add(activeTrack); | 
 | 5760 |                 i++; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5761 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5762 |                 if (activeTrack->isFastTrack()) { | 
 | 5763 |                     ALOG_ASSERT(!mFastTrackAvail); | 
 | 5764 |                     ALOG_ASSERT(fastTrack == 0); | 
 | 5765 |                     fastTrack = activeTrack; | 
 | 5766 |                 } | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5767 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5768 |             if (doBroadcast) { | 
 | 5769 |                 mStartStopCond.broadcast(); | 
 | 5770 |             } | 
 | 5771 |  | 
 | 5772 |             // sleep if there are no active tracks to process | 
 | 5773 |             if (activeTracks.size() == 0) { | 
 | 5774 |                 if (sleepUs == 0) { | 
 | 5775 |                     sleepUs = kRecordThreadSleepUs; | 
 | 5776 |                 } | 
 | 5777 |                 continue; | 
 | 5778 |             } | 
 | 5779 |             sleepUs = 0; | 
| Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5780 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5781 |             lockEffectChains_l(effectChains); | 
 | 5782 |         } | 
 | 5783 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5784 |         // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 | 
| Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5785 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5786 |         size_t size = effectChains.size(); | 
 | 5787 |         for (size_t i = 0; i < size; i++) { | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5788 |             // thread mutex is not locked, but effect chain is locked | 
 | 5789 |             effectChains[i]->process_l(); | 
 | 5790 |         } | 
 | 5791 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5792 |         // 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] | 5793 |         if (mFastCapture != 0) { | 
 | 5794 |             FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 5795 |             FastCaptureState *state = sq->begin(); | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5796 |             bool didModify = false; | 
 | 5797 |             FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5798 |             if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && | 
 | 5799 |                     (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { | 
 | 5800 |                 if (state->mCommand == FastCaptureState::COLD_IDLE) { | 
 | 5801 |                     int32_t old = android_atomic_inc(&mFastCaptureFutex); | 
 | 5802 |                     if (old == -1) { | 
 | 5803 |                         (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 5804 |                     } | 
 | 5805 |                 } | 
 | 5806 |                 state->mCommand = FastCaptureState::READ_WRITE; | 
 | 5807 | #if 0   // FIXME | 
 | 5808 |                 mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? | 
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5809 |                         FastThreadDumpState::kSamplingNforLowRamDevice : | 
 | 5810 |                         FastThreadDumpState::kSamplingN); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5811 | #endif | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5812 |                 didModify = true; | 
 | 5813 |             } | 
 | 5814 |             audio_track_cblk_t *cblkOld = state->mCblk; | 
 | 5815 |             audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; | 
 | 5816 |             if (cblkNew != cblkOld) { | 
 | 5817 |                 state->mCblk = cblkNew; | 
 | 5818 |                 // block until acked if removing a fast track | 
 | 5819 |                 if (cblkOld != NULL) { | 
 | 5820 |                     block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; | 
 | 5821 |                 } | 
 | 5822 |                 didModify = true; | 
 | 5823 |             } | 
 | 5824 |             sq->end(didModify); | 
 | 5825 |             if (didModify) { | 
 | 5826 |                 sq->push(block); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5827 | #if 0 | 
 | 5828 |                 if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 5829 |                     mNormalSource = mPipeSource; | 
 | 5830 |                 } | 
 | 5831 | #endif | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5832 |             } | 
 | 5833 |         } | 
 | 5834 |  | 
| Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5835 |         // now run the fast track destructor with thread mutex unlocked | 
 | 5836 |         fastTrackToRemove.clear(); | 
 | 5837 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5838 |         // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. | 
 | 5839 |         // Only the client(s) that are too slow will overrun. But if even the fastest client is too | 
 | 5840 |         // slow, then this RecordThread will overrun by not calling HAL read often enough. | 
 | 5841 |         // If destination is non-contiguous, first read past the nominal end of buffer, then | 
 | 5842 |         // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated. | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5843 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5844 |         int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5845 |         ssize_t framesRead; | 
 | 5846 |  | 
 | 5847 |         // If an NBAIO source is present, use it to read the normal capture's data | 
 | 5848 |         if (mPipeSource != 0) { | 
 | 5849 |             size_t framesToRead = mBufferSize / mFrameSize; | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5850 |             framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5851 |                     framesToRead, AudioBufferProvider::kInvalidPTS); | 
 | 5852 |             if (framesRead == 0) { | 
 | 5853 |                 // since pipe is non-blocking, simulate blocking input | 
 | 5854 |                 sleepUs = (framesToRead * 1000000LL) / mSampleRate; | 
 | 5855 |             } | 
 | 5856 |         // otherwise use the HAL / AudioStreamIn directly | 
 | 5857 |         } else { | 
 | 5858 |             ssize_t bytesRead = mInput->stream->read(mInput->stream, | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5859 |                     (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5860 |             if (bytesRead < 0) { | 
 | 5861 |                 framesRead = bytesRead; | 
 | 5862 |             } else { | 
 | 5863 |                 framesRead = bytesRead / mFrameSize; | 
 | 5864 |             } | 
 | 5865 |         } | 
 | 5866 |  | 
 | 5867 |         if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { | 
 | 5868 |             ALOGE("read failed: framesRead=%d", framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5869 |             // Force input into standby so that it tries to recover at next read attempt | 
 | 5870 |             inputStandBy(); | 
 | 5871 |             sleepUs = kRecordThreadSleepUs; | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5872 |         } | 
 | 5873 |         if (framesRead <= 0) { | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5874 |             goto unlock; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5875 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5876 |         ALOG_ASSERT(framesRead > 0); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5877 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5878 |         if (mTeeSink != 0) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5879 |             (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5880 |         } | 
 | 5881 |         // 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] | 5882 |         { | 
 | 5883 |             size_t part1 = mRsmpInFramesP2 - rear; | 
 | 5884 |             if ((size_t) framesRead > part1) { | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5885 |                 memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5886 |                         (framesRead - part1) * mFrameSize); | 
 | 5887 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5888 |         } | 
 | 5889 |         rear = mRsmpInRear += framesRead; | 
 | 5890 |  | 
 | 5891 |         size = activeTracks.size(); | 
 | 5892 |         // loop over each active track | 
 | 5893 |         for (size_t i = 0; i < size; i++) { | 
 | 5894 |             activeTrack = activeTracks[i]; | 
 | 5895 |  | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5896 |             // skip fast tracks, as those are handled directly by FastCapture | 
 | 5897 |             if (activeTrack->isFastTrack()) { | 
 | 5898 |                 continue; | 
 | 5899 |             } | 
 | 5900 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5901 |             // TODO: This code probably should be moved to RecordTrack. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 5902 |             // TODO: Update the activeTrack buffer converter in case of reconfigure. | 
 | 5903 |  | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5904 |             enum { | 
 | 5905 |                 OVERRUN_UNKNOWN, | 
 | 5906 |                 OVERRUN_TRUE, | 
 | 5907 |                 OVERRUN_FALSE | 
 | 5908 |             } overrun = OVERRUN_UNKNOWN; | 
 | 5909 |  | 
 | 5910 |             // loop over getNextBuffer to handle circular sink | 
 | 5911 |             for (;;) { | 
 | 5912 |  | 
 | 5913 |                 activeTrack->mSink.frameCount = ~0; | 
 | 5914 |                 status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); | 
 | 5915 |                 size_t framesOut = activeTrack->mSink.frameCount; | 
 | 5916 |                 LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); | 
 | 5917 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5918 |                 // check available frames and handle overrun conditions | 
 | 5919 |                 // if the record track isn't draining fast enough. | 
 | 5920 |                 bool hasOverrun; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5921 |                 size_t framesIn; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5922 |                 activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); | 
 | 5923 |                 if (hasOverrun) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5924 |                     overrun = OVERRUN_TRUE; | 
 | 5925 |                 } | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5926 |                 if (framesOut == 0 || framesIn == 0) { | 
 | 5927 |                     break; | 
 | 5928 |                 } | 
 | 5929 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 5930 |                 // Don't allow framesOut to be larger than what is possible with resampling | 
 | 5931 |                 // from framesIn. | 
 | 5932 |                 // This isn't strictly necessary but helps limit buffer resizing in | 
 | 5933 |                 // RecordBufferConverter.  TODO: remove when no longer needed. | 
 | 5934 |                 framesOut = min(framesOut, | 
 | 5935 |                         destinationFramesPossible( | 
 | 5936 |                                 framesIn, mSampleRate, activeTrack->mSampleRate)); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 5937 |                 // process frames from the RecordThread buffer provider to the RecordTrack buffer | 
 | 5938 |                 framesOut = activeTrack->mRecordBufferConverter->convert( | 
 | 5939 |                         activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5940 |  | 
 | 5941 |                 if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { | 
 | 5942 |                     overrun = OVERRUN_FALSE; | 
 | 5943 |                 } | 
 | 5944 |  | 
 | 5945 |                 if (activeTrack->mFramesToDrop == 0) { | 
 | 5946 |                     if (framesOut > 0) { | 
 | 5947 |                         activeTrack->mSink.frameCount = framesOut; | 
 | 5948 |                         activeTrack->releaseBuffer(&activeTrack->mSink); | 
 | 5949 |                     } | 
 | 5950 |                 } else { | 
 | 5951 |                     // FIXME could do a partial drop of framesOut | 
 | 5952 |                     if (activeTrack->mFramesToDrop > 0) { | 
 | 5953 |                         activeTrack->mFramesToDrop -= framesOut; | 
 | 5954 |                         if (activeTrack->mFramesToDrop <= 0) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5955 |                             activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5956 |                         } | 
 | 5957 |                     } else { | 
 | 5958 |                         activeTrack->mFramesToDrop += framesOut; | 
 | 5959 |                         if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || | 
 | 5960 |                                 activeTrack->mSyncStartEvent->isCancelled()) { | 
 | 5961 |                             ALOGW("Synced record %s, session %d, trigger session %d", | 
 | 5962 |                                   (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", | 
 | 5963 |                                   activeTrack->sessionId(), | 
 | 5964 |                                   (activeTrack->mSyncStartEvent != 0) ? | 
 | 5965 |                                           activeTrack->mSyncStartEvent->triggerSession() : 0); | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5966 |                             activeTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5967 |                         } | 
 | 5968 |                     } | 
 | 5969 |                 } | 
 | 5970 |  | 
 | 5971 |                 if (framesOut == 0) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5972 |                     break; | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5973 |                 } | 
 | 5974 |             } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5975 |  | 
 | 5976 |             switch (overrun) { | 
 | 5977 |             case OVERRUN_TRUE: | 
 | 5978 |                 // client isn't retrieving buffers fast enough | 
 | 5979 |                 if (!activeTrack->setOverflow()) { | 
 | 5980 |                     nsecs_t now = systemTime(); | 
 | 5981 |                     // FIXME should lastWarning per track? | 
 | 5982 |                     if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 5983 |                         ALOGW("RecordThread: buffer overflow"); | 
 | 5984 |                         lastWarning = now; | 
 | 5985 |                     } | 
 | 5986 |                 } | 
 | 5987 |                 break; | 
 | 5988 |             case OVERRUN_FALSE: | 
 | 5989 |                 activeTrack->clearOverflow(); | 
 | 5990 |                 break; | 
 | 5991 |             case OVERRUN_UNKNOWN: | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5992 |                 break; | 
 | 5993 |             } | 
 | 5994 |  | 
| Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5995 |         } | 
 | 5996 |  | 
| Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5997 | unlock: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5998 |         // enable changes in effect chain | 
 | 5999 |         unlockEffectChains(effectChains); | 
| Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6000 |         // 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] | 6001 |     } | 
 | 6002 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6003 |     standbyIfNotAlreadyInStandby(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6004 |  | 
 | 6005 |     { | 
 | 6006 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6007 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6008 |             sp<RecordTrack> track = mTracks[i]; | 
 | 6009 |             track->invalidate(); | 
 | 6010 |         } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6011 |         mActiveTracks.clear(); | 
 | 6012 |         mActiveTracksGen++; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6013 |         mStartStopCond.broadcast(); | 
 | 6014 |     } | 
 | 6015 |  | 
 | 6016 |     releaseWakeLock(); | 
 | 6017 |  | 
 | 6018 |     ALOGV("RecordThread %p exiting", this); | 
 | 6019 |     return false; | 
 | 6020 | } | 
 | 6021 |  | 
| Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6022 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6023 | { | 
 | 6024 |     if (!mStandby) { | 
 | 6025 |         inputStandBy(); | 
 | 6026 |         mStandby = true; | 
 | 6027 |     } | 
 | 6028 | } | 
 | 6029 |  | 
 | 6030 | void AudioFlinger::RecordThread::inputStandBy() | 
 | 6031 | { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6032 |     // Idle the fast capture if it's currently running | 
 | 6033 |     if (mFastCapture != 0) { | 
 | 6034 |         FastCaptureStateQueue *sq = mFastCapture->sq(); | 
 | 6035 |         FastCaptureState *state = sq->begin(); | 
 | 6036 |         if (!(state->mCommand & FastCaptureState::IDLE)) { | 
 | 6037 |             state->mCommand = FastCaptureState::COLD_IDLE; | 
 | 6038 |             state->mColdFutexAddr = &mFastCaptureFutex; | 
 | 6039 |             state->mColdGen++; | 
 | 6040 |             mFastCaptureFutex = 0; | 
 | 6041 |             sq->end(); | 
 | 6042 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 6043 |             sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); | 
 | 6044 | #if 0 | 
 | 6045 |             if (kUseFastCapture == FastCapture_Dynamic) { | 
 | 6046 |                 // FIXME | 
 | 6047 |             } | 
 | 6048 | #endif | 
 | 6049 | #ifdef AUDIO_WATCHDOG | 
 | 6050 |             // FIXME | 
 | 6051 | #endif | 
 | 6052 |         } else { | 
 | 6053 |             sq->end(false /*didModify*/); | 
 | 6054 |         } | 
 | 6055 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6056 |     mInput->stream->common.standby(&mInput->stream->common); | 
 | 6057 | } | 
 | 6058 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6059 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6060 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6061 |         const sp<AudioFlinger::Client>& client, | 
 | 6062 |         uint32_t sampleRate, | 
 | 6063 |         audio_format_t format, | 
 | 6064 |         audio_channel_mask_t channelMask, | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6065 |         size_t *pFrameCount, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6066 |         int sessionId, | 
| Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6067 |         size_t *notificationFrames, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6068 |         int uid, | 
| Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 6069 |         IAudioFlinger::track_flags_t *flags, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6070 |         pid_t tid, | 
 | 6071 |         status_t *status) | 
 | 6072 | { | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6073 |     size_t frameCount = *pFrameCount; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6074 |     sp<RecordTrack> track; | 
 | 6075 |     status_t lStatus; | 
 | 6076 |  | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6077 |     // client expresses a preference for FAST, but we get the final say | 
 | 6078 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 6079 |       if ( | 
| Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6080 |             // we formerly checked for a callback handler (non-0 tid), | 
 | 6081 |             // but that is no longer required for TRANSFER_OBTAIN mode | 
 | 6082 |             // | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6083 |             // frame count is not specified, or is exactly the pipe depth | 
 | 6084 |             ((frameCount == 0) || (frameCount == mPipeFramesP2)) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6085 |             // PCM data | 
 | 6086 |             audio_is_linear_pcm(format) && | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6087 |             // native format | 
 | 6088 |             (format == mFormat) && | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6089 |             // native channel mask | 
 | 6090 |             (channelMask == mChannelMask) && | 
 | 6091 |             // native hardware sample rate | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6092 |             (sampleRate == mSampleRate) && | 
| Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6093 |             // record thread has an associated fast capture | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6094 |             hasFastCapture() && | 
 | 6095 |             // there are sufficient fast track slots available | 
 | 6096 |             mFastTrackAvail | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6097 |         ) { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6098 |         ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6099 |                 frameCount, mFrameCount); | 
 | 6100 |       } else { | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6101 |         ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " | 
 | 6102 |                 "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6103 |                 "hasFastCapture=%d tid=%d mFastTrackAvail=%d", | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6104 |                 frameCount, mFrameCount, mPipeFramesP2, | 
 | 6105 |                 format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, | 
 | 6106 |                 hasFastCapture(), tid, mFastTrackAvail); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6107 |         *flags &= ~IAudioFlinger::TRACK_FAST; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6108 |       } | 
 | 6109 |     } | 
 | 6110 |  | 
 | 6111 |     // compute track buffer size in frames, and suggest the notification frame count | 
 | 6112 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 6113 |         // fast track: frame count is exactly the pipe depth | 
 | 6114 |         frameCount = mPipeFramesP2; | 
 | 6115 |         // ignore requested notificationFrames, and always notify exactly once every HAL buffer | 
 | 6116 |         *notificationFrames = mFrameCount; | 
 | 6117 |     } else { | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6118 |         // not fast track: max notification period is resampled equivalent of one HAL buffer time | 
 | 6119 |         //                 or 20 ms if there is a fast capture | 
 | 6120 |         // TODO This could be a roundupRatio inline, and const | 
 | 6121 |         size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) | 
 | 6122 |                 * sampleRate + mSampleRate - 1) / mSampleRate; | 
 | 6123 |         // minimum number of notification periods is at least kMinNotifications, | 
 | 6124 |         // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) | 
 | 6125 |         static const size_t kMinNotifications = 3; | 
 | 6126 |         static const uint32_t kMinMs = 30; | 
 | 6127 |         // TODO This could be a roundupRatio inline | 
 | 6128 |         const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; | 
 | 6129 |         // TODO This could be a roundupRatio inline | 
 | 6130 |         const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / | 
 | 6131 |                 maxNotificationFrames; | 
 | 6132 |         const size_t minFrameCount = maxNotificationFrames * | 
 | 6133 |                 max(kMinNotifications, minNotificationsByMs); | 
 | 6134 |         frameCount = max(frameCount, minFrameCount); | 
 | 6135 |         if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { | 
 | 6136 |             *notificationFrames = maxNotificationFrames; | 
| Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6137 |         } | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6138 |     } | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6139 |     *pFrameCount = frameCount; | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6140 |  | 
| Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6141 |     lStatus = initCheck(); | 
 | 6142 |     if (lStatus != NO_ERROR) { | 
 | 6143 |         ALOGE("createRecordTrack_l() audio driver not initialized"); | 
 | 6144 |         goto Exit; | 
 | 6145 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6146 |  | 
 | 6147 |     { // scope for mLock | 
 | 6148 |         Mutex::Autolock _l(mLock); | 
 | 6149 |  | 
 | 6150 |         track = new RecordTrack(this, client, sampleRate, | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6151 |                       format, channelMask, frameCount, NULL, sessionId, uid, | 
 | 6152 |                       *flags, TrackBase::TYPE_DEFAULT); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6153 |  | 
| Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6154 |         lStatus = track->initCheck(); | 
 | 6155 |         if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6156 |             ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6157 |             // 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] | 6158 |             goto Exit; | 
 | 6159 |         } | 
 | 6160 |         mTracks.add(track); | 
 | 6161 |  | 
 | 6162 |         // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
 | 6163 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 6164 |                         mAudioFlinger->btNrecIsOff(); | 
 | 6165 |         setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
 | 6166 |         setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
| Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6167 |  | 
 | 6168 |         if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
 | 6169 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 6170 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 6171 |             // so ask activity manager to do this on our behalf | 
 | 6172 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
 | 6173 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6174 |     } | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6175 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6176 |     lStatus = NO_ERROR; | 
 | 6177 |  | 
 | 6178 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6179 |     *status = lStatus; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6180 |     return track; | 
 | 6181 | } | 
 | 6182 |  | 
 | 6183 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, | 
 | 6184 |                                            AudioSystem::sync_event_t event, | 
 | 6185 |                                            int triggerSession) | 
 | 6186 | { | 
 | 6187 |     ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
 | 6188 |     sp<ThreadBase> strongMe = this; | 
 | 6189 |     status_t status = NO_ERROR; | 
 | 6190 |  | 
 | 6191 |     if (event == AudioSystem::SYNC_EVENT_NONE) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6192 |         recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6193 |     } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6194 |         recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6195 |                                        triggerSession, | 
 | 6196 |                                        recordTrack->sessionId(), | 
 | 6197 |                                        syncStartEventCallback, | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6198 |                                        recordTrack); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6199 |         // Sync event can be cancelled by the trigger session if the track is not in a | 
 | 6200 |         // compatible state in which case we start record immediately | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6201 |         if (recordTrack->mSyncStartEvent->isCancelled()) { | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6202 |             recordTrack->clearSyncStartEvent(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6203 |         } else { | 
 | 6204 |             // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6205 |             recordTrack->mFramesToDrop = - | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6206 |                     ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6207 |         } | 
 | 6208 |     } | 
 | 6209 |  | 
 | 6210 |     { | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6211 |         // This section is a rendezvous between binder thread executing start() and RecordThread | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6212 |         AutoMutex lock(mLock); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6213 |         if (mActiveTracks.indexOf(recordTrack) >= 0) { | 
 | 6214 |             if (recordTrack->mState == TrackBase::PAUSING) { | 
 | 6215 |                 ALOGV("active record track PAUSING -> ACTIVE"); | 
| Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6216 |                 recordTrack->mState = TrackBase::ACTIVE; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6217 |             } else { | 
 | 6218 |                 ALOGV("active record track state %d", recordTrack->mState); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6219 |             } | 
 | 6220 |             return status; | 
 | 6221 |         } | 
 | 6222 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6223 |         // TODO consider other ways of handling this, such as changing the state to :STARTING and | 
 | 6224 |         //      adding the track to mActiveTracks after returning from AudioSystem::startInput(), | 
 | 6225 |         //      or using a separate command thread | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6226 |         recordTrack->mState = TrackBase::STARTING_1; | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6227 |         mActiveTracks.add(recordTrack); | 
 | 6228 |         mActiveTracksGen++; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6229 |         status_t status = NO_ERROR; | 
 | 6230 |         if (recordTrack->isExternalTrack()) { | 
 | 6231 |             mLock.unlock(); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6232 |             status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6233 |             mLock.lock(); | 
 | 6234 |             // FIXME should verify that recordTrack is still in mActiveTracks | 
 | 6235 |             if (status != NO_ERROR) { | 
 | 6236 |                 mActiveTracks.remove(recordTrack); | 
 | 6237 |                 mActiveTracksGen++; | 
 | 6238 |                 recordTrack->clearSyncStartEvent(); | 
 | 6239 |                 ALOGV("RecordThread::start error %d", status); | 
 | 6240 |                 return status; | 
 | 6241 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6242 |         } | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6243 |         // Catch up with current buffer indices if thread is already running. | 
 | 6244 |         // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront | 
 | 6245 |         // was initialized to some value closer to the thread's mRsmpInFront, then the track could | 
 | 6246 |         // see previously buffered data before it called start(), but with greater risk of overrun. | 
 | 6247 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6248 |         recordTrack->mResamplerBufferProvider->reset(); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6249 |         // clear any converter state as new data will be discontinuous | 
 | 6250 |         recordTrack->mRecordBufferConverter->reset(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6251 |         recordTrack->mState = TrackBase::STARTING_2; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6252 |         // signal thread to start | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6253 |         mWaitWorkCV.broadcast(); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6254 |         if (mActiveTracks.indexOf(recordTrack) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6255 |             ALOGV("Record failed to start"); | 
 | 6256 |             status = BAD_VALUE; | 
 | 6257 |             goto startError; | 
 | 6258 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6259 |         return status; | 
 | 6260 |     } | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6261 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6262 | startError: | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6263 |     if (recordTrack->isExternalTrack()) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6264 |         AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6265 |     } | 
| Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6266 |     recordTrack->clearSyncStartEvent(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6267 |     // FIXME I wonder why we do not reset the state here? | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6268 |     return status; | 
 | 6269 | } | 
 | 6270 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6271 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
 | 6272 | { | 
 | 6273 |     sp<SyncEvent> strongEvent = event.promote(); | 
 | 6274 |  | 
 | 6275 |     if (strongEvent != 0) { | 
| Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6276 |         sp<RefBase> ptr = strongEvent->cookie().promote(); | 
 | 6277 |         if (ptr != 0) { | 
 | 6278 |             RecordTrack *recordTrack = (RecordTrack *)ptr.get(); | 
 | 6279 |             recordTrack->handleSyncStartEvent(strongEvent); | 
 | 6280 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6281 |     } | 
 | 6282 | } | 
 | 6283 |  | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6284 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6285 |     ALOGV("RecordThread::stop"); | 
| Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6286 |     AutoMutex _l(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6287 |     if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6288 |         return false; | 
 | 6289 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6290 |     // 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] | 6291 |     recordTrack->mState = TrackBase::PAUSING; | 
 | 6292 |     // do not wait for mStartStopCond if exiting | 
 | 6293 |     if (exitPending()) { | 
 | 6294 |         return true; | 
 | 6295 |     } | 
| Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6296 |     // FIXME incorrect usage of wait: no explicit predicate or loop | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6297 |     mStartStopCond.wait(mLock); | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6298 |     // if we have been restarted, recordTrack is in mActiveTracks here | 
 | 6299 |     if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6300 |         ALOGV("Record stopped OK"); | 
 | 6301 |         return true; | 
 | 6302 |     } | 
 | 6303 |     return false; | 
 | 6304 | } | 
 | 6305 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6306 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6307 | { | 
 | 6308 |     return false; | 
 | 6309 | } | 
 | 6310 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6311 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6312 | { | 
 | 6313 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
 | 6314 |     if (!isValidSyncEvent(event)) { | 
 | 6315 |         return BAD_VALUE; | 
 | 6316 |     } | 
 | 6317 |  | 
 | 6318 |     int eventSession = event->triggerSession(); | 
 | 6319 |     status_t ret = NAME_NOT_FOUND; | 
 | 6320 |  | 
 | 6321 |     Mutex::Autolock _l(mLock); | 
 | 6322 |  | 
 | 6323 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6324 |         sp<RecordTrack> track = mTracks[i]; | 
 | 6325 |         if (eventSession == track->sessionId()) { | 
 | 6326 |             (void) track->setSyncEvent(event); | 
 | 6327 |             ret = NO_ERROR; | 
 | 6328 |         } | 
 | 6329 |     } | 
 | 6330 |     return ret; | 
 | 6331 | #else | 
 | 6332 |     return BAD_VALUE; | 
 | 6333 | #endif | 
 | 6334 | } | 
 | 6335 |  | 
 | 6336 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
 | 6337 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) | 
 | 6338 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6339 |     track->terminate(); | 
 | 6340 |     track->mState = TrackBase::STOPPED; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6341 |     // active tracks are removed by threadLoop() | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6342 |     if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6343 |         removeTrack_l(track); | 
 | 6344 |     } | 
 | 6345 | } | 
 | 6346 |  | 
 | 6347 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) | 
 | 6348 | { | 
 | 6349 |     mTracks.remove(track); | 
 | 6350 |     // need anything related to effects here? | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6351 |     if (track->isFastTrack()) { | 
 | 6352 |         ALOG_ASSERT(!mFastTrackAvail); | 
 | 6353 |         mFastTrackAvail = true; | 
 | 6354 |     } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6355 | } | 
 | 6356 |  | 
 | 6357 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
 | 6358 | { | 
 | 6359 |     dumpInternals(fd, args); | 
 | 6360 |     dumpTracks(fd, args); | 
 | 6361 |     dumpEffectChains(fd, args); | 
 | 6362 | } | 
 | 6363 |  | 
 | 6364 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 6365 | { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6366 |     dprintf(fd, "\nInput thread %p:\n", this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6367 |  | 
| Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6368 |     dumpBase(fd, args); | 
 | 6369 |  | 
 | 6370 |     if (mActiveTracks.size() == 0) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6371 |         dprintf(fd, "  No active record clients\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6372 |     } | 
| Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6373 |     dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6374 |     dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); | 
| Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6375 |  | 
 | 6376 |     //  Make a non-atomic copy of fast capture dump state so it won't change underneath us | 
 | 6377 |     const FastCaptureDumpState copy(mFastCaptureDumpState); | 
 | 6378 |     copy.dump(fd); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6379 | } | 
 | 6380 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6381 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6382 | { | 
 | 6383 |     const size_t SIZE = 256; | 
 | 6384 |     char buffer[SIZE]; | 
 | 6385 |     String8 result; | 
 | 6386 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6387 |     size_t numtracks = mTracks.size(); | 
 | 6388 |     size_t numactive = mActiveTracks.size(); | 
 | 6389 |     size_t numactiveseen = 0; | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6390 |     dprintf(fd, "  %d Tracks", numtracks); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6391 |     if (numtracks) { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6392 |         dprintf(fd, " of which %d are active\n", numactive); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6393 |         RecordTrack::appendDumpHeader(result); | 
 | 6394 |         for (size_t i = 0; i < numtracks ; ++i) { | 
 | 6395 |             sp<RecordTrack> track = mTracks[i]; | 
 | 6396 |             if (track != 0) { | 
 | 6397 |                 bool active = mActiveTracks.indexOf(track) >= 0; | 
 | 6398 |                 if (active) { | 
 | 6399 |                     numactiveseen++; | 
 | 6400 |                 } | 
 | 6401 |                 track->dump(buffer, SIZE, active); | 
 | 6402 |                 result.append(buffer); | 
 | 6403 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6404 |         } | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6405 |     } else { | 
| Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6406 |         dprintf(fd, "\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6407 |     } | 
 | 6408 |  | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6409 |     if (numactiveseen != numactive) { | 
 | 6410 |         snprintf(buffer, SIZE, "  The following tracks are in the active list but" | 
 | 6411 |                 " not in the track list\n"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6412 |         result.append(buffer); | 
 | 6413 |         RecordTrack::appendDumpHeader(result); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6414 |         for (size_t i = 0; i < numactive; ++i) { | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6415 |             sp<RecordTrack> track = mActiveTracks[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6416 |             if (mTracks.indexOf(track) < 0) { | 
 | 6417 |                 track->dump(buffer, SIZE, true); | 
 | 6418 |                 result.append(buffer); | 
 | 6419 |             } | 
| Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6420 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6421 |  | 
 | 6422 |     } | 
 | 6423 |     write(fd, result.string(), result.size()); | 
 | 6424 | } | 
 | 6425 |  | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6426 |  | 
 | 6427 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() | 
 | 6428 | { | 
 | 6429 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
 | 6430 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6431 |     mRsmpInFront = recordThread->mRsmpInRear; | 
 | 6432 |     mRsmpInUnrel = 0; | 
 | 6433 | } | 
 | 6434 |  | 
 | 6435 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( | 
 | 6436 |         size_t *framesAvailable, bool *hasOverrun) | 
 | 6437 | { | 
 | 6438 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
 | 6439 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6440 |     const int32_t rear = recordThread->mRsmpInRear; | 
 | 6441 |     const int32_t front = mRsmpInFront; | 
 | 6442 |     const ssize_t filled = rear - front; | 
 | 6443 |  | 
 | 6444 |     size_t framesIn; | 
 | 6445 |     bool overrun = false; | 
 | 6446 |     if (filled < 0) { | 
 | 6447 |         // should not happen, but treat like a massive overrun and re-sync | 
 | 6448 |         framesIn = 0; | 
 | 6449 |         mRsmpInFront = rear; | 
 | 6450 |         overrun = true; | 
 | 6451 |     } else if ((size_t) filled <= recordThread->mRsmpInFrames) { | 
 | 6452 |         framesIn = (size_t) filled; | 
 | 6453 |     } else { | 
 | 6454 |         // client is not keeping up with server, but give it latest data | 
 | 6455 |         framesIn = recordThread->mRsmpInFrames; | 
 | 6456 |         mRsmpInFront = /* front = */ rear - framesIn; | 
 | 6457 |         overrun = true; | 
 | 6458 |     } | 
 | 6459 |     if (framesAvailable != NULL) { | 
 | 6460 |         *framesAvailable = framesIn; | 
 | 6461 |     } | 
 | 6462 |     if (hasOverrun != NULL) { | 
 | 6463 |         *hasOverrun = overrun; | 
 | 6464 |     } | 
 | 6465 | } | 
 | 6466 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6467 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6468 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( | 
 | 6469 |         AudioBufferProvider::Buffer* buffer, int64_t pts __unused) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6470 | { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6471 |     sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6472 |     if (threadBase == 0) { | 
 | 6473 |         buffer->frameCount = 0; | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6474 |         buffer->raw = NULL; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6475 |         return NOT_ENOUGH_DATA; | 
 | 6476 |     } | 
 | 6477 |     RecordThread *recordThread = (RecordThread *) threadBase.get(); | 
 | 6478 |     int32_t rear = recordThread->mRsmpInRear; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6479 |     int32_t front = mRsmpInFront; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6480 |     ssize_t filled = rear - front; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6481 |     // FIXME should not be P2 (don't want to increase latency) | 
 | 6482 |     // FIXME if client not keeping up, discard | 
| Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6483 |     LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6484 |     // 'filled' may be non-contiguous, so return only the first contiguous chunk | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6485 |     front &= recordThread->mRsmpInFramesP2 - 1; | 
 | 6486 |     size_t part1 = recordThread->mRsmpInFramesP2 - front; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6487 |     if (part1 > (size_t) filled) { | 
 | 6488 |         part1 = filled; | 
 | 6489 |     } | 
 | 6490 |     size_t ask = buffer->frameCount; | 
 | 6491 |     ALOG_ASSERT(ask > 0); | 
 | 6492 |     if (part1 > ask) { | 
 | 6493 |         part1 = ask; | 
 | 6494 |     } | 
 | 6495 |     if (part1 == 0) { | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6496 |         // 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] | 6497 |         buffer->raw = NULL; | 
 | 6498 |         buffer->frameCount = 0; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6499 |         mRsmpInUnrel = 0; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6500 |         return NOT_ENOUGH_DATA; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6501 |     } | 
 | 6502 |  | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6503 |     buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6504 |     buffer->frameCount = part1; | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6505 |     mRsmpInUnrel = part1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6506 |     return NO_ERROR; | 
 | 6507 | } | 
 | 6508 |  | 
 | 6509 | // AudioBufferProvider interface | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6510 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( | 
 | 6511 |         AudioBufferProvider::Buffer* buffer) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6512 | { | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6513 |     size_t stepCount = buffer->frameCount; | 
 | 6514 |     if (stepCount == 0) { | 
 | 6515 |         return; | 
 | 6516 |     } | 
| Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6517 |     ALOG_ASSERT(stepCount <= mRsmpInUnrel); | 
 | 6518 |     mRsmpInUnrel -= stepCount; | 
 | 6519 |     mRsmpInFront += stepCount; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6520 |     buffer->raw = NULL; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6521 |     buffer->frameCount = 0; | 
 | 6522 | } | 
 | 6523 |  | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6524 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( | 
 | 6525 |         audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, | 
 | 6526 |         uint32_t srcSampleRate, | 
 | 6527 |         audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, | 
 | 6528 |         uint32_t dstSampleRate) : | 
 | 6529 |             mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars | 
 | 6530 |             // mSrcFormat | 
 | 6531 |             // mSrcSampleRate | 
 | 6532 |             // mDstChannelMask | 
 | 6533 |             // mDstFormat | 
 | 6534 |             // mDstSampleRate | 
 | 6535 |             // mSrcChannelCount | 
 | 6536 |             // mDstChannelCount | 
 | 6537 |             // mDstFrameSize | 
 | 6538 |             mBuf(NULL), mBufFrames(0), mBufFrameSize(0), | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6539 |             mResampler(NULL), | 
 | 6540 |             mIsLegacyDownmix(false), | 
 | 6541 |             mIsLegacyUpmix(false), | 
 | 6542 |             mRequiresFloat(false), | 
 | 6543 |             mInputConverterProvider(NULL) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6544 | { | 
 | 6545 |     (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, | 
 | 6546 |             dstChannelMask, dstFormat, dstSampleRate); | 
 | 6547 | } | 
 | 6548 |  | 
 | 6549 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { | 
 | 6550 |     free(mBuf); | 
 | 6551 |     delete mResampler; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6552 |     delete mInputConverterProvider; | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6553 | } | 
 | 6554 |  | 
 | 6555 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, | 
 | 6556 |         AudioBufferProvider *provider, size_t frames) | 
 | 6557 | { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6558 |     if (mInputConverterProvider != NULL) { | 
 | 6559 |         mInputConverterProvider->setBufferProvider(provider); | 
 | 6560 |         provider = mInputConverterProvider; | 
 | 6561 |     } | 
 | 6562 |  | 
 | 6563 |     if (mResampler == NULL) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6564 |         ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", | 
 | 6565 |                 mSrcSampleRate, mSrcFormat, mDstFormat); | 
 | 6566 |  | 
 | 6567 |         AudioBufferProvider::Buffer buffer; | 
 | 6568 |         for (size_t i = frames; i > 0; ) { | 
 | 6569 |             buffer.frameCount = i; | 
 | 6570 |             status_t status = provider->getNextBuffer(&buffer, 0); | 
 | 6571 |             if (status != OK || buffer.frameCount == 0) { | 
 | 6572 |                 frames -= i; // cannot fill request. | 
 | 6573 |                 break; | 
 | 6574 |             } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6575 |             // format convert to destination buffer | 
 | 6576 |             convertNoResampler(dst, buffer.raw, buffer.frameCount); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6577 |  | 
 | 6578 |             dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; | 
 | 6579 |             i -= buffer.frameCount; | 
 | 6580 |             provider->releaseBuffer(&buffer); | 
 | 6581 |         } | 
 | 6582 |     } else { | 
 | 6583 |          ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", | 
 | 6584 |                  mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); | 
 | 6585 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6586 |          // reallocate buffer if needed | 
 | 6587 |          if (mBufFrameSize != 0 && mBufFrames < frames) { | 
 | 6588 |              free(mBuf); | 
 | 6589 |              mBufFrames = frames; | 
 | 6590 |              (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); | 
 | 6591 |          } | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6592 |         // resampler accumulates, but we only have one source track | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6593 |         memset(mBuf, 0, frames * mBufFrameSize); | 
 | 6594 |         frames = mResampler->resample((int32_t*)mBuf, frames, provider); | 
 | 6595 |         // format convert to destination buffer | 
 | 6596 |         convertResampler(dst, mBuf, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6597 |     } | 
 | 6598 |     return frames; | 
 | 6599 | } | 
 | 6600 |  | 
 | 6601 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( | 
 | 6602 |         audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, | 
 | 6603 |         uint32_t srcSampleRate, | 
 | 6604 |         audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, | 
 | 6605 |         uint32_t dstSampleRate) | 
 | 6606 | { | 
 | 6607 |     // quick evaluation if there is any change. | 
 | 6608 |     if (mSrcFormat == srcFormat | 
 | 6609 |             && mSrcChannelMask == srcChannelMask | 
 | 6610 |             && mSrcSampleRate == srcSampleRate | 
 | 6611 |             && mDstFormat == dstFormat | 
 | 6612 |             && mDstChannelMask == dstChannelMask | 
 | 6613 |             && mDstSampleRate == dstSampleRate) { | 
 | 6614 |         return NO_ERROR; | 
 | 6615 |     } | 
 | 6616 |  | 
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 6617 |     ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" | 
 | 6618 |             "  srcFormat:%#x dstFormat:%#x  srcRate:%u dstRate:%u", | 
 | 6619 |             srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6620 |     const bool valid = | 
 | 6621 |             audio_is_input_channel(srcChannelMask) | 
 | 6622 |             && audio_is_input_channel(dstChannelMask) | 
 | 6623 |             && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) | 
 | 6624 |             && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) | 
 | 6625 |             && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) | 
 | 6626 |             ; // no upsampling checks for now | 
 | 6627 |     if (!valid) { | 
 | 6628 |         return BAD_VALUE; | 
 | 6629 |     } | 
 | 6630 |  | 
 | 6631 |     mSrcFormat = srcFormat; | 
 | 6632 |     mSrcChannelMask = srcChannelMask; | 
 | 6633 |     mSrcSampleRate = srcSampleRate; | 
 | 6634 |     mDstFormat = dstFormat; | 
 | 6635 |     mDstChannelMask = dstChannelMask; | 
 | 6636 |     mDstSampleRate = dstSampleRate; | 
 | 6637 |  | 
 | 6638 |     // compute derived parameters | 
 | 6639 |     mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); | 
 | 6640 |     mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); | 
 | 6641 |     mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); | 
 | 6642 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6643 |     // do we need to resample? | 
 | 6644 |     delete mResampler; | 
 | 6645 |     mResampler = NULL; | 
 | 6646 |     if (mSrcSampleRate != mDstSampleRate) { | 
 | 6647 |         mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, | 
 | 6648 |                 mSrcChannelCount, mDstSampleRate); | 
 | 6649 |         mResampler->setSampleRate(mSrcSampleRate); | 
 | 6650 |         mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); | 
 | 6651 |     } | 
 | 6652 |  | 
 | 6653 |     // are we running legacy channel conversion modes? | 
 | 6654 |     mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO | 
 | 6655 |                             || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) | 
 | 6656 |                    && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; | 
 | 6657 |     mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO | 
 | 6658 |                    && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO | 
 | 6659 |                             || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); | 
 | 6660 |  | 
 | 6661 |     // do we need to process in float? | 
 | 6662 |     mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; | 
 | 6663 |  | 
 | 6664 |     // do we need a staging buffer to convert for destination (we can still optimize this)? | 
 | 6665 |     // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity | 
 | 6666 |     if (mResampler != NULL) { | 
 | 6667 |         mBufFrameSize = max(mSrcChannelCount, FCC_2) | 
 | 6668 |                 * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); | 
| Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame] | 6669 |     } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6670 |         mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); | 
 | 6671 |     } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6672 |         mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); | 
 | 6673 |     } else { | 
 | 6674 |         mBufFrameSize = 0; | 
 | 6675 |     } | 
 | 6676 |     mBufFrames = 0; // force the buffer to be resized. | 
 | 6677 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6678 |     // do we need an input converter buffer provider to give us float? | 
 | 6679 |     delete mInputConverterProvider; | 
 | 6680 |     mInputConverterProvider = NULL; | 
 | 6681 |     if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
 | 6682 |         mInputConverterProvider = new ReformatBufferProvider( | 
 | 6683 |                 audio_channel_count_from_in_mask(mSrcChannelMask), | 
 | 6684 |                 mSrcFormat, | 
 | 6685 |                 AUDIO_FORMAT_PCM_FLOAT, | 
 | 6686 |                 256 /* provider buffer frame count */); | 
 | 6687 |     } | 
 | 6688 |  | 
 | 6689 |     // do we need a remixer to do channel mask conversion | 
 | 6690 |     if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { | 
 | 6691 |         (void) memcpy_by_index_array_initialization_from_channel_mask( | 
 | 6692 |                 mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6693 |     } | 
 | 6694 |     return NO_ERROR; | 
 | 6695 | } | 
 | 6696 |  | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6697 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( | 
 | 6698 |         void *dst, const void *src, size_t frames) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6699 | { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6700 |     // src is native type unless there is legacy upmix or downmix, whereupon it is float. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6701 |     if (mBufFrameSize != 0 && mBufFrames < frames) { | 
 | 6702 |         free(mBuf); | 
 | 6703 |         mBufFrames = frames; | 
 | 6704 |         (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); | 
 | 6705 |     } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6706 |     // do we need to do legacy upmix and downmix? | 
 | 6707 |     if (mIsLegacyUpmix || mIsLegacyDownmix) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6708 |         void *dstBuf = mBuf != NULL ? mBuf : dst; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6709 |         if (mIsLegacyUpmix) { | 
 | 6710 |             upmix_to_stereo_float_from_mono_float((float *)dstBuf, | 
 | 6711 |                     (const float *)src, frames); | 
 | 6712 |         } else /*mIsLegacyDownmix */ { | 
 | 6713 |             downmix_to_mono_float_from_stereo_float((float *)dstBuf, | 
 | 6714 |                     (const float *)src, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6715 |         } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6716 |         if (mBuf != NULL) { | 
 | 6717 |             memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, | 
 | 6718 |                     frames * mDstChannelCount); | 
 | 6719 |         } | 
 | 6720 |         return; | 
 | 6721 |     } | 
 | 6722 |     // do we need to do channel mask conversion? | 
 | 6723 |     if (mSrcChannelMask != mDstChannelMask) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6724 |         void *dstBuf = mBuf != NULL ? mBuf : dst; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6725 |         memcpy_by_index_array(dstBuf, mDstChannelCount, | 
 | 6726 |                 src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); | 
 | 6727 |         if (dstBuf == dst) { | 
 | 6728 |             return; // format is the same | 
 | 6729 |         } | 
 | 6730 |     } | 
 | 6731 |     // convert to destination buffer | 
 | 6732 |     const void *convertBuf = mBuf != NULL ? mBuf : src; | 
 | 6733 |     memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, | 
 | 6734 |             frames * mDstChannelCount); | 
 | 6735 | } | 
 | 6736 |  | 
 | 6737 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( | 
 | 6738 |         void *dst, /*not-a-const*/ void *src, size_t frames) | 
 | 6739 | { | 
 | 6740 |     // src buffer format is ALWAYS float when entering this routine | 
 | 6741 |     if (mIsLegacyUpmix) { | 
 | 6742 |         ; // mono to stereo already handled by resampler | 
 | 6743 |     } else if (mIsLegacyDownmix | 
 | 6744 |             || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { | 
 | 6745 |         // the resampler outputs stereo for mono input channel (a feature?) | 
 | 6746 |         // must convert to mono | 
 | 6747 |         downmix_to_mono_float_from_stereo_float((float *)src, | 
 | 6748 |                 (const float *)src, frames); | 
 | 6749 |     } else if (mSrcChannelMask != mDstChannelMask) { | 
 | 6750 |         // convert to mono channel again for channel mask conversion (could be skipped | 
 | 6751 |         // with further optimization). | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6752 |         if (mSrcChannelCount == 1) { | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6753 |             downmix_to_mono_float_from_stereo_float((float *)src, | 
 | 6754 |                 (const float *)src, frames); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6755 |         } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6756 |         // convert to destination format (in place, OK as float is larger than other types) | 
 | 6757 |         if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { | 
 | 6758 |             memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, | 
 | 6759 |                     frames * mSrcChannelCount); | 
 | 6760 |         } | 
 | 6761 |         // channel convert and save to dst | 
 | 6762 |         memcpy_by_index_array(dst, mDstChannelCount, | 
 | 6763 |                 src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); | 
 | 6764 |         return; | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6765 |     } | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6766 |     // convert to destination format and save to dst | 
 | 6767 |     memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, | 
 | 6768 |             frames * mDstChannelCount); | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6769 | } | 
 | 6770 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6771 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, | 
 | 6772 |                                                         status_t& status) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6773 | { | 
 | 6774 |     bool reconfig = false; | 
 | 6775 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6776 |     status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6777 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6778 |     audio_format_t reqFormat = mFormat; | 
 | 6779 |     uint32_t samplingRate = mSampleRate; | 
| Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 6780 |     // 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] | 6781 |     audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); | 
 | 6782 |  | 
 | 6783 |     AudioParameter param = AudioParameter(keyValuePair); | 
 | 6784 |     int value; | 
 | 6785 |     // TODO Investigate when this code runs. Check with audio policy when a sample rate and | 
 | 6786 |     //      channel count change can be requested. Do we mandate the first client defines the | 
 | 6787 |     //      HAL sampling rate and channel count or do we allow changes on the fly? | 
 | 6788 |     if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 6789 |         samplingRate = value; | 
 | 6790 |         reconfig = true; | 
 | 6791 |     } | 
 | 6792 |     if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6793 |         if (!audio_is_linear_pcm((audio_format_t) value)) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6794 |             status = BAD_VALUE; | 
 | 6795 |         } else { | 
 | 6796 |             reqFormat = (audio_format_t) value; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6797 |             reconfig = true; | 
 | 6798 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6799 |     } | 
 | 6800 |     if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
 | 6801 |         audio_channel_mask_t mask = (audio_channel_mask_t) value; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6802 |         if (!audio_is_input_channel(mask) || | 
 | 6803 |                 audio_channel_count_from_in_mask(mask) > FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6804 |             status = BAD_VALUE; | 
 | 6805 |         } else { | 
 | 6806 |             channelMask = mask; | 
 | 6807 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6808 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6809 |     } | 
 | 6810 |     if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 6811 |         // do not accept frame count changes if tracks are open as the track buffer | 
 | 6812 |         // size depends on frame count and correct behavior would not be guaranteed | 
 | 6813 |         // if frame count is changed after track creation | 
 | 6814 |         if (mActiveTracks.size() > 0) { | 
 | 6815 |             status = INVALID_OPERATION; | 
 | 6816 |         } else { | 
 | 6817 |             reconfig = true; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6818 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6819 |     } | 
 | 6820 |     if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 6821 |         // forward device change to effects that have requested to be | 
 | 6822 |         // aware of attached audio device. | 
 | 6823 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 6824 |             mEffectChains[i]->setDevice_l(value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6825 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6826 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6827 |         // store input device and output device but do not forward output device to audio HAL. | 
 | 6828 |         // Note that status is ignored by the caller for output device | 
 | 6829 |         // (see AudioFlinger::setParameters() | 
 | 6830 |         if (audio_is_output_devices(value)) { | 
 | 6831 |             mOutDevice = value; | 
 | 6832 |             status = BAD_VALUE; | 
 | 6833 |         } else { | 
 | 6834 |             mInDevice = value; | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 6835 |             if (value != AUDIO_DEVICE_NONE) { | 
 | 6836 |                 mPrevInDevice = value; | 
 | 6837 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6838 |             // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 6839 |             // pre processings | 
 | 6840 |             if (mTracks.size() > 0) { | 
 | 6841 |                 bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 6842 |                                     mAudioFlinger->btNrecIsOff(); | 
 | 6843 |                 for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 6844 |                     sp<RecordTrack> track = mTracks[i]; | 
 | 6845 |                     setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 6846 |                     setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6847 |                 } | 
 | 6848 |             } | 
 | 6849 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6850 |     } | 
 | 6851 |     if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
 | 6852 |             mAudioSource != (audio_source_t)value) { | 
 | 6853 |         // forward device change to effects that have requested to be | 
 | 6854 |         // aware of attached audio device. | 
 | 6855 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 6856 |             mEffectChains[i]->setAudioSource_l((audio_source_t)value); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6857 |         } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6858 |         mAudioSource = (audio_source_t)value; | 
 | 6859 |     } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6860 |  | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6861 |     if (status == NO_ERROR) { | 
 | 6862 |         status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 6863 |                 keyValuePair.string()); | 
 | 6864 |         if (status == INVALID_OPERATION) { | 
 | 6865 |             inputStandBy(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6866 |             status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 6867 |                     keyValuePair.string()); | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6868 |         } | 
 | 6869 |         if (reconfig) { | 
 | 6870 |             if (status == BAD_VALUE && | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6871 |                 audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && | 
 | 6872 |                 audio_is_linear_pcm(reqFormat) && | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6873 |                 (mInput->stream->common.get_sample_rate(&mInput->stream->common) | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6874 |                         <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6875 |                 audio_channel_count_from_in_mask( | 
| Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 6876 |                         mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6877 |                 status = NO_ERROR; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6878 |             } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6879 |             if (status == NO_ERROR) { | 
 | 6880 |                 readInputParameters_l(); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6881 |                 sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6882 |             } | 
 | 6883 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6884 |     } | 
| Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6885 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6886 |     return reconfig; | 
 | 6887 | } | 
 | 6888 |  | 
 | 6889 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
 | 6890 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6891 |     Mutex::Autolock _l(mLock); | 
 | 6892 |     if (initCheck() != NO_ERROR) { | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6893 |         return String8(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6894 |     } | 
 | 6895 |  | 
| Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6896 |     char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); | 
 | 6897 |     const String8 out_s8(s); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6898 |     free(s); | 
 | 6899 |     return out_s8; | 
 | 6900 | } | 
 | 6901 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 6902 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6903 |     sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); | 
 | 6904 |  | 
 | 6905 |     desc->mIoHandle = mId; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6906 |  | 
 | 6907 |     switch (event) { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6908 |     case AUDIO_INPUT_OPENED: | 
 | 6909 |     case AUDIO_INPUT_CONFIG_CHANGED: | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 6910 |         desc->mPatch = mPatch; | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6911 |         desc->mChannelMask = mChannelMask; | 
 | 6912 |         desc->mSamplingRate = mSampleRate; | 
 | 6913 |         desc->mFormat = mFormat; | 
 | 6914 |         desc->mFrameCount = mFrameCount; | 
 | 6915 |         desc->mLatency = 0; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6916 |         break; | 
 | 6917 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6918 |     case AUDIO_INPUT_CLOSED: | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6919 |     default: | 
 | 6920 |         break; | 
 | 6921 |     } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 6922 |     mAudioFlinger->ioConfigChanged(event, desc, pid); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6923 | } | 
 | 6924 |  | 
| Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6925 | void AudioFlinger::RecordThread::readInputParameters_l() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6926 | { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6927 |     mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); | 
 | 6928 |     mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6929 |     mChannelCount = audio_channel_count_from_in_mask(mChannelMask); | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6930 |     if (mChannelCount > FCC_8) { | 
 | 6931 |         ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); | 
 | 6932 |     } | 
| Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 6933 |     mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); | 
 | 6934 |     mFormat = mHALFormat; | 
| Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6935 |     if (!audio_is_linear_pcm(mFormat)) { | 
 | 6936 |         ALOGE("HAL format %#x is not linear pcm", mFormat); | 
| Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6937 |     } | 
| Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 6938 |     mFrameSize = audio_stream_in_frame_size(mInput->stream); | 
| Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 6939 |     mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); | 
 | 6940 |     mFrameCount = mBufferSize / mFrameSize; | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6941 |     // This is the formula for calculating the temporary buffer size. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6942 |     // 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] | 6943 |     // 1 full output buffer, regardless of the alignment of the available input. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6944 |     // The value is somewhat arbitrary, and could probably be even larger. | 
| Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6945 |     // A larger value should allow more old data to be read after a track calls start(), | 
 | 6946 |     // without increasing latency. | 
| Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6947 |     // | 
 | 6948 |     // Note this is independent of the maximum downsampling ratio permitted for capture. | 
| Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6949 |     mRsmpInFrames = mFrameCount * 7; | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6950 |     mRsmpInFramesP2 = roundup(mRsmpInFrames); | 
| Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6951 |     free(mRsmpInBuffer); | 
| Andy Hung | 4c6e77f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 6952 |     mRsmpInBuffer = NULL; | 
| Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6953 |  | 
 | 6954 |     // TODO optimize audio capture buffer sizes ... | 
 | 6955 |     // Here we calculate the size of the sliding buffer used as a source | 
 | 6956 |     // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7). | 
 | 6957 |     // For current HAL frame counts, this is usually 2048 = 40 ms.  It would | 
 | 6958 |     // be better to have it derived from the pipe depth in the long term. | 
 | 6959 |     // The current value is higher than necessary.  However it should not add to latency. | 
 | 6960 |  | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6961 |     // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer | 
| Andy Hung | 4c6e77f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 6962 |     size_t bufferSize = (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize; | 
 | 6963 |     (void)posix_memalign(&mRsmpInBuffer, 32, bufferSize); | 
 | 6964 |     memset(mRsmpInBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6965 |  | 
| Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6966 |     // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. | 
 | 6967 |     // 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] | 6968 | } | 
 | 6969 |  | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 6970 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6971 | { | 
 | 6972 |     Mutex::Autolock _l(mLock); | 
 | 6973 |     if (initCheck() != NO_ERROR) { | 
 | 6974 |         return 0; | 
 | 6975 |     } | 
 | 6976 |  | 
 | 6977 |     return mInput->stream->get_input_frames_lost(mInput->stream); | 
 | 6978 | } | 
 | 6979 |  | 
 | 6980 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const | 
 | 6981 | { | 
 | 6982 |     Mutex::Autolock _l(mLock); | 
 | 6983 |     uint32_t result = 0; | 
 | 6984 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 6985 |         result = EFFECT_SESSION; | 
 | 6986 |     } | 
 | 6987 |  | 
 | 6988 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 6989 |         if (sessionId == mTracks[i]->sessionId()) { | 
 | 6990 |             result |= TRACK_SESSION; | 
 | 6991 |             break; | 
 | 6992 |         } | 
 | 6993 |     } | 
 | 6994 |  | 
 | 6995 |     return result; | 
 | 6996 | } | 
 | 6997 |  | 
 | 6998 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const | 
 | 6999 | { | 
 | 7000 |     KeyedVector<int, bool> ids; | 
 | 7001 |     Mutex::Autolock _l(mLock); | 
 | 7002 |     for (size_t j = 0; j < mTracks.size(); ++j) { | 
 | 7003 |         sp<RecordThread::RecordTrack> track = mTracks[j]; | 
 | 7004 |         int sessionId = track->sessionId(); | 
 | 7005 |         if (ids.indexOfKey(sessionId) < 0) { | 
 | 7006 |             ids.add(sessionId, true); | 
 | 7007 |         } | 
 | 7008 |     } | 
 | 7009 |     return ids; | 
 | 7010 | } | 
 | 7011 |  | 
 | 7012 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
 | 7013 | { | 
 | 7014 |     Mutex::Autolock _l(mLock); | 
 | 7015 |     AudioStreamIn *input = mInput; | 
 | 7016 |     mInput = NULL; | 
 | 7017 |     return input; | 
 | 7018 | } | 
 | 7019 |  | 
 | 7020 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
 | 7021 | audio_stream_t* AudioFlinger::RecordThread::stream() const | 
 | 7022 | { | 
 | 7023 |     if (mInput == NULL) { | 
 | 7024 |         return NULL; | 
 | 7025 |     } | 
 | 7026 |     return &mInput->stream->common; | 
 | 7027 | } | 
 | 7028 |  | 
 | 7029 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 7030 | { | 
 | 7031 |     // only one chain per input thread | 
 | 7032 |     if (mEffectChains.size() != 0) { | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7033 |         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] | 7034 |         return INVALID_OPERATION; | 
 | 7035 |     } | 
 | 7036 |     ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7037 |     chain->setThread(this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7038 |     chain->setInBuffer(NULL); | 
 | 7039 |     chain->setOutBuffer(NULL); | 
 | 7040 |  | 
 | 7041 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 7042 |  | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7043 |     // make sure enabled pre processing effects state is communicated to the HAL as we | 
 | 7044 |     // just moved them to a new input stream. | 
 | 7045 |     chain->syncHalEffectsState(); | 
 | 7046 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7047 |     mEffectChains.add(chain); | 
 | 7048 |  | 
 | 7049 |     return NO_ERROR; | 
 | 7050 | } | 
 | 7051 |  | 
 | 7052 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 7053 | { | 
 | 7054 |     ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
 | 7055 |     ALOGW_IF(mEffectChains.size() != 1, | 
 | 7056 |             "removeEffectChain_l() %p invalid chain size %d on thread %p", | 
 | 7057 |             chain.get(), mEffectChains.size(), this); | 
 | 7058 |     if (mEffectChains.size() == 1) { | 
 | 7059 |         mEffectChains.removeAt(0); | 
 | 7060 |     } | 
 | 7061 |     return 0; | 
 | 7062 | } | 
 | 7063 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7064 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, | 
 | 7065 |                                                           audio_patch_handle_t *handle) | 
 | 7066 | { | 
 | 7067 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7068 |  | 
 | 7069 |     // store new device and send to effects | 
 | 7070 |     mInDevice = patch->sources[0].ext.device.type; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7071 |     mPatch = *patch; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7072 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 7073 |         mEffectChains[i]->setDevice_l(mInDevice); | 
 | 7074 |     } | 
 | 7075 |  | 
 | 7076 |     // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 7077 |     // pre processings | 
 | 7078 |     if (mTracks.size() > 0) { | 
 | 7079 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 7080 |                             mAudioFlinger->btNrecIsOff(); | 
 | 7081 |         for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 7082 |             sp<RecordTrack> track = mTracks[i]; | 
 | 7083 |             setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 7084 |             setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
 | 7085 |         } | 
 | 7086 |     } | 
 | 7087 |  | 
 | 7088 |     // store new source and send to effects | 
 | 7089 |     if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { | 
 | 7090 |         mAudioSource = patch->sinks[0].ext.mix.usecase.source; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7091 |         for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7092 |             mEffectChains[i]->setAudioSource_l(mAudioSource); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7093 |         } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7094 |     } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7095 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7096 |     if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7097 |         audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 7098 |         status = hwDevice->create_audio_patch(hwDevice, | 
 | 7099 |                                                patch->num_sources, | 
 | 7100 |                                                patch->sources, | 
 | 7101 |                                                patch->num_sinks, | 
 | 7102 |                                                patch->sinks, | 
 | 7103 |                                                handle); | 
 | 7104 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7105 |         char *address; | 
 | 7106 |         if (strcmp(patch->sources[0].ext.device.address, "") != 0) { | 
 | 7107 |             address = audio_device_address_to_parameter( | 
 | 7108 |                                                 patch->sources[0].ext.device.type, | 
 | 7109 |                                                 patch->sources[0].ext.device.address); | 
 | 7110 |         } else { | 
 | 7111 |             address = (char *)calloc(1, 1); | 
 | 7112 |         } | 
 | 7113 |         AudioParameter param = AudioParameter(String8(address)); | 
 | 7114 |         free(address); | 
 | 7115 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), | 
 | 7116 |                      (int)patch->sources[0].ext.device.type); | 
 | 7117 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), | 
 | 7118 |                                          (int)patch->sinks[0].ext.mix.usecase.source); | 
 | 7119 |         status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 7120 |                 param.toString().string()); | 
 | 7121 |         *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7122 |     } | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7123 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7124 |     if (mInDevice != mPrevInDevice) { | 
 | 7125 |         sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); | 
 | 7126 |         mPrevInDevice = mInDevice; | 
 | 7127 |     } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7128 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7129 |     return status; | 
 | 7130 | } | 
 | 7131 |  | 
 | 7132 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) | 
 | 7133 | { | 
 | 7134 |     status_t status = NO_ERROR; | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7135 |  | 
 | 7136 |     mInDevice = AUDIO_DEVICE_NONE; | 
 | 7137 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7138 |     if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { | 
 | 7139 |         audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); | 
 | 7140 |         status = hwDevice->release_audio_patch(hwDevice, handle); | 
 | 7141 |     } else { | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7142 |         AudioParameter param; | 
 | 7143 |         param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); | 
 | 7144 |         status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 7145 |                 param.toString().string()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7146 |     } | 
 | 7147 |     return status; | 
 | 7148 | } | 
 | 7149 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7150 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) | 
 | 7151 | { | 
 | 7152 |     Mutex::Autolock _l(mLock); | 
 | 7153 |     mTracks.add(record); | 
 | 7154 | } | 
 | 7155 |  | 
 | 7156 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) | 
 | 7157 | { | 
 | 7158 |     Mutex::Autolock _l(mLock); | 
 | 7159 |     destroyTrack_l(record); | 
 | 7160 | } | 
 | 7161 |  | 
 | 7162 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) | 
 | 7163 | { | 
 | 7164 |     ThreadBase::getAudioPortConfig(config); | 
 | 7165 |     config->role = AUDIO_PORT_ROLE_SINK; | 
 | 7166 |     config->ext.mix.hw_module = mInput->audioHwDev->handle(); | 
 | 7167 |     config->ext.mix.usecase.source = mAudioSource; | 
 | 7168 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7169 |  | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7170 | } // namespace android |