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> |
Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 26 | #include <memory> |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 27 | #include <sstream> |
Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 28 | #include <string> |
Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 29 | #include <linux/futex.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 31 | #include <sys/syscall.h> |
Glenn Kasten | c9a2367 | 2020-06-30 12:12:42 -0700 | [diff] [blame] | 32 | #include <cutils/bitops.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 33 | #include <cutils/properties.h> |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 34 | #include <media/AudioContainers.h> |
| 35 | #include <media/AudioDeviceTypeAddr.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 36 | #include <media/AudioParameter.h> |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 37 | #include <media/AudioResamplerPublic.h> |
Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 38 | #include <media/RecordBufferConverter.h> |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 39 | #include <media/TypeConverter.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | #include <utils/Log.h> |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 41 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 42 | |
| 43 | #include <private/media/AudioTrackShared.h> |
Wei Jia | f2ae3e1 | 2016-10-27 17:10:59 -0700 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 45 | #include <audio_utils/Balance.h> |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 46 | #include <audio_utils/Metadata.h> |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 47 | #include <audio_utils/channels.h> |
Glenn Kasten | 6bf707f | 2017-02-23 16:53:58 -0800 | [diff] [blame] | 48 | #include <audio_utils/mono_blend.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 49 | #include <audio_utils/primitives.h> |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 50 | #include <audio_utils/format.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 51 | #include <audio_utils/minifloat.h> |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 52 | #include <audio_utils/safe_math.h> |
Mikhail Naganov | 9fe9401 | 2016-10-14 14:57:40 -0700 | [diff] [blame] | 53 | #include <system/audio_effects/effect_ns.h> |
| 54 | #include <system/audio_effects/effect_aec.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 55 | #include <system/audio.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 56 | |
| 57 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 58 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | #include <media/nbaio/AudioStreamOutSink.h> |
| 60 | #include <media/nbaio/MonoPipe.h> |
| 61 | #include <media/nbaio/MonoPipeReader.h> |
| 62 | #include <media/nbaio/Pipe.h> |
| 63 | #include <media/nbaio/PipeReader.h> |
| 64 | #include <media/nbaio/SourceAudioBufferProvider.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 65 | #include <mediautils/BatteryNotifier.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 66 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 67 | #include <audiomanager/AudioManager.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 68 | #include <powermanager/PowerManager.h> |
| 69 | |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 70 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 71 | #include <media/audiohal/StreamHalInterface.h> |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 72 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 73 | #include "AudioFlinger.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 74 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 75 | #include "FastCapture.h" |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 76 | #include <mediautils/SchedulingPolicyService.h> |
| 77 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 78 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 79 | #ifdef ADD_BATTERY_DATA |
| 80 | #include <media/IMediaPlayerService.h> |
| 81 | #include <media/IMediaDeathNotifier.h> |
| 82 | #endif |
| 83 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 84 | #ifdef DEBUG_CPU_USAGE |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 85 | #include <audio_utils/Statistics.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 86 | #include <cpustats/ThreadCpuUsage.h> |
| 87 | #endif |
| 88 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 89 | #include "AutoPark.h" |
| 90 | |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 91 | #include <pthread.h> |
| 92 | #include "TypedLogger.h" |
| 93 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 94 | // ---------------------------------------------------------------------------- |
| 95 | |
| 96 | // Note: the following macro is used for extremely verbose logging message. In |
| 97 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 98 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 99 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 100 | // turned on. Do not uncomment the #def below unless you really know what you |
| 101 | // are doing and want to see all of the extremely verbose messages. |
| 102 | //#define VERY_VERY_VERBOSE_LOGGING |
| 103 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 104 | #define ALOGVV ALOGV |
| 105 | #else |
| 106 | #define ALOGVV(a...) do { } while(0) |
| 107 | #endif |
| 108 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 109 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 110 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 111 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 112 | template <typename T> |
| 113 | static inline T min(const T& a, const T& b) |
| 114 | { |
| 115 | return a < b ? a : b; |
| 116 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 117 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 118 | namespace android { |
| 119 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 120 | using media::IEffectClient; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 121 | using media::permission::Identity; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 122 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 123 | // retry counts for buffer fill timeout |
| 124 | // 50 * ~20msecs = 1 second |
| 125 | static const int8_t kMaxTrackRetries = 50; |
| 126 | static const int8_t kMaxTrackStartupRetries = 50; |
| 127 | // allow less retry attempts on direct output thread. |
| 128 | // direct outputs can be a scarce resource in audio hardware and should |
| 129 | // be released as quickly as possible. |
| 130 | static const int8_t kMaxTrackRetriesDirect = 2; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 131 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 132 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 133 | |
| 134 | // don't warn about blocked writes or record buffer overflows more often than this |
| 135 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 136 | |
| 137 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 138 | static const int kRecordThreadSleepUs = 5000; |
| 139 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 140 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 141 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 142 | |
| 143 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 144 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 145 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 146 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 147 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 148 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 149 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 150 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 151 | // maximum normal sink buffer size |
| 152 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 153 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 154 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 155 | // FIXME This should be based on experimentally observed scheduling jitter |
| 156 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 157 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 158 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 159 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 160 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 161 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 162 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 163 | |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 164 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good |
| 165 | // balance between power consumption and latency, and allows threads to be scheduled reliably |
| 166 | // by the CFS scheduler. |
| 167 | // FIXME Express other hardcoded references to 20ms with references to this constant and move |
| 168 | // it appropriately. |
| 169 | #define FMS_20 20 |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 170 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 171 | // Whether to use fast mixer |
| 172 | static const enum { |
| 173 | FastMixer_Never, // never initialize or use: for debugging only |
| 174 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 175 | // normal mixer multiplier is 1 |
| 176 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 177 | // multiplier is calculated based on min & max normal mixer buffer size |
| 178 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 179 | // multiplier is calculated based on min & max normal mixer buffer size |
| 180 | // FIXME for FastMixer_Dynamic: |
| 181 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 182 | // For example, one HAL implementation returns an error from a large write, |
| 183 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 184 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 185 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 186 | } kUseFastMixer = FastMixer_Static; |
| 187 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 188 | // Whether to use fast capture |
| 189 | static const enum { |
| 190 | FastCapture_Never, // never initialize or use: for debugging only |
| 191 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 192 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 193 | } kUseFastCapture = FastCapture_Static; |
| 194 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 195 | // Priorities for requestPriority |
| 196 | static const int kPriorityAudioApp = 2; |
| 197 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 198 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 199 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 200 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 201 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 202 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 203 | |
| 204 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 205 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 206 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 207 | // The minimum and maximum allowed values |
| 208 | static const int kFastTrackMultiplierMin = 1; |
| 209 | static const int kFastTrackMultiplierMax = 2; |
| 210 | |
| 211 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 212 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 213 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 214 | // See Thread::readOnlyHeap(). |
| 215 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 216 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 217 | // and that all "fast" AudioRecord clients read from. In either case, the size can be small. |
Mikhail Naganov | 79a7782 | 2018-05-10 15:57:25 -0700 | [diff] [blame] | 218 | static const size_t kRecordThreadReadOnlyHeapSize = 0xD000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 219 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 220 | // ---------------------------------------------------------------------------- |
| 221 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 222 | // TODO: move all toString helpers to audio.h |
| 223 | // under #ifdef __cplusplus #endif |
| 224 | static std::string patchSinksToString(const struct audio_patch *patch) |
| 225 | { |
| 226 | std::stringstream ss; |
| 227 | for (size_t i = 0; i < patch->num_sinks; ++i) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 228 | if (i > 0) { |
| 229 | ss << "|"; |
| 230 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 231 | ss << "(" << toString(patch->sinks[i].ext.device.type) |
| 232 | << ", " << patch->sinks[i].ext.device.address << ")"; |
| 233 | } |
| 234 | return ss.str(); |
| 235 | } |
| 236 | |
| 237 | static std::string patchSourcesToString(const struct audio_patch *patch) |
| 238 | { |
| 239 | std::stringstream ss; |
| 240 | for (size_t i = 0; i < patch->num_sources; ++i) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 241 | if (i > 0) { |
| 242 | ss << "|"; |
| 243 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 244 | ss << "(" << toString(patch->sources[i].ext.device.type) |
| 245 | << ", " << patch->sources[i].ext.device.address << ")"; |
| 246 | } |
| 247 | return ss.str(); |
| 248 | } |
| 249 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 250 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 251 | |
| 252 | static void sFastTrackMultiplierInit() |
| 253 | { |
| 254 | char value[PROPERTY_VALUE_MAX]; |
| 255 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 256 | char *endptr; |
| 257 | unsigned long ul = strtoul(value, &endptr, 0); |
| 258 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 259 | sFastTrackMultiplier = (int) ul; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // ---------------------------------------------------------------------------- |
| 265 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 266 | #ifdef ADD_BATTERY_DATA |
| 267 | // To collect the amplifier usage |
| 268 | static void addBatteryData(uint32_t params) { |
| 269 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 270 | if (service == NULL) { |
| 271 | // it already logged |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | service->addBatteryData(params); |
| 276 | } |
| 277 | #endif |
| 278 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 279 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 280 | struct { |
| 281 | // call when you acquire a partial wakelock |
| 282 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 283 | pthread_mutex_lock(&mLock); |
| 284 | if (wakeLockToken.get() == nullptr) { |
| 285 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 286 | } else { |
| 287 | if (mCount == 0) { |
| 288 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 289 | } |
| 290 | ++mCount; |
| 291 | } |
| 292 | pthread_mutex_unlock(&mLock); |
| 293 | } |
| 294 | |
| 295 | // call when you release a partial wakelock. |
| 296 | void release(const sp<IBinder> &wakeLockToken) { |
| 297 | if (wakeLockToken.get() == nullptr) { |
| 298 | return; |
| 299 | } |
| 300 | pthread_mutex_lock(&mLock); |
| 301 | if (--mCount < 0) { |
| 302 | ALOGE("negative wakelock count"); |
| 303 | mCount = 0; |
| 304 | } |
| 305 | pthread_mutex_unlock(&mLock); |
| 306 | } |
| 307 | |
| 308 | // retrieves the boottime timebase offset from monotonic. |
| 309 | int64_t getBoottimeOffset() { |
| 310 | pthread_mutex_lock(&mLock); |
| 311 | int64_t boottimeOffset = mBoottimeOffset; |
| 312 | pthread_mutex_unlock(&mLock); |
| 313 | return boottimeOffset; |
| 314 | } |
| 315 | |
| 316 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 317 | // and the selected timebase. |
| 318 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 319 | // |
| 320 | // This only needs to be called upon acquiring the first partial wakelock |
| 321 | // after all other partial wakelocks are released. |
| 322 | // |
| 323 | // We do an empirical measurement of the offset rather than parsing |
| 324 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 325 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 326 | int clockbase; |
| 327 | switch (timebase) { |
| 328 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 329 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 330 | break; |
| 331 | default: |
| 332 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 333 | break; |
| 334 | } |
| 335 | // try three times to get the clock offset, choose the one |
| 336 | // with the minimum gap in measurements. |
| 337 | const int tries = 3; |
| 338 | nsecs_t bestGap, measured; |
| 339 | for (int i = 0; i < tries; ++i) { |
| 340 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 341 | const nsecs_t tbase = systemTime(clockbase); |
| 342 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 343 | const nsecs_t gap = tmono2 - tmono; |
| 344 | if (i == 0 || gap < bestGap) { |
| 345 | bestGap = gap; |
| 346 | measured = tbase - ((tmono + tmono2) >> 1); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // to avoid micro-adjusting, we don't change the timebase |
| 351 | // unless it is significantly different. |
| 352 | // |
| 353 | // Assumption: It probably takes more than toleranceNs to |
| 354 | // suspend and resume the device. |
| 355 | static int64_t toleranceNs = 10000; // 10 us |
| 356 | if (llabs(*offset - measured) > toleranceNs) { |
| 357 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 358 | (long long)*offset, (long long)measured); |
| 359 | *offset = measured; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | pthread_mutex_t mLock; |
| 364 | int32_t mCount; |
| 365 | int64_t mBoottimeOffset; |
| 366 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 367 | |
| 368 | // ---------------------------------------------------------------------------- |
| 369 | // CPU Stats |
| 370 | // ---------------------------------------------------------------------------- |
| 371 | |
| 372 | class CpuStats { |
| 373 | public: |
| 374 | CpuStats(); |
| 375 | void sample(const String8 &title); |
| 376 | #ifdef DEBUG_CPU_USAGE |
| 377 | private: |
| 378 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 379 | audio_utils::Statistics<double> mWcStats; // statistics on thread CPU usage in wall clock ns |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 380 | |
Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 381 | audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 382 | |
| 383 | int mCpuNum; // thread's current CPU number |
| 384 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 385 | #endif |
| 386 | }; |
| 387 | |
| 388 | CpuStats::CpuStats() |
| 389 | #ifdef DEBUG_CPU_USAGE |
| 390 | : mCpuNum(-1), mCpukHz(-1) |
| 391 | #endif |
| 392 | { |
| 393 | } |
| 394 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 395 | void CpuStats::sample(const String8 &title |
| 396 | #ifndef DEBUG_CPU_USAGE |
| 397 | __unused |
| 398 | #endif |
| 399 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 400 | #ifdef DEBUG_CPU_USAGE |
| 401 | // get current thread's delta CPU time in wall clock ns |
| 402 | double wcNs; |
| 403 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 404 | |
| 405 | // record sample for wall clock statistics |
| 406 | if (valid) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 407 | mWcStats.add(wcNs); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // get the current CPU number |
| 411 | int cpuNum = sched_getcpu(); |
| 412 | |
| 413 | // get the current CPU frequency in kHz |
| 414 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 415 | |
| 416 | // check if either CPU number or frequency changed |
| 417 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 418 | mCpuNum = cpuNum; |
| 419 | mCpukHz = cpukHz; |
| 420 | // ignore sample for purposes of cycles |
| 421 | valid = false; |
| 422 | } |
| 423 | |
| 424 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 425 | if (valid && mCpukHz > 0) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 426 | const double cycles = wcNs * cpukHz * 0.000001; |
| 427 | mHzStats.add(cycles); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 430 | const unsigned n = mWcStats.getN(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 431 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 432 | if ((n & 127) == 1) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 433 | const long long elapsed = mCpuUsage.elapsed(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 434 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 435 | const double perLoop = elapsed / (double) n; |
| 436 | const double perLoop100 = perLoop * 0.01; |
| 437 | const double perLoop1k = perLoop * 0.001; |
| 438 | const double mean = mWcStats.getMean(); |
| 439 | const double stddev = mWcStats.getStdDev(); |
| 440 | const double minimum = mWcStats.getMin(); |
| 441 | const double maximum = mWcStats.getMax(); |
| 442 | const double meanCycles = mHzStats.getMean(); |
| 443 | const double stddevCycles = mHzStats.getStdDev(); |
| 444 | const double minCycles = mHzStats.getMin(); |
| 445 | const double maxCycles = mHzStats.getMax(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 446 | mCpuUsage.resetElapsed(); |
| 447 | mWcStats.reset(); |
| 448 | mHzStats.reset(); |
| 449 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 450 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 451 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 452 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 453 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 454 | title.string(), |
| 455 | elapsed * .000000001, n, perLoop * .000001, |
| 456 | mean * .001, |
| 457 | stddev * .001, |
| 458 | minimum * .001, |
| 459 | maximum * .001, |
| 460 | mean / perLoop100, |
| 461 | stddev / perLoop100, |
| 462 | minimum / perLoop100, |
| 463 | maximum / perLoop100, |
| 464 | meanCycles / perLoop1k, |
| 465 | stddevCycles / perLoop1k, |
| 466 | minCycles / perLoop1k, |
| 467 | maxCycles / perLoop1k); |
| 468 | |
| 469 | } |
| 470 | } |
| 471 | #endif |
| 472 | }; |
| 473 | |
| 474 | // ---------------------------------------------------------------------------- |
| 475 | // ThreadBase |
| 476 | // ---------------------------------------------------------------------------- |
| 477 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 478 | // static |
| 479 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 480 | { |
| 481 | switch (type) { |
| 482 | case MIXER: |
| 483 | return "MIXER"; |
| 484 | case DIRECT: |
| 485 | return "DIRECT"; |
| 486 | case DUPLICATING: |
| 487 | return "DUPLICATING"; |
| 488 | case RECORD: |
| 489 | return "RECORD"; |
| 490 | case OFFLOAD: |
| 491 | return "OFFLOAD"; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 492 | case MMAP_PLAYBACK: |
| 493 | return "MMAP_PLAYBACK"; |
| 494 | case MMAP_CAPTURE: |
| 495 | return "MMAP_CAPTURE"; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 496 | default: |
| 497 | return "unknown"; |
| 498 | } |
| 499 | } |
| 500 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 501 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 502 | type_t type, bool systemReady, bool isOut) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 503 | : Thread(false /*canCallJava*/), |
| 504 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 505 | mAudioFlinger(audioFlinger), |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 506 | mThreadMetrics(std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) + std::to_string(id), |
| 507 | isOut), |
| 508 | mIsOut(isOut), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 509 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 510 | // are set by PlaybackThread::readOutputParameters_l() or |
| 511 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 512 | //FIXME: mStandby should be true here. Is this some kind of hack? |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 513 | mStandby(false), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 514 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 515 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 516 | mDeathRecipient(new PMDeathRecipient(this)), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 517 | mSystemReady(systemReady), |
| 518 | mSignalPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 519 | { |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 520 | mThreadMetrics.logConstructor(getpid(), threadTypeToString(type), id); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 521 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | AudioFlinger::ThreadBase::~ThreadBase() |
| 525 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 526 | // 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] | 527 | mConfigEvents.clear(); |
| 528 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 529 | // do not lock the mutex in destructor |
| 530 | releaseWakeLock_l(); |
| 531 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 532 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 533 | binder->unlinkToDeath(mDeathRecipient); |
| 534 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 535 | |
| 536 | sendStatistics(true /* force */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 539 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 540 | { |
| 541 | status_t status = initCheck(); |
| 542 | if (status == NO_ERROR) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 543 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 544 | } else { |
| 545 | ALOGE("No working audio driver found."); |
| 546 | } |
| 547 | return status; |
| 548 | } |
| 549 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 550 | void AudioFlinger::ThreadBase::exit() |
| 551 | { |
| 552 | ALOGV("ThreadBase::exit"); |
| 553 | // do any cleanup required for exit to succeed |
| 554 | preExit(); |
| 555 | { |
| 556 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 557 | // if (!exitPending()) { |
| 558 | // // context switch from here to exit() |
| 559 | // // exit() calls requestExit(), what exitPending() observes |
| 560 | // // exit() calls signal(), which is dropped since no waiters |
| 561 | // // context switch back from exit() to here |
| 562 | // mWaitWorkCV.wait(...); |
| 563 | // // now thread is hung |
| 564 | // } |
| 565 | AutoMutex lock(mLock); |
| 566 | requestExit(); |
| 567 | mWaitWorkCV.broadcast(); |
| 568 | } |
| 569 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 570 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 571 | requestExitAndWait(); |
| 572 | } |
| 573 | |
| 574 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 575 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 576 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 577 | Mutex::Autolock _l(mLock); |
| 578 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 579 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 580 | } |
| 581 | |
| 582 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 583 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 584 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 585 | { |
| 586 | status_t status = NO_ERROR; |
| 587 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 588 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 589 | event->mWaitStatus = false; |
| 590 | mPendingConfigEvents.add(event); |
| 591 | return status; |
| 592 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 593 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 594 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 595 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 596 | mLock.unlock(); |
| 597 | { |
| 598 | Mutex::Autolock _l(event->mLock); |
| 599 | while (event->mWaitStatus) { |
| 600 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 601 | event->mStatus = TIMED_OUT; |
| 602 | event->mWaitStatus = false; |
| 603 | } |
| 604 | } |
| 605 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 606 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 607 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 608 | return status; |
| 609 | } |
| 610 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 611 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid, |
| 612 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 613 | { |
| 614 | Mutex::Autolock _l(mLock); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 615 | sendIoConfigEvent_l(event, pid, portId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 619 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid, |
| 620 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 621 | { |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 622 | // The audio statistics history is exponentially weighted to forget events |
| 623 | // about five or more seconds in the past. In order to have |
| 624 | // crisper statistics for mediametrics, we reset the statistics on |
| 625 | // an IoConfigEvent, to reflect different properties for a new device. |
| 626 | mIoJitterMs.reset(); |
| 627 | mLatencyMs.reset(); |
| 628 | mProcessTimeMs.reset(); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 629 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 630 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 631 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 632 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 635 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 636 | { |
| 637 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 638 | sendPrioConfigEvent_l(pid, tid, prio, forApp); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 642 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l( |
| 643 | pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 644 | { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 645 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); |
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 | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 649 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 650 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 651 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 652 | sp<ConfigEvent> configEvent; |
| 653 | AudioParameter param(keyValuePair); |
| 654 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 655 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 656 | setMasterMono_l(value != 0); |
| 657 | if (param.size() == 1) { |
| 658 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 659 | } |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 660 | param.remove(String8(AudioParameter::keyMonoOutput)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 661 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 662 | } else { |
| 663 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 664 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 665 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 668 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 669 | const struct audio_patch *patch, |
| 670 | audio_patch_handle_t *handle) |
| 671 | { |
| 672 | Mutex::Autolock _l(mLock); |
| 673 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 674 | status_t status = sendConfigEvent_l(configEvent); |
| 675 | if (status == NO_ERROR) { |
| 676 | CreateAudioPatchConfigEventData *data = |
| 677 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 678 | *handle = data->mHandle; |
| 679 | } |
| 680 | return status; |
| 681 | } |
| 682 | |
| 683 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 684 | const audio_patch_handle_t handle) |
| 685 | { |
| 686 | Mutex::Autolock _l(mLock); |
| 687 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 688 | return sendConfigEvent_l(configEvent); |
| 689 | } |
| 690 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 691 | status_t AudioFlinger::ThreadBase::sendUpdateOutDeviceConfigEvent( |
| 692 | const DeviceDescriptorBaseVector& outDevices) |
| 693 | { |
| 694 | if (type() != RECORD) { |
| 695 | // The update out device operation is only for record thread. |
| 696 | return INVALID_OPERATION; |
| 697 | } |
| 698 | Mutex::Autolock _l(mLock); |
| 699 | sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices); |
| 700 | return sendConfigEvent_l(configEvent); |
| 701 | } |
| 702 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 703 | void AudioFlinger::ThreadBase::sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) |
| 704 | { |
| 705 | ALOG_ASSERT(type() == RECORD, "sendResizeBufferConfigEvent_l() called on non record thread"); |
| 706 | sp<ConfigEvent> configEvent = |
| 707 | (ConfigEvent *)new ResizeBufferConfigEvent(maxSharedAudioHistoryMs); |
| 708 | sendConfigEvent_l(configEvent); |
| 709 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 710 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 711 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 712 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 713 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 714 | bool configChanged = false; |
| 715 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 716 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 717 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 718 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 719 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 720 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 721 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 722 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 723 | // FIXME Need to understand why this has to be done asynchronously |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 724 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 725 | true /*asynchronous*/); |
| 726 | if (err != 0) { |
| 727 | 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] | 728 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 729 | } |
| 730 | } break; |
| 731 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 732 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 733 | ioConfigChanged(data->mEvent, data->mPid, data->mPortId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 734 | } break; |
| 735 | case CFG_EVENT_SET_PARAMETER: { |
| 736 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 737 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 738 | configChanged = true; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 739 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", |
| 740 | data->mKeyValuePairs.string()); |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 741 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 742 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 743 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 744 | const DeviceTypeSet oldDevices = getDeviceTypes(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 745 | CreateAudioPatchConfigEventData *data = |
| 746 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 747 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 748 | const DeviceTypeSet newDevices = getDeviceTypes(); |
| 749 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", |
| 750 | dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), |
| 751 | dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 752 | } break; |
| 753 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 754 | const DeviceTypeSet oldDevices = getDeviceTypes(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 755 | ReleaseAudioPatchConfigEventData *data = |
| 756 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 757 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 758 | const DeviceTypeSet newDevices = getDeviceTypes(); |
| 759 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", |
| 760 | dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), |
| 761 | dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); |
| 762 | } break; |
| 763 | case CFG_EVENT_UPDATE_OUT_DEVICE: { |
| 764 | UpdateOutDevicesConfigEventData *data = |
| 765 | (UpdateOutDevicesConfigEventData *)event->mData.get(); |
| 766 | updateOutDevices(data->mOutDevices); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 767 | } break; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 768 | case CFG_EVENT_RESIZE_BUFFER: { |
| 769 | ResizeBufferConfigEventData *data = |
| 770 | (ResizeBufferConfigEventData *)event->mData.get(); |
| 771 | resizeInputBuffer_l(data->mMaxSharedAudioHistoryMs); |
| 772 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 773 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 774 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 775 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 776 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 777 | { |
| 778 | Mutex::Autolock _l(event->mLock); |
| 779 | if (event->mWaitStatus) { |
| 780 | event->mWaitStatus = false; |
| 781 | event->mCond.signal(); |
| 782 | } |
| 783 | } |
| 784 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 785 | } |
| 786 | |
| 787 | if (configChanged) { |
| 788 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 789 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 792 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 793 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 794 | const audio_channel_representation_t representation = |
| 795 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 796 | |
| 797 | switch (representation) { |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 798 | // Travel all single bit channel mask to convert channel mask to string. |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 799 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 800 | if (output) { |
| 801 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 802 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 803 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 804 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 805 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 806 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 807 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 808 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 809 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 810 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 811 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 812 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 813 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 814 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 815 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 816 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 817 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 818 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
Mikhail Naganov | c994849 | 2018-05-10 17:25:28 -0700 | [diff] [blame] | 819 | if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, " ); |
| 820 | if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, " ); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 821 | if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, " ); |
| 822 | if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, " ); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 823 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 824 | } else { |
| 825 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 826 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 827 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 828 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 829 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 830 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 831 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 832 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 833 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 834 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 835 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 836 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
Mikhail Naganov | c994849 | 2018-05-10 17:25:28 -0700 | [diff] [blame] | 837 | if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, "); |
| 838 | if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, "); |
| 839 | if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, "); |
| 840 | if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low freq, "); |
| 841 | if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, " ); |
| 842 | if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, " ); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 843 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 844 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 845 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 846 | } |
| 847 | const int len = s.length(); |
| 848 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 849 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 850 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 851 | } |
| 852 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 853 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 854 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 855 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 856 | return s; |
| 857 | default: |
| 858 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 859 | representation, audio_channel_mask_get_bits(mask)); |
| 860 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 861 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 864 | void AudioFlinger::ThreadBase::dump(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 865 | { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 866 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", |
| 867 | this, mThreadName, getTid(), type(), threadTypeToString(type())); |
| 868 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 869 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 870 | if (!locked) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 871 | dprintf(fd, " Thread may be deadlocked\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 874 | dumpBase_l(fd, args); |
| 875 | dumpInternals_l(fd, args); |
| 876 | dumpTracks_l(fd, args); |
| 877 | dumpEffectChains_l(fd, args); |
| 878 | |
| 879 | if (locked) { |
| 880 | mLock.unlock(); |
| 881 | } |
| 882 | |
| 883 | dprintf(fd, " Local log:\n"); |
| 884 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
| 885 | } |
| 886 | |
| 887 | void AudioFlinger::ThreadBase::dumpBase_l(int fd, const Vector<String16>& args __unused) |
| 888 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 889 | dprintf(fd, " I/O handle: %d\n", mId); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 890 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 891 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 892 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 893 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat).c_str()); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 894 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 895 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 896 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 897 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 898 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat).c_str()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 899 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 900 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 901 | size_t numConfig = mConfigEvents.size(); |
| 902 | if (numConfig) { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 903 | const size_t SIZE = 256; |
| 904 | char buffer[SIZE]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 905 | for (size_t i = 0; i < numConfig; i++) { |
| 906 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 907 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 908 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 909 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 910 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 911 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 912 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 913 | // Note: output device may be used by capture threads for effects such as AEC. |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 914 | dprintf(fd, " Output devices: %s (%s)\n", |
| 915 | dumpDeviceTypes(outDeviceTypes()).c_str(), toString(outDeviceTypes()).c_str()); |
| 916 | dprintf(fd, " Input device: %#x (%s)\n", |
| 917 | inDeviceType(), toString(inDeviceType()).c_str()); |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 918 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 919 | |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 920 | // Dump timestamp statistics for the Thread types that support it. |
| 921 | if (mType == RECORD |
| 922 | || mType == MIXER |
| 923 | || mType == DUPLICATING |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 924 | || mType == DIRECT |
| 925 | || mType == OFFLOAD) { |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 926 | dprintf(fd, " Timestamp stats: %s\n", mTimestampVerifier.toString().c_str()); |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 927 | dprintf(fd, " Timestamp corrected: %s\n", isTimestampCorrectionEnabled() ? "yes" : "no"); |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 930 | if (mLastIoBeginNs > 0) { // MMAP may not set this |
| 931 | dprintf(fd, " Last %s occurred (msecs): %lld\n", |
| 932 | isOutput() ? "write" : "read", |
| 933 | (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND); |
| 934 | } |
| 935 | |
| 936 | if (mProcessTimeMs.getN() > 0) { |
| 937 | dprintf(fd, " Process time ms stats: %s\n", mProcessTimeMs.toString().c_str()); |
| 938 | } |
| 939 | |
| 940 | if (mIoJitterMs.getN() > 0) { |
| 941 | dprintf(fd, " Hal %s jitter ms stats: %s\n", |
| 942 | isOutput() ? "write" : "read", |
| 943 | mIoJitterMs.toString().c_str()); |
| 944 | } |
| 945 | |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 946 | if (mLatencyMs.getN() > 0) { |
| 947 | dprintf(fd, " Threadloop %s latency stats: %s\n", |
| 948 | isOutput() ? "write" : "read", |
| 949 | mLatencyMs.toString().c_str()); |
| 950 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 953 | void AudioFlinger::ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 954 | { |
| 955 | const size_t SIZE = 256; |
| 956 | char buffer[SIZE]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 957 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 958 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 959 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 960 | write(fd, buffer, strlen(buffer)); |
| 961 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 962 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 963 | sp<EffectChain> chain = mEffectChains[i]; |
| 964 | if (chain != 0) { |
| 965 | chain->dump(fd, args); |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 970 | void AudioFlinger::ThreadBase::acquireWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 971 | { |
| 972 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 973 | acquireWakeLock_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 976 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 977 | { |
| 978 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 979 | case MIXER: |
| 980 | return String16("AudioMix"); |
| 981 | case DIRECT: |
| 982 | return String16("AudioDirectOut"); |
| 983 | case DUPLICATING: |
| 984 | return String16("AudioDup"); |
| 985 | case RECORD: |
| 986 | return String16("AudioIn"); |
| 987 | case OFFLOAD: |
| 988 | return String16("AudioOffload"); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 989 | case MMAP_PLAYBACK: |
| 990 | return String16("MmapPlayback"); |
| 991 | case MMAP_CAPTURE: |
| 992 | return String16("MmapCapture"); |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 993 | default: |
| 994 | ALOG_ASSERT(false); |
| 995 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 999 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1000 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1001 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1002 | if (mPowerManager != 0) { |
| 1003 | sp<IBinder> binder = new BBinder(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1004 | // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids. |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1005 | binder::Status status = mPowerManager->acquireWakeLockAsync(binder, |
| 1006 | POWERMANAGER_PARTIAL_WAKE_LOCK, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1007 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1008 | String16("audioserver"), |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1009 | {} /* workSource */, |
| 1010 | {} /* historyTag */); |
| 1011 | if (status.isOk()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1012 | mWakeLockToken = binder; |
| 1013 | } |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1014 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status.exceptionCode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1015 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1016 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1017 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1018 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 1019 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1023 | { |
| 1024 | Mutex::Autolock _l(mLock); |
| 1025 | releaseWakeLock_l(); |
| 1026 | } |
| 1027 | |
| 1028 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1029 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1030 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1031 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1032 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1033 | if (mPowerManager != 0) { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1034 | mPowerManager->releaseWakeLockAsync(mWakeLockToken, 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1035 | } |
| 1036 | mWakeLockToken.clear(); |
| 1037 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1041 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1042 | // use checkService() to avoid blocking if power service is not up yet |
| 1043 | sp<IBinder> binder = |
| 1044 | defaultServiceManager()->checkService(String16("power")); |
| 1045 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1046 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1047 | } else { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1048 | mPowerManager = interface_cast<os::IPowerManager>(binder); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1049 | binder->linkToDeath(mDeathRecipient); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1054 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1055 | getPowerManager_l(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1056 | |
| 1057 | #if !LOG_NDEBUG |
| 1058 | std::stringstream s; |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1059 | for (uid_t uid : uids) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1060 | s << uid << " "; |
| 1061 | } |
| 1062 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); |
| 1063 | #endif |
| 1064 | |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1065 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1066 | if (mSystemReady) { |
| 1067 | ALOGE("no wake lock to update, but system ready!"); |
| 1068 | } else { |
| 1069 | ALOGW("no wake lock to update, system not ready yet"); |
| 1070 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1071 | return; |
| 1072 | } |
| 1073 | if (mPowerManager != 0) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1074 | std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1075 | binder::Status status = mPowerManager->updateWakeLockUidsAsync( |
| 1076 | mWakeLockToken, uidsAsInt); |
| 1077 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status.exceptionCode()); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1081 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1082 | { |
| 1083 | Mutex::Autolock _l(mLock); |
| 1084 | releaseWakeLock_l(); |
| 1085 | mPowerManager.clear(); |
| 1086 | } |
| 1087 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1088 | void AudioFlinger::ThreadBase::updateOutDevices( |
| 1089 | const DeviceDescriptorBaseVector& outDevices __unused) |
| 1090 | { |
| 1091 | ALOGE("%s should only be called in RecordThread", __func__); |
| 1092 | } |
| 1093 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1094 | void AudioFlinger::ThreadBase::resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs __unused) |
| 1095 | { |
| 1096 | ALOGE("%s should only be called in RecordThread", __func__); |
| 1097 | } |
| 1098 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1099 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1100 | { |
| 1101 | sp<ThreadBase> thread = mThread.promote(); |
| 1102 | if (thread != 0) { |
| 1103 | thread->clearPowerManager(); |
| 1104 | } |
| 1105 | ALOGW("power manager service died !!!"); |
| 1106 | } |
| 1107 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1108 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1109 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1110 | { |
| 1111 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1112 | if (chain != 0) { |
| 1113 | if (type != NULL) { |
| 1114 | chain->setEffectSuspended_l(type, suspend); |
| 1115 | } else { |
| 1116 | chain->setEffectSuspendedAll_l(suspend); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1121 | } |
| 1122 | |
| 1123 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1124 | { |
| 1125 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1126 | if (index < 0) { |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1131 | mSuspendedSessions.valueAt(index); |
| 1132 | |
| 1133 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1134 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1135 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1136 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1137 | chain->setEffectSuspendedAll_l(true); |
| 1138 | } else { |
| 1139 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1140 | desc->mType.timeLow); |
| 1141 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1148 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1149 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1150 | { |
| 1151 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1152 | |
| 1153 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1154 | |
| 1155 | if (suspend) { |
| 1156 | if (index >= 0) { |
| 1157 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1158 | } else { |
| 1159 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1160 | } |
| 1161 | } else { |
| 1162 | if (index < 0) { |
| 1163 | return; |
| 1164 | } |
| 1165 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | int key = EffectChain::kKeyForSuspendAll; |
| 1170 | if (type != NULL) { |
| 1171 | key = type->timeLow; |
| 1172 | } |
| 1173 | index = sessionEffects.indexOfKey(key); |
| 1174 | |
| 1175 | sp<SuspendedSessionDesc> desc; |
| 1176 | if (suspend) { |
| 1177 | if (index >= 0) { |
| 1178 | desc = sessionEffects.valueAt(index); |
| 1179 | } else { |
| 1180 | desc = new SuspendedSessionDesc(); |
| 1181 | if (type != NULL) { |
| 1182 | desc->mType = *type; |
| 1183 | } |
| 1184 | sessionEffects.add(key, desc); |
| 1185 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1186 | } |
| 1187 | desc->mRefCount++; |
| 1188 | } else { |
| 1189 | if (index < 0) { |
| 1190 | return; |
| 1191 | } |
| 1192 | desc = sessionEffects.valueAt(index); |
| 1193 | if (--desc->mRefCount == 0) { |
| 1194 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1195 | sessionEffects.removeItemsAt(index); |
| 1196 | if (sessionEffects.isEmpty()) { |
| 1197 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1198 | sessionId); |
| 1199 | mSuspendedSessions.removeItem(sessionId); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | if (!sessionEffects.isEmpty()) { |
| 1204 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1205 | } |
| 1206 | } |
| 1207 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1208 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(bool enabled, |
| 1209 | audio_session_t sessionId, |
| 1210 | bool threadLocked) { |
| 1211 | if (!threadLocked) { |
| 1212 | mLock.lock(); |
| 1213 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1214 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1215 | if (mType != RECORD) { |
| 1216 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1217 | // another session. This gives the priority to well behaved effect control panels |
| 1218 | // and applications not using global effects. |
| 1219 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1220 | // global effects |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1221 | if (!audio_is_global_session(sessionId)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1222 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1226 | if (!threadLocked) { |
| 1227 | mLock.unlock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1231 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1232 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( |
| 1233 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1234 | { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1235 | // No global output effect sessions on record threads |
| 1236 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX |
| 1237 | || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1238 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1239 | desc->name, mThreadName); |
| 1240 | return BAD_VALUE; |
| 1241 | } |
| 1242 | // only pre processing effects on record thread |
| 1243 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1244 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1245 | desc->name, mThreadName); |
| 1246 | return BAD_VALUE; |
| 1247 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1248 | |
| 1249 | // always allow effects without processing load or latency |
| 1250 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1251 | return NO_ERROR; |
| 1252 | } |
| 1253 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1254 | audio_input_flags_t flags = mInput->flags; |
| 1255 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1256 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1257 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1258 | desc->name, mThreadName); |
| 1259 | return BAD_VALUE; |
| 1260 | } |
| 1261 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1262 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1263 | desc->name, mThreadName); |
| 1264 | return BAD_VALUE; |
| 1265 | } |
| 1266 | } |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1267 | |
| 1268 | if (EffectModule::isHapticGenerator(&desc->type)) { |
| 1269 | ALOGE("%s(): HapticGenerator is not supported in RecordThread", __func__); |
| 1270 | return BAD_VALUE; |
| 1271 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1272 | return NO_ERROR; |
| 1273 | } |
| 1274 | |
| 1275 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1276 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( |
| 1277 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1278 | { |
| 1279 | // no preprocessing on playback threads |
| 1280 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1281 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" |
| 1282 | " thread %s", desc->name, mThreadName); |
| 1283 | return BAD_VALUE; |
| 1284 | } |
| 1285 | |
Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1286 | // always allow effects without processing load or latency |
| 1287 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1288 | return NO_ERROR; |
| 1289 | } |
| 1290 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1291 | if (EffectModule::isHapticGenerator(&desc->type) && mHapticChannelCount == 0) { |
| 1292 | ALOGW("%s: thread doesn't support haptic playback while the effect is HapticGenerator", |
| 1293 | __func__); |
| 1294 | return BAD_VALUE; |
| 1295 | } |
| 1296 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1297 | switch (mType) { |
| 1298 | case MIXER: { |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1299 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1300 | // Reject any effect on mixer multichannel sinks. |
| 1301 | // TODO: fix both format and multichannel issues with effects. |
| 1302 | if (mChannelCount != FCC_2) { |
| 1303 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" |
| 1304 | " thread %s", desc->name, mChannelCount, mThreadName); |
| 1305 | return BAD_VALUE; |
| 1306 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1307 | #endif |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1308 | audio_output_flags_t flags = mOutput->flags; |
| 1309 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1310 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1311 | // global effects are applied only to non fast tracks if they are SW |
| 1312 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1313 | break; |
| 1314 | } |
| 1315 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1316 | // only post processing on output stage session |
| 1317 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1318 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1319 | " on output stage session", desc->name); |
| 1320 | return BAD_VALUE; |
| 1321 | } |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1322 | } else if (sessionId == AUDIO_SESSION_DEVICE) { |
| 1323 | // only post processing on output stage session |
| 1324 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1325 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1326 | " on device session", desc->name); |
| 1327 | return BAD_VALUE; |
| 1328 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1329 | } else { |
| 1330 | // no restriction on effects applied on non fast tracks |
| 1331 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1332 | break; |
| 1333 | } |
| 1334 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1335 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1336 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
| 1337 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", |
| 1338 | desc->name); |
| 1339 | return BAD_VALUE; |
| 1340 | } |
| 1341 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1342 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" |
| 1343 | " in fast mode", desc->name); |
| 1344 | return BAD_VALUE; |
| 1345 | } |
| 1346 | } |
| 1347 | } break; |
| 1348 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1349 | // nothing actionable on offload threads, if the effect: |
| 1350 | // - is offloadable: the effect can be created |
| 1351 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1352 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1353 | break; |
| 1354 | case DIRECT: |
| 1355 | // Reject any effect on Direct output threads for now, since the format of |
| 1356 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1357 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", |
| 1358 | desc->name, mThreadName); |
| 1359 | return BAD_VALUE; |
| 1360 | case DUPLICATING: |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1361 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1362 | // Reject any effect on mixer multichannel sinks. |
| 1363 | // TODO: fix both format and multichannel issues with effects. |
| 1364 | if (mChannelCount != FCC_2) { |
| 1365 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" |
| 1366 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); |
| 1367 | return BAD_VALUE; |
| 1368 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1369 | #endif |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1370 | if (audio_is_global_session(sessionId)) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1371 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" |
| 1372 | " thread %s", desc->name, mThreadName); |
| 1373 | return BAD_VALUE; |
| 1374 | } |
| 1375 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 1376 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" |
| 1377 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1378 | return BAD_VALUE; |
| 1379 | } |
| 1380 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1381 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" |
| 1382 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1383 | return BAD_VALUE; |
| 1384 | } |
| 1385 | break; |
| 1386 | default: |
| 1387 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1388 | } |
| 1389 | |
| 1390 | return NO_ERROR; |
| 1391 | } |
| 1392 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1393 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1394 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1395 | const sp<AudioFlinger::Client>& client, |
| 1396 | const sp<IEffectClient>& effectClient, |
| 1397 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1398 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1399 | effect_descriptor_t *desc, |
| 1400 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1401 | status_t *status, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1402 | bool pinned, |
| 1403 | bool probe) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1404 | { |
| 1405 | sp<EffectModule> effect; |
| 1406 | sp<EffectHandle> handle; |
| 1407 | status_t lStatus; |
| 1408 | sp<EffectChain> chain; |
| 1409 | bool chainCreated = false; |
| 1410 | bool effectCreated = false; |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1411 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1412 | |
| 1413 | lStatus = initCheck(); |
| 1414 | if (lStatus != NO_ERROR) { |
| 1415 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1416 | goto Exit; |
| 1417 | } |
| 1418 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1419 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1420 | |
| 1421 | { // scope for mLock |
| 1422 | Mutex::Autolock _l(mLock); |
| 1423 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1424 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1425 | if (probe || lStatus != NO_ERROR) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1426 | goto Exit; |
| 1427 | } |
| 1428 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1429 | // check for existing effect chain with the requested audio session |
| 1430 | chain = getEffectChain_l(sessionId); |
| 1431 | if (chain == 0) { |
| 1432 | // create a new chain for this session |
| 1433 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1434 | chain = new EffectChain(this, sessionId); |
| 1435 | addEffectChain_l(chain); |
| 1436 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1437 | chainCreated = true; |
| 1438 | } else { |
| 1439 | effect = chain->getEffectFromDesc_l(desc); |
| 1440 | } |
| 1441 | |
| 1442 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1443 | |
| 1444 | if (effect == 0) { |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1445 | effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1446 | // create a new effect module if none present in the chain |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1447 | lStatus = chain->createEffect_l(effect, desc, effectId, sessionId, pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1448 | if (lStatus != NO_ERROR) { |
| 1449 | goto Exit; |
| 1450 | } |
| 1451 | effectCreated = true; |
| 1452 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1453 | // FIXME: use vector of device and address when effect interface is ready. |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1454 | effect->setDevices(outDeviceTypeAddrs()); |
| 1455 | effect->setInputDevice(inDeviceTypeAddr()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1456 | effect->setMode(mAudioFlinger->getMode()); |
| 1457 | effect->setAudioSource(mAudioSource); |
| 1458 | } |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1459 | if (effect->isHapticGenerator()) { |
| 1460 | // TODO(b/184194057): Use the vibrator information from the vibrator that will be used |
| 1461 | // for the HapticGenerator. |
| 1462 | const media::AudioVibratorInfo* defaultVibratorInfo = |
| 1463 | mAudioFlinger->getDefaultVibratorInfo_l(); |
| 1464 | if (defaultVibratorInfo != nullptr) { |
| 1465 | // Only set the vibrator info when it is a valid one. |
| 1466 | effect->setVibratorInfo(defaultVibratorInfo); |
| 1467 | } |
| 1468 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1469 | // create effect handle and connect it to effect module |
| 1470 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1471 | lStatus = handle->initCheck(); |
| 1472 | if (lStatus == OK) { |
| 1473 | lStatus = effect->addHandle(handle.get()); |
| 1474 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1475 | if (enabled != NULL) { |
| 1476 | *enabled = (int)effect->isEnabled(); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | Exit: |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1481 | if (!probe && lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1482 | Mutex::Autolock _l(mLock); |
| 1483 | if (effectCreated) { |
| 1484 | chain->removeEffect_l(effect); |
| 1485 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1486 | if (chainCreated) { |
| 1487 | removeEffectChain_l(chain); |
| 1488 | } |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 1489 | // handle must be cleared by caller to avoid deadlock. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1492 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1493 | return handle; |
| 1494 | } |
| 1495 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1496 | void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle, |
| 1497 | bool unpinIfLast) |
| 1498 | { |
| 1499 | bool remove = false; |
| 1500 | sp<EffectModule> effect; |
| 1501 | { |
| 1502 | Mutex::Autolock _l(mLock); |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1503 | sp<EffectBase> effectBase = handle->effect().promote(); |
| 1504 | if (effectBase == nullptr) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1505 | return; |
| 1506 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1507 | effect = effectBase->asEffectModule(); |
| 1508 | if (effect == nullptr) { |
| 1509 | return; |
| 1510 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1511 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 1512 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 1513 | if (remove) { |
| 1514 | removeEffect_l(effect, true); |
| 1515 | } |
| 1516 | } |
| 1517 | if (remove) { |
| 1518 | mAudioFlinger->updateOrphanEffectChains(effect); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1519 | if (handle->enabled()) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1520 | effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1525 | void AudioFlinger::ThreadBase::onEffectEnable(const sp<EffectModule>& effect) { |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1526 | if (isOffloadOrMmap()) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1527 | Mutex::Autolock _l(mLock); |
| 1528 | broadcast_l(); |
| 1529 | } |
| 1530 | if (!effect->isOffloadable()) { |
| 1531 | if (mType == ThreadBase::OFFLOAD) { |
| 1532 | PlaybackThread *t = (PlaybackThread *)this; |
| 1533 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 1534 | } |
| 1535 | if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { |
| 1536 | mAudioFlinger->onNonOffloadableGlobalEffectEnable(); |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | void AudioFlinger::ThreadBase::onEffectDisable() { |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1542 | if (isOffloadOrMmap()) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1543 | Mutex::Autolock _l(mLock); |
| 1544 | broadcast_l(); |
| 1545 | } |
| 1546 | } |
| 1547 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1548 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1549 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1550 | { |
| 1551 | Mutex::Autolock _l(mLock); |
| 1552 | return getEffect_l(sessionId, effectId); |
| 1553 | } |
| 1554 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1555 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1556 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1557 | { |
| 1558 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1559 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1560 | } |
| 1561 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1562 | std::vector<int> AudioFlinger::ThreadBase::getEffectIds_l(audio_session_t sessionId) |
| 1563 | { |
| 1564 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1565 | return chain != nullptr ? chain->getEffectIds() : std::vector<int>{}; |
| 1566 | } |
| 1567 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1568 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1569 | // PlaybackThread::mLock held |
| 1570 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1571 | { |
| 1572 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1573 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1574 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1575 | bool chainCreated = false; |
| 1576 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1577 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1578 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %#x", |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1579 | this, effect->desc().name, effect->desc().flags); |
| 1580 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1581 | if (chain == 0) { |
| 1582 | // create a new chain for this session |
| 1583 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1584 | chain = new EffectChain(this, sessionId); |
| 1585 | addEffectChain_l(chain); |
| 1586 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1587 | chainCreated = true; |
| 1588 | } |
| 1589 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1590 | |
| 1591 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1592 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1593 | this, effect->desc().name, chain.get()); |
| 1594 | return BAD_VALUE; |
| 1595 | } |
| 1596 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1597 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1598 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1599 | status_t status = chain->addEffect_l(effect); |
| 1600 | if (status != NO_ERROR) { |
| 1601 | if (chainCreated) { |
| 1602 | removeEffectChain_l(chain); |
| 1603 | } |
| 1604 | return status; |
| 1605 | } |
| 1606 | |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1607 | effect->setDevices(outDeviceTypeAddrs()); |
| 1608 | effect->setInputDevice(inDeviceTypeAddr()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1609 | effect->setMode(mAudioFlinger->getMode()); |
| 1610 | effect->setAudioSource(mAudioSource); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1611 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1612 | return NO_ERROR; |
| 1613 | } |
| 1614 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1615 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1616 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1617 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1618 | effect_descriptor_t desc = effect->desc(); |
| 1619 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1620 | detachAuxEffect_l(effect->id()); |
| 1621 | } |
| 1622 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1623 | sp<EffectChain> chain = effect->callback()->chain().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1624 | if (chain != 0) { |
| 1625 | // remove effect chain if removing last effect |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1626 | if (chain->removeEffect_l(effect, release) == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1627 | removeEffectChain_l(chain); |
| 1628 | } |
| 1629 | } else { |
| 1630 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1635 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1636 | { |
| 1637 | effectChains = mEffectChains; |
| 1638 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1639 | mEffectChains[i]->lock(); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1644 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1645 | { |
| 1646 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1647 | effectChains[i]->unlock(); |
| 1648 | } |
| 1649 | } |
| 1650 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1651 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1652 | { |
| 1653 | Mutex::Autolock _l(mLock); |
| 1654 | return getEffectChain_l(sessionId); |
| 1655 | } |
| 1656 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1657 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1658 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 | { |
| 1660 | size_t size = mEffectChains.size(); |
| 1661 | for (size_t i = 0; i < size; i++) { |
| 1662 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1663 | return mEffectChains[i]; |
| 1664 | } |
| 1665 | } |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1670 | { |
| 1671 | Mutex::Autolock _l(mLock); |
| 1672 | size_t size = mEffectChains.size(); |
| 1673 | for (size_t i = 0; i < size; i++) { |
| 1674 | mEffectChains[i]->setMode_l(mode); |
| 1675 | } |
| 1676 | } |
| 1677 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 1678 | void AudioFlinger::ThreadBase::toAudioPortConfig(struct audio_port_config *config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1679 | { |
| 1680 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1681 | config->ext.mix.handle = mId; |
| 1682 | config->sample_rate = mSampleRate; |
| 1683 | config->format = mFormat; |
| 1684 | config->channel_mask = mChannelMask; |
| 1685 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1686 | AUDIO_PORT_CONFIG_FORMAT; |
| 1687 | } |
| 1688 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1689 | void AudioFlinger::ThreadBase::systemReady() |
| 1690 | { |
| 1691 | Mutex::Autolock _l(mLock); |
| 1692 | if (mSystemReady) { |
| 1693 | return; |
| 1694 | } |
| 1695 | mSystemReady = true; |
| 1696 | |
| 1697 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1698 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1699 | } |
| 1700 | mPendingConfigEvents.clear(); |
| 1701 | } |
| 1702 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1703 | template <typename T> |
| 1704 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) { |
| 1705 | ssize_t index = mActiveTracks.indexOf(track); |
| 1706 | if (index >= 0) { |
| 1707 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); |
| 1708 | return index; |
| 1709 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1710 | logTrack("add", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1711 | mActiveTracksGeneration++; |
| 1712 | mLatestActiveTrack = track; |
| 1713 | ++mBatteryCounter[track->uid()].second; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1714 | mHasChanged = true; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1715 | return mActiveTracks.add(track); |
| 1716 | } |
| 1717 | |
| 1718 | template <typename T> |
| 1719 | ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) { |
| 1720 | ssize_t index = mActiveTracks.remove(track); |
| 1721 | if (index < 0) { |
| 1722 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); |
| 1723 | return index; |
| 1724 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1725 | logTrack("remove", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1726 | mActiveTracksGeneration++; |
| 1727 | --mBatteryCounter[track->uid()].second; |
| 1728 | // mLatestActiveTrack is not cleared even if is the same as track. |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1729 | mHasChanged = true; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 1730 | #ifdef TEE_SINK |
| 1731 | track->dumpTee(-1 /* fd */, "_REMOVE"); |
| 1732 | #endif |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 1733 | track->logEndInterval(); // log to MediaMetrics |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1734 | return index; |
| 1735 | } |
| 1736 | |
| 1737 | template <typename T> |
| 1738 | void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() { |
| 1739 | for (const sp<T> &track : mActiveTracks) { |
| 1740 | BatteryNotifier::getInstance().noteStopAudio(track->uid()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1741 | logTrack("clear", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1742 | } |
| 1743 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1744 | if (!mActiveTracks.empty()) { mHasChanged = true; } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1745 | mActiveTracks.clear(); |
| 1746 | mLatestActiveTrack.clear(); |
| 1747 | mBatteryCounter.clear(); |
| 1748 | } |
| 1749 | |
| 1750 | template <typename T> |
| 1751 | void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState( |
| 1752 | sp<ThreadBase> thread, bool force) { |
| 1753 | // Updates ActiveTracks client uids to the thread wakelock. |
| 1754 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { |
| 1755 | thread->updateWakeLockUids_l(getWakeLockUids()); |
| 1756 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1757 | } |
| 1758 | |
| 1759 | // Updates BatteryNotifier uids |
| 1760 | for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) { |
| 1761 | const uid_t uid = it->first; |
| 1762 | ssize_t &previous = it->second.first; |
| 1763 | ssize_t ¤t = it->second.second; |
| 1764 | if (current > 0) { |
| 1765 | if (previous == 0) { |
| 1766 | BatteryNotifier::getInstance().noteStartAudio(uid); |
| 1767 | } |
| 1768 | previous = current; |
| 1769 | ++it; |
| 1770 | } else if (current == 0) { |
| 1771 | if (previous > 0) { |
| 1772 | BatteryNotifier::getInstance().noteStopAudio(uid); |
| 1773 | } |
| 1774 | it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase. |
| 1775 | } else /* (current < 0) */ { |
| 1776 | LOG_ALWAYS_FATAL("negative battery count %zd", current); |
| 1777 | } |
| 1778 | } |
| 1779 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1780 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1781 | template <typename T> |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1782 | bool AudioFlinger::ThreadBase::ActiveTracks<T>::readAndClearHasChanged() { |
| 1783 | const bool hasChanged = mHasChanged; |
| 1784 | mHasChanged = false; |
| 1785 | return hasChanged; |
| 1786 | } |
| 1787 | |
| 1788 | template <typename T> |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1789 | void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack( |
| 1790 | const char *funcName, const sp<T> &track) const { |
| 1791 | if (mLocalLog != nullptr) { |
| 1792 | String8 result; |
| 1793 | track->appendDump(result, false /* active */); |
| 1794 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string()); |
| 1795 | } |
| 1796 | } |
| 1797 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1798 | void AudioFlinger::ThreadBase::broadcast_l() |
| 1799 | { |
| 1800 | // Thread could be blocked waiting for async |
| 1801 | // so signal it to handle state changes immediately |
| 1802 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1803 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1804 | mSignalPending = true; |
| 1805 | mWaitWorkCV.broadcast(); |
| 1806 | } |
| 1807 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 1808 | // Call only from threadLoop() or when it is idle. |
| 1809 | // Do not call from high performance code as this may do binder rpc to the MediaMetrics service. |
| 1810 | void AudioFlinger::ThreadBase::sendStatistics(bool force) |
| 1811 | { |
| 1812 | // Do not log if we have no stats. |
| 1813 | // We choose the timestamp verifier because it is the most likely item to be present. |
| 1814 | const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN; |
| 1815 | if (nstats == 0) { |
| 1816 | return; |
| 1817 | } |
| 1818 | |
| 1819 | // Don't log more frequently than once per 12 hours. |
| 1820 | // We use BOOTTIME to include suspend time. |
| 1821 | const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1822 | const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0 |
| 1823 | if (!force && sinceNs <= 12 * NANOS_PER_HOUR) { |
| 1824 | return; |
| 1825 | } |
| 1826 | |
| 1827 | mLastRecordedTimestampVerifierN = mTimestampVerifier.getN(); |
| 1828 | mLastRecordedTimeNs = timeNs; |
| 1829 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 1830 | std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("audiothread")); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 1831 | |
| 1832 | #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors. |
| 1833 | |
| 1834 | // thread configuration |
| 1835 | item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle |
| 1836 | // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId); |
| 1837 | item->setCString(MM_PREFIX "type", threadTypeToString(mType)); |
| 1838 | item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate); |
| 1839 | item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask); |
| 1840 | item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str()); |
| 1841 | item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1842 | item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes()).c_str()); |
| 1843 | item->setCString(MM_PREFIX "inDevice", toString(inDeviceType()).c_str()); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 1844 | |
| 1845 | // thread statistics |
| 1846 | if (mIoJitterMs.getN() > 0) { |
| 1847 | item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean()); |
| 1848 | item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev()); |
| 1849 | } |
| 1850 | if (mProcessTimeMs.getN() > 0) { |
| 1851 | item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean()); |
| 1852 | item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev()); |
| 1853 | } |
| 1854 | const auto tsjitter = mTimestampVerifier.getJitterMs(); |
| 1855 | if (tsjitter.getN() > 0) { |
| 1856 | item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean()); |
| 1857 | item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev()); |
| 1858 | } |
| 1859 | if (mLatencyMs.getN() > 0) { |
| 1860 | item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean()); |
| 1861 | item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev()); |
| 1862 | } |
| 1863 | |
| 1864 | item->selfrecord(); |
| 1865 | } |
| 1866 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | // ---------------------------------------------------------------------------- |
| 1868 | // Playback |
| 1869 | // ---------------------------------------------------------------------------- |
| 1870 | |
| 1871 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1872 | AudioStreamOut* output, |
| 1873 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1874 | type_t type, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1875 | bool systemReady) |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 1876 | : ThreadBase(audioFlinger, id, type, systemReady, true /* isOut */), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1877 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1878 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1879 | mMixerBuffer(NULL), |
| 1880 | mMixerBufferSize(0), |
| 1881 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1882 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1883 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1884 | mEffectBuffer(NULL), |
| 1885 | mEffectBufferSize(0), |
| 1886 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1887 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1888 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1889 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1890 | mSuspendedFrames(0), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1891 | mActiveTracks(&this->mLocalLog), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1892 | // mStreamTypes[] initialized in constructor body |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1893 | mTracks(type == MIXER), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1894 | mOutput(output), |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 1895 | mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1896 | mMixerStatus(MIXER_IDLE), |
| 1897 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1898 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1899 | mBytesRemaining(0), |
| 1900 | mCurrentWriteLength(0), |
| 1901 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1902 | mWriteAckSequence(0), |
| 1903 | mDrainSequence(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1904 | mScreenState(AudioFlinger::mScreenState), |
| 1905 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1906 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 1907 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 1908 | mLeftVolFloat(-1.0), mRightVolFloat(-1.0), |
| 1909 | mDownStreamPatch{} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1910 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1911 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1912 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1913 | |
| 1914 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1915 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1916 | // parameter. |
| 1917 | // |
| 1918 | // If the HAL we are using has support for master volume or master mute, |
| 1919 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1920 | // and the mute set to false). |
| 1921 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1922 | mMasterMute = audioFlinger->masterMute_l(); |
George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 1923 | if (mOutput->audioHwDev) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1924 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1925 | mMasterVolume = 1.0; |
| 1926 | } |
| 1927 | |
| 1928 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1929 | mMasterMute = false; |
| 1930 | } |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 1931 | mIsMsdDevice = strcmp( |
| 1932 | mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1933 | } |
| 1934 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1935 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1936 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 1937 | // TODO: We may also match on address as well as device type for |
| 1938 | // AUDIO_DEVICE_OUT_BUS, AUDIO_DEVICE_OUT_ALL_A2DP, AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
Dean Wheatley | f8726f0 | 2019-12-02 14:12:01 +1100 | [diff] [blame] | 1939 | if (type == MIXER || type == DIRECT || type == OFFLOAD) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1940 | // TODO: This property should be ensure that only contains one single device type. |
| 1941 | mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( |
| 1942 | "audio.timestamp.corrected_output_device", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 1943 | (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD |
| 1944 | : AUDIO_DEVICE_NONE)); |
| 1945 | } |
| 1946 | |
Mikhail Naganov | f33115d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 1947 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { |
| 1948 | const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)}; |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1949 | mStreamTypes[stream].volume = 0.0f; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1950 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1951 | } |
Eric Laurent | 35f8c7c | 2020-02-08 11:42:35 -0800 | [diff] [blame] | 1952 | // Audio patch and call assistant volume are always max |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1953 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 1954 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
Eric Laurent | 35f8c7c | 2020-02-08 11:42:35 -0800 | [diff] [blame] | 1955 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; |
| 1956 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1960 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1961 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1962 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1963 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1964 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1967 | // Thread virtuals |
| 1968 | |
| 1969 | void AudioFlinger::PlaybackThread::onFirstRef() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1970 | { |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 1971 | if (mOutput == nullptr || mOutput->stream == nullptr) { |
| 1972 | ALOGE("The stream is not open yet"); // This should not happen. |
| 1973 | } else { |
| 1974 | // setEventCallback will need a strong pointer as a parameter. Calling it |
| 1975 | // here instead of constructor of PlaybackThread so that the onFirstRef |
| 1976 | // callback would not be made on an incompletely constructed object. |
| 1977 | if (mOutput->stream->setEventCallback(this) != OK) { |
jiabin | f1a3605 | 2020-08-19 14:33:31 -0700 | [diff] [blame] | 1978 | ALOGD("Failed to add event callback"); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 1979 | } |
| 1980 | } |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1981 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1982 | } |
| 1983 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1984 | // ThreadBase virtuals |
| 1985 | void AudioFlinger::PlaybackThread::preExit() |
| 1986 | { |
| 1987 | ALOGV(" preExit()"); |
| 1988 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1989 | // converted to use audio HAL extensions required to support tunneling |
| 1990 | status_t result = mOutput->stream->setParameters(String8("exiting=1")); |
| 1991 | ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result); |
| 1992 | } |
| 1993 | |
| 1994 | void AudioFlinger::PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1995 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1996 | String8 result; |
| 1997 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1998 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1999 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 2000 | const stream_type_t *st = &mStreamTypes[i]; |
| 2001 | if (i > 0) { |
| 2002 | result.appendFormat(", "); |
| 2003 | } |
| 2004 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 2005 | if (st->mute) { |
| 2006 | result.append("M"); |
| 2007 | } |
| 2008 | } |
| 2009 | result.append("\n"); |
| 2010 | write(fd, result.string(), result.length()); |
| 2011 | result.clear(); |
| 2012 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2013 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 2014 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2015 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2016 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2017 | |
| 2018 | size_t numtracks = mTracks.size(); |
| 2019 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2020 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2021 | size_t numactiveseen = 0; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2022 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2023 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2024 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2025 | result.append(prefix); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2026 | mTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2027 | for (size_t i = 0; i < numtracks; ++i) { |
| 2028 | sp<Track> track = mTracks[i]; |
| 2029 | if (track != 0) { |
| 2030 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 2031 | if (active) { |
| 2032 | numactiveseen++; |
| 2033 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2034 | result.append(prefix); |
| 2035 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | } else { |
| 2039 | result.append("\n"); |
| 2040 | } |
| 2041 | if (numactiveseen != numactive) { |
| 2042 | // some tracks in the active list were not in the tracks list |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2043 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2044 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2045 | result.append(prefix); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2046 | mActiveTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2047 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2048 | sp<Track> track = mActiveTracks[i]; |
| 2049 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2050 | result.append(prefix); |
| 2051 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2059 | void AudioFlinger::PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2060 | { |
Andy Hung | 04cb8f7 | 2020-03-20 13:44:33 -0700 | [diff] [blame] | 2061 | dprintf(fd, " Master volume: %f\n", mMasterVolume); |
Mikhail Naganov | dfb411f | 2018-09-11 13:39:56 -0700 | [diff] [blame] | 2062 | dprintf(fd, " Master mute: %s\n", mMasterMute ? "on" : "off"); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2063 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE) { |
| 2064 | dprintf(fd, " Haptic channel mask: %#x (%s)\n", mHapticChannelMask, |
| 2065 | channelMaskToString(mHapticChannelMask, true /* output */).c_str()); |
| 2066 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2067 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2068 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 2069 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 2070 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 2071 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 2072 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 2073 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 2074 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 2075 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2076 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 2077 | AudioStreamOut *output = mOutput; |
| 2078 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 2079 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 2080 | output, flags, toString(flags).c_str()); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2081 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 2082 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 2083 | if (mPipeSink.get() != nullptr) { |
| 2084 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 2085 | } |
| 2086 | if (output != nullptr) { |
| 2087 | dprintf(fd, " Hal stream dump:\n"); |
| 2088 | (void)output->stream->dump(fd); |
| 2089 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2090 | } |
| 2091 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2092 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 2093 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 2094 | const sp<AudioFlinger::Client>& client, |
| 2095 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2096 | const audio_attributes_t& attr, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2097 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2098 | audio_format_t format, |
| 2099 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2100 | size_t *pFrameCount, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2101 | size_t *pNotificationFrameCount, |
| 2102 | uint32_t notificationsPerBuffer, |
| 2103 | float speed, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2104 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2105 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2106 | audio_output_flags_t *flags, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2107 | pid_t creatorPid, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2108 | const Identity& identity, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2109 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2110 | status_t *status, |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2111 | audio_port_handle_t portId, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2112 | const sp<media::IAudioTrackCallback>& callback) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2113 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2114 | size_t frameCount = *pFrameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2115 | size_t notificationFrameCount = *pNotificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2116 | sp<Track> track; |
| 2117 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2118 | audio_output_flags_t outputFlags = mOutput->flags; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2119 | audio_output_flags_t requestedFlags = *flags; |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2120 | uint32_t sampleRate; |
| 2121 | |
| 2122 | if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) { |
| 2123 | lStatus = BAD_VALUE; |
| 2124 | goto Exit; |
| 2125 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2126 | |
| 2127 | if (*pSampleRate == 0) { |
| 2128 | *pSampleRate = mSampleRate; |
| 2129 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2130 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2131 | |
| 2132 | // special case for FAST flag considered OK if fast mixer is present |
| 2133 | if (hasFastMixer()) { |
| 2134 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 2135 | } |
| 2136 | |
| 2137 | // Check if requested flags are compatible with output stream flags |
| 2138 | if ((*flags & outputFlags) != *flags) { |
| 2139 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 2140 | *flags, outputFlags); |
| 2141 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 2142 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2143 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2144 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2145 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2146 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2147 | // PCM data |
| 2148 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2149 | // TODO: extract as a data library function that checks that a computationally |
| 2150 | // expensive downmixer is not required: isFastOutputChannelConversion() |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2151 | (channelMask == (mChannelMask | mHapticChannelMask) || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2152 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 2153 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 2154 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2155 | // hardware sample rate |
| 2156 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2157 | // normal mixer has an associated fast mixer |
| 2158 | hasFastMixer() && |
| 2159 | // there are sufficient fast track slots available |
| 2160 | (mFastTrackAvailMask != 0) |
| 2161 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 2162 | // FIXME add a permission test also? |
| 2163 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2164 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 2165 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 2166 | // read the fast track multiplier property the first time it is needed |
| 2167 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 2168 | if (ok != 0) { |
| 2169 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 2170 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2171 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2172 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2173 | |
| 2174 | // check compatibility with audio effects. |
| 2175 | { // scope for mLock |
| 2176 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2177 | for (audio_session_t session : { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 2178 | AUDIO_SESSION_DEVICE, |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2179 | AUDIO_SESSION_OUTPUT_STAGE, |
| 2180 | AUDIO_SESSION_OUTPUT_MIX, |
| 2181 | sessionId, |
| 2182 | }) { |
| 2183 | sp<EffectChain> chain = getEffectChain_l(session); |
| 2184 | if (chain.get() != nullptr) { |
| 2185 | audio_output_flags_t old = *flags; |
| 2186 | chain->checkOutputFlagCompatibility(flags); |
| 2187 | if (old != *flags) { |
| 2188 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 2189 | (int)session, (int)old, (int)*flags); |
| 2190 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 2194 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2195 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 2196 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2197 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2198 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 2199 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2200 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2201 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2202 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2203 | audio_is_linear_pcm(format), channelMask, sampleRate, |
| 2204 | mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2205 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 2206 | } |
| 2207 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2208 | |
| 2209 | if (!audio_has_proportional_frames(format)) { |
| 2210 | if (sharedBuffer != 0) { |
| 2211 | // Same comment as below about ignoring frameCount parameter for set() |
| 2212 | frameCount = sharedBuffer->size(); |
| 2213 | } else if (frameCount == 0) { |
| 2214 | frameCount = mNormalFrameCount; |
| 2215 | } |
| 2216 | if (notificationFrameCount != frameCount) { |
| 2217 | notificationFrameCount = frameCount; |
| 2218 | } |
| 2219 | } else if (sharedBuffer != 0) { |
| 2220 | // FIXME: Ensure client side memory buffers need |
| 2221 | // not have additional alignment beyond sample |
| 2222 | // (e.g. 16 bit stereo accessed as 32 bit frame). |
| 2223 | size_t alignment = audio_bytes_per_sample(format); |
| 2224 | if (alignment & 1) { |
| 2225 | // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). |
| 2226 | alignment = 1; |
| 2227 | } |
| 2228 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2229 | size_t frameSize = channelCount * audio_bytes_per_sample(format); |
| 2230 | if (channelCount > 1) { |
| 2231 | // More than 2 channels does not require stronger alignment than stereo |
| 2232 | alignment <<= 1; |
| 2233 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2234 | if (((uintptr_t)sharedBuffer->unsecurePointer() & (alignment - 1)) != 0) { |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2235 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2236 | sharedBuffer->unsecurePointer(), channelCount); |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2237 | lStatus = BAD_VALUE; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2238 | goto Exit; |
| 2239 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2240 | |
| 2241 | // When initializing a shared buffer AudioTrack via constructors, |
| 2242 | // there's no frameCount parameter. |
| 2243 | // But when initializing a shared buffer AudioTrack via set(), |
| 2244 | // there _is_ a frameCount parameter. We silently ignore it. |
| 2245 | frameCount = sharedBuffer->size() / frameSize; |
| 2246 | } else { |
| 2247 | size_t minFrameCount = 0; |
| 2248 | // For fast tracks we try to respect the application's request for notifications per buffer. |
| 2249 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2250 | if (notificationsPerBuffer > 0) { |
| 2251 | // Avoid possible arithmetic overflow during multiplication. |
| 2252 | if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { |
| 2253 | ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", |
| 2254 | notificationsPerBuffer, mFrameCount); |
| 2255 | } else { |
| 2256 | minFrameCount = mFrameCount * notificationsPerBuffer; |
| 2257 | } |
| 2258 | } |
| 2259 | } else { |
| 2260 | // For normal PCM streaming tracks, update minimum frame count. |
| 2261 | // Buffer depth is forced to be at least 2 x the normal mixer frame count and |
| 2262 | // cover audio hardware latency. |
| 2263 | // This is probably too conservative, but legacy application code may depend on it. |
| 2264 | // If you change this calculation, also review the start threshold which is related. |
| 2265 | uint32_t latencyMs = latency_l(); |
| 2266 | if (latencyMs == 0) { |
| 2267 | ALOGE("Error when retrieving output stream latency"); |
| 2268 | lStatus = UNKNOWN_ERROR; |
| 2269 | goto Exit; |
| 2270 | } |
| 2271 | |
| 2272 | minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, |
| 2273 | mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); |
| 2274 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2275 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2276 | if (frameCount < minFrameCount) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2277 | frameCount = minFrameCount; |
| 2278 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2279 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2280 | |
| 2281 | // Make sure that application is notified with sufficient margin before underrun. |
| 2282 | // The client can divide the AudioTrack buffer into sub-buffers, |
| 2283 | // and expresses its desire to server as the notification frame count. |
| 2284 | if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { |
| 2285 | size_t maxNotificationFrames; |
| 2286 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2287 | // notify every HAL buffer, regardless of the size of the track buffer |
| 2288 | maxNotificationFrames = mFrameCount; |
| 2289 | } else { |
Andy Hung | fa11780 | 2019-04-12 13:50:39 -0700 | [diff] [blame] | 2290 | // Triple buffer the notification period for a triple buffered mixer period; |
| 2291 | // otherwise, double buffering for the notification period is fine. |
| 2292 | // |
| 2293 | // TODO: This should be moved to AudioTrack to modify the notification period |
| 2294 | // on AudioTrack::setBufferSizeInFrames() changes. |
| 2295 | const int nBuffering = |
| 2296 | (uint64_t{frameCount} * mSampleRate) |
| 2297 | / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2; |
| 2298 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2299 | maxNotificationFrames = frameCount / nBuffering; |
| 2300 | // If client requested a fast track but this was denied, then use the smaller maximum. |
| 2301 | if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2302 | size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; |
| 2303 | if (maxNotificationFrames > maxNotificationFramesFastDenied) { |
| 2304 | maxNotificationFrames = maxNotificationFramesFastDenied; |
| 2305 | } |
| 2306 | } |
| 2307 | } |
| 2308 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 2309 | if (notificationFrameCount == 0) { |
| 2310 | ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", |
| 2311 | maxNotificationFrames, frameCount); |
| 2312 | } else { |
| 2313 | ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", |
| 2314 | notificationFrameCount, maxNotificationFrames, frameCount); |
| 2315 | } |
| 2316 | notificationFrameCount = maxNotificationFrames; |
| 2317 | } |
| 2318 | } |
| 2319 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2320 | *pFrameCount = frameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2321 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2322 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2323 | switch (mType) { |
| 2324 | |
| 2325 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2326 | if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2327 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2328 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 2329 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2330 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2331 | lStatus = BAD_VALUE; |
| 2332 | goto Exit; |
| 2333 | } |
| 2334 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2335 | break; |
| 2336 | |
| 2337 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2338 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2339 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 2340 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2341 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2342 | lStatus = BAD_VALUE; |
| 2343 | goto Exit; |
| 2344 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2345 | break; |
| 2346 | |
| 2347 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2348 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2349 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 2350 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2351 | format, mOutput, mFormat); |
| 2352 | lStatus = BAD_VALUE; |
| 2353 | goto Exit; |
| 2354 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2355 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2356 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2357 | lStatus = BAD_VALUE; |
| 2358 | goto Exit; |
| 2359 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2360 | break; |
| 2361 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | lStatus = initCheck(); |
| 2365 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2366 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2367 | goto Exit; |
| 2368 | } |
| 2369 | |
| 2370 | { // scope for mLock |
| 2371 | Mutex::Autolock _l(mLock); |
| 2372 | |
| 2373 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2374 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2375 | // manager |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2376 | product_strategy_t strategy = AudioSystem::getStrategyForStream(streamType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2377 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2378 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2379 | if (t != 0 && t->isExternalTrack()) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2380 | product_strategy_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2381 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2382 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2383 | strategy, actual); |
| 2384 | lStatus = BAD_VALUE; |
| 2385 | goto Exit; |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2390 | // Set DIRECT flag if current thread is DirectOutputThread. This can |
| 2391 | // happen when the playback is rerouted to direct output thread by |
| 2392 | // dynamic audio policy. |
| 2393 | // Do NOT report the flag changes back to client, since the client |
| 2394 | // doesn't explicitly request a direct flag. |
| 2395 | audio_output_flags_t trackFlags = *flags; |
| 2396 | if (mType == DIRECT) { |
| 2397 | trackFlags = static_cast<audio_output_flags_t>(trackFlags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 2398 | } |
| 2399 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2400 | track = new Track(this, client, streamType, attr, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2401 | channelMask, frameCount, |
| 2402 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2403 | sessionId, creatorPid, identity, trackFlags, TrackBase::TYPE_DEFAULT, |
| 2404 | portId, SIZE_MAX /*frameCountToBeReady*/); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2405 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2406 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2407 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2408 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2409 | // 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] | 2410 | goto Exit; |
| 2411 | } |
| 2412 | mTracks.add(track); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2413 | { |
| 2414 | Mutex::Autolock _atCbL(mAudioTrackCbLock); |
| 2415 | if (callback.get() != nullptr) { |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2416 | mAudioTrackCallbacks.emplace(track, callback); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2417 | } |
| 2418 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2419 | |
| 2420 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 2421 | if (chain != 0) { |
| 2422 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2423 | track->setMainBuffer(chain->inBuffer()); |
| 2424 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 2425 | chain->incTrackCnt(); |
| 2426 | } |
| 2427 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2428 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2429 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2430 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2431 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2432 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | lStatus = NO_ERROR; |
| 2437 | |
| 2438 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2439 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2440 | return track; |
| 2441 | } |
| 2442 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2443 | template<typename T> |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2444 | ssize_t AudioFlinger::PlaybackThread::Tracks<T>::remove(const sp<T> &track) |
| 2445 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2446 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2447 | const ssize_t index = mTracks.remove(track); |
| 2448 | if (index >= 0) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2449 | if (mSaveDeletedTrackIds) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2450 | // We can't directly access mAudioMixer since the caller may be outside of threadLoop. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2451 | // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update, |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2452 | // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2453 | mDeletedTrackIds.emplace(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2454 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2455 | } |
| 2456 | return index; |
| 2457 | } |
| 2458 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2459 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 2460 | { |
| 2461 | return latency; |
| 2462 | } |
| 2463 | |
| 2464 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 2465 | { |
| 2466 | Mutex::Autolock _l(mLock); |
| 2467 | return latency_l(); |
| 2468 | } |
| 2469 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 2470 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2471 | uint32_t latency; |
| 2472 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { |
| 2473 | return correctLatency_l(latency); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2474 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2475 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 2479 | { |
| 2480 | Mutex::Autolock _l(mLock); |
| 2481 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2482 | if (mOutput && mOutput->audioHwDev && |
| 2483 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2484 | mMasterVolume = 1.0; |
| 2485 | } else { |
| 2486 | mMasterVolume = value; |
| 2487 | } |
| 2488 | } |
| 2489 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 2490 | void AudioFlinger::PlaybackThread::setMasterBalance(float balance) |
| 2491 | { |
| 2492 | mMasterBalance.store(balance); |
| 2493 | } |
| 2494 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2495 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 2496 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2497 | if (isDuplicating()) { |
| 2498 | return; |
| 2499 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2500 | Mutex::Autolock _l(mLock); |
| 2501 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2502 | if (mOutput && mOutput->audioHwDev && |
| 2503 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2504 | mMasterMute = false; |
| 2505 | } else { |
| 2506 | mMasterMute = muted; |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2511 | { |
| 2512 | Mutex::Autolock _l(mLock); |
| 2513 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2514 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2518 | { |
| 2519 | Mutex::Autolock _l(mLock); |
| 2520 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2521 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2525 | { |
| 2526 | Mutex::Autolock _l(mLock); |
| 2527 | return mStreamTypes[stream].volume; |
| 2528 | } |
| 2529 | |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 2530 | void AudioFlinger::PlaybackThread::setVolumeForOutput_l(float left, float right) const |
| 2531 | { |
| 2532 | mOutput->stream->setVolume(left, right); |
| 2533 | } |
| 2534 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2535 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2536 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2537 | { |
| 2538 | status_t status = ALREADY_EXISTS; |
| 2539 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2540 | if (mActiveTracks.indexOf(track) < 0) { |
| 2541 | // the track is newly added, make sure it fills up all its |
| 2542 | // buffers before playing. This is to ensure the client will |
| 2543 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2544 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2545 | TrackBase::track_state state = track->mState; |
| 2546 | mLock.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2547 | status = AudioSystem::startOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2548 | mLock.lock(); |
| 2549 | // abort track was stopped/paused while we released the lock |
| 2550 | if (state != track->mState) { |
| 2551 | if (status == NO_ERROR) { |
| 2552 | mLock.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2553 | AudioSystem::stopOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2554 | mLock.lock(); |
| 2555 | } |
| 2556 | return INVALID_OPERATION; |
| 2557 | } |
| 2558 | // abort if start is rejected by audio policy manager |
| 2559 | if (status != NO_ERROR) { |
| 2560 | return PERMISSION_DENIED; |
| 2561 | } |
| 2562 | #ifdef ADD_BATTERY_DATA |
| 2563 | // to track the speaker usage |
| 2564 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2565 | #endif |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2566 | sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2569 | // set retry count for buffer fill |
| 2570 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2571 | if (track->isStopping_1()) { |
| 2572 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 2573 | } else { |
| 2574 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2575 | } |
| 2576 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2577 | } else { |
| 2578 | track->mRetryCount = kMaxTrackStartupRetries; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2579 | track->mFillingUpStatus = |
| 2580 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2581 | } |
| 2582 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 2583 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2584 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE |
| 2585 | && ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
| 2586 | || (chain != nullptr && chain->containsHapticGeneratingEffect_l()))) { |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2587 | // Unlock due to VibratorService will lock for this call and will |
| 2588 | // call Tracks.mute/unmute which also require thread's lock. |
| 2589 | mLock.unlock(); |
| 2590 | const int intensity = AudioFlinger::onExternalVibrationStart( |
| 2591 | track->getExternalVibration()); |
| 2592 | mLock.lock(); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2593 | track->setHapticIntensity(static_cast<os::HapticScale>(intensity)); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2594 | // Haptic playback should be enabled by vibrator service. |
| 2595 | if (track->getHapticPlaybackEnabled()) { |
| 2596 | // Disable haptic playback of all active track to ensure only |
| 2597 | // one track playing haptic if current track should play haptic. |
| 2598 | for (const auto &t : mActiveTracks) { |
| 2599 | t->setHapticPlaybackEnabled(false); |
| 2600 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2601 | } |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2602 | |
| 2603 | // Set haptic intensity for effect |
| 2604 | if (chain != nullptr) { |
| 2605 | chain->setHapticIntensity_l(track->id(), intensity); |
| 2606 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2609 | track->mResetDone = false; |
| 2610 | track->mPresentationCompleteFrames = 0; |
| 2611 | mActiveTracks.add(track); |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2612 | if (chain != 0) { |
| 2613 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2614 | track->sessionId()); |
| 2615 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2616 | } |
| 2617 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 2618 | track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2619 | status = NO_ERROR; |
| 2620 | } |
| 2621 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2622 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2623 | return status; |
| 2624 | } |
| 2625 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2626 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2627 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2628 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2629 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2630 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2631 | track->mState = TrackBase::STOPPED; |
| 2632 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2633 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2634 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2635 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2636 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2637 | |
| 2638 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2642 | { |
| 2643 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2644 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2645 | String8 result; |
| 2646 | track->appendDump(result, false /* active */); |
| 2647 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2648 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2649 | mTracks.remove(track); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2650 | { |
| 2651 | Mutex::Autolock _atCbL(mAudioTrackCbLock); |
| 2652 | mAudioTrackCallbacks.erase(track); |
| 2653 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2654 | if (track->isFastTrack()) { |
| 2655 | int index = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2656 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2657 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2658 | mFastTrackAvailMask |= 1 << index; |
| 2659 | // redundant as track is about to be destroyed, for dumpsys only |
| 2660 | track->mFastIndex = -1; |
| 2661 | } |
| 2662 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2663 | if (chain != 0) { |
| 2664 | chain->decTrackCnt(); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2669 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2671 | String8 out_s8; |
| 2672 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { |
| 2673 | return out_s8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2674 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2675 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2676 | } |
| 2677 | |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 2678 | status_t AudioFlinger::DirectOutputThread::selectPresentation(int presentationId, int programId) { |
| 2679 | Mutex::Autolock _l(mLock); |
| 2680 | if (mOutput == nullptr || mOutput->stream == nullptr) { |
| 2681 | return NO_INIT; |
| 2682 | } |
| 2683 | return mOutput->stream->selectPresentation(presentationId, programId); |
| 2684 | } |
| 2685 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2686 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid, |
| 2687 | audio_port_handle_t portId) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2688 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2689 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2690 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2691 | desc->mIoHandle = mId; |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 2692 | struct audio_patch patch = mPatch; |
| 2693 | if (isMsdDevice()) { |
| 2694 | patch = mDownStreamPatch; |
| 2695 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2696 | |
| 2697 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2698 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2699 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2700 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 2701 | desc->mPatch = patch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2702 | desc->mChannelMask = mChannelMask; |
| 2703 | desc->mSamplingRate = mSampleRate; |
| 2704 | desc->mFormat = mFormat; |
| 2705 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2706 | // AudioFlinger::frameCount(audio_io_handle_t) |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2707 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2708 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2709 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2710 | case AUDIO_CLIENT_STARTED: |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 2711 | desc->mPatch = patch; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2712 | desc->mPortId = portId; |
| 2713 | break; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2714 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2715 | default: |
| 2716 | break; |
| 2717 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2718 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2721 | void AudioFlinger::PlaybackThread::onWriteReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2722 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2723 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2726 | void AudioFlinger::PlaybackThread::onDrainReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2727 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2728 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2731 | void AudioFlinger::PlaybackThread::onError() |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2732 | { |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2733 | mCallbackThread->setAsyncError(); |
| 2734 | } |
| 2735 | |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2736 | void AudioFlinger::PlaybackThread::onCodecFormatChanged( |
| 2737 | const std::basic_string<uint8_t>& metadataBs) |
| 2738 | { |
| 2739 | std::thread([this, metadataBs]() { |
| 2740 | audio_utils::metadata::Data metadata = |
| 2741 | audio_utils::metadata::dataFromByteString(metadataBs); |
| 2742 | if (metadata.empty()) { |
| 2743 | ALOGW("Can not transform the buffer to audio metadata, %s, %d", |
| 2744 | reinterpret_cast<char*>(const_cast<uint8_t*>(metadataBs.data())), |
| 2745 | (int)metadataBs.size()); |
| 2746 | return; |
| 2747 | } |
| 2748 | |
| 2749 | audio_utils::metadata::ByteString metaDataStr = |
| 2750 | audio_utils::metadata::byteStringFromData(metadata); |
| 2751 | std::vector metadataVec(metaDataStr.begin(), metaDataStr.end()); |
| 2752 | Mutex::Autolock _l(mAudioTrackCbLock); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2753 | for (const auto& callbackPair : mAudioTrackCallbacks) { |
| 2754 | callbackPair.second->onCodecFormatChanged(metadataVec); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2755 | } |
| 2756 | }).detach(); |
| 2757 | } |
| 2758 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2759 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2760 | { |
| 2761 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2762 | // reject out of sequence requests |
| 2763 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2764 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2765 | mWaitWorkCV.signal(); |
| 2766 | } |
| 2767 | } |
| 2768 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2769 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2770 | { |
| 2771 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2772 | // reject out of sequence requests |
| 2773 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 2774 | // Register discontinuity when HW drain is completed because that can cause |
| 2775 | // the timestamp frame position to reset to 0 for direct and offload threads. |
| 2776 | // (Out of sequence requests are ignored, since the discontinuity would be handled |
| 2777 | // elsewhere, e.g. in flush). |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 2778 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2779 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2780 | mWaitWorkCV.signal(); |
| 2781 | } |
| 2782 | } |
| 2783 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2784 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2785 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2786 | // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 2787 | const audio_config_base_t audioConfig = mOutput->getAudioProperties(); |
| 2788 | mSampleRate = audioConfig.sample_rate; |
| 2789 | mChannelMask = audioConfig.channel_mask; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2790 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2791 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2792 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2793 | if ((mType == MIXER || mType == DUPLICATING) |
| 2794 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2795 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2796 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2797 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2798 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 2799 | mBalance.setChannelMask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2800 | |
| 2801 | // Get actual HAL format. |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 2802 | status_t result = mOutput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2803 | LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2804 | // Get format from the shim, which will be different than the HAL format |
| 2805 | // if playing compressed audio over HDMI passthrough. |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 2806 | mFormat = audioConfig.format; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2807 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2808 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2809 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2810 | if ((mType == MIXER || mType == DUPLICATING) |
| 2811 | && !isValidPcmSinkFormat(mFormat)) { |
| 2812 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2813 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2814 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2815 | mFrameSize = mOutput->getFrameSize(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2816 | result = mOutput->stream->getBufferSize(&mBufferSize); |
| 2817 | LOG_ALWAYS_FATAL_IF(result != OK, |
| 2818 | "Error when retrieving output stream buffer size: %d", result); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2819 | mFrameCount = mBufferSize / mFrameSize; |
Dean Wheatley | 77cbebd | 2019-04-23 14:18:44 +1000 | [diff] [blame] | 2820 | if ((mType == MIXER || mType == DUPLICATING) && (mFrameCount & 15)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2821 | ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2822 | mFrameCount); |
| 2823 | } |
| 2824 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2825 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) { |
| 2826 | if (mOutput->stream->setCallback(this) == OK) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2827 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2828 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2832 | mHwSupportsPause = false; |
| 2833 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2834 | bool supportsPause = false, supportsResume = false; |
| 2835 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { |
| 2836 | if (supportsPause && supportsResume) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2837 | mHwSupportsPause = true; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2838 | } else if (supportsPause) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2839 | ALOGW("direct output implements pause but not resume"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2840 | } else if (supportsResume) { |
| 2841 | ALOGW("direct output implements resume but not pause"); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2842 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2843 | } |
| 2844 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2845 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2846 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2847 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2848 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2849 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2850 | // For best precision, we use float instead of the associated output |
| 2851 | // device format (typically PCM 16 bit). |
| 2852 | |
| 2853 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2854 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2855 | mBufferSize = mFrameSize * mFrameCount; |
| 2856 | |
| 2857 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2858 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2859 | // (if a valid mask) to avoid premature downmix. |
| 2860 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2861 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2862 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2863 | } |
| 2864 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2865 | // 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] | 2866 | double multiplier = 1.0; |
| 2867 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2868 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2869 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2870 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2871 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2872 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2873 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2874 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2875 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2876 | maxNormalFrameCount = minNormalFrameCount; |
| 2877 | } |
| 2878 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2879 | if (multiplier <= 1.0) { |
| 2880 | multiplier = 1.0; |
| 2881 | } else if (multiplier <= 2.0) { |
| 2882 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2883 | multiplier = 2.0; |
| 2884 | } else { |
| 2885 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2886 | } |
| 2887 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2888 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | mNormalFrameCount = multiplier * mFrameCount; |
| 2892 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2893 | if (mType == MIXER || mType == DUPLICATING) { |
| 2894 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2895 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2896 | ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", mFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2897 | mNormalFrameCount); |
| 2898 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2899 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2900 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2901 | mThreadThrottleTimeMs = 0; |
| 2902 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2903 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2904 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2905 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2906 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2907 | free(mSinkBuffer); |
| 2908 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2909 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2910 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2911 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2912 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2913 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2914 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2915 | // drives the output. |
| 2916 | free(mMixerBuffer); |
| 2917 | mMixerBuffer = NULL; |
| 2918 | if (mMixerBufferEnabled) { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 2919 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2920 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2921 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2922 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2923 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2924 | free(mEffectBuffer); |
| 2925 | mEffectBuffer = NULL; |
| 2926 | if (mEffectBufferEnabled) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2927 | mEffectBufferFormat = EFFECT_BUFFER_FORMAT; |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2928 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2929 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2930 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2931 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2932 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 2933 | mHapticChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
| 2934 | mChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & ~mHapticChannelMask); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2935 | mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask); |
| 2936 | mChannelCount -= mHapticChannelCount; |
| 2937 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2938 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2939 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2940 | // 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] | 2941 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2942 | // matter. |
| 2943 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2944 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2945 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2946 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), |
| 2947 | this/* srcThread */, this/* dstThread */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2948 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2949 | |
George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 2950 | audio_output_flags_t flags = mOutput->flags; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 2951 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); // TODO: method in ThreadMetrics? |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2952 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
| 2953 | .set(AMEDIAMETRICS_PROP_ENCODING, formatToString(mFormat).c_str()) |
| 2954 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 2955 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 2956 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 2957 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mNormalFrameCount) |
| 2958 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 2959 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, |
| 2960 | (int32_t)mHapticChannelMask) |
| 2961 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, |
| 2962 | (int32_t)mHapticChannelCount) |
| 2963 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING, |
| 2964 | formatToString(mHALFormat).c_str()) |
| 2965 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT, |
| 2966 | (int32_t)mFrameCount) // sic - added HAL |
| 2967 | ; |
| 2968 | uint32_t latencyMs; |
| 2969 | if (mOutput->stream->getLatency(&latencyMs) == NO_ERROR) { |
| 2970 | item.set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_LATENCYMS, (double)latencyMs); |
| 2971 | } |
| 2972 | item.record(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2973 | } |
| 2974 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2975 | void AudioFlinger::PlaybackThread::updateMetadata_l() |
| 2976 | { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2977 | if (mOutput == nullptr || mOutput->stream == nullptr ) { |
| 2978 | return; // That should not happen |
| 2979 | } |
| 2980 | bool hasChanged = mActiveTracks.readAndClearHasChanged(); |
| 2981 | for (const sp<Track> &track : mActiveTracks) { |
| 2982 | // Do not short-circuit as all hasChanged states must be reset |
| 2983 | // as all the metadata are going to be sent |
| 2984 | hasChanged |= track->readAndClearHasChanged(); |
| 2985 | } |
| 2986 | if (!hasChanged) { |
| 2987 | return; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2988 | } |
| 2989 | StreamOutHalInterface::SourceMetadata metadata; |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2990 | auto backInserter = std::back_inserter(metadata.tracks); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2991 | for (const sp<Track> &track : mActiveTracks) { |
| 2992 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
Eric Laurent | b9cc0ab | 2021-01-29 11:46:52 +0100 | [diff] [blame] | 2993 | // Do not forward metadata for PatchTrack with unspecified stream type |
| 2994 | if (track->streamType() != AUDIO_STREAM_PATCH) { |
| 2995 | track->copyMetadataTo(backInserter); |
| 2996 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2997 | } |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2998 | sendMetadataToBackend_l(metadata); |
Kevin Rocard | 80ee272 | 2018-04-11 15:53:48 +0000 | [diff] [blame] | 2999 | } |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 3000 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3001 | void AudioFlinger::PlaybackThread::sendMetadataToBackend_l( |
| 3002 | const StreamOutHalInterface::SourceMetadata& metadata) |
| 3003 | { |
| 3004 | mOutput->stream->updateSourceMetadata(metadata); |
| 3005 | }; |
| 3006 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3007 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3008 | { |
| 3009 | if (halFrames == NULL || dspFrames == NULL) { |
| 3010 | return BAD_VALUE; |
| 3011 | } |
| 3012 | Mutex::Autolock _l(mLock); |
| 3013 | if (initCheck() != NO_ERROR) { |
| 3014 | return INVALID_OPERATION; |
| 3015 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3016 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3017 | *halFrames = framesWritten; |
| 3018 | |
| 3019 | if (isSuspended()) { |
| 3020 | // return an estimation of rendered frames when the output is suspended |
| 3021 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3022 | *dspFrames = (uint32_t) |
| 3023 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3024 | return NO_ERROR; |
| 3025 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3026 | status_t status; |
| 3027 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3028 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3029 | *dspFrames = (size_t)frames; |
| 3030 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3034 | product_strategy_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3035 | { |
| 3036 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 3037 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 3038 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 3039 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 3040 | } |
| 3041 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 3042 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3043 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3044 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 3045 | } |
| 3046 | } |
| 3047 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 3048 | } |
| 3049 | |
| 3050 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3051 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3052 | { |
| 3053 | Mutex::Autolock _l(mLock); |
| 3054 | return mOutput; |
| 3055 | } |
| 3056 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3057 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3058 | { |
| 3059 | Mutex::Autolock _l(mLock); |
| 3060 | AudioStreamOut *output = mOutput; |
| 3061 | mOutput = NULL; |
| 3062 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 3063 | // must push a NULL and wait for ack |
| 3064 | mOutputSink.clear(); |
| 3065 | mPipeSink.clear(); |
| 3066 | mNormalSink.clear(); |
| 3067 | return output; |
| 3068 | } |
| 3069 | |
| 3070 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3071 | sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3072 | { |
| 3073 | if (mOutput == NULL) { |
| 3074 | return NULL; |
| 3075 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3076 | return mOutput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 3080 | { |
| 3081 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 3082 | } |
| 3083 | |
| 3084 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 3085 | { |
| 3086 | if (!isValidSyncEvent(event)) { |
| 3087 | return BAD_VALUE; |
| 3088 | } |
| 3089 | |
| 3090 | Mutex::Autolock _l(mLock); |
| 3091 | |
| 3092 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3093 | sp<Track> track = mTracks[i]; |
| 3094 | if (event->triggerSession() == track->sessionId()) { |
| 3095 | (void) track->setSyncEvent(event); |
| 3096 | return NO_ERROR; |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | return NAME_NOT_FOUND; |
| 3101 | } |
| 3102 | |
| 3103 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 3104 | { |
| 3105 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 3106 | } |
| 3107 | |
| 3108 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 3109 | const Vector< sp<Track> >& tracksToRemove) |
| 3110 | { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3111 | // Miscellaneous track cleanup when removed from the active list, |
| 3112 | // called without Thread lock but synchronized with threadLoop processing. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3113 | #ifdef ADD_BATTERY_DATA |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3114 | for (const auto& track : tracksToRemove) { |
| 3115 | if (track->isExternalTrack()) { |
| 3116 | // to track the speaker usage |
| 3117 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3118 | } |
| 3119 | } |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3120 | #else |
| 3121 | (void)tracksToRemove; // suppress unused warning |
| 3122 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 3126 | { |
| 3127 | if (!mMasterMute) { |
| 3128 | char value[PROPERTY_VALUE_MAX]; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 3129 | if (mOutDeviceTypeAddrs.empty()) { |
| 3130 | ALOGD("ro.audio.silent is ignored since no output device is set"); |
| 3131 | return; |
| 3132 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3133 | if (isSingleDeviceType(outDeviceTypes(), AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) { |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 3134 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 3135 | return; |
| 3136 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3137 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 3138 | char *endptr; |
| 3139 | unsigned long ul = strtoul(value, &endptr, 0); |
| 3140 | if (*endptr == '\0' && ul != 0) { |
| 3141 | ALOGD("Silence is golden"); |
| 3142 | // The setprop command will not allow a property to be changed after |
| 3143 | // the first time it is set, so we don't have to worry about un-muting. |
| 3144 | setMasterMute_l(true); |
| 3145 | } |
| 3146 | } |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3151 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3152 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3153 | LOG_HIST_TS(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3154 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3155 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3156 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3157 | |
| 3158 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 3159 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3160 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3161 | const size_t count = mBytesRemaining / mFrameSize; |
| 3162 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3163 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3164 | // update the setpoint when AudioFlinger::mScreenState changes |
| 3165 | uint32_t screenState = AudioFlinger::mScreenState; |
| 3166 | if (screenState != mScreenState) { |
| 3167 | mScreenState = screenState; |
| 3168 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3169 | if (pipe != NULL) { |
| 3170 | pipe->setAvgFrames((mScreenState & 1) ? |
| 3171 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3172 | } |
| 3173 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3174 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3175 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3176 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3177 | bytesWritten = framesWritten * mFrameSize; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 3178 | #ifdef TEE_SINK |
| 3179 | mTee.write((char *)mSinkBuffer + offset, framesWritten); |
| 3180 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3181 | } else { |
| 3182 | bytesWritten = framesWritten; |
| 3183 | } |
| 3184 | // otherwise use the HAL / AudioStreamOut directly |
| 3185 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3186 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3187 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3188 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3189 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 3190 | mWriteAckSequence += 2; |
| 3191 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3192 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3193 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3194 | } |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3195 | ATRACE_BEGIN("write"); |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 3196 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 3197 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3198 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3199 | ATRACE_END(); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3200 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3201 | if (mUseAsyncWrite && |
| 3202 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 3203 | // 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] | 3204 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3205 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3206 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3207 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3208 | } |
| 3209 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3210 | mNumWrites++; |
| 3211 | mInWrite = false; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3212 | if (mStandby) { |
| 3213 | mThreadMetrics.logBeginInterval(); |
| 3214 | mStandby = false; |
| 3215 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3216 | return bytesWritten; |
| 3217 | } |
| 3218 | |
| 3219 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 3220 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3221 | bool supportsDrain = false; |
| 3222 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3223 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 3224 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3225 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 3226 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3227 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3228 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3229 | } |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 3230 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3231 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 3236 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3237 | { |
| 3238 | Mutex::Autolock _l(mLock); |
| 3239 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 3240 | sp<Track> track = mTracks[i]; |
| 3241 | track->invalidate(); |
| 3242 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3243 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. |
| 3244 | // After we exit there are no more track changes sent to BatteryNotifier |
| 3245 | // because that requires an active threadLoop. |
| 3246 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? |
| 3247 | mActiveTracks.clear(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3248 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3253 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3254 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 3255 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3256 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 3257 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3258 | - maxPeriod from frame count and sample rate (MIXER only) |
| 3259 | |
| 3260 | The parameters that affect these derived values are: |
| 3261 | - frame count |
| 3262 | - frame size |
| 3263 | - sample rate |
| 3264 | - device type: A2DP or not |
| 3265 | - device latency |
| 3266 | - format: PCM or not |
| 3267 | - active sleep time |
| 3268 | - idle sleep time |
| 3269 | */ |
| 3270 | |
| 3271 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 3272 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3273 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3274 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 3275 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3276 | |
| 3277 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 3278 | // truncating audio when going to standby. |
| 3279 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3280 | if (!Intersection(outDeviceTypes(), getAudioDeviceOutAllA2dpSet()).empty()) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3281 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 3282 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 3283 | } |
| 3284 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3285 | } |
| 3286 | |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3287 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3288 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3289 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3290 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3291 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3292 | size_t size = mTracks.size(); |
| 3293 | for (size_t i = 0; i < size; i++) { |
| 3294 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3295 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3296 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3297 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3298 | } |
| 3299 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3300 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3301 | } |
| 3302 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3303 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 3304 | { |
| 3305 | Mutex::Autolock _l(mLock); |
| 3306 | invalidateTracks_l(streamType); |
| 3307 | } |
| 3308 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3309 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 3310 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3311 | audio_session_t session = chain->sessionId(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3312 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 3313 | status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer( |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3314 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, |
| 3315 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, |
| 3316 | &halInBuffer); |
| 3317 | if (result != OK) return result; |
| 3318 | halOutBuffer = halInBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3319 | effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3320 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3321 | if (!audio_is_global_session(session)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3322 | // 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] | 3323 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3324 | if (mType != DIRECT) { |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3325 | size_t numSamples = mNormalFrameCount * (mChannelCount + mHapticChannelCount); |
Kevin Rocard | 7588ff4 | 2018-01-08 11:11:30 -0800 | [diff] [blame] | 3326 | status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer( |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3327 | numSamples * sizeof(effect_buffer_t), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3328 | &halInBuffer); |
| 3329 | if (result != OK) return result; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3330 | #ifdef FLOAT_EFFECT_CHAIN |
| 3331 | buffer = halInBuffer->audioBuffer()->f32; |
| 3332 | #else |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3333 | buffer = halInBuffer->audioBuffer()->s16; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3334 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3335 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 3336 | buffer, session); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | // Attach all tracks with same session ID to this chain. |
| 3340 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3341 | sp<Track> track = mTracks[i]; |
| 3342 | if (session == track->sessionId()) { |
| 3343 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 3344 | buffer); |
| 3345 | track->setMainBuffer(buffer); |
| 3346 | chain->incTrackCnt(); |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | // indicate all active tracks in the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3351 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3352 | if (session == track->sessionId()) { |
| 3353 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 3354 | chain->incActiveTrackCnt(); |
| 3355 | } |
| 3356 | } |
| 3357 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3358 | chain->setThread(this); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3359 | chain->setInBuffer(halInBuffer); |
| 3360 | chain->setOutBuffer(halOutBuffer); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3361 | // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect |
| 3362 | // chains list in order to be processed last as it contains output device effects. |
| 3363 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post |
| 3364 | // processing effects specific to an output stream before effects applied to all streams |
| 3365 | // routed to a given device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3366 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 3367 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3368 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3369 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3370 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3371 | // Effect chain for other sessions are inserted at beginning of effect |
| 3372 | // chains list to be processed before output mix effects. Relative order between other |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3373 | // sessions is not important. |
| 3374 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3375 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX && |
| 3376 | AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3377 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3378 | size_t size = mEffectChains.size(); |
| 3379 | size_t i = 0; |
| 3380 | for (i = 0; i < size; i++) { |
| 3381 | if (mEffectChains[i]->sessionId() < session) { |
| 3382 | break; |
| 3383 | } |
| 3384 | } |
| 3385 | mEffectChains.insertAt(chain, i); |
| 3386 | checkSuspendOnAddEffectChain_l(chain); |
| 3387 | |
| 3388 | return NO_ERROR; |
| 3389 | } |
| 3390 | |
| 3391 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 3392 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3393 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3394 | |
| 3395 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 3396 | |
| 3397 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3398 | if (chain == mEffectChains[i]) { |
| 3399 | mEffectChains.removeAt(i); |
| 3400 | // detach all active tracks from the chain |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3401 | for (const sp<Track> &track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3402 | if (session == track->sessionId()) { |
| 3403 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 3404 | chain.get(), session); |
| 3405 | chain->decActiveTrackCnt(); |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | // detach all tracks with same session ID from this chain |
| 3410 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3411 | sp<Track> track = mTracks[i]; |
| 3412 | if (session == track->sessionId()) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 3413 | track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3414 | chain->decTrackCnt(); |
| 3415 | } |
| 3416 | } |
| 3417 | break; |
| 3418 | } |
| 3419 | } |
| 3420 | return mEffectChains.size(); |
| 3421 | } |
| 3422 | |
| 3423 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3424 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3425 | { |
| 3426 | Mutex::Autolock _l(mLock); |
| 3427 | return attachAuxEffect_l(track, EffectId); |
| 3428 | } |
| 3429 | |
| 3430 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3431 | const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3432 | { |
| 3433 | status_t status = NO_ERROR; |
| 3434 | |
| 3435 | if (EffectId == 0) { |
| 3436 | track->setAuxBuffer(0, NULL); |
| 3437 | } else { |
| 3438 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 3439 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 3440 | if (effect != 0) { |
| 3441 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 3442 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 3443 | } else { |
| 3444 | status = INVALID_OPERATION; |
| 3445 | } |
| 3446 | } else { |
| 3447 | status = BAD_VALUE; |
| 3448 | } |
| 3449 | } |
| 3450 | return status; |
| 3451 | } |
| 3452 | |
| 3453 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 3454 | { |
| 3455 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 3456 | sp<Track> track = mTracks[i]; |
| 3457 | if (track->auxEffectId() == effectId) { |
| 3458 | attachAuxEffect_l(track, 0); |
| 3459 | } |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 3464 | { |
Glenn Kasten | 388d571 | 2017-04-07 14:38:41 -0700 | [diff] [blame] | 3465 | tlNBLogWriter = mNBLogWriter.get(); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3466 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3467 | Vector< sp<Track> > tracksToRemove; |
| 3468 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3469 | mStandbyTimeNs = systemTime(); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3470 | int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3471 | |
| 3472 | // MIXER |
| 3473 | nsecs_t lastWarning = 0; |
| 3474 | |
| 3475 | // DUPLICATING |
| 3476 | // FIXME could this be made local to while loop? |
| 3477 | writeFrames = 0; |
| 3478 | |
| 3479 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3480 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3481 | |
| 3482 | if (mType == MIXER) { |
| 3483 | sleepTimeShift = 0; |
| 3484 | } |
| 3485 | |
| 3486 | CpuStats cpuStats; |
| 3487 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 3488 | |
| 3489 | acquireWakeLock(); |
| 3490 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3491 | // mNBLogWriter logging APIs can only be called by a single thread, typically the |
| 3492 | // thread associated with this PlaybackThread. |
| 3493 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) |
| 3494 | // then all such threads must agree to hold a common mutex before logging. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3495 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 3496 | // and then that string will be logged at the next convenient opportunity. |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3497 | // See reference to logString below. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3498 | const char *logString = NULL; |
| 3499 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3500 | // Estimated time for next buffer to be written to hal. This is used only on |
| 3501 | // suspended mode (for now) to help schedule the wait time until next iteration. |
| 3502 | nsecs_t timeLoopNextNs = 0; |
| 3503 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 3504 | checkSilentMode_l(); |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3505 | |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3506 | audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 3507 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3508 | // loopCount is used for statistics and diagnostics. |
| 3509 | for (int64_t loopCount = 0; !exitPending(); ++loopCount) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3510 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 3511 | // Log merge requests are performed during AudioFlinger binder transactions, but |
| 3512 | // that does not cover audio playback. It's requested here for that reason. |
| 3513 | mAudioFlinger->requestLogMerge(); |
| 3514 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3515 | cpuStats.sample(myName); |
| 3516 | |
| 3517 | Vector< sp<EffectChain> > effectChains; |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 3518 | audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE; |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 3519 | std::vector<sp<Track>> activeTracks; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3520 | |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3521 | // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency. |
| 3522 | // |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3523 | // Note: we access outDeviceTypes() outside of mLock. |
| 3524 | if (isMsdDevice() && outDeviceTypes().count(AUDIO_DEVICE_OUT_BUS) != 0) { |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3525 | // Here, we try for the AF lock, but do not block on it as the latency |
| 3526 | // is more informational. |
| 3527 | if (mAudioFlinger->mLock.tryLock() == NO_ERROR) { |
| 3528 | std::vector<PatchPanel::SoftwarePatch> swPatches; |
| 3529 | double latencyMs; |
| 3530 | status_t status = INVALID_OPERATION; |
| 3531 | audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3532 | if (mAudioFlinger->mPatchPanel.getDownstreamSoftwarePatches(id(), &swPatches) == OK |
| 3533 | && swPatches.size() > 0) { |
| 3534 | status = swPatches[0].getLatencyMs_l(&latencyMs); |
| 3535 | downstreamPatchHandle = swPatches[0].getPatchHandle(); |
| 3536 | } |
| 3537 | if (downstreamPatchHandle != lastDownstreamPatchHandle) { |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 3538 | mDownstreamLatencyStatMs.reset(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3539 | lastDownstreamPatchHandle = downstreamPatchHandle; |
| 3540 | } |
| 3541 | if (status == OK) { |
| 3542 | // verify downstream latency (we assume a max reasonable |
Mikhail Naganov | 6aa0a31 | 2018-11-09 11:00:01 -0800 | [diff] [blame] | 3543 | // latency of 5 seconds). |
| 3544 | const double minLatency = 0., maxLatency = 5000.; |
| 3545 | if (latencyMs >= minLatency && latencyMs <= maxLatency) { |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 3546 | ALOGVV("new downstream latency %lf ms", latencyMs); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3547 | } else { |
| 3548 | ALOGD("out of range downstream latency %lf ms", latencyMs); |
Mikhail Naganov | 6aa0a31 | 2018-11-09 11:00:01 -0800 | [diff] [blame] | 3549 | if (latencyMs < minLatency) latencyMs = minLatency; |
| 3550 | else if (latencyMs > maxLatency) latencyMs = maxLatency; |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3551 | } |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 3552 | mDownstreamLatencyStatMs.add(latencyMs); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3553 | } |
| 3554 | mAudioFlinger->mLock.unlock(); |
| 3555 | } |
| 3556 | } else { |
| 3557 | if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 3558 | // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats. |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 3559 | mDownstreamLatencyStatMs.reset(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 3560 | lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3561 | } |
| 3562 | } |
| 3563 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3564 | { // scope for mLock |
| 3565 | |
| 3566 | Mutex::Autolock _l(mLock); |
| 3567 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3568 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3569 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 3570 | // See comment at declaration of logString for why this is done under mLock |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3571 | if (logString != NULL) { |
| 3572 | mNBLogWriter->logTimestamp(); |
| 3573 | mNBLogWriter->log(logString); |
| 3574 | logString = NULL; |
| 3575 | } |
| 3576 | |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 3577 | collectTimestamps_l(); |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 3578 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3579 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3580 | if (mSignalPending) { |
| 3581 | // A signal was raised while we were unlocked |
| 3582 | mSignalPending = false; |
| 3583 | } else if (waitingAsyncCallback_l()) { |
| 3584 | if (exitPending()) { |
| 3585 | break; |
| 3586 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3587 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3588 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3589 | releaseWakeLock_l(); |
| 3590 | released = true; |
| 3591 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 3592 | |
| 3593 | const int64_t waitNs = computeWaitTimeNs_l(); |
| 3594 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); |
| 3595 | status_t status = mWaitWorkCV.waitRelative(mLock, waitNs); |
| 3596 | if (status == TIMED_OUT) { |
| 3597 | mSignalPending = true; // if timeout recheck everything |
| 3598 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3599 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3600 | if (released) { |
| 3601 | acquireWakeLock_l(); |
| 3602 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3603 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3604 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3605 | |
| 3606 | continue; |
| 3607 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 3608 | if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3609 | isSuspended()) { |
| 3610 | // put audio hardware into standby after short delay |
| 3611 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 | |
| 3613 | threadLoop_standby(); |
| 3614 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3615 | // This is where we go into standby |
| 3616 | if (!mStandby) { |
| 3617 | LOG_AUDIO_STATE(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3618 | mThreadMetrics.logEndInterval(); |
| 3619 | mStandby = true; |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3620 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 3621 | sendStatistics(false /* force */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3622 | } |
| 3623 | |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 3624 | if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3625 | // we're about to wait, flush the binder command buffer |
| 3626 | IPCThreadState::self()->flushCommands(); |
| 3627 | |
| 3628 | clearOutputTracks(); |
| 3629 | |
| 3630 | if (exitPending()) { |
| 3631 | break; |
| 3632 | } |
| 3633 | |
| 3634 | releaseWakeLock_l(); |
| 3635 | // wait until we have something to do... |
| 3636 | ALOGV("%s going to sleep", myName.string()); |
| 3637 | mWaitWorkCV.wait(mLock); |
| 3638 | ALOGV("%s waking up", myName.string()); |
| 3639 | acquireWakeLock_l(); |
| 3640 | |
| 3641 | mMixerStatus = MIXER_IDLE; |
| 3642 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 3643 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3644 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3645 | checkSilentMode_l(); |
| 3646 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3647 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3648 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3649 | if (mType == MIXER) { |
| 3650 | sleepTimeShift = 0; |
| 3651 | } |
| 3652 | |
| 3653 | continue; |
| 3654 | } |
| 3655 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3656 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 3657 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 3658 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3659 | mActiveTracks.updatePowerState(this); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3660 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3661 | updateMetadata_l(); |
| 3662 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3663 | // prevent any changes in effect chain list and in each effect chain |
| 3664 | // during mixing and effect process as the audio buffers could be deleted |
| 3665 | // or modified if an effect is created or deleted |
| 3666 | lockEffectChains_l(effectChains); |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 3667 | |
| 3668 | // Determine which session to pick up haptic data. |
| 3669 | // This must be done under the same lock as prepareTracks_l(). |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 3670 | // The haptic data from the effect is at a higher priority than the one from track. |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 3671 | // TODO: Write haptic data directly to sink buffer when mixing. |
| 3672 | if (mHapticChannelCount > 0 && effectChains.size() > 0) { |
| 3673 | for (const auto& track : mActiveTracks) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 3674 | sp<EffectChain> effectChain = getEffectChain_l(track->sessionId()); |
| 3675 | if (effectChain != nullptr && effectChain->containsHapticGeneratingEffect_l()) { |
| 3676 | activeHapticSessionId = track->sessionId(); |
| 3677 | break; |
| 3678 | } |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 3679 | if (track->getHapticPlaybackEnabled()) { |
| 3680 | activeHapticSessionId = track->sessionId(); |
| 3681 | break; |
| 3682 | } |
| 3683 | } |
| 3684 | } |
| 3685 | |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 3686 | // Acquire a local copy of active tracks with lock (release w/o lock). |
| 3687 | // |
| 3688 | // Control methods on the track acquire the ThreadBase lock (e.g. start() |
| 3689 | // stop(), pause(), etc.), but the threadLoop is entitled to call audio |
| 3690 | // data / buffer methods on tracks from activeTracks without the ThreadBase lock. |
| 3691 | activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end()); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3692 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3693 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3694 | if (mBytesRemaining == 0) { |
| 3695 | mCurrentWriteLength = 0; |
| 3696 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3697 | // threadLoop_mix() sets mCurrentWriteLength |
| 3698 | threadLoop_mix(); |
| 3699 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3700 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3701 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3702 | // must be written to HAL |
| 3703 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3704 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3705 | mCurrentWriteLength = mSinkBufferSize; |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 3706 | |
| 3707 | // Tally underrun frames as we are inserting 0s here. |
| 3708 | for (const auto& track : activeTracks) { |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 3709 | if (track->mFillingUpStatus == Track::FS_ACTIVE |
| 3710 | && !track->isStopped() |
| 3711 | && !track->isPaused() |
| 3712 | && !track->isTerminated()) { |
| 3713 | ALOGV("%s: track(%d) %s underrun due to thread sleep of %zu frames", |
| 3714 | __func__, track->id(), track->getTrackStateAsString(), |
| 3715 | mNormalFrameCount); |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 3716 | track->mAudioTrackServerProxy->tallyUnderrunFrames(mNormalFrameCount); |
| 3717 | } |
| 3718 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3719 | } |
| 3720 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3721 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3722 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3723 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3724 | // or mSinkBuffer (if there are no effects). |
| 3725 | // |
| 3726 | // This is done pre-effects computation; if effects change to |
| 3727 | // support higher precision, this needs to move. |
| 3728 | // |
| 3729 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3730 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3731 | if (mMixerBufferValid) { |
| 3732 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3733 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3734 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3735 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3736 | // and is handled here if we're going directly to the sink. |
| 3737 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3738 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3739 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3740 | } |
| 3741 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3742 | if (!hasFastMixer()) { |
| 3743 | // Balance must take effect after mono conversion. |
| 3744 | // We do it here if there is no FastMixer. |
| 3745 | // mBalance detects zero balance within the class for speed (not needed here). |
| 3746 | mBalance.setBalance(mMasterBalance.load()); |
| 3747 | mBalance.process((float *)mMixerBuffer, mNormalFrameCount); |
| 3748 | } |
| 3749 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3750 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3751 | mNormalFrameCount * (mChannelCount + mHapticChannelCount)); |
| 3752 | |
| 3753 | // If we're going directly to the sink and there are haptic channels, |
| 3754 | // we should adjust channels as the sample data is partially interleaved |
| 3755 | // in this case. |
| 3756 | if (!mEffectBufferValid && mHapticChannelCount > 0) { |
| 3757 | adjust_channels_non_destructive(buffer, mChannelCount, buffer, |
| 3758 | mChannelCount + mHapticChannelCount, |
| 3759 | audio_bytes_per_sample(format), |
| 3760 | audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount); |
| 3761 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3762 | } |
| 3763 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3764 | mBytesRemaining = mCurrentWriteLength; |
| 3765 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3766 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 3767 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 3768 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 3769 | mBytesWritten += mBytesRemaining; |
| 3770 | mFramesWritten += framesRemaining; |
| 3771 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3772 | mBytesRemaining = 0; |
| 3773 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3774 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3775 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3776 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3777 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3778 | effectChains[i]->process_l(); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 3779 | // TODO: Write haptic data directly to sink buffer when mixing. |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 3780 | if (activeHapticSessionId != AUDIO_SESSION_NONE |
| 3781 | && activeHapticSessionId == effectChains[i]->sessionId()) { |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 3782 | // Haptic data is active in this case, copy it directly from |
| 3783 | // in buffer to out buffer. |
| 3784 | const size_t audioBufferSize = mNormalFrameCount |
| 3785 | * audio_bytes_per_frame(mChannelCount, EFFECT_BUFFER_FORMAT); |
| 3786 | memcpy_by_audio_format( |
| 3787 | (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize, |
| 3788 | EFFECT_BUFFER_FORMAT, |
| 3789 | (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize, |
| 3790 | EFFECT_BUFFER_FORMAT, mNormalFrameCount * mHapticChannelCount); |
| 3791 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3792 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3793 | } |
| 3794 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3795 | // Process effect chains for offloaded thread even if no audio |
| 3796 | // was read from audio track: process only updates effect state |
| 3797 | // and thus does have to be synchronized with audio writes but may have |
| 3798 | // to be called while waiting for async write callback |
| 3799 | if (mType == OFFLOAD) { |
| 3800 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3801 | effectChains[i]->process_l(); |
| 3802 | } |
| 3803 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3804 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3805 | // Only if the Effects buffer is enabled and there is data in the |
| 3806 | // Effects buffer (buffer valid), we need to |
| 3807 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3808 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3809 | if (mEffectBufferValid) { |
| 3810 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3811 | |
| 3812 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3813 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3814 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3815 | } |
| 3816 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3817 | if (!hasFastMixer()) { |
| 3818 | // Balance must take effect after mono conversion. |
| 3819 | // We do it here if there is no FastMixer. |
| 3820 | // mBalance detects zero balance within the class for speed (not needed here). |
| 3821 | mBalance.setBalance(mMasterBalance.load()); |
| 3822 | mBalance.process((float *)mEffectBuffer, mNormalFrameCount); |
| 3823 | } |
| 3824 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3825 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3826 | mNormalFrameCount * (mChannelCount + mHapticChannelCount)); |
| 3827 | // The sample data is partially interleaved when haptic channels exist, |
| 3828 | // we need to adjust channels here. |
| 3829 | if (mHapticChannelCount > 0) { |
| 3830 | adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer, |
| 3831 | mChannelCount + mHapticChannelCount, |
| 3832 | audio_bytes_per_sample(mFormat), |
| 3833 | audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount); |
| 3834 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3835 | } |
| 3836 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3837 | // enable changes in effect chain |
| 3838 | unlockEffectChains(effectChains); |
| 3839 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3840 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3841 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3842 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3843 | ssize_t ret = 0; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3844 | // writePeriodNs is updated >= 0 when ret > 0. |
| 3845 | int64_t writePeriodNs = -1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3846 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3847 | // FIXME rewrite to reduce number of system calls |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3848 | const int64_t lastIoBeginNs = systemTime(); |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3849 | ret = threadLoop_write(); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3850 | const int64_t lastIoEndNs = systemTime(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3851 | if (ret < 0) { |
| 3852 | mBytesRemaining = 0; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3853 | } else if (ret > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3854 | mBytesWritten += ret; |
| 3855 | mBytesRemaining -= ret; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3856 | const int64_t frames = ret / mFrameSize; |
| 3857 | mFramesWritten += frames; |
| 3858 | |
| 3859 | writePeriodNs = lastIoEndNs - mLastIoEndNs; |
| 3860 | // process information relating to write time. |
| 3861 | if (audio_has_proportional_frames(mFormat)) { |
| 3862 | // we are in a continuous mixing cycle |
| 3863 | if (mMixerStatus == MIXER_TRACKS_READY && |
| 3864 | loopCount == lastLoopCountWritten + 1) { |
| 3865 | |
| 3866 | const double jitterMs = |
| 3867 | TimestampVerifier<int64_t, int64_t>::computeJitterMs( |
| 3868 | {frames, writePeriodNs}, |
| 3869 | {0, 0} /* lastTimestamp */, mSampleRate); |
| 3870 | const double processMs = |
| 3871 | (lastIoBeginNs - mLastIoEndNs) * 1e-6; |
| 3872 | |
| 3873 | Mutex::Autolock _l(mLock); |
| 3874 | mIoJitterMs.add(jitterMs); |
| 3875 | mProcessTimeMs.add(processMs); |
| 3876 | } |
| 3877 | |
| 3878 | // write blocked detection |
| 3879 | const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs; |
| 3880 | if (mType == MIXER && deltaWriteNs > maxPeriod) { |
| 3881 | mNumDelayedWrites++; |
| 3882 | if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) { |
| 3883 | ATRACE_NAME("underrun"); |
| 3884 | ALOGW("write blocked for %lld msecs, " |
| 3885 | "%d delayed writes, thread %d", |
| 3886 | (long long)deltaWriteNs / NANOS_PER_MILLISECOND, |
| 3887 | mNumDelayedWrites, mId); |
| 3888 | lastWarning = lastIoEndNs; |
| 3889 | } |
| 3890 | } |
| 3891 | } |
| 3892 | // update timing info. |
| 3893 | mLastIoBeginNs = lastIoBeginNs; |
| 3894 | mLastIoEndNs = lastIoEndNs; |
| 3895 | lastLoopCountWritten = loopCount; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3896 | } |
| 3897 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3898 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3899 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3900 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3901 | if (mType == MIXER && !mStandby) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3902 | |
| 3903 | if (mThreadThrottle |
| 3904 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3905 | && writePeriodNs > 0) { // we have write period info |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3906 | // Limit MixerThread data processing to no more than twice the |
| 3907 | // expected processing rate. |
| 3908 | // |
| 3909 | // This helps prevent underruns with NuPlayer and other applications |
| 3910 | // which may set up buffers that are close to the minimum size, or use |
| 3911 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3912 | // |
| 3913 | // The throttle smooths out sudden large data drains from the device, |
| 3914 | // e.g. when it comes out of standby, which often causes problems with |
| 3915 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3916 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3917 | // the app won't fill fast enough to handle the sudden draw). |
Haynes Mathew George | f92b217 | 2016-05-09 11:34:15 -0700 | [diff] [blame] | 3918 | // |
| 3919 | // Total time spent in last processing cycle equals time spent in |
| 3920 | // 1. threadLoop_write, as well as time spent in |
| 3921 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 3922 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3923 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3924 | // it's OK if deltaMs is an overestimate. |
| 3925 | |
| 3926 | const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND; |
Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 3927 | |
Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 3928 | const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3929 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3930 | mThreadMetrics.logThrottleMs((double)throttleMs); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3931 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3932 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3933 | // notify of throttle start on verbose log |
| 3934 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3935 | "mixer(%p) throttle begin:" |
| 3936 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3937 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3938 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3939 | // Throttle must be attributed to the previous mixer loop's write time |
| 3940 | // to allow back-to-back throttling. |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 3941 | // This also ensures proper timing statistics. |
| 3942 | mLastIoEndNs = systemTime(); // we fetch the write end time again. |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3943 | } else { |
| 3944 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3945 | if (diff > 0) { |
| 3946 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3947 | // but prevent spamming for bluetooth |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3948 | ALOGD_IF(!isSingleDeviceType( |
| 3949 | outDeviceTypes(), audio_is_a2dp_out_device) && |
| 3950 | !isSingleDeviceType( |
| 3951 | outDeviceTypes(), audio_is_hearing_aid_out_device), |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3952 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3953 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3954 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3955 | } |
| 3956 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3957 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3958 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3959 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3960 | ATRACE_BEGIN("sleep"); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3961 | Mutex::Autolock _l(mLock); |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 3962 | // suspended requires accurate metering of sleep time. |
| 3963 | if (isSuspended()) { |
| 3964 | // advance by expected sleepTime |
| 3965 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); |
| 3966 | const nsecs_t nowNs = systemTime(); |
| 3967 | |
| 3968 | // compute expected next time vs current time. |
| 3969 | // (negative deltas are treated as delays). |
| 3970 | nsecs_t deltaNs = timeLoopNextNs - nowNs; |
| 3971 | if (deltaNs < -kMaxNextBufferDelayNs) { |
| 3972 | // Delays longer than the max allowed trigger a reset. |
| 3973 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); |
| 3974 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); |
| 3975 | timeLoopNextNs = nowNs + deltaNs; |
| 3976 | } else if (deltaNs < 0) { |
| 3977 | // Delays within the max delay allowed: zero the delta/sleepTime |
| 3978 | // to help the system catch up in the next iteration(s) |
| 3979 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); |
| 3980 | deltaNs = 0; |
| 3981 | } |
| 3982 | // update sleep time (which is >= 0) |
| 3983 | mSleepTimeUs = deltaNs / 1000; |
| 3984 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3985 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
| 3986 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3987 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3988 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3989 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | // Finally let go of removed track(s), without the lock held |
| 3993 | // since we can't guarantee the destructors won't acquire that |
| 3994 | // same lock. This will also mutate and push a new fast mixer state. |
| 3995 | threadLoop_removeTracks(tracksToRemove); |
| 3996 | tracksToRemove.clear(); |
| 3997 | |
| 3998 | // FIXME I don't understand the need for this here; |
| 3999 | // it was in the original code but maybe the |
| 4000 | // assignment in saveOutputTracks() makes this unnecessary? |
| 4001 | clearOutputTracks(); |
| 4002 | |
| 4003 | // Effect chains will be actually deleted here if they were removed from |
| 4004 | // mEffectChains list during mixing or effects processing |
| 4005 | effectChains.clear(); |
| 4006 | |
| 4007 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 4008 | // is now local to this block, but will keep it for now (at least until merge done). |
| 4009 | } |
| 4010 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4011 | threadLoop_exit(); |
| 4012 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 4013 | if (!mStandby) { |
| 4014 | threadLoop_standby(); |
| 4015 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | releaseWakeLock(); |
| 4019 | |
| 4020 | ALOGV("Thread %p type %d exiting", this, mType); |
| 4021 | return false; |
| 4022 | } |
| 4023 | |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4024 | void AudioFlinger::PlaybackThread::collectTimestamps_l() |
| 4025 | { |
| 4026 | // Collect timestamp statistics for the Playback Thread types that support it. |
| 4027 | if (mType != MIXER |
| 4028 | && mType != DUPLICATING |
| 4029 | && mType != DIRECT |
| 4030 | && mType != OFFLOAD) { |
| 4031 | return; |
| 4032 | } |
| 4033 | if (mStandby) { |
| 4034 | mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); |
| 4035 | return; |
| 4036 | } else if (mHwPaused) { |
| 4037 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); |
| 4038 | return; |
| 4039 | } |
| 4040 | |
| 4041 | // Gather the framesReleased counters for all active tracks, |
| 4042 | // and associate with the sink frames written out. We need |
| 4043 | // this to convert the sink timestamp to the track timestamp. |
| 4044 | bool kernelLocationUpdate = false; |
| 4045 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 4046 | |
| 4047 | // Always query HAL timestamp and update timestamp verifier. In standby or pause, |
| 4048 | // HAL may be draining some small duration buffered data for fade out. |
| 4049 | if (threadloop_getHalTimestamp_l(×tamp) == OK) { |
| 4050 | mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 4051 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4052 | mSampleRate); |
| 4053 | |
| 4054 | if (isTimestampCorrectionEnabled()) { |
| 4055 | ALOGVV("TS_BEFORE: %d %lld %lld", id(), |
| 4056 | (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4057 | (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); |
| 4058 | auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); |
| 4059 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4060 | = correctedTimestamp.mFrames; |
| 4061 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] |
| 4062 | = correctedTimestamp.mTimeNs; |
| 4063 | ALOGVV("TS_AFTER: %d %lld %lld", id(), |
| 4064 | (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4065 | (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); |
| 4066 | |
| 4067 | // Note: Downstream latency only added if timestamp correction enabled. |
| 4068 | if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info. |
| 4069 | const int64_t newPosition = |
| 4070 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4071 | - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3); |
| 4072 | // prevent retrograde |
| 4073 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max( |
| 4074 | newPosition, |
| 4075 | (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4076 | - mSuspendedFrames)); |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | // We always fetch the timestamp here because often the downstream |
| 4081 | // sink will block while writing. |
| 4082 | |
| 4083 | // We keep track of the last valid kernel position in case we are in underrun |
| 4084 | // and the normal mixer period is the same as the fast mixer period, or there |
| 4085 | // is some error from the HAL. |
| 4086 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 4087 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 4088 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4089 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 4090 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4091 | |
| 4092 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 4093 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 4094 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 4095 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
| 4096 | } |
| 4097 | |
| 4098 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 4099 | kernelLocationUpdate = true; |
| 4100 | } else { |
| 4101 | ALOGVV("getTimestamp error - no valid kernel position"); |
| 4102 | } |
| 4103 | |
| 4104 | // copy over kernel info |
| 4105 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
| 4106 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4107 | + mSuspendedFrames; // add frames discarded when suspended |
| 4108 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 4109 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4110 | } else { |
| 4111 | mTimestampVerifier.error(); |
| 4112 | } |
| 4113 | |
| 4114 | // mFramesWritten for non-offloaded tracks are contiguous |
| 4115 | // even after standby() is called. This is useful for the track frame |
| 4116 | // to sink frame mapping. |
| 4117 | bool serverLocationUpdate = false; |
| 4118 | if (mFramesWritten != mLastFramesWritten) { |
| 4119 | serverLocationUpdate = true; |
| 4120 | mLastFramesWritten = mFramesWritten; |
| 4121 | } |
| 4122 | // Only update timestamps if there is a meaningful change. |
| 4123 | // Either the kernel timestamp must be valid or we have written something. |
| 4124 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 4125 | if (serverLocationUpdate) { |
| 4126 | // use the time before we called the HAL write - it is a bit more accurate |
| 4127 | // to when the server last read data than the current time here. |
| 4128 | // |
| 4129 | // If we haven't written anything, mLastIoBeginNs will be -1 |
| 4130 | // and we use systemTime(). |
| 4131 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 4132 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1 |
| 4133 | ? systemTime() : mLastIoBeginNs; |
| 4134 | } |
| 4135 | |
| 4136 | for (const sp<Track> &t : mActiveTracks) { |
| 4137 | if (!t->isFastTrack()) { |
| 4138 | t->updateTrackFrameInfo( |
| 4139 | t->mAudioTrackServerProxy->framesReleased(), |
| 4140 | mFramesWritten, |
| 4141 | mSampleRate, |
| 4142 | mTimestamp); |
| 4143 | } |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | if (audio_has_proportional_frames(mFormat)) { |
| 4148 | const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate); |
| 4149 | if (latencyMs != 0.) { // note 0. means timestamp is empty. |
| 4150 | mLatencyMs.add(latencyMs); |
| 4151 | } |
| 4152 | } |
| 4153 | #if 0 |
| 4154 | // logFormat example |
| 4155 | if (z % 100 == 0) { |
| 4156 | timespec ts; |
| 4157 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 4158 | LOGT("This is an integer %d, this is a float %f, this is my " |
| 4159 | "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts); |
| 4160 | LOGT("A deceptive null-terminated string %\0"); |
| 4161 | } |
| 4162 | ++z; |
| 4163 | #endif |
| 4164 | } |
| 4165 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4166 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 4167 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 4168 | { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4169 | for (const auto& track : tracksToRemove) { |
| 4170 | mActiveTracks.remove(track); |
| 4171 | ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId()); |
| 4172 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 4173 | if (chain != 0) { |
| 4174 | ALOGV("%s(%d): stopping track on chain %p for session Id: %d", |
| 4175 | __func__, track->id(), chain.get(), track->sessionId()); |
| 4176 | chain->decActiveTrackCnt(); |
| 4177 | } |
| 4178 | // If an external client track, inform APM we're no longer active, and remove if needed. |
| 4179 | // We do this under lock so that the state is consistent if the Track is destroyed. |
| 4180 | if (track->isExternalTrack()) { |
| 4181 | AudioSystem::stopOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4182 | if (track->isTerminated()) { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4183 | AudioSystem::releaseOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4184 | } |
| 4185 | } |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4186 | if (track->isTerminated()) { |
| 4187 | // remove from our tracks vector |
| 4188 | removeTrack_l(track); |
| 4189 | } |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4190 | if (mHapticChannelCount > 0 && |
| 4191 | ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
| 4192 | || (chain != nullptr && chain->containsHapticGeneratingEffect_l()))) { |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 4193 | mLock.unlock(); |
| 4194 | // Unlock due to VibratorService will lock for this call and will |
| 4195 | // call Tracks.mute/unmute which also require thread's lock. |
| 4196 | AudioFlinger::onExternalVibrationStop(track->getExternalVibration()); |
| 4197 | mLock.lock(); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4198 | |
| 4199 | // When the track is stop, set the haptic intensity as MUTE |
| 4200 | // for the HapticGenerator effect. |
| 4201 | if (chain != nullptr) { |
| 4202 | chain->setHapticIntensity_l(track->id(), static_cast<int>(os::HapticScale::MUTE)); |
| 4203 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4204 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4205 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4206 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4207 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4208 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 4209 | { |
| 4210 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4211 | ExtendedTimestamp ets; |
| 4212 | status_t status = mNormalSink->getTimestamp(ets); |
| 4213 | if (status == NO_ERROR) { |
| 4214 | status = ets.getBestTimestamp(×tamp); |
| 4215 | } |
| 4216 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4217 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4218 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4219 | collectTimestamps_l(); |
| 4220 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] <= 0) { |
| 4221 | return INVALID_OPERATION; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4222 | } |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4223 | timestamp.mPosition = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4224 | const int64_t timeNs = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4225 | timestamp.mTime.tv_sec = timeNs / NANOS_PER_SECOND; |
| 4226 | timestamp.mTime.tv_nsec = timeNs - (timestamp.mTime.tv_sec * NANOS_PER_SECOND); |
| 4227 | return NO_ERROR; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4228 | } |
| 4229 | return INVALID_OPERATION; |
| 4230 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4231 | |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4232 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is |
| 4233 | // still applied by the mixer. |
| 4234 | // All tracks attached to a mixer with flag VOIP_RX are tied to the same |
| 4235 | // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even |
| 4236 | // if more than one track are active |
| 4237 | status_t AudioFlinger::PlaybackThread::handleVoipVolume_l(float *volume) |
| 4238 | { |
| 4239 | status_t result = NO_ERROR; |
| 4240 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { |
| 4241 | if (*volume != mLeftVolFloat) { |
| 4242 | result = mOutput->stream->setVolume(*volume, *volume); |
| 4243 | ALOGE_IF(result != OK, |
| 4244 | "Error when setting output stream volume: %d", result); |
| 4245 | if (result == NO_ERROR) { |
| 4246 | mLeftVolFloat = *volume; |
| 4247 | } |
| 4248 | } |
| 4249 | // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we |
| 4250 | // remove stream volume contribution from software volume. |
| 4251 | if (mLeftVolFloat == *volume) { |
| 4252 | *volume = 1.0f; |
| 4253 | } |
| 4254 | } |
| 4255 | return result; |
| 4256 | } |
| 4257 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4258 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 4259 | audio_patch_handle_t *handle) |
| 4260 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 4261 | status_t status; |
| 4262 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 4263 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 4264 | // or if HAL does not properly lock against access. |
| 4265 | AutoPark<FastMixer> park(mFastMixer); |
| 4266 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 4267 | } else { |
| 4268 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 4269 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4270 | return status; |
| 4271 | } |
| 4272 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4273 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 4274 | audio_patch_handle_t *handle) |
| 4275 | { |
| 4276 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4277 | |
| 4278 | // store new device and send to effects |
| 4279 | audio_devices_t type = AUDIO_DEVICE_NONE; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4280 | AudioDeviceTypeAddrVector deviceTypeAddrs; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4281 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4282 | LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 |
| 4283 | && !mOutput->audioHwDev->supportsAudioPatches(), |
| 4284 | "Enumerated device type(%#x) must not be used " |
| 4285 | "as it does not support audio patches", |
| 4286 | patch->sinks[i].ext.device.type); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4287 | type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4288 | deviceTypeAddrs.push_back(AudioDeviceTypeAddr(patch->sinks[i].ext.device.type, |
| 4289 | patch->sinks[i].ext.device.address)); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 4292 | audio_port_handle_t sinkPortId = patch->sinks[0].id; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4293 | #ifdef ADD_BATTERY_DATA |
| 4294 | // when changing the audio output device, call addBatteryData to notify |
| 4295 | // the change |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4296 | if (outDeviceTypes() != deviceTypes) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4297 | uint32_t params = 0; |
| 4298 | // check whether speaker is on |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4299 | if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4300 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4301 | } |
| 4302 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4303 | // check if any other device (except speaker) is on |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4304 | if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4305 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4306 | } |
| 4307 | |
| 4308 | if (params != 0) { |
| 4309 | addBatteryData(params); |
| 4310 | } |
| 4311 | } |
| 4312 | #endif |
| 4313 | |
| 4314 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 4315 | mEffectChains[i]->setDevices_l(deviceTypeAddrs); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4316 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 4317 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4318 | // mPatch.num_sinks is not set when the thread is created so that |
| 4319 | // the first patch creation triggers an ioConfigChanged callback |
| 4320 | bool configChanged = (mPatch.num_sinks == 0) || |
| 4321 | (mPatch.sinks[0].id != sinkPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 4322 | mPatch = *patch; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4323 | mOutDeviceTypeAddrs = deviceTypeAddrs; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 4324 | checkSilentMode_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4325 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 4326 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 4327 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 4328 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 4329 | patch->sources, |
| 4330 | patch->num_sinks, |
| 4331 | patch->sinks, |
| 4332 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4333 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4334 | char *address; |
| 4335 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 4336 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 4337 | address = audio_device_address_to_parameter( |
| 4338 | patch->sinks[0].ext.device.type, |
| 4339 | patch->sinks[0].ext.device.address); |
| 4340 | } else { |
| 4341 | address = (char *)calloc(1, 1); |
| 4342 | } |
| 4343 | AudioParameter param = AudioParameter(String8(address)); |
| 4344 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 4345 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4346 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4347 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4348 | } |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 4349 | const std::string patchSinksAsString = patchSinksToString(patch); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4350 | |
| 4351 | mThreadMetrics.logEndInterval(); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4352 | mThreadMetrics.logCreatePatch(/* inDevices */ {}, patchSinksAsString); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4353 | mThreadMetrics.logBeginInterval(); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 4354 | // also dispatch to active AudioTracks for MediaMetrics |
| 4355 | for (const auto &track : mActiveTracks) { |
| 4356 | track->logEndInterval(); |
| 4357 | track->logBeginInterval(patchSinksAsString); |
| 4358 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 4359 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 4360 | if (configChanged) { |
| 4361 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 4362 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4363 | return status; |
| 4364 | } |
| 4365 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4366 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 4367 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 4368 | status_t status; |
| 4369 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 4370 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 4371 | // or if HAL does not properly lock against access. |
| 4372 | AutoPark<FastMixer> park(mFastMixer); |
| 4373 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 4374 | } else { |
| 4375 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 4376 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4377 | return status; |
| 4378 | } |
| 4379 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4380 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 4381 | { |
| 4382 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4383 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4384 | mPatch = audio_patch{}; |
| 4385 | mOutDeviceTypeAddrs.clear(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4386 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 4387 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 4388 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 4389 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4390 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4391 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 4392 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4393 | status = mOutput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4394 | } |
| 4395 | return status; |
| 4396 | } |
| 4397 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4398 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 4399 | { |
| 4400 | Mutex::Autolock _l(mLock); |
| 4401 | mTracks.add(track); |
| 4402 | } |
| 4403 | |
| 4404 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 4405 | { |
| 4406 | Mutex::Autolock _l(mLock); |
| 4407 | destroyTrack_l(track); |
| 4408 | } |
| 4409 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4410 | void AudioFlinger::PlaybackThread::toAudioPortConfig(struct audio_port_config *config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4411 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4412 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4413 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 4414 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 4415 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 4416 | if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { |
| 4417 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 4418 | config->flags.output = mOutput->flags; |
| 4419 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4422 | // ---------------------------------------------------------------------------- |
| 4423 | |
| 4424 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4425 | audio_io_handle_t id, bool systemReady, type_t type) |
| 4426 | : PlaybackThread(audioFlinger, output, id, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4427 | // mAudioMixer below |
| 4428 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4429 | mFastMixerFutex(0), |
| 4430 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4431 | // mOutputSink below |
| 4432 | // mPipeSink below |
| 4433 | // mNormalSink below |
| 4434 | { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4435 | setMasterBalance(audioFlinger->getMasterBalance_l()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4436 | ALOGV("MixerThread() id=%d type=%d", id, type); |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 4437 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 4438 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4439 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 4440 | mNormalFrameCount); |
| 4441 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4442 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 4443 | if (type == DUPLICATING) { |
| 4444 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 4445 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 4446 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 4447 | return; |
| 4448 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4449 | // create an NBAIO sink for the HAL output stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 4450 | mOutputSink = new AudioStreamOutSink(output->stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4451 | size_t numCounterOffers = 0; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4452 | const NBAIO_Format offers[1] = {Format_from_SR_C( |
| 4453 | mSampleRate, mChannelCount + mHapticChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4454 | #if !LOG_NDEBUG |
| 4455 | ssize_t index = |
| 4456 | #else |
| 4457 | (void) |
| 4458 | #endif |
| 4459 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4460 | ALOG_ASSERT(index == 0); |
| 4461 | |
| 4462 | // initialize fast mixer depending on configuration |
| 4463 | bool initFastMixer; |
| 4464 | switch (kUseFastMixer) { |
| 4465 | case FastMixer_Never: |
| 4466 | initFastMixer = false; |
| 4467 | break; |
| 4468 | case FastMixer_Always: |
| 4469 | initFastMixer = true; |
| 4470 | break; |
| 4471 | case FastMixer_Static: |
| 4472 | case FastMixer_Dynamic: |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 4473 | // FastMixer was designed to operate with a HAL that pulls at a regular rate, |
| 4474 | // where the period is less than an experimentally determined threshold that can be |
| 4475 | // scheduled reliably with CFS. However, the BT A2DP HAL is |
| 4476 | // bursty (does not pull at a regular rate) and so cannot operate with FastMixer. |
| 4477 | initFastMixer = mFrameCount < mNormalFrameCount |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4478 | && Intersection(outDeviceTypes(), getAudioDeviceOutAllA2dpSet()).empty(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4479 | break; |
| 4480 | } |
Andy Hung | fda6940 | 2017-02-15 14:33:12 -0800 | [diff] [blame] | 4481 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, |
| 4482 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", |
| 4483 | mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4484 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 4485 | audio_format_t fastMixerFormat; |
| 4486 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 4487 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 4488 | } else { |
| 4489 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 4490 | } |
| 4491 | if (mFormat != fastMixerFormat) { |
| 4492 | // change our Sink format to accept our intermediate precision |
| 4493 | mFormat = fastMixerFormat; |
| 4494 | free(mSinkBuffer); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4495 | mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 4496 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 4497 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 4498 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4499 | |
| 4500 | // create a MonoPipe to connect our submix to FastMixer |
| 4501 | NBAIO_Format format = mOutputSink->format(); |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 4502 | |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 4503 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 4504 | ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 4505 | format.mFormat = fastMixerFormat; |
| 4506 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 4507 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4508 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 4509 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 4510 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 4511 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 4512 | const NBAIO_Format offers[1] = {format}; |
| 4513 | size_t numCounterOffers = 0; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 4514 | #if !LOG_NDEBUG |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4515 | ssize_t index = |
| 4516 | #else |
| 4517 | (void) |
| 4518 | #endif |
| 4519 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4520 | ALOG_ASSERT(index == 0); |
| 4521 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 4522 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 4523 | mPipeSink = monoPipe; |
| 4524 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4525 | // create fast mixer and configure it initially with just one fast track for our submix |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 4526 | mFastMixer = new FastMixer(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4527 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4528 | #ifdef STATE_QUEUE_DUMP |
| 4529 | sq->setObserverDump(&mStateQueueObserverDump); |
| 4530 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 4531 | #endif |
| 4532 | FastMixerState *state = sq->begin(); |
| 4533 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 4534 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 4535 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 4536 | fastTrack->mVolumeProvider = NULL; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4537 | fastTrack->mChannelMask = static_cast<audio_channel_mask_t>( |
| 4538 | mChannelMask | mHapticChannelMask); // mPipeSink channel mask for |
| 4539 | // audio to FastMixer |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4540 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4541 | fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE; |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4542 | fastTrack->mHapticIntensity = os::HapticScale::NONE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4543 | fastTrack->mGeneration++; |
| 4544 | state->mFastTracksGen++; |
| 4545 | state->mTrackMask = 1; |
| 4546 | // fast mixer will use the HAL output sink |
| 4547 | state->mOutputSink = mOutputSink.get(); |
| 4548 | state->mOutputSinkGen++; |
| 4549 | state->mFrameCount = mFrameCount; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4550 | // specify sink channel mask when haptic channel mask present as it can not |
| 4551 | // be calculated directly from channel count |
| 4552 | state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4553 | ? AUDIO_CHANNEL_NONE |
| 4554 | : static_cast<audio_channel_mask_t>(mChannelMask | mHapticChannelMask); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4555 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4556 | // already done in constructor initialization list |
| 4557 | //mFastMixerFutex = 0; |
| 4558 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4559 | state->mColdGen++; |
| 4560 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4561 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 4562 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4563 | sq->end(); |
| 4564 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4565 | |
Eric Tan | 0513b5d | 2018-09-17 10:32:48 -0700 | [diff] [blame] | 4566 | NBLog::thread_info_t info; |
| 4567 | info.id = mId; |
| 4568 | info.type = NBLog::FASTMIXER; |
| 4569 | mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info); |
| 4570 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4571 | // start the fast mixer |
| 4572 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 4573 | pid_t tid = mFastMixer->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 4574 | sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 4575 | stream()->setHalThreadPriority(kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4576 | |
| 4577 | #ifdef AUDIO_WATCHDOG |
| 4578 | // create and start the watchdog |
| 4579 | mAudioWatchdog = new AudioWatchdog(); |
| 4580 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 4581 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 4582 | tid = mAudioWatchdog->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 4583 | sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4584 | #endif |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 4585 | } else { |
| 4586 | #ifdef TEE_SINK |
| 4587 | // Only use the MixerThread tee if there is no FastMixer. |
| 4588 | mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD); |
| 4589 | mTee.setId(std::string("_") + std::to_string(mId) + "_M"); |
| 4590 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | switch (kUseFastMixer) { |
| 4594 | case FastMixer_Never: |
| 4595 | case FastMixer_Dynamic: |
| 4596 | mNormalSink = mOutputSink; |
| 4597 | break; |
| 4598 | case FastMixer_Always: |
| 4599 | mNormalSink = mPipeSink; |
| 4600 | break; |
| 4601 | case FastMixer_Static: |
| 4602 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 4603 | break; |
| 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | AudioFlinger::MixerThread::~MixerThread() |
| 4608 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4609 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4610 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4611 | FastMixerState *state = sq->begin(); |
| 4612 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 4613 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 4614 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 4615 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4616 | } |
| 4617 | } |
| 4618 | state->mCommand = FastMixerState::EXIT; |
| 4619 | sq->end(); |
| 4620 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4621 | mFastMixer->join(); |
| 4622 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 4623 | // We'll use that extract the final state which contains one remaining fast track |
| 4624 | // corresponding to our sub-mix. |
| 4625 | state = sq->begin(); |
| 4626 | ALOG_ASSERT(state->mTrackMask == 1); |
| 4627 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 4628 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 4629 | delete fastTrack->mBufferProvider; |
| 4630 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4631 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4632 | #ifdef AUDIO_WATCHDOG |
| 4633 | if (mAudioWatchdog != 0) { |
| 4634 | mAudioWatchdog->requestExit(); |
| 4635 | mAudioWatchdog->requestExitAndWait(); |
| 4636 | mAudioWatchdog.clear(); |
| 4637 | } |
| 4638 | #endif |
| 4639 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4640 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4641 | delete mAudioMixer; |
| 4642 | } |
| 4643 | |
| 4644 | |
| 4645 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 4646 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4647 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4648 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 4649 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 4650 | } |
| 4651 | return latency; |
| 4652 | } |
| 4653 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4654 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4655 | { |
| 4656 | // FIXME we should only do one push per cycle; confirm this is true |
| 4657 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4658 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4659 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4660 | FastMixerState *state = sq->begin(); |
| 4661 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 4662 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 4663 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 4664 | |
| 4665 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 4666 | ATRACE_BEGIN("write"); |
| 4667 | mOutput->write((char *)mSinkBuffer, 0); |
| 4668 | ATRACE_END(); |
| 4669 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4670 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 4671 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 4672 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4673 | } |
| 4674 | #ifdef AUDIO_WATCHDOG |
| 4675 | if (mAudioWatchdog != 0) { |
| 4676 | mAudioWatchdog->resume(); |
| 4677 | } |
| 4678 | #endif |
| 4679 | } |
| 4680 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 4681 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 4682 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 4683 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 4684 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4685 | sq->end(); |
| 4686 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4687 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4688 | mNormalSink = mPipeSink; |
| 4689 | } |
| 4690 | } else { |
| 4691 | sq->end(false /*didModify*/); |
| 4692 | } |
| 4693 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4694 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 4698 | { |
| 4699 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4700 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4701 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4702 | FastMixerState *state = sq->begin(); |
| 4703 | if (!(state->mCommand & FastMixerState::IDLE)) { |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 4704 | // Report any frames trapped in the Monopipe |
| 4705 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); |
| 4706 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); |
| 4707 | mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld " |
| 4708 | "monoPipeWritten:%lld monoPipeLeft:%lld", |
| 4709 | (long long)mFramesWritten, (long long)mSuspendedFrames, |
| 4710 | (long long)mPipeSink->framesWritten(), pipeFrames); |
| 4711 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); |
| 4712 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4713 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4714 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4715 | state->mColdGen++; |
| 4716 | mFastMixerFutex = 0; |
| 4717 | sq->end(); |
| 4718 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 4719 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 4720 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4721 | mNormalSink = mOutputSink; |
| 4722 | } |
| 4723 | #ifdef AUDIO_WATCHDOG |
| 4724 | if (mAudioWatchdog != 0) { |
| 4725 | mAudioWatchdog->pause(); |
| 4726 | } |
| 4727 | #endif |
| 4728 | } else { |
| 4729 | sq->end(false /*didModify*/); |
| 4730 | } |
| 4731 | } |
| 4732 | PlaybackThread::threadLoop_standby(); |
| 4733 | } |
| 4734 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4735 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 4736 | { |
| 4737 | return false; |
| 4738 | } |
| 4739 | |
| 4740 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 4741 | { |
| 4742 | return !mStandby; |
| 4743 | } |
| 4744 | |
| 4745 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 4746 | { |
| 4747 | Mutex::Autolock _l(mLock); |
| 4748 | return waitingAsyncCallback_l(); |
| 4749 | } |
| 4750 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4751 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 4752 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 4753 | { |
| 4754 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4755 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4756 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4757 | // discard any pending drain or write ack by incrementing sequence |
| 4758 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 4759 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4760 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4761 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 4762 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4763 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4764 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4765 | } |
| 4766 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4767 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 4768 | { |
| 4769 | ALOGV("signal playback thread"); |
| 4770 | broadcast_l(); |
| 4771 | } |
| 4772 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 4773 | void AudioFlinger::PlaybackThread::onAsyncError() |
| 4774 | { |
| 4775 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 4776 | invalidateTracks((audio_stream_type_t)i); |
| 4777 | } |
| 4778 | } |
| 4779 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4780 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 4781 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4782 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 4783 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4784 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4785 | // increase sleep time progressively when application underrun condition clears. |
| 4786 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 4787 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 4788 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4789 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4790 | sleepTimeShift--; |
| 4791 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4792 | mSleepTimeUs = 0; |
| 4793 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4794 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 4795 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 4799 | { |
| 4800 | // If no tracks are ready, sleep once for the duration of an output |
| 4801 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4802 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4803 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Andy Hung | 06d1322 | 2018-05-03 20:13:31 -0700 | [diff] [blame] | 4804 | if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) { |
| 4805 | // Using the Monopipe availableToWrite, we estimate the |
| 4806 | // sleep time to retry for more data (before we underrun). |
| 4807 | MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get()); |
| 4808 | const ssize_t availableToWrite = mPipeSink->availableToWrite(); |
| 4809 | const size_t pipeFrames = monoPipe->maxFrames(); |
| 4810 | const size_t framesLeft = pipeFrames - max(availableToWrite, 0); |
| 4811 | // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount |
| 4812 | const size_t framesDelay = std::min( |
| 4813 | mNormalFrameCount, max(framesLeft / 2, mFrameCount)); |
| 4814 | ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu", |
| 4815 | pipeFrames, framesLeft, framesDelay); |
| 4816 | mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate; |
| 4817 | } else { |
| 4818 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 4819 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 4820 | mSleepTimeUs = kMinThreadSleepTimeUs; |
| 4821 | } |
| 4822 | // reduce sleep time in case of consecutive application underruns to avoid |
| 4823 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 4824 | // duration we would end up writing less data than needed by the audio HAL if |
| 4825 | // the condition persists. |
| 4826 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 4827 | sleepTimeShift++; |
| 4828 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4829 | } |
| 4830 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4831 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4832 | } |
| 4833 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4834 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 4835 | // before effects processing or output. |
| 4836 | if (mMixerBufferValid) { |
| 4837 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4838 | } else { |
| 4839 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 4840 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4841 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4842 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 4843 | "anticipated start"); |
| 4844 | } |
| 4845 | // TODO add standby time extension fct of effect tail |
| 4846 | } |
| 4847 | |
| 4848 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 4849 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 4850 | Vector< sp<Track> > *tracksToRemove) |
| 4851 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 4852 | // clean up deleted track ids in AudioMixer before allocating new tracks |
| 4853 | (void)mTracks.processDeletedTrackIds([this](int trackId) { |
| 4854 | // for each trackId, destroy it in the AudioMixer |
| 4855 | if (mAudioMixer->exists(trackId)) { |
| 4856 | mAudioMixer->destroy(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 4857 | } |
| 4858 | }); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 4859 | mTracks.clearDeletedTrackIds(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4860 | |
| 4861 | mixer_state mixerStatus = MIXER_IDLE; |
| 4862 | // find out which tracks need to be processed |
| 4863 | size_t count = mActiveTracks.size(); |
| 4864 | size_t mixedTracks = 0; |
| 4865 | size_t tracksWithEffect = 0; |
| 4866 | // counts only _active_ fast tracks |
| 4867 | size_t fastTracks = 0; |
| 4868 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 4869 | |
| 4870 | float masterVolume = mMasterVolume; |
| 4871 | bool masterMute = mMasterMute; |
| 4872 | |
| 4873 | if (masterMute) { |
| 4874 | masterVolume = 0; |
| 4875 | } |
| 4876 | // Delegate master volume control to effect in output mix effect chain if needed |
| 4877 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 4878 | if (chain != 0) { |
| 4879 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 4880 | chain->setVolume_l(&v, &v); |
| 4881 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 4882 | chain.clear(); |
| 4883 | } |
| 4884 | |
| 4885 | // prepare a new state to push |
| 4886 | FastMixerStateQueue *sq = NULL; |
| 4887 | FastMixerState *state = NULL; |
| 4888 | bool didModify = false; |
| 4889 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4890 | bool coldIdle = false; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4891 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4892 | sq = mFastMixer->sq(); |
| 4893 | state = sq->begin(); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 4894 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4895 | } |
| 4896 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4897 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4898 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4899 | |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4900 | // DeferredOperations handles statistics after setting mixerStatus. |
| 4901 | class DeferredOperations { |
| 4902 | public: |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4903 | DeferredOperations(mixer_state *mixerStatus, ThreadMetrics *threadMetrics) |
| 4904 | : mMixerStatus(mixerStatus) |
| 4905 | , mThreadMetrics(threadMetrics) {} |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4906 | |
| 4907 | // when leaving scope, tally frames properly. |
| 4908 | ~DeferredOperations() { |
| 4909 | // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY) |
| 4910 | // because that is when the underrun occurs. |
| 4911 | // We do not distinguish between FastTracks and NormalTracks here. |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4912 | size_t maxUnderrunFrames = 0; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 4913 | if (*mMixerStatus == MIXER_TRACKS_READY && mUnderrunFrames.size() > 0) { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4914 | for (const auto &underrun : mUnderrunFrames) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 4915 | underrun.first->tallyUnderrunFrames(underrun.second); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4916 | maxUnderrunFrames = max(underrun.second, maxUnderrunFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4917 | } |
| 4918 | } |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4919 | // send the max underrun frames for this mixer period |
| 4920 | mThreadMetrics->logUnderrunFrames(maxUnderrunFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | // tallyUnderrunFrames() is called to update the track counters |
| 4924 | // with the number of underrun frames for a particular mixer period. |
| 4925 | // We defer tallying until we know the final mixer status. |
| 4926 | void tallyUnderrunFrames(sp<Track> track, size_t underrunFrames) { |
| 4927 | mUnderrunFrames.emplace_back(track, underrunFrames); |
| 4928 | } |
| 4929 | |
| 4930 | private: |
| 4931 | const mixer_state * const mMixerStatus; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4932 | ThreadMetrics * const mThreadMetrics; |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4933 | std::vector<std::pair<sp<Track>, size_t>> mUnderrunFrames; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 4934 | } deferredOperations(&mixerStatus, &mThreadMetrics); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 4935 | // implicit nested scope for variable capture |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4936 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4937 | bool noFastHapticTrack = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4938 | for (size_t i=0 ; i<count ; i++) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 4939 | const sp<Track> t = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4940 | |
| 4941 | // this const just means the local variable doesn't change |
| 4942 | Track* const track = t.get(); |
| 4943 | |
| 4944 | // process fast tracks |
| 4945 | if (track->isFastTrack()) { |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 4946 | LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr, |
| 4947 | "%s(%d): FastTrack(%d) present without FastMixer", |
| 4948 | __func__, id(), track->id()); |
| 4949 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4950 | if (track->getHapticPlaybackEnabled()) { |
| 4951 | noFastHapticTrack = false; |
| 4952 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4953 | |
| 4954 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 4955 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 4956 | // the track never becomes active so it's fast mixer slot is never touched. |
| 4957 | // The converse, of removing an (active) track and then creating a new track |
| 4958 | // at the identical fast mixer slot within the same normal mix cycle, |
| 4959 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 4960 | int j = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4961 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4962 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 4963 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 4964 | |
| 4965 | // Determine whether the track is currently in underrun condition, |
| 4966 | // and whether it had a recent underrun. |
| 4967 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 4968 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 4969 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 4970 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 4971 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 4972 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 4973 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 4974 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 4975 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 4976 | track->mObservedUnderruns = underruns; |
| 4977 | // don't count underruns that occur while stopping or pausing |
| 4978 | // or stopped which can occur when flush() is called while active |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4979 | size_t underrunFrames = 0; |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4980 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 4981 | recentUnderruns > 0) { |
| 4982 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4983 | underrunFrames = recentUnderruns * mFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4984 | } |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 4985 | // Immediately account for FastTrack underruns. |
| 4986 | track->mAudioTrackServerProxy->tallyUnderrunFrames(underrunFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4987 | |
| 4988 | // This is similar to the state machine for normal tracks, |
| 4989 | // with a few modifications for fast tracks. |
| 4990 | bool isActive = true; |
| 4991 | switch (track->mState) { |
| 4992 | case TrackBase::STOPPING_1: |
| 4993 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4994 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4995 | track->mState = TrackBase::STOPPING_2; |
| 4996 | } |
| 4997 | break; |
| 4998 | case TrackBase::PAUSING: |
| 4999 | // ramp down is not yet implemented |
| 5000 | track->setPaused(); |
| 5001 | break; |
| 5002 | case TrackBase::RESUMING: |
| 5003 | // ramp up is not yet implemented |
| 5004 | track->mState = TrackBase::ACTIVE; |
| 5005 | break; |
| 5006 | case TrackBase::ACTIVE: |
| 5007 | if (recentFull > 0 || recentPartial > 0) { |
| 5008 | // track has provided at least some frames recently: reset retry count |
| 5009 | track->mRetryCount = kMaxTrackRetries; |
| 5010 | } |
| 5011 | if (recentUnderruns == 0) { |
| 5012 | // no recent underruns: stay active |
| 5013 | break; |
| 5014 | } |
| 5015 | // there has recently been an underrun of some kind |
| 5016 | if (track->sharedBuffer() == 0) { |
| 5017 | // were any of the recent underruns "empty" (no frames available)? |
| 5018 | if (recentEmpty == 0) { |
| 5019 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 5020 | break; |
| 5021 | } |
| 5022 | // there has recently been an "empty" underrun: decrement the retry counter |
| 5023 | if (--(track->mRetryCount) > 0) { |
| 5024 | break; |
| 5025 | } |
| 5026 | // indicate to client process that the track was disabled because of underrun; |
| 5027 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5028 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5029 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 5030 | isActive = false; |
| 5031 | break; |
| 5032 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 5033 | FALLTHROUGH_INTENDED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5034 | case TrackBase::STOPPING_2: |
| 5035 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5036 | case TrackBase::STOPPED: |
| 5037 | case TrackBase::FLUSHED: // flush() while active |
| 5038 | // Check for presentation complete if track is inactive |
| 5039 | // We have consumed all the buffers of this track. |
| 5040 | // This would be incomplete if we auto-paused on underrun |
| 5041 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5042 | uint32_t latency = 0; |
| 5043 | status_t result = mOutput->stream->getLatency(&latency); |
| 5044 | ALOGE_IF(result != OK, |
| 5045 | "Error when retrieving output stream latency: %d", result); |
| 5046 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5047 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5048 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 5049 | // track stays in active list until presentation is complete |
| 5050 | break; |
| 5051 | } |
| 5052 | } |
| 5053 | if (track->isStopping_2()) { |
| 5054 | track->mState = TrackBase::STOPPED; |
| 5055 | } |
| 5056 | if (track->isStopped()) { |
| 5057 | // Can't reset directly, as fast mixer is still polling this track |
| 5058 | // track->reset(); |
| 5059 | // So instead mark this track as needing to be reset after push with ack |
| 5060 | resetMask |= 1 << i; |
| 5061 | } |
| 5062 | isActive = false; |
| 5063 | break; |
| 5064 | case TrackBase::IDLE: |
| 5065 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5066 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | if (isActive) { |
| 5070 | // was it previously inactive? |
| 5071 | if (!(state->mTrackMask & (1 << j))) { |
| 5072 | ExtendedAudioBufferProvider *eabp = track; |
| 5073 | VolumeProvider *vp = track; |
| 5074 | fastTrack->mBufferProvider = eabp; |
| 5075 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5076 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 5077 | fastTrack->mFormat = track->mFormat; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5078 | fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 5079 | fastTrack->mHapticIntensity = track->getHapticIntensity(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5080 | fastTrack->mGeneration++; |
| 5081 | state->mTrackMask |= 1 << j; |
| 5082 | didModify = true; |
| 5083 | // no acknowledgement required for newly active tracks |
| 5084 | } |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5085 | sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5086 | float volume; |
| 5087 | if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) { |
| 5088 | volume = 0.f; |
| 5089 | } else { |
| 5090 | volume = masterVolume * mStreamTypes[track->streamType()].volume; |
| 5091 | } |
| 5092 | |
| 5093 | handleVoipVolume_l(&volume); |
| 5094 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5095 | // cache the combined master volume and stream type volume for fast mixer; this |
| 5096 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5097 | const float vh = track->getVolumeHandler()->getVolume( |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5098 | proxy->framesReleased()).first; |
| 5099 | volume *= vh; |
Kevin Rocard | b0eeaf12 | 2018-04-11 08:30:16 -0700 | [diff] [blame] | 5100 | track->mCachedVolume = volume; |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5101 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 5102 | float vlf = volume * float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5103 | float vrf = volume * float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5104 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5105 | track->setFinalVolume((vlf + vrf) / 2.f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5106 | ++fastTracks; |
| 5107 | } else { |
| 5108 | // was it previously active? |
| 5109 | if (state->mTrackMask & (1 << j)) { |
| 5110 | fastTrack->mBufferProvider = NULL; |
| 5111 | fastTrack->mGeneration++; |
| 5112 | state->mTrackMask &= ~(1 << j); |
| 5113 | didModify = true; |
| 5114 | // If any fast tracks were removed, we must wait for acknowledgement |
| 5115 | // because we're about to decrement the last sp<> on those tracks. |
| 5116 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 5117 | } else { |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5118 | // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an |
| 5119 | // AudioTrack may start (which may not be with a start() but with a write() |
| 5120 | // after underrun) and immediately paused or released. In that case the |
| 5121 | // FastTrack state hasn't had time to update. |
| 5122 | // TODO Remove the ALOGW when this theory is confirmed. |
| 5123 | ALOGW("fast track %d should have been active; " |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5124 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 5125 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 5126 | track->sharedBuffer() != 0); |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5127 | // Since the FastMixer state already has the track inactive, do nothing here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5128 | } |
| 5129 | tracksToRemove->add(track); |
| 5130 | // Avoids a misleading display in dumpsys |
| 5131 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 5132 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5133 | if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) { |
| 5134 | fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); |
| 5135 | didModify = true; |
| 5136 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5137 | continue; |
| 5138 | } |
| 5139 | |
| 5140 | { // local variable scope to avoid goto warning |
| 5141 | |
| 5142 | audio_track_cblk_t* cblk = track->cblk(); |
| 5143 | |
| 5144 | // The first time a track is added we wait |
| 5145 | // for all its buffers to be filled before processing it |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5146 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5147 | |
| 5148 | // if an active track doesn't exist in the AudioMixer, create it. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5149 | // use the trackId as the AudioMixer name. |
| 5150 | if (!mAudioMixer->exists(trackId)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5151 | status_t status = mAudioMixer->create( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5152 | trackId, |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5153 | track->mChannelMask, |
| 5154 | track->mFormat, |
| 5155 | track->mSessionId); |
| 5156 | if (status != OK) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5157 | ALOGW("%s(): AudioMixer cannot create track(%d)" |
| 5158 | " mask %#x, format %#x, sessionId %d", |
| 5159 | __func__, trackId, |
| 5160 | track->mChannelMask, track->mFormat, track->mSessionId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5161 | tracksToRemove->add(track); |
| 5162 | track->invalidate(); // consider it dead. |
| 5163 | continue; |
| 5164 | } |
| 5165 | } |
| 5166 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5167 | // make sure that we have enough frames to mix one full buffer. |
| 5168 | // enforce this condition only once to enable draining the buffer in case the client |
| 5169 | // app does not call stop() and relies on underrun to stop: |
| 5170 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 5171 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5172 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5173 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 5174 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5175 | |
| 5176 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 5177 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5178 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 5179 | // add frames already consumed but not yet released by the resampler |
| 5180 | // because mAudioTrackServerProxy->framesReady() will include these frames |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5181 | desiredFrames += mAudioMixer->getUnreleasedFrames(trackId); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5182 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5183 | uint32_t minFrames = 1; |
| 5184 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 5185 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5186 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5187 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 5188 | |
| 5189 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5190 | if (ATRACE_ENABLED()) { |
| 5191 | // I wish we had formatted trace names |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5192 | std::string traceName("nRdy"); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5193 | traceName += std::to_string(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5194 | ATRACE_INT(traceName.c_str(), framesReady); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5195 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5196 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5197 | !track->isPaused() && !track->isTerminated()) |
| 5198 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5199 | ALOGVV("track(%d) s=%08x [OK] on thread %p", trackId, cblk->mServer, this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5200 | |
| 5201 | mixedTracks++; |
| 5202 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5203 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 5204 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5205 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5206 | if (track->mainBuffer() != mSinkBuffer && |
| 5207 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5208 | if (mEffectBufferEnabled) { |
| 5209 | mEffectBufferValid = true; // Later can set directly. |
| 5210 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5211 | chain = getEffectChain_l(track->sessionId()); |
| 5212 | // Delegate volume control to effect in track effect chain if needed |
| 5213 | if (chain != 0) { |
| 5214 | tracksWithEffect++; |
| 5215 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5216 | ALOGW("prepareTracks_l(): track(%d) attached to effect but no chain found on " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5217 | "session %d", |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5218 | trackId, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | |
| 5223 | int param = AudioMixer::VOLUME; |
| 5224 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5225 | // no ramp for the first volume setting |
| 5226 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 5227 | if (track->mState == TrackBase::RESUMING) { |
| 5228 | track->mState = TrackBase::ACTIVE; |
Revathi Uddaraju | 453bcb5 | 2017-08-14 14:19:40 +0800 | [diff] [blame] | 5229 | // If a new track is paused immediately after start, do not ramp on resume. |
| 5230 | if (cblk->mServer != 0) { |
| 5231 | param = AudioMixer::RAMP_VOLUME; |
| 5232 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5233 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5234 | mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5235 | mLeftVolFloat = -1.0; |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5236 | // FIXME should not make a decision based on mServer |
| 5237 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5238 | // If the track is stopped before the first frame was mixed, |
| 5239 | // do not apply ramp |
| 5240 | param = AudioMixer::RAMP_VOLUME; |
| 5241 | } |
| 5242 | |
| 5243 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5244 | uint32_t vl, vr; // in U8.24 integer format |
| 5245 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5246 | // read original volumes with volume control |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5247 | float v = masterVolume * mStreamTypes[track->streamType()].volume; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5248 | // Always fetch volumeshaper volume to ensure state is updated. |
| 5249 | const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
| 5250 | const float vh = track->getVolumeHandler()->getVolume( |
| 5251 | track->mAudioTrackServerProxy->framesReleased()).first; |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5252 | |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5253 | if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { |
| 5254 | v = 0; |
| 5255 | } |
| 5256 | |
| 5257 | handleVoipVolume_l(&v); |
| 5258 | |
| 5259 | if (track->isPausing()) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5260 | vl = vr = 0; |
| 5261 | vlf = vrf = vaf = 0.; |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5262 | track->setPaused(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | } else { |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5264 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5265 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5266 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5267 | // 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] | 5268 | if (vlf > GAIN_FLOAT_UNITY) { |
| 5269 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 5270 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5271 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5272 | if (vrf > GAIN_FLOAT_UNITY) { |
| 5273 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 5274 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5275 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5276 | // now apply the master volume and stream type volume and shaper volume |
| 5277 | vlf *= v * vh; |
| 5278 | vrf *= v * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5279 | // 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] | 5280 | // then derive vl and vr as U8.24 versions for the effect chain |
| 5281 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 5282 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 5283 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 5284 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 5285 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5286 | // send level comes from shared memory and so may be corrupt |
| 5287 | if (sendLevel > MAX_GAIN_INT) { |
| 5288 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 5289 | sendLevel = MAX_GAIN_INT; |
| 5290 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5291 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 5292 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5293 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5294 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5295 | track->setFinalVolume((vrf + vlf) / 2.f); |
| 5296 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5297 | // Delegate volume control to effect in track effect chain if needed |
| 5298 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 5299 | // Do not ramp volume if volume is controlled by effect |
| 5300 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 5301 | // Update remaining floating point volume levels |
| 5302 | vlf = (float)vl / (1 << 24); |
| 5303 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5304 | track->mHasVolumeController = true; |
| 5305 | } else { |
| 5306 | // force no volume ramp when volume controller was just disabled or removed |
| 5307 | // from effect chain to avoid volume spike |
| 5308 | if (track->mHasVolumeController) { |
| 5309 | param = AudioMixer::VOLUME; |
| 5310 | } |
| 5311 | track->mHasVolumeController = false; |
| 5312 | } |
| 5313 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5314 | // XXX: these things DON'T need to be done each time |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5315 | mAudioMixer->setBufferProvider(trackId, track); |
| 5316 | mAudioMixer->enable(trackId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5317 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5318 | mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf); |
| 5319 | mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf); |
| 5320 | mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5321 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5322 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5323 | AudioMixer::TRACK, |
| 5324 | AudioMixer::FORMAT, (void *)track->format()); |
| 5325 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5326 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5327 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 5328 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 5329 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5330 | trackId, |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 5331 | AudioMixer::TRACK, |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5332 | AudioMixer::MIXER_CHANNEL_MASK, |
| 5333 | (void *)(uintptr_t)(mChannelMask | mHapticChannelMask)); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 5334 | // 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] | 5335 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5336 | uint32_t reqSampleRate = proxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 5337 | if (reqSampleRate == 0) { |
| 5338 | reqSampleRate = mSampleRate; |
| 5339 | } else if (reqSampleRate > maxSampleRate) { |
| 5340 | reqSampleRate = maxSampleRate; |
| 5341 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5342 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5343 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5344 | AudioMixer::RESAMPLE, |
| 5345 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 5346 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5347 | |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5348 | AudioPlaybackRate playbackRate = proxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5349 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5350 | trackId, |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5351 | AudioMixer::TIMESTRETCH, |
| 5352 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 5353 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5354 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5355 | /* |
| 5356 | * Select the appropriate output buffer for the track. |
| 5357 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5358 | * Tracks with effects go into their own effects chain buffer |
| 5359 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5360 | * |
| 5361 | * Other tracks can use mMixerBuffer for higher precision |
| 5362 | * channel accumulation. If this buffer is enabled |
| 5363 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 5364 | * into it. |
| 5365 | * |
| 5366 | */ |
| 5367 | if (mMixerBufferEnabled |
| 5368 | && (track->mainBuffer() == mSinkBuffer |
| 5369 | || track->mainBuffer() == mMixerBuffer)) { |
| 5370 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5371 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5372 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 5373 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5374 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5375 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5376 | AudioMixer::TRACK, |
| 5377 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 5378 | // TODO: override track->mainBuffer()? |
| 5379 | mMixerBufferValid = true; |
| 5380 | } else { |
| 5381 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5382 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5383 | AudioMixer::TRACK, |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 5384 | AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5385 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5386 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5387 | AudioMixer::TRACK, |
| 5388 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 5389 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5390 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5391 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5392 | AudioMixer::TRACK, |
| 5393 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5394 | mAudioMixer->setParameter( |
| 5395 | trackId, |
| 5396 | AudioMixer::TRACK, |
| 5397 | AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled()); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 5398 | mAudioMixer->setParameter( |
| 5399 | trackId, |
| 5400 | AudioMixer::TRACK, |
| 5401 | AudioMixer::HAPTIC_INTENSITY, (void *)(uintptr_t)track->getHapticIntensity()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5402 | |
| 5403 | // reset retry count |
| 5404 | track->mRetryCount = kMaxTrackRetries; |
| 5405 | |
| 5406 | // If one track is ready, set the mixer ready if: |
| 5407 | // - the mixer was not ready during previous round OR |
| 5408 | // - no other track is not ready |
| 5409 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 5410 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 5411 | mixerStatus = MIXER_TRACKS_READY; |
| 5412 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5413 | |
| 5414 | // Enable the next few lines to instrument a test for underrun log handling. |
| 5415 | // TODO: Remove when we have a better way of testing the underrun log. |
| 5416 | #if 0 |
| 5417 | static int i; |
| 5418 | if ((++i & 0xf) == 0) { |
| 5419 | deferredOperations.tallyUnderrunFrames(track, 10 /* underrunFrames */); |
| 5420 | } |
| 5421 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5422 | } else { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5423 | size_t underrunFrames = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5424 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5425 | ALOGV("track(%d) underrun, track state %s framesReady(%zu) < framesDesired(%zd)", |
| 5426 | trackId, track->getTrackStateAsString(), framesReady, desiredFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5427 | underrunFrames = desiredFrames; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5428 | } |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5429 | deferredOperations.tallyUnderrunFrames(track, underrunFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 5430 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5431 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 5432 | // previous audio buffer again to effects |
| 5433 | chain = getEffectChain_l(track->sessionId()); |
| 5434 | if (chain != 0) { |
| 5435 | chain->clearInputBuffer(); |
| 5436 | } |
| 5437 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5438 | ALOGVV("track(%d) s=%08x [NOT READY] on thread %p", trackId, cblk->mServer, this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5439 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 5440 | track->isStopped() || track->isPaused()) { |
| 5441 | // We have consumed all the buffers of this track. |
| 5442 | // Remove it from the list of active tracks. |
| 5443 | // TODO: use actual buffer filling status instead of latency when available from |
| 5444 | // audio HAL |
| 5445 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5446 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5447 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 5448 | if (track->isStopped()) { |
| 5449 | track->reset(); |
| 5450 | } |
| 5451 | tracksToRemove->add(track); |
| 5452 | } |
| 5453 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5454 | // No buffers for this track. Give it a few chances to |
| 5455 | // fill a buffer, then remove it from active list. |
| 5456 | if (--(track->mRetryCount) <= 0) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5457 | ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", |
| 5458 | trackId, this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5459 | tracksToRemove->add(track); |
| 5460 | // indicate to client process that the track was disabled because of underrun; |
| 5461 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5462 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5463 | // If one track is not ready, mark the mixer also not ready if: |
| 5464 | // - the mixer was ready during previous round OR |
| 5465 | // - no other track is ready |
| 5466 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 5467 | mixerStatus != MIXER_TRACKS_READY) { |
| 5468 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5469 | } |
| 5470 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5471 | mAudioMixer->disable(trackId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5472 | } |
| 5473 | |
| 5474 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5475 | |
| 5476 | } |
| 5477 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5478 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) { |
| 5479 | // When there is no fast track playing haptic and FastMixer exists, |
| 5480 | // enabling the first FastTrack, which provides mixed data from normal |
| 5481 | // tracks, to play haptic data. |
| 5482 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 5483 | if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) { |
| 5484 | fastTrack->mHapticPlaybackEnabled = noFastHapticTrack; |
| 5485 | didModify = true; |
| 5486 | } |
| 5487 | } |
| 5488 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5489 | // Push the new FastMixer state if necessary |
| 5490 | bool pauseAudioWatchdog = false; |
| 5491 | if (didModify) { |
| 5492 | state->mFastTracksGen++; |
| 5493 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 5494 | if (kUseFastMixer == FastMixer_Dynamic && |
| 5495 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 5496 | state->mCommand = FastMixerState::COLD_IDLE; |
| 5497 | state->mColdFutexAddr = &mFastMixerFutex; |
| 5498 | state->mColdGen++; |
| 5499 | mFastMixerFutex = 0; |
| 5500 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 5501 | mNormalSink = mOutputSink; |
| 5502 | } |
| 5503 | // If we go into cold idle, need to wait for acknowledgement |
| 5504 | // so that fast mixer stops doing I/O. |
| 5505 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 5506 | pauseAudioWatchdog = true; |
| 5507 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5508 | } |
| 5509 | if (sq != NULL) { |
| 5510 | sq->end(didModify); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5511 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread |
| 5512 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE |
| 5513 | // when bringing the output sink into standby.) |
| 5514 | // |
| 5515 | // We will get the latest FastMixer state when we come out of COLD_IDLE. |
| 5516 | // |
| 5517 | // This occurs with BT suspend when we idle the FastMixer with |
| 5518 | // active tracks, which may be added or removed. |
| 5519 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5520 | } |
| 5521 | #ifdef AUDIO_WATCHDOG |
| 5522 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 5523 | mAudioWatchdog->pause(); |
| 5524 | } |
| 5525 | #endif |
| 5526 | |
| 5527 | // Now perform the deferred reset on fast tracks that have stopped |
| 5528 | while (resetMask != 0) { |
| 5529 | size_t i = __builtin_ctz(resetMask); |
| 5530 | ALOG_ASSERT(i < count); |
| 5531 | resetMask &= ~(1 << i); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5532 | sp<Track> track = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5533 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 5534 | track->reset(); |
| 5535 | } |
| 5536 | |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 5537 | // Track destruction may occur outside of threadLoop once it is removed from active tracks. |
| 5538 | // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if |
| 5539 | // it ceases to be active, to allow safe removal from the AudioMixer at the start |
| 5540 | // of prepareTracks_l(); this releases any outstanding buffer back to the track. |
| 5541 | // See also the implementation of destroyTrack_l(). |
| 5542 | for (const auto &track : *tracksToRemove) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5543 | const int trackId = track->id(); |
| 5544 | if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer. |
| 5545 | mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */); |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 5546 | } |
| 5547 | } |
| 5548 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5549 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5550 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5551 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 5552 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 5553 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 5557 | // as long as there are effects we should clear the effects buffer, to avoid |
| 5558 | // passing a non-clean buffer to the effect chain |
| 5559 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 5560 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5561 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 5562 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 5563 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5564 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 5565 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5566 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5567 | if (mMixerBufferValid) { |
| 5568 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5569 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 5570 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 5571 | // after mixing. |
| 5572 | // |
| 5573 | // To enforce this guarantee: |
| 5574 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 5575 | // (mixedTracks == 0 && fastTracks > 0)) |
| 5576 | // must imply MIXER_TRACKS_READY. |
| 5577 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 5578 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5579 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 5580 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | // if any fast tracks, then status is ready |
| 5584 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 5585 | if (fastTracks > 0) { |
| 5586 | mixerStatus = MIXER_TRACKS_READY; |
| 5587 | } |
| 5588 | return mixerStatus; |
| 5589 | } |
| 5590 | |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5591 | // trackCountForUid_l() must be called with ThreadBase::mLock held |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5592 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) const |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5593 | { |
| 5594 | uint32_t trackCount = 0; |
| 5595 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 5596 | if (mTracks[i]->uid() == uid) { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5597 | trackCount++; |
| 5598 | } |
| 5599 | } |
| 5600 | return trackCount; |
| 5601 | } |
| 5602 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5603 | // isTrackAllowed_l() must be called with ThreadBase::mLock held |
| 5604 | bool AudioFlinger::MixerThread::isTrackAllowed_l( |
| 5605 | audio_channel_mask_t channelMask, audio_format_t format, |
| 5606 | audio_session_t sessionId, uid_t uid) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5607 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5608 | if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 5609 | return false; |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5610 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5611 | // Check validity as we don't call AudioMixer::create() here. |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 5612 | if (!mAudioMixer->isValidFormat(format)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5613 | ALOGW("%s: invalid format: %#x", __func__, format); |
| 5614 | return false; |
| 5615 | } |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 5616 | if (!mAudioMixer->isValidChannelMask(channelMask)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5617 | ALOGW("%s: invalid channelMask: %#x", __func__, channelMask); |
| 5618 | return false; |
| 5619 | } |
| 5620 | return true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5621 | } |
| 5622 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5623 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 5624 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5625 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5626 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5627 | bool reconfig = false; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5628 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5629 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 5630 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5631 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5632 | AudioParameter param = AudioParameter(keyValuePair); |
| 5633 | int value; |
| 5634 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 5635 | reconfig = true; |
| 5636 | } |
| 5637 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 5638 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5639 | status = BAD_VALUE; |
| 5640 | } else { |
| 5641 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5642 | reconfig = true; |
| 5643 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5644 | } |
| 5645 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 5646 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5647 | status = BAD_VALUE; |
| 5648 | } else { |
| 5649 | // no need to save value, since it's constant |
| 5650 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5651 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5652 | } |
| 5653 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5654 | // do not accept frame count changes if tracks are open as the track buffer |
| 5655 | // size depends on frame count and correct behavior would not be guaranteed |
| 5656 | // if frame count is changed after track creation |
| 5657 | if (!mTracks.isEmpty()) { |
| 5658 | status = INVALID_OPERATION; |
| 5659 | } else { |
| 5660 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5661 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5662 | } |
| 5663 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5664 | LOG_FATAL("Should not set routing device in MixerThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5665 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5666 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5667 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5668 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5669 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5670 | mOutput->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 5671 | if (!mStandby) { |
| 5672 | mThreadMetrics.logEndInterval(); |
| 5673 | mStandby = true; |
| 5674 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5675 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5676 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5677 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5678 | if (status == NO_ERROR && reconfig) { |
| 5679 | readOutputParameters_l(); |
| 5680 | delete mAudioMixer; |
| 5681 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5682 | for (const auto &track : mTracks) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5683 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5684 | status_t status = mAudioMixer->create( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5685 | trackId, |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5686 | track->mChannelMask, |
| 5687 | track->mFormat, |
| 5688 | track->mSessionId); |
| 5689 | ALOGW_IF(status != NO_ERROR, |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5690 | "%s(): AudioMixer cannot create track(%d)" |
| 5691 | " mask %#x, format %#x, sessionId %d", |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5692 | __func__, |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5693 | trackId, track->mChannelMask, track->mFormat, track->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5694 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5695 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5696 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5697 | } |
| 5698 | |
Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 5699 | return reconfig; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 5703 | void AudioFlinger::MixerThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5704 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 5705 | PlaybackThread::dumpInternals_l(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 5706 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 5707 | dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5708 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 5709 | dprintf(fd, " Master balance: %f (%s)\n", mMasterBalance.load(), |
| 5710 | (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance()) |
| 5711 | : mBalance.toString()).c_str()); |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5712 | if (hasFastMixer()) { |
| 5713 | dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); |
| 5714 | |
| 5715 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 5716 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 5717 | // This is a large object so we place it on the heap. |
| 5718 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
Eric Tan | 2942a4e | 2018-09-12 17:41:27 -0700 | [diff] [blame] | 5719 | const std::unique_ptr<FastMixerDumpState> copy = |
| 5720 | std::make_unique<FastMixerDumpState>(mFastMixerDumpState); |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5721 | copy->dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5722 | |
| 5723 | #ifdef STATE_QUEUE_DUMP |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5724 | // Similar for state queue |
| 5725 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 5726 | observerCopy.dump(fd); |
| 5727 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 5728 | mutatorCopy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5729 | #endif |
| 5730 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 5731 | #ifdef AUDIO_WATCHDOG |
| 5732 | if (mAudioWatchdog != 0) { |
| 5733 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 5734 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 5735 | wdCopy.dump(fd); |
| 5736 | } |
| 5737 | #endif |
| 5738 | |
| 5739 | } else { |
| 5740 | dprintf(fd, " No FastMixer\n"); |
| 5741 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 5745 | { |
| 5746 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5747 | } |
| 5748 | |
| 5749 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 5750 | { |
| 5751 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 5752 | } |
| 5753 | |
| 5754 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 5755 | { |
| 5756 | PlaybackThread::cacheParameters_l(); |
| 5757 | |
| 5758 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 5759 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 5760 | // increase threshold again due to low power audio mode. The way this warning |
| 5761 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 5762 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 5763 | } |
| 5764 | |
| 5765 | // ---------------------------------------------------------------------------- |
| 5766 | |
| 5767 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5768 | AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady) |
| 5769 | : PlaybackThread(audioFlinger, output, id, type, systemReady) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5770 | { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 5771 | setMasterBalance(audioFlinger->getMasterBalance_l()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5772 | } |
| 5773 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5774 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 5775 | { |
| 5776 | } |
| 5777 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 5778 | void AudioFlinger::DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 5779 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 5780 | PlaybackThread::dumpInternals_l(fd, args); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 5781 | dprintf(fd, " Master balance: %f Left: %f Right: %f\n", |
| 5782 | mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight); |
| 5783 | } |
| 5784 | |
| 5785 | void AudioFlinger::DirectOutputThread::setMasterBalance(float balance) |
| 5786 | { |
| 5787 | Mutex::Autolock _l(mLock); |
| 5788 | if (mMasterBalance != balance) { |
| 5789 | mMasterBalance.store(balance); |
| 5790 | mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight); |
| 5791 | broadcast_l(); |
| 5792 | } |
| 5793 | } |
| 5794 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5795 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5796 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5797 | float left, right; |
| 5798 | |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5799 | // Ensure volumeshaper state always advances even when muted. |
| 5800 | const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy; |
| 5801 | const auto [shaperVolume, shaperActive] = track->getVolumeHandler()->getVolume( |
| 5802 | proxy->framesReleased()); |
| 5803 | mVolumeShaperActive = shaperActive; |
| 5804 | |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 5805 | if (mMasterMute || mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5806 | left = right = 0; |
| 5807 | } else { |
| 5808 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5809 | const float v = mMasterVolume * typeVolume * shaperVolume; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5810 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5811 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 5812 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5813 | if (left > GAIN_FLOAT_UNITY) { |
| 5814 | left = GAIN_FLOAT_UNITY; |
| 5815 | } |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 5816 | left *= v * mMasterBalanceLeft; // DirectOutputThread balance applied as track volume |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5817 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 5818 | if (right > GAIN_FLOAT_UNITY) { |
| 5819 | right = GAIN_FLOAT_UNITY; |
| 5820 | } |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 5821 | right *= v * mMasterBalanceRight; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | if (lastTrack) { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5825 | track->setFinalVolume((left + right) / 2.f); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5826 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 5827 | mLeftVolFloat = left; |
| 5828 | mRightVolFloat = right; |
| 5829 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5830 | // Delegate volume control to effect in track effect chain if needed |
| 5831 | // only one effect chain can be present on DirectOutputThread, so if |
| 5832 | // there is one, the track is connected to it |
| 5833 | if (!mEffectChains.isEmpty()) { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 5834 | // if effect chain exists, volume is handled by it. |
| 5835 | // Convert volumes from float to 8.24 |
| 5836 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 5837 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 5838 | // Direct/Offload effect chains set output volume in setVolume_l(). |
| 5839 | (void)mEffectChains[0]->setVolume_l(&vl, &vr); |
| 5840 | } else { |
| 5841 | // otherwise we directly set the volume. |
| 5842 | setVolumeForOutput_l(left, right); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5843 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5844 | } |
| 5845 | } |
| 5846 | } |
| 5847 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5848 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 5849 | { |
| 5850 | sp<Track> previousTrack = mPreviousTrack.promote(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5851 | sp<Track> latestTrack = mActiveTracks.getLatest(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5852 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5853 | if (previousTrack != 0 && latestTrack != 0) { |
| 5854 | if (mType == DIRECT) { |
| 5855 | if (previousTrack.get() != latestTrack.get()) { |
| 5856 | mFlushPending = true; |
| 5857 | } |
| 5858 | } else /* mType == OFFLOAD */ { |
| 5859 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 5860 | mFlushPending = true; |
| 5861 | } |
| 5862 | } |
Revathi Uddaraju | 20413a9 | 2016-10-13 17:16:14 +0800 | [diff] [blame] | 5863 | } else if (previousTrack == 0) { |
| 5864 | // there could be an old track added back during track transition for direct |
| 5865 | // output, so always issues flush to flush data of the previous track if it |
| 5866 | // was already destroyed with HAL paused, then flush can resume the playback |
| 5867 | mFlushPending = true; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5868 | } |
| 5869 | PlaybackThread::onAddNewTrack_l(); |
| 5870 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5871 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5872 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 5873 | Vector< sp<Track> > *tracksToRemove |
| 5874 | ) |
| 5875 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5876 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5877 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5878 | bool doHwPause = false; |
| 5879 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5880 | |
| 5881 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5882 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5883 | if (t->isInvalid()) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5884 | ALOGW("An invalidated track shouldn't be in active list"); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5885 | tracksToRemove->add(t); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5886 | continue; |
| 5887 | } |
| 5888 | |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5889 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5890 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5891 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5892 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5893 | // Only consider last track started for volume and mixer state control. |
| 5894 | // In theory an older track could underrun and restart after the new one starts |
| 5895 | // but as we only care about the transition phase between two tracks on a |
| 5896 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 5897 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5898 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5899 | |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 5900 | if (track->isPausePending()) { |
| 5901 | track->pauseAck(); |
| 5902 | // It is possible a track might have been flushed or stopped. |
| 5903 | // Other operations such as flush pending might occur on the next prepare. |
| 5904 | if (track->isPausing()) { |
| 5905 | track->setPaused(); |
| 5906 | } |
| 5907 | // Always perform pause, as an immediate flush will change |
| 5908 | // the pause state to be no longer isPausing(). |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5909 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5910 | doHwPause = true; |
| 5911 | mHwPaused = true; |
| 5912 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5913 | } else if (track->isFlushPending()) { |
| 5914 | track->flushAck(); |
| 5915 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5916 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5917 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 5918 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5919 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5920 | if (last) { |
| 5921 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5922 | if (mHwPaused) { |
| 5923 | doHwResume = true; |
| 5924 | mHwPaused = false; |
| 5925 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5926 | } |
| 5927 | } |
| 5928 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5929 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5930 | // for all its buffers to be filled before processing it. |
| 5931 | // Allow draining the buffer in case the client |
| 5932 | // app does not call stop() and relies on underrun to stop: |
| 5933 | // hence the test on (track->mRetryCount > 1). |
| 5934 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5935 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5936 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 5937 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5938 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5939 | minFrames = mNormalFrameCount; |
| 5940 | } else { |
| 5941 | minFrames = 1; |
| 5942 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5943 | |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 5944 | const size_t framesReady = track->framesReady(); |
| 5945 | const int trackId = track->id(); |
| 5946 | if (ATRACE_ENABLED()) { |
| 5947 | std::string traceName("nRdy"); |
| 5948 | traceName += std::to_string(trackId); |
| 5949 | ATRACE_INT(traceName.c_str(), framesReady); |
| 5950 | } |
| 5951 | if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() && |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5952 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5953 | { |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 5954 | ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5955 | |
| 5956 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5957 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5958 | if (last) { |
| 5959 | // make sure processVolume_l() will apply new volume even if 0 |
| 5960 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5961 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5962 | if (!mHwSupportsPause) { |
| 5963 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5968 | processVolume_l(track, last); |
| 5969 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5970 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5971 | if (previousTrack != 0) { |
| 5972 | if (track != previousTrack.get()) { |
| 5973 | // Flush any data still being written from last track |
| 5974 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 5975 | // Invalidate previous track to force a seek when resuming. |
| 5976 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5977 | } |
| 5978 | } |
| 5979 | mPreviousTrack = track; |
| 5980 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5981 | // reset retry count |
| 5982 | track->mRetryCount = kMaxTrackRetriesDirect; |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 5983 | mActiveTrack = t; |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5984 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5985 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5986 | doHwResume = true; |
| 5987 | mHwPaused = false; |
| 5988 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5989 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5990 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5991 | // clear effect chain input buffer if the last active track started underruns |
| 5992 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5993 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5994 | mEffectChains[0]->clearInputBuffer(); |
| 5995 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5996 | if (track->isStopping_1()) { |
| 5997 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5998 | if (last && mHwPaused) { |
| 5999 | doHwResume = true; |
| 6000 | mHwPaused = false; |
| 6001 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6002 | } |
| 6003 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 6004 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6005 | // We have consumed all the buffers of this track. |
| 6006 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6007 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 6008 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6009 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 6010 | } else { |
| 6011 | audioHALFrames = 0; |
| 6012 | } |
| 6013 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 6014 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6015 | if (mStandby || !last || |
Aniket Kumar Lata | 179a074 | 2019-01-30 14:16:16 -0800 | [diff] [blame] | 6016 | track->presentationComplete(framesWritten, audioHALFrames) || |
Jindong | 32dc26e | 2019-11-11 18:10:01 +0800 | [diff] [blame] | 6017 | track->isPaused() || mHwPaused) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6018 | if (track->isStopping_2()) { |
| 6019 | track->mState = TrackBase::STOPPED; |
| 6020 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6021 | if (track->isStopped()) { |
| 6022 | track->reset(); |
| 6023 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6024 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6025 | } |
| 6026 | } else { |
| 6027 | // No buffers for this track. Give it a few chances to |
| 6028 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6029 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6030 | if (--(track->mRetryCount) <= 0) { |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 6031 | ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", trackId); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6032 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 6033 | // indicate to client process that the track was disabled because of underrun; |
| 6034 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 6035 | track->disable(); |
Haynes Mathew George | 5c6daae | 2017-01-24 20:06:05 -0800 | [diff] [blame] | 6036 | // only do hw pause when track is going to be removed due to BUFFER TIMEOUT. |
| 6037 | // unlike mixerthread, HAL can be paused for direct output |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 6038 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 6039 | "minFrames = %u, mFormat = %#x", |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 6040 | framesReady, minFrames, mFormat); |
Haynes Mathew George | 5c6daae | 2017-01-24 20:06:05 -0800 | [diff] [blame] | 6041 | if (last && mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 6042 | doHwPause = true; |
| 6043 | mHwPaused = true; |
| 6044 | } |
Haynes Mathew George | 5c6daae | 2017-01-24 20:06:05 -0800 | [diff] [blame] | 6045 | } else if (last) { |
| 6046 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6047 | } |
| 6048 | } |
| 6049 | } |
| 6050 | } |
| 6051 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6052 | // 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] | 6053 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6054 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6055 | if (mTracks[i]->isFlushPending()) { |
| 6056 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6057 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6058 | } |
| 6059 | } |
| 6060 | } |
| 6061 | |
| 6062 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 6063 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 6064 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 6065 | // if resume is received before pause is executed. |
| 6066 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6067 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6068 | status_t result = mOutput->stream->pause(); |
| 6069 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6070 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6071 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6072 | flushHw_l(); |
| 6073 | } |
| 6074 | if (mHwSupportsPause && !mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6075 | status_t result = mOutput->stream->resume(); |
| 6076 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6077 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6078 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6079 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6080 | |
| 6081 | return mixerStatus; |
| 6082 | } |
| 6083 | |
| 6084 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 6085 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6086 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 6087 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6088 | // output audio to hardware |
| 6089 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 6090 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6091 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6092 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 6093 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 6094 | // no need to pad with 0 for compressed audio |
| 6095 | if (audio_has_proportional_frames(mFormat)) { |
| 6096 | memset(curBuf, 0, frameCount * mFrameSize); |
| 6097 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6098 | break; |
| 6099 | } |
| 6100 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 6101 | frameCount -= buffer.frameCount; |
| 6102 | curBuf += buffer.frameCount * mFrameSize; |
| 6103 | mActiveTrack->releaseBuffer(&buffer); |
| 6104 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 6105 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6106 | mSleepTimeUs = 0; |
| 6107 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6108 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 6112 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6113 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 6114 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6115 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6116 | return; |
| 6117 | } |
Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame^] | 6118 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 6119 | mSleepTimeUs = mActiveSleepTimeUs; |
| 6120 | } else { |
| 6121 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6122 | } |
Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame^] | 6123 | // Note: In S or later, we do not write zeroes for |
| 6124 | // linear or proportional PCM direct tracks in underrun. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6125 | } |
| 6126 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6127 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 6128 | { |
| 6129 | { |
| 6130 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6131 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6132 | if (mTracks[i]->isFlushPending()) { |
| 6133 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6134 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6135 | } |
| 6136 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6137 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6138 | flushHw_l(); |
| 6139 | } |
| 6140 | } |
| 6141 | PlaybackThread::threadLoop_exit(); |
| 6142 | } |
| 6143 | |
| 6144 | // must be called with thread mutex locked |
| 6145 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 6146 | { |
| 6147 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 6148 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6149 | |
| 6150 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 6151 | // after a timeout and we will enter standby then. |
| 6152 | if (mTracks.size() > 0) { |
| 6153 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 6154 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 6155 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6156 | } |
| 6157 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 6158 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6159 | } |
| 6160 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6161 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 6162 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 6163 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6164 | { |
| 6165 | bool reconfig = false; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6166 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6167 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6168 | AudioParameter param = AudioParameter(keyValuePair); |
| 6169 | int value; |
| 6170 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6171 | LOG_FATAL("Should not set routing device in DirectOutputThread"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6172 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6173 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6174 | // do not accept frame count changes if tracks are open as the track buffer |
| 6175 | // size depends on frame count and correct behavior would not be garantied |
| 6176 | // if frame count is changed after track creation |
| 6177 | if (!mTracks.isEmpty()) { |
| 6178 | status = INVALID_OPERATION; |
| 6179 | } else { |
| 6180 | reconfig = true; |
| 6181 | } |
| 6182 | } |
| 6183 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6184 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6185 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6186 | mOutput->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 6187 | if (!mStandby) { |
| 6188 | mThreadMetrics.logEndInterval(); |
| 6189 | mStandby = true; |
| 6190 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6191 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6192 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6193 | } |
| 6194 | if (status == NO_ERROR && reconfig) { |
| 6195 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6196 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6197 | } |
| 6198 | } |
| 6199 | |
Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 6200 | return reconfig; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6201 | } |
| 6202 | |
| 6203 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 6204 | { |
| 6205 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 6206 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6207 | time = PlaybackThread::activeSleepTimeUs(); |
| 6208 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 6209 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6210 | } |
| 6211 | return time; |
| 6212 | } |
| 6213 | |
| 6214 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 6215 | { |
| 6216 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 6217 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6218 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 6219 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 6220 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6221 | } |
| 6222 | return time; |
| 6223 | } |
| 6224 | |
| 6225 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 6226 | { |
| 6227 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 6228 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6229 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 6230 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 6231 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6232 | } |
| 6233 | return time; |
| 6234 | } |
| 6235 | |
| 6236 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 6237 | { |
| 6238 | PlaybackThread::cacheParameters_l(); |
| 6239 | |
| 6240 | // use shorter standby delay as on normal output to release |
| 6241 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 6242 | // no delay on outputs with HW A/V sync |
| 6243 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6244 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 6245 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6246 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 6247 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6248 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 6249 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6250 | } |
| 6251 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 6252 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 6253 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6254 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6255 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6256 | mFlushPending = false; |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 6257 | mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); |
Sampath Shetty | 999f0e8 | 2020-01-15 10:19:06 +1100 | [diff] [blame] | 6258 | mTimestamp.clear(); |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 6259 | } |
| 6260 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 6261 | int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const { |
| 6262 | // If a VolumeShaper is active, we must wake up periodically to update volume. |
| 6263 | const int64_t NS_PER_MS = 1000000; |
| 6264 | return mVolumeShaperActive ? |
| 6265 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); |
| 6266 | } |
| 6267 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6268 | // ---------------------------------------------------------------------------- |
| 6269 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6270 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 6271 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6272 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 6273 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6274 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 6275 | mDrainSequence(0), |
| 6276 | mAsyncError(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6277 | { |
| 6278 | } |
| 6279 | |
| 6280 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 6281 | { |
| 6282 | } |
| 6283 | |
| 6284 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 6285 | { |
| 6286 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 6287 | } |
| 6288 | |
| 6289 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 6290 | { |
| 6291 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6292 | uint32_t writeAckSequence; |
| 6293 | uint32_t drainSequence; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 6294 | bool asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6295 | |
| 6296 | { |
| 6297 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 6298 | while (!((mWriteAckSequence & 1) || |
| 6299 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 6300 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 6301 | exitPending())) { |
| 6302 | mWaitWorkCV.wait(mLock); |
| 6303 | } |
| 6304 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6305 | if (exitPending()) { |
| 6306 | break; |
| 6307 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6308 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 6309 | mWriteAckSequence, mDrainSequence); |
| 6310 | writeAckSequence = mWriteAckSequence; |
| 6311 | mWriteAckSequence &= ~1; |
| 6312 | drainSequence = mDrainSequence; |
| 6313 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 6314 | asyncError = mAsyncError; |
| 6315 | mAsyncError = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6316 | } |
| 6317 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 6318 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 6319 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6320 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 6321 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6322 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6323 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 6324 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6325 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 6326 | if (asyncError) { |
| 6327 | playbackThread->onAsyncError(); |
| 6328 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6329 | } |
| 6330 | } |
| 6331 | } |
| 6332 | return false; |
| 6333 | } |
| 6334 | |
| 6335 | void AudioFlinger::AsyncCallbackThread::exit() |
| 6336 | { |
| 6337 | ALOGV("AsyncCallbackThread::exit"); |
| 6338 | Mutex::Autolock _l(mLock); |
| 6339 | requestExit(); |
| 6340 | mWaitWorkCV.broadcast(); |
| 6341 | } |
| 6342 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6343 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6344 | { |
| 6345 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6346 | // bit 0 is cleared |
| 6347 | mWriteAckSequence = sequence << 1; |
| 6348 | } |
| 6349 | |
| 6350 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 6351 | { |
| 6352 | Mutex::Autolock _l(mLock); |
| 6353 | // ignore unexpected callbacks |
| 6354 | if (mWriteAckSequence & 2) { |
| 6355 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6356 | mWaitWorkCV.signal(); |
| 6357 | } |
| 6358 | } |
| 6359 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6360 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6361 | { |
| 6362 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6363 | // bit 0 is cleared |
| 6364 | mDrainSequence = sequence << 1; |
| 6365 | } |
| 6366 | |
| 6367 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 6368 | { |
| 6369 | Mutex::Autolock _l(mLock); |
| 6370 | // ignore unexpected callbacks |
| 6371 | if (mDrainSequence & 2) { |
| 6372 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6373 | mWaitWorkCV.signal(); |
| 6374 | } |
| 6375 | } |
| 6376 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 6377 | void AudioFlinger::AsyncCallbackThread::setAsyncError() |
| 6378 | { |
| 6379 | Mutex::Autolock _l(mLock); |
| 6380 | mAsyncError = true; |
| 6381 | mWaitWorkCV.signal(); |
| 6382 | } |
| 6383 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6384 | |
| 6385 | // ---------------------------------------------------------------------------- |
| 6386 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6387 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady) |
| 6388 | : DirectOutputThread(audioFlinger, output, id, OFFLOAD, systemReady), |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 6389 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), |
| 6390 | mOffloadUnderrunPosition(~0LL) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6391 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 6392 | //FIXME: mStandby should be set to true by ThreadBase constructo |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6393 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 6394 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6395 | } |
| 6396 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6397 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 6398 | { |
| 6399 | if (mFlushPending || mHwPaused) { |
| 6400 | // If a flush is pending or track was paused, just discard buffered data |
| 6401 | flushHw_l(); |
| 6402 | } else { |
| 6403 | mMixerStatus = MIXER_DRAIN_ALL; |
| 6404 | threadLoop_drain(); |
| 6405 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 6406 | if (mUseAsyncWrite) { |
| 6407 | ALOG_ASSERT(mCallbackThread != 0); |
| 6408 | mCallbackThread->exit(); |
| 6409 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6410 | PlaybackThread::threadLoop_exit(); |
| 6411 | } |
| 6412 | |
| 6413 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 6414 | Vector< sp<Track> > *tracksToRemove |
| 6415 | ) |
| 6416 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6417 | size_t count = mActiveTracks.size(); |
| 6418 | |
| 6419 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 6420 | bool doHwPause = false; |
| 6421 | bool doHwResume = false; |
| 6422 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6423 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 6424 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6425 | // find out which tracks need to be processed |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6426 | for (const sp<Track> &t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6427 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6428 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6429 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6430 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6431 | // Only consider last track started for volume and mixer state control. |
| 6432 | // In theory an older track could underrun and restart after the new one starts |
| 6433 | // but as we only care about the transition phase between two tracks on a |
| 6434 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 6435 | sp<Track> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6436 | bool last = l.get() == track; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6437 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 6438 | if (track->isInvalid()) { |
| 6439 | ALOGW("An invalidated track shouldn't be in active list"); |
| 6440 | tracksToRemove->add(track); |
| 6441 | continue; |
| 6442 | } |
| 6443 | |
| 6444 | if (track->mState == TrackBase::IDLE) { |
| 6445 | ALOGW("An idle track shouldn't be in active list"); |
| 6446 | continue; |
| 6447 | } |
| 6448 | |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 6449 | if (track->isPausePending()) { |
| 6450 | track->pauseAck(); |
| 6451 | // It is possible a track might have been flushed or stopped. |
| 6452 | // Other operations such as flush pending might occur on the next prepare. |
| 6453 | if (track->isPausing()) { |
| 6454 | track->setPaused(); |
| 6455 | } |
| 6456 | // Always perform pause if last, as an immediate flush will change |
| 6457 | // the pause state to be no longer isPausing(). |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6458 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 6459 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 6460 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6461 | mHwPaused = true; |
| 6462 | } |
| 6463 | // If we were part way through writing the mixbuffer to |
| 6464 | // the HAL we must save this until we resume |
| 6465 | // BUG - this will be wrong if a different track is made active, |
| 6466 | // in that case we want to discard the pending data in the |
| 6467 | // mixbuffer and tell the client to present it again when the |
| 6468 | // track is resumed |
| 6469 | mPausedWriteLength = mCurrentWriteLength; |
| 6470 | mPausedBytesRemaining = mBytesRemaining; |
| 6471 | mBytesRemaining = 0; // stop writing |
| 6472 | } |
| 6473 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 6474 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 6475 | if (track->isStopping_1()) { |
| 6476 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 6477 | } else { |
| 6478 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 6479 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 6480 | track->flushAck(); |
| 6481 | if (last) { |
| 6482 | mFlushPending = true; |
| 6483 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 6484 | } else if (track->isResumePending()){ |
| 6485 | track->resumeAck(); |
| 6486 | if (last) { |
| 6487 | if (mPausedBytesRemaining) { |
| 6488 | // Need to continue write that was interrupted |
| 6489 | mCurrentWriteLength = mPausedWriteLength; |
| 6490 | mBytesRemaining = mPausedBytesRemaining; |
| 6491 | mPausedBytesRemaining = 0; |
| 6492 | } |
| 6493 | if (mHwPaused) { |
| 6494 | doHwResume = true; |
| 6495 | mHwPaused = false; |
| 6496 | // threadLoop_mix() will handle the case that we need to |
| 6497 | // resume an interrupted write |
| 6498 | } |
| 6499 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6500 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 6501 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6502 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 6503 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 6504 | // Do not handle new data in this iteration even if track->framesReady() |
| 6505 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 6506 | } |
| 6507 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6508 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6509 | ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6510 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 6511 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6512 | if (last) { |
| 6513 | // make sure processVolume_l() will apply new volume even if 0 |
| 6514 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 6515 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6516 | } |
| 6517 | |
| 6518 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 6519 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 6520 | if (previousTrack != 0) { |
| 6521 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 6522 | // Flush any data still being written from last track |
| 6523 | mBytesRemaining = 0; |
| 6524 | if (mPausedBytesRemaining) { |
| 6525 | // Last track was paused so we also need to flush saved |
| 6526 | // mixbuffer state and invalidate track so that it will |
| 6527 | // re-submit that unwritten data when it is next resumed |
| 6528 | mPausedBytesRemaining = 0; |
| 6529 | // Invalidate is a bit drastic - would be more efficient |
| 6530 | // to have a flag to tell client that some of the |
| 6531 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 6532 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 6533 | } |
| 6534 | // flush data already sent to the DSP if changing audio session as audio |
| 6535 | // comes from a different source. Also invalidate previous track to force a |
| 6536 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 6537 | if (previousTrack->sessionId() != track->sessionId()) { |
| 6538 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 6539 | } |
| 6540 | } |
| 6541 | } |
| 6542 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6543 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 6544 | if (track->isStopping_1()) { |
| 6545 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 6546 | } else { |
| 6547 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 6548 | } |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6549 | mActiveTrack = t; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6550 | mixerStatus = MIXER_TRACKS_READY; |
| 6551 | } |
| 6552 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6553 | ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6554 | if (track->isStopping_1()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 6555 | if (--(track->mRetryCount) <= 0) { |
| 6556 | // Hardware buffer can hold a large amount of audio so we must |
| 6557 | // wait for all current track's data to drain before we say |
| 6558 | // that the track is stopped. |
| 6559 | if (mBytesRemaining == 0) { |
| 6560 | // Only start draining when all data in mixbuffer |
| 6561 | // has been written |
| 6562 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 6563 | track->mState = TrackBase::STOPPING_2; // so presentation completes after |
| 6564 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 6565 | if (last && !mStandby) { |
| 6566 | // do not modify drain sequence if we are already draining. This happens |
| 6567 | // when resuming from pause after drain. |
| 6568 | if ((mDrainSequence & 1) == 0) { |
| 6569 | mSleepTimeUs = 0; |
| 6570 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 6571 | mixerStatus = MIXER_DRAIN_TRACK; |
| 6572 | mDrainSequence += 2; |
| 6573 | } |
| 6574 | if (mHwPaused) { |
| 6575 | // It is possible to move from PAUSED to STOPPING_1 without |
| 6576 | // a resume so we must ensure hardware is running |
| 6577 | doHwResume = true; |
| 6578 | mHwPaused = false; |
| 6579 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6580 | } |
| 6581 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 6582 | } else if (last) { |
| 6583 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); |
| 6584 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6585 | } |
| 6586 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 6587 | // Drain has completed or we are in standby, signal presentation complete |
| 6588 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6589 | track->mState = TrackBase::STOPPED; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6590 | uint32_t latency = 0; |
| 6591 | status_t result = mOutput->stream->getLatency(&latency); |
| 6592 | ALOGE_IF(result != OK, |
| 6593 | "Error when retrieving output stream latency: %d", result); |
| 6594 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 6595 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6596 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6597 | track->presentationComplete(framesWritten, audioHALFrames); |
| 6598 | track->reset(); |
| 6599 | tracksToRemove->add(track); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 6600 | // OFFLOADED stop resets frame counts. |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 6601 | if (!mUseAsyncWrite) { |
| 6602 | // If we don't get explicit drain notification we must |
| 6603 | // register discontinuity regardless of whether this is |
| 6604 | // the previous (!last) or the upcoming (last) track |
| 6605 | // to avoid skipping the discontinuity. |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 6606 | mTimestampVerifier.discontinuity( |
| 6607 | mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 6608 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6609 | } |
| 6610 | } else { |
| 6611 | // No buffers for this track. Give it a few chances to |
| 6612 | // fill a buffer, then remove it from active list. |
| 6613 | if (--(track->mRetryCount) <= 0) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 6614 | bool running = false; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6615 | uint64_t position = 0; |
| 6616 | struct timespec unused; |
| 6617 | // The running check restarts the retry counter at least once. |
| 6618 | status_t ret = mOutput->stream->getPresentationPosition(&position, &unused); |
| 6619 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { |
| 6620 | running = true; |
| 6621 | mOffloadUnderrunPosition = position; |
| 6622 | } |
| 6623 | if (ret == NO_ERROR) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 6624 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, |
| 6625 | (long long)position, (long long)mOffloadUnderrunPosition); |
| 6626 | } |
| 6627 | if (running) { // still running, give us more time. |
| 6628 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 6629 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6630 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove track(%d) from active list", |
| 6631 | track->id()); |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 6632 | tracksToRemove->add(track); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 6633 | // tell client process that the track was disabled because of underrun; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 6634 | // it will then automatically call start() when data is available |
| 6635 | track->disable(); |
| 6636 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6637 | } else if (last){ |
| 6638 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 6639 | } |
| 6640 | } |
| 6641 | } |
| 6642 | // compute volume for this track |
Wenfeng Sun | 7945833 | 2019-05-29 20:12:43 +0800 | [diff] [blame] | 6643 | if (track->isReady()) { // check ready to prevent premature start. |
| 6644 | processVolume_l(track, last); |
| 6645 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6646 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 6647 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 6648 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 6649 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 6650 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 6651 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6652 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6653 | status_t result = mOutput->stream->pause(); |
| 6654 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 6655 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 6656 | if (mFlushPending) { |
| 6657 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 6658 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6659 | if (!mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6660 | status_t result = mOutput->stream->resume(); |
| 6661 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 6662 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 6663 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6664 | // remove all the tracks that need to be... |
| 6665 | removeTracks_l(*tracksToRemove); |
| 6666 | |
| 6667 | return mixerStatus; |
| 6668 | } |
| 6669 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6670 | // must be called with thread mutex locked |
| 6671 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 6672 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6673 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 6674 | mWriteAckSequence, mDrainSequence); |
| 6675 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6676 | return true; |
| 6677 | } |
| 6678 | return false; |
| 6679 | } |
| 6680 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6681 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 6682 | { |
| 6683 | Mutex::Autolock _l(mLock); |
| 6684 | return waitingAsyncCallback_l(); |
| 6685 | } |
| 6686 | |
| 6687 | void AudioFlinger::OffloadThread::flushHw_l() |
| 6688 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 6689 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6690 | // Flush anything still waiting in the mixbuffer |
| 6691 | mCurrentWriteLength = 0; |
| 6692 | mBytesRemaining = 0; |
| 6693 | mPausedWriteLength = 0; |
| 6694 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 6695 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 6696 | mBytesWritten = 0; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 6697 | mOffloadUnderrunPosition = ~0LL; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 6698 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6699 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6700 | // discard any pending drain or write ack by incrementing sequence |
| 6701 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 6702 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6703 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 6704 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 6705 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6706 | } |
| 6707 | } |
| 6708 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 6709 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
| 6710 | { |
| 6711 | Mutex::Autolock _l(mLock); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 6712 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 6713 | mFlushPending = true; |
| 6714 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 6715 | } |
| 6716 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6717 | // ---------------------------------------------------------------------------- |
| 6718 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6719 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6720 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6721 | : MixerThread(audioFlinger, mainThread->getOutput(), id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6722 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6723 | mWaitTimeMs(UINT_MAX) |
| 6724 | { |
| 6725 | addOutputTrack(mainThread); |
| 6726 | } |
| 6727 | |
| 6728 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 6729 | { |
| 6730 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6731 | mOutputTracks[i]->destroy(); |
| 6732 | } |
| 6733 | } |
| 6734 | |
| 6735 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 6736 | { |
| 6737 | // mix buffers... |
| 6738 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6739 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6740 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 6741 | if (mMixerBufferValid) { |
| 6742 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 6743 | } else { |
| 6744 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 6745 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6746 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6747 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6748 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 6749 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6750 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 6754 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6755 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6756 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6757 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6758 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6759 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6760 | } |
| 6761 | } else if (mBytesWritten != 0) { |
| 6762 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 6763 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 6764 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6765 | } else { |
| 6766 | // flush remaining overflow buffers in output tracks |
| 6767 | writeFrames = 0; |
| 6768 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 6769 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6770 | } |
| 6771 | } |
| 6772 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6773 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6774 | { |
| 6775 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 6776 | const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames); |
| 6777 | |
| 6778 | // Consider the first OutputTrack for timestamp and frame counting. |
| 6779 | |
| 6780 | // The threadLoop() generally assumes writing a full sink buffer size at a time. |
| 6781 | // Here, we correct for writeFrames of 0 (a stop) or underruns because |
| 6782 | // we always claim success. |
| 6783 | if (i == 0) { |
| 6784 | const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten; |
| 6785 | ALOGD_IF(correction != 0 && writeFrames != 0, |
| 6786 | "%s: writeFrames:%u actualWritten:%zd correction:%zd mFramesWritten:%lld", |
| 6787 | __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten); |
| 6788 | mFramesWritten -= correction; |
| 6789 | } |
| 6790 | |
| 6791 | // TODO: Report correction for the other output tracks and show in the dump. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6792 | } |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 6793 | if (mStandby) { |
| 6794 | mThreadMetrics.logBeginInterval(); |
| 6795 | mStandby = false; |
| 6796 | } |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 6797 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 6801 | { |
| 6802 | // DuplicatingThread implements standby by stopping all tracks |
| 6803 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 6804 | outputTracks[i]->stop(); |
| 6805 | } |
| 6806 | } |
| 6807 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6808 | void AudioFlinger::DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args __unused) |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6809 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6810 | MixerThread::dumpInternals_l(fd, args); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6811 | |
| 6812 | std::stringstream ss; |
| 6813 | const size_t numTracks = mOutputTracks.size(); |
| 6814 | ss << " " << numTracks << " OutputTracks"; |
| 6815 | if (numTracks > 0) { |
| 6816 | ss << ":"; |
| 6817 | for (const auto &track : mOutputTracks) { |
| 6818 | const sp<ThreadBase> thread = track->thread().promote(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6819 | ss << " (" << track->id() << " : "; |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6820 | if (thread.get() != nullptr) { |
| 6821 | ss << thread.get() << ", " << thread->id(); |
| 6822 | } else { |
| 6823 | ss << "null"; |
| 6824 | } |
| 6825 | ss << ")"; |
| 6826 | } |
| 6827 | } |
| 6828 | ss << "\n"; |
| 6829 | std::string result = ss.str(); |
| 6830 | write(fd, result.c_str(), result.size()); |
| 6831 | } |
| 6832 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6833 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 6834 | { |
| 6835 | outputTracks = mOutputTracks; |
| 6836 | } |
| 6837 | |
| 6838 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 6839 | { |
| 6840 | outputTracks.clear(); |
| 6841 | } |
| 6842 | |
| 6843 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 6844 | { |
| 6845 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6846 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 6847 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 6848 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 6849 | const size_t frameCount = |
| 6850 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 6851 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 6852 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 6853 | // nearly empty and the other may be dropping data). |
| 6854 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 6855 | // TODO b/182392769: use identity util, move to server edge |
| 6856 | Identity identity = Identity(); |
| 6857 | identity.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t( |
| 6858 | IPCThreadState::self()->getCallingUid())); |
| 6859 | identity.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t( |
| 6860 | IPCThreadState::self()->getCallingPid())); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6861 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6862 | this, |
| 6863 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 6864 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6865 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6866 | frameCount, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 6867 | identity); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 6868 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 6869 | if (status != NO_ERROR) { |
| 6870 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 6871 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6872 | } |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 6873 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
| 6874 | mOutputTracks.add(outputTrack); |
| 6875 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 6876 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6877 | } |
| 6878 | |
| 6879 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 6880 | { |
| 6881 | Mutex::Autolock _l(mLock); |
| 6882 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6883 | if (mOutputTracks[i]->thread() == thread) { |
| 6884 | mOutputTracks[i]->destroy(); |
| 6885 | mOutputTracks.removeAt(i); |
| 6886 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 6887 | if (thread->getOutput() == mOutput) { |
| 6888 | mOutput = NULL; |
| 6889 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6890 | return; |
| 6891 | } |
| 6892 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 6893 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | // caller must hold mLock |
| 6897 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 6898 | { |
| 6899 | mWaitTimeMs = UINT_MAX; |
| 6900 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 6901 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 6902 | if (strong != 0) { |
| 6903 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 6904 | if (waitTimeMs < mWaitTimeMs) { |
| 6905 | mWaitTimeMs = waitTimeMs; |
| 6906 | } |
| 6907 | } |
| 6908 | } |
| 6909 | } |
| 6910 | |
| 6911 | |
| 6912 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 6913 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 6914 | { |
| 6915 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 6916 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 6917 | if (thread == 0) { |
| 6918 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 6919 | outputTracks[i].get()); |
| 6920 | return false; |
| 6921 | } |
| 6922 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 6923 | // see note at standby() declaration |
| 6924 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 6925 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 6926 | thread.get()); |
| 6927 | return false; |
| 6928 | } |
| 6929 | } |
| 6930 | return true; |
| 6931 | } |
| 6932 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6933 | void AudioFlinger::DuplicatingThread::sendMetadataToBackend_l( |
| 6934 | const StreamOutHalInterface::SourceMetadata& metadata) |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 6935 | { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6936 | for (auto& outputTrack : outputTracks) { // not mOutputTracks |
| 6937 | outputTrack->setMetadatas(metadata.tracks); |
| 6938 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 6939 | } |
| 6940 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6941 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 6942 | { |
| 6943 | return (mWaitTimeMs * 1000) / 2; |
| 6944 | } |
| 6945 | |
| 6946 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 6947 | { |
| 6948 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 6949 | updateWaitTime_l(); |
| 6950 | |
| 6951 | MixerThread::cacheParameters_l(); |
| 6952 | } |
| 6953 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 6954 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6955 | // ---------------------------------------------------------------------------- |
| 6956 | // Record |
| 6957 | // ---------------------------------------------------------------------------- |
| 6958 | |
| 6959 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 6960 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6961 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 6962 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 6963 | ) : |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 6964 | ThreadBase(audioFlinger, id, RECORD, systemReady, false /* isOut */), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6965 | mInput(input), |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 6966 | mSource(mInput), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 6967 | mActiveTracks(&this->mLocalLog), |
| 6968 | mRsmpInBuffer(NULL), |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 6969 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6970 | mRsmpInRear(0) |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 6971 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 6972 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6973 | // mFastCapture below |
| 6974 | , mFastCaptureFutex(0) |
| 6975 | // mInputSource |
| 6976 | // mPipeSink |
| 6977 | // mPipeSource |
| 6978 | , mPipeFramesP2(0) |
| 6979 | // mPipeMemory |
| 6980 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6981 | , mFastTrackAvail(false) |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 6982 | , mBtNrecSuspended(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6983 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6984 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 6985 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6986 | |
George Burgess IV | a8f90c1 | 2020-05-14 11:27:19 -0700 | [diff] [blame] | 6987 | if (mInput->audioHwDev != nullptr) { |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 6988 | mIsMsdDevice = strcmp( |
| 6989 | mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; |
| 6990 | } |
| 6991 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6992 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6993 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 6994 | // TODO: We may also match on address as well as device type for |
| 6995 | // AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_IN_REMOTE_SUBMIX |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6996 | // TODO: This property should be ensure that only contains one single device type. |
| 6997 | mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( |
| 6998 | "audio.timestamp.corrected_input_device", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 6999 | (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD |
| 7000 | : AUDIO_DEVICE_NONE)); |
| 7001 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7002 | // create an NBAIO source for the HAL input stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 7003 | mInputSource = new AudioStreamInSource(input->stream); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7004 | size_t numCounterOffers = 0; |
| 7005 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7006 | #if !LOG_NDEBUG |
| 7007 | ssize_t index = |
| 7008 | #else |
| 7009 | (void) |
| 7010 | #endif |
| 7011 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7012 | ALOG_ASSERT(index == 0); |
| 7013 | |
| 7014 | // initialize fast capture depending on configuration |
| 7015 | bool initFastCapture; |
| 7016 | switch (kUseFastCapture) { |
| 7017 | case FastCapture_Never: |
| 7018 | initFastCapture = false; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7019 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7020 | break; |
| 7021 | case FastCapture_Always: |
| 7022 | initFastCapture = true; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7023 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7024 | break; |
| 7025 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 7026 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7027 | ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d", |
| 7028 | this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs, |
| 7029 | initFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7030 | break; |
| 7031 | // case FastCapture_Dynamic: |
| 7032 | } |
| 7033 | |
| 7034 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 7035 | // 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] | 7036 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 7037 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread |
| 7038 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7039 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7040 | void *pipeBuffer = nullptr; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7041 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 7042 | sp<IMemory> pipeMemory; |
| 7043 | if ((roHeap == 0) || |
| 7044 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 7045 | (pipeBuffer = pipeMemory->unsecurePointer()) == nullptr) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7046 | ALOGE("not enough memory for pipe buffer size=%zu; " |
| 7047 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", |
| 7048 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, |
| 7049 | (long long)kRecordThreadReadOnlyHeapSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7050 | goto failed; |
| 7051 | } |
| 7052 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 7053 | memset(pipeBuffer, 0, pipeSize); |
| 7054 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 7055 | const NBAIO_Format offers[1] = {format}; |
| 7056 | size_t numCounterOffers = 0; |
| 7057 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 7058 | ALOG_ASSERT(index == 0); |
| 7059 | mPipeSink = pipe; |
| 7060 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 7061 | numCounterOffers = 0; |
| 7062 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 7063 | ALOG_ASSERT(index == 0); |
| 7064 | mPipeSource = pipeReader; |
| 7065 | mPipeFramesP2 = pipeFramesP2; |
| 7066 | mPipeMemory = pipeMemory; |
| 7067 | |
| 7068 | // create fast capture |
| 7069 | mFastCapture = new FastCapture(); |
| 7070 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 7071 | #ifdef STATE_QUEUE_DUMP |
| 7072 | // FIXME |
| 7073 | #endif |
| 7074 | FastCaptureState *state = sq->begin(); |
| 7075 | state->mCblk = NULL; |
| 7076 | state->mInputSource = mInputSource.get(); |
| 7077 | state->mInputSourceGen++; |
| 7078 | state->mPipeSink = pipe; |
| 7079 | state->mPipeSinkGen++; |
| 7080 | state->mFrameCount = mFrameCount; |
| 7081 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 7082 | // already done in constructor initialization list |
| 7083 | //mFastCaptureFutex = 0; |
| 7084 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 7085 | state->mColdGen++; |
| 7086 | state->mDumpState = &mFastCaptureDumpState; |
| 7087 | #ifdef TEE_SINK |
| 7088 | // FIXME |
| 7089 | #endif |
| 7090 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 7091 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 7092 | sq->end(); |
| 7093 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 7094 | |
| 7095 | // start the fast capture |
| 7096 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 7097 | pid_t tid = mFastCapture->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 7098 | sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 7099 | stream()->setHalThreadPriority(kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7100 | #ifdef AUDIO_WATCHDOG |
| 7101 | // FIXME |
| 7102 | #endif |
| 7103 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 7104 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7105 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 7106 | #ifdef TEE_SINK |
| 7107 | mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD); |
| 7108 | mTee.setId(std::string("_") + std::to_string(mId) + "_C"); |
| 7109 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7110 | failed: ; |
| 7111 | |
| 7112 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7113 | } |
| 7114 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7115 | AudioFlinger::RecordThread::~RecordThread() |
| 7116 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7117 | if (mFastCapture != 0) { |
| 7118 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 7119 | FastCaptureState *state = sq->begin(); |
| 7120 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 7121 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 7122 | if (old == -1) { |
| 7123 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 7124 | } |
| 7125 | } |
| 7126 | state->mCommand = FastCaptureState::EXIT; |
| 7127 | sq->end(); |
| 7128 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 7129 | mFastCapture->join(); |
| 7130 | mFastCapture.clear(); |
| 7131 | } |
| 7132 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 7133 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7134 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7135 | } |
| 7136 | |
| 7137 | void AudioFlinger::RecordThread::onFirstRef() |
| 7138 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 7139 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7140 | } |
| 7141 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 7142 | void AudioFlinger::RecordThread::preExit() |
| 7143 | { |
| 7144 | ALOGV(" preExit()"); |
| 7145 | Mutex::Autolock _l(mLock); |
| 7146 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7147 | sp<RecordTrack> track = mTracks[i]; |
| 7148 | track->invalidate(); |
| 7149 | } |
| 7150 | mActiveTracks.clear(); |
| 7151 | mStartStopCond.broadcast(); |
| 7152 | } |
| 7153 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7154 | bool AudioFlinger::RecordThread::threadLoop() |
| 7155 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7156 | nsecs_t lastWarning = 0; |
| 7157 | |
| 7158 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7159 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 7160 | reacquire_wakelock: |
| 7161 | sp<RecordTrack> activeTrack; |
| 7162 | { |
| 7163 | Mutex::Autolock _l(mLock); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 7164 | acquireWakeLock_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 7165 | } |
| 7166 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7167 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 7168 | uint32_t sleepUs = 0; |
| 7169 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 7170 | int64_t lastLoopCountRead = -2; // never matches "previous" loop, when loopCount = 0. |
| 7171 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7172 | // loop while there is work to do |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 7173 | for (int64_t loopCount = 0;; ++loopCount) { // loopCount used for statistics tracking |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 7174 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 7175 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7176 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 7177 | Vector< sp<RecordTrack> > activeTracks; |
| 7178 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7179 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7180 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7181 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7182 | // reference to a fast track which is about to be removed |
| 7183 | sp<RecordTrack> fastTrackToRemove; |
| 7184 | |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 7185 | bool silenceFastCapture = false; |
| 7186 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7187 | { // scope for mLock |
| 7188 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 7189 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 7190 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 7191 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 7192 | // check exitPending here because checkForNewParameters_l() and |
| 7193 | // checkForNewParameters_l() can temporarily release mLock |
| 7194 | if (exitPending()) { |
| 7195 | break; |
| 7196 | } |
| 7197 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7198 | // sleep with mutex unlocked |
| 7199 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 7200 | ATRACE_BEGIN("sleepC"); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7201 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); |
| 7202 | ATRACE_END(); |
| 7203 | sleepUs = 0; |
| 7204 | continue; |
| 7205 | } |
| 7206 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7207 | // if no active track(s), then standby and release wakelock |
| 7208 | size_t size = mActiveTracks.size(); |
| 7209 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 7210 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 7211 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7212 | releaseWakeLock_l(); |
| 7213 | ALOGV("RecordThread: loop stopping"); |
| 7214 | // go to sleep |
| 7215 | mWaitWorkCV.wait(mLock); |
| 7216 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 7217 | goto reacquire_wakelock; |
| 7218 | } |
| 7219 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7220 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7221 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7222 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7223 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7224 | activeTrack = mActiveTracks[i]; |
| 7225 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7226 | if (activeTrack->isFastTrack()) { |
| 7227 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 7228 | fastTrackToRemove = activeTrack; |
| 7229 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7230 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7231 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7232 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7233 | continue; |
| 7234 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7235 | |
| 7236 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 7237 | switch (activeTrackState) { |
| 7238 | |
| 7239 | case TrackBase::PAUSING: |
| 7240 | mActiveTracks.remove(activeTrack); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 7241 | activeTrack->mState = TrackBase::PAUSED; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7242 | doBroadcast = true; |
| 7243 | size--; |
| 7244 | continue; |
| 7245 | |
| 7246 | case TrackBase::STARTING_1: |
| 7247 | sleepUs = 10000; |
| 7248 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7249 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7250 | continue; |
| 7251 | |
| 7252 | case TrackBase::STARTING_2: |
| 7253 | doBroadcast = true; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7254 | if (mStandby) { |
| 7255 | mThreadMetrics.logBeginInterval(); |
| 7256 | mStandby = false; |
| 7257 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7258 | activeTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7259 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7260 | break; |
| 7261 | |
| 7262 | case TrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7263 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7264 | break; |
| 7265 | |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 7266 | case TrackBase::IDLE: // cannot be on ActiveTracks if idle |
| 7267 | case TrackBase::PAUSED: // cannot be on ActiveTracks if paused |
| 7268 | case TrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7269 | default: |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 7270 | LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu", |
| 7271 | __func__, activeTrackState, activeTrack->id(), size); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7272 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7273 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7274 | if (activeTrack->isFastTrack()) { |
| 7275 | ALOG_ASSERT(!mFastTrackAvail); |
| 7276 | ALOG_ASSERT(fastTrack == 0); |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 7277 | // if the active fast track is silenced either: |
| 7278 | // 1) silence the whole capture from fast capture buffer if this is |
| 7279 | // the only active track |
| 7280 | // 2) invalidate this track: this will cause the client to reconnect and possibly |
| 7281 | // be invalidated again until unsilenced |
| 7282 | if (activeTrack->isSilenced()) { |
| 7283 | if (size > 1) { |
| 7284 | activeTrack->invalidate(); |
| 7285 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 7286 | fastTrackToRemove = activeTrack; |
| 7287 | removeTrack_l(activeTrack); |
| 7288 | mActiveTracks.remove(activeTrack); |
| 7289 | size--; |
| 7290 | continue; |
| 7291 | } else { |
| 7292 | silenceFastCapture = true; |
| 7293 | } |
| 7294 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7295 | fastTrack = activeTrack; |
| 7296 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 7297 | |
| 7298 | activeTracks.add(activeTrack); |
| 7299 | i++; |
| 7300 | |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7301 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7302 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 7303 | mActiveTracks.updatePowerState(this); |
| 7304 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 7305 | updateMetadata_l(); |
| 7306 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 7307 | if (allStopped) { |
| 7308 | standbyIfNotAlreadyInStandby(); |
| 7309 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7310 | if (doBroadcast) { |
| 7311 | mStartStopCond.broadcast(); |
| 7312 | } |
| 7313 | |
| 7314 | // sleep if there are no active tracks to process |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 7315 | if (activeTracks.isEmpty()) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7316 | if (sleepUs == 0) { |
| 7317 | sleepUs = kRecordThreadSleepUs; |
| 7318 | } |
| 7319 | continue; |
| 7320 | } |
| 7321 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 7322 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7323 | lockEffectChains_l(effectChains); |
| 7324 | } |
| 7325 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7326 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 7327 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7328 | size_t size = effectChains.size(); |
| 7329 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 7330 | // thread mutex is not locked, but effect chain is locked |
| 7331 | effectChains[i]->process_l(); |
| 7332 | } |
| 7333 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7334 | // 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] | 7335 | if (mFastCapture != 0) { |
| 7336 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 7337 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7338 | bool didModify = false; |
| 7339 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7340 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 7341 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 7342 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 7343 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 7344 | if (old == -1) { |
| 7345 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 7346 | } |
| 7347 | } |
| 7348 | state->mCommand = FastCaptureState::READ_WRITE; |
| 7349 | #if 0 // FIXME |
| 7350 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 7351 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 7352 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7353 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7354 | didModify = true; |
| 7355 | } |
| 7356 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 7357 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 7358 | if (cblkNew != cblkOld) { |
| 7359 | state->mCblk = cblkNew; |
| 7360 | // block until acked if removing a fast track |
| 7361 | if (cblkOld != NULL) { |
| 7362 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 7363 | } |
| 7364 | didModify = true; |
| 7365 | } |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 7366 | AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ? |
| 7367 | reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr; |
| 7368 | if (state->mFastPatchRecordBufferProvider != abp) { |
| 7369 | state->mFastPatchRecordBufferProvider = abp; |
| 7370 | state->mFastPatchRecordFormat = fastTrack == 0 ? |
| 7371 | AUDIO_FORMAT_INVALID : fastTrack->format(); |
| 7372 | didModify = true; |
| 7373 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 7374 | if (state->mSilenceCapture != silenceFastCapture) { |
| 7375 | state->mSilenceCapture = silenceFastCapture; |
| 7376 | didModify = true; |
| 7377 | } |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7378 | sq->end(didModify); |
| 7379 | if (didModify) { |
| 7380 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7381 | #if 0 |
| 7382 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 7383 | mNormalSource = mPipeSource; |
| 7384 | } |
| 7385 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7386 | } |
| 7387 | } |
| 7388 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 7389 | // now run the fast track destructor with thread mutex unlocked |
| 7390 | fastTrackToRemove.clear(); |
| 7391 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7392 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 7393 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 7394 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 7395 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 7396 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 7397 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7398 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7399 | ssize_t framesRead; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 7400 | const int64_t lastIoBeginNs = systemTime(); // start IO timing |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7401 | |
| 7402 | // If an NBAIO source is present, use it to read the normal capture's data |
| 7403 | if (mPipeSource != 0) { |
Andy Hung | 156317a | 2018-08-02 11:49:29 -0700 | [diff] [blame] | 7404 | size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); |
Andy Hung | d5b638f | 2018-04-30 13:56:10 -0700 | [diff] [blame] | 7405 | |
| 7406 | // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer |
| 7407 | // to the full buffer point (clearing the overflow condition). Upon OVERRUN error, |
| 7408 | // we immediately retry the read() to get data and prevent another overflow. |
| 7409 | for (int retries = 0; retries <= 2; ++retries) { |
| 7410 | ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries); |
| 7411 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
| 7412 | framesToRead); |
| 7413 | if (framesRead != OVERRUN) break; |
| 7414 | } |
| 7415 | |
Andy Hung | 7a3dc6b | 2018-05-01 16:39:51 -0700 | [diff] [blame] | 7416 | const ssize_t availableToRead = mPipeSource->availableToRead(); |
| 7417 | if (availableToRead >= 0) { |
Glenn Kasten | a2f59b3 | 2020-08-03 16:37:24 -0700 | [diff] [blame] | 7418 | // PipeSource is the primary clock. It is up to the AudioRecord client to keep up. |
Andy Hung | 7a3dc6b | 2018-05-01 16:39:51 -0700 | [diff] [blame] | 7419 | LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2, |
| 7420 | "more frames to read than fifo size, %zd > %zu", |
| 7421 | availableToRead, mPipeFramesP2); |
| 7422 | const size_t pipeFramesFree = mPipeFramesP2 - availableToRead; |
| 7423 | const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2; |
| 7424 | ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd", |
| 7425 | mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 7426 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; |
| 7427 | } |
| 7428 | if (framesRead < 0) { |
| 7429 | status_t status = (status_t) framesRead; |
| 7430 | switch (status) { |
| 7431 | case OVERRUN: |
| 7432 | ALOGW("overrun on read from pipe"); |
| 7433 | framesRead = 0; |
| 7434 | break; |
| 7435 | case NEGOTIATE: |
| 7436 | ALOGE("re-negotiation is needed"); |
| 7437 | framesRead = -1; // Will cause an attempt to recover. |
| 7438 | break; |
| 7439 | default: |
| 7440 | ALOGE("unknown error %d on read from pipe", status); |
| 7441 | break; |
| 7442 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7443 | } |
| 7444 | // otherwise use the HAL / AudioStreamIn directly |
| 7445 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 7446 | ATRACE_BEGIN("read"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7447 | size_t bytesRead; |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 7448 | status_t result = mSource->read( |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7449 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 7450 | ATRACE_END(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7451 | if (result < 0) { |
| 7452 | framesRead = result; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7453 | } else { |
| 7454 | framesRead = bytesRead / mFrameSize; |
| 7455 | } |
| 7456 | } |
| 7457 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 7458 | const int64_t lastIoEndNs = systemTime(); // end IO timing |
| 7459 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7460 | // Update server timestamp with server stats |
| 7461 | // systemTime() is optional if the hardware supports timestamps. |
Andy Hung | 743f640 | 2020-06-03 13:17:04 -0700 | [diff] [blame] | 7462 | if (framesRead >= 0) { |
| 7463 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 7464 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs; |
| 7465 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7466 | |
| 7467 | // Update server timestamp with kernel stats |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7468 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7469 | int64_t position, time; |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 7470 | if (mStandby) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7471 | mTimestampVerifier.discontinuity(audio_is_linear_pcm(mFormat) ? |
| 7472 | mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS : |
| 7473 | mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 7474 | } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 7475 | && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) { |
| 7476 | |
| 7477 | mTimestampVerifier.add(position, time, mSampleRate); |
| 7478 | |
| 7479 | // Correct timestamps |
| 7480 | if (isTimestampCorrectionEnabled()) { |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 7481 | ALOGVV("TS_BEFORE: %d %lld %lld", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 7482 | id(), (long long)time, (long long)position); |
| 7483 | auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); |
| 7484 | position = correctedTimestamp.mFrames; |
| 7485 | time = correctedTimestamp.mTimeNs; |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 7486 | ALOGVV("TS_AFTER: %d %lld %lld", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 7487 | id(), (long long)time, (long long)position); |
| 7488 | } |
| 7489 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7490 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 7491 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 7492 | // Note: In general record buffers should tend to be empty in |
| 7493 | // a properly running pipeline. |
| 7494 | // |
| 7495 | // Also, it is not advantageous to call get_presentation_position during the read |
| 7496 | // as the read obtains a lock, preventing the timestamp call from executing. |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 7497 | } else { |
| 7498 | mTimestampVerifier.error(); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7499 | } |
| 7500 | } |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 7501 | |
| 7502 | // From the timestamp, input read latency is negative output write latency. |
| 7503 | const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE; |
| 7504 | const double latencyMs = RecordTrack::checkServerLatencySupported(mFormat, flags) |
| 7505 | ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.; |
| 7506 | if (latencyMs != 0.) { // note 0. means timestamp is empty. |
| 7507 | mLatencyMs.add(latencyMs); |
| 7508 | } |
| 7509 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7510 | // Use this to track timestamp information |
| 7511 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 7512 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7513 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7514 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7515 | // Force input into standby so that it tries to recover at next read attempt |
| 7516 | inputStandBy(); |
| 7517 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7518 | } |
| 7519 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 7520 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 7521 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7522 | ALOG_ASSERT(framesRead > 0); |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 7523 | mFramesRead += framesRead; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7524 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 7525 | #ifdef TEE_SINK |
| 7526 | (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
| 7527 | #endif |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7528 | // 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] | 7529 | { |
| 7530 | size_t part1 = mRsmpInFramesP2 - rear; |
| 7531 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7532 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 7533 | (framesRead - part1) * mFrameSize); |
| 7534 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7535 | } |
Dean Wheatley | fd56e81 | 2020-11-06 22:32:21 +1100 | [diff] [blame] | 7536 | mRsmpInRear = audio_utils::safe_add_overflow(mRsmpInRear, (int32_t)framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7537 | |
| 7538 | size = activeTracks.size(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7539 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7540 | // loop over each active track |
| 7541 | for (size_t i = 0; i < size; i++) { |
| 7542 | activeTrack = activeTracks[i]; |
| 7543 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7544 | // skip fast tracks, as those are handled directly by FastCapture |
| 7545 | if (activeTrack->isFastTrack()) { |
| 7546 | continue; |
| 7547 | } |
| 7548 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7549 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7550 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 7551 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7552 | enum { |
| 7553 | OVERRUN_UNKNOWN, |
| 7554 | OVERRUN_TRUE, |
| 7555 | OVERRUN_FALSE |
| 7556 | } overrun = OVERRUN_UNKNOWN; |
| 7557 | |
| 7558 | // loop over getNextBuffer to handle circular sink |
| 7559 | for (;;) { |
| 7560 | |
| 7561 | activeTrack->mSink.frameCount = ~0; |
| 7562 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 7563 | size_t framesOut = activeTrack->mSink.frameCount; |
| 7564 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 7565 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7566 | // check available frames and handle overrun conditions |
| 7567 | // if the record track isn't draining fast enough. |
| 7568 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7569 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7570 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 7571 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7572 | overrun = OVERRUN_TRUE; |
| 7573 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7574 | if (framesOut == 0 || framesIn == 0) { |
| 7575 | break; |
| 7576 | } |
| 7577 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 7578 | // Don't allow framesOut to be larger than what is possible with resampling |
| 7579 | // from framesIn. |
| 7580 | // This isn't strictly necessary but helps limit buffer resizing in |
| 7581 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 7582 | framesOut = min(framesOut, |
| 7583 | destinationFramesPossible( |
| 7584 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 7585 | |
| 7586 | if (activeTrack->isDirect()) { |
Daniel Van Veen | 9e2376e | 2018-09-27 14:37:58 +1000 | [diff] [blame] | 7587 | // No RecordBufferConverter used for direct streams. Pass |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 7588 | // straight from RecordThread buffer to RecordTrack buffer. |
| 7589 | AudioBufferProvider::Buffer buffer; |
| 7590 | buffer.frameCount = framesOut; |
| 7591 | status_t status = activeTrack->mResamplerBufferProvider->getNextBuffer(&buffer); |
| 7592 | if (status == OK && buffer.frameCount != 0) { |
| 7593 | ALOGV_IF(buffer.frameCount != framesOut, |
| 7594 | "%s() read less than expected (%zu vs %zu)", |
| 7595 | __func__, buffer.frameCount, framesOut); |
| 7596 | framesOut = buffer.frameCount; |
Daniel Van Veen | 9e2376e | 2018-09-27 14:37:58 +1000 | [diff] [blame] | 7597 | memcpy(activeTrack->mSink.raw, buffer.raw, buffer.frameCount * mFrameSize); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 7598 | activeTrack->mResamplerBufferProvider->releaseBuffer(&buffer); |
| 7599 | } else { |
| 7600 | framesOut = 0; |
| 7601 | ALOGE("%s() cannot fill request, status: %d, frameCount: %zu", |
| 7602 | __func__, status, buffer.frameCount); |
| 7603 | } |
| 7604 | } else { |
| 7605 | // process frames from the RecordThread buffer provider to the RecordTrack |
| 7606 | // buffer |
| 7607 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 7608 | activeTrack->mSink.raw, |
| 7609 | activeTrack->mResamplerBufferProvider, |
| 7610 | framesOut); |
| 7611 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7612 | |
| 7613 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 7614 | overrun = OVERRUN_FALSE; |
| 7615 | } |
| 7616 | |
| 7617 | if (activeTrack->mFramesToDrop == 0) { |
| 7618 | if (framesOut > 0) { |
| 7619 | activeTrack->mSink.frameCount = framesOut; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7620 | // Sanitize before releasing if the track has no access to the source data |
| 7621 | // An idle UID receives silence from non virtual devices until active |
| 7622 | if (activeTrack->isSilenced()) { |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 7623 | memset(activeTrack->mSink.raw, 0, framesOut * activeTrack->frameSize()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 7624 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7625 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 7626 | } |
| 7627 | } else { |
| 7628 | // FIXME could do a partial drop of framesOut |
| 7629 | if (activeTrack->mFramesToDrop > 0) { |
Mikhail Naganov | 6d91ba5 | 2019-03-26 14:35:28 -0700 | [diff] [blame] | 7630 | activeTrack->mFramesToDrop -= (ssize_t)framesOut; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7631 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7632 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7633 | } |
| 7634 | } else { |
| 7635 | activeTrack->mFramesToDrop += framesOut; |
| 7636 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 7637 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 7638 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 7639 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 7640 | activeTrack->sessionId(), |
| 7641 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7642 | activeTrack->mSyncStartEvent->triggerSession() : |
| 7643 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7644 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7645 | } |
| 7646 | } |
| 7647 | } |
| 7648 | |
| 7649 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7650 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 7651 | } |
| 7652 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7653 | |
| 7654 | switch (overrun) { |
| 7655 | case OVERRUN_TRUE: |
| 7656 | // client isn't retrieving buffers fast enough |
| 7657 | if (!activeTrack->setOverflow()) { |
| 7658 | nsecs_t now = systemTime(); |
| 7659 | // FIXME should lastWarning per track? |
| 7660 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 7661 | ALOGW("RecordThread: buffer overflow"); |
| 7662 | lastWarning = now; |
| 7663 | } |
| 7664 | } |
| 7665 | break; |
| 7666 | case OVERRUN_FALSE: |
| 7667 | activeTrack->clearOverflow(); |
| 7668 | break; |
| 7669 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7670 | break; |
| 7671 | } |
| 7672 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7673 | // update frame information and push timestamp out |
| 7674 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 7675 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 7676 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 7677 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 7678 | } |
| 7679 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 7680 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7681 | // enable changes in effect chain |
| 7682 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 7683 | // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end |
Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 7684 | if (audio_has_proportional_frames(mFormat) |
| 7685 | && loopCount == lastLoopCountRead + 1) { |
| 7686 | const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs; |
| 7687 | const double jitterMs = |
| 7688 | TimestampVerifier<int64_t, int64_t>::computeJitterMs( |
| 7689 | {framesRead, readPeriodNs}, |
| 7690 | {0, 0} /* lastTimestamp */, mSampleRate); |
| 7691 | const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6; |
| 7692 | |
| 7693 | Mutex::Autolock _l(mLock); |
| 7694 | mIoJitterMs.add(jitterMs); |
| 7695 | mProcessTimeMs.add(processMs); |
| 7696 | } |
| 7697 | // update timing info. |
| 7698 | mLastIoBeginNs = lastIoBeginNs; |
| 7699 | mLastIoEndNs = lastIoEndNs; |
| 7700 | lastLoopCountRead = loopCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7701 | } |
| 7702 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 7703 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7704 | |
| 7705 | { |
| 7706 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 7707 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7708 | sp<RecordTrack> track = mTracks[i]; |
| 7709 | track->invalidate(); |
| 7710 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 7711 | mActiveTracks.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7712 | mStartStopCond.broadcast(); |
| 7713 | } |
| 7714 | |
| 7715 | releaseWakeLock(); |
| 7716 | |
| 7717 | ALOGV("RecordThread %p exiting", this); |
| 7718 | return false; |
| 7719 | } |
| 7720 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 7721 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7722 | { |
| 7723 | if (!mStandby) { |
| 7724 | inputStandBy(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7725 | mThreadMetrics.logEndInterval(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7726 | mStandby = true; |
| 7727 | } |
| 7728 | } |
| 7729 | |
| 7730 | void AudioFlinger::RecordThread::inputStandBy() |
| 7731 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7732 | // Idle the fast capture if it's currently running |
| 7733 | if (mFastCapture != 0) { |
| 7734 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 7735 | FastCaptureState *state = sq->begin(); |
| 7736 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 7737 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 7738 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 7739 | state->mColdGen++; |
| 7740 | mFastCaptureFutex = 0; |
| 7741 | sq->end(); |
| 7742 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 7743 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 7744 | #if 0 |
| 7745 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 7746 | // FIXME |
| 7747 | } |
| 7748 | #endif |
| 7749 | #ifdef AUDIO_WATCHDOG |
| 7750 | // FIXME |
| 7751 | #endif |
| 7752 | } else { |
| 7753 | sq->end(false /*didModify*/); |
| 7754 | } |
| 7755 | } |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 7756 | status_t result = mSource->standby(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7757 | ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result); |
Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 7758 | |
| 7759 | // If going into standby, flush the pipe source. |
| 7760 | if (mPipeSource.get() != nullptr) { |
| 7761 | const ssize_t flushed = mPipeSource->flush(); |
| 7762 | if (flushed > 0) { |
| 7763 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); |
| 7764 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; |
| 7765 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 7766 | } |
| 7767 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7768 | } |
| 7769 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 7770 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7771 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7772 | const sp<AudioFlinger::Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 7773 | const audio_attributes_t& attr, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7774 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7775 | audio_format_t format, |
| 7776 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 7777 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7778 | audio_session_t sessionId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7779 | size_t *pNotificationFrameCount, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 7780 | pid_t creatorPid, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7781 | const Identity& identity, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7782 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7783 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 7784 | status_t *status, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 7785 | audio_port_handle_t portId, |
| 7786 | int32_t maxSharedAudioHistoryMs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7787 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 7788 | size_t frameCount = *pFrameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7789 | size_t notificationFrameCount = *pNotificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7790 | sp<RecordTrack> track; |
| 7791 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7792 | audio_input_flags_t inputFlags = mInput->flags; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7793 | audio_input_flags_t requestedFlags = *flags; |
| 7794 | uint32_t sampleRate; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 7795 | Identity checkedIdentity = AudioFlinger::checkIdentityPackage(identity); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7796 | |
| 7797 | lStatus = initCheck(); |
| 7798 | if (lStatus != NO_ERROR) { |
| 7799 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 7800 | goto Exit; |
| 7801 | } |
| 7802 | |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 7803 | if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) { |
| 7804 | ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT"); |
| 7805 | lStatus = BAD_VALUE; |
| 7806 | goto Exit; |
| 7807 | } |
| 7808 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 7809 | if (maxSharedAudioHistoryMs != 0) { |
| 7810 | if (!captureHotwordAllowed(checkedIdentity)) { |
| 7811 | lStatus = PERMISSION_DENIED; |
| 7812 | goto Exit; |
| 7813 | } |
| 7814 | //TODO: b/185972521 allow resampling buffer resizing on fast mixers by pausing |
| 7815 | // the fast mixer thread while resizing the buffer in the normal thread |
| 7816 | if (hasFastCapture()) { |
| 7817 | lStatus = BAD_VALUE; |
| 7818 | goto Exit; |
| 7819 | } |
| 7820 | if (maxSharedAudioHistoryMs < 0 |
| 7821 | || maxSharedAudioHistoryMs > AudioFlinger::kMaxSharedAudioHistoryMs) { |
| 7822 | lStatus = BAD_VALUE; |
| 7823 | goto Exit; |
| 7824 | } |
| 7825 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7826 | if (*pSampleRate == 0) { |
| 7827 | *pSampleRate = mSampleRate; |
| 7828 | } |
| 7829 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7830 | |
| 7831 | // special case for FAST flag considered OK if fast capture is present |
| 7832 | if (hasFastCapture()) { |
| 7833 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 7834 | } |
| 7835 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7836 | // Check if requested flags are compatible with input stream flags |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7837 | if ((*flags & inputFlags) != *flags) { |
| 7838 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 7839 | " input flags (%08x)", |
| 7840 | *flags, inputFlags); |
| 7841 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 7842 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7843 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7844 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7845 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7846 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 7847 | // we formerly checked for a callback handler (non-0 tid), |
| 7848 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 7849 | // |
Phil Burk | 7ed66a1 | 2019-04-18 13:20:30 -0700 | [diff] [blame] | 7850 | // Frame count is not specified (0), or is less than or equal the pipe depth. |
| 7851 | // It is OK to provide a higher capacity than requested. |
| 7852 | // We will force it to mPipeFramesP2 below. |
| 7853 | (frameCount <= mPipeFramesP2) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 7854 | // PCM data |
| 7855 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 7856 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7857 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 7858 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7859 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 7860 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7861 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 7862 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7863 | hasFastCapture() && |
| 7864 | // there are sufficient fast track slots available |
| 7865 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7866 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7867 | // check compatibility with audio effects. |
| 7868 | Mutex::Autolock _l(mLock); |
| 7869 | // Do not accept FAST flag if the session has software effects |
| 7870 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 7871 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 7872 | audio_input_flags_t old = *flags; |
| 7873 | chain->checkInputFlagCompatibility(flags); |
| 7874 | if (old != *flags) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7875 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 7876 | this, (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7877 | } |
| 7878 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 7879 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7880 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 7881 | this, frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7882 | } else { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7883 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
| 7884 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 7885 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 7886 | this, frameCount, mFrameCount, mPipeFramesP2, |
| 7887 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7888 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7889 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7890 | } |
| 7891 | } |
| 7892 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7893 | // If FAST or RAW flags were corrected, ask caller to request new input from audio policy |
| 7894 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != |
| 7895 | (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { |
| 7896 | *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); |
| 7897 | lStatus = BAD_TYPE; |
| 7898 | goto Exit; |
| 7899 | } |
| 7900 | |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7901 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7902 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7903 | // fast track: frame count is exactly the pipe depth |
| 7904 | frameCount = mPipeFramesP2; |
| 7905 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7906 | notificationFrameCount = mFrameCount; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7907 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7908 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 7909 | // or 20 ms if there is a fast capture |
| 7910 | // TODO This could be a roundupRatio inline, and const |
| 7911 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 7912 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 7913 | // minimum number of notification periods is at least kMinNotifications, |
| 7914 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 7915 | static const size_t kMinNotifications = 3; |
| 7916 | static const uint32_t kMinMs = 30; |
| 7917 | // TODO This could be a roundupRatio inline |
| 7918 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 7919 | // TODO This could be a roundupRatio inline |
| 7920 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 7921 | maxNotificationFrames; |
| 7922 | const size_t minFrameCount = maxNotificationFrames * |
| 7923 | max(kMinNotifications, minNotificationsByMs); |
| 7924 | frameCount = max(frameCount, minFrameCount); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7925 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 7926 | notificationFrameCount = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 7927 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7928 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 7929 | *pFrameCount = frameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 7930 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7931 | |
| 7932 | { // scope for mLock |
| 7933 | Mutex::Autolock _l(mLock); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 7934 | int32_t startFrames = -1; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 7935 | if (!mSharedAudioPackageName.empty() |
| 7936 | && mSharedAudioPackageName == checkedIdentity.packageName |
| 7937 | && mSharedAudioSessionId == sessionId |
| 7938 | && captureHotwordAllowed(checkedIdentity)) { |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 7939 | startFrames = mSharedAudioStartFrames; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 7940 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7941 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 7942 | track = new RecordTrack(this, client, attr, sampleRate, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 7943 | format, channelMask, frameCount, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7944 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 7945 | checkedIdentity, *flags, TrackBase::TYPE_DEFAULT, portId, startFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7946 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 7947 | lStatus = track->initCheck(); |
| 7948 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 7949 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 7950 | // 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] | 7951 | goto Exit; |
| 7952 | } |
| 7953 | mTracks.add(track); |
| 7954 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 7955 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7956 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 7957 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 7958 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 7959 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 7960 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 7961 | |
| 7962 | if (maxSharedAudioHistoryMs != 0) { |
| 7963 | sendResizeBufferConfigEvent_l(maxSharedAudioHistoryMs); |
| 7964 | } |
| 7965 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7966 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 7967 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7968 | lStatus = NO_ERROR; |
| 7969 | |
| 7970 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 7971 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7972 | return track; |
| 7973 | } |
| 7974 | |
| 7975 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 7976 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7977 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7978 | { |
| 7979 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 7980 | sp<ThreadBase> strongMe = this; |
| 7981 | status_t status = NO_ERROR; |
| 7982 | |
| 7983 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7984 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7985 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7986 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7987 | triggerSession, |
| 7988 | recordTrack->sessionId(), |
| 7989 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7990 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7991 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 7992 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7993 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 7994 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7995 | } else { |
| 7996 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Ivan Lozano | 1b35dd6 | 2017-12-06 16:55:10 -0800 | [diff] [blame] | 7997 | recordTrack->mFramesToDrop = -(ssize_t) |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7998 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7999 | } |
| 8000 | } |
| 8001 | |
| 8002 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 8003 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8004 | AutoMutex lock(mLock); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8005 | if (recordTrack->isInvalid()) { |
| 8006 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 8007 | ALOGW("%s track %d: invalidated before startInput", __func__, recordTrack->portId()); |
| 8008 | return DEAD_OBJECT; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8009 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8010 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 8011 | if (recordTrack->mState == TrackBase::PAUSING) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8012 | // We haven't stopped yet (moved to PAUSED and not in mActiveTracks) |
| 8013 | // so no need to startInput(). |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8014 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8015 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8016 | } else { |
| 8017 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8018 | } |
| 8019 | return status; |
| 8020 | } |
| 8021 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8022 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 8023 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 8024 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8025 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8026 | mActiveTracks.add(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8027 | status_t status = NO_ERROR; |
| 8028 | if (recordTrack->isExternalTrack()) { |
| 8029 | mLock.unlock(); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 8030 | status = AudioSystem::startInput(recordTrack->portId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8031 | mLock.lock(); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8032 | if (recordTrack->isInvalid()) { |
| 8033 | recordTrack->clearSyncStartEvent(); |
| 8034 | if (status == NO_ERROR && recordTrack->mState == TrackBase::STARTING_1) { |
| 8035 | recordTrack->mState = TrackBase::STARTING_2; |
| 8036 | // STARTING_2 forces destroy to call stopInput. |
| 8037 | } |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 8038 | ALOGW("%s track %d: invalidated after startInput", __func__, recordTrack->portId()); |
| 8039 | return DEAD_OBJECT; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8040 | } |
| 8041 | if (recordTrack->mState != TrackBase::STARTING_1) { |
| 8042 | ALOGW("%s(%d): unsynchronized mState:%d change", |
| 8043 | __func__, recordTrack->id(), recordTrack->mState); |
| 8044 | // Someone else has changed state, let them take over, |
| 8045 | // leave mState in the new state. |
| 8046 | recordTrack->clearSyncStartEvent(); |
| 8047 | return INVALID_OPERATION; |
| 8048 | } |
| 8049 | // we're ok, but perhaps startInput has failed |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8050 | if (status != NO_ERROR) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8051 | ALOGW("%s(%d): startInput failed, status %d", |
| 8052 | __func__, recordTrack->id(), status); |
| 8053 | // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks, |
| 8054 | // leave in STARTING_1, so destroy() will not call stopInput. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8055 | mActiveTracks.remove(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8056 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8057 | return status; |
| 8058 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 8059 | sendIoConfigEvent_l( |
| 8060 | AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8061 | } |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 8062 | |
| 8063 | recordTrack->logBeginInterval(patchSourcesToString(&mPatch)); // log to MediaMetrics |
| 8064 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8065 | // Catch up with current buffer indices if thread is already running. |
| 8066 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 8067 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 8068 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 8069 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8070 | recordTrack->mResamplerBufferProvider->reset(); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8071 | if (!recordTrack->isDirect()) { |
| 8072 | // clear any converter state as new data will be discontinuous |
| 8073 | recordTrack->mRecordBufferConverter->reset(); |
| 8074 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8075 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8076 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8077 | mWaitWorkCV.broadcast(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8078 | return status; |
| 8079 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8080 | } |
| 8081 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8082 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 8083 | { |
| 8084 | sp<SyncEvent> strongEvent = event.promote(); |
| 8085 | |
| 8086 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 8087 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 8088 | if (ptr != 0) { |
| 8089 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 8090 | recordTrack->handleSyncStartEvent(strongEvent); |
| 8091 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8092 | } |
| 8093 | } |
| 8094 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 8095 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8096 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 8097 | AutoMutex _l(mLock); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8098 | // if we're invalid, we can't be on the ActiveTracks. |
Jean-Michel Trivi | 38f8671 | 2017-04-11 14:10:17 -0700 | [diff] [blame] | 8099 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8100 | return false; |
| 8101 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 8102 | // 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] | 8103 | recordTrack->mState = TrackBase::PAUSING; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8104 | |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 8105 | // NOTE: Waiting here is important to keep stop synchronous. |
| 8106 | // This is needed for proper patchRecord peer release. |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8107 | while (recordTrack->mState == TrackBase::PAUSING && !recordTrack->isInvalid()) { |
| 8108 | mWaitWorkCV.broadcast(); // signal thread to stop |
| 8109 | mStartStopCond.wait(mLock); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8110 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8111 | |
| 8112 | if (recordTrack->mState == TrackBase::PAUSED) { // successful stop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8113 | ALOGV("Record stopped OK"); |
| 8114 | return true; |
| 8115 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8116 | |
| 8117 | // don't handle anything - we've been invalidated or restarted and in a different state |
| 8118 | ALOGW_IF("%s(%d): unsynchronized stop, state: %d", |
| 8119 | __func__, recordTrack->id(), recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8120 | return false; |
| 8121 | } |
| 8122 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 8123 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8124 | { |
| 8125 | return false; |
| 8126 | } |
| 8127 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 8128 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8129 | { |
| 8130 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 8131 | if (!isValidSyncEvent(event)) { |
| 8132 | return BAD_VALUE; |
| 8133 | } |
| 8134 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 8135 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8136 | status_t ret = NAME_NOT_FOUND; |
| 8137 | |
| 8138 | Mutex::Autolock _l(mLock); |
| 8139 | |
| 8140 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 8141 | sp<RecordTrack> track = mTracks[i]; |
| 8142 | if (eventSession == track->sessionId()) { |
| 8143 | (void) track->setSyncEvent(event); |
| 8144 | ret = NO_ERROR; |
| 8145 | } |
| 8146 | } |
| 8147 | return ret; |
| 8148 | #else |
| 8149 | return BAD_VALUE; |
| 8150 | #endif |
| 8151 | } |
| 8152 | |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 8153 | status_t AudioFlinger::RecordThread::getActiveMicrophones( |
| 8154 | std::vector<media::MicrophoneInfo>* activeMicrophones) |
| 8155 | { |
| 8156 | ALOGV("RecordThread::getActiveMicrophones"); |
| 8157 | AutoMutex _l(mLock); |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 8158 | if (mInput == nullptr || mInput->stream == nullptr) { |
| 8159 | return NO_INIT; |
| 8160 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 8161 | status_t status = mInput->stream->getActiveMicrophones(activeMicrophones); |
| 8162 | return status; |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 8163 | } |
| 8164 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 8165 | status_t AudioFlinger::RecordThread::setPreferredMicrophoneDirection( |
| 8166 | audio_microphone_direction_t direction) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 8167 | { |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 8168 | ALOGV("setPreferredMicrophoneDirection(%d)", direction); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 8169 | AutoMutex _l(mLock); |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 8170 | if (mInput == nullptr || mInput->stream == nullptr) { |
| 8171 | return NO_INIT; |
| 8172 | } |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 8173 | return mInput->stream->setPreferredMicrophoneDirection(direction); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 8174 | } |
| 8175 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 8176 | status_t AudioFlinger::RecordThread::setPreferredMicrophoneFieldDimension(float zoom) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 8177 | { |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 8178 | ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 8179 | AutoMutex _l(mLock); |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 8180 | if (mInput == nullptr || mInput->stream == nullptr) { |
| 8181 | return NO_INIT; |
| 8182 | } |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 8183 | return mInput->stream->setPreferredMicrophoneFieldDimension(zoom); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 8184 | } |
| 8185 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8186 | status_t AudioFlinger::RecordThread::shareAudioHistory( |
| 8187 | const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, |
| 8188 | int64_t sharedAudioStartMs) { |
| 8189 | AutoMutex _l(mLock); |
| 8190 | return shareAudioHistory_l(sharedAudioPackageName, sharedSessionId, sharedAudioStartMs); |
| 8191 | } |
| 8192 | |
| 8193 | status_t AudioFlinger::RecordThread::shareAudioHistory_l( |
| 8194 | const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, |
| 8195 | int64_t sharedAudioStartMs) { |
| 8196 | if (hasFastCapture()) { |
| 8197 | return BAD_VALUE; |
| 8198 | } |
| 8199 | if ((hasAudioSession_l(sharedSessionId) & ThreadBase::TRACK_SESSION) == 0) { |
| 8200 | return BAD_VALUE; |
| 8201 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8202 | |
| 8203 | if (sharedAudioStartMs < 0 |
| 8204 | || sharedAudioStartMs > INT64_MAX / mSampleRate) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8205 | return BAD_VALUE; |
| 8206 | } |
| 8207 | |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8208 | // Current implementation of the input resampling buffer wraps around indexes at 32 bit. |
| 8209 | // As we cannot detect more than one wraparound, only accept values up current write position |
| 8210 | // after one wraparound |
| 8211 | // We assume recent wraparounds on mRsmpInRear only given it is unlikely that the requesting |
| 8212 | // app waits several hours after the start time was computed. |
| 8213 | const int64_t sharedAudioStartFrames = sharedAudioStartMs * mSampleRate / 1000; |
| 8214 | const int32_t sharedOffset = audio_utils::safe_sub_overflow(mRsmpInRear, |
| 8215 | (int32_t)sharedAudioStartFrames); |
| 8216 | if (sharedOffset < 0 |
| 8217 | || sharedOffset > mRsmpInFrames) { |
| 8218 | return BAD_VALUE; |
| 8219 | } |
| 8220 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8221 | mSharedAudioPackageName = sharedAudioPackageName; |
| 8222 | if (mSharedAudioPackageName.empty()) { |
| 8223 | mSharedAudioSessionId = AUDIO_SESSION_NONE; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8224 | mSharedAudioStartFrames = -1; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8225 | } else { |
| 8226 | mSharedAudioSessionId = sharedSessionId; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8227 | mSharedAudioStartFrames = (int32_t)sharedAudioStartFrames; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8228 | } |
| 8229 | return NO_ERROR; |
| 8230 | } |
| 8231 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8232 | void AudioFlinger::RecordThread::updateMetadata_l() |
| 8233 | { |
| 8234 | if (mInput == nullptr || mInput->stream == nullptr || |
| 8235 | !mActiveTracks.readAndClearHasChanged()) { |
| 8236 | return; |
| 8237 | } |
| 8238 | StreamInHalInterface::SinkMetadata metadata; |
| 8239 | for (const sp<RecordTrack> &track : mActiveTracks) { |
Eric Laurent | b9cc0ab | 2021-01-29 11:46:52 +0100 | [diff] [blame] | 8240 | // Do not forward PatchRecord metadata to audio HAL |
| 8241 | if (track->isPatchTrack()) { |
| 8242 | continue; |
| 8243 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8244 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 8245 | record_track_metadata_v7_t trackMetadata; |
| 8246 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8247 | .source = track->attributes().source, |
| 8248 | .gain = 1, // capture tracks do not have volumes |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 8249 | }; |
| 8250 | trackMetadata.channel_mask = track->channelMask(), |
| 8251 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 8252 | |
| 8253 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8254 | } |
| 8255 | mInput->stream->updateSinkMetadata(metadata); |
| 8256 | } |
| 8257 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8258 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 8259 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 8260 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 8261 | track->terminate(); |
| 8262 | track->mState = TrackBase::STOPPED; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8263 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8264 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8265 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8266 | removeTrack_l(track); |
| 8267 | } |
| 8268 | } |
| 8269 | |
| 8270 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 8271 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8272 | String8 result; |
| 8273 | track->appendDump(result, false /* active */); |
| 8274 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string()); |
| 8275 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8276 | mTracks.remove(track); |
| 8277 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8278 | if (track->isFastTrack()) { |
| 8279 | ALOG_ASSERT(!mFastTrackAvail); |
| 8280 | mFastTrackAvail = true; |
| 8281 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8282 | } |
| 8283 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 8284 | void AudioFlinger::RecordThread::dumpInternals_l(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8285 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 8286 | AudioStreamIn *input = mInput; |
| 8287 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; |
| 8288 | dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n", |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 8289 | input, flags, toString(flags).c_str()); |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 8290 | dprintf(fd, " Frames read: %lld\n", (long long)mFramesRead); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 8291 | if (mActiveTracks.isEmpty()) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 8292 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8293 | } |
Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 8294 | |
| 8295 | if (input != nullptr) { |
| 8296 | dprintf(fd, " Hal stream dump:\n"); |
| 8297 | (void)input->stream->dump(fd); |
| 8298 | } |
| 8299 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8300 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8301 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 8302 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 8303 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 8304 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 8305 | // This is a large object so we place it on the heap. |
| 8306 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
Eric Tan | 2942a4e | 2018-09-12 17:41:27 -0700 | [diff] [blame] | 8307 | const std::unique_ptr<FastCaptureDumpState> copy = |
| 8308 | std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState); |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 8309 | copy->dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8310 | } |
| 8311 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 8312 | void AudioFlinger::RecordThread::dumpTracks_l(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8313 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8314 | String8 result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8315 | size_t numtracks = mTracks.size(); |
| 8316 | size_t numactive = mActiveTracks.size(); |
| 8317 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 8318 | dprintf(fd, " %zu Tracks", numtracks); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8319 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8320 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 8321 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8322 | result.append(prefix); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 8323 | mTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8324 | for (size_t i = 0; i < numtracks ; ++i) { |
| 8325 | sp<RecordTrack> track = mTracks[i]; |
| 8326 | if (track != 0) { |
| 8327 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 8328 | if (active) { |
| 8329 | numactiveseen++; |
| 8330 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8331 | result.append(prefix); |
| 8332 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8333 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8334 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8335 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 8336 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8337 | } |
| 8338 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8339 | if (numactiveseen != numactive) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8340 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8341 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8342 | result.append(prefix); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 8343 | mActiveTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8344 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8345 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8346 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8347 | result.append(prefix); |
| 8348 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 8349 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8350 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8351 | |
| 8352 | } |
| 8353 | write(fd, result.string(), result.size()); |
| 8354 | } |
| 8355 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 8356 | void AudioFlinger::RecordThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8357 | { |
| 8358 | Mutex::Autolock _l(mLock); |
| 8359 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 8360 | sp<RecordTrack> track = mTracks[i]; |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 8361 | if (track != 0 && track->portId() == portId) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8362 | track->setSilenced(silenced); |
| 8363 | } |
| 8364 | } |
| 8365 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8366 | |
| 8367 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 8368 | { |
| 8369 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 8370 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8371 | mRsmpInUnrel = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8372 | const int32_t rear = recordThread->mRsmpInRear; |
| 8373 | ssize_t deltaFrames = 0; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8374 | if (mRecordTrack->startFrames() >= 0) { |
| 8375 | int32_t startFrames = mRecordTrack->startFrames(); |
| 8376 | // Accept a recent wraparound of mRsmpInRear |
| 8377 | if (startFrames <= rear) { |
| 8378 | deltaFrames = rear - startFrames; |
| 8379 | } else { |
| 8380 | deltaFrames = (int32_t)((int64_t)rear + UINT32_MAX + 1 - startFrames); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8381 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8382 | // start frame cannot be further in the past than start of resampling buffer |
| 8383 | if ((size_t) deltaFrames > recordThread->mRsmpInFrames) { |
| 8384 | deltaFrames = recordThread->mRsmpInFrames; |
| 8385 | } |
| 8386 | } |
| 8387 | mRsmpInFront = audio_utils::safe_sub_overflow(rear, static_cast<int32_t>(deltaFrames)); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8388 | } |
| 8389 | |
| 8390 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 8391 | size_t *framesAvailable, bool *hasOverrun) |
| 8392 | { |
| 8393 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 8394 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 8395 | const int32_t rear = recordThread->mRsmpInRear; |
| 8396 | const int32_t front = mRsmpInFront; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 8397 | const ssize_t filled = audio_utils::safe_sub_overflow(rear, front); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8398 | |
| 8399 | size_t framesIn; |
| 8400 | bool overrun = false; |
| 8401 | if (filled < 0) { |
| 8402 | // should not happen, but treat like a massive overrun and re-sync |
| 8403 | framesIn = 0; |
| 8404 | mRsmpInFront = rear; |
| 8405 | overrun = true; |
| 8406 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 8407 | framesIn = (size_t) filled; |
| 8408 | } else { |
| 8409 | // client is not keeping up with server, but give it latest data |
| 8410 | framesIn = recordThread->mRsmpInFrames; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 8411 | mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow( |
| 8412 | rear, static_cast<int32_t>(framesIn)); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8413 | overrun = true; |
| 8414 | } |
| 8415 | if (framesAvailable != NULL) { |
| 8416 | *framesAvailable = framesIn; |
| 8417 | } |
| 8418 | if (hasOverrun != NULL) { |
| 8419 | *hasOverrun = overrun; |
| 8420 | } |
| 8421 | } |
| 8422 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8423 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8424 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 8425 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8426 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8427 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8428 | if (threadBase == 0) { |
| 8429 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 8430 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8431 | return NOT_ENOUGH_DATA; |
| 8432 | } |
| 8433 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 8434 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8435 | int32_t front = mRsmpInFront; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 8436 | ssize_t filled = audio_utils::safe_sub_overflow(rear, front); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8437 | // FIXME should not be P2 (don't want to increase latency) |
| 8438 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 8439 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 8440 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8441 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 8442 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 8443 | if (part1 > (size_t) filled) { |
| 8444 | part1 = filled; |
| 8445 | } |
| 8446 | size_t ask = buffer->frameCount; |
| 8447 | ALOG_ASSERT(ask > 0); |
| 8448 | if (part1 > ask) { |
| 8449 | part1 = ask; |
| 8450 | } |
| 8451 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8452 | // 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] | 8453 | buffer->raw = NULL; |
| 8454 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8455 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 8456 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8457 | } |
| 8458 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8459 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 8460 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8461 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8462 | return NO_ERROR; |
| 8463 | } |
| 8464 | |
| 8465 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8466 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 8467 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8468 | { |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 8469 | int32_t stepCount = static_cast<int32_t>(buffer->frameCount); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 8470 | if (stepCount == 0) { |
| 8471 | return; |
| 8472 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8473 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 8474 | mRsmpInUnrel -= stepCount; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 8475 | mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 8476 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8477 | buffer->frameCount = 0; |
| 8478 | } |
| 8479 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 8480 | void AudioFlinger::RecordThread::checkBtNrec() |
| 8481 | { |
| 8482 | Mutex::Autolock _l(mLock); |
| 8483 | checkBtNrec_l(); |
| 8484 | } |
| 8485 | |
| 8486 | void AudioFlinger::RecordThread::checkBtNrec_l() |
| 8487 | { |
| 8488 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 8489 | // pre processings |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8490 | bool suspend = audio_is_bluetooth_sco_device(inDeviceType()) && |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 8491 | mAudioFlinger->btNrecIsOff(); |
| 8492 | if (mBtNrecSuspended.exchange(suspend) != suspend) { |
| 8493 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8494 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); |
| 8495 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); |
| 8496 | } |
| 8497 | } |
| 8498 | } |
| 8499 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 8500 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8501 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 8502 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8503 | { |
| 8504 | bool reconfig = false; |
| 8505 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8506 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8507 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8508 | audio_format_t reqFormat = mFormat; |
| 8509 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 8510 | // 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] | 8511 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 8512 | |
| 8513 | AudioParameter param = AudioParameter(keyValuePair); |
| 8514 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 8515 | |
| 8516 | // scope for AutoPark extends to end of method |
| 8517 | AutoPark<FastCapture> park(mFastCapture); |
| 8518 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8519 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 8520 | // channel count change can be requested. Do we mandate the first client defines the |
| 8521 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 8522 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 8523 | samplingRate = value; |
| 8524 | reconfig = true; |
| 8525 | } |
| 8526 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 8527 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8528 | status = BAD_VALUE; |
| 8529 | } else { |
| 8530 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8531 | reconfig = true; |
| 8532 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8533 | } |
| 8534 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 8535 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 8536 | if (!audio_is_input_channel(mask) || |
| 8537 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8538 | status = BAD_VALUE; |
| 8539 | } else { |
| 8540 | channelMask = mask; |
| 8541 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8542 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8543 | } |
| 8544 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 8545 | // do not accept frame count changes if tracks are open as the track buffer |
| 8546 | // size depends on frame count and correct behavior would not be guaranteed |
| 8547 | // if frame count is changed after track creation |
| 8548 | if (mActiveTracks.size() > 0) { |
| 8549 | status = INVALID_OPERATION; |
| 8550 | } else { |
| 8551 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8552 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8553 | } |
| 8554 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8555 | LOG_FATAL("Should not set routing device in RecordThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8556 | } |
| 8557 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 8558 | mAudioSource != (audio_source_t)value) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8559 | LOG_FATAL("Should not set audio source in RecordThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8560 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 8561 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8562 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8563 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8564 | if (status == INVALID_OPERATION) { |
| 8565 | inputStandBy(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8566 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8567 | } |
| 8568 | if (reconfig) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8569 | if (status == BAD_VALUE) { |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 8570 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 8571 | if (mInput->stream->getAudioProperties(&config) == OK && |
| 8572 | audio_is_linear_pcm(config.format) && audio_is_linear_pcm(reqFormat) && |
| 8573 | config.sample_rate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && |
| 8574 | audio_channel_count_from_in_mask(config.channel_mask) <= FCC_8) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8575 | status = NO_ERROR; |
| 8576 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8577 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8578 | if (status == NO_ERROR) { |
| 8579 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8580 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8581 | } |
| 8582 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8583 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8584 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8585 | return reconfig; |
| 8586 | } |
| 8587 | |
| 8588 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 8589 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8590 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8591 | if (initCheck() == NO_ERROR) { |
| 8592 | String8 out_s8; |
| 8593 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { |
| 8594 | return out_s8; |
| 8595 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8596 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8597 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8598 | } |
| 8599 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 8600 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid, |
| 8601 | audio_port_handle_t portId) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8602 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 8603 | |
| 8604 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8605 | |
| 8606 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8607 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 8608 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8609 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 8610 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8611 | desc->mChannelMask = mChannelMask; |
| 8612 | desc->mSamplingRate = mSampleRate; |
| 8613 | desc->mFormat = mFormat; |
| 8614 | desc->mFrameCount = mFrameCount; |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 8615 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8616 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8617 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 8618 | case AUDIO_CLIENT_STARTED: |
| 8619 | desc->mPatch = mPatch; |
| 8620 | desc->mPortId = portId; |
| 8621 | break; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 8622 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8623 | default: |
| 8624 | break; |
| 8625 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 8626 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8627 | } |
| 8628 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 8629 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8630 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8631 | status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 8632 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 8633 | mFormat = mHALFormat; |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 8634 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 8635 | if (audio_is_linear_pcm(mFormat)) { |
| 8636 | LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_8, "HAL channel count %d > %d", |
| 8637 | mChannelCount, FCC_8); |
| 8638 | } else { |
| 8639 | // Can have more that FCC_8 channels in encoded streams. |
| 8640 | ALOGI("HAL format %#x is not linear pcm", mFormat); |
| 8641 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8642 | result = mInput->stream->getFrameSize(&mFrameSize); |
| 8643 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 8644 | LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", |
| 8645 | mFrameSize); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8646 | result = mInput->stream->getBufferSize(&mBufferSize); |
| 8647 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 8648 | mFrameCount = mBufferSize / mFrameSize; |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 8649 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
| 8650 | "mBufferSize=%zu, mFrameCount=%zu", |
| 8651 | this, mChannelCount, mFormat, mFrameSize, mBufferSize, mFrameCount); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 8652 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8653 | // mRsmpInFrames must be 0 before calling resizeInputBuffer_l for the first time |
| 8654 | mRsmpInFrames = 0; |
| 8655 | resizeInputBuffer_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8656 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8657 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 8658 | // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 8659 | |
| 8660 | audio_input_flags_t flags = mInput->flags; |
| 8661 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); |
| 8662 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
| 8663 | .set(AMEDIAMETRICS_PROP_ENCODING, formatToString(mFormat).c_str()) |
| 8664 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 8665 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 8666 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 8667 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 8668 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 8669 | .record(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8670 | } |
| 8671 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 8672 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8673 | { |
| 8674 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8675 | uint32_t result; |
| 8676 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { |
| 8677 | return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8678 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8679 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8680 | } |
| 8681 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 8682 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8683 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 8684 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8685 | Mutex::Autolock _l(mLock); |
| 8686 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 8687 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 8688 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8689 | if (ids.indexOfKey(sessionId) < 0) { |
| 8690 | ids.add(sessionId, true); |
| 8691 | } |
| 8692 | } |
| 8693 | return ids; |
| 8694 | } |
| 8695 | |
| 8696 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 8697 | { |
| 8698 | Mutex::Autolock _l(mLock); |
| 8699 | AudioStreamIn *input = mInput; |
| 8700 | mInput = NULL; |
| 8701 | return input; |
| 8702 | } |
| 8703 | |
| 8704 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8705 | sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8706 | { |
| 8707 | if (mInput == NULL) { |
| 8708 | return NULL; |
| 8709 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8710 | return mInput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8711 | } |
| 8712 | |
| 8713 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 8714 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8715 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 8716 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8717 | chain->setInBuffer(NULL); |
| 8718 | chain->setOutBuffer(NULL); |
| 8719 | |
| 8720 | checkSuspendOnAddEffectChain_l(chain); |
| 8721 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 8722 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 8723 | // just moved them to a new input stream. |
| 8724 | chain->syncHalEffectsState(); |
| 8725 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8726 | mEffectChains.add(chain); |
| 8727 | |
| 8728 | return NO_ERROR; |
| 8729 | } |
| 8730 | |
| 8731 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 8732 | { |
| 8733 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 8734 | |
| 8735 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 8736 | if (chain == mEffectChains[i]) { |
| 8737 | mEffectChains.removeAt(i); |
| 8738 | break; |
| 8739 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8740 | } |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 8741 | return mEffectChains.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8742 | } |
| 8743 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8744 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 8745 | audio_patch_handle_t *handle) |
| 8746 | { |
| 8747 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8748 | |
| 8749 | // store new device and send to effects |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8750 | mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type; |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 8751 | mInDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 8752 | audio_port_handle_t deviceId = patch->sources[0].id; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8753 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 8754 | mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8755 | } |
| 8756 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 8757 | checkBtNrec_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8758 | |
| 8759 | // store new source and send to effects |
| 8760 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 8761 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8762 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8763 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8764 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8765 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8766 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 8767 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 8768 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 8769 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 8770 | patch->sources, |
| 8771 | patch->num_sinks, |
| 8772 | patch->sinks, |
| 8773 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8774 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8775 | char *address; |
| 8776 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 8777 | address = audio_device_address_to_parameter( |
| 8778 | patch->sources[0].ext.device.type, |
| 8779 | patch->sources[0].ext.device.address); |
| 8780 | } else { |
| 8781 | address = (char *)calloc(1, 1); |
| 8782 | } |
| 8783 | AudioParameter param = AudioParameter(String8(address)); |
| 8784 | free(address); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 8785 | param.addInt(String8(AudioParameter::keyRouting), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8786 | (int)patch->sources[0].ext.device.type); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 8787 | param.addInt(String8(AudioParameter::keyInputSource), |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8788 | (int)patch->sinks[0].ext.mix.usecase.source); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8789 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8790 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8791 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8792 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8793 | if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 8794 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8795 | mPatch = *patch; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 8796 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 8797 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 8798 | const std::string pathSourcesAsString = patchSourcesToString(patch); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8799 | mThreadMetrics.logEndInterval(); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 8800 | mThreadMetrics.logCreatePatch(pathSourcesAsString, /* outDevices */ {}); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8801 | mThreadMetrics.logBeginInterval(); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 8802 | // also dispatch to active AudioRecords |
| 8803 | for (const auto &track : mActiveTracks) { |
| 8804 | track->logEndInterval(); |
| 8805 | track->logBeginInterval(pathSourcesAsString); |
| 8806 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8807 | return status; |
| 8808 | } |
| 8809 | |
| 8810 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 8811 | { |
| 8812 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8813 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8814 | mPatch = audio_patch{}; |
| 8815 | mInDeviceTypeAddr.reset(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8816 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 8817 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 8818 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 8819 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8820 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 8821 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 8822 | param.addInt(String8(AudioParameter::keyRouting), 0); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8823 | status = mInput->stream->setParameters(param.toString()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8824 | } |
| 8825 | return status; |
| 8826 | } |
| 8827 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8828 | void AudioFlinger::RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices) |
| 8829 | { |
wendy lin | 56aa82b | 2020-12-02 15:19:55 +0800 | [diff] [blame] | 8830 | Mutex::Autolock _l(mLock); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8831 | mOutDevices = outDevices; |
| 8832 | mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices); |
| 8833 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 8834 | mEffectChains[i]->setDevices_l(outDeviceTypeAddrs()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 8835 | } |
| 8836 | } |
| 8837 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8838 | int32_t AudioFlinger::RecordThread::getOldestFront_l() |
| 8839 | { |
| 8840 | if (mTracks.size() == 0) { |
| 8841 | return 0; |
| 8842 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8843 | int32_t oldestFront = mRsmpInRear; |
| 8844 | int32_t maxFilled = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8845 | for (size_t i = 0; i < mTracks.size(); i++) { |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8846 | int32_t front = mTracks[i]->mResamplerBufferProvider->getFront(); |
| 8847 | int32_t filled; |
| 8848 | if (front <= mRsmpInRear) { |
| 8849 | filled = mRsmpInRear - front; |
| 8850 | } else { |
| 8851 | filled = (int32_t)((int64_t)mRsmpInRear + UINT32_MAX + 1 - front); |
| 8852 | } |
| 8853 | if (filled > maxFilled) { |
| 8854 | oldestFront = front; |
| 8855 | maxFilled = filled; |
| 8856 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8857 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 8858 | return oldestFront; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8859 | } |
| 8860 | |
| 8861 | void AudioFlinger::RecordThread::updateFronts_l(int32_t offset) |
| 8862 | { |
| 8863 | if (offset == 0) { |
| 8864 | return; |
| 8865 | } |
| 8866 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 8867 | int32_t front = mTracks[i]->mResamplerBufferProvider->getFront(); |
| 8868 | front = audio_utils::safe_sub_overflow(front, offset); |
| 8869 | mTracks[i]->mResamplerBufferProvider->setFront(front); |
| 8870 | } |
| 8871 | } |
| 8872 | |
| 8873 | void AudioFlinger::RecordThread::resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) |
| 8874 | { |
| 8875 | // This is the formula for calculating the temporary buffer size. |
| 8876 | // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to |
| 8877 | // 1 full output buffer, regardless of the alignment of the available input. |
| 8878 | // The value is somewhat arbitrary, and could probably be even larger. |
| 8879 | // A larger value should allow more old data to be read after a track calls start(), |
| 8880 | // without increasing latency. |
| 8881 | // |
| 8882 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
| 8883 | size_t minRsmpInFrames = mFrameCount * 7; |
| 8884 | |
| 8885 | // maxSharedAudioHistoryMs != 0 indicates a request to possibly make some part of the audio |
| 8886 | // capture history available to another client using the same session ID: |
| 8887 | // dimension the resampler input buffer accordingly. |
| 8888 | |
| 8889 | // Get oldest client read position: getOldestFront_l() must be called before altering |
| 8890 | // mRsmpInRear, or mRsmpInFrames |
| 8891 | int32_t previousFront = getOldestFront_l(); |
| 8892 | size_t previousRsmpInFramesP2 = mRsmpInFramesP2; |
| 8893 | int32_t previousRear = mRsmpInRear; |
| 8894 | mRsmpInRear = 0; |
| 8895 | |
| 8896 | if (maxSharedAudioHistoryMs != 0) { |
| 8897 | // resizeInputBuffer_l should never be called with a non zero shared history if the |
| 8898 | // buffer was not already allocated |
| 8899 | ALOG_ASSERT(mRsmpInBuffer != nullptr && mRsmpInFrames != 0, |
| 8900 | "resizeInputBuffer_l() called with shared history and unallocated buffer"); |
| 8901 | size_t rsmpInFrames = (size_t)maxSharedAudioHistoryMs * mSampleRate / 1000; |
| 8902 | // never reduce resampler input buffer size |
| 8903 | if (rsmpInFrames < mRsmpInFrames) { |
| 8904 | return; |
| 8905 | } |
| 8906 | mRsmpInFrames = rsmpInFrames; |
| 8907 | } |
| 8908 | // Note: mRsmpInFrames is 0 when called with maxSharedAudioHistoryMs equals to 0 so it is always |
| 8909 | // initialized |
| 8910 | if (mRsmpInFrames < minRsmpInFrames) { |
| 8911 | mRsmpInFrames = minRsmpInFrames; |
| 8912 | } |
| 8913 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
| 8914 | |
| 8915 | // TODO optimize audio capture buffer sizes ... |
| 8916 | // Here we calculate the size of the sliding buffer used as a source |
| 8917 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 8918 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 8919 | // be better to have it derived from the pipe depth in the long term. |
| 8920 | // The current value is higher than necessary. However it should not add to latency. |
| 8921 | |
| 8922 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
| 8923 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; |
| 8924 | |
| 8925 | void *rsmpInBuffer; |
| 8926 | (void)posix_memalign(&rsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); |
| 8927 | // if posix_memalign fails, will segv here. |
| 8928 | memset(rsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); |
| 8929 | |
| 8930 | // Copy audio history if any from old buffer before freeing it |
| 8931 | if (previousRear != 0) { |
| 8932 | ALOG_ASSERT(mRsmpInBuffer != nullptr, |
| 8933 | "resizeInputBuffer_l() called with null buffer but frames already read from HAL"); |
| 8934 | |
| 8935 | ssize_t unread = audio_utils::safe_sub_overflow(previousRear, previousFront); |
| 8936 | previousFront &= previousRsmpInFramesP2 - 1; |
| 8937 | size_t part1 = previousRsmpInFramesP2 - previousFront; |
| 8938 | if (part1 > (size_t) unread) { |
| 8939 | part1 = unread; |
| 8940 | } |
| 8941 | if (part1 != 0) { |
| 8942 | memcpy(rsmpInBuffer, (const uint8_t*)mRsmpInBuffer + previousFront * mFrameSize, |
| 8943 | part1 * mFrameSize); |
| 8944 | mRsmpInRear = part1; |
| 8945 | part1 = unread - part1; |
| 8946 | if (part1 != 0) { |
| 8947 | memcpy((uint8_t*)rsmpInBuffer + mRsmpInRear * mFrameSize, |
| 8948 | (const uint8_t*)mRsmpInBuffer, part1 * mFrameSize); |
| 8949 | mRsmpInRear += part1; |
| 8950 | } |
| 8951 | } |
| 8952 | // Update front for all clients according to new rear |
| 8953 | updateFronts_l(audio_utils::safe_sub_overflow(previousRear, mRsmpInRear)); |
| 8954 | } else { |
| 8955 | mRsmpInRear = 0; |
| 8956 | } |
| 8957 | free(mRsmpInBuffer); |
| 8958 | mRsmpInBuffer = rsmpInBuffer; |
| 8959 | } |
| 8960 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 8961 | void AudioFlinger::RecordThread::addPatchTrack(const sp<PatchRecord>& record) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8962 | { |
| 8963 | Mutex::Autolock _l(mLock); |
| 8964 | mTracks.add(record); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8965 | if (record->getSource()) { |
| 8966 | mSource = record->getSource(); |
| 8967 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8968 | } |
| 8969 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 8970 | void AudioFlinger::RecordThread::deletePatchTrack(const sp<PatchRecord>& record) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8971 | { |
| 8972 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8973 | if (mSource == record->getSource()) { |
| 8974 | mSource = mInput; |
| 8975 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8976 | destroyTrack_l(record); |
| 8977 | } |
| 8978 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8979 | void AudioFlinger::RecordThread::toAudioPortConfig(struct audio_port_config *config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8980 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8981 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8982 | config->role = AUDIO_PORT_ROLE_SINK; |
| 8983 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 8984 | config->ext.mix.usecase.source = mAudioSource; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 8985 | if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { |
| 8986 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 8987 | config->flags.input = mInput->flags; |
| 8988 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 8989 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 8990 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8991 | // ---------------------------------------------------------------------------- |
| 8992 | // Mmap |
| 8993 | // ---------------------------------------------------------------------------- |
| 8994 | |
| 8995 | AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread) |
| 8996 | : mThread(thread) |
| 8997 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 8998 | assert(thread != 0); // thread must start non-null and stay non-null |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 8999 | } |
| 9000 | |
| 9001 | AudioFlinger::MmapThreadHandle::~MmapThreadHandle() |
| 9002 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 9003 | mThread->disconnect(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9004 | } |
| 9005 | |
| 9006 | status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, |
| 9007 | struct audio_mmap_buffer_info *info) |
| 9008 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9009 | return mThread->createMmapBuffer(minSizeFrames, info); |
| 9010 | } |
| 9011 | |
| 9012 | status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position) |
| 9013 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9014 | return mThread->getMmapPosition(position); |
| 9015 | } |
| 9016 | |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 9017 | status_t AudioFlinger::MmapThreadHandle::getExternalPosition(uint64_t *position, |
| 9018 | int64_t *timeNanos) { |
| 9019 | return mThread->getExternalPosition(position, timeNanos); |
| 9020 | } |
| 9021 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9022 | status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 9023 | const audio_attributes_t *attr, audio_port_handle_t *handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9024 | |
| 9025 | { |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 9026 | return mThread->start(client, attr, handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9027 | } |
| 9028 | |
| 9029 | status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle) |
| 9030 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9031 | return mThread->stop(handle); |
| 9032 | } |
| 9033 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9034 | status_t AudioFlinger::MmapThreadHandle::standby() |
| 9035 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9036 | return mThread->standby(); |
| 9037 | } |
| 9038 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9039 | |
| 9040 | AudioFlinger::MmapThread::MmapThread( |
| 9041 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9042 | AudioHwDevice *hwDev, sp<StreamHalInterface> stream, bool systemReady, bool isOut) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9043 | : ThreadBase(audioFlinger, id, (isOut ? MMAP_PLAYBACK : MMAP_CAPTURE), systemReady, isOut), |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9044 | mSessionId(AUDIO_SESSION_NONE), |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 9045 | mPortId(AUDIO_PORT_HANDLE_NONE), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9046 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), |
Eric Laurent | 67f9729 | 2018-04-20 18:05:41 -0700 | [diff] [blame] | 9047 | mActiveTracks(&this->mLocalLog), |
| 9048 | mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later. |
| 9049 | mNoCallbackWarningCount(0) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9050 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9051 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9052 | readHalParameters_l(); |
| 9053 | } |
| 9054 | |
| 9055 | AudioFlinger::MmapThread::~MmapThread() |
| 9056 | { |
| 9057 | } |
| 9058 | |
| 9059 | void AudioFlinger::MmapThread::onFirstRef() |
| 9060 | { |
| 9061 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 9062 | } |
| 9063 | |
| 9064 | void AudioFlinger::MmapThread::disconnect() |
| 9065 | { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9066 | ActiveTracks<MmapTrack> activeTracks; |
| 9067 | { |
| 9068 | Mutex::Autolock _l(mLock); |
| 9069 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 9070 | activeTracks.add(t); |
| 9071 | } |
| 9072 | } |
| 9073 | for (const sp<MmapTrack> &t : activeTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9074 | stop(t->portId()); |
| 9075 | } |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 9076 | // This will decrement references and may cause the destruction of this thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9077 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 9078 | AudioSystem::releaseOutput(mPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9079 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 9080 | AudioSystem::releaseInput(mPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9081 | } |
| 9082 | } |
| 9083 | |
| 9084 | |
| 9085 | void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr, |
| 9086 | audio_stream_type_t streamType __unused, |
| 9087 | audio_session_t sessionId, |
| 9088 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9089 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9090 | audio_port_handle_t portId) |
| 9091 | { |
| 9092 | mAttr = *attr; |
| 9093 | mSessionId = sessionId; |
| 9094 | mCallback = callback; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9095 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9096 | mPortId = portId; |
| 9097 | } |
| 9098 | |
| 9099 | status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames, |
| 9100 | struct audio_mmap_buffer_info *info) |
| 9101 | { |
| 9102 | if (mHalStream == 0) { |
| 9103 | return NO_INIT; |
| 9104 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9105 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9106 | return mHalStream->createMmapBuffer(minSizeFrames, info); |
| 9107 | } |
| 9108 | |
| 9109 | status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position) |
| 9110 | { |
| 9111 | if (mHalStream == 0) { |
| 9112 | return NO_INIT; |
| 9113 | } |
| 9114 | return mHalStream->getMmapPosition(position); |
| 9115 | } |
| 9116 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9117 | status_t AudioFlinger::MmapThread::exitStandby() |
| 9118 | { |
| 9119 | status_t ret = mHalStream->start(); |
| 9120 | if (ret != NO_ERROR) { |
| 9121 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); |
| 9122 | return ret; |
| 9123 | } |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9124 | if (mStandby) { |
| 9125 | mThreadMetrics.logBeginInterval(); |
| 9126 | mStandby = false; |
| 9127 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9128 | return NO_ERROR; |
| 9129 | } |
| 9130 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9131 | status_t AudioFlinger::MmapThread::start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 9132 | const audio_attributes_t *attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9133 | audio_port_handle_t *handle) |
| 9134 | { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9135 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9136 | client.identity.uid, mStandby, mPortId, *handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9137 | if (mHalStream == 0) { |
| 9138 | return NO_INIT; |
| 9139 | } |
| 9140 | |
| 9141 | status_t ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9142 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9143 | if (*handle == mPortId) { |
Phil Burk | 98ab408 | 2020-09-25 21:46:34 +0000 | [diff] [blame] | 9144 | // For the first track, reuse portId and session allocated when the stream was opened. |
| 9145 | ret = exitStandby(); |
| 9146 | if (ret == NO_ERROR) { |
| 9147 | acquireWakeLock(); |
| 9148 | } |
| 9149 | return ret; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9150 | } |
| 9151 | |
| 9152 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 9153 | |
| 9154 | audio_io_handle_t io = mId; |
| 9155 | if (isOutput()) { |
| 9156 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 9157 | config.sample_rate = mSampleRate; |
| 9158 | config.channel_mask = mChannelMask; |
| 9159 | config.format = mFormat; |
| 9160 | audio_stream_type_t stream = streamType(); |
| 9161 | audio_output_flags_t flags = |
| 9162 | (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9163 | audio_port_handle_t deviceId = mDeviceId; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 9164 | std::vector<audio_io_handle_t> secondaryOutputs; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9165 | ret = AudioSystem::getOutputForAttr(&mAttr, &io, |
| 9166 | mSessionId, |
| 9167 | &stream, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9168 | client.identity, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9169 | &config, |
| 9170 | flags, |
| 9171 | &deviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 9172 | &portId, |
| 9173 | &secondaryOutputs); |
| 9174 | ALOGD_IF(!secondaryOutputs.empty(), |
| 9175 | "MmapThread::start does not support secondary outputs, ignoring them"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9176 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9177 | audio_config_base_t config; |
| 9178 | config.sample_rate = mSampleRate; |
| 9179 | config.channel_mask = mChannelMask; |
| 9180 | config.format = mFormat; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9181 | audio_port_handle_t deviceId = mDeviceId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9182 | ret = AudioSystem::getInputForAttr(&mAttr, &io, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 9183 | RECORD_RIID_INVALID, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9184 | mSessionId, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9185 | client.identity, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9186 | &config, |
| 9187 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, |
| 9188 | &deviceId, |
| 9189 | &portId); |
| 9190 | } |
| 9191 | // APM should not chose a different input or output stream for the same set of attributes |
| 9192 | // and audo configuration |
| 9193 | if (ret != NO_ERROR || io != mId) { |
| 9194 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", |
| 9195 | __FUNCTION__, ret, io, mId); |
| 9196 | return BAD_VALUE; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9197 | } |
| 9198 | |
| 9199 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 9200 | ret = AudioSystem::startOutput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9201 | } else { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 9202 | ret = AudioSystem::startInput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9203 | } |
| 9204 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9205 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9206 | // abort if start is rejected by audio policy manager |
| 9207 | if (ret != NO_ERROR) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 9208 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 9209 | if (!mActiveTracks.isEmpty()) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9210 | mLock.unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9211 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 9212 | AudioSystem::releaseOutput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9213 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 9214 | AudioSystem::releaseInput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9215 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9216 | mLock.lock(); |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9217 | } else { |
| 9218 | mHalStream->stop(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9219 | } |
| 9220 | return PERMISSION_DENIED; |
| 9221 | } |
| 9222 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 9223 | // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ? |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 9224 | sp<MmapTrack> track = new MmapTrack(this, attr == nullptr ? mAttr : *attr, mSampleRate, mFormat, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9225 | mChannelMask, mSessionId, isOutput(), client.identity, |
| 9226 | IPCThreadState::self()->getCallingPid(), portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9227 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 9228 | if (isOutput()) { |
| 9229 | // force volume update when a new track is added |
| 9230 | mHalVolFloat = -1.0f; |
| 9231 | } else if (!track->isSilenced_l()) { |
| 9232 | for (const sp<MmapTrack> &t : mActiveTracks) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9233 | if (t->isSilenced_l() && t->uid() != client.identity.uid) |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 9234 | t->invalidate(); |
| 9235 | } |
| 9236 | } |
| 9237 | |
| 9238 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9239 | mActiveTracks.add(track); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9240 | sp<EffectChain> chain = getEffectChain_l(mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9241 | if (chain != 0) { |
| 9242 | chain->setStrategy(AudioSystem::getStrategyForStream(streamType())); |
| 9243 | chain->incTrackCnt(); |
| 9244 | chain->incActiveTrackCnt(); |
| 9245 | } |
| 9246 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9247 | track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9248 | *handle = portId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9249 | broadcast_l(); |
| 9250 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9251 | ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9252 | |
| 9253 | return NO_ERROR; |
| 9254 | } |
| 9255 | |
| 9256 | status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle) |
| 9257 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9258 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 9259 | |
| 9260 | if (mHalStream == 0) { |
| 9261 | return NO_INIT; |
| 9262 | } |
| 9263 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9264 | if (handle == mPortId) { |
| 9265 | mHalStream->stop(); |
Phil Burk | 98ab408 | 2020-09-25 21:46:34 +0000 | [diff] [blame] | 9266 | releaseWakeLock(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 9267 | return NO_ERROR; |
| 9268 | } |
| 9269 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9270 | Mutex::Autolock _l(mLock); |
| 9271 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9272 | sp<MmapTrack> track; |
| 9273 | for (const sp<MmapTrack> &t : mActiveTracks) { |
| 9274 | if (handle == t->portId()) { |
| 9275 | track = t; |
| 9276 | break; |
| 9277 | } |
| 9278 | } |
| 9279 | if (track == 0) { |
| 9280 | return BAD_VALUE; |
| 9281 | } |
| 9282 | |
| 9283 | mActiveTracks.remove(track); |
| 9284 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9285 | mLock.unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9286 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 9287 | AudioSystem::stopOutput(track->portId()); |
| 9288 | AudioSystem::releaseOutput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9289 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 9290 | AudioSystem::stopInput(track->portId()); |
| 9291 | AudioSystem::releaseInput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9292 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9293 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9294 | |
| 9295 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 9296 | if (chain != 0) { |
| 9297 | chain->decActiveTrackCnt(); |
| 9298 | chain->decTrackCnt(); |
| 9299 | } |
| 9300 | |
| 9301 | broadcast_l(); |
| 9302 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9303 | return NO_ERROR; |
| 9304 | } |
| 9305 | |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9306 | status_t AudioFlinger::MmapThread::standby() |
| 9307 | { |
| 9308 | ALOGV("%s", __FUNCTION__); |
| 9309 | |
| 9310 | if (mHalStream == 0) { |
| 9311 | return NO_INIT; |
| 9312 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 9313 | if (!mActiveTracks.isEmpty()) { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9314 | return INVALID_OPERATION; |
| 9315 | } |
| 9316 | mHalStream->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9317 | if (!mStandby) { |
| 9318 | mThreadMetrics.logEndInterval(); |
| 9319 | mStandby = true; |
| 9320 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 9321 | releaseWakeLock(); |
| 9322 | return NO_ERROR; |
| 9323 | } |
| 9324 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9325 | |
| 9326 | void AudioFlinger::MmapThread::readHalParameters_l() |
| 9327 | { |
| 9328 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 9329 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
| 9330 | mFormat = mHALFormat; |
| 9331 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 9332 | result = mHalStream->getFrameSize(&mFrameSize); |
| 9333 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result); |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9334 | LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", |
| 9335 | mFrameSize); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9336 | result = mHalStream->getBufferSize(&mBufferSize); |
| 9337 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
| 9338 | mFrameCount = mBufferSize / mFrameSize; |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9339 | |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9340 | // TODO: make a readHalParameters call? |
| 9341 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9342 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
| 9343 | .set(AMEDIAMETRICS_PROP_ENCODING, formatToString(mFormat).c_str()) |
| 9344 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 9345 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 9346 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 9347 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 9348 | /* |
| 9349 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 9350 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, |
| 9351 | (int32_t)mHapticChannelMask) |
| 9352 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, |
| 9353 | (int32_t)mHapticChannelCount) |
| 9354 | */ |
| 9355 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING, |
| 9356 | formatToString(mHALFormat).c_str()) |
| 9357 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT, |
| 9358 | (int32_t)mFrameCount) // sic - added HAL |
| 9359 | .record(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9360 | } |
| 9361 | |
| 9362 | bool AudioFlinger::MmapThread::threadLoop() |
| 9363 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9364 | checkSilentMode_l(); |
| 9365 | |
| 9366 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 9367 | |
| 9368 | while (!exitPending()) |
| 9369 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9370 | Vector< sp<EffectChain> > effectChains; |
| 9371 | |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 9372 | { // under Thread lock |
| 9373 | Mutex::Autolock _l(mLock); |
| 9374 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9375 | if (mSignalPending) { |
| 9376 | // A signal was raised while we were unlocked |
| 9377 | mSignalPending = false; |
| 9378 | } else { |
| 9379 | if (mConfigEvents.isEmpty()) { |
| 9380 | // we're about to wait, flush the binder command buffer |
| 9381 | IPCThreadState::self()->flushCommands(); |
| 9382 | |
| 9383 | if (exitPending()) { |
| 9384 | break; |
| 9385 | } |
| 9386 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9387 | // wait until we have something to do... |
| 9388 | ALOGV("%s going to sleep", myName.string()); |
| 9389 | mWaitWorkCV.wait(mLock); |
| 9390 | ALOGV("%s waking up", myName.string()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9391 | |
| 9392 | checkSilentMode_l(); |
| 9393 | |
| 9394 | continue; |
| 9395 | } |
| 9396 | } |
| 9397 | |
| 9398 | processConfigEvents_l(); |
| 9399 | |
| 9400 | processVolume_l(); |
| 9401 | |
| 9402 | checkInvalidTracks_l(); |
| 9403 | |
| 9404 | mActiveTracks.updatePowerState(this); |
| 9405 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9406 | updateMetadata_l(); |
| 9407 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9408 | lockEffectChains_l(effectChains); |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 9409 | } // release Thread lock |
| 9410 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9411 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 9412 | effectChains[i]->process_l(); // Thread is not locked, but effect chain is locked |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9413 | } |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 9414 | |
| 9415 | // enable changes in effect chain, including moving to another thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9416 | unlockEffectChains(effectChains); |
| 9417 | // Effect chains will be actually deleted here if they were removed from |
| 9418 | // mEffectChains list during mixing or effects processing |
| 9419 | } |
| 9420 | |
| 9421 | threadLoop_exit(); |
| 9422 | |
| 9423 | if (!mStandby) { |
| 9424 | threadLoop_standby(); |
| 9425 | mStandby = true; |
| 9426 | } |
| 9427 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9428 | ALOGV("Thread %p type %d exiting", this, mType); |
| 9429 | return false; |
| 9430 | } |
| 9431 | |
| 9432 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 9433 | bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair, |
| 9434 | status_t& status) |
| 9435 | { |
| 9436 | AudioParameter param = AudioParameter(keyValuePair); |
| 9437 | int value; |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 9438 | bool sendToHal = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9439 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9440 | LOG_FATAL("Should not happen set routing device in MmapThread"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9441 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 9442 | if (sendToHal) { |
| 9443 | status = mHalStream->setParameters(keyValuePair); |
| 9444 | } else { |
| 9445 | status = NO_ERROR; |
| 9446 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9447 | |
| 9448 | return false; |
| 9449 | } |
| 9450 | |
| 9451 | String8 AudioFlinger::MmapThread::getParameters(const String8& keys) |
| 9452 | { |
| 9453 | Mutex::Autolock _l(mLock); |
| 9454 | String8 out_s8; |
| 9455 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { |
| 9456 | return out_s8; |
| 9457 | } |
| 9458 | return String8(); |
| 9459 | } |
| 9460 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9461 | void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid, |
| 9462 | audio_port_handle_t portId __unused) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9463 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 9464 | |
| 9465 | desc->mIoHandle = mId; |
| 9466 | |
| 9467 | switch (event) { |
| 9468 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 9469 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9470 | case AUDIO_INPUT_CONFIG_CHANGED: |
| 9471 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 9472 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9473 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 9474 | desc->mPatch = mPatch; |
| 9475 | desc->mChannelMask = mChannelMask; |
| 9476 | desc->mSamplingRate = mSampleRate; |
| 9477 | desc->mFormat = mFormat; |
| 9478 | desc->mFrameCount = mFrameCount; |
| 9479 | desc->mFrameCountHAL = mFrameCount; |
| 9480 | desc->mLatency = 0; |
| 9481 | break; |
| 9482 | |
| 9483 | case AUDIO_INPUT_CLOSED: |
| 9484 | case AUDIO_OUTPUT_CLOSED: |
| 9485 | default: |
| 9486 | break; |
| 9487 | } |
| 9488 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
| 9489 | } |
| 9490 | |
| 9491 | status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch, |
| 9492 | audio_patch_handle_t *handle) |
| 9493 | { |
| 9494 | status_t status = NO_ERROR; |
| 9495 | |
| 9496 | // store new device and send to effects |
| 9497 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 9498 | audio_port_handle_t deviceId; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9499 | AudioDeviceTypeAddrVector sinkDeviceTypeAddrs; |
| 9500 | AudioDeviceTypeAddr sourceDeviceTypeAddr; |
| 9501 | uint32_t numDevices = 0; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9502 | if (isOutput()) { |
| 9503 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9504 | LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 |
| 9505 | && !mAudioHwDev->supportsAudioPatches(), |
| 9506 | "Enumerated device type(%#x) must not be used " |
| 9507 | "as it does not support audio patches", |
| 9508 | patch->sinks[i].ext.device.type); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 9509 | type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9510 | sinkDeviceTypeAddrs.push_back(AudioDeviceTypeAddr(patch->sinks[i].ext.device.type, |
| 9511 | patch->sinks[i].ext.device.address)); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9512 | } |
| 9513 | deviceId = patch->sinks[0].id; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9514 | numDevices = mPatch.num_sinks; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9515 | } else { |
| 9516 | type = patch->sources[0].ext.device.type; |
| 9517 | deviceId = patch->sources[0].id; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9518 | numDevices = mPatch.num_sources; |
| 9519 | sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type; |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 9520 | sourceDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9521 | } |
| 9522 | |
| 9523 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 9524 | if (isOutput()) { |
| 9525 | mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs); |
| 9526 | } else { |
| 9527 | mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr); |
| 9528 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9529 | } |
| 9530 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9531 | if (!isOutput()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9532 | // store new source and send to effects |
| 9533 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 9534 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 9535 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 9536 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 9537 | } |
| 9538 | } |
| 9539 | } |
| 9540 | |
| 9541 | if (mAudioHwDev->supportsAudioPatches()) { |
| 9542 | status = mHalDevice->createAudioPatch(patch->num_sources, |
| 9543 | patch->sources, |
| 9544 | patch->num_sinks, |
| 9545 | patch->sinks, |
| 9546 | handle); |
| 9547 | } else { |
| 9548 | char *address; |
| 9549 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 9550 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 9551 | address = audio_device_address_to_parameter( |
| 9552 | patch->sinks[0].ext.device.type, |
| 9553 | patch->sinks[0].ext.device.address); |
| 9554 | } else { |
| 9555 | address = (char *)calloc(1, 1); |
| 9556 | } |
| 9557 | AudioParameter param = AudioParameter(String8(address)); |
| 9558 | free(address); |
| 9559 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
| 9560 | if (!isOutput()) { |
| 9561 | param.addInt(String8(AudioParameter::keyInputSource), |
| 9562 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 9563 | } |
| 9564 | status = mHalStream->setParameters(param.toString()); |
| 9565 | *handle = AUDIO_PATCH_HANDLE_NONE; |
| 9566 | } |
| 9567 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9568 | if (numDevices == 0 || mDeviceId != deviceId) { |
| 9569 | if (isOutput()) { |
| 9570 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 9571 | mOutDeviceTypeAddrs = sinkDeviceTypeAddrs; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 9572 | checkSilentMode_l(); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9573 | } else { |
| 9574 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 9575 | mInDeviceTypeAddr = sourceDeviceTypeAddr; |
| 9576 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 9577 | sp<MmapStreamCallback> callback = mCallback.promote(); |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9578 | if (mDeviceId != deviceId && callback != 0) { |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 9579 | mLock.unlock(); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 9580 | callback->onRoutingChanged(deviceId); |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 9581 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9582 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9583 | mPatch = *patch; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9584 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9585 | } |
| 9586 | return status; |
| 9587 | } |
| 9588 | |
| 9589 | status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 9590 | { |
| 9591 | status_t status = NO_ERROR; |
| 9592 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9593 | mPatch = audio_patch{}; |
| 9594 | mOutDeviceTypeAddrs.clear(); |
| 9595 | mInDeviceTypeAddr.reset(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9596 | |
| 9597 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? |
| 9598 | supportsAudioPatches : false; |
| 9599 | |
| 9600 | if (supportsAudioPatches) { |
| 9601 | status = mHalDevice->releaseAudioPatch(handle); |
| 9602 | } else { |
| 9603 | AudioParameter param; |
| 9604 | param.addInt(String8(AudioParameter::keyRouting), 0); |
| 9605 | status = mHalStream->setParameters(param.toString()); |
| 9606 | } |
| 9607 | return status; |
| 9608 | } |
| 9609 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 9610 | void AudioFlinger::MmapThread::toAudioPortConfig(struct audio_port_config *config) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9611 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 9612 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9613 | if (isOutput()) { |
| 9614 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 9615 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 9616 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 9617 | } else { |
| 9618 | config->role = AUDIO_PORT_ROLE_SINK; |
| 9619 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 9620 | config->ext.mix.usecase.source = mAudioSource; |
| 9621 | } |
| 9622 | } |
| 9623 | |
| 9624 | status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 9625 | { |
| 9626 | audio_session_t session = chain->sessionId(); |
| 9627 | |
| 9628 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 9629 | // Attach all tracks with same session ID to this chain. |
| 9630 | // indicate all active tracks in the chain |
| 9631 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 9632 | if (session == track->sessionId()) { |
| 9633 | chain->incTrackCnt(); |
| 9634 | chain->incActiveTrackCnt(); |
| 9635 | } |
| 9636 | } |
| 9637 | |
| 9638 | chain->setThread(this); |
| 9639 | chain->setInBuffer(nullptr); |
| 9640 | chain->setOutBuffer(nullptr); |
| 9641 | chain->syncHalEffectsState(); |
| 9642 | |
| 9643 | mEffectChains.add(chain); |
| 9644 | checkSuspendOnAddEffectChain_l(chain); |
| 9645 | return NO_ERROR; |
| 9646 | } |
| 9647 | |
| 9648 | size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 9649 | { |
| 9650 | audio_session_t session = chain->sessionId(); |
| 9651 | |
| 9652 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 9653 | |
| 9654 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 9655 | if (chain == mEffectChains[i]) { |
| 9656 | mEffectChains.removeAt(i); |
| 9657 | // detach all active tracks from the chain |
| 9658 | // detach all tracks with same session ID from this chain |
| 9659 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 9660 | if (session == track->sessionId()) { |
| 9661 | chain->decActiveTrackCnt(); |
| 9662 | chain->decTrackCnt(); |
| 9663 | } |
| 9664 | } |
| 9665 | break; |
| 9666 | } |
| 9667 | } |
| 9668 | return mEffectChains.size(); |
| 9669 | } |
| 9670 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9671 | void AudioFlinger::MmapThread::threadLoop_standby() |
| 9672 | { |
| 9673 | mHalStream->standby(); |
| 9674 | } |
| 9675 | |
| 9676 | void AudioFlinger::MmapThread::threadLoop_exit() |
| 9677 | { |
Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 9678 | // Do not call callback->onTearDown() because it is redundant for thread exit |
| 9679 | // and because it can cause a recursive mutex lock on stop(). |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9680 | } |
| 9681 | |
| 9682 | status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
| 9683 | { |
| 9684 | return BAD_VALUE; |
| 9685 | } |
| 9686 | |
| 9687 | bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
| 9688 | { |
| 9689 | return false; |
| 9690 | } |
| 9691 | |
| 9692 | status_t AudioFlinger::MmapThread::checkEffectCompatibility_l( |
| 9693 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 9694 | { |
| 9695 | // No global effect sessions on mmap threads |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 9696 | if (audio_is_global_session(sessionId)) { |
| 9697 | ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s", |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9698 | desc->name, mThreadName); |
| 9699 | return BAD_VALUE; |
| 9700 | } |
| 9701 | |
| 9702 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 9703 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", |
| 9704 | desc->name); |
| 9705 | return BAD_VALUE; |
| 9706 | } |
| 9707 | if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 9708 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " |
| 9709 | "thread", desc->name); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9710 | return BAD_VALUE; |
| 9711 | } |
| 9712 | |
| 9713 | // Only allow effects without processing load or latency |
| 9714 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { |
| 9715 | return BAD_VALUE; |
| 9716 | } |
| 9717 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 9718 | if (EffectModule::isHapticGenerator(&desc->type)) { |
| 9719 | ALOGE("%s(): HapticGenerator is not supported for MmapThread", __func__); |
| 9720 | return BAD_VALUE; |
| 9721 | } |
| 9722 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9723 | return NO_ERROR; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9724 | } |
| 9725 | |
| 9726 | void AudioFlinger::MmapThread::checkInvalidTracks_l() |
| 9727 | { |
| 9728 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 9729 | if (track->isInvalid()) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 9730 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 9731 | if (callback != 0) { |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 9732 | mLock.unlock(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9733 | callback->onTearDown(track->portId()); |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 9734 | mLock.lock(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 9735 | } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 9736 | ALOGW("Could not notify MMAP stream tear down: no onTearDown callback!"); |
| 9737 | mNoCallbackWarningCount++; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9738 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9739 | } |
| 9740 | } |
| 9741 | } |
| 9742 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 9743 | void AudioFlinger::MmapThread::dumpInternals_l(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9744 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9745 | dprintf(fd, " Attributes: content type %d usage %d source %d\n", |
| 9746 | mAttr.content_type, mAttr.usage, mAttr.source); |
| 9747 | dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 9748 | if (mActiveTracks.isEmpty()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9749 | dprintf(fd, " No active clients\n"); |
| 9750 | } |
| 9751 | } |
| 9752 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 9753 | void AudioFlinger::MmapThread::dumpTracks_l(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9754 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9755 | String8 result; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9756 | size_t numtracks = mActiveTracks.size(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9757 | dprintf(fd, " %zu Tracks\n", numtracks); |
| 9758 | const char *prefix = " "; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9759 | if (numtracks) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9760 | result.append(prefix); |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 9761 | mActiveTracks[0]->appendDumpHeader(result); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9762 | for (size_t i = 0; i < numtracks ; ++i) { |
| 9763 | sp<MmapTrack> track = mActiveTracks[i]; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9764 | result.append(prefix); |
| 9765 | track->appendDump(result, true /* active */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9766 | } |
| 9767 | } else { |
| 9768 | dprintf(fd, "\n"); |
| 9769 | } |
| 9770 | write(fd, result.string(), result.size()); |
| 9771 | } |
| 9772 | |
| 9773 | AudioFlinger::MmapPlaybackThread::MmapPlaybackThread( |
| 9774 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9775 | AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady) |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9776 | : MmapThread(audioFlinger, id, hwDev, output->stream, systemReady, true /* isOut */), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9777 | mStreamType(AUDIO_STREAM_MUSIC), |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 9778 | mStreamVolume(1.0), |
| 9779 | mStreamMute(false), |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 9780 | mOutput(output) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9781 | { |
| 9782 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); |
| 9783 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 9784 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 9785 | mMasterMute = audioFlinger->masterMute_l(); |
| 9786 | if (mAudioHwDev) { |
| 9787 | if (mAudioHwDev->canSetMasterVolume()) { |
| 9788 | mMasterVolume = 1.0; |
| 9789 | } |
| 9790 | |
| 9791 | if (mAudioHwDev->canSetMasterMute()) { |
| 9792 | mMasterMute = false; |
| 9793 | } |
| 9794 | } |
| 9795 | } |
| 9796 | |
| 9797 | void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr, |
| 9798 | audio_stream_type_t streamType, |
| 9799 | audio_session_t sessionId, |
| 9800 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9801 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9802 | audio_port_handle_t portId) |
| 9803 | { |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 9804 | MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9805 | mStreamType = streamType; |
| 9806 | } |
| 9807 | |
| 9808 | AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput() |
| 9809 | { |
| 9810 | Mutex::Autolock _l(mLock); |
| 9811 | AudioStreamOut *output = mOutput; |
| 9812 | mOutput = NULL; |
| 9813 | return output; |
| 9814 | } |
| 9815 | |
| 9816 | void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value) |
| 9817 | { |
| 9818 | Mutex::Autolock _l(mLock); |
| 9819 | // Don't apply master volume in SW if our HAL can do it for us. |
| 9820 | if (mAudioHwDev && |
| 9821 | mAudioHwDev->canSetMasterVolume()) { |
| 9822 | mMasterVolume = 1.0; |
| 9823 | } else { |
| 9824 | mMasterVolume = value; |
| 9825 | } |
| 9826 | } |
| 9827 | |
| 9828 | void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted) |
| 9829 | { |
| 9830 | Mutex::Autolock _l(mLock); |
| 9831 | // Don't apply master mute in SW if our HAL can do it for us. |
| 9832 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { |
| 9833 | mMasterMute = false; |
| 9834 | } else { |
| 9835 | mMasterMute = muted; |
| 9836 | } |
| 9837 | } |
| 9838 | |
| 9839 | void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 9840 | { |
| 9841 | Mutex::Autolock _l(mLock); |
| 9842 | if (stream == mStreamType) { |
| 9843 | mStreamVolume = value; |
| 9844 | broadcast_l(); |
| 9845 | } |
| 9846 | } |
| 9847 | |
| 9848 | float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 9849 | { |
| 9850 | Mutex::Autolock _l(mLock); |
| 9851 | if (stream == mStreamType) { |
| 9852 | return mStreamVolume; |
| 9853 | } |
| 9854 | return 0.0f; |
| 9855 | } |
| 9856 | |
| 9857 | void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 9858 | { |
| 9859 | Mutex::Autolock _l(mLock); |
| 9860 | if (stream == mStreamType) { |
| 9861 | mStreamMute= muted; |
| 9862 | broadcast_l(); |
| 9863 | } |
| 9864 | } |
| 9865 | |
| 9866 | void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 9867 | { |
| 9868 | Mutex::Autolock _l(mLock); |
| 9869 | if (streamType == mStreamType) { |
| 9870 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 9871 | track->invalidate(); |
| 9872 | } |
| 9873 | broadcast_l(); |
| 9874 | } |
| 9875 | } |
| 9876 | |
| 9877 | void AudioFlinger::MmapPlaybackThread::processVolume_l() |
| 9878 | { |
| 9879 | float volume; |
| 9880 | |
| 9881 | if (mMasterMute || mStreamMute) { |
| 9882 | volume = 0; |
| 9883 | } else { |
| 9884 | volume = mMasterVolume * mStreamVolume; |
| 9885 | } |
| 9886 | |
| 9887 | if (volume != mHalVolFloat) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9888 | |
| 9889 | // Convert volumes from float to 8.24 |
| 9890 | uint32_t vol = (uint32_t)(volume * (1 << 24)); |
| 9891 | |
| 9892 | // Delegate volume control to effect in track effect chain if needed |
| 9893 | // only one effect chain can be present on DirectOutputThread, so if |
| 9894 | // there is one, the track is connected to it |
| 9895 | if (!mEffectChains.isEmpty()) { |
| 9896 | mEffectChains[0]->setVolume_l(&vol, &vol); |
| 9897 | volume = (float)vol / (1 << 24); |
| 9898 | } |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 9899 | // Try to use HW volume control and fall back to SW control if not implemented |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 9900 | if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) { |
| 9901 | mHalVolFloat = volume; // HW volume control worked, so update value. |
| 9902 | mNoCallbackWarningCount = 0; |
| 9903 | } else { |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 9904 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 9905 | if (callback != 0) { |
| 9906 | int channelCount; |
| 9907 | if (isOutput()) { |
| 9908 | channelCount = audio_channel_count_from_out_mask(mChannelMask); |
| 9909 | } else { |
| 9910 | channelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 9911 | } |
| 9912 | Vector<float> values; |
| 9913 | for (int i = 0; i < channelCount; i++) { |
| 9914 | values.add(volume); |
| 9915 | } |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 9916 | mHalVolFloat = volume; // SW volume control worked, so update value. |
| 9917 | mNoCallbackWarningCount = 0; |
Eric Laurent | 734046e | 2018-04-16 14:50:52 -0700 | [diff] [blame] | 9918 | mLock.unlock(); |
| 9919 | callback->onVolumeChanged(mChannelMask, values); |
| 9920 | mLock.lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9921 | } else { |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 9922 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 9923 | ALOGW("Could not set MMAP stream volume: no volume callback!"); |
| 9924 | mNoCallbackWarningCount++; |
| 9925 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9926 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9927 | } |
| 9928 | } |
| 9929 | } |
| 9930 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9931 | void AudioFlinger::MmapPlaybackThread::updateMetadata_l() |
| 9932 | { |
| 9933 | if (mOutput == nullptr || mOutput->stream == nullptr || |
| 9934 | !mActiveTracks.readAndClearHasChanged()) { |
| 9935 | return; |
| 9936 | } |
| 9937 | StreamOutHalInterface::SourceMetadata metadata; |
| 9938 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 9939 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 9940 | playback_track_metadata_v7_t trackMetadata; |
| 9941 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9942 | .usage = track->attributes().usage, |
| 9943 | .content_type = track->attributes().content_type, |
| 9944 | .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 9945 | }; |
| 9946 | trackMetadata.channel_mask = track->channelMask(), |
| 9947 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 9948 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9949 | } |
| 9950 | mOutput->stream->updateSourceMetadata(metadata); |
| 9951 | } |
| 9952 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9953 | void AudioFlinger::MmapPlaybackThread::checkSilentMode_l() |
| 9954 | { |
| 9955 | if (!mMasterMute) { |
| 9956 | char value[PROPERTY_VALUE_MAX]; |
| 9957 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 9958 | char *endptr; |
| 9959 | unsigned long ul = strtoul(value, &endptr, 0); |
| 9960 | if (*endptr == '\0' && ul != 0) { |
| 9961 | ALOGD("Silence is golden"); |
| 9962 | // The setprop command will not allow a property to be changed after |
| 9963 | // the first time it is set, so we don't have to worry about un-muting. |
| 9964 | setMasterMute_l(true); |
| 9965 | } |
| 9966 | } |
| 9967 | } |
| 9968 | } |
| 9969 | |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 9970 | void AudioFlinger::MmapPlaybackThread::toAudioPortConfig(struct audio_port_config *config) |
| 9971 | { |
| 9972 | MmapThread::toAudioPortConfig(config); |
| 9973 | if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { |
| 9974 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 9975 | config->flags.output = mOutput->flags; |
| 9976 | } |
| 9977 | } |
| 9978 | |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 9979 | status_t AudioFlinger::MmapPlaybackThread::getExternalPosition(uint64_t *position, |
| 9980 | int64_t *timeNanos) |
| 9981 | { |
| 9982 | if (mOutput == nullptr) { |
| 9983 | return NO_INIT; |
| 9984 | } |
| 9985 | struct timespec timestamp; |
| 9986 | status_t status = mOutput->getPresentationPosition(position, ×tamp); |
| 9987 | if (status == NO_ERROR) { |
| 9988 | *timeNanos = timestamp.tv_sec * NANOS_PER_SECOND + timestamp.tv_nsec; |
| 9989 | } |
| 9990 | return status; |
| 9991 | } |
| 9992 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 9993 | void AudioFlinger::MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9994 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 9995 | MmapThread::dumpInternals_l(fd, args); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9996 | |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 9997 | dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", |
| 9998 | mStreamType, mStreamVolume, mHalVolFloat, mStreamMute); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 9999 | dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); |
| 10000 | } |
| 10001 | |
| 10002 | AudioFlinger::MmapCaptureThread::MmapCaptureThread( |
| 10003 | const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10004 | AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady) |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10005 | : MmapThread(audioFlinger, id, hwDev, input->stream, systemReady, false /* isOut */), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10006 | mInput(input) |
| 10007 | { |
| 10008 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); |
| 10009 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 10010 | } |
| 10011 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10012 | status_t AudioFlinger::MmapCaptureThread::exitStandby() |
| 10013 | { |
Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 10014 | { |
| 10015 | // mInput might have been cleared by clearInput() |
| 10016 | Mutex::Autolock _l(mLock); |
| 10017 | if (mInput != nullptr && mInput->stream != nullptr) { |
| 10018 | mInput->stream->setGain(1.0f); |
| 10019 | } |
| 10020 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10021 | return MmapThread::exitStandby(); |
| 10022 | } |
| 10023 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10024 | AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput() |
| 10025 | { |
| 10026 | Mutex::Autolock _l(mLock); |
| 10027 | AudioStreamIn *input = mInput; |
| 10028 | mInput = NULL; |
| 10029 | return input; |
| 10030 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10031 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10032 | |
| 10033 | void AudioFlinger::MmapCaptureThread::processVolume_l() |
| 10034 | { |
| 10035 | bool changed = false; |
| 10036 | bool silenced = false; |
| 10037 | |
| 10038 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 10039 | if (callback == 0) { |
| 10040 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 10041 | ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!"); |
| 10042 | mNoCallbackWarningCount++; |
| 10043 | } |
| 10044 | } |
| 10045 | |
| 10046 | // After a change occurred in track silenced state, mute capture in audio DSP if at least one |
| 10047 | // track is silenced and unmute otherwise |
| 10048 | for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) { |
| 10049 | if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) { |
| 10050 | changed = true; |
| 10051 | silenced = mActiveTracks[i]->isSilenced_l(); |
| 10052 | } |
| 10053 | } |
| 10054 | |
| 10055 | if (changed) { |
| 10056 | mInput->stream->setGain(silenced ? 0.0f: 1.0f); |
| 10057 | } |
| 10058 | } |
| 10059 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10060 | void AudioFlinger::MmapCaptureThread::updateMetadata_l() |
| 10061 | { |
| 10062 | if (mInput == nullptr || mInput->stream == nullptr || |
| 10063 | !mActiveTracks.readAndClearHasChanged()) { |
| 10064 | return; |
| 10065 | } |
| 10066 | StreamInHalInterface::SinkMetadata metadata; |
| 10067 | for (const sp<MmapTrack> &track : mActiveTracks) { |
| 10068 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 10069 | record_track_metadata_v7_t trackMetadata; |
| 10070 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10071 | .source = track->attributes().source, |
| 10072 | .gain = 1, // capture tracks do not have volumes |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 10073 | }; |
| 10074 | trackMetadata.channel_mask = track->channelMask(), |
| 10075 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 10076 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10077 | } |
| 10078 | mInput->stream->updateSinkMetadata(metadata); |
| 10079 | } |
| 10080 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 10081 | void AudioFlinger::MmapCaptureThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10082 | { |
| 10083 | Mutex::Autolock _l(mLock); |
| 10084 | for (size_t i = 0; i < mActiveTracks.size() ; i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 10085 | if (mActiveTracks[i]->portId() == portId) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10086 | mActiveTracks[i]->setSilenced_l(silenced); |
| 10087 | broadcast_l(); |
| 10088 | } |
| 10089 | } |
| 10090 | } |
| 10091 | |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 10092 | void AudioFlinger::MmapCaptureThread::toAudioPortConfig(struct audio_port_config *config) |
| 10093 | { |
| 10094 | MmapThread::toAudioPortConfig(config); |
| 10095 | if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { |
| 10096 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 10097 | config->flags.input = mInput->flags; |
| 10098 | } |
| 10099 | } |
| 10100 | |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 10101 | status_t AudioFlinger::MmapCaptureThread::getExternalPosition( |
| 10102 | uint64_t *position, int64_t *timeNanos) |
| 10103 | { |
| 10104 | if (mInput == nullptr) { |
| 10105 | return NO_INIT; |
| 10106 | } |
| 10107 | return mInput->getCapturePosition((int64_t*)position, timeNanos); |
| 10108 | } |
| 10109 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 10110 | } // namespace android |