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" |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 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 | |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 23 | #include "Threads.h" |
| 24 | |
| 25 | #include "Client.h" |
| 26 | #include "IAfEffect.h" |
| 27 | #include "MelReporter.h" |
| 28 | #include "ResamplerBufferProvider.h" |
| 29 | |
| 30 | #include <afutils/DumpTryLock.h> |
| 31 | #include <afutils/Permission.h> |
| 32 | #include <afutils/TypedLogger.h> |
| 33 | #include <afutils/Vibrator.h> |
| 34 | #include <audio_utils/MelProcessor.h> |
| 35 | #include <audio_utils/Metadata.h> |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 36 | #include <com_android_media_audioserver.h> |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 37 | #ifdef DEBUG_CPU_USAGE |
| 38 | #include <audio_utils/Statistics.h> |
| 39 | #include <cpustats/ThreadCpuUsage.h> |
| 40 | #endif |
| 41 | #include <audio_utils/channels.h> |
| 42 | #include <audio_utils/format.h> |
| 43 | #include <audio_utils/minifloat.h> |
| 44 | #include <audio_utils/mono_blend.h> |
| 45 | #include <audio_utils/primitives.h> |
| 46 | #include <audio_utils/safe_math.h> |
| 47 | #include <audiomanager/AudioManager.h> |
| 48 | #include <binder/IPCThreadState.h> |
| 49 | #include <binder/IServiceManager.h> |
| 50 | #include <binder/PersistableBundle.h> |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 51 | #include <com_android_media_audio.h> |
Glenn Kasten | c9a2367 | 2020-06-30 12:12:42 -0700 | [diff] [blame] | 52 | #include <cutils/bitops.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 53 | #include <cutils/properties.h> |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 54 | #include <fastpath/AutoPark.h> |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 55 | #include <media/AudioContainers.h> |
| 56 | #include <media/AudioDeviceTypeAddr.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 57 | #include <media/AudioParameter.h> |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 58 | #include <media/AudioResamplerPublic.h> |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 59 | #ifdef ADD_BATTERY_DATA |
| 60 | #include <media/IMediaPlayerService.h> |
| 61 | #include <media/IMediaDeathNotifier.h> |
| 62 | #endif |
| 63 | #include <media/MmapStreamCallback.h> |
Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 64 | #include <media/RecordBufferConverter.h> |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 65 | #include <media/TypeConverter.h> |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 66 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
| 67 | #include <media/audiohal/StreamHalInterface.h> |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 68 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 69 | #include <media/nbaio/AudioStreamOutSink.h> |
| 70 | #include <media/nbaio/MonoPipe.h> |
| 71 | #include <media/nbaio/MonoPipeReader.h> |
| 72 | #include <media/nbaio/Pipe.h> |
| 73 | #include <media/nbaio/PipeReader.h> |
| 74 | #include <media/nbaio/SourceAudioBufferProvider.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 75 | #include <mediautils/BatteryNotifier.h> |
Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 76 | #include <mediautils/Process.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 77 | #include <mediautils/SchedulingPolicyService.h> |
| 78 | #include <mediautils/ServiceUtilities.h> |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 79 | #include <powermanager/PowerManager.h> |
| 80 | #include <private/android_filesystem_config.h> |
| 81 | #include <private/media/AudioTrackShared.h> |
| 82 | #include <system/audio_effects/effect_aec.h> |
| 83 | #include <system/audio_effects/effect_downmix.h> |
| 84 | #include <system/audio_effects/effect_ns.h> |
| 85 | #include <system/audio_effects/effect_spatializer.h> |
| 86 | #include <utils/Log.h> |
| 87 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 88 | |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 89 | #include <fcntl.h> |
| 90 | #include <linux/futex.h> |
| 91 | #include <math.h> |
| 92 | #include <memory> |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 93 | #include <pthread.h> |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 94 | #include <sstream> |
| 95 | #include <string> |
| 96 | #include <sys/stat.h> |
| 97 | #include <sys/syscall.h> |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 98 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 99 | // ---------------------------------------------------------------------------- |
| 100 | |
| 101 | // Note: the following macro is used for extremely verbose logging message. In |
| 102 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 103 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 104 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 105 | // turned on. Do not uncomment the #def below unless you really know what you |
| 106 | // are doing and want to see all of the extremely verbose messages. |
| 107 | //#define VERY_VERY_VERBOSE_LOGGING |
| 108 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 109 | #define ALOGVV ALOGV |
| 110 | #else |
| 111 | #define ALOGVV(a...) do { } while(0) |
| 112 | #endif |
| 113 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 114 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 115 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 116 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 117 | template <typename T> |
| 118 | static inline T min(const T& a, const T& b) |
| 119 | { |
| 120 | return a < b ? a : b; |
| 121 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 122 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 123 | namespace android { |
| 124 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 125 | using audioflinger::SyncEvent; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 126 | using media::IEffectClient; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 127 | using content::AttributionSourceState; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 128 | |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 129 | // Keep in sync with java definition in media/java/android/media/AudioRecord.java |
| 130 | static constexpr int32_t kMaxSharedAudioHistoryMs = 5000; |
| 131 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 132 | // retry counts for buffer fill timeout |
| 133 | // 50 * ~20msecs = 1 second |
| 134 | static const int8_t kMaxTrackRetries = 50; |
| 135 | static const int8_t kMaxTrackStartupRetries = 50; |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 136 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 137 | // allow less retry attempts on direct output thread. |
| 138 | // direct outputs can be a scarce resource in audio hardware and should |
| 139 | // be released as quickly as possible. |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 140 | // Notes: |
| 141 | // 1) The retry duration kMaxTrackRetriesDirectMs may be increased |
| 142 | // in case the data write is bursty for the AudioTrack. The application |
| 143 | // should endeavor to write at least once every kMaxTrackRetriesDirectMs |
| 144 | // to prevent an underrun situation. If the data is bursty, then |
| 145 | // the application can also throttle the data sent to be even. |
| 146 | // 2) For compressed audio data, any data present in the AudioTrack buffer |
| 147 | // will be sent and reset the retry count. This delivers data as |
| 148 | // it arrives, with approximately kDirectMinSleepTimeUs = 10ms checking interval. |
| 149 | // 3) For linear PCM or proportional PCM, we wait one period for a period's worth |
| 150 | // of data to be available, then any remaining data is delivered. |
| 151 | // This is required to ensure the last bit of data is delivered before underrun. |
| 152 | // |
| 153 | // Sleep time per cycle is kDirectMinSleepTimeUs for compressed tracks |
| 154 | // or the size of the HAL period for proportional / linear PCM tracks. |
| 155 | static const int32_t kMaxTrackRetriesDirectMs = 200; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 156 | |
| 157 | // don't warn about blocked writes or record buffer overflows more often than this |
| 158 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 159 | |
| 160 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 161 | static const int kRecordThreadSleepUs = 5000; |
| 162 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 163 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 164 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 165 | |
| 166 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 167 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 168 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 169 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 170 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 171 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 172 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 173 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 174 | // maximum normal sink buffer size |
| 175 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 176 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 177 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 178 | // FIXME This should be based on experimentally observed scheduling jitter |
| 179 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 180 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 181 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 182 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 183 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 184 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 185 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 186 | |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 187 | // Minimum amount of time between checking to see if the timestamp is advancing |
| 188 | // for underrun detection. If we check too frequently, we may not detect a |
| 189 | // timestamp update and will falsely detect underrun. |
Andy Hung | 0ff1429 | 2023-12-20 15:55:16 -0800 | [diff] [blame] | 190 | static constexpr nsecs_t kMinimumTimeBetweenTimestampChecksNs = 150 /* ms */ * 1'000'000; |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 191 | |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 192 | // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good |
| 193 | // balance between power consumption and latency, and allows threads to be scheduled reliably |
| 194 | // by the CFS scheduler. |
| 195 | // FIXME Express other hardcoded references to 20ms with references to this constant and move |
| 196 | // it appropriately. |
| 197 | #define FMS_20 20 |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 198 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 199 | // Whether to use fast mixer |
| 200 | static const enum { |
| 201 | FastMixer_Never, // never initialize or use: for debugging only |
| 202 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 203 | // normal mixer multiplier is 1 |
| 204 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 205 | // multiplier is calculated based on min & max normal mixer buffer size |
| 206 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 207 | // multiplier is calculated based on min & max normal mixer buffer size |
| 208 | // FIXME for FastMixer_Dynamic: |
| 209 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 210 | // For example, one HAL implementation returns an error from a large write, |
| 211 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 212 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 213 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 214 | } kUseFastMixer = FastMixer_Static; |
| 215 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 216 | // Whether to use fast capture |
| 217 | static const enum { |
| 218 | FastCapture_Never, // never initialize or use: for debugging only |
| 219 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 220 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 221 | } kUseFastCapture = FastCapture_Static; |
| 222 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 223 | // Priorities for requestPriority |
| 224 | static const int kPriorityAudioApp = 2; |
| 225 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 226 | static const int kPriorityFastCapture = 3; |
Pattara Teerapong | 9a332c5 | 2024-01-26 08:18:05 +0000 | [diff] [blame] | 227 | // Request real-time priority for PlaybackThread in ARC |
| 228 | static const int kPriorityPlaybackThreadArc = 1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 229 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 230 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 231 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 232 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 233 | |
| 234 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 235 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 236 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 237 | // The minimum and maximum allowed values |
| 238 | static const int kFastTrackMultiplierMin = 1; |
| 239 | static const int kFastTrackMultiplierMax = 2; |
| 240 | |
| 241 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 242 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 243 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 244 | // See Thread::readOnlyHeap(). |
| 245 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 246 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 247 | // 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] | 248 | static const size_t kRecordThreadReadOnlyHeapSize = 0xD000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 249 | |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 250 | static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3); |
Andy Hung | d58c473 | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 251 | |
| 252 | static nsecs_t getStandbyTimeInNanos() { |
| 253 | static nsecs_t standbyTimeInNanos = []() { |
| 254 | const int ms = property_get_int32("ro.audio.flinger_standbytime_ms", |
| 255 | kDefaultStandbyTimeInNsecs / NANOS_PER_MILLISECOND); |
| 256 | ALOGI("%s: Using %d ms as standby time", __func__, ms); |
| 257 | return milliseconds(ms); |
| 258 | }(); |
| 259 | return standbyTimeInNanos; |
| 260 | } |
| 261 | |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 262 | // Set kEnableExtendedChannels to true to enable greater than stereo output |
| 263 | // for the MixerThread and device sink. Number of channels allowed is |
| 264 | // FCC_2 <= channels <= FCC_LIMIT. |
| 265 | constexpr bool kEnableExtendedChannels = true; |
| 266 | |
| 267 | // Returns true if channel mask is permitted for the PCM sink in the MixerThread |
| 268 | /* static */ |
| 269 | bool IAfThreadBase::isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) { |
| 270 | switch (audio_channel_mask_get_representation(channelMask)) { |
| 271 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 272 | // Haptic channel mask is only applicable for channel position mask. |
| 273 | const uint32_t channelCount = audio_channel_count_from_out_mask( |
| 274 | static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL)); |
| 275 | const uint32_t maxChannelCount = kEnableExtendedChannels |
| 276 | ? FCC_LIMIT : FCC_2; |
| 277 | if (channelCount < FCC_2 // mono is not supported at this time |
| 278 | || channelCount > maxChannelCount) { |
| 279 | return false; |
| 280 | } |
| 281 | // check that channelMask is the "canonical" one we expect for the channelCount. |
| 282 | return audio_channel_position_mask_is_out_canonical(channelMask); |
| 283 | } |
| 284 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 285 | if (kEnableExtendedChannels) { |
| 286 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 287 | if (channelCount >= FCC_2 // mono is not supported at this time |
| 288 | && channelCount <= FCC_LIMIT) { |
| 289 | return true; |
| 290 | } |
| 291 | } |
| 292 | return false; |
| 293 | default: |
| 294 | return false; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Set kEnableExtendedPrecision to true to use extended precision in MixerThread |
| 299 | constexpr bool kEnableExtendedPrecision = true; |
| 300 | |
| 301 | // Returns true if format is permitted for the PCM sink in the MixerThread |
| 302 | /* static */ |
| 303 | bool IAfThreadBase::isValidPcmSinkFormat(audio_format_t format) { |
| 304 | switch (format) { |
| 305 | case AUDIO_FORMAT_PCM_16_BIT: |
| 306 | return true; |
| 307 | case AUDIO_FORMAT_PCM_FLOAT: |
| 308 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 309 | case AUDIO_FORMAT_PCM_32_BIT: |
| 310 | case AUDIO_FORMAT_PCM_8_24_BIT: |
| 311 | return kEnableExtendedPrecision; |
| 312 | default: |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 317 | // ---------------------------------------------------------------------------- |
| 318 | |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 319 | // formatToString() needs to be exact for MediaMetrics purposes. |
| 320 | // Do not use media/TypeConverter.h toString(). |
| 321 | /* static */ |
| 322 | std::string IAfThreadBase::formatToString(audio_format_t format) { |
| 323 | std::string result; |
| 324 | FormatConverter::toString(format, result); |
| 325 | return result; |
| 326 | } |
| 327 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 328 | // TODO: move all toString helpers to audio.h |
| 329 | // under #ifdef __cplusplus #endif |
| 330 | static std::string patchSinksToString(const struct audio_patch *patch) |
| 331 | { |
| 332 | std::stringstream ss; |
| 333 | for (size_t i = 0; i < patch->num_sinks; ++i) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 334 | if (i > 0) { |
| 335 | ss << "|"; |
| 336 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 337 | ss << "(" << toString(patch->sinks[i].ext.device.type) |
| 338 | << ", " << patch->sinks[i].ext.device.address << ")"; |
| 339 | } |
| 340 | return ss.str(); |
| 341 | } |
| 342 | |
| 343 | static std::string patchSourcesToString(const struct audio_patch *patch) |
| 344 | { |
| 345 | std::stringstream ss; |
| 346 | for (size_t i = 0; i < patch->num_sources; ++i) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 347 | if (i > 0) { |
| 348 | ss << "|"; |
| 349 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 350 | ss << "(" << toString(patch->sources[i].ext.device.type) |
| 351 | << ", " << patch->sources[i].ext.device.address << ")"; |
| 352 | } |
| 353 | return ss.str(); |
| 354 | } |
| 355 | |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 356 | static std::string toString(audio_latency_mode_t mode) { |
| 357 | // We convert to the AIDL type to print (eventually the legacy type will be removed). |
Mikhail Naganov | f53e182 | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 358 | const auto result = legacy2aidl_audio_latency_mode_t_AudioLatencyMode(mode); |
| 359 | return result.has_value() ? media::audio::common::toString(*result) : "UNKNOWN"; |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Could be made a template, but other toString overloads for std::vector are confused. |
| 363 | static std::string toString(const std::vector<audio_latency_mode_t>& elements) { |
| 364 | std::string s("{ "); |
| 365 | for (const auto& e : elements) { |
| 366 | s.append(toString(e)); |
| 367 | s.append(" "); |
| 368 | } |
| 369 | s.append("}"); |
| 370 | return s; |
| 371 | } |
| 372 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 373 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 374 | |
| 375 | static void sFastTrackMultiplierInit() |
| 376 | { |
| 377 | char value[PROPERTY_VALUE_MAX]; |
| 378 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 379 | char *endptr; |
| 380 | unsigned long ul = strtoul(value, &endptr, 0); |
| 381 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 382 | sFastTrackMultiplier = (int) ul; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // ---------------------------------------------------------------------------- |
| 388 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 389 | #ifdef ADD_BATTERY_DATA |
| 390 | // To collect the amplifier usage |
| 391 | static void addBatteryData(uint32_t params) { |
| 392 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 393 | if (service == NULL) { |
| 394 | // it already logged |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | service->addBatteryData(params); |
| 399 | } |
| 400 | #endif |
| 401 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 402 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 403 | struct { |
| 404 | // call when you acquire a partial wakelock |
| 405 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 406 | pthread_mutex_lock(&mLock); |
| 407 | if (wakeLockToken.get() == nullptr) { |
| 408 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 409 | } else { |
| 410 | if (mCount == 0) { |
| 411 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 412 | } |
| 413 | ++mCount; |
| 414 | } |
| 415 | pthread_mutex_unlock(&mLock); |
| 416 | } |
| 417 | |
| 418 | // call when you release a partial wakelock. |
| 419 | void release(const sp<IBinder> &wakeLockToken) { |
| 420 | if (wakeLockToken.get() == nullptr) { |
| 421 | return; |
| 422 | } |
| 423 | pthread_mutex_lock(&mLock); |
| 424 | if (--mCount < 0) { |
| 425 | ALOGE("negative wakelock count"); |
| 426 | mCount = 0; |
| 427 | } |
| 428 | pthread_mutex_unlock(&mLock); |
| 429 | } |
| 430 | |
| 431 | // retrieves the boottime timebase offset from monotonic. |
| 432 | int64_t getBoottimeOffset() { |
| 433 | pthread_mutex_lock(&mLock); |
| 434 | int64_t boottimeOffset = mBoottimeOffset; |
| 435 | pthread_mutex_unlock(&mLock); |
| 436 | return boottimeOffset; |
| 437 | } |
| 438 | |
| 439 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 440 | // and the selected timebase. |
| 441 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 442 | // |
| 443 | // This only needs to be called upon acquiring the first partial wakelock |
| 444 | // after all other partial wakelocks are released. |
| 445 | // |
| 446 | // We do an empirical measurement of the offset rather than parsing |
| 447 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 448 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 449 | int clockbase; |
| 450 | switch (timebase) { |
| 451 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 452 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 453 | break; |
| 454 | default: |
| 455 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 456 | break; |
| 457 | } |
| 458 | // try three times to get the clock offset, choose the one |
| 459 | // with the minimum gap in measurements. |
| 460 | const int tries = 3; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 461 | nsecs_t bestGap = 0, measured = 0; // not required, initialized for clang-tidy |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 462 | for (int i = 0; i < tries; ++i) { |
| 463 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 464 | const nsecs_t tbase = systemTime(clockbase); |
| 465 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 466 | const nsecs_t gap = tmono2 - tmono; |
| 467 | if (i == 0 || gap < bestGap) { |
| 468 | bestGap = gap; |
| 469 | measured = tbase - ((tmono + tmono2) >> 1); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // to avoid micro-adjusting, we don't change the timebase |
| 474 | // unless it is significantly different. |
| 475 | // |
| 476 | // Assumption: It probably takes more than toleranceNs to |
| 477 | // suspend and resume the device. |
| 478 | static int64_t toleranceNs = 10000; // 10 us |
| 479 | if (llabs(*offset - measured) > toleranceNs) { |
| 480 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 481 | (long long)*offset, (long long)measured); |
| 482 | *offset = measured; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | pthread_mutex_t mLock; |
| 487 | int32_t mCount; |
| 488 | int64_t mBoottimeOffset; |
| 489 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 490 | |
| 491 | // ---------------------------------------------------------------------------- |
| 492 | // CPU Stats |
| 493 | // ---------------------------------------------------------------------------- |
| 494 | |
| 495 | class CpuStats { |
| 496 | public: |
| 497 | CpuStats(); |
| 498 | void sample(const String8 &title); |
| 499 | #ifdef DEBUG_CPU_USAGE |
| 500 | private: |
| 501 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 502 | 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] | 503 | |
Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 504 | audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 505 | |
| 506 | int mCpuNum; // thread's current CPU number |
| 507 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 508 | #endif |
| 509 | }; |
| 510 | |
| 511 | CpuStats::CpuStats() |
| 512 | #ifdef DEBUG_CPU_USAGE |
| 513 | : mCpuNum(-1), mCpukHz(-1) |
| 514 | #endif |
| 515 | { |
| 516 | } |
| 517 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 518 | void CpuStats::sample(const String8 &title |
| 519 | #ifndef DEBUG_CPU_USAGE |
| 520 | __unused |
| 521 | #endif |
| 522 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 523 | #ifdef DEBUG_CPU_USAGE |
| 524 | // get current thread's delta CPU time in wall clock ns |
| 525 | double wcNs; |
| 526 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 527 | |
| 528 | // record sample for wall clock statistics |
| 529 | if (valid) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 530 | mWcStats.add(wcNs); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // get the current CPU number |
| 534 | int cpuNum = sched_getcpu(); |
| 535 | |
| 536 | // get the current CPU frequency in kHz |
| 537 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 538 | |
| 539 | // check if either CPU number or frequency changed |
| 540 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 541 | mCpuNum = cpuNum; |
| 542 | mCpukHz = cpukHz; |
| 543 | // ignore sample for purposes of cycles |
| 544 | valid = false; |
| 545 | } |
| 546 | |
| 547 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 548 | if (valid && mCpukHz > 0) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 549 | const double cycles = wcNs * cpukHz * 0.000001; |
| 550 | mHzStats.add(cycles); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 553 | const unsigned n = mWcStats.getN(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 554 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 555 | if ((n & 127) == 1) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 556 | const long long elapsed = mCpuUsage.elapsed(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 557 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 558 | const double perLoop = elapsed / (double) n; |
| 559 | const double perLoop100 = perLoop * 0.01; |
| 560 | const double perLoop1k = perLoop * 0.001; |
| 561 | const double mean = mWcStats.getMean(); |
| 562 | const double stddev = mWcStats.getStdDev(); |
| 563 | const double minimum = mWcStats.getMin(); |
| 564 | const double maximum = mWcStats.getMax(); |
| 565 | const double meanCycles = mHzStats.getMean(); |
| 566 | const double stddevCycles = mHzStats.getStdDev(); |
| 567 | const double minCycles = mHzStats.getMin(); |
| 568 | const double maxCycles = mHzStats.getMax(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 569 | mCpuUsage.resetElapsed(); |
| 570 | mWcStats.reset(); |
| 571 | mHzStats.reset(); |
| 572 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 573 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 574 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 575 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 576 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 577 | title.c_str(), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 578 | elapsed * .000000001, n, perLoop * .000001, |
| 579 | mean * .001, |
| 580 | stddev * .001, |
| 581 | minimum * .001, |
| 582 | maximum * .001, |
| 583 | mean / perLoop100, |
| 584 | stddev / perLoop100, |
| 585 | minimum / perLoop100, |
| 586 | maximum / perLoop100, |
| 587 | meanCycles / perLoop1k, |
| 588 | stddevCycles / perLoop1k, |
| 589 | minCycles / perLoop1k, |
| 590 | maxCycles / perLoop1k); |
| 591 | |
| 592 | } |
| 593 | } |
| 594 | #endif |
| 595 | }; |
| 596 | |
| 597 | // ---------------------------------------------------------------------------- |
| 598 | // ThreadBase |
| 599 | // ---------------------------------------------------------------------------- |
| 600 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 601 | // static |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 602 | const char* ThreadBase::threadTypeToString(ThreadBase::type_t type) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 603 | { |
| 604 | switch (type) { |
| 605 | case MIXER: |
| 606 | return "MIXER"; |
| 607 | case DIRECT: |
| 608 | return "DIRECT"; |
| 609 | case DUPLICATING: |
| 610 | return "DUPLICATING"; |
| 611 | case RECORD: |
| 612 | return "RECORD"; |
| 613 | case OFFLOAD: |
| 614 | return "OFFLOAD"; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 615 | case MMAP_PLAYBACK: |
| 616 | return "MMAP_PLAYBACK"; |
| 617 | case MMAP_CAPTURE: |
| 618 | return "MMAP_CAPTURE"; |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 619 | case SPATIALIZER: |
| 620 | return "SPATIALIZER"; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 621 | case BIT_PERFECT: |
| 622 | return "BIT_PERFECT"; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 623 | default: |
| 624 | return "unknown"; |
| 625 | } |
| 626 | } |
| 627 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 628 | ThreadBase::ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 629 | type_t type, bool systemReady, bool isOut) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 630 | : Thread(false /*canCallJava*/), |
| 631 | mType(type), |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 632 | mAfThreadCallback(afThreadCallback), |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 633 | mThreadMetrics(std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) + std::to_string(id), |
| 634 | isOut), |
| 635 | mIsOut(isOut), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 636 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 637 | // are set by PlaybackThread::readOutputParameters_l() or |
| 638 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 639 | //FIXME: mStandby should be true here. Is this some kind of hack? |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 640 | mStandby(false), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 641 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 642 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 643 | mDeathRecipient(new PMDeathRecipient(this)), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 644 | mSystemReady(systemReady), |
| 645 | mSignalPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 646 | { |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 647 | mThreadMetrics.logConstructor(getpid(), threadTypeToString(type), id); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 648 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 651 | ThreadBase::~ThreadBase() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 652 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 653 | // 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] | 654 | mConfigEvents.clear(); |
| 655 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 656 | // do not lock the mutex in destructor |
| 657 | releaseWakeLock_l(); |
| 658 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 659 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 660 | binder->unlinkToDeath(mDeathRecipient); |
| 661 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 662 | |
| 663 | sendStatistics(true /* force */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 666 | status_t ThreadBase::readyToRun() |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 667 | { |
| 668 | status_t status = initCheck(); |
| 669 | if (status == NO_ERROR) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 670 | ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid()); |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 671 | } else { |
| 672 | ALOGE("No working audio driver found."); |
| 673 | } |
| 674 | return status; |
| 675 | } |
| 676 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 677 | void ThreadBase::exit() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 678 | { |
| 679 | ALOGV("ThreadBase::exit"); |
| 680 | // do any cleanup required for exit to succeed |
| 681 | preExit(); |
| 682 | { |
| 683 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 684 | // if (!exitPending()) { |
| 685 | // // context switch from here to exit() |
| 686 | // // exit() calls requestExit(), what exitPending() observes |
| 687 | // // exit() calls signal(), which is dropped since no waiters |
| 688 | // // context switch back from exit() to here |
| 689 | // mWaitWorkCV.wait(...); |
| 690 | // // now thread is hung |
| 691 | // } |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 692 | audio_utils::lock_guard lock(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 693 | requestExit(); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 694 | mWaitWorkCV.notify_all(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 695 | } |
| 696 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 697 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
Andy Hung | ef2096d | 2024-03-21 19:43:05 -0700 | [diff] [blame] | 698 | |
| 699 | // For TimeCheck: track waiting on the thread join of getTid(). |
| 700 | audio_utils::mutex::scoped_join_wait_check sjw(getTid()); |
| 701 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 702 | requestExitAndWait(); |
| 703 | } |
| 704 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 705 | status_t ThreadBase::setParameters(const String8& keyValuePairs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 706 | { |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 707 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.c_str()); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 708 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 709 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 710 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 711 | } |
| 712 | |
| 713 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 714 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 715 | status_t ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 716 | NO_THREAD_SAFETY_ANALYSIS // condition variable |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 717 | { |
| 718 | status_t status = NO_ERROR; |
| 719 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 720 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 721 | event->mWaitStatus = false; |
| 722 | mPendingConfigEvents.add(event); |
| 723 | return status; |
| 724 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 725 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 726 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 727 | mWaitWorkCV.notify_one(); |
| 728 | mutex().unlock(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 729 | { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 730 | audio_utils::unique_lock _l(event->mutex()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 731 | while (event->mWaitStatus) { |
Andy Hung | 5529c13 | 2024-01-25 17:02:30 -0800 | [diff] [blame] | 732 | if (event->mCondition.wait_for( |
| 733 | _l, std::chrono::nanoseconds(kConfigEventTimeoutNs), getTid()) |
| 734 | == std::cv_status::timeout) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 735 | event->mStatus = TIMED_OUT; |
| 736 | event->mWaitStatus = false; |
| 737 | } |
| 738 | } |
| 739 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 740 | } |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 741 | mutex().lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 742 | return status; |
| 743 | } |
| 744 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 745 | void ThreadBase::sendIoConfigEvent(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 746 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 747 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 748 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 749 | sendIoConfigEvent_l(event, pid, portId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 752 | // sendIoConfigEvent_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 753 | void ThreadBase::sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 754 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 755 | { |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 756 | // The audio statistics history is exponentially weighted to forget events |
| 757 | // about five or more seconds in the past. In order to have |
| 758 | // crisper statistics for mediametrics, we reset the statistics on |
| 759 | // an IoConfigEvent, to reflect different properties for a new device. |
| 760 | mIoJitterMs.reset(); |
| 761 | mLatencyMs.reset(); |
| 762 | mProcessTimeMs.reset(); |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 763 | mMonopipePipeDepthStats.reset(); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 764 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 765 | |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 766 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 767 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 770 | void 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] | 771 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 772 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 773 | sendPrioConfigEvent_l(pid, tid, prio, forApp); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 776 | // sendPrioConfigEvent_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 777 | void ThreadBase::sendPrioConfigEvent_l( |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 778 | pid_t pid, pid_t tid, int32_t prio, bool forApp) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 779 | { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 780 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 781 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 784 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 785 | status_t ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 786 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 787 | sp<ConfigEvent> configEvent; |
| 788 | AudioParameter param(keyValuePair); |
| 789 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 790 | if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 791 | setMasterMono_l(value != 0); |
| 792 | if (param.size() == 1) { |
| 793 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 794 | } |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 795 | param.remove(String8(AudioParameter::keyMonoOutput)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 796 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 797 | } else { |
| 798 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 799 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 800 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 803 | status_t ThreadBase::sendCreateAudioPatchConfigEvent( |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 804 | const struct audio_patch *patch, |
| 805 | audio_patch_handle_t *handle) |
| 806 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 807 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 808 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 809 | status_t status = sendConfigEvent_l(configEvent); |
| 810 | if (status == NO_ERROR) { |
| 811 | CreateAudioPatchConfigEventData *data = |
| 812 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 813 | *handle = data->mHandle; |
| 814 | } |
| 815 | return status; |
| 816 | } |
| 817 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 818 | status_t ThreadBase::sendReleaseAudioPatchConfigEvent( |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 819 | const audio_patch_handle_t handle) |
| 820 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 821 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 822 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 823 | return sendConfigEvent_l(configEvent); |
| 824 | } |
| 825 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 826 | status_t ThreadBase::sendUpdateOutDeviceConfigEvent( |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 827 | const DeviceDescriptorBaseVector& outDevices) |
| 828 | { |
| 829 | if (type() != RECORD) { |
| 830 | // The update out device operation is only for record thread. |
| 831 | return INVALID_OPERATION; |
| 832 | } |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 833 | audio_utils::lock_guard _l(mutex()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 834 | sp<ConfigEvent> configEvent = (ConfigEvent *)new UpdateOutDevicesConfigEvent(outDevices); |
| 835 | return sendConfigEvent_l(configEvent); |
| 836 | } |
| 837 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 838 | void ThreadBase::sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 839 | { |
| 840 | ALOG_ASSERT(type() == RECORD, "sendResizeBufferConfigEvent_l() called on non record thread"); |
| 841 | sp<ConfigEvent> configEvent = |
| 842 | (ConfigEvent *)new ResizeBufferConfigEvent(maxSharedAudioHistoryMs); |
| 843 | sendConfigEvent_l(configEvent); |
| 844 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 845 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 846 | void ThreadBase::sendCheckOutputStageEffectsEvent() |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 847 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 848 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 849 | sendCheckOutputStageEffectsEvent_l(); |
| 850 | } |
| 851 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 852 | void ThreadBase::sendCheckOutputStageEffectsEvent_l() |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 853 | { |
| 854 | sp<ConfigEvent> configEvent = |
| 855 | (ConfigEvent *)new CheckOutputStageEffectsEvent(); |
| 856 | sendConfigEvent_l(configEvent); |
| 857 | } |
| 858 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 859 | void ThreadBase::sendHalLatencyModesChangedEvent_l() |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 860 | { |
| 861 | sp<ConfigEvent> configEvent = sp<HalLatencyModesChangedEvent>::make(); |
| 862 | sendConfigEvent_l(configEvent); |
| 863 | } |
| 864 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 865 | // post condition: mConfigEvents.isEmpty() |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 866 | void ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 867 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 868 | bool configChanged = false; |
| 869 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 870 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 871 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 872 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 873 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 874 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 875 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 876 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 877 | // FIXME Need to understand why this has to be done asynchronously |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 878 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 879 | true /*asynchronous*/); |
| 880 | if (err != 0) { |
| 881 | 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] | 882 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 883 | } |
| 884 | } break; |
| 885 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 886 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 887 | ioConfigChanged_l(data->mEvent, data->mPid, data->mPortId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 888 | } break; |
| 889 | case CFG_EVENT_SET_PARAMETER: { |
| 890 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 891 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 892 | configChanged = true; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 893 | mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed", |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 894 | data->mKeyValuePairs.c_str()); |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 895 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 896 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 897 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 898 | const DeviceTypeSet oldDevices = getDeviceTypes_l(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 899 | CreateAudioPatchConfigEventData *data = |
| 900 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 901 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 902 | const DeviceTypeSet newDevices = getDeviceTypes_l(); |
Eric Laurent | 5256814 | 2022-10-28 11:23:28 +0200 | [diff] [blame] | 903 | configChanged = oldDevices != newDevices; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 904 | mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", |
| 905 | dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), |
| 906 | dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 907 | } break; |
| 908 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 909 | const DeviceTypeSet oldDevices = getDeviceTypes_l(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 910 | ReleaseAudioPatchConfigEventData *data = |
| 911 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 912 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 913 | const DeviceTypeSet newDevices = getDeviceTypes_l(); |
Eric Laurent | 5256814 | 2022-10-28 11:23:28 +0200 | [diff] [blame] | 914 | configChanged = oldDevices != newDevices; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 915 | mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %s (%s) new device %s (%s)", |
| 916 | dumpDeviceTypes(oldDevices).c_str(), toString(oldDevices).c_str(), |
| 917 | dumpDeviceTypes(newDevices).c_str(), toString(newDevices).c_str()); |
| 918 | } break; |
| 919 | case CFG_EVENT_UPDATE_OUT_DEVICE: { |
| 920 | UpdateOutDevicesConfigEventData *data = |
| 921 | (UpdateOutDevicesConfigEventData *)event->mData.get(); |
| 922 | updateOutDevices(data->mOutDevices); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 923 | } break; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 924 | case CFG_EVENT_RESIZE_BUFFER: { |
| 925 | ResizeBufferConfigEventData *data = |
| 926 | (ResizeBufferConfigEventData *)event->mData.get(); |
| 927 | resizeInputBuffer_l(data->mMaxSharedAudioHistoryMs); |
| 928 | } break; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 929 | |
| 930 | case CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS: { |
| 931 | setCheckOutputStageEffects(); |
| 932 | } break; |
| 933 | |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 934 | case CFG_EVENT_HAL_LATENCY_MODES_CHANGED: { |
| 935 | onHalLatencyModesChanged_l(); |
| 936 | } break; |
| 937 | |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 938 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 939 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 940 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 941 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 942 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 943 | audio_utils::lock_guard _l(event->mutex()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 944 | if (event->mWaitStatus) { |
| 945 | event->mWaitStatus = false; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 946 | event->mCondition.notify_one(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 950 | } |
| 951 | |
| 952 | if (configChanged) { |
| 953 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 954 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 957 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 958 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 959 | const audio_channel_representation_t representation = |
| 960 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 961 | |
| 962 | switch (representation) { |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 963 | // Travel all single bit channel mask to convert channel mask to string. |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 964 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 965 | if (output) { |
| 966 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 967 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 968 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 969 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low-frequency, "); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 970 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 971 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 972 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 973 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 974 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 975 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 976 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 977 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 978 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 979 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 980 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 981 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 982 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, "); |
| 983 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, "); |
| 984 | if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, "); |
| 985 | if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, "); |
| 986 | if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_LEFT) s.append("bottom-front-left, "); |
| 987 | if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_CENTER) s.append("bottom-front-center, "); |
| 988 | if (mask & AUDIO_CHANNEL_OUT_BOTTOM_FRONT_RIGHT) s.append("bottom-front-right, "); |
Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 989 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2) s.append("low-frequency-2, "); |
Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 990 | if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, "); |
| 991 | if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, "); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 992 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 993 | } else { |
| 994 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 995 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 996 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 997 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 998 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 999 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 1000 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 1001 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 1002 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 1003 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 1004 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 1005 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
Mikhail Naganov | c994849 | 2018-05-10 17:25:28 -0700 | [diff] [blame] | 1006 | if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, "); |
| 1007 | if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, "); |
| 1008 | if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, "); |
Andy Hung | fba7125 | 2021-05-07 11:58:32 -0700 | [diff] [blame] | 1009 | if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low-frequency, "); |
Andy Hung | ae81b4c | 2021-05-07 08:54:28 -0700 | [diff] [blame] | 1010 | if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, "); |
| 1011 | if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, "); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1012 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 1013 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 1014 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 1015 | } |
| 1016 | const int len = s.length(); |
| 1017 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 1018 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1019 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 1020 | } |
| 1021 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1022 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 1023 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 1024 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 1025 | return s; |
| 1026 | default: |
| 1027 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 1028 | representation, audio_channel_mask_get_bits(mask)); |
| 1029 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1030 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1033 | void ThreadBase::dump(int fd, const Vector<String16>& args) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1034 | NO_THREAD_SAFETY_ANALYSIS // conditional try lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1035 | { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 1036 | dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input", |
| 1037 | this, mThreadName, getTid(), type(), threadTypeToString(type())); |
| 1038 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1039 | const bool locked = afutils::dumpTryLock(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1040 | if (!locked) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 1041 | dprintf(fd, " Thread may be deadlocked\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1044 | dumpBase_l(fd, args); |
| 1045 | dumpInternals_l(fd, args); |
| 1046 | dumpTracks_l(fd, args); |
| 1047 | dumpEffectChains_l(fd, args); |
| 1048 | |
| 1049 | if (locked) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1050 | mutex().unlock(); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | dprintf(fd, " Local log:\n"); |
| 1054 | mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */); |
Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1055 | |
| 1056 | // --all does the statistics |
| 1057 | bool dumpAll = false; |
| 1058 | for (const auto &arg : args) { |
| 1059 | if (arg == String16("--all")) { |
| 1060 | dumpAll = true; |
| 1061 | } |
| 1062 | } |
| 1063 | if (dumpAll || type() == SPATIALIZER) { |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1064 | const std::string sched = mThreadSnapshot.toString(); |
Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1065 | if (!sched.empty()) { |
| 1066 | (void)write(fd, sched.c_str(), sched.size()); |
| 1067 | } |
| 1068 | } |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1071 | void ThreadBase::dumpBase_l(int fd, const Vector<String16>& /* args */) |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1072 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1073 | dprintf(fd, " I/O handle: %d\n", mId); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1074 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1075 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1076 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 1077 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, |
| 1078 | IAfThreadBase::formatToString(mHALFormat).c_str()); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1079 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1080 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 1081 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 1082 | channelMaskToString(mChannelMask, mType != RECORD).c_str()); |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 1083 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, |
| 1084 | IAfThreadBase::formatToString(mFormat).c_str()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 1085 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1086 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1087 | size_t numConfig = mConfigEvents.size(); |
| 1088 | if (numConfig) { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1089 | const size_t SIZE = 256; |
| 1090 | char buffer[SIZE]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1091 | for (size_t i = 0; i < numConfig; i++) { |
| 1092 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1093 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1094 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1095 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1096 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1097 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1098 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 1099 | // 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] | 1100 | dprintf(fd, " Output devices: %s (%s)\n", |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1101 | dumpDeviceTypes(outDeviceTypes_l()).c_str(), toString(outDeviceTypes_l()).c_str()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1102 | dprintf(fd, " Input device: %#x (%s)\n", |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1103 | inDeviceType_l(), toString(inDeviceType_l()).c_str()); |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 1104 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1105 | |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1106 | // Dump timestamp statistics for the Thread types that support it. |
| 1107 | if (mType == RECORD |
| 1108 | || mType == MIXER |
| 1109 | || mType == DUPLICATING |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 1110 | || mType == DIRECT |
Andy Hung | 4b7f54f | 2022-04-28 17:45:28 -0700 | [diff] [blame] | 1111 | || mType == OFFLOAD |
| 1112 | || mType == SPATIALIZER) { |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1113 | dprintf(fd, " Timestamp stats: %s\n", mTimestampVerifier.toString().c_str()); |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1114 | dprintf(fd, " Timestamp corrected: %s\n", |
| 1115 | isTimestampCorrectionEnabled_l() ? "yes" : "no"); |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 1118 | if (mLastIoBeginNs > 0) { // MMAP may not set this |
| 1119 | dprintf(fd, " Last %s occurred (msecs): %lld\n", |
| 1120 | isOutput() ? "write" : "read", |
| 1121 | (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND); |
| 1122 | } |
| 1123 | |
| 1124 | if (mProcessTimeMs.getN() > 0) { |
| 1125 | dprintf(fd, " Process time ms stats: %s\n", mProcessTimeMs.toString().c_str()); |
| 1126 | } |
| 1127 | |
| 1128 | if (mIoJitterMs.getN() > 0) { |
| 1129 | dprintf(fd, " Hal %s jitter ms stats: %s\n", |
| 1130 | isOutput() ? "write" : "read", |
| 1131 | mIoJitterMs.toString().c_str()); |
| 1132 | } |
| 1133 | |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 1134 | if (mLatencyMs.getN() > 0) { |
| 1135 | dprintf(fd, " Threadloop %s latency stats: %s\n", |
| 1136 | isOutput() ? "write" : "read", |
| 1137 | mLatencyMs.toString().c_str()); |
| 1138 | } |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 1139 | |
| 1140 | if (mMonopipePipeDepthStats.getN() > 0) { |
| 1141 | dprintf(fd, " Monopipe %s pipe depth stats: %s\n", |
| 1142 | isOutput() ? "write" : "read", |
| 1143 | mMonopipePipeDepthStats.toString().c_str()); |
| 1144 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1147 | void ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1148 | { |
| 1149 | const size_t SIZE = 256; |
| 1150 | char buffer[SIZE]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1151 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1152 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 1153 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1154 | write(fd, buffer, strlen(buffer)); |
| 1155 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1156 | for (size_t i = 0; i < numEffectChains; ++i) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1157 | sp<IAfEffectChain> chain = mEffectChains[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1158 | if (chain != 0) { |
| 1159 | chain->dump(fd, args); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1164 | void ThreadBase::acquireWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1165 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1166 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1167 | acquireWakeLock_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1170 | String16 ThreadBase::getWakeLockTag() |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1171 | { |
| 1172 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1173 | case MIXER: |
| 1174 | return String16("AudioMix"); |
| 1175 | case DIRECT: |
| 1176 | return String16("AudioDirectOut"); |
| 1177 | case DUPLICATING: |
| 1178 | return String16("AudioDup"); |
| 1179 | case RECORD: |
| 1180 | return String16("AudioIn"); |
| 1181 | case OFFLOAD: |
| 1182 | return String16("AudioOffload"); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1183 | case MMAP_PLAYBACK: |
| 1184 | return String16("MmapPlayback"); |
| 1185 | case MMAP_CAPTURE: |
| 1186 | return String16("MmapCapture"); |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1187 | case SPATIALIZER: |
| 1188 | return String16("AudioSpatial"); |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 1189 | default: |
| 1190 | ALOG_ASSERT(false); |
| 1191 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1195 | void ThreadBase::acquireWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1196 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1197 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1198 | if (mPowerManager != 0) { |
| 1199 | sp<IBinder> binder = new BBinder(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1200 | // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids. |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1201 | binder::Status status = mPowerManager->acquireWakeLockAsync(binder, |
| 1202 | POWERMANAGER_PARTIAL_WAKE_LOCK, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1203 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1204 | String16("audioserver"), |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1205 | {} /* workSource */, |
| 1206 | {} /* historyTag */); |
| 1207 | if (status.isOk()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1208 | mWakeLockToken = binder; |
| 1209 | } |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1210 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status.exceptionCode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1211 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1212 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1213 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1214 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 1215 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1218 | void ThreadBase::releaseWakeLock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1219 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1220 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1221 | releaseWakeLock_l(); |
| 1222 | } |
| 1223 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1224 | void ThreadBase::releaseWakeLock_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1225 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1226 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1227 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1228 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1229 | if (mPowerManager != 0) { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1230 | mPowerManager->releaseWakeLockAsync(mWakeLockToken, 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1231 | } |
| 1232 | mWakeLockToken.clear(); |
| 1233 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1236 | void ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1237 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1238 | // use checkService() to avoid blocking if power service is not up yet |
| 1239 | sp<IBinder> binder = |
| 1240 | defaultServiceManager()->checkService(String16("power")); |
| 1241 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1242 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1243 | } else { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 1244 | mPowerManager = interface_cast<os::IPowerManager>(binder); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1245 | binder->linkToDeath(mDeathRecipient); |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1250 | void ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t>& uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1251 | getPowerManager_l(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1252 | |
| 1253 | #if !LOG_NDEBUG |
| 1254 | std::stringstream s; |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1255 | for (uid_t uid : uids) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1256 | s << uid << " "; |
| 1257 | } |
| 1258 | ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str()); |
| 1259 | #endif |
| 1260 | |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1261 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1262 | if (mSystemReady) { |
| 1263 | ALOGE("no wake lock to update, but system ready!"); |
| 1264 | } else { |
| 1265 | ALOGW("no wake lock to update, system not ready yet"); |
| 1266 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | if (mPowerManager != 0) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1270 | 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] | 1271 | binder::Status status = mPowerManager->updateWakeLockUidsAsync( |
| 1272 | mWakeLockToken, uidsAsInt); |
| 1273 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status.exceptionCode()); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1277 | void ThreadBase::clearPowerManager() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1278 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1279 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1280 | releaseWakeLock_l(); |
| 1281 | mPowerManager.clear(); |
| 1282 | } |
| 1283 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1284 | void ThreadBase::updateOutDevices( |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1285 | const DeviceDescriptorBaseVector& outDevices __unused) |
| 1286 | { |
| 1287 | ALOGE("%s should only be called in RecordThread", __func__); |
| 1288 | } |
| 1289 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1290 | void ThreadBase::resizeInputBuffer_l(int32_t /* maxSharedAudioHistoryMs */) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1291 | { |
| 1292 | ALOGE("%s should only be called in RecordThread", __func__); |
| 1293 | } |
| 1294 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1295 | void ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& /* who */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1296 | { |
| 1297 | sp<ThreadBase> thread = mThread.promote(); |
| 1298 | if (thread != 0) { |
| 1299 | thread->clearPowerManager(); |
| 1300 | } |
| 1301 | ALOGW("power manager service died !!!"); |
| 1302 | } |
| 1303 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1304 | void ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1305 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1306 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1307 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1308 | if (chain != 0) { |
| 1309 | if (type != NULL) { |
| 1310 | chain->setEffectSuspended_l(type, suspend); |
| 1311 | } else { |
| 1312 | chain->setEffectSuspendedAll_l(suspend); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1317 | } |
| 1318 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1319 | void ThreadBase::checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1320 | { |
| 1321 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1322 | if (index < 0) { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1327 | mSuspendedSessions.valueAt(index); |
| 1328 | |
| 1329 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1330 | const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1331 | for (int j = 0; j < desc->mRefCount; j++) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1332 | if (sessionEffects.keyAt(i) == IAfEffectChain::kKeyForSuspendAll) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1333 | chain->setEffectSuspendedAll_l(true); |
| 1334 | } else { |
| 1335 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1336 | desc->mType.timeLow); |
| 1337 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1343 | void ThreadBase::updateSuspendedSessions_l(const effect_uuid_t* type, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1344 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1345 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1346 | { |
| 1347 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1348 | |
| 1349 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1350 | |
| 1351 | if (suspend) { |
| 1352 | if (index >= 0) { |
| 1353 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1354 | } else { |
| 1355 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1356 | } |
| 1357 | } else { |
| 1358 | if (index < 0) { |
| 1359 | return; |
| 1360 | } |
| 1361 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1362 | } |
| 1363 | |
| 1364 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1365 | int key = IAfEffectChain::kKeyForSuspendAll; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1366 | if (type != NULL) { |
| 1367 | key = type->timeLow; |
| 1368 | } |
| 1369 | index = sessionEffects.indexOfKey(key); |
| 1370 | |
| 1371 | sp<SuspendedSessionDesc> desc; |
| 1372 | if (suspend) { |
| 1373 | if (index >= 0) { |
| 1374 | desc = sessionEffects.valueAt(index); |
| 1375 | } else { |
| 1376 | desc = new SuspendedSessionDesc(); |
| 1377 | if (type != NULL) { |
| 1378 | desc->mType = *type; |
| 1379 | } |
| 1380 | sessionEffects.add(key, desc); |
| 1381 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1382 | } |
| 1383 | desc->mRefCount++; |
| 1384 | } else { |
| 1385 | if (index < 0) { |
| 1386 | return; |
| 1387 | } |
| 1388 | desc = sessionEffects.valueAt(index); |
| 1389 | if (--desc->mRefCount == 0) { |
| 1390 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1391 | sessionEffects.removeItemsAt(index); |
| 1392 | if (sessionEffects.isEmpty()) { |
| 1393 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1394 | sessionId); |
| 1395 | mSuspendedSessions.removeItem(sessionId); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | if (!sessionEffects.isEmpty()) { |
| 1400 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1401 | } |
| 1402 | } |
| 1403 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1404 | void ThreadBase::checkSuspendOnEffectEnabled(bool enabled, |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1405 | audio_session_t sessionId, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1406 | bool threadLocked) |
| 1407 | NO_THREAD_SAFETY_ANALYSIS // manual locking |
| 1408 | { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1409 | if (!threadLocked) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1410 | mutex().lock(); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1411 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1412 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1413 | if (mType != RECORD) { |
| 1414 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1415 | // another session. This gives the priority to well behaved effect control panels |
| 1416 | // and applications not using global effects. |
| 1417 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1418 | // global effects |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1419 | if (!audio_is_global_session(sessionId)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1420 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1421 | } |
| 1422 | } |
| 1423 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1424 | if (!threadLocked) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1425 | mutex().unlock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1429 | // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1430 | status_t RecordThread::checkEffectCompatibility_l( |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1431 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1432 | { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1433 | // No global output effect sessions on record threads |
| 1434 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX |
| 1435 | || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1436 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1437 | desc->name, mThreadName); |
| 1438 | return BAD_VALUE; |
| 1439 | } |
| 1440 | // only pre processing effects on record thread |
| 1441 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1442 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1443 | desc->name, mThreadName); |
| 1444 | return BAD_VALUE; |
| 1445 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1446 | |
| 1447 | // always allow effects without processing load or latency |
| 1448 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1449 | return NO_ERROR; |
| 1450 | } |
| 1451 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1452 | audio_input_flags_t flags = mInput->flags; |
| 1453 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1454 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1455 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1456 | desc->name, mThreadName); |
| 1457 | return BAD_VALUE; |
| 1458 | } |
| 1459 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1460 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1461 | desc->name, mThreadName); |
| 1462 | return BAD_VALUE; |
| 1463 | } |
| 1464 | } |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1465 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1466 | if (IAfEffectModule::isHapticGenerator(&desc->type)) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1467 | ALOGE("%s(): HapticGenerator is not supported in RecordThread", __func__); |
| 1468 | return BAD_VALUE; |
| 1469 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1470 | return NO_ERROR; |
| 1471 | } |
| 1472 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1473 | // checkEffectCompatibility_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1474 | status_t PlaybackThread::checkEffectCompatibility_l( |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1475 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1476 | { |
| 1477 | // no preprocessing on playback threads |
| 1478 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1479 | ALOGW("%s: pre processing effect %s created on playback" |
| 1480 | " thread %s", __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1481 | return BAD_VALUE; |
| 1482 | } |
| 1483 | |
Eric Laurent | 3e4de77 | 2017-07-16 16:55:08 -0700 | [diff] [blame] | 1484 | // always allow effects without processing load or latency |
| 1485 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1486 | return NO_ERROR; |
| 1487 | } |
| 1488 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1489 | if (IAfEffectModule::isHapticGenerator(&desc->type) && mHapticChannelCount == 0) { |
Shunkai Yao | 4c3af93 | 2024-04-26 04:12:21 +0000 | [diff] [blame] | 1490 | ALOGW("%s: thread (%s) doesn't support haptic playback while the effect is HapticGenerator", |
| 1491 | __func__, threadTypeToString(mType)); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1492 | return BAD_VALUE; |
| 1493 | } |
| 1494 | |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 1495 | if (IAfEffectModule::isSpatializer(&desc->type) |
Eric Laurent | f690c46 | 2021-09-17 14:47:03 +0200 | [diff] [blame] | 1496 | && mType != SPATIALIZER) { |
| 1497 | ALOGW("%s: attempt to create a spatializer effect on a thread of type %d", |
| 1498 | __func__, mType); |
| 1499 | return BAD_VALUE; |
| 1500 | } |
| 1501 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1502 | switch (mType) { |
| 1503 | case MIXER: { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1504 | audio_output_flags_t flags = mOutput->flags; |
| 1505 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1506 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1507 | // global effects are applied only to non fast tracks if they are SW |
| 1508 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1509 | break; |
| 1510 | } |
| 1511 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1512 | // only post processing on output stage session |
| 1513 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1514 | ALOGW("%s: non post processing effect %s not allowed on output stage session", |
| 1515 | __func__, desc->name); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1516 | return BAD_VALUE; |
| 1517 | } |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1518 | } else if (sessionId == AUDIO_SESSION_DEVICE) { |
| 1519 | // only post processing on output stage session |
| 1520 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1521 | ALOGW("%s: non post processing effect %s not allowed on device session", |
| 1522 | __func__, desc->name); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1523 | return BAD_VALUE; |
| 1524 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1525 | } else { |
| 1526 | // no restriction on effects applied on non fast tracks |
| 1527 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1528 | break; |
| 1529 | } |
| 1530 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1531 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1532 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1533 | ALOGW("%s: effect %s on playback thread in raw mode", __func__, desc->name); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1534 | return BAD_VALUE; |
| 1535 | } |
| 1536 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1537 | ALOGW("%s: non HW effect %s on playback thread in fast mode", |
| 1538 | __func__, desc->name); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1539 | return BAD_VALUE; |
| 1540 | } |
| 1541 | } |
| 1542 | } break; |
| 1543 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1544 | // nothing actionable on offload threads, if the effect: |
| 1545 | // - is offloadable: the effect can be created |
| 1546 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1547 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1548 | break; |
| 1549 | case DIRECT: |
| 1550 | // Reject any effect on Direct output threads for now, since the format of |
| 1551 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1552 | ALOGW("%s: effect %s on DIRECT output thread %s", |
| 1553 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1554 | return BAD_VALUE; |
| 1555 | case DUPLICATING: |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 1556 | if (audio_is_global_session(sessionId)) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1557 | ALOGW("%s: global effect %s on DUPLICATING thread %s", |
| 1558 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1559 | return BAD_VALUE; |
| 1560 | } |
| 1561 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1562 | ALOGW("%s: post processing effect %s on DUPLICATING thread %s", |
| 1563 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1564 | return BAD_VALUE; |
| 1565 | } |
| 1566 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1567 | ALOGW("%s: HW tunneled effect %s on DUPLICATING thread %s", |
| 1568 | __func__, desc->name, mThreadName); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1569 | return BAD_VALUE; |
| 1570 | } |
| 1571 | break; |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1572 | case SPATIALIZER: |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1573 | // Global effects (AUDIO_SESSION_OUTPUT_MIX) are not supported on spatializer mixer |
| 1574 | // as there is no common accumulation buffer for sptialized and non sptialized tracks. |
| 1575 | // Post processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) |
| 1576 | // are supported and added after the spatializer. |
| 1577 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1578 | ALOGW("%s: global effect %s not supported on spatializer thread %s", |
| 1579 | __func__, desc->name, mThreadName); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1580 | return BAD_VALUE; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1581 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1582 | // only post processing , downmixer or spatializer effects on output stage session |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 1583 | if (IAfEffectModule::isSpatializer(&desc->type) |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1584 | || memcmp(&desc->type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { |
| 1585 | break; |
| 1586 | } |
| 1587 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1588 | ALOGW("%s: non post processing effect %s not allowed on output stage session", |
| 1589 | __func__, desc->name); |
| 1590 | return BAD_VALUE; |
| 1591 | } |
| 1592 | } else if (sessionId == AUDIO_SESSION_DEVICE) { |
| 1593 | // only post processing on output stage session |
| 1594 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1595 | ALOGW("%s: non post processing effect %s not allowed on device session", |
| 1596 | __func__, desc->name); |
| 1597 | return BAD_VALUE; |
| 1598 | } |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1599 | } |
| 1600 | break; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1601 | case BIT_PERFECT: |
| 1602 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1603 | // Allow HW accelerated effects of tunnel type |
| 1604 | break; |
| 1605 | } |
| 1606 | // As bit-perfect tracks will not be allowed to apply audio effect that will touch the audio |
| 1607 | // data, effects will not be allowed on 1) global effects (AUDIO_SESSION_OUTPUT_MIX), |
| 1608 | // 2) post-processing effects (AUDIO_SESSION_OUTPUT_STAGE or AUDIO_SESSION_DEVICE) and |
| 1609 | // 3) there is any bit-perfect track with the given session id. |
| 1610 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE || |
| 1611 | sessionId == AUDIO_SESSION_DEVICE) { |
| 1612 | ALOGW("%s: effect %s not supported on bit-perfect thread %s", |
| 1613 | __func__, desc->name, mThreadName); |
| 1614 | return BAD_VALUE; |
| 1615 | } else if ((hasAudioSession_l(sessionId) & ThreadBase::BIT_PERFECT_SESSION) != 0) { |
| 1616 | ALOGW("%s: effect %s not supported as there is a bit-perfect track with session as %d", |
| 1617 | __func__, desc->name, sessionId); |
| 1618 | return BAD_VALUE; |
| 1619 | } |
| 1620 | break; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1621 | default: |
| 1622 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1623 | } |
| 1624 | |
| 1625 | return NO_ERROR; |
| 1626 | } |
| 1627 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1628 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1629 | sp<IAfEffectHandle> ThreadBase::createEffect_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 1630 | const sp<Client>& client, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1631 | const sp<IEffectClient>& effectClient, |
| 1632 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1633 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1634 | effect_descriptor_t *desc, |
| 1635 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1636 | status_t *status, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1637 | bool pinned, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 1638 | bool probe, |
| 1639 | bool notifyFramesProcessed) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1640 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1641 | sp<IAfEffectModule> effect; |
| 1642 | sp<IAfEffectHandle> handle; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1643 | status_t lStatus; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1644 | sp<IAfEffectChain> chain; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1645 | bool chainCreated = false; |
| 1646 | bool effectCreated = false; |
Mikhail Naganov | 2247f7b | 2017-01-13 11:52:54 -0800 | [diff] [blame] | 1647 | audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1648 | |
| 1649 | lStatus = initCheck(); |
| 1650 | if (lStatus != NO_ERROR) { |
| 1651 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1652 | goto Exit; |
| 1653 | } |
| 1654 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1655 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1656 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1657 | { // scope for mutex() |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1658 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1660 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1661 | if (probe || lStatus != NO_ERROR) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1662 | goto Exit; |
| 1663 | } |
| 1664 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1665 | // check for existing effect chain with the requested audio session |
| 1666 | chain = getEffectChain_l(sessionId); |
| 1667 | if (chain == 0) { |
| 1668 | // create a new chain for this session |
| 1669 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1670 | chain = IAfEffectChain::create(this, sessionId, mAfThreadCallback); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1671 | addEffectChain_l(chain); |
| 1672 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1673 | chainCreated = true; |
| 1674 | } else { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1675 | effect = chain->getEffectFromDesc(desc); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1679 | |
| 1680 | if (effect == 0) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1681 | effectId = mAfThreadCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1682 | // create a new effect module if none present in the chain |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1683 | lStatus = chain->createEffect(effect, desc, effectId, sessionId, pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1684 | if (lStatus != NO_ERROR) { |
| 1685 | goto Exit; |
| 1686 | } |
| 1687 | effectCreated = true; |
| 1688 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1689 | // FIXME: use vector of device and address when effect interface is ready. |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1690 | effect->setDevices(outDeviceTypeAddrs()); |
| 1691 | effect->setInputDevice(inDeviceTypeAddr()); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1692 | effect->setMode(mAfThreadCallback->getMode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1693 | effect->setAudioSource(mAudioSource); |
| 1694 | } |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1695 | if (effect->isHapticGenerator()) { |
| 1696 | // TODO(b/184194057): Use the vibrator information from the vibrator that will be used |
| 1697 | // for the HapticGenerator. |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 1698 | const std::optional<media::AudioVibratorInfo> defaultVibratorInfo = |
Yi Kong | 3ac211f | 2024-08-12 07:31:44 +0800 | [diff] [blame] | 1699 | mAfThreadCallback->getDefaultVibratorInfo_l(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 1700 | if (defaultVibratorInfo) { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1701 | audio_utils::lock_guard _cl(chain->mutex()); |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1702 | // Only set the vibrator info when it is a valid one. |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1703 | effect->setVibratorInfo_l(*defaultVibratorInfo); |
jiabin | 1319f5a | 2021-03-30 22:21:24 +0000 | [diff] [blame] | 1704 | } |
| 1705 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1706 | // create effect handle and connect it to effect module |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1707 | handle = IAfEffectHandle::create( |
| 1708 | effect, client, effectClient, priority, notifyFramesProcessed); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1709 | lStatus = handle->initCheck(); |
| 1710 | if (lStatus == OK) { |
| 1711 | lStatus = effect->addHandle(handle.get()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1712 | sendCheckOutputStageEffectsEvent_l(); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1713 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1714 | if (enabled != NULL) { |
| 1715 | *enabled = (int)effect->isEnabled(); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | Exit: |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 1720 | if (!probe && lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1721 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1722 | if (effectCreated) { |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1723 | chain->removeEffect(effect); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1724 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | if (chainCreated) { |
| 1726 | removeEffectChain_l(chain); |
| 1727 | } |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 1728 | // handle must be cleared by caller to avoid deadlock. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1731 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1732 | return handle; |
| 1733 | } |
| 1734 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1735 | void ThreadBase::disconnectEffectHandle(IAfEffectHandle* handle, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1736 | bool unpinIfLast) |
| 1737 | { |
| 1738 | bool remove = false; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1739 | sp<IAfEffectModule> effect; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1740 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1741 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1742 | sp<IAfEffectBase> effectBase = handle->effect().promote(); |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1743 | if (effectBase == nullptr) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1744 | return; |
| 1745 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1746 | effect = effectBase->asEffectModule(); |
| 1747 | if (effect == nullptr) { |
| 1748 | return; |
| 1749 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1750 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 1751 | remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 1752 | if (remove) { |
| 1753 | removeEffect_l(effect, true); |
| 1754 | } |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1755 | sendCheckOutputStageEffectsEvent_l(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1756 | } |
| 1757 | if (remove) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1758 | mAfThreadCallback->updateOrphanEffectChains(effect); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1759 | if (handle->enabled()) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1760 | effect->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1765 | void ThreadBase::onEffectEnable(const sp<IAfEffectModule>& effect) { |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1766 | if (isOffloadOrMmap()) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1767 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1768 | broadcast_l(); |
| 1769 | } |
| 1770 | if (!effect->isOffloadable()) { |
| 1771 | if (mType == ThreadBase::OFFLOAD) { |
| 1772 | PlaybackThread *t = (PlaybackThread *)this; |
| 1773 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 1774 | } |
| 1775 | if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1776 | mAfThreadCallback->onNonOffloadableGlobalEffectEnable(); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1781 | void ThreadBase::onEffectDisable() { |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 1782 | if (isOffloadOrMmap()) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1783 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1784 | broadcast_l(); |
| 1785 | } |
| 1786 | } |
| 1787 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1788 | sp<IAfEffectModule> ThreadBase::getEffect(audio_session_t sessionId, |
Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1789 | int effectId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1790 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1791 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1792 | return getEffect_l(sessionId, effectId); |
| 1793 | } |
| 1794 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1795 | sp<IAfEffectModule> ThreadBase::getEffect_l(audio_session_t sessionId, |
Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1796 | int effectId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1797 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1798 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1799 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1800 | } |
| 1801 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1802 | std::vector<int> ThreadBase::getEffectIds_l(audio_session_t sessionId) const |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1803 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1804 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1805 | return chain != nullptr ? chain->getEffectIds_l() : std::vector<int>{}; |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1808 | // PlaybackThread::addEffect_ll() must be called with AudioFlinger::mutex() and |
| 1809 | // ThreadBase::mutex() held |
| 1810 | status_t ThreadBase::addEffect_ll(const sp<IAfEffectModule>& effect) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1811 | { |
| 1812 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1813 | audio_session_t sessionId = effect->sessionId(); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1814 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1815 | bool chainCreated = false; |
| 1816 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1817 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1818 | "%s: on offloaded thread %p: effect %s does not support offload flags %#x", |
| 1819 | __func__, this, effect->desc().name, effect->desc().flags); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1820 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1821 | if (chain == 0) { |
| 1822 | // create a new chain for this session |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1823 | ALOGV("%s: new effect chain for session %d", __func__, sessionId); |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1824 | chain = IAfEffectChain::create(this, sessionId, mAfThreadCallback); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1825 | addEffectChain_l(chain); |
| 1826 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1827 | chainCreated = true; |
| 1828 | } |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1829 | ALOGV("%s: %p chain %p effect %p", __func__, this, chain.get(), effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1830 | |
| 1831 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1832 | ALOGW("%s: %p effect %s already present in chain %p", |
| 1833 | __func__, this, effect->desc().name, chain.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1834 | return BAD_VALUE; |
| 1835 | } |
| 1836 | |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 1837 | effect->setOffloaded_l(mType == OFFLOAD, mId); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1838 | |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1839 | status_t status = chain->addEffect(effect); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1840 | if (status != NO_ERROR) { |
| 1841 | if (chainCreated) { |
| 1842 | removeEffectChain_l(chain); |
| 1843 | } |
| 1844 | return status; |
| 1845 | } |
| 1846 | |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1847 | effect->setDevices(outDeviceTypeAddrs()); |
| 1848 | effect->setInputDevice(inDeviceTypeAddr()); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1849 | effect->setMode(mAfThreadCallback->getMode()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1850 | effect->setAudioSource(mAudioSource); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1851 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1852 | return NO_ERROR; |
| 1853 | } |
| 1854 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1855 | void ThreadBase::removeEffect_l(const sp<IAfEffectModule>& effect, bool release) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1856 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1857 | ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1858 | effect_descriptor_t desc = effect->desc(); |
| 1859 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1860 | detachAuxEffect_l(effect->id()); |
| 1861 | } |
| 1862 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1863 | sp<IAfEffectChain> chain = effect->getCallback()->chain().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1864 | if (chain != 0) { |
| 1865 | // remove effect chain if removing last effect |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 1866 | if (chain->removeEffect(effect, release) == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | removeEffectChain_l(chain); |
| 1868 | } |
| 1869 | } else { |
| 1870 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1871 | } |
| 1872 | } |
| 1873 | |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1874 | void ThreadBase::lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) |
| 1875 | NO_THREAD_SAFETY_ANALYSIS // calls EffectChain::lock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1876 | { |
| 1877 | effectChains = mEffectChains; |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1878 | for (const auto& effectChain : effectChains) { |
| 1879 | effectChain->mutex().lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1883 | void ThreadBase::unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) |
| 1884 | NO_THREAD_SAFETY_ANALYSIS // calls EffectChain::unlock() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1885 | { |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 1886 | for (const auto& effectChain : effectChains) { |
| 1887 | effectChain->mutex().unlock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1891 | sp<IAfEffectChain> ThreadBase::getEffectChain(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1892 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1893 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1894 | return getEffectChain_l(sessionId); |
| 1895 | } |
| 1896 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1897 | sp<IAfEffectChain> ThreadBase::getEffectChain_l(audio_session_t sessionId) |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1898 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1899 | { |
| 1900 | size_t size = mEffectChains.size(); |
| 1901 | for (size_t i = 0; i < size; i++) { |
| 1902 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1903 | return mEffectChains[i]; |
| 1904 | } |
| 1905 | } |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1909 | void ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1910 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1911 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1912 | size_t size = mEffectChains.size(); |
| 1913 | for (size_t i = 0; i < size; i++) { |
| 1914 | mEffectChains[i]->setMode_l(mode); |
| 1915 | } |
| 1916 | } |
| 1917 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1918 | void ThreadBase::toAudioPortConfig(struct audio_port_config* config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1919 | { |
| 1920 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1921 | config->ext.mix.handle = mId; |
| 1922 | config->sample_rate = mSampleRate; |
Mikhail Naganov | 88d54da | 2023-09-11 11:53:50 -0700 | [diff] [blame] | 1923 | config->format = mHALFormat; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1924 | config->channel_mask = mChannelMask; |
| 1925 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1926 | AUDIO_PORT_CONFIG_FORMAT; |
| 1927 | } |
| 1928 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1929 | void ThreadBase::systemReady() |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1930 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1931 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1932 | if (mSystemReady) { |
| 1933 | return; |
| 1934 | } |
| 1935 | mSystemReady = true; |
| 1936 | |
| 1937 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1938 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1939 | } |
| 1940 | mPendingConfigEvents.clear(); |
| 1941 | } |
| 1942 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1943 | template <typename T> |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1944 | ssize_t ThreadBase::ActiveTracks<T>::add(const sp<T>& track) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1945 | ssize_t index = mActiveTracks.indexOf(track); |
| 1946 | if (index >= 0) { |
| 1947 | ALOGW("ActiveTracks<T>::add track %p already there", track.get()); |
| 1948 | return index; |
| 1949 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1950 | logTrack("add", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1951 | mActiveTracksGeneration++; |
| 1952 | mLatestActiveTrack = track; |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1953 | track->beginBatteryAttribution(); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1954 | mHasChanged = true; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1955 | return mActiveTracks.add(track); |
| 1956 | } |
| 1957 | |
| 1958 | template <typename T> |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1959 | ssize_t ThreadBase::ActiveTracks<T>::remove(const sp<T>& track) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1960 | ssize_t index = mActiveTracks.remove(track); |
| 1961 | if (index < 0) { |
| 1962 | ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get()); |
| 1963 | return index; |
| 1964 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1965 | logTrack("remove", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1966 | mActiveTracksGeneration++; |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1967 | track->endBatteryAttribution(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1968 | // mLatestActiveTrack is not cleared even if is the same as track. |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1969 | mHasChanged = true; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 1970 | #ifdef TEE_SINK |
| 1971 | track->dumpTee(-1 /* fd */, "_REMOVE"); |
| 1972 | #endif |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 1973 | track->logEndInterval(); // log to MediaMetrics |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1974 | return index; |
| 1975 | } |
| 1976 | |
| 1977 | template <typename T> |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1978 | void ThreadBase::ActiveTracks<T>::clear() { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1979 | for (const sp<T> &track : mActiveTracks) { |
Atneya Nair | 166663a | 2023-06-27 19:16:24 -0700 | [diff] [blame] | 1980 | track->endBatteryAttribution(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1981 | logTrack("clear", track); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1982 | } |
| 1983 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1984 | if (!mActiveTracks.empty()) { mHasChanged = true; } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1985 | mActiveTracks.clear(); |
| 1986 | mLatestActiveTrack.clear(); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | template <typename T> |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1990 | void ThreadBase::ActiveTracks<T>::updatePowerState_l( |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1991 | const sp<ThreadBase>& thread, bool force) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1992 | // Updates ActiveTracks client uids to the thread wakelock. |
| 1993 | if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { |
| 1994 | thread->updateWakeLockUids_l(getWakeLockUids()); |
| 1995 | mLastActiveTracksGeneration = mActiveTracksGeneration; |
| 1996 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1997 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1998 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1999 | template <typename T> |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2000 | bool ThreadBase::ActiveTracks<T>::readAndClearHasChanged() { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2001 | bool hasChanged = mHasChanged; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2002 | mHasChanged = false; |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2003 | |
| 2004 | for (const sp<T> &track : mActiveTracks) { |
| 2005 | // Do not short-circuit as all hasChanged states must be reset |
| 2006 | // as all the metadata are going to be sent |
| 2007 | hasChanged |= track->readAndClearHasChanged(); |
| 2008 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2009 | return hasChanged; |
| 2010 | } |
| 2011 | |
| 2012 | template <typename T> |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2013 | void ThreadBase::ActiveTracks<T>::logTrack( |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2014 | const char *funcName, const sp<T> &track) const { |
| 2015 | if (mLocalLog != nullptr) { |
| 2016 | String8 result; |
| 2017 | track->appendDump(result, false /* active */); |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2018 | mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.c_str()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2019 | } |
| 2020 | } |
| 2021 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2022 | void ThreadBase::broadcast_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2023 | { |
| 2024 | // Thread could be blocked waiting for async |
| 2025 | // so signal it to handle state changes immediately |
| 2026 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 2027 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 2028 | mSignalPending = true; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2029 | mWaitWorkCV.notify_all(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2032 | // Call only from threadLoop() or when it is idle. |
| 2033 | // Do not call from high performance code as this may do binder rpc to the MediaMetrics service. |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2034 | void ThreadBase::sendStatistics(bool force) |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2035 | NO_THREAD_SAFETY_ANALYSIS |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2036 | { |
| 2037 | // Do not log if we have no stats. |
| 2038 | // We choose the timestamp verifier because it is the most likely item to be present. |
| 2039 | const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN; |
| 2040 | if (nstats == 0) { |
| 2041 | return; |
| 2042 | } |
| 2043 | |
| 2044 | // Don't log more frequently than once per 12 hours. |
| 2045 | // We use BOOTTIME to include suspend time. |
| 2046 | const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME); |
| 2047 | const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0 |
| 2048 | if (!force && sinceNs <= 12 * NANOS_PER_HOUR) { |
| 2049 | return; |
| 2050 | } |
| 2051 | |
| 2052 | mLastRecordedTimestampVerifierN = mTimestampVerifier.getN(); |
| 2053 | mLastRecordedTimeNs = timeNs; |
| 2054 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 2055 | std::unique_ptr<mediametrics::Item> item(mediametrics::Item::create("audiothread")); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2056 | |
| 2057 | #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors. |
| 2058 | |
| 2059 | // thread configuration |
| 2060 | item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle |
| 2061 | // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId); |
| 2062 | item->setCString(MM_PREFIX "type", threadTypeToString(mType)); |
| 2063 | item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate); |
| 2064 | item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask); |
| 2065 | item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str()); |
| 2066 | item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount); |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2067 | item->setCString(MM_PREFIX "outDevice", toString(outDeviceTypes_l()).c_str()); |
| 2068 | item->setCString(MM_PREFIX "inDevice", toString(inDeviceType_l()).c_str()); |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2069 | |
| 2070 | // thread statistics |
| 2071 | if (mIoJitterMs.getN() > 0) { |
| 2072 | item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean()); |
| 2073 | item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev()); |
| 2074 | } |
| 2075 | if (mProcessTimeMs.getN() > 0) { |
| 2076 | item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean()); |
| 2077 | item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev()); |
| 2078 | } |
| 2079 | const auto tsjitter = mTimestampVerifier.getJitterMs(); |
| 2080 | if (tsjitter.getN() > 0) { |
| 2081 | item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean()); |
| 2082 | item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev()); |
| 2083 | } |
| 2084 | if (mLatencyMs.getN() > 0) { |
| 2085 | item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean()); |
| 2086 | item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev()); |
| 2087 | } |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 2088 | if (mMonopipePipeDepthStats.getN() > 0) { |
| 2089 | item->setDouble(MM_PREFIX "monopipePipeDepthStats.mean", |
| 2090 | mMonopipePipeDepthStats.getMean()); |
| 2091 | item->setDouble(MM_PREFIX "monopipePipeDepthStats.std", |
| 2092 | mMonopipePipeDepthStats.getStdDev()); |
| 2093 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 2094 | |
| 2095 | item->selfrecord(); |
| 2096 | } |
| 2097 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2098 | product_strategy_t ThreadBase::getStrategyForStream(audio_stream_type_t stream) const |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2099 | { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2100 | if (!mAfThreadCallback->isAudioPolicyReady()) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2101 | return PRODUCT_STRATEGY_NONE; |
| 2102 | } |
| 2103 | return AudioSystem::getStrategyForStream(stream); |
| 2104 | } |
| 2105 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2106 | // startMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2107 | void ThreadBase::startMelComputation_l( |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2108 | const sp<audio_utils::MelProcessor>& /*processor*/) |
| 2109 | { |
| 2110 | // Do nothing |
| 2111 | ALOGW("%s: ThreadBase does not support CSD", __func__); |
| 2112 | } |
| 2113 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2114 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2115 | void ThreadBase::stopMelComputation_l() |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2116 | { |
| 2117 | // Do nothing |
| 2118 | ALOGW("%s: ThreadBase does not support CSD", __func__); |
| 2119 | } |
| 2120 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2121 | // ---------------------------------------------------------------------------- |
| 2122 | // Playback |
| 2123 | // ---------------------------------------------------------------------------- |
| 2124 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2125 | PlaybackThread::PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2126 | AudioStreamOut* output, |
| 2127 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2128 | type_t type, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2129 | bool systemReady, |
| 2130 | audio_config_base_t *mixerConfig) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2131 | : ThreadBase(afThreadCallback, id, type, systemReady, true /* isOut */), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2132 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 2133 | mMixerBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2134 | mMixerBuffer(NULL), |
| 2135 | mMixerBufferSize(0), |
| 2136 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 2137 | mMixerBufferValid(false), |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 2138 | mEffectBufferEnabled(kEnableExtendedPrecision || type == SPATIALIZER), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2139 | mEffectBuffer(NULL), |
| 2140 | mEffectBufferSize(0), |
| 2141 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 2142 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 2143 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 2144 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 2145 | mSuspendedFrames(0), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2146 | mActiveTracks(&this->mLocalLog), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2147 | // mStreamTypes[] initialized in constructor body |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2148 | mTracks(type == MIXER), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2149 | mOutput(output), |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 2150 | mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2151 | mMixerStatus(MIXER_IDLE), |
| 2152 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Andy Hung | d58c473 | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 2153 | mStandbyDelayNs(getStandbyTimeInNanos()), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2154 | mBytesRemaining(0), |
| 2155 | mCurrentWriteLength(0), |
| 2156 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2157 | mWriteAckSequence(0), |
| 2158 | mDrainSequence(0), |
Andy Hung | 1b6d46a | 2023-07-19 16:22:58 -0700 | [diff] [blame] | 2159 | mScreenState(mAfThreadCallback->getScreenState()), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2160 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2161 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 2162 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 2163 | mLeftVolFloat(-1.0), mRightVolFloat(-1.0), |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 2164 | mDownStreamPatch{}, |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 2165 | mIsTimestampAdvancing(kMinimumTimeBetweenTimestampChecksNs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2166 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 2167 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2168 | mNBLogWriter = afThreadCallback->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2169 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2170 | // Assumes constructor is called by AudioFlinger with its mutex() held, but |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2171 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 2172 | // parameter. |
| 2173 | // |
| 2174 | // If the HAL we are using has support for master volume or master mute, |
| 2175 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 2176 | // and the mute set to false). |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2177 | mMasterVolume = afThreadCallback->masterVolume_l(); |
| 2178 | mMasterMute = afThreadCallback->masterMute_l(); |
George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 2179 | if (mOutput->audioHwDev) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2180 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 2181 | mMasterVolume = 1.0; |
| 2182 | } |
| 2183 | |
| 2184 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 2185 | mMasterMute = false; |
| 2186 | } |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2187 | mIsMsdDevice = strcmp( |
| 2188 | mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2189 | } |
| 2190 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2191 | if (mixerConfig != nullptr && mixerConfig->channel_mask != AUDIO_CHANNEL_NONE) { |
| 2192 | mMixerChannelMask = mixerConfig->channel_mask; |
| 2193 | } |
| 2194 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2195 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2196 | |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 2197 | if (mType != SPATIALIZER |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 2198 | && mMixerChannelMask != mChannelMask) { |
| 2199 | LOG_ALWAYS_FATAL("HAL channel mask %#x does not match mixer channel mask %#x", |
| 2200 | mChannelMask, mMixerChannelMask); |
| 2201 | } |
| 2202 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2203 | // TODO: We may also match on address as well as device type for |
| 2204 | // 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] | 2205 | if (type == MIXER || type == DIRECT || type == OFFLOAD) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2206 | // TODO: This property should be ensure that only contains one single device type. |
| 2207 | mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( |
| 2208 | "audio.timestamp.corrected_output_device", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2209 | (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD |
| 2210 | : AUDIO_DEVICE_NONE)); |
| 2211 | } |
| 2212 | |
Mikhail Naganov | f33115d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 2213 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { |
| 2214 | 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] | 2215 | mStreamTypes[stream].volume = 0.0f; |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2216 | mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2217 | } |
Eric Laurent | 35f8c7c | 2020-02-08 11:42:35 -0800 | [diff] [blame] | 2218 | // Audio patch and call assistant volume are always max |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 2219 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 2220 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
Eric Laurent | 35f8c7c | 2020-02-08 11:42:35 -0800 | [diff] [blame] | 2221 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; |
| 2222 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2223 | } |
| 2224 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2225 | PlaybackThread::~PlaybackThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2226 | { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2227 | mAfThreadCallback->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2228 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2229 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2230 | free(mEffectBuffer); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 2231 | free(mPostSpatializerBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2232 | } |
| 2233 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2234 | // Thread virtuals |
| 2235 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2236 | void PlaybackThread::onFirstRef() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2237 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2238 | if (!isStreamInitialized()) { |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2239 | ALOGE("The stream is not open yet"); // This should not happen. |
| 2240 | } else { |
Mikhail Naganov | aec565e | 2023-02-22 16:31:28 -0800 | [diff] [blame] | 2241 | // Callbacks take strong or weak pointers as a parameter. |
| 2242 | // Since PlaybackThread passes itself as a callback handler, it can only |
| 2243 | // be done outside of the constructor. Creating weak and especially strong |
| 2244 | // pointers to a refcounted object in its own constructor is strongly |
| 2245 | // discouraged, see comments in system/core/libutils/include/utils/RefBase.h. |
| 2246 | // Even if a function takes a weak pointer, it is possible that it will |
| 2247 | // need to convert it to a strong pointer down the line. |
| 2248 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING && |
| 2249 | mOutput->stream->setCallback(this) == OK) { |
| 2250 | mUseAsyncWrite = true; |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2251 | mCallbackThread = sp<AsyncCallbackThread>::make(this); |
Mikhail Naganov | aec565e | 2023-02-22 16:31:28 -0800 | [diff] [blame] | 2252 | } |
| 2253 | |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2254 | if (mOutput->stream->setEventCallback(this) != OK) { |
jiabin | f1a3605 | 2020-08-19 14:33:31 -0700 | [diff] [blame] | 2255 | ALOGD("Failed to add event callback"); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2256 | } |
| 2257 | } |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2258 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 2259 | mThreadSnapshot.setTid(getTid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2260 | } |
| 2261 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2262 | // ThreadBase virtuals |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2263 | void PlaybackThread::preExit() |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2264 | { |
| 2265 | ALOGV(" preExit()"); |
Ytai Ben-Tsvi | 7e0183f | 2022-02-04 10:49:54 -0800 | [diff] [blame] | 2266 | status_t result = mOutput->stream->exit(); |
| 2267 | ALOGE_IF(result != OK, "Error when calling exit(): %d", result); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2270 | void PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2271 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2272 | String8 result; |
| 2273 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2274 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2275 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 2276 | const stream_type_t *st = &mStreamTypes[i]; |
| 2277 | if (i > 0) { |
| 2278 | result.appendFormat(", "); |
| 2279 | } |
| 2280 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 2281 | if (st->mute) { |
| 2282 | result.append("M"); |
| 2283 | } |
| 2284 | } |
| 2285 | result.append("\n"); |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2286 | write(fd, result.c_str(), result.length()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2287 | result.clear(); |
| 2288 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2289 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 2290 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2291 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2292 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2293 | |
| 2294 | size_t numtracks = mTracks.size(); |
| 2295 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2296 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2297 | size_t numactiveseen = 0; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2298 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2299 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2300 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2301 | result.append(prefix); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2302 | mTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2303 | for (size_t i = 0; i < numtracks; ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2304 | sp<IAfTrack> track = mTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2305 | if (track != 0) { |
| 2306 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 2307 | if (active) { |
| 2308 | numactiveseen++; |
| 2309 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2310 | result.append(prefix); |
| 2311 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | } else { |
| 2315 | result.append("\n"); |
| 2316 | } |
| 2317 | if (numactiveseen != numactive) { |
| 2318 | // some tracks in the active list were not in the tracks list |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2319 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2320 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2321 | result.append(prefix); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 2322 | mActiveTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2323 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2324 | sp<IAfTrack> track = mActiveTracks[i]; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2325 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2326 | result.append(prefix); |
| 2327 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2332 | write(fd, result.c_str(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2333 | } |
| 2334 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2335 | void PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2336 | { |
Andy Hung | 04cb8f7 | 2020-03-20 13:44:33 -0700 | [diff] [blame] | 2337 | dprintf(fd, " Master volume: %f\n", mMasterVolume); |
Mikhail Naganov | dfb411f | 2018-09-11 13:39:56 -0700 | [diff] [blame] | 2338 | dprintf(fd, " Master mute: %s\n", mMasterMute ? "on" : "off"); |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 2339 | dprintf(fd, " Mixer channel Mask: %#x (%s)\n", |
| 2340 | mMixerChannelMask, channelMaskToString(mMixerChannelMask, true /* output */).c_str()); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2341 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE) { |
| 2342 | dprintf(fd, " Haptic channel mask: %#x (%s)\n", mHapticChannelMask, |
| 2343 | channelMaskToString(mHapticChannelMask, true /* output */).c_str()); |
| 2344 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2345 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2346 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 2347 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 2348 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2349 | dprintf(fd, " Suspend count: %d\n", (int32_t)mSuspended); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 2350 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2351 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 2352 | AudioStreamOut *output = mOutput; |
| 2353 | 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] | 2354 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 2355 | output, flags, toString(flags).c_str()); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2356 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 2357 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 2358 | if (mPipeSink.get() != nullptr) { |
| 2359 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 2360 | } |
| 2361 | if (output != nullptr) { |
| 2362 | dprintf(fd, " Hal stream dump:\n"); |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 2363 | (void)output->stream->dump(fd, args); |
Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 2364 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2367 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2368 | sp<IAfTrack> PlaybackThread::createTrack_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 2369 | const sp<Client>& client, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2370 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2371 | const audio_attributes_t& attr, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2372 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2373 | audio_format_t format, |
| 2374 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2375 | size_t *pFrameCount, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2376 | size_t *pNotificationFrameCount, |
| 2377 | uint32_t notificationsPerBuffer, |
| 2378 | float speed, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2379 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2380 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2381 | audio_output_flags_t *flags, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2382 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2383 | const AttributionSourceState& attributionSource, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2384 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2385 | status_t *status, |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2386 | audio_port_handle_t portId, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 2387 | const sp<media::IAudioTrackCallback>& callback, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2388 | bool isSpatialized, |
jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 2389 | bool isBitPerfect, |
| 2390 | audio_output_flags_t *afTrackFlags) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2391 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2392 | size_t frameCount = *pFrameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2393 | size_t notificationFrameCount = *pNotificationFrameCount; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2394 | sp<IAfTrack> track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2395 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2396 | audio_output_flags_t outputFlags = mOutput->flags; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2397 | audio_output_flags_t requestedFlags = *flags; |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2398 | uint32_t sampleRate; |
| 2399 | |
| 2400 | if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) { |
| 2401 | lStatus = BAD_VALUE; |
| 2402 | goto Exit; |
| 2403 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2404 | |
| 2405 | if (*pSampleRate == 0) { |
| 2406 | *pSampleRate = mSampleRate; |
| 2407 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 2408 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2409 | |
| 2410 | // special case for FAST flag considered OK if fast mixer is present |
| 2411 | if (hasFastMixer()) { |
| 2412 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 2413 | } |
| 2414 | |
| 2415 | // Check if requested flags are compatible with output stream flags |
| 2416 | if ((*flags & outputFlags) != *flags) { |
| 2417 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 2418 | *flags, outputFlags); |
| 2419 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 2420 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2421 | |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2422 | if (isBitPerfect) { |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2423 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2424 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2425 | if (chain.get() != nullptr) { |
| 2426 | // Bit-perfect is required according to the configuration and preferred mixer |
| 2427 | // attributes, but it is not in the output flag from the client's request. Explicitly |
| 2428 | // adding bit-perfect flag to check the compatibility |
| 2429 | audio_output_flags_t flagsToCheck = |
| 2430 | (audio_output_flags_t)(*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT); |
| 2431 | chain->checkOutputFlagCompatibility(&flagsToCheck); |
| 2432 | if ((flagsToCheck & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE) { |
| 2433 | ALOGE("%s cannot create track as there is data-processing effect attached to " |
| 2434 | "given session id(%d)", __func__, sessionId); |
| 2435 | lStatus = BAD_VALUE; |
| 2436 | goto Exit; |
| 2437 | } |
| 2438 | *flags = flagsToCheck; |
| 2439 | } |
| 2440 | } |
| 2441 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2442 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2443 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2444 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2445 | // PCM data |
| 2446 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2447 | // TODO: extract as a data library function that checks that a computationally |
| 2448 | // expensive downmixer is not required: isFastOutputChannelConversion() |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2449 | (channelMask == (mChannelMask | mHapticChannelMask) || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 2450 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 2451 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 2452 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2453 | // hardware sample rate |
| 2454 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2455 | // normal mixer has an associated fast mixer |
| 2456 | hasFastMixer() && |
| 2457 | // there are sufficient fast track slots available |
| 2458 | (mFastTrackAvailMask != 0) |
| 2459 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 2460 | // FIXME add a permission test also? |
| 2461 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2462 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 2463 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 2464 | // read the fast track multiplier property the first time it is needed |
| 2465 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 2466 | if (ok != 0) { |
| 2467 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 2468 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 2469 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2470 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2471 | |
| 2472 | // check compatibility with audio effects. |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2473 | { // scope for mutex() |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2474 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2475 | for (audio_session_t session : { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 2476 | AUDIO_SESSION_DEVICE, |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2477 | AUDIO_SESSION_OUTPUT_STAGE, |
| 2478 | AUDIO_SESSION_OUTPUT_MIX, |
| 2479 | sessionId, |
| 2480 | }) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2481 | sp<IAfEffectChain> chain = getEffectChain_l(session); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2482 | if (chain.get() != nullptr) { |
| 2483 | audio_output_flags_t old = *flags; |
| 2484 | chain->checkOutputFlagCompatibility(flags); |
| 2485 | if (old != *flags) { |
| 2486 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 2487 | (int)session, (int)old, (int)*flags); |
| 2488 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 2492 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2493 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 2494 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2495 | } else { |
Robert Wu | 4c7bb7d | 2021-08-12 18:50:13 +0000 | [diff] [blame] | 2496 | ALOGD("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2497 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2498 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2499 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2500 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2501 | audio_is_linear_pcm(format), channelMask, sampleRate, |
| 2502 | mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2503 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 2504 | } |
| 2505 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2506 | |
| 2507 | if (!audio_has_proportional_frames(format)) { |
| 2508 | if (sharedBuffer != 0) { |
| 2509 | // Same comment as below about ignoring frameCount parameter for set() |
| 2510 | frameCount = sharedBuffer->size(); |
| 2511 | } else if (frameCount == 0) { |
| 2512 | frameCount = mNormalFrameCount; |
| 2513 | } |
| 2514 | if (notificationFrameCount != frameCount) { |
| 2515 | notificationFrameCount = frameCount; |
| 2516 | } |
| 2517 | } else if (sharedBuffer != 0) { |
| 2518 | // FIXME: Ensure client side memory buffers need |
| 2519 | // not have additional alignment beyond sample |
| 2520 | // (e.g. 16 bit stereo accessed as 32 bit frame). |
| 2521 | size_t alignment = audio_bytes_per_sample(format); |
| 2522 | if (alignment & 1) { |
| 2523 | // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java). |
| 2524 | alignment = 1; |
| 2525 | } |
| 2526 | uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2527 | size_t frameSize = channelCount * audio_bytes_per_sample(format); |
| 2528 | if (channelCount > 1) { |
| 2529 | // More than 2 channels does not require stronger alignment than stereo |
| 2530 | alignment <<= 1; |
| 2531 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2532 | if (((uintptr_t)sharedBuffer->unsecurePointer() & (alignment - 1)) != 0) { |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2533 | ALOGE("Invalid buffer alignment: address %p, channel count %u", |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 2534 | sharedBuffer->unsecurePointer(), channelCount); |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2535 | lStatus = BAD_VALUE; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2536 | goto Exit; |
| 2537 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2538 | |
| 2539 | // When initializing a shared buffer AudioTrack via constructors, |
| 2540 | // there's no frameCount parameter. |
| 2541 | // But when initializing a shared buffer AudioTrack via set(), |
| 2542 | // there _is_ a frameCount parameter. We silently ignore it. |
| 2543 | frameCount = sharedBuffer->size() / frameSize; |
| 2544 | } else { |
| 2545 | size_t minFrameCount = 0; |
| 2546 | // For fast tracks we try to respect the application's request for notifications per buffer. |
| 2547 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2548 | if (notificationsPerBuffer > 0) { |
| 2549 | // Avoid possible arithmetic overflow during multiplication. |
| 2550 | if (notificationsPerBuffer > SIZE_MAX / mFrameCount) { |
| 2551 | ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu", |
| 2552 | notificationsPerBuffer, mFrameCount); |
| 2553 | } else { |
| 2554 | minFrameCount = mFrameCount * notificationsPerBuffer; |
| 2555 | } |
| 2556 | } |
| 2557 | } else { |
| 2558 | // For normal PCM streaming tracks, update minimum frame count. |
| 2559 | // Buffer depth is forced to be at least 2 x the normal mixer frame count and |
| 2560 | // cover audio hardware latency. |
| 2561 | // This is probably too conservative, but legacy application code may depend on it. |
| 2562 | // If you change this calculation, also review the start threshold which is related. |
| 2563 | uint32_t latencyMs = latency_l(); |
| 2564 | if (latencyMs == 0) { |
| 2565 | ALOGE("Error when retrieving output stream latency"); |
| 2566 | lStatus = UNKNOWN_ERROR; |
| 2567 | goto Exit; |
| 2568 | } |
| 2569 | |
| 2570 | minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount, |
| 2571 | mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/); |
| 2572 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2573 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2574 | if (frameCount < minFrameCount) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2575 | frameCount = minFrameCount; |
| 2576 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2577 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2578 | |
| 2579 | // Make sure that application is notified with sufficient margin before underrun. |
| 2580 | // The client can divide the AudioTrack buffer into sub-buffers, |
| 2581 | // and expresses its desire to server as the notification frame count. |
| 2582 | if (sharedBuffer == 0 && audio_is_linear_pcm(format)) { |
| 2583 | size_t maxNotificationFrames; |
| 2584 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2585 | // notify every HAL buffer, regardless of the size of the track buffer |
| 2586 | maxNotificationFrames = mFrameCount; |
| 2587 | } else { |
Andy Hung | fa11780 | 2019-04-12 13:50:39 -0700 | [diff] [blame] | 2588 | // Triple buffer the notification period for a triple buffered mixer period; |
| 2589 | // otherwise, double buffering for the notification period is fine. |
| 2590 | // |
| 2591 | // TODO: This should be moved to AudioTrack to modify the notification period |
| 2592 | // on AudioTrack::setBufferSizeInFrames() changes. |
| 2593 | const int nBuffering = |
| 2594 | (uint64_t{frameCount} * mSampleRate) |
| 2595 | / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2; |
| 2596 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2597 | maxNotificationFrames = frameCount / nBuffering; |
| 2598 | // If client requested a fast track but this was denied, then use the smaller maximum. |
| 2599 | if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) { |
| 2600 | size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000; |
| 2601 | if (maxNotificationFrames > maxNotificationFramesFastDenied) { |
| 2602 | maxNotificationFrames = maxNotificationFramesFastDenied; |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 2607 | if (notificationFrameCount == 0) { |
| 2608 | ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu", |
| 2609 | maxNotificationFrames, frameCount); |
| 2610 | } else { |
| 2611 | ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu", |
| 2612 | notificationFrameCount, maxNotificationFrames, frameCount); |
| 2613 | } |
| 2614 | notificationFrameCount = maxNotificationFrames; |
| 2615 | } |
| 2616 | } |
| 2617 | |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2618 | *pFrameCount = frameCount; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 2619 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2620 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2621 | switch (mType) { |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2622 | case BIT_PERFECT: |
| 2623 | if (isBitPerfect) { |
| 2624 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
| 2625 | ALOGE("%s, bad parameter when request streaming bit-perfect, sampleRate=%u, " |
| 2626 | "format=%#x, channelMask=%#x, mSampleRate=%u, mFormat=%#x, mChannelMask=%#x", |
| 2627 | __func__, sampleRate, format, channelMask, mSampleRate, mFormat, |
| 2628 | mChannelMask); |
| 2629 | lStatus = BAD_VALUE; |
| 2630 | goto Exit; |
| 2631 | } |
| 2632 | } |
| 2633 | break; |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2634 | |
| 2635 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 2636 | 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] | 2637 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2638 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 2639 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2640 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2641 | lStatus = BAD_VALUE; |
| 2642 | goto Exit; |
| 2643 | } |
| 2644 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2645 | break; |
| 2646 | |
| 2647 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2648 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2649 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 2650 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2651 | sampleRate, format, channelMask, mOutput, mFormat); |
| 2652 | lStatus = BAD_VALUE; |
| 2653 | goto Exit; |
| 2654 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2655 | break; |
| 2656 | |
| 2657 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2658 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2659 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 2660 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2661 | format, mOutput, mFormat); |
| 2662 | lStatus = BAD_VALUE; |
| 2663 | goto Exit; |
| 2664 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2665 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2666 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2667 | lStatus = BAD_VALUE; |
| 2668 | goto Exit; |
| 2669 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2670 | break; |
| 2671 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | lStatus = initCheck(); |
| 2675 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2676 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2677 | goto Exit; |
| 2678 | } |
| 2679 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2680 | { // scope for mutex() |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2681 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2682 | |
| 2683 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2684 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2685 | // manager |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2686 | product_strategy_t strategy = getStrategyForStream(streamType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2687 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2688 | sp<IAfTrack> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2689 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2690 | product_strategy_t actual = getStrategyForStream(t->streamType()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2691 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2692 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2693 | strategy, actual); |
| 2694 | lStatus = BAD_VALUE; |
| 2695 | goto Exit; |
| 2696 | } |
| 2697 | } |
| 2698 | } |
| 2699 | |
Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2700 | // Set DIRECT/OFFLOAD flag if current thread is DirectOutputThread/OffloadThread. |
| 2701 | // This can happen when the playback is rerouted to direct output/offload thread by |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2702 | // dynamic audio policy. |
| 2703 | // Do NOT report the flag changes back to client, since the client |
Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2704 | // doesn't explicitly request a direct/offload flag. |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2705 | audio_output_flags_t trackFlags = *flags; |
| 2706 | if (mType == DIRECT) { |
| 2707 | trackFlags = static_cast<audio_output_flags_t>(trackFlags | AUDIO_OUTPUT_FLAG_DIRECT); |
Deeraj Soman | 2b51523 | 2024-05-14 12:58:24 +0530 | [diff] [blame] | 2708 | } else if (mType == OFFLOAD) { |
| 2709 | trackFlags = static_cast<audio_output_flags_t>(trackFlags | |
| 2710 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT); |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2711 | } |
jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 2712 | *afTrackFlags = trackFlags; |
yucliu | c9c49cd | 2020-07-13 16:25:21 -0700 | [diff] [blame] | 2713 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2714 | track = IAfTrack::create(this, client, streamType, attr, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2715 | channelMask, frameCount, |
| 2716 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2717 | sessionId, creatorPid, attributionSource, trackFlags, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2718 | IAfTrackBase::TYPE_DEFAULT, portId, SIZE_MAX /*frameCountToBeReady*/, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2719 | speed, isSpatialized, isBitPerfect); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2720 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2721 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2722 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2723 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2724 | // 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] | 2725 | goto Exit; |
| 2726 | } |
| 2727 | mTracks.add(track); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2728 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2729 | audio_utils::lock_guard _atCbL(audioTrackCbMutex()); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2730 | if (callback.get() != nullptr) { |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2731 | mAudioTrackCallbacks.emplace(track, callback); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 2732 | } |
| 2733 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2734 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2735 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2736 | if (chain != 0) { |
| 2737 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2738 | track->setMainBuffer(chain->inBuffer()); |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 2739 | chain->setStrategy(getStrategyForStream(track->streamType())); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2740 | chain->incTrackCnt(); |
| 2741 | } |
| 2742 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2743 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2744 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2745 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2746 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 2747 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | |
| 2751 | lStatus = NO_ERROR; |
| 2752 | |
| 2753 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2754 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2755 | return track; |
| 2756 | } |
| 2757 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2758 | template<typename T> |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2759 | ssize_t PlaybackThread::Tracks<T>::remove(const sp<T>& track) |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2760 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2761 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2762 | const ssize_t index = mTracks.remove(track); |
| 2763 | if (index >= 0) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2764 | if (mSaveDeletedTrackIds) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2765 | // 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] | 2766 | // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update, |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2767 | // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 2768 | mDeletedTrackIds.emplace(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2769 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 2770 | } |
| 2771 | return index; |
| 2772 | } |
| 2773 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2774 | uint32_t PlaybackThread::correctLatency_l(uint32_t latency) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2775 | { |
| 2776 | return latency; |
| 2777 | } |
| 2778 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2779 | uint32_t PlaybackThread::latency() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2780 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2781 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2782 | return latency_l(); |
| 2783 | } |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2784 | uint32_t PlaybackThread::latency_l() const |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2785 | NO_THREAD_SAFETY_ANALYSIS |
| 2786 | // Fix later. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2787 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2788 | uint32_t latency; |
| 2789 | if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) { |
| 2790 | return correctLatency_l(latency); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2791 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2792 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2793 | } |
| 2794 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2795 | void PlaybackThread::setMasterVolume(float value) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2796 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2797 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2798 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2799 | if (mOutput && mOutput->audioHwDev && |
| 2800 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2801 | mMasterVolume = 1.0; |
| 2802 | } else { |
| 2803 | mMasterVolume = value; |
| 2804 | } |
| 2805 | } |
| 2806 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2807 | void PlaybackThread::setMasterBalance(float balance) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 2808 | { |
| 2809 | mMasterBalance.store(balance); |
| 2810 | } |
| 2811 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2812 | void PlaybackThread::setMasterMute(bool muted) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2813 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2814 | if (isDuplicating()) { |
| 2815 | return; |
| 2816 | } |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2817 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2818 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2819 | if (mOutput && mOutput->audioHwDev && |
| 2820 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2821 | mMasterMute = false; |
| 2822 | } else { |
| 2823 | mMasterMute = muted; |
| 2824 | } |
| 2825 | } |
| 2826 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2827 | void PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2828 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2829 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2830 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2831 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2832 | } |
| 2833 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2834 | void PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2835 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2836 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2837 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2838 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2839 | } |
| 2840 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2841 | float PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2842 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2843 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2844 | return mStreamTypes[stream].volume; |
| 2845 | } |
| 2846 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2847 | void PlaybackThread::setVolumeForOutput_l(float left, float right) const |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 2848 | { |
| 2849 | mOutput->stream->setVolume(left, right); |
| 2850 | } |
| 2851 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2852 | // addTrack_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2853 | status_t PlaybackThread::addTrack_l(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2854 | { |
| 2855 | status_t status = ALREADY_EXISTS; |
| 2856 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2857 | if (mActiveTracks.indexOf(track) < 0) { |
| 2858 | // the track is newly added, make sure it fills up all its |
| 2859 | // buffers before playing. This is to ensure the client will |
| 2860 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2861 | if (track->isExternalTrack()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2862 | IAfTrackBase::track_state state = track->state(); |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2863 | // Because the track is not on the ActiveTracks, |
| 2864 | // at this point, only the TrackHandle will be adding the track. |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2865 | mutex().unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2866 | status = AudioSystem::startOutput(track->portId()); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2867 | mutex().lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2868 | // abort track was stopped/paused while we released the lock |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2869 | if (state != track->state()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2870 | if (status == NO_ERROR) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2871 | mutex().unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2872 | AudioSystem::stopOutput(track->portId()); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2873 | mutex().lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2874 | } |
| 2875 | return INVALID_OPERATION; |
| 2876 | } |
| 2877 | // abort if start is rejected by audio policy manager |
| 2878 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2879 | // Do not replace the error if it is DEAD_OBJECT. When this happens, it indicates |
| 2880 | // current playback thread is reopened, which may happen when clients set preferred |
| 2881 | // mixer configuration. Returning DEAD_OBJECT will make the client restore track |
| 2882 | // immediately. |
| 2883 | return status == DEAD_OBJECT ? status : PERMISSION_DENIED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2884 | } |
| 2885 | #ifdef ADD_BATTERY_DATA |
| 2886 | // to track the speaker usage |
| 2887 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2888 | #endif |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2889 | sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2890 | } |
| 2891 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2892 | // set retry count for buffer fill |
| 2893 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2894 | if (track->isStopping_1()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2895 | track->retryCount() = kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2896 | } else { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2897 | track->retryCount() = kMaxTrackStartupRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2898 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2899 | track->fillingStatus() = mStandby ? IAfTrack::FS_FILLING : IAfTrack::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2900 | } else { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2901 | track->retryCount() = kMaxTrackStartupRetries; |
| 2902 | track->fillingStatus() = |
| 2903 | track->sharedBuffer() != 0 ? IAfTrack::FS_FILLED : IAfTrack::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2904 | } |
| 2905 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 2906 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 2907 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE |
| 2908 | && ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 2909 | || (chain != nullptr && chain->containsHapticGeneratingEffect()))) { |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2910 | // Unlock due to VibratorService will lock for this call and will |
| 2911 | // call Tracks.mute/unmute which also require thread's lock. |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2912 | mutex().unlock(); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2913 | const os::HapticScale hapticScale = afutils::onExternalVibrationStart( |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2914 | track->getExternalVibration()); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2915 | std::optional<media::AudioVibratorInfo> vibratorInfo; |
| 2916 | { |
| 2917 | // TODO(b/184194780): Use the vibrator information from the vibrator that will be |
| 2918 | // used to play this track. |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2919 | audio_utils::lock_guard _l(mAfThreadCallback->mutex()); |
Yi Kong | 3ac211f | 2024-08-12 07:31:44 +0800 | [diff] [blame] | 2920 | vibratorInfo = mAfThreadCallback->getDefaultVibratorInfo_l(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2921 | } |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2922 | mutex().lock(); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2923 | track->setHapticScale(hapticScale); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 2924 | if (vibratorInfo) { |
| 2925 | track->setHapticMaxAmplitude(vibratorInfo->maxAmplitude); |
| 2926 | } |
| 2927 | |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2928 | // Haptic playback should be enabled by vibrator service. |
| 2929 | if (track->getHapticPlaybackEnabled()) { |
| 2930 | // Disable haptic playback of all active track to ensure only |
| 2931 | // one track playing haptic if current track should play haptic. |
| 2932 | for (const auto &t : mActiveTracks) { |
| 2933 | t->setHapticPlaybackEnabled(false); |
| 2934 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2935 | } |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2936 | |
| 2937 | // Set haptic intensity for effect |
| 2938 | if (chain != nullptr) { |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 2939 | chain->setHapticScale_l(track->id(), hapticScale); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 2940 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 2941 | } |
| 2942 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2943 | track->setResetDone(false); |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 2944 | track->resetPresentationComplete(); |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2945 | |
| 2946 | // Do not release the ThreadBase mutex after the track is added to mActiveTracks unless |
| 2947 | // all key changes are complete. It is possible that the threadLoop will begin |
| 2948 | // processing the added track immediately after the ThreadBase mutex is released. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2949 | mActiveTracks.add(track); |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 2950 | |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2951 | if (chain != 0) { |
| 2952 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2953 | track->sessionId()); |
| 2954 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2955 | } |
| 2956 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 2957 | track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2958 | status = NO_ERROR; |
| 2959 | } |
| 2960 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2961 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2962 | return status; |
| 2963 | } |
| 2964 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2965 | bool PlaybackThread::destroyTrack_l(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2966 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2967 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2968 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2969 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2970 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2971 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2972 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2973 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Andy Hung | 916987e | 2023-03-20 19:08:16 -0700 | [diff] [blame] | 2974 | if (track->isPausePending()) { |
| 2975 | track->pauseAck(); |
| 2976 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2977 | track->setState(IAfTrackBase::STOPPING_1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2978 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2979 | |
| 2980 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2981 | } |
| 2982 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2983 | void PlaybackThread::removeTrack_l(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2984 | { |
| 2985 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2986 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2987 | String8 result; |
| 2988 | track->appendDump(result, false /* active */); |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 2989 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str()); |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 2990 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2991 | mTracks.remove(track); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2992 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2993 | audio_utils::lock_guard _atCbL(audioTrackCbMutex()); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 2994 | mAudioTrackCallbacks.erase(track); |
| 2995 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2996 | if (track->isFastTrack()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2997 | int index = track->fastIndex(); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2998 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2999 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 3000 | mFastTrackAvailMask |= 1 << index; |
| 3001 | // redundant as track is about to be destroyed, for dumpsys only |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3002 | track->fastIndex() = -1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3003 | } |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3004 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3005 | if (chain != 0) { |
| 3006 | chain->decTrackCnt(); |
| 3007 | } |
| 3008 | } |
| 3009 | |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 3010 | std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds_l() |
| 3011 | { |
| 3012 | std::set<int32_t> result; |
| 3013 | for (const auto& t : mTracks) { |
| 3014 | if (t->isExternalTrack()) { |
| 3015 | result.insert(t->portId()); |
| 3016 | } |
| 3017 | } |
| 3018 | return result; |
| 3019 | } |
| 3020 | |
| 3021 | std::set<audio_port_handle_t> PlaybackThread::getTrackPortIds() |
| 3022 | { |
| 3023 | audio_utils::lock_guard _l(mutex()); |
| 3024 | return getTrackPortIds_l(); |
| 3025 | } |
| 3026 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3027 | String8 PlaybackThread::getParameters(const String8& keys) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3028 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3029 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3030 | String8 out_s8; |
| 3031 | if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) { |
| 3032 | return out_s8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3033 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3034 | return {}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3035 | } |
| 3036 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3037 | status_t DirectOutputThread::selectPresentation(int presentationId, int programId) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3038 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 3039 | if (!isStreamInitialized()) { |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 3040 | return NO_INIT; |
| 3041 | } |
| 3042 | return mOutput->stream->selectPresentation(presentationId, programId); |
| 3043 | } |
| 3044 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3045 | void PlaybackThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3046 | audio_port_handle_t portId) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3047 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3048 | sp<AudioIoDescriptor> desc; |
| 3049 | const struct audio_patch patch = isMsdDevice() ? mDownStreamPatch : mPatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3050 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3051 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 3052 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3053 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3054 | desc = sp<AudioIoDescriptor>::make(mId, patch, false /*isInput*/, |
| 3055 | mSampleRate, mFormat, mChannelMask, |
| 3056 | // FIXME AudioFlinger::frameCount(audio_io_handle_t) instead of mNormalFrameCount? |
| 3057 | mNormalFrameCount, mFrameCount, latency_l()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3058 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3059 | case AUDIO_CLIENT_STARTED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3060 | desc = sp<AudioIoDescriptor>::make(mId, patch, portId); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3061 | break; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 3062 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3063 | default: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 3064 | desc = sp<AudioIoDescriptor>::make(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3065 | break; |
| 3066 | } |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3067 | mAfThreadCallback->ioConfigChanged_l(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3068 | } |
| 3069 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3070 | void PlaybackThread::onWriteReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3071 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3072 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3073 | } |
| 3074 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3075 | void PlaybackThread::onDrainReady() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3076 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3077 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 3080 | void PlaybackThread::onError(bool isHardError) |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3081 | { |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 3082 | mCallbackThread->setAsyncError(isHardError); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3085 | void PlaybackThread::onCodecFormatChanged( |
Ryan Prichard | 78c5e45 | 2024-02-08 16:16:57 -0800 | [diff] [blame] | 3086 | const std::vector<uint8_t>& metadataBs) |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3087 | { |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3088 | const auto weakPointerThis = wp<PlaybackThread>::fromExisting(this); |
Kuowei Li | 9e2f616 | 2022-11-23 16:25:26 +0800 | [diff] [blame] | 3089 | std::thread([this, metadataBs, weakPointerThis]() { |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3090 | const sp<PlaybackThread> playbackThread = weakPointerThis.promote(); |
Kuowei Li | 9e2f616 | 2022-11-23 16:25:26 +0800 | [diff] [blame] | 3091 | if (playbackThread == nullptr) { |
| 3092 | ALOGW("PlaybackThread was destroyed, skip codec format change event"); |
| 3093 | return; |
| 3094 | } |
| 3095 | |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3096 | audio_utils::metadata::Data metadata = |
| 3097 | audio_utils::metadata::dataFromByteString(metadataBs); |
| 3098 | if (metadata.empty()) { |
| 3099 | ALOGW("Can not transform the buffer to audio metadata, %s, %d", |
| 3100 | reinterpret_cast<char*>(const_cast<uint8_t*>(metadataBs.data())), |
| 3101 | (int)metadataBs.size()); |
| 3102 | return; |
| 3103 | } |
| 3104 | |
| 3105 | audio_utils::metadata::ByteString metaDataStr = |
| 3106 | audio_utils::metadata::byteStringFromData(metadata); |
| 3107 | std::vector metadataVec(metaDataStr.begin(), metaDataStr.end()); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3108 | audio_utils::lock_guard _l(audioTrackCbMutex()); |
jiabin | 18a4b1c | 2020-09-17 11:40:42 -0700 | [diff] [blame] | 3109 | for (const auto& callbackPair : mAudioTrackCallbacks) { |
| 3110 | callbackPair.second->onCodecFormatChanged(metadataVec); |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 3111 | } |
| 3112 | }).detach(); |
| 3113 | } |
| 3114 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3115 | void PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3116 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3117 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3118 | // reject out of sequence requests |
| 3119 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 3120 | mWriteAckSequence &= ~1; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3121 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3122 | } |
| 3123 | } |
| 3124 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3125 | void PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3126 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3127 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3128 | // reject out of sequence requests |
| 3129 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 3130 | // Register discontinuity when HW drain is completed because that can cause |
| 3131 | // the timestamp frame position to reset to 0 for direct and offload threads. |
| 3132 | // (Out of sequence requests are ignored, since the discontinuity would be handled |
| 3133 | // elsewhere, e.g. in flush). |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 3134 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3135 | mDrainSequence &= ~1; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3136 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3137 | } |
| 3138 | } |
| 3139 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3140 | void PlaybackThread::readOutputParameters_l() |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3141 | NO_THREAD_SAFETY_ANALYSIS |
| 3142 | // 'moveEffectChain_ll' requires holding mutex 'AudioFlinger_Mutex' exclusively |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3143 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3144 | // 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] | 3145 | const audio_config_base_t audioConfig = mOutput->getAudioProperties(); |
| 3146 | mSampleRate = audioConfig.sample_rate; |
| 3147 | mChannelMask = audioConfig.channel_mask; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3148 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3149 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3150 | } |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 3151 | if (hasMixer() && !isValidPcmSinkChannelMask(mChannelMask)) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3152 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 3153 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3154 | } |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3155 | |
| 3156 | if (mMixerChannelMask == AUDIO_CHANNEL_NONE) { |
| 3157 | mMixerChannelMask = mChannelMask; |
| 3158 | } |
| 3159 | |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 3160 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3161 | mBalance.setChannelMask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3162 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3163 | uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mMixerChannelMask); |
| 3164 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 3165 | // Get actual HAL format. |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3166 | status_t result = mOutput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3167 | 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] | 3168 | // Get format from the shim, which will be different than the HAL format |
| 3169 | // if playing compressed audio over HDMI passthrough. |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 3170 | mFormat = audioConfig.format; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3171 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3172 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3173 | } |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 3174 | if (hasMixer() && !isValidPcmSinkFormat(mFormat)) { |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 3175 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 3176 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 3177 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3178 | mFrameSize = mOutput->getFrameSize(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3179 | result = mOutput->stream->getBufferSize(&mBufferSize); |
| 3180 | LOG_ALWAYS_FATAL_IF(result != OK, |
| 3181 | "Error when retrieving output stream buffer size: %d", result); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 3182 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 3183 | if (hasMixer() && (mFrameCount & 15)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3184 | 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] | 3185 | mFrameCount); |
| 3186 | } |
| 3187 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3188 | mHwSupportsPause = false; |
| 3189 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3190 | bool supportsPause = false, supportsResume = false; |
| 3191 | if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) { |
| 3192 | if (supportsPause && supportsResume) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3193 | mHwSupportsPause = true; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3194 | } else if (supportsPause) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3195 | ALOGW("direct output implements pause but not resume"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3196 | } else if (supportsResume) { |
| 3197 | ALOGW("direct output implements resume but not pause"); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3198 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3199 | } |
| 3200 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 3201 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 3202 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 3203 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3204 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3205 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3206 | // For best precision, we use float instead of the associated output |
| 3207 | // device format (typically PCM 16 bit). |
| 3208 | |
| 3209 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3210 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3211 | mBufferSize = mFrameSize * mFrameCount; |
| 3212 | |
| 3213 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 3214 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 3215 | // (if a valid mask) to avoid premature downmix. |
| 3216 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 3217 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 3218 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 3219 | } |
| 3220 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 3221 | // 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] | 3222 | double multiplier = 1.0; |
Henrik Tillman | 470b399 | 2024-10-08 12:49:28 +0200 | [diff] [blame^] | 3223 | // Note: mType == SPATIALIZER does not support FastMixer and DEEP is by definition not "fast" |
| 3224 | if ((mType == MIXER && !(mOutput->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) && |
| 3225 | (kUseFastMixer == FastMixer_Static || kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 3226 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 3227 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 3228 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3229 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 3230 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 3231 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 3232 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 3233 | maxNormalFrameCount = minNormalFrameCount; |
| 3234 | } |
| 3235 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 3236 | if (multiplier <= 1.0) { |
| 3237 | multiplier = 1.0; |
| 3238 | } else if (multiplier <= 2.0) { |
| 3239 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 3240 | multiplier = 2.0; |
| 3241 | } else { |
| 3242 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 3243 | } |
| 3244 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 3245 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | mNormalFrameCount = multiplier * mFrameCount; |
| 3249 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 3250 | if (hasMixer()) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3251 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 3252 | } |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3253 | ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", |
| 3254 | (size_t)mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3255 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3256 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 3257 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3258 | mThreadThrottleTimeMs = 0; |
| 3259 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3260 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 3261 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3262 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 3263 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 3264 | free(mSinkBuffer); |
| 3265 | mSinkBuffer = NULL; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 3266 | |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 3267 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 3268 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 3269 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3270 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3271 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3272 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 3273 | // drives the output. |
| 3274 | free(mMixerBuffer); |
| 3275 | mMixerBuffer = NULL; |
| 3276 | if (mMixerBufferEnabled) { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 3277 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT. |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3278 | mMixerBufferSize = mNormalFrameCount * mixerChannelCount |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3279 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 3280 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 3281 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3282 | free(mEffectBuffer); |
| 3283 | mEffectBuffer = NULL; |
| 3284 | if (mEffectBufferEnabled) { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3285 | mEffectBufferFormat = AUDIO_FORMAT_PCM_FLOAT; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3286 | mEffectBufferSize = mNormalFrameCount * mixerChannelCount |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3287 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 3288 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 3289 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3290 | |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3291 | if (mType == SPATIALIZER) { |
| 3292 | free(mPostSpatializerBuffer); |
| 3293 | mPostSpatializerBuffer = nullptr; |
| 3294 | mPostSpatializerBufferSize = mNormalFrameCount * mChannelCount |
| 3295 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 3296 | (void)posix_memalign(&mPostSpatializerBuffer, 32, mPostSpatializerBufferSize); |
| 3297 | } |
| 3298 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3299 | mHapticChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
| 3300 | mChannelMask = static_cast<audio_channel_mask_t>(mChannelMask & ~mHapticChannelMask); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3301 | mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask); |
| 3302 | mChannelCount -= mHapticChannelCount; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3303 | mMixerChannelMask = static_cast<audio_channel_mask_t>(mMixerChannelMask & ~mHapticChannelMask); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 3304 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3305 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 3306 | // parameters into account |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3307 | // Note that mutex() is not held when readOutputParameters_l() is called from the constructor |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3308 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 3309 | // matter. |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3310 | // create a copy of mEffectChains as calling moveEffectChain_ll() |
| 3311 | // can reorder some effect chains |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3312 | Vector<sp<IAfEffectChain>> effectChains = mEffectChains; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3313 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3314 | mAfThreadCallback->moveEffectChain_ll(effectChains[i]->sessionId(), |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3315 | this/* srcThread */, this/* dstThread */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3316 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3317 | |
George Burgess IV | 5e4d86f | 2020-02-18 12:55:36 -0800 | [diff] [blame] | 3318 | audio_output_flags_t flags = mOutput->flags; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3319 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); // TODO: method in ThreadMetrics? |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3320 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 3321 | .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3322 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 3323 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 3324 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 3325 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mNormalFrameCount) |
| 3326 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 3327 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, |
| 3328 | (int32_t)mHapticChannelMask) |
| 3329 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, |
| 3330 | (int32_t)mHapticChannelCount) |
| 3331 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING, |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 3332 | IAfThreadBase::formatToString(mHALFormat).c_str()) |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 3333 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT, |
| 3334 | (int32_t)mFrameCount) // sic - added HAL |
| 3335 | ; |
| 3336 | uint32_t latencyMs; |
| 3337 | if (mOutput->stream->getLatency(&latencyMs) == NO_ERROR) { |
| 3338 | item.set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_LATENCYMS, (double)latencyMs); |
| 3339 | } |
| 3340 | item.record(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3341 | } |
| 3342 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3343 | ThreadBase::MetadataUpdate PlaybackThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3344 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 3345 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 3346 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3347 | } |
| 3348 | StreamOutHalInterface::SourceMetadata metadata; |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 3349 | static const bool stereo_spatialization_property = |
| 3350 | property_get_bool("ro.audio.stereo_spatialization_enabled", false); |
| 3351 | const bool stereo_spatialization_enabled = |
| 3352 | stereo_spatialization_property && com_android_media_audio_stereo_spatialization(); |
| 3353 | if (stereo_spatialization_enabled) { |
Eric Laurent | 4eb45d0 | 2023-12-20 12:07:17 +0100 | [diff] [blame] | 3354 | std::map<audio_session_t, std::vector<playback_track_metadata_v7_t> >allSessionsMetadata; |
| 3355 | for (const sp<IAfTrack>& track : mActiveTracks) { |
| 3356 | std::vector<playback_track_metadata_v7_t>& sessionMetadata = |
| 3357 | allSessionsMetadata[track->sessionId()]; |
| 3358 | auto backInserter = std::back_inserter(sessionMetadata); |
| 3359 | // No track is invalid as this is called after prepareTrack_l in the same |
| 3360 | // critical section |
| 3361 | track->copyMetadataTo(backInserter); |
| 3362 | } |
| 3363 | std::vector<playback_track_metadata_v7_t> spatializedTracksMetaData; |
| 3364 | for (const auto& [session, sessionTrackMetadata] : allSessionsMetadata) { |
| 3365 | metadata.tracks.insert(metadata.tracks.end(), |
| 3366 | sessionTrackMetadata.begin(), sessionTrackMetadata.end()); |
| 3367 | if (auto chain = getEffectChain_l(session) ; chain != nullptr) { |
| 3368 | chain->sendMetadata_l(sessionTrackMetadata, {}); |
| 3369 | } |
| 3370 | if ((hasAudioSession_l(session) & IAfThreadBase::SPATIALIZED_SESSION) != 0) { |
| 3371 | spatializedTracksMetaData.insert(spatializedTracksMetaData.end(), |
| 3372 | sessionTrackMetadata.begin(), sessionTrackMetadata.end()); |
| 3373 | } |
| 3374 | } |
| 3375 | if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); chain != nullptr) { |
| 3376 | chain->sendMetadata_l(metadata.tracks, {}); |
| 3377 | } |
| 3378 | if (auto chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); chain != nullptr) { |
| 3379 | chain->sendMetadata_l(metadata.tracks, spatializedTracksMetaData); |
| 3380 | } |
| 3381 | if (auto chain = getEffectChain_l(AUDIO_SESSION_DEVICE); chain != nullptr) { |
| 3382 | chain->sendMetadata_l(metadata.tracks, {}); |
| 3383 | } |
| 3384 | } else { |
| 3385 | auto backInserter = std::back_inserter(metadata.tracks); |
| 3386 | for (const sp<IAfTrack>& track : mActiveTracks) { |
| 3387 | // No track is invalid as this is called after prepareTrack_l in the same |
| 3388 | // critical section |
| 3389 | track->copyMetadataTo(backInserter); |
| 3390 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 3391 | } |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3392 | sendMetadataToBackend_l(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 3393 | MetadataUpdate change; |
| 3394 | change.playbackMetadataUpdate = metadata.tracks; |
| 3395 | return change; |
Kevin Rocard | 80ee272 | 2018-04-11 15:53:48 +0000 | [diff] [blame] | 3396 | } |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 3397 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3398 | void PlaybackThread::sendMetadataToBackend_l( |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 3399 | const StreamOutHalInterface::SourceMetadata& metadata) |
| 3400 | { |
| 3401 | mOutput->stream->updateSourceMetadata(metadata); |
| 3402 | }; |
| 3403 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3404 | status_t PlaybackThread::getRenderPosition( |
Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 3405 | uint32_t* halFrames, uint32_t* dspFrames) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3406 | { |
| 3407 | if (halFrames == NULL || dspFrames == NULL) { |
| 3408 | return BAD_VALUE; |
| 3409 | } |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3410 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3411 | if (initCheck() != NO_ERROR) { |
| 3412 | return INVALID_OPERATION; |
| 3413 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3414 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3415 | *halFrames = framesWritten; |
| 3416 | |
| 3417 | if (isSuspended()) { |
| 3418 | // return an estimation of rendered frames when the output is suspended |
| 3419 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3420 | *dspFrames = (uint32_t) |
| 3421 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3422 | return NO_ERROR; |
| 3423 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3424 | status_t status; |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 3425 | uint64_t frames = 0; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3426 | status = mOutput->getRenderPosition(&frames); |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 3427 | *dspFrames = (uint32_t)frames; |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3428 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3429 | } |
| 3430 | } |
| 3431 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3432 | product_strategy_t PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3433 | { |
| 3434 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 3435 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 3436 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3437 | return getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3438 | } |
| 3439 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3440 | sp<IAfTrack> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3441 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3442 | return getStrategyForStream(track->streamType()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3443 | } |
| 3444 | } |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 3445 | return getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3449 | AudioStreamOut* PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3450 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3451 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3452 | return mOutput; |
| 3453 | } |
| 3454 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3455 | AudioStreamOut* PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3456 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3457 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3458 | AudioStreamOut *output = mOutput; |
| 3459 | mOutput = NULL; |
| 3460 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 3461 | // must push a NULL and wait for ack |
| 3462 | mOutputSink.clear(); |
| 3463 | mPipeSink.clear(); |
| 3464 | mNormalSink.clear(); |
| 3465 | return output; |
| 3466 | } |
| 3467 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3468 | // this method must always be called either with ThreadBase mutex() held or inside the thread loop |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3469 | sp<StreamHalInterface> PlaybackThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3470 | { |
| 3471 | if (mOutput == NULL) { |
| 3472 | return NULL; |
| 3473 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3474 | return mOutput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3475 | } |
| 3476 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3477 | uint32_t PlaybackThread::activeSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3478 | { |
| 3479 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 3480 | } |
| 3481 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3482 | status_t PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3483 | { |
| 3484 | if (!isValidSyncEvent(event)) { |
| 3485 | return BAD_VALUE; |
| 3486 | } |
| 3487 | |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3488 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3489 | |
| 3490 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3491 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3492 | if (event->triggerSession() == track->sessionId()) { |
| 3493 | (void) track->setSyncEvent(event); |
| 3494 | return NO_ERROR; |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | return NAME_NOT_FOUND; |
| 3499 | } |
| 3500 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3501 | bool PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3502 | { |
| 3503 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 3504 | } |
| 3505 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3506 | void PlaybackThread::threadLoop_removeTracks( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3507 | [[maybe_unused]] const Vector<sp<IAfTrack>>& tracksToRemove) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3508 | { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3509 | // Miscellaneous track cleanup when removed from the active list, |
| 3510 | // called without Thread lock but synchronized with threadLoop processing. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3511 | #ifdef ADD_BATTERY_DATA |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3512 | for (const auto& track : tracksToRemove) { |
| 3513 | if (track->isExternalTrack()) { |
| 3514 | // to track the speaker usage |
| 3515 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3516 | } |
| 3517 | } |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 3518 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3519 | } |
| 3520 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3521 | void PlaybackThread::checkSilentMode_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3522 | { |
| 3523 | if (!mMasterMute) { |
| 3524 | char value[PROPERTY_VALUE_MAX]; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 3525 | if (mOutDeviceTypeAddrs.empty()) { |
| 3526 | ALOGD("ro.audio.silent is ignored since no output device is set"); |
| 3527 | return; |
| 3528 | } |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3529 | if (isSingleDeviceType(outDeviceTypes_l(), AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) { |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 3530 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 3531 | return; |
| 3532 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3533 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 3534 | char *endptr; |
| 3535 | unsigned long ul = strtoul(value, &endptr, 0); |
| 3536 | if (*endptr == '\0' && ul != 0) { |
Shunkai Yao | dd3de69 | 2024-03-06 02:56:57 +0000 | [diff] [blame] | 3537 | ALOGW("%s: mute from ro.audio.silent. Silence is golden", __func__); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3538 | // The setprop command will not allow a property to be changed after |
| 3539 | // the first time it is set, so we don't have to worry about un-muting. |
| 3540 | setMasterMute_l(true); |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3547 | ssize_t PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3548 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 3549 | LOG_HIST_TS(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3550 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3551 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3552 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3553 | |
| 3554 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 3555 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3556 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3557 | const size_t count = mBytesRemaining / mFrameSize; |
| 3558 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3559 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3560 | // update the setpoint when AudioFlinger::mScreenState changes |
Andy Hung | 1b6d46a | 2023-07-19 16:22:58 -0700 | [diff] [blame] | 3561 | const uint32_t screenState = mAfThreadCallback->getScreenState(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3562 | if (screenState != mScreenState) { |
| 3563 | mScreenState = screenState; |
| 3564 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3565 | if (pipe != NULL) { |
| 3566 | pipe->setAvgFrames((mScreenState & 1) ? |
| 3567 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3568 | } |
| 3569 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3570 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 3571 | ATRACE_END(); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3572 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3573 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3574 | bytesWritten = framesWritten * mFrameSize; |
Andy Hung | 58e3287 | 2022-11-15 16:12:24 -0800 | [diff] [blame] | 3575 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 3576 | #ifdef TEE_SINK |
| 3577 | mTee.write((char *)mSinkBuffer + offset, framesWritten); |
| 3578 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3579 | } else { |
| 3580 | bytesWritten = framesWritten; |
| 3581 | } |
| 3582 | // otherwise use the HAL / AudioStreamOut directly |
| 3583 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3584 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 3585 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3586 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3587 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 3588 | mWriteAckSequence += 2; |
| 3589 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3590 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3591 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3592 | } |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3593 | ATRACE_BEGIN("write"); |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 3594 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 3595 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3596 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 3597 | ATRACE_END(); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3598 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3599 | if (mUseAsyncWrite && |
| 3600 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 3601 | // 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] | 3602 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3603 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3604 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3605 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3608 | mNumWrites++; |
| 3609 | mInWrite = false; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3610 | if (mStandby) { |
| 3611 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 3612 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 3613 | mStandby = false; |
| 3614 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3615 | return bytesWritten; |
| 3616 | } |
| 3617 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3618 | // startMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3619 | void PlaybackThread::startMelComputation_l( |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 3620 | const sp<audio_utils::MelProcessor>& processor) |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3621 | { |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 3622 | auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get()); |
Vlad Popa | 1a0c3ab | 2023-03-17 01:07:01 +0100 | [diff] [blame] | 3623 | if (outputSink != nullptr) { |
| 3624 | outputSink->startMelComputation(processor); |
| 3625 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3626 | } |
| 3627 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3628 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3629 | void PlaybackThread::stopMelComputation_l() |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 3630 | { |
| 3631 | auto outputSink = static_cast<AudioStreamOutSink*>(mOutputSink.get()); |
Vlad Popa | 1a0c3ab | 2023-03-17 01:07:01 +0100 | [diff] [blame] | 3632 | if (outputSink != nullptr) { |
| 3633 | outputSink->stopMelComputation(); |
| 3634 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 3635 | } |
| 3636 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3637 | void PlaybackThread::threadLoop_drain() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3638 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3639 | bool supportsDrain = false; |
| 3640 | if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3641 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 3642 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3643 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 3644 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3645 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3646 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3647 | } |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 3648 | status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3649 | ALOGE_IF(result != OK, "Error when draining stream: %d", result); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3650 | } |
| 3651 | } |
| 3652 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3653 | void PlaybackThread::threadLoop_exit() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3654 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3655 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3656 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3657 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3658 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3659 | track->invalidate(); |
| 3660 | } |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 3661 | // Clear ActiveTracks to update BatteryNotifier in case active tracks remain. |
| 3662 | // After we exit there are no more track changes sent to BatteryNotifier |
| 3663 | // because that requires an active threadLoop. |
| 3664 | // TODO: should we decActiveTrackCnt() of the cleared track effect chain? |
| 3665 | mActiveTracks.clear(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3666 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3671 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3672 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 3673 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3674 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 3675 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3676 | - maxPeriod from frame count and sample rate (MIXER only) |
| 3677 | |
| 3678 | The parameters that affect these derived values are: |
| 3679 | - frame count |
| 3680 | - frame size |
| 3681 | - sample rate |
| 3682 | - device type: A2DP or not |
| 3683 | - device latency |
| 3684 | - format: PCM or not |
| 3685 | - active sleep time |
| 3686 | - idle sleep time |
| 3687 | */ |
| 3688 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3689 | void PlaybackThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3690 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3691 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3692 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 3693 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3694 | |
Andy Hung | d58c473 | 2023-07-20 21:31:38 -0700 | [diff] [blame] | 3695 | mStandbyDelayNs = getStandbyTimeInNanos(); |
Carter Hsu | 0ca47c2 | 2023-06-02 18:01:45 +0800 | [diff] [blame] | 3696 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3697 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 3698 | // truncating audio when going to standby. |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 3699 | if (!Intersection(outDeviceTypes_l(), getAudioDeviceOutAllA2dpSet()).empty()) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 3700 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 3701 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 3702 | } |
| 3703 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3704 | } |
| 3705 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3706 | bool PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3707 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3708 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3709 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3710 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3711 | size_t size = mTracks.size(); |
| 3712 | for (size_t i = 0; i < size; i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3713 | sp<IAfTrack> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3714 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 3715 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3716 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3717 | } |
| 3718 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 3719 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3720 | } |
| 3721 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3722 | void PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3723 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3724 | audio_utils::lock_guard _l(mutex()); |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 3725 | invalidateTracks_l(streamType); |
| 3726 | } |
| 3727 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3728 | void PlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3729 | audio_utils::lock_guard _l(mutex()); |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3730 | invalidateTracks_l(portIds); |
| 3731 | } |
| 3732 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3733 | bool PlaybackThread::invalidateTracks_l(std::set<audio_port_handle_t>& portIds) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3734 | bool trackMatch = false; |
| 3735 | const size_t size = mTracks.size(); |
| 3736 | for (size_t i = 0; i < size; i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3737 | sp<IAfTrack> t = mTracks[i]; |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 3738 | if (t->isExternalTrack() && portIds.find(t->portId()) != portIds.end()) { |
| 3739 | t->invalidate(); |
| 3740 | portIds.erase(t->portId()); |
| 3741 | trackMatch = true; |
| 3742 | } |
| 3743 | if (portIds.empty()) { |
| 3744 | break; |
| 3745 | } |
| 3746 | } |
| 3747 | return trackMatch; |
| 3748 | } |
| 3749 | |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 3750 | // getTrackById_l must be called with holding thread lock |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3751 | IAfTrack* PlaybackThread::getTrackById_l( |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 3752 | audio_port_handle_t trackPortId) { |
| 3753 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 3754 | if (mTracks[i]->portId() == trackPortId) { |
| 3755 | return mTracks[i].get(); |
| 3756 | } |
| 3757 | } |
| 3758 | return nullptr; |
| 3759 | } |
| 3760 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3761 | status_t PlaybackThread::addEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3762 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3763 | audio_session_t session = chain->sessionId(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3764 | sp<EffectBufferHalInterface> halInBuffer, halOutBuffer; |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3765 | float *buffer = nullptr; // only used for non global sessions |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3766 | |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 3767 | if (mType == SPATIALIZER) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3768 | if (!audio_is_global_session(session)) { |
| 3769 | // player sessions on a spatializer output will use a dedicated input buffer and |
| 3770 | // will either output multi channel to mEffectBuffer if the track is spatilaized |
| 3771 | // or stereo to mPostSpatializerBuffer if not spatialized. |
| 3772 | uint32_t channelMask; |
| 3773 | bool isSessionSpatialized = |
| 3774 | (hasAudioSession_l(session) & ThreadBase::SPATIALIZED_SESSION) != 0; |
| 3775 | if (isSessionSpatialized) { |
| 3776 | channelMask = mMixerChannelMask; |
| 3777 | } else { |
| 3778 | channelMask = mChannelMask; |
| 3779 | } |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 3780 | size_t numSamples = mNormalFrameCount |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3781 | * (audio_channel_count_from_out_mask(channelMask) + mHapticChannelCount); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3782 | status_t result = mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer( |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3783 | numSamples * sizeof(float), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3784 | &halInBuffer); |
| 3785 | if (result != OK) return result; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3786 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3787 | result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3788 | isSessionSpatialized ? mEffectBuffer : mPostSpatializerBuffer, |
| 3789 | isSessionSpatialized ? mEffectBufferSize : mPostSpatializerBufferSize, |
| 3790 | &halOutBuffer); |
| 3791 | if (result != OK) return result; |
| 3792 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3793 | buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer; |
Andy Hung | 2683692 | 2023-05-22 17:31:57 -0700 | [diff] [blame] | 3794 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3795 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 3796 | buffer, session); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3797 | } else { |
| 3798 | // A global session on a SPATIALIZER thread is either OUTPUT_STAGE or DEVICE |
| 3799 | // - OUTPUT_STAGE session uses the mEffectBuffer as input buffer and |
| 3800 | // mPostSpatializerBuffer as output buffer |
| 3801 | // - DEVICE session uses the mPostSpatializerBuffer as input and output buffer. |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3802 | status_t result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3803 | mEffectBuffer, mEffectBufferSize, &halInBuffer); |
| 3804 | if (result != OK) return result; |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3805 | result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3806 | mPostSpatializerBuffer, mPostSpatializerBufferSize, &halOutBuffer); |
| 3807 | if (result != OK) return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3808 | |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3809 | if (session == AUDIO_SESSION_DEVICE) { |
| 3810 | halInBuffer = halOutBuffer; |
| 3811 | } |
| 3812 | } |
| 3813 | } else { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3814 | status_t result = mAfThreadCallback->getEffectsFactoryHal()->mirrorBuffer( |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3815 | mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer, |
| 3816 | mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize, |
| 3817 | &halInBuffer); |
| 3818 | if (result != OK) return result; |
| 3819 | halOutBuffer = halInBuffer; |
| 3820 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 3821 | if (!audio_is_global_session(session)) { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3822 | buffer = halInBuffer ? reinterpret_cast<float*>(halInBuffer->externalData()) |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3823 | : buffer; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3824 | // Only one effect chain can be present in direct output thread and it uses |
| 3825 | // the sink buffer as input |
| 3826 | if (mType != DIRECT) { |
| 3827 | size_t numSamples = mNormalFrameCount |
| 3828 | * (audio_channel_count_from_out_mask(mMixerChannelMask) |
| 3829 | + mHapticChannelCount); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 3830 | const status_t allocateStatus = |
| 3831 | mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer( |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3832 | numSamples * sizeof(float), |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3833 | &halInBuffer); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3834 | if (allocateStatus != OK) return allocateStatus; |
Andy Hung | 2683692 | 2023-05-22 17:31:57 -0700 | [diff] [blame] | 3835 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 3836 | buffer = halInBuffer ? halInBuffer->audioBuffer()->f32 : buffer; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3837 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", |
| 3838 | buffer, session); |
| 3839 | } |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | if (!audio_is_global_session(session)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3844 | // Attach all tracks with same session ID to this chain. |
| 3845 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3846 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3847 | if (session == track->sessionId()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3848 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", |
| 3849 | track.get(), buffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3850 | track->setMainBuffer(buffer); |
| 3851 | chain->incTrackCnt(); |
| 3852 | } |
| 3853 | } |
| 3854 | |
| 3855 | // indicate all active tracks in the chain |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3856 | for (const sp<IAfTrack>& track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3857 | if (session == track->sessionId()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3858 | ALOGV("addEffectChain_l() activating track %p on session %d", |
| 3859 | track.get(), session); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3860 | chain->incActiveTrackCnt(); |
| 3861 | } |
| 3862 | } |
| 3863 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 3864 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3865 | chain->setThread(this); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 3866 | chain->setInBuffer(halInBuffer); |
| 3867 | chain->setOutBuffer(halOutBuffer); |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3868 | // Effect chain for session AUDIO_SESSION_DEVICE is inserted at end of effect |
| 3869 | // chains list in order to be processed last as it contains output device effects. |
| 3870 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted just before to apply post |
| 3871 | // processing effects specific to an output stream before effects applied to all streams |
| 3872 | // routed to a given device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3873 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 3874 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3875 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3876 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3877 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3878 | // Effect chain for other sessions are inserted at beginning of effect |
| 3879 | // 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] | 3880 | // sessions is not important. |
| 3881 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3882 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX && |
| 3883 | AUDIO_SESSION_DEVICE < AUDIO_SESSION_OUTPUT_STAGE, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3884 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3885 | size_t size = mEffectChains.size(); |
| 3886 | size_t i = 0; |
| 3887 | for (i = 0; i < size; i++) { |
| 3888 | if (mEffectChains[i]->sessionId() < session) { |
| 3889 | break; |
| 3890 | } |
| 3891 | } |
| 3892 | mEffectChains.insertAt(chain, i); |
| 3893 | checkSuspendOnAddEffectChain_l(chain); |
| 3894 | |
| 3895 | return NO_ERROR; |
| 3896 | } |
| 3897 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3898 | size_t PlaybackThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3899 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3900 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3901 | |
| 3902 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 3903 | |
| 3904 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3905 | if (chain == mEffectChains[i]) { |
| 3906 | mEffectChains.removeAt(i); |
| 3907 | // detach all active tracks from the chain |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3908 | for (const sp<IAfTrack>& track : mActiveTracks) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3909 | if (session == track->sessionId()) { |
| 3910 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 3911 | chain.get(), session); |
| 3912 | chain->decActiveTrackCnt(); |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | // detach all tracks with same session ID from this chain |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3917 | for (size_t j = 0; j < mTracks.size(); ++j) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3918 | sp<IAfTrack> track = mTracks[j]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3919 | if (session == track->sessionId()) { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 3920 | track->setMainBuffer(reinterpret_cast<float*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3921 | chain->decTrackCnt(); |
| 3922 | } |
| 3923 | } |
| 3924 | break; |
| 3925 | } |
| 3926 | } |
| 3927 | return mEffectChains.size(); |
| 3928 | } |
| 3929 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3930 | status_t PlaybackThread::attachAuxEffect( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3931 | const sp<IAfTrack>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3932 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 3933 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3934 | return attachAuxEffect_l(track, EffectId); |
| 3935 | } |
| 3936 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3937 | status_t PlaybackThread::attachAuxEffect_l( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3938 | const sp<IAfTrack>& track, int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3939 | { |
| 3940 | status_t status = NO_ERROR; |
| 3941 | |
| 3942 | if (EffectId == 0) { |
| 3943 | track->setAuxBuffer(0, NULL); |
| 3944 | } else { |
| 3945 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 3946 | sp<IAfEffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3947 | if (effect != 0) { |
| 3948 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 3949 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 3950 | } else { |
| 3951 | status = INVALID_OPERATION; |
| 3952 | } |
| 3953 | } else { |
| 3954 | status = BAD_VALUE; |
| 3955 | } |
| 3956 | } |
| 3957 | return status; |
| 3958 | } |
| 3959 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3960 | void PlaybackThread::detachAuxEffect_l(int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3961 | { |
| 3962 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3963 | sp<IAfTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3964 | if (track->auxEffectId() == effectId) { |
| 3965 | attachAuxEffect_l(track, 0); |
| 3966 | } |
| 3967 | } |
| 3968 | } |
| 3969 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 3970 | bool PlaybackThread::threadLoop() |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3971 | NO_THREAD_SAFETY_ANALYSIS // manual locking of AudioFlinger |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3972 | { |
Andy Hung | 78d8d95 | 2023-05-30 18:10:23 -0700 | [diff] [blame] | 3973 | aflog::setThreadWriter(mNBLogWriter.get()); |
Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 3974 | |
Andy Hung | 45a38f2 | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 3975 | if (mType == SPATIALIZER) { |
| 3976 | const pid_t tid = getTid(); |
| 3977 | if (tid == -1) { // odd: we are here, we must be a running thread. |
| 3978 | ALOGW("%s: Cannot update Spatializer mixer thread priority, no tid", __func__); |
| 3979 | } else { |
| 3980 | const int priorityBoost = requestSpatializerPriority(getpid(), tid); |
| 3981 | if (priorityBoost > 0) { |
| 3982 | stream()->setHalThreadPriority(priorityBoost); |
| 3983 | } |
| 3984 | } |
Pattara Teerapong | 9a332c5 | 2024-01-26 08:18:05 +0000 | [diff] [blame] | 3985 | } else if (property_get_bool("ro.boot.container", false /* default_value */)) { |
| 3986 | // In ARC experiments (b/73091832), the latency under using CFS scheduler with any priority |
| 3987 | // is not enough for PlaybackThread to process audio data in time. We request the lowest |
| 3988 | // real-time priority, SCHED_FIFO=1, for PlaybackThread in ARC. ro.boot.container is true |
| 3989 | // only on ARC. |
| 3990 | const pid_t tid = getTid(); |
| 3991 | if (tid == -1) { |
| 3992 | ALOGW("%s: Cannot update PlaybackThread priority for ARC, no tid", __func__); |
| 3993 | } else { |
| 3994 | const status_t status = requestPriority(getpid(), |
| 3995 | tid, |
| 3996 | kPriorityPlaybackThreadArc, |
| 3997 | false /* isForApp */, |
| 3998 | true /* asynchronous */); |
| 3999 | if (status != OK) { |
| 4000 | ALOGW("%s: Cannot update PlaybackThread priority for ARC, status %d", __func__, |
| 4001 | status); |
| 4002 | } else { |
| 4003 | stream()->setHalThreadPriority(kPriorityPlaybackThreadArc); |
| 4004 | } |
| 4005 | } |
Andy Hung | 45a38f2 | 2023-10-03 10:49:34 -0700 | [diff] [blame] | 4006 | } |
| 4007 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4008 | Vector<sp<IAfTrack>> tracksToRemove; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4009 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4010 | mStandbyTimeNs = systemTime(); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4011 | int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4012 | |
| 4013 | // MIXER |
| 4014 | nsecs_t lastWarning = 0; |
| 4015 | |
| 4016 | // DUPLICATING |
| 4017 | // FIXME could this be made local to while loop? |
| 4018 | writeFrames = 0; |
| 4019 | |
| 4020 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4021 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4022 | |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4023 | if (mType == MIXER || mType == SPATIALIZER) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4024 | sleepTimeShift = 0; |
| 4025 | } |
| 4026 | |
| 4027 | CpuStats cpuStats; |
| 4028 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 4029 | |
| 4030 | acquireWakeLock(); |
| 4031 | |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4032 | // mNBLogWriter logging APIs can only be called by a single thread, typically the |
| 4033 | // thread associated with this PlaybackThread. |
| 4034 | // If you want to share the mNBLogWriter with other threads (for example, binder threads) |
| 4035 | // 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] | 4036 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 4037 | // and then that string will be logged at the next convenient opportunity. |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4038 | // See reference to logString below. |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4039 | const char *logString = NULL; |
| 4040 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 4041 | // Estimated time for next buffer to be written to hal. This is used only on |
| 4042 | // suspended mode (for now) to help schedule the wait time until next iteration. |
| 4043 | nsecs_t timeLoopNextNs = 0; |
| 4044 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 4045 | checkSilentMode_l(); |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 4046 | |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4047 | audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 4048 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4049 | sendCheckOutputStageEffectsEvent(); |
| 4050 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4051 | // loopCount is used for statistics and diagnostics. |
| 4052 | for (int64_t loopCount = 0; !exitPending(); ++loopCount) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4053 | { |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 4054 | // Log merge requests are performed during AudioFlinger binder transactions, but |
| 4055 | // that does not cover audio playback. It's requested here for that reason. |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4056 | mAfThreadCallback->requestLogMerge(); |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 4057 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4058 | cpuStats.sample(myName); |
| 4059 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4060 | Vector<sp<IAfEffectChain>> effectChains; |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4061 | audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4062 | bool isHapticSessionSpatialized = false; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4063 | std::vector<sp<IAfTrack>> activeTracks; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4064 | |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4065 | // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency. |
| 4066 | // |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4067 | // Note: we access outDeviceTypes() outside of mutex(). |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4068 | if (isMsdDevice() && outDeviceTypes_l().count(AUDIO_DEVICE_OUT_BUS) != 0) { |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4069 | // Here, we try for the AF lock, but do not block on it as the latency |
| 4070 | // is more informational. |
Andy Hung | 85a0745 | 2023-08-28 18:36:53 -0700 | [diff] [blame] | 4071 | if (mAfThreadCallback->mutex().try_lock()) { |
Andy Hung | d25fe39 | 2023-07-13 16:52:46 -0700 | [diff] [blame] | 4072 | std::vector<SoftwarePatch> swPatches; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4073 | double latencyMs = 0.; // not required; initialized for clang-tidy |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4074 | status_t status = INVALID_OPERATION; |
| 4075 | audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4076 | if (mAfThreadCallback->getPatchPanel()->getDownstreamSoftwarePatches( |
Andy Hung | d25fe39 | 2023-07-13 16:52:46 -0700 | [diff] [blame] | 4077 | id(), &swPatches) == OK |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4078 | && swPatches.size() > 0) { |
| 4079 | status = swPatches[0].getLatencyMs_l(&latencyMs); |
| 4080 | downstreamPatchHandle = swPatches[0].getPatchHandle(); |
| 4081 | } |
| 4082 | if (downstreamPatchHandle != lastDownstreamPatchHandle) { |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4083 | mDownstreamLatencyStatMs.reset(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4084 | lastDownstreamPatchHandle = downstreamPatchHandle; |
| 4085 | } |
| 4086 | if (status == OK) { |
| 4087 | // verify downstream latency (we assume a max reasonable |
Mikhail Naganov | 6aa0a31 | 2018-11-09 11:00:01 -0800 | [diff] [blame] | 4088 | // latency of 5 seconds). |
| 4089 | const double minLatency = 0., maxLatency = 5000.; |
| 4090 | if (latencyMs >= minLatency && latencyMs <= maxLatency) { |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 4091 | ALOGVV("new downstream latency %lf ms", latencyMs); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4092 | } else { |
| 4093 | ALOGD("out of range downstream latency %lf ms", latencyMs); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4094 | latencyMs = std::clamp(latencyMs, minLatency, maxLatency); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4095 | } |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 4096 | mDownstreamLatencyStatMs.add(latencyMs); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4097 | } |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4098 | mAfThreadCallback->mutex().unlock(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4099 | } |
| 4100 | } else { |
| 4101 | if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4102 | // 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] | 4103 | mDownstreamLatencyStatMs.reset(); |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 4104 | lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4105 | } |
| 4106 | } |
| 4107 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4108 | if (mCheckOutputStageEffects.exchange(false)) { |
| 4109 | checkOutputStageEffects(); |
| 4110 | } |
| 4111 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 4112 | MetadataUpdate metadataUpdate; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4113 | { // scope for mutex() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4114 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4115 | audio_utils::unique_lock _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4116 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 4117 | processConfigEvents_l(); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 4118 | if (mCheckOutputStageEffects.load()) { |
| 4119 | continue; |
| 4120 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4121 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4122 | // See comment at declaration of logString for why this is done under mutex() |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 4123 | if (logString != NULL) { |
| 4124 | mNBLogWriter->logTimestamp(); |
| 4125 | mNBLogWriter->log(logString); |
| 4126 | logString = NULL; |
| 4127 | } |
| 4128 | |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4129 | collectTimestamps_l(); |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 4130 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4131 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4132 | if (mSignalPending) { |
| 4133 | // A signal was raised while we were unlocked |
| 4134 | mSignalPending = false; |
| 4135 | } else if (waitingAsyncCallback_l()) { |
| 4136 | if (exitPending()) { |
| 4137 | break; |
| 4138 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4139 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 4140 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4141 | releaseWakeLock_l(); |
| 4142 | released = true; |
| 4143 | } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4144 | |
| 4145 | const int64_t waitNs = computeWaitTimeNs_l(); |
| 4146 | ALOGV("wait async completion (wait time: %lld)", (long long)waitNs); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4147 | std::cv_status cvstatus = |
| 4148 | mWaitWorkCV.wait_for(_l, std::chrono::nanoseconds(waitNs)); |
| 4149 | if (cvstatus == std::cv_status::timeout) { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 4150 | mSignalPending = true; // if timeout recheck everything |
| 4151 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4152 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 4153 | if (released) { |
| 4154 | acquireWakeLock_l(); |
| 4155 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4156 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 4157 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 4158 | |
| 4159 | continue; |
| 4160 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 4161 | if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4162 | isSuspended()) { |
| 4163 | // put audio hardware into standby after short delay |
| 4164 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4165 | |
| 4166 | threadLoop_standby(); |
| 4167 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 4168 | // This is where we go into standby |
| 4169 | if (!mStandby) { |
| 4170 | LOG_AUDIO_STATE(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4171 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 4172 | mThreadSnapshot.onEnd(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 4173 | setStandby_l(); |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 4174 | } |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 4175 | sendStatistics(false /* force */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 4178 | if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4179 | // we're about to wait, flush the binder command buffer |
| 4180 | IPCThreadState::self()->flushCommands(); |
| 4181 | |
| 4182 | clearOutputTracks(); |
| 4183 | |
| 4184 | if (exitPending()) { |
| 4185 | break; |
| 4186 | } |
| 4187 | |
| 4188 | releaseWakeLock_l(); |
| 4189 | // wait until we have something to do... |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4190 | ALOGV("%s going to sleep", myName.c_str()); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4191 | mWaitWorkCV.wait(_l); |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 4192 | ALOGV("%s waking up", myName.c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4193 | acquireWakeLock_l(); |
| 4194 | |
| 4195 | mMixerStatus = MIXER_IDLE; |
| 4196 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 4197 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4198 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4199 | checkSilentMode_l(); |
| 4200 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4201 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 4202 | mSleepTimeUs = mIdleSleepTimeUs; |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4203 | if (mType == MIXER || mType == SPATIALIZER) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4204 | sleepTimeShift = 0; |
| 4205 | } |
| 4206 | |
| 4207 | continue; |
| 4208 | } |
| 4209 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4210 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 4211 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 4212 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4213 | mActiveTracks.updatePowerState_l(this); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 4214 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 4215 | metadataUpdate = updateMetadata_l(); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 4216 | |
Andy Hung | f302e81 | 2024-01-26 11:55:15 -0800 | [diff] [blame] | 4217 | // Acquire a local copy of active tracks with lock (release w/o lock). |
| 4218 | // |
| 4219 | // Control methods on the track acquire the ThreadBase lock (e.g. start() |
| 4220 | // stop(), pause(), etc.), but the threadLoop is entitled to call audio |
| 4221 | // data / buffer methods on tracks from activeTracks without the ThreadBase lock. |
| 4222 | activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end()); |
| 4223 | |
| 4224 | setHalLatencyMode_l(); |
| 4225 | |
| 4226 | // updateTeePatches_l will acquire the ThreadBase_Mutex of other threads, |
| 4227 | // so this is done before we lock our effect chains. |
| 4228 | for (const auto& track : mActiveTracks) { |
| 4229 | track->updateTeePatches_l(); |
| 4230 | } |
| 4231 | |
| 4232 | // signal actual start of output stream when the render position reported by |
| 4233 | // the kernel starts moving. |
| 4234 | if (!mHalStarted && ((isSuspended() && (mBytesWritten != 0)) || (!mStandby |
| 4235 | && (mKernelPositionOnStandby |
| 4236 | != mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL])))) { |
| 4237 | mHalStarted = true; |
| 4238 | mWaitHalStartCV.notify_all(); |
| 4239 | } |
| 4240 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4241 | // prevent any changes in effect chain list and in each effect chain |
| 4242 | // during mixing and effect process as the audio buffers could be deleted |
| 4243 | // or modified if an effect is created or deleted |
| 4244 | lockEffectChains_l(effectChains); |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4245 | |
| 4246 | // Determine which session to pick up haptic data. |
| 4247 | // This must be done under the same lock as prepareTracks_l(). |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4248 | // 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] | 4249 | // TODO: Write haptic data directly to sink buffer when mixing. |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4250 | if (mHapticChannelCount > 0) { |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4251 | for (const auto& track : mActiveTracks) { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4252 | sp<IAfEffectChain> effectChain = getEffectChain_l(track->sessionId()); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4253 | if (effectChain != nullptr |
| 4254 | && effectChain->containsHapticGeneratingEffect_l()) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4255 | activeHapticSessionId = track->sessionId(); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4256 | isHapticSessionSpatialized = |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4257 | mType == SPATIALIZER && track->isSpatialized(); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4258 | break; |
| 4259 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4260 | if (activeHapticSessionId == AUDIO_SESSION_NONE |
| 4261 | && track->getHapticPlaybackEnabled()) { |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4262 | activeHapticSessionId = track->sessionId(); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4263 | isHapticSessionSpatialized = |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4264 | mType == SPATIALIZER && track->isSpatialized(); |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4265 | } |
| 4266 | } |
| 4267 | } |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4268 | } // mutex() scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4269 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4270 | if (mBytesRemaining == 0) { |
| 4271 | mCurrentWriteLength = 0; |
| 4272 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 4273 | // threadLoop_mix() sets mCurrentWriteLength |
| 4274 | threadLoop_mix(); |
| 4275 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 4276 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4277 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4278 | // must be written to HAL |
| 4279 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4280 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4281 | mCurrentWriteLength = mSinkBufferSize; |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 4282 | |
| 4283 | // Tally underrun frames as we are inserting 0s here. |
| 4284 | for (const auto& track : activeTracks) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4285 | if (track->fillingStatus() == IAfTrack::FS_ACTIVE |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 4286 | && !track->isStopped() |
| 4287 | && !track->isPaused() |
| 4288 | && !track->isTerminated()) { |
| 4289 | ALOGV("%s: track(%d) %s underrun due to thread sleep of %zu frames", |
| 4290 | __func__, track->id(), track->getTrackStateAsString(), |
| 4291 | mNormalFrameCount); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4292 | track->audioTrackServerProxy()->tallyUnderrunFrames( |
| 4293 | mNormalFrameCount); |
Andy Hung | c164638 | 2019-04-30 16:12:10 -0700 | [diff] [blame] | 4294 | } |
| 4295 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4296 | } |
| 4297 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4298 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4299 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4300 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4301 | // or mSinkBuffer (if there are no effects and there is no data already copied to |
| 4302 | // mSinkBuffer). |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4303 | // |
| 4304 | // This is done pre-effects computation; if effects change to |
| 4305 | // support higher precision, this needs to move. |
| 4306 | // |
| 4307 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4308 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4309 | uint32_t mixerChannelCount = mEffectBufferValid ? |
| 4310 | audio_channel_count_from_out_mask(mMixerChannelMask) : mChannelCount; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4311 | if (mMixerBufferValid && (mEffectBufferValid || !mHasDataCopiedToSinkBuffer)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4312 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 4313 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 4314 | |
David Li | 88ee090 | 2022-06-22 10:01:21 +0800 | [diff] [blame] | 4315 | // Apply mono blending and balancing if the effect buffer is not valid. Otherwise, |
| 4316 | // do these processes after effects are applied. |
| 4317 | if (!mEffectBufferValid) { |
| 4318 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 4319 | // and is handled here if we're going directly to the sink. |
| 4320 | if (requireMonoBlend()) { |
| 4321 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, |
| 4322 | mNormalFrameCount, true /*limit*/); |
| 4323 | } |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4324 | |
David Li | 88ee090 | 2022-06-22 10:01:21 +0800 | [diff] [blame] | 4325 | if (!hasFastMixer()) { |
| 4326 | // Balance must take effect after mono conversion. |
| 4327 | // We do it here if there is no FastMixer. |
| 4328 | // mBalance detects zero balance within the class for speed |
| 4329 | // (not needed here). |
| 4330 | mBalance.setBalance(mMasterBalance.load()); |
| 4331 | mBalance.process((float *)mMixerBuffer, mNormalFrameCount); |
| 4332 | } |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4333 | } |
| 4334 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4335 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4336 | mNormalFrameCount * (mixerChannelCount + mHapticChannelCount)); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4337 | |
| 4338 | // If we're going directly to the sink and there are haptic channels, |
| 4339 | // we should adjust channels as the sample data is partially interleaved |
| 4340 | // in this case. |
| 4341 | if (!mEffectBufferValid && mHapticChannelCount > 0) { |
| 4342 | adjust_channels_non_destructive(buffer, mChannelCount, buffer, |
| 4343 | mChannelCount + mHapticChannelCount, |
| 4344 | audio_bytes_per_sample(format), |
| 4345 | audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount); |
| 4346 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4347 | } |
| 4348 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4349 | mBytesRemaining = mCurrentWriteLength; |
| 4350 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 4351 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 4352 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 4353 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 4354 | mBytesWritten += mBytesRemaining; |
| 4355 | mFramesWritten += framesRemaining; |
| 4356 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4357 | mBytesRemaining = 0; |
| 4358 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4359 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4360 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4361 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4362 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4363 | effectChains[i]->process_l(); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4364 | // TODO: Write haptic data directly to sink buffer when mixing. |
Andy Hung | 6e6a2e6 | 2019-04-30 16:38:41 -0700 | [diff] [blame] | 4365 | if (activeHapticSessionId != AUDIO_SESSION_NONE |
| 4366 | && activeHapticSessionId == effectChains[i]->sessionId()) { |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4367 | // Haptic data is active in this case, copy it directly from |
| 4368 | // in buffer to out buffer. |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4369 | uint32_t hapticSessionChannelCount = mEffectBufferValid ? |
| 4370 | audio_channel_count_from_out_mask(mMixerChannelMask) : |
| 4371 | mChannelCount; |
| 4372 | if (mType == SPATIALIZER && !isHapticSessionSpatialized) { |
| 4373 | hapticSessionChannelCount = mChannelCount; |
| 4374 | } |
| 4375 | |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4376 | const size_t audioBufferSize = mNormalFrameCount |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4377 | * audio_bytes_per_frame(hapticSessionChannelCount, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4378 | AUDIO_FORMAT_PCM_FLOAT); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4379 | memcpy_by_audio_format( |
| 4380 | (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4381 | AUDIO_FORMAT_PCM_FLOAT, |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4382 | (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 4383 | AUDIO_FORMAT_PCM_FLOAT, mNormalFrameCount * mHapticChannelCount); |
jiabin | 47affe5 | 2019-04-04 18:02:07 -0700 | [diff] [blame] | 4384 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4385 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4386 | } |
| 4387 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 4388 | // Process effect chains for offloaded thread even if no audio |
| 4389 | // was read from audio track: process only updates effect state |
| 4390 | // and thus does have to be synchronized with audio writes but may have |
| 4391 | // to be called while waiting for async write callback |
| 4392 | if (mType == OFFLOAD) { |
| 4393 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4394 | effectChains[i]->process_l(); |
| 4395 | } |
| 4396 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4397 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4398 | // Only if the Effects buffer is enabled and there is data in the |
| 4399 | // Effects buffer (buffer valid), we need to |
| 4400 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4401 | // TODO use mSleepTimeUs == 0 as an additional condition. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4402 | if (mEffectBufferValid && !mHasDataCopiedToSinkBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4403 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4404 | void *effectBuffer = (mType == SPATIALIZER) ? mPostSpatializerBuffer : mEffectBuffer; |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4405 | if (requireMonoBlend()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4406 | mono_blend(effectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 4407 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4408 | } |
| 4409 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4410 | if (!hasFastMixer()) { |
| 4411 | // Balance must take effect after mono conversion. |
| 4412 | // We do it here if there is no FastMixer. |
| 4413 | // mBalance detects zero balance within the class for speed (not needed here). |
| 4414 | mBalance.setBalance(mMasterBalance.load()); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4415 | mBalance.process((float *)effectBuffer, mNormalFrameCount); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 4418 | // for SPATIALIZER thread, Move haptics channels from mEffectBuffer to |
| 4419 | // mPostSpatializerBuffer if the haptics track is spatialized. |
| 4420 | // Otherwise, the haptics channels are already in mPostSpatializerBuffer. |
| 4421 | // For other thread types, the haptics channels are already in mEffectBuffer. |
| 4422 | if (mType == SPATIALIZER && isHapticSessionSpatialized) { |
| 4423 | const size_t srcBufferSize = mNormalFrameCount * |
| 4424 | audio_bytes_per_frame(audio_channel_count_from_out_mask(mMixerChannelMask), |
| 4425 | mEffectBufferFormat); |
| 4426 | const size_t dstBufferSize = mNormalFrameCount |
| 4427 | * audio_bytes_per_frame(mChannelCount, mEffectBufferFormat); |
| 4428 | |
| 4429 | memcpy_by_audio_format((uint8_t*)mPostSpatializerBuffer + dstBufferSize, |
| 4430 | mEffectBufferFormat, |
| 4431 | (uint8_t*)mEffectBuffer + srcBufferSize, |
| 4432 | mEffectBufferFormat, |
| 4433 | mNormalFrameCount * mHapticChannelCount); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 4434 | } |
Atneya Nair | ba9a107 | 2022-09-19 17:51:37 -0700 | [diff] [blame] | 4435 | const size_t framesToCopy = mNormalFrameCount * (mChannelCount + mHapticChannelCount); |
| 4436 | if (mFormat == AUDIO_FORMAT_PCM_FLOAT && |
| 4437 | mEffectBufferFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 4438 | // Clamp PCM float values more than this distance from 0 to insulate |
| 4439 | // a HAL which doesn't handle NaN correctly. |
| 4440 | static constexpr float HAL_FLOAT_SAMPLE_LIMIT = 2.0f; |
| 4441 | memcpy_to_float_from_float_with_clamping(static_cast<float*>(mSinkBuffer), |
| 4442 | static_cast<const float*>(effectBuffer), |
| 4443 | framesToCopy, HAL_FLOAT_SAMPLE_LIMIT /* absMax */); |
| 4444 | } else { |
| 4445 | memcpy_by_audio_format(mSinkBuffer, mFormat, |
| 4446 | effectBuffer, mEffectBufferFormat, framesToCopy); |
| 4447 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4448 | // The sample data is partially interleaved when haptic channels exist, |
| 4449 | // we need to adjust channels here. |
| 4450 | if (mHapticChannelCount > 0) { |
| 4451 | adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer, |
| 4452 | mChannelCount + mHapticChannelCount, |
| 4453 | audio_bytes_per_sample(mFormat), |
| 4454 | audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount); |
| 4455 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4456 | } |
| 4457 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4458 | // enable changes in effect chain |
| 4459 | unlockEffectChains(effectChains); |
| 4460 | |
Vlad Popa | fce1086 | 2023-02-03 10:37:07 +0100 | [diff] [blame] | 4461 | if (!metadataUpdate.playbackMetadataUpdate.empty()) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 4462 | mAfThreadCallback->getMelReporter()->updateMetadataForCsd(id(), |
Vlad Popa | fce1086 | 2023-02-03 10:37:07 +0100 | [diff] [blame] | 4463 | metadataUpdate.playbackMetadataUpdate); |
| 4464 | } |
| 4465 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4466 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4467 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 4468 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4469 | ssize_t ret = 0; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4470 | // writePeriodNs is updated >= 0 when ret > 0. |
| 4471 | int64_t writePeriodNs = -1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4472 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 4473 | // FIXME rewrite to reduce number of system calls |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4474 | const int64_t lastIoBeginNs = systemTime(); |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4475 | ret = threadLoop_write(); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4476 | const int64_t lastIoEndNs = systemTime(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4477 | if (ret < 0) { |
| 4478 | mBytesRemaining = 0; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4479 | } else if (ret > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4480 | mBytesWritten += ret; |
| 4481 | mBytesRemaining -= ret; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4482 | const int64_t frames = ret / mFrameSize; |
| 4483 | mFramesWritten += frames; |
| 4484 | |
| 4485 | writePeriodNs = lastIoEndNs - mLastIoEndNs; |
| 4486 | // process information relating to write time. |
| 4487 | if (audio_has_proportional_frames(mFormat)) { |
| 4488 | // we are in a continuous mixing cycle |
| 4489 | if (mMixerStatus == MIXER_TRACKS_READY && |
| 4490 | loopCount == lastLoopCountWritten + 1) { |
| 4491 | |
| 4492 | const double jitterMs = |
| 4493 | TimestampVerifier<int64_t, int64_t>::computeJitterMs( |
| 4494 | {frames, writePeriodNs}, |
| 4495 | {0, 0} /* lastTimestamp */, mSampleRate); |
| 4496 | const double processMs = |
| 4497 | (lastIoBeginNs - mLastIoEndNs) * 1e-6; |
| 4498 | |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 4499 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4500 | mIoJitterMs.add(jitterMs); |
| 4501 | mProcessTimeMs.add(processMs); |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 4502 | |
| 4503 | if (mPipeSink.get() != nullptr) { |
| 4504 | // Using the Monopipe availableToWrite, we estimate the current |
| 4505 | // buffer size. |
| 4506 | MonoPipe* monoPipe = static_cast<MonoPipe*>(mPipeSink.get()); |
| 4507 | const ssize_t |
| 4508 | availableToWrite = mPipeSink->availableToWrite(); |
| 4509 | const size_t pipeFrames = monoPipe->maxFrames(); |
| 4510 | const size_t |
| 4511 | remainingFrames = pipeFrames - max(availableToWrite, 0); |
| 4512 | mMonopipePipeDepthStats.add(remainingFrames); |
| 4513 | } |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | // write blocked detection |
| 4517 | const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs; |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4518 | if ((mType == MIXER || mType == SPATIALIZER) |
| 4519 | && deltaWriteNs > maxPeriod) { |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4520 | mNumDelayedWrites++; |
| 4521 | if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) { |
| 4522 | ATRACE_NAME("underrun"); |
| 4523 | ALOGW("write blocked for %lld msecs, " |
| 4524 | "%d delayed writes, thread %d", |
| 4525 | (long long)deltaWriteNs / NANOS_PER_MILLISECOND, |
| 4526 | mNumDelayedWrites, mId); |
| 4527 | lastWarning = lastIoEndNs; |
| 4528 | } |
| 4529 | } |
| 4530 | } |
| 4531 | // update timing info. |
| 4532 | mLastIoBeginNs = lastIoBeginNs; |
| 4533 | mLastIoEndNs = lastIoEndNs; |
| 4534 | lastLoopCountWritten = loopCount; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4535 | } |
| 4536 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 4537 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 4538 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4539 | } |
Andy Hung | d363992 | 2022-04-28 18:00:49 -0700 | [diff] [blame] | 4540 | if ((mType == MIXER || mType == SPATIALIZER) && !mStandby) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4541 | |
| 4542 | if (mThreadThrottle |
| 4543 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4544 | && writePeriodNs > 0) { // we have write period info |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4545 | // Limit MixerThread data processing to no more than twice the |
| 4546 | // expected processing rate. |
| 4547 | // |
| 4548 | // This helps prevent underruns with NuPlayer and other applications |
| 4549 | // which may set up buffers that are close to the minimum size, or use |
| 4550 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 4551 | // |
| 4552 | // The throttle smooths out sudden large data drains from the device, |
| 4553 | // e.g. when it comes out of standby, which often causes problems with |
| 4554 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 4555 | // (2) minimum buffer sized tracks (even if the track is full, |
| 4556 | // 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] | 4557 | // |
| 4558 | // Total time spent in last processing cycle equals time spent in |
| 4559 | // 1. threadLoop_write, as well as time spent in |
| 4560 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 4561 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4562 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4563 | // it's OK if deltaMs is an overestimate. |
| 4564 | |
| 4565 | const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND; |
Ivan Lozano | e02bc54 | 2017-10-26 09:51:54 -0700 | [diff] [blame] | 4566 | |
Ivan Lozano | ea04d39 | 2017-11-07 14:37:07 -0800 | [diff] [blame] | 4567 | const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4568 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4569 | mThreadMetrics.logThrottleMs((double)throttleMs); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 4570 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4571 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4572 | // notify of throttle start on verbose log |
| 4573 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 4574 | "mixer(%p) throttle begin:" |
| 4575 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4576 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4577 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 4578 | // Throttle must be attributed to the previous mixer loop's write time |
| 4579 | // to allow back-to-back throttling. |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 4580 | // This also ensures proper timing statistics. |
| 4581 | mLastIoEndNs = systemTime(); // we fetch the write end time again. |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4582 | } else { |
| 4583 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 4584 | if (diff > 0) { |
| 4585 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 4586 | // but prevent spamming for bluetooth |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4587 | ALOGD_IF(!isSingleDeviceType( |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4588 | outDeviceTypes_l(), audio_is_a2dp_out_device) && |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4589 | !isSingleDeviceType( |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4590 | outDeviceTypes_l(), |
| 4591 | audio_is_hearing_aid_out_device), |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 4592 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4593 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 4594 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4595 | } |
| 4596 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4597 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4598 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4599 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4600 | ATRACE_BEGIN("sleep"); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4601 | audio_utils::unique_lock _l(mutex()); |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 4602 | // suspended requires accurate metering of sleep time. |
| 4603 | if (isSuspended()) { |
| 4604 | // advance by expected sleepTime |
| 4605 | timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs); |
| 4606 | const nsecs_t nowNs = systemTime(); |
| 4607 | |
| 4608 | // compute expected next time vs current time. |
| 4609 | // (negative deltas are treated as delays). |
| 4610 | nsecs_t deltaNs = timeLoopNextNs - nowNs; |
| 4611 | if (deltaNs < -kMaxNextBufferDelayNs) { |
| 4612 | // Delays longer than the max allowed trigger a reset. |
| 4613 | ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs); |
| 4614 | deltaNs = microseconds((nsecs_t)mSleepTimeUs); |
| 4615 | timeLoopNextNs = nowNs + deltaNs; |
| 4616 | } else if (deltaNs < 0) { |
| 4617 | // Delays within the max delay allowed: zero the delta/sleepTime |
| 4618 | // to help the system catch up in the next iteration(s) |
| 4619 | ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs); |
| 4620 | deltaNs = 0; |
| 4621 | } |
| 4622 | // update sleep time (which is >= 0) |
| 4623 | mSleepTimeUs = deltaNs / 1000; |
| 4624 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4625 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4626 | mWaitWorkCV.wait_for(_l, std::chrono::microseconds(mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 4627 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4628 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4629 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | // Finally let go of removed track(s), without the lock held |
| 4633 | // since we can't guarantee the destructors won't acquire that |
| 4634 | // same lock. This will also mutate and push a new fast mixer state. |
| 4635 | threadLoop_removeTracks(tracksToRemove); |
| 4636 | tracksToRemove.clear(); |
| 4637 | |
| 4638 | // FIXME I don't understand the need for this here; |
| 4639 | // it was in the original code but maybe the |
| 4640 | // assignment in saveOutputTracks() makes this unnecessary? |
| 4641 | clearOutputTracks(); |
| 4642 | |
| 4643 | // Effect chains will be actually deleted here if they were removed from |
| 4644 | // mEffectChains list during mixing or effects processing |
| 4645 | effectChains.clear(); |
| 4646 | |
| 4647 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 4648 | // is now local to this block, but will keep it for now (at least until merge done). |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 4649 | |
| 4650 | mThreadloopExecutor.process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4651 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 4652 | mThreadloopExecutor.process(); // process any remaining deferred actions. |
| 4653 | // deferred actions after this point are ignored. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4654 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4655 | threadLoop_exit(); |
| 4656 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 4657 | if (!mStandby) { |
| 4658 | threadLoop_standby(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 4659 | setStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | releaseWakeLock(); |
| 4663 | |
| 4664 | ALOGV("Thread %p type %d exiting", this, mType); |
| 4665 | return false; |
| 4666 | } |
| 4667 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4668 | void PlaybackThread::collectTimestamps_l() |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4669 | { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4670 | if (mStandby) { |
| 4671 | mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); |
| 4672 | return; |
| 4673 | } else if (mHwPaused) { |
| 4674 | mTimestampVerifier.discontinuity(mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS); |
| 4675 | return; |
| 4676 | } |
| 4677 | |
| 4678 | // Gather the framesReleased counters for all active tracks, |
| 4679 | // and associate with the sink frames written out. We need |
| 4680 | // this to convert the sink timestamp to the track timestamp. |
| 4681 | bool kernelLocationUpdate = false; |
| 4682 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 4683 | |
| 4684 | // Always query HAL timestamp and update timestamp verifier. In standby or pause, |
| 4685 | // HAL may be draining some small duration buffered data for fade out. |
| 4686 | if (threadloop_getHalTimestamp_l(×tamp) == OK) { |
| 4687 | mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 4688 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4689 | mSampleRate); |
| 4690 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 4691 | if (isTimestampCorrectionEnabled_l()) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4692 | ALOGVV("TS_BEFORE: %d %lld %lld", id(), |
| 4693 | (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4694 | (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); |
| 4695 | auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); |
| 4696 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4697 | = correctedTimestamp.mFrames; |
| 4698 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] |
| 4699 | = correctedTimestamp.mTimeNs; |
| 4700 | ALOGVV("TS_AFTER: %d %lld %lld", id(), |
| 4701 | (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
| 4702 | (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]); |
| 4703 | |
| 4704 | // Note: Downstream latency only added if timestamp correction enabled. |
| 4705 | if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info. |
| 4706 | const int64_t newPosition = |
| 4707 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4708 | - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3); |
| 4709 | // prevent retrograde |
| 4710 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max( |
| 4711 | newPosition, |
| 4712 | (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4713 | - mSuspendedFrames)); |
| 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | // We always fetch the timestamp here because often the downstream |
| 4718 | // sink will block while writing. |
| 4719 | |
| 4720 | // We keep track of the last valid kernel position in case we are in underrun |
| 4721 | // and the normal mixer period is the same as the fast mixer period, or there |
| 4722 | // is some error from the HAL. |
| 4723 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 4724 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 4725 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4726 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 4727 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4728 | |
| 4729 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 4730 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 4731 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 4732 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
| 4733 | } |
| 4734 | |
| 4735 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 4736 | kernelLocationUpdate = true; |
| 4737 | } else { |
| 4738 | ALOGVV("getTimestamp error - no valid kernel position"); |
| 4739 | } |
| 4740 | |
| 4741 | // copy over kernel info |
| 4742 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
| 4743 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 4744 | + mSuspendedFrames; // add frames discarded when suspended |
| 4745 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 4746 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4747 | } else { |
| 4748 | mTimestampVerifier.error(); |
| 4749 | } |
| 4750 | |
| 4751 | // mFramesWritten for non-offloaded tracks are contiguous |
| 4752 | // even after standby() is called. This is useful for the track frame |
| 4753 | // to sink frame mapping. |
| 4754 | bool serverLocationUpdate = false; |
| 4755 | if (mFramesWritten != mLastFramesWritten) { |
| 4756 | serverLocationUpdate = true; |
| 4757 | mLastFramesWritten = mFramesWritten; |
| 4758 | } |
| 4759 | // Only update timestamps if there is a meaningful change. |
| 4760 | // Either the kernel timestamp must be valid or we have written something. |
| 4761 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 4762 | if (serverLocationUpdate) { |
| 4763 | // use the time before we called the HAL write - it is a bit more accurate |
| 4764 | // to when the server last read data than the current time here. |
| 4765 | // |
| 4766 | // If we haven't written anything, mLastIoBeginNs will be -1 |
| 4767 | // and we use systemTime(). |
| 4768 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 4769 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1 |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 4770 | ? systemTime() : (int64_t)mLastIoBeginNs; |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4771 | } |
| 4772 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4773 | for (const sp<IAfTrack>& t : mActiveTracks) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4774 | if (!t->isFastTrack()) { |
| 4775 | t->updateTrackFrameInfo( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 4776 | t->audioTrackServerProxy()->framesReleased(), |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4777 | mFramesWritten, |
| 4778 | mSampleRate, |
| 4779 | mTimestamp); |
| 4780 | } |
| 4781 | } |
| 4782 | } |
| 4783 | |
| 4784 | if (audio_has_proportional_frames(mFormat)) { |
| 4785 | const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate); |
| 4786 | if (latencyMs != 0.) { // note 0. means timestamp is empty. |
| 4787 | mLatencyMs.add(latencyMs); |
| 4788 | } |
| 4789 | } |
| 4790 | #if 0 |
| 4791 | // logFormat example |
| 4792 | if (z % 100 == 0) { |
| 4793 | timespec ts; |
| 4794 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 4795 | LOGT("This is an integer %d, this is a float %f, this is my " |
| 4796 | "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts); |
| 4797 | LOGT("A deceptive null-terminated string %\0"); |
| 4798 | } |
| 4799 | ++z; |
| 4800 | #endif |
| 4801 | } |
| 4802 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4803 | // removeTracks_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4804 | void PlaybackThread::removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove) |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4805 | NO_THREAD_SAFETY_ANALYSIS // release and re-acquire mutex() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4806 | { |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4807 | if (tracksToRemove.empty()) return; |
| 4808 | |
| 4809 | // Block all incoming TrackHandle requests until we are finished with the release. |
| 4810 | setThreadBusy_l(true); |
| 4811 | |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4812 | for (const auto& track : tracksToRemove) { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4813 | ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 4814 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4815 | if (chain != 0) { |
| 4816 | ALOGV("%s(%d): stopping track on chain %p for session Id: %d", |
| 4817 | __func__, track->id(), chain.get(), track->sessionId()); |
| 4818 | chain->decActiveTrackCnt(); |
| 4819 | } |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4820 | |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4821 | // If an external client track, inform APM we're no longer active, and remove if needed. |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4822 | // Since the track is active, we do it here instead of TrackBase::destroy(). |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4823 | if (track->isExternalTrack()) { |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4824 | mutex().unlock(); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4825 | AudioSystem::stopOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4826 | if (track->isTerminated()) { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4827 | AudioSystem::releaseOutput(track->portId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4828 | } |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4829 | mutex().lock(); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 4830 | } |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 4831 | if (mHapticChannelCount > 0 && |
| 4832 | ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
Shunkai Yao | 29d1057 | 2024-03-19 04:31:47 +0000 | [diff] [blame] | 4833 | || (chain != nullptr && chain->containsHapticGeneratingEffect()))) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4834 | mutex().unlock(); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 4835 | // Unlock due to VibratorService will lock for this call and will |
| 4836 | // call Tracks.mute/unmute which also require thread's lock. |
Andy Hung | 76cb915 | 2023-07-20 21:23:42 -0700 | [diff] [blame] | 4837 | afutils::onExternalVibrationStop(track->getExternalVibration()); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 4838 | mutex().lock(); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4839 | |
| 4840 | // When the track is stop, set the haptic intensity as MUTE |
| 4841 | // for the HapticGenerator effect. |
| 4842 | if (chain != nullptr) { |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 4843 | chain->setHapticScale_l(track->id(), os::HapticScale::mute()); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 4844 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 4845 | } |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4846 | |
| 4847 | // Under lock, the track is removed from the active tracks list. |
| 4848 | // |
| 4849 | // Once the track is no longer active, the TrackHandle may directly |
| 4850 | // modify it as the threadLoop() is no longer responsible for its maintenance. |
| 4851 | // Do not modify the track from threadLoop after the mutex is unlocked |
| 4852 | // if it is not active. |
| 4853 | mActiveTracks.remove(track); |
| 4854 | |
| 4855 | if (track->isTerminated()) { |
| 4856 | // remove from our tracks vector |
| 4857 | removeTrack_l(track); |
| 4858 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4859 | } |
Andy Hung | a718771 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 4860 | |
| 4861 | // Allow incoming TrackHandle requests. We still hold the mutex, |
| 4862 | // so pending TrackHandle requests will occur after we unlock it. |
| 4863 | setThreadBusy_l(false); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4864 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4865 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4866 | status_t PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4867 | { |
| 4868 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4869 | ExtendedTimestamp ets; |
| 4870 | status_t status = mNormalSink->getTimestamp(ets); |
| 4871 | if (status == NO_ERROR) { |
| 4872 | status = ets.getBestTimestamp(×tamp); |
| 4873 | } |
| 4874 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4875 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 4876 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4877 | collectTimestamps_l(); |
| 4878 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] <= 0) { |
| 4879 | return INVALID_OPERATION; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4880 | } |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 4881 | timestamp.mPosition = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4882 | const int64_t timeNs = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 4883 | timestamp.mTime.tv_sec = timeNs / NANOS_PER_SECOND; |
| 4884 | timestamp.mTime.tv_nsec = timeNs - (timestamp.mTime.tv_sec * NANOS_PER_SECOND); |
| 4885 | return NO_ERROR; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 4886 | } |
| 4887 | return INVALID_OPERATION; |
| 4888 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4889 | |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4890 | // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is |
| 4891 | // still applied by the mixer. |
| 4892 | // All tracks attached to a mixer with flag VOIP_RX are tied to the same |
| 4893 | // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even |
| 4894 | // if more than one track are active |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4895 | status_t PlaybackThread::handleVoipVolume_l(float* volume) |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4896 | { |
| 4897 | status_t result = NO_ERROR; |
| 4898 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { |
| 4899 | if (*volume != mLeftVolFloat) { |
| 4900 | result = mOutput->stream->setVolume(*volume, *volume); |
Alex Leung | c5dedb2 | 2023-05-10 08:00:50 -0700 | [diff] [blame] | 4901 | // HAL can return INVALID_OPERATION if operation is not supported. |
| 4902 | ALOGE_IF(result != OK && result != INVALID_OPERATION, |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 4903 | "Error when setting output stream volume: %d", result); |
| 4904 | if (result == NO_ERROR) { |
| 4905 | mLeftVolFloat = *volume; |
| 4906 | } |
| 4907 | } |
| 4908 | // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we |
| 4909 | // remove stream volume contribution from software volume. |
| 4910 | if (mLeftVolFloat == *volume) { |
| 4911 | *volume = 1.0f; |
| 4912 | } |
| 4913 | } |
| 4914 | return result; |
| 4915 | } |
| 4916 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4917 | status_t MixerThread::createAudioPatch_l(const struct audio_patch* patch, |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4918 | audio_patch_handle_t *handle) |
| 4919 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 4920 | status_t status; |
| 4921 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 4922 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 4923 | // or if HAL does not properly lock against access. |
| 4924 | AutoPark<FastMixer> park(mFastMixer); |
| 4925 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 4926 | } else { |
| 4927 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 4928 | } |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 4929 | |
| 4930 | updateHalSupportedLatencyModes_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4931 | return status; |
| 4932 | } |
| 4933 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 4934 | status_t PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4935 | audio_patch_handle_t *handle) |
| 4936 | { |
| 4937 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4938 | |
| 4939 | // store new device and send to effects |
| 4940 | audio_devices_t type = AUDIO_DEVICE_NONE; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4941 | AudioDeviceTypeAddrVector deviceTypeAddrs; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4942 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4943 | LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 |
| 4944 | && !mOutput->audioHwDev->supportsAudioPatches(), |
| 4945 | "Enumerated device type(%#x) must not be used " |
| 4946 | "as it does not support audio patches", |
| 4947 | patch->sinks[i].ext.device.type); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 4948 | type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 4949 | deviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type, |
| 4950 | patch->sinks[i].ext.device.address); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4951 | } |
| 4952 | |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 4953 | audio_port_handle_t sinkPortId = patch->sinks[0].id; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4954 | #ifdef ADD_BATTERY_DATA |
| 4955 | // when changing the audio output device, call addBatteryData to notify |
| 4956 | // the change |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4957 | if (outDeviceTypes() != deviceTypes) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4958 | uint32_t params = 0; |
| 4959 | // check whether speaker is on |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4960 | if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) > 0) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4961 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4962 | } |
| 4963 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4964 | // check if any other device (except speaker) is on |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4965 | if (!isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_SPEAKER)) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4966 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4967 | } |
| 4968 | |
| 4969 | if (params != 0) { |
| 4970 | addBatteryData(params); |
| 4971 | } |
| 4972 | } |
| 4973 | #endif |
| 4974 | |
| 4975 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 4976 | mEffectChains[i]->setDevices_l(deviceTypeAddrs); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4977 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 4978 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4979 | // mPatch.num_sinks is not set when the thread is created so that |
| 4980 | // the first patch creation triggers an ioConfigChanged callback |
| 4981 | bool configChanged = (mPatch.num_sinks == 0) || |
| 4982 | (mPatch.sinks[0].id != sinkPortId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 4983 | mPatch = *patch; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 4984 | mOutDeviceTypeAddrs = deviceTypeAddrs; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 4985 | checkSilentMode_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4986 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 4987 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 4988 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 4989 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 4990 | patch->sources, |
| 4991 | patch->num_sinks, |
| 4992 | patch->sinks, |
| 4993 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4994 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 4995 | status = mOutput->stream->legacyCreateAudioPatch(patch->sinks[0], std::nullopt, type); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4996 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4997 | } |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 4998 | const std::string patchSinksAsString = patchSinksToString(patch); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 4999 | |
| 5000 | mThreadMetrics.logEndInterval(); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5001 | mThreadMetrics.logCreatePatch(/* inDevices */ {}, patchSinksAsString); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 5002 | mThreadMetrics.logBeginInterval(); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5003 | // also dispatch to active AudioTracks for MediaMetrics |
| 5004 | for (const auto &track : mActiveTracks) { |
| 5005 | track->logEndInterval(); |
| 5006 | track->logBeginInterval(patchSinksAsString); |
| 5007 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5008 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 5009 | if (configChanged) { |
| 5010 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 5011 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 5012 | // Force metadata update after a route change |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 5013 | mActiveTracks.setHasChanged(); |
| 5014 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5015 | return status; |
| 5016 | } |
| 5017 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5018 | status_t MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5019 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 5020 | status_t status; |
| 5021 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 5022 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 5023 | // or if HAL does not properly lock against access. |
| 5024 | AutoPark<FastMixer> park(mFastMixer); |
| 5025 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 5026 | } else { |
| 5027 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 5028 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5029 | return status; |
| 5030 | } |
| 5031 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5032 | status_t PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5033 | { |
| 5034 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5035 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5036 | mPatch = audio_patch{}; |
| 5037 | mOutDeviceTypeAddrs.clear(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 5038 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 5039 | if (mOutput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 5040 | sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice(); |
| 5041 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5042 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 5043 | status = mOutput->stream->legacyReleaseAudioPatch(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5044 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 5045 | // Force meteadata update after a route change |
| 5046 | mActiveTracks.setHasChanged(); |
| 5047 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5048 | return status; |
| 5049 | } |
| 5050 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5051 | void PlaybackThread::addPatchTrack(const sp<IAfPatchTrack>& track) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5052 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5053 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5054 | mTracks.add(track); |
| 5055 | } |
| 5056 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5057 | void PlaybackThread::deletePatchTrack(const sp<IAfPatchTrack>& track) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5058 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5059 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5060 | destroyTrack_l(track); |
| 5061 | } |
| 5062 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5063 | void PlaybackThread::toAudioPortConfig(struct audio_port_config* config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5064 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5065 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5066 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 5067 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 5068 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 5069 | if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { |
| 5070 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 5071 | config->flags.output = mOutput->flags; |
| 5072 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5075 | // ---------------------------------------------------------------------------- |
| 5076 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5077 | /* static */ |
| 5078 | sp<IAfPlaybackThread> IAfPlaybackThread::createMixerThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5079 | const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5080 | audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t* mixerConfig) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5081 | return sp<MixerThread>::make(afThreadCallback, output, id, systemReady, type, mixerConfig); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5082 | } |
| 5083 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5084 | MixerThread::MixerThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 5085 | audio_io_handle_t id, bool systemReady, type_t type, audio_config_base_t *mixerConfig) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5086 | : PlaybackThread(afThreadCallback, output, id, type, systemReady, mixerConfig), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5087 | // mAudioMixer below |
| 5088 | // mFastMixer below |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5089 | mBluetoothLatencyModesEnabled(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5090 | mFastMixerFutex(0), |
| 5091 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5092 | // mOutputSink below |
| 5093 | // mPipeSink below |
| 5094 | // mNormalSink below |
| 5095 | { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 5096 | ALOGV("MixerThread() id=%d type=%d", id, type); |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 5097 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5098 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5099 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 5100 | mNormalFrameCount); |
| 5101 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 5102 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 5103 | if (type == DUPLICATING) { |
| 5104 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 5105 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 5106 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
Andy Hung | 7de02c9 | 2024-06-25 17:07:58 -0700 | [diff] [blame] | 5107 | // Balance is *not* set in the DuplicatingThread here (or from AudioFlinger), |
| 5108 | // as the downstream MixerThreads implement it. |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 5109 | return; |
| 5110 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5111 | // create an NBAIO sink for the HAL output stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 5112 | mOutputSink = new AudioStreamOutSink(output->stream); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5113 | size_t numCounterOffers = 0; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5114 | const NBAIO_Format offers[1] = {Format_from_SR_C( |
| 5115 | mSampleRate, mChannelCount + mHapticChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5116 | #if !LOG_NDEBUG |
| 5117 | ssize_t index = |
| 5118 | #else |
| 5119 | (void) |
| 5120 | #endif |
| 5121 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5122 | ALOG_ASSERT(index == 0); |
| 5123 | |
| 5124 | // initialize fast mixer depending on configuration |
| 5125 | bool initFastMixer; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 5126 | if (mType == SPATIALIZER || mType == BIT_PERFECT) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5127 | initFastMixer = false; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 5128 | } else { |
| 5129 | switch (kUseFastMixer) { |
| 5130 | case FastMixer_Never: |
| 5131 | initFastMixer = false; |
| 5132 | break; |
| 5133 | case FastMixer_Always: |
| 5134 | initFastMixer = true; |
| 5135 | break; |
| 5136 | case FastMixer_Static: |
| 5137 | case FastMixer_Dynamic: |
Henrik Tillman | 470b399 | 2024-10-08 12:49:28 +0200 | [diff] [blame^] | 5138 | if (mType == MIXER && (output->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) { |
| 5139 | /* Do not init fast mixer on deep buffer, warn if buffers are confed too small */ |
| 5140 | initFastMixer = false; |
| 5141 | ALOGW_IF(mFrameCount * 1000 / mSampleRate < kMinNormalSinkBufferSizeMs, |
| 5142 | "HAL DEEP BUFFER Buffer (%zu ms) is smaller than set minimal buffer " |
| 5143 | "(%u ms), seems like a configuration error", |
| 5144 | mFrameCount * 1000 / mSampleRate, kMinNormalSinkBufferSizeMs); |
| 5145 | } else { |
| 5146 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 5147 | } |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 5148 | break; |
| 5149 | } |
| 5150 | ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount, |
| 5151 | "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu", |
| 5152 | mFrameCount, mNormalFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5153 | } |
| 5154 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5155 | audio_format_t fastMixerFormat; |
| 5156 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 5157 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 5158 | } else { |
| 5159 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 5160 | } |
| 5161 | if (mFormat != fastMixerFormat) { |
| 5162 | // change our Sink format to accept our intermediate precision |
| 5163 | mFormat = fastMixerFormat; |
| 5164 | free(mSinkBuffer); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5165 | mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5166 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 5167 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 5168 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5169 | |
| 5170 | // create a MonoPipe to connect our submix to FastMixer |
| 5171 | NBAIO_Format format = mOutputSink->format(); |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5172 | |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5173 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 5174 | ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 5175 | format.mFormat = fastMixerFormat; |
| 5176 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 5177 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5178 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 5179 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 5180 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 5181 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 5182 | const NBAIO_Format offersFast[1] = {format}; |
| 5183 | size_t numCounterOffersFast = 0; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5184 | #if !LOG_NDEBUG |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 5185 | index = |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5186 | #else |
| 5187 | (void) |
| 5188 | #endif |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 5189 | monoPipe->negotiate(offersFast, std::size(offersFast), |
| 5190 | nullptr /* counterOffers */, numCounterOffersFast); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5191 | ALOG_ASSERT(index == 0); |
| 5192 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 5193 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 5194 | mPipeSink = monoPipe; |
| 5195 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5196 | // 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] | 5197 | mFastMixer = new FastMixer(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5198 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5199 | #ifdef STATE_QUEUE_DUMP |
| 5200 | sq->setObserverDump(&mStateQueueObserverDump); |
| 5201 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 5202 | #endif |
| 5203 | FastMixerState *state = sq->begin(); |
| 5204 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 5205 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 5206 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 5207 | fastTrack->mVolumeProvider = NULL; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 5208 | fastTrack->mChannelMask = static_cast<audio_channel_mask_t>( |
| 5209 | mChannelMask | mHapticChannelMask); // mPipeSink channel mask for |
| 5210 | // audio to FastMixer |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 5211 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5212 | fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE; |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 5213 | fastTrack->mHapticScale = {/*level=*/os::HapticLevel::NONE }; |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 5214 | fastTrack->mHapticMaxAmplitude = NAN; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5215 | fastTrack->mGeneration++; |
| 5216 | state->mFastTracksGen++; |
| 5217 | state->mTrackMask = 1; |
| 5218 | // fast mixer will use the HAL output sink |
| 5219 | state->mOutputSink = mOutputSink.get(); |
| 5220 | state->mOutputSinkGen++; |
| 5221 | state->mFrameCount = mFrameCount; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5222 | // specify sink channel mask when haptic channel mask present as it can not |
| 5223 | // be calculated directly from channel count |
| 5224 | state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 5225 | ? AUDIO_CHANNEL_NONE |
| 5226 | : static_cast<audio_channel_mask_t>(mChannelMask | mHapticChannelMask); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5227 | state->mCommand = FastMixerState::COLD_IDLE; |
| 5228 | // already done in constructor initialization list |
| 5229 | //mFastMixerFutex = 0; |
| 5230 | state->mColdFutexAddr = &mFastMixerFutex; |
| 5231 | state->mColdGen++; |
| 5232 | state->mDumpState = &mFastMixerDumpState; |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5233 | mFastMixerNBLogWriter = afThreadCallback->newWriter_l(kFastMixerLogSize, "FastMixer"); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 5234 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5235 | sq->end(); |
| 5236 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 5237 | |
Eric Tan | 0513b5d | 2018-09-17 10:32:48 -0700 | [diff] [blame] | 5238 | NBLog::thread_info_t info; |
| 5239 | info.id = mId; |
| 5240 | info.type = NBLog::FASTMIXER; |
| 5241 | mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info); |
| 5242 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5243 | // start the fast mixer |
| 5244 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 5245 | pid_t tid = mFastMixer->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5246 | sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 5247 | stream()->setHalThreadPriority(kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5248 | |
| 5249 | #ifdef AUDIO_WATCHDOG |
| 5250 | // create and start the watchdog |
| 5251 | mAudioWatchdog = new AudioWatchdog(); |
| 5252 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 5253 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 5254 | tid = mAudioWatchdog->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5255 | sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5256 | #endif |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 5257 | } else { |
| 5258 | #ifdef TEE_SINK |
| 5259 | // Only use the MixerThread tee if there is no FastMixer. |
| 5260 | mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD); |
| 5261 | mTee.setId(std::string("_") + std::to_string(mId) + "_M"); |
| 5262 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | switch (kUseFastMixer) { |
| 5266 | case FastMixer_Never: |
| 5267 | case FastMixer_Dynamic: |
| 5268 | mNormalSink = mOutputSink; |
| 5269 | break; |
| 5270 | case FastMixer_Always: |
| 5271 | mNormalSink = mPipeSink; |
| 5272 | break; |
| 5273 | case FastMixer_Static: |
| 5274 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 5275 | break; |
| 5276 | } |
Andy Hung | 7de02c9 | 2024-06-25 17:07:58 -0700 | [diff] [blame] | 5277 | // setMasterBalance needs to be called after the FastMixer |
| 5278 | // (if any) is set up, in order to deliver the balance settings to it. |
| 5279 | setMasterBalance(afThreadCallback->getMasterBalance_l()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5280 | } |
| 5281 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5282 | MixerThread::~MixerThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5283 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5284 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5285 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5286 | FastMixerState *state = sq->begin(); |
| 5287 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 5288 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 5289 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 5290 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | state->mCommand = FastMixerState::EXIT; |
| 5294 | sq->end(); |
| 5295 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 5296 | mFastMixer->join(); |
| 5297 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 5298 | // We'll use that extract the final state which contains one remaining fast track |
| 5299 | // corresponding to our sub-mix. |
| 5300 | state = sq->begin(); |
| 5301 | ALOG_ASSERT(state->mTrackMask == 1); |
| 5302 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 5303 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 5304 | delete fastTrack->mBufferProvider; |
| 5305 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5306 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5307 | #ifdef AUDIO_WATCHDOG |
| 5308 | if (mAudioWatchdog != 0) { |
| 5309 | mAudioWatchdog->requestExit(); |
| 5310 | mAudioWatchdog->requestExitAndWait(); |
| 5311 | mAudioWatchdog.clear(); |
| 5312 | } |
| 5313 | #endif |
| 5314 | } |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5315 | mAfThreadCallback->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5316 | delete mAudioMixer; |
| 5317 | } |
| 5318 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5319 | void MixerThread::onFirstRef() { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5320 | PlaybackThread::onFirstRef(); |
| 5321 | |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5322 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 5323 | if (mOutput != nullptr && mOutput->stream != nullptr) { |
| 5324 | status_t status = mOutput->stream->setLatencyModeCallback(this); |
| 5325 | if (status != INVALID_OPERATION) { |
| 5326 | updateHalSupportedLatencyModes_l(); |
| 5327 | } |
| 5328 | // Default to enabled if the HAL supports it. This can be changed by Audioflinger after |
| 5329 | // the thread construction according to AudioFlinger::mBluetoothLatencyModesEnabled |
| 5330 | mBluetoothLatencyModesEnabled.store( |
| 5331 | mOutput->audioHwDev->supportsBluetoothVariableLatency()); |
| 5332 | } |
| 5333 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5334 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5335 | uint32_t MixerThread::correctLatency_l(uint32_t latency) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5336 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5337 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5338 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 5339 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 5340 | } |
| 5341 | return latency; |
| 5342 | } |
| 5343 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5344 | ssize_t MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5345 | { |
| 5346 | // FIXME we should only do one push per cycle; confirm this is true |
| 5347 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5348 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5349 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5350 | FastMixerState *state = sq->begin(); |
| 5351 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 5352 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 5353 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 5354 | |
| 5355 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 5356 | ATRACE_BEGIN("write"); |
| 5357 | mOutput->write((char *)mSinkBuffer, 0); |
| 5358 | ATRACE_END(); |
| 5359 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5360 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 5361 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 5362 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5363 | } |
| 5364 | #ifdef AUDIO_WATCHDOG |
| 5365 | if (mAudioWatchdog != 0) { |
| 5366 | mAudioWatchdog->resume(); |
| 5367 | } |
| 5368 | #endif |
| 5369 | } |
| 5370 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 5371 | #ifdef FAST_THREAD_STATISTICS |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5372 | mFastMixerDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5373 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 5374 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5375 | sq->end(); |
| 5376 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 5377 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 5378 | mNormalSink = mPipeSink; |
| 5379 | } |
| 5380 | } else { |
| 5381 | sq->end(false /*didModify*/); |
| 5382 | } |
| 5383 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5384 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5385 | } |
| 5386 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5387 | void MixerThread::threadLoop_standby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5388 | { |
| 5389 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5390 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5391 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 5392 | FastMixerState *state = sq->begin(); |
| 5393 | if (!(state->mCommand & FastMixerState::IDLE)) { |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 5394 | // Report any frames trapped in the Monopipe |
| 5395 | MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get(); |
| 5396 | const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite(); |
| 5397 | mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld " |
| 5398 | "monoPipeWritten:%lld monoPipeLeft:%lld", |
| 5399 | (long long)mFramesWritten, (long long)mSuspendedFrames, |
| 5400 | (long long)mPipeSink->framesWritten(), pipeFrames); |
| 5401 | mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str()); |
| 5402 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5403 | state->mCommand = FastMixerState::COLD_IDLE; |
| 5404 | state->mColdFutexAddr = &mFastMixerFutex; |
| 5405 | state->mColdGen++; |
| 5406 | mFastMixerFutex = 0; |
| 5407 | sq->end(); |
| 5408 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 5409 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 5410 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 5411 | mNormalSink = mOutputSink; |
| 5412 | } |
| 5413 | #ifdef AUDIO_WATCHDOG |
| 5414 | if (mAudioWatchdog != 0) { |
| 5415 | mAudioWatchdog->pause(); |
| 5416 | } |
| 5417 | #endif |
| 5418 | } else { |
| 5419 | sq->end(false /*didModify*/); |
| 5420 | } |
| 5421 | } |
| 5422 | PlaybackThread::threadLoop_standby(); |
| 5423 | } |
| 5424 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5425 | bool PlaybackThread::waitingAsyncCallback_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5426 | { |
| 5427 | return false; |
| 5428 | } |
| 5429 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5430 | bool PlaybackThread::shouldStandby_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5431 | { |
| 5432 | return !mStandby; |
| 5433 | } |
| 5434 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5435 | bool PlaybackThread::waitingAsyncCallback() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5436 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 5437 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5438 | return waitingAsyncCallback_l(); |
| 5439 | } |
| 5440 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5441 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5442 | void PlaybackThread::threadLoop_standby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5443 | { |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 5444 | ALOGV("%s: audio hardware entering standby, mixer %p, suspend count %d", |
| 5445 | __func__, this, (int32_t)mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5446 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5447 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5448 | // discard any pending drain or write ack by incrementing sequence |
| 5449 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5450 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5451 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5452 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5453 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5454 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5455 | mHwPaused = false; |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 5456 | setHalLatencyMode_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5457 | } |
| 5458 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5459 | void PlaybackThread::onAddNewTrack_l() |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 5460 | { |
| 5461 | ALOGV("signal playback thread"); |
| 5462 | broadcast_l(); |
| 5463 | } |
| 5464 | |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 5465 | void PlaybackThread::onAsyncError(bool isHardError) |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5466 | { |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 5467 | auto allTrackPortIds = getTrackPortIds(); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5468 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 5469 | invalidateTracks((audio_stream_type_t)i); |
| 5470 | } |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 5471 | if (isHardError) { |
| 5472 | mAfThreadCallback->onHardError(allTrackPortIds); |
| 5473 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5474 | } |
| 5475 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5476 | void MixerThread::threadLoop_mix() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5477 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5478 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5479 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5480 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5481 | // increase sleep time progressively when application underrun condition clears. |
| 5482 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 5483 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 5484 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5485 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5486 | sleepTimeShift--; |
| 5487 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5488 | mSleepTimeUs = 0; |
| 5489 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5490 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 5491 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5492 | } |
| 5493 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5494 | void MixerThread::threadLoop_sleepTime() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5495 | { |
| 5496 | // If no tracks are ready, sleep once for the duration of an output |
| 5497 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5498 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5499 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Andy Hung | 06d1322 | 2018-05-03 20:13:31 -0700 | [diff] [blame] | 5500 | if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) { |
| 5501 | // Using the Monopipe availableToWrite, we estimate the |
| 5502 | // sleep time to retry for more data (before we underrun). |
| 5503 | MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get()); |
| 5504 | const ssize_t availableToWrite = mPipeSink->availableToWrite(); |
| 5505 | const size_t pipeFrames = monoPipe->maxFrames(); |
| 5506 | const size_t framesLeft = pipeFrames - max(availableToWrite, 0); |
| 5507 | // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount |
| 5508 | const size_t framesDelay = std::min( |
| 5509 | mNormalFrameCount, max(framesLeft / 2, mFrameCount)); |
| 5510 | ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu", |
| 5511 | pipeFrames, framesLeft, framesDelay); |
| 5512 | mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate; |
| 5513 | } else { |
| 5514 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 5515 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 5516 | mSleepTimeUs = kMinThreadSleepTimeUs; |
| 5517 | } |
| 5518 | // reduce sleep time in case of consecutive application underruns to avoid |
| 5519 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 5520 | // duration we would end up writing less data than needed by the audio HAL if |
| 5521 | // the condition persists. |
| 5522 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 5523 | sleepTimeShift++; |
| 5524 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5525 | } |
| 5526 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5527 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5528 | } |
| 5529 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5530 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 5531 | // before effects processing or output. |
| 5532 | if (mMixerBufferValid) { |
| 5533 | memset(mMixerBuffer, 0, mMixerBufferSize); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5534 | if (mType == SPATIALIZER) { |
| 5535 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5536 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5537 | } else { |
| 5538 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5539 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5540 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5541 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 5542 | "anticipated start"); |
| 5543 | } |
| 5544 | // TODO add standby time extension fct of effect tail |
| 5545 | } |
| 5546 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 5547 | // prepareTracks_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 5548 | PlaybackThread::mixer_state MixerThread::prepareTracks_l( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5549 | Vector<sp<IAfTrack>>* tracksToRemove) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5550 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5551 | // clean up deleted track ids in AudioMixer before allocating new tracks |
| 5552 | (void)mTracks.processDeletedTrackIds([this](int trackId) { |
| 5553 | // for each trackId, destroy it in the AudioMixer |
| 5554 | if (mAudioMixer->exists(trackId)) { |
| 5555 | mAudioMixer->destroy(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5556 | } |
| 5557 | }); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5558 | mTracks.clearDeletedTrackIds(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5559 | |
| 5560 | mixer_state mixerStatus = MIXER_IDLE; |
| 5561 | // find out which tracks need to be processed |
| 5562 | size_t count = mActiveTracks.size(); |
| 5563 | size_t mixedTracks = 0; |
| 5564 | size_t tracksWithEffect = 0; |
| 5565 | // counts only _active_ fast tracks |
| 5566 | size_t fastTracks = 0; |
| 5567 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 5568 | |
| 5569 | float masterVolume = mMasterVolume; |
| 5570 | bool masterMute = mMasterMute; |
| 5571 | |
| 5572 | if (masterMute) { |
| 5573 | masterVolume = 0; |
| 5574 | } |
| 5575 | // Delegate master volume control to effect in output mix effect chain if needed |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 5576 | sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5577 | if (chain != 0) { |
| 5578 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 5579 | chain->setVolume(&v, &v); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5580 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 5581 | chain.clear(); |
| 5582 | } |
| 5583 | |
| 5584 | // prepare a new state to push |
| 5585 | FastMixerStateQueue *sq = NULL; |
| 5586 | FastMixerState *state = NULL; |
| 5587 | bool didModify = false; |
| 5588 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5589 | bool coldIdle = false; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 5590 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5591 | sq = mFastMixer->sq(); |
| 5592 | state = sq->begin(); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 5593 | coldIdle = state->mCommand == FastMixerState::COLD_IDLE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5594 | } |
| 5595 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5596 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5597 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5598 | |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5599 | // DeferredOperations handles statistics after setting mixerStatus. |
| 5600 | class DeferredOperations { |
| 5601 | public: |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5602 | DeferredOperations(mixer_state *mixerStatus, ThreadMetrics *threadMetrics) |
| 5603 | : mMixerStatus(mixerStatus) |
| 5604 | , mThreadMetrics(threadMetrics) {} |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5605 | |
| 5606 | // when leaving scope, tally frames properly. |
| 5607 | ~DeferredOperations() { |
| 5608 | // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY) |
| 5609 | // because that is when the underrun occurs. |
| 5610 | // We do not distinguish between FastTracks and NormalTracks here. |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5611 | size_t maxUnderrunFrames = 0; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5612 | if (*mMixerStatus == MIXER_TRACKS_READY && mUnderrunFrames.size() > 0) { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5613 | for (const auto &underrun : mUnderrunFrames) { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 5614 | underrun.first->tallyUnderrunFrames(underrun.second); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5615 | maxUnderrunFrames = max(underrun.second, maxUnderrunFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5616 | } |
| 5617 | } |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5618 | // send the max underrun frames for this mixer period |
| 5619 | mThreadMetrics->logUnderrunFrames(maxUnderrunFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | // tallyUnderrunFrames() is called to update the track counters |
| 5623 | // with the number of underrun frames for a particular mixer period. |
| 5624 | // We defer tallying until we know the final mixer status. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5625 | void tallyUnderrunFrames(const sp<IAfTrack>& track, size_t underrunFrames) { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5626 | mUnderrunFrames.emplace_back(track, underrunFrames); |
| 5627 | } |
| 5628 | |
| 5629 | private: |
| 5630 | const mixer_state * const mMixerStatus; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5631 | ThreadMetrics * const mThreadMetrics; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5632 | std::vector<std::pair<sp<IAfTrack>, size_t>> mUnderrunFrames; |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 5633 | } deferredOperations(&mixerStatus, &mThreadMetrics); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 5634 | // implicit nested scope for variable capture |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5635 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5636 | bool noFastHapticTrack = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5637 | for (size_t i=0 ; i<count ; i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5638 | const sp<IAfTrack> t = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5639 | |
| 5640 | // this const just means the local variable doesn't change |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5641 | IAfTrack* const track = t.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5642 | |
| 5643 | // process fast tracks |
| 5644 | if (track->isFastTrack()) { |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 5645 | LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr, |
| 5646 | "%s(%d): FastTrack(%d) present without FastMixer", |
| 5647 | __func__, id(), track->id()); |
| 5648 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5649 | if (track->getHapticPlaybackEnabled()) { |
| 5650 | noFastHapticTrack = false; |
| 5651 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5652 | |
| 5653 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 5654 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 5655 | // the track never becomes active so it's fast mixer slot is never touched. |
| 5656 | // The converse, of removing an (active) track and then creating a new track |
| 5657 | // at the identical fast mixer slot within the same normal mix cycle, |
| 5658 | // is impossible because the slot isn't marked available until the end of each cycle. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5659 | int j = track->fastIndex(); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 5660 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5661 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 5662 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 5663 | |
| 5664 | // Determine whether the track is currently in underrun condition, |
| 5665 | // and whether it had a recent underrun. |
| 5666 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 5667 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 5668 | uint32_t recentFull = (underruns.mBitFields.mFull - |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5669 | track->fastTrackUnderruns().mBitFields.mFull) & UNDERRUN_MASK; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5670 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5671 | track->fastTrackUnderruns().mBitFields.mPartial) & UNDERRUN_MASK; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5672 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5673 | track->fastTrackUnderruns().mBitFields.mEmpty) & UNDERRUN_MASK; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5674 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5675 | track->fastTrackUnderruns() = underruns; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5676 | // don't count underruns that occur while stopping or pausing |
| 5677 | // or stopped which can occur when flush() is called while active |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5678 | size_t underrunFrames = 0; |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 5679 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 5680 | recentUnderruns > 0) { |
| 5681 | // 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] | 5682 | underrunFrames = recentUnderruns * mFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5683 | } |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 5684 | // Immediately account for FastTrack underruns. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5685 | track->audioTrackServerProxy()->tallyUnderrunFrames(underrunFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5686 | |
| 5687 | // This is similar to the state machine for normal tracks, |
| 5688 | // with a few modifications for fast tracks. |
| 5689 | bool isActive = true; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5690 | switch (track->state()) { |
| 5691 | case IAfTrackBase::STOPPING_1: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5692 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5693 | if (recentUnderruns > 0 || track->isTerminated()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5694 | track->setState(IAfTrackBase::STOPPING_2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5695 | } |
| 5696 | break; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5697 | case IAfTrackBase::PAUSING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5698 | // ramp down is not yet implemented |
| 5699 | track->setPaused(); |
| 5700 | break; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5701 | case IAfTrackBase::RESUMING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5702 | // ramp up is not yet implemented |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5703 | track->setState(IAfTrackBase::ACTIVE); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5704 | break; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5705 | case IAfTrackBase::ACTIVE: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5706 | if (recentFull > 0 || recentPartial > 0) { |
| 5707 | // track has provided at least some frames recently: reset retry count |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5708 | track->retryCount() = kMaxTrackRetries; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5709 | } |
| 5710 | if (recentUnderruns == 0) { |
| 5711 | // no recent underruns: stay active |
| 5712 | break; |
| 5713 | } |
| 5714 | // there has recently been an underrun of some kind |
| 5715 | if (track->sharedBuffer() == 0) { |
| 5716 | // were any of the recent underruns "empty" (no frames available)? |
| 5717 | if (recentEmpty == 0) { |
| 5718 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 5719 | break; |
| 5720 | } |
| 5721 | // there has recently been an "empty" underrun: decrement the retry counter |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5722 | if (--(track->retryCount()) > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5723 | break; |
| 5724 | } |
| 5725 | // indicate to client process that the track was disabled because of underrun; |
| 5726 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5727 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5728 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 5729 | isActive = false; |
| 5730 | break; |
| 5731 | } |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 5732 | FALLTHROUGH_INTENDED; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5733 | case IAfTrackBase::STOPPING_2: |
| 5734 | case IAfTrackBase::PAUSED: |
| 5735 | case IAfTrackBase::STOPPED: |
| 5736 | case IAfTrackBase::FLUSHED: // flush() while active |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5737 | // Check for presentation complete if track is inactive |
| 5738 | // We have consumed all the buffers of this track. |
| 5739 | // This would be incomplete if we auto-paused on underrun |
| 5740 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 5741 | uint32_t latency = 0; |
| 5742 | status_t result = mOutput->stream->getLatency(&latency); |
| 5743 | ALOGE_IF(result != OK, |
| 5744 | "Error when retrieving output stream latency: %d", result); |
| 5745 | size_t audioHALFrames = (latency * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5746 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5747 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 5748 | // track stays in active list until presentation is complete |
| 5749 | break; |
| 5750 | } |
| 5751 | } |
| 5752 | if (track->isStopping_2()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5753 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5754 | } |
| 5755 | if (track->isStopped()) { |
| 5756 | // Can't reset directly, as fast mixer is still polling this track |
| 5757 | // track->reset(); |
| 5758 | // So instead mark this track as needing to be reset after push with ack |
| 5759 | resetMask |= 1 << i; |
| 5760 | } |
| 5761 | isActive = false; |
| 5762 | break; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5763 | case IAfTrackBase::IDLE: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5764 | default: |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5765 | LOG_ALWAYS_FATAL("unexpected track state %d", (int)track->state()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | if (isActive) { |
| 5769 | // was it previously inactive? |
| 5770 | if (!(state->mTrackMask & (1 << j))) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5771 | ExtendedAudioBufferProvider *eabp = track->asExtendedAudioBufferProvider(); |
| 5772 | VolumeProvider *vp = track->asVolumeProvider(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5773 | fastTrack->mBufferProvider = eabp; |
| 5774 | fastTrack->mVolumeProvider = vp; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5775 | fastTrack->mChannelMask = track->channelMask(); |
| 5776 | fastTrack->mFormat = track->format(); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5777 | fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 5778 | fastTrack->mHapticScale = track->getHapticScale(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 5779 | fastTrack->mHapticMaxAmplitude = track->getHapticMaxAmplitude(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5780 | fastTrack->mGeneration++; |
| 5781 | state->mTrackMask |= 1 << j; |
| 5782 | didModify = true; |
| 5783 | // no acknowledgement required for newly active tracks |
| 5784 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5785 | sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5786 | float volume; |
| 5787 | if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) { |
| 5788 | volume = 0.f; |
| 5789 | } else { |
| 5790 | volume = masterVolume * mStreamTypes[track->streamType()].volume; |
| 5791 | } |
| 5792 | |
| 5793 | handleVoipVolume_l(&volume); |
| 5794 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5795 | // cache the combined master volume and stream type volume for fast mixer; this |
| 5796 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 5797 | const float vh = track->getVolumeHandler()->getVolume( |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5798 | proxy->framesReleased()).first; |
| 5799 | volume *= vh; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5800 | track->setCachedVolume(volume); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 5801 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5802 | float vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5803 | float vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 5804 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5805 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5806 | /*muteState=*/{masterVolume == 0.f, |
| 5807 | mStreamTypes[track->streamType()].volume == 0.f, |
| 5808 | mStreamTypes[track->streamType()].mute, |
| 5809 | track->isPlaybackRestricted(), |
Vlad Popa | 436c917 | 2022-08-02 15:25:08 +0200 | [diff] [blame] | 5810 | vlf == 0.f && vrf == 0.f, |
| 5811 | vh == 0.f}); |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5812 | |
| 5813 | vlf *= volume; |
| 5814 | vrf *= volume; |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5815 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 5816 | if (track->getInternalMute()) { |
| 5817 | vlf = 0.f; |
| 5818 | vrf = 0.f; |
| 5819 | } |
| 5820 | |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 5821 | track->setFinalVolume(vlf, vrf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5822 | ++fastTracks; |
| 5823 | } else { |
| 5824 | // was it previously active? |
| 5825 | if (state->mTrackMask & (1 << j)) { |
| 5826 | fastTrack->mBufferProvider = NULL; |
| 5827 | fastTrack->mGeneration++; |
| 5828 | state->mTrackMask &= ~(1 << j); |
| 5829 | didModify = true; |
| 5830 | // If any fast tracks were removed, we must wait for acknowledgement |
| 5831 | // because we're about to decrement the last sp<> on those tracks. |
| 5832 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 5833 | } else { |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5834 | // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an |
| 5835 | // AudioTrack may start (which may not be with a start() but with a write() |
| 5836 | // after underrun) and immediately paused or released. In that case the |
| 5837 | // FastTrack state hasn't had time to update. |
| 5838 | // TODO Remove the ALOGW when this theory is confirmed. |
| 5839 | ALOGW("fast track %d should have been active; " |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5840 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5841 | j, (int)track->state(), state->mTrackMask, recentUnderruns, |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 5842 | track->sharedBuffer() != 0); |
Glenn Kasten | bf848af | 2017-12-22 11:55:01 -0800 | [diff] [blame] | 5843 | // Since the FastMixer state already has the track inactive, do nothing here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5844 | } |
| 5845 | tracksToRemove->add(track); |
| 5846 | // Avoids a misleading display in dumpsys |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5847 | track->fastTrackUnderruns().mBitFields.mMostRecent = UNDERRUN_FULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5848 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 5849 | if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) { |
| 5850 | fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled(); |
| 5851 | didModify = true; |
| 5852 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5853 | continue; |
| 5854 | } |
| 5855 | |
| 5856 | { // local variable scope to avoid goto warning |
| 5857 | |
| 5858 | audio_track_cblk_t* cblk = track->cblk(); |
| 5859 | |
| 5860 | // The first time a track is added we wait |
| 5861 | // for all its buffers to be filled before processing it |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5862 | const int trackId = track->id(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5863 | |
| 5864 | // if an active track doesn't exist in the AudioMixer, create it. |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5865 | // use the trackId as the AudioMixer name. |
| 5866 | if (!mAudioMixer->exists(trackId)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5867 | status_t status = mAudioMixer->create( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5868 | trackId, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5869 | track->channelMask(), |
| 5870 | track->format(), |
| 5871 | track->sessionId()); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5872 | if (status != OK) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5873 | ALOGW("%s(): AudioMixer cannot create track(%d)" |
| 5874 | " mask %#x, format %#x, sessionId %d", |
| 5875 | __func__, trackId, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5876 | track->channelMask(), track->format(), track->sessionId()); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5877 | tracksToRemove->add(track); |
| 5878 | track->invalidate(); // consider it dead. |
| 5879 | continue; |
| 5880 | } |
| 5881 | } |
| 5882 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5883 | // make sure that we have enough frames to mix one full buffer. |
| 5884 | // enforce this condition only once to enable draining the buffer in case the client |
| 5885 | // app does not call stop() and relies on underrun to stop: |
| 5886 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 5887 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5888 | size_t desiredFrames; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5889 | const uint32_t sampleRate = track->audioTrackServerProxy()->getSampleRate(); |
| 5890 | const AudioPlaybackRate playbackRate = track->audioTrackServerProxy()->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5891 | |
| 5892 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 5893 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5894 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 5895 | // add frames already consumed but not yet released by the resampler |
| 5896 | // because mAudioTrackServerProxy->framesReady() will include these frames |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5897 | desiredFrames += mAudioMixer->getUnreleasedFrames(trackId); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 5898 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5899 | uint32_t minFrames = 1; |
| 5900 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 5901 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5902 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5903 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 5904 | |
| 5905 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5906 | if (ATRACE_ENABLED()) { |
| 5907 | // I wish we had formatted trace names |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5908 | std::string traceName("nRdy"); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5909 | traceName += std::to_string(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 5910 | ATRACE_INT(traceName.c_str(), framesReady); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5911 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 5912 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5913 | !track->isPaused() && !track->isTerminated()) |
| 5914 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5915 | 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] | 5916 | |
| 5917 | mixedTracks++; |
| 5918 | |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 5919 | // track->mainBuffer() != mSinkBuffer and mMixerBuffer means |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5920 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5921 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 5922 | if (track->mainBuffer() != mSinkBuffer && |
| 5923 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 5924 | if (mEffectBufferEnabled) { |
| 5925 | mEffectBufferValid = true; // Later can set directly. |
| 5926 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5927 | chain = getEffectChain_l(track->sessionId()); |
| 5928 | // Delegate volume control to effect in track effect chain if needed |
| 5929 | if (chain != 0) { |
| 5930 | tracksWithEffect++; |
| 5931 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5932 | 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] | 5933 | "session %d", |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5934 | trackId, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5935 | } |
| 5936 | } |
| 5937 | |
| 5938 | |
| 5939 | int param = AudioMixer::VOLUME; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5940 | if (track->fillingStatus() == IAfTrack::FS_FILLED) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5941 | // no ramp for the first volume setting |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5942 | track->fillingStatus() = IAfTrack::FS_ACTIVE; |
| 5943 | if (track->state() == IAfTrackBase::RESUMING) { |
| 5944 | track->setState(IAfTrackBase::ACTIVE); |
Revathi Uddaraju | 453bcb5 | 2017-08-14 14:19:40 +0800 | [diff] [blame] | 5945 | // If a new track is paused immediately after start, do not ramp on resume. |
| 5946 | if (cblk->mServer != 0) { |
| 5947 | param = AudioMixer::RAMP_VOLUME; |
| 5948 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5949 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 5950 | mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5951 | mLeftVolFloat = -1.0; |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5952 | // FIXME should not make a decision based on mServer |
| 5953 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5954 | // If the track is stopped before the first frame was mixed, |
| 5955 | // do not apply ramp |
| 5956 | param = AudioMixer::RAMP_VOLUME; |
| 5957 | } |
| 5958 | |
| 5959 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5960 | uint32_t vl, vr; // in U8.24 integer format |
| 5961 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5962 | // read original volumes with volume control |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5963 | float v = masterVolume * mStreamTypes[track->streamType()].volume; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5964 | // Always fetch volumeshaper volume to ensure state is updated. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5965 | const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 5966 | const float vh = track->getVolumeHandler()->getVolume( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 5967 | track->audioTrackServerProxy()->framesReleased()).first; |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 5968 | |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5969 | if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { |
| 5970 | v = 0; |
| 5971 | } |
| 5972 | |
| 5973 | handleVoipVolume_l(&v); |
| 5974 | |
| 5975 | if (track->isPausing()) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5976 | vl = vr = 0; |
| 5977 | vlf = vrf = vaf = 0.; |
Eric Laurent | eab9045 | 2019-06-24 15:17:46 -0700 | [diff] [blame] | 5978 | track->setPaused(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5979 | } else { |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5980 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 5981 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 5982 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5983 | // 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] | 5984 | if (vlf > GAIN_FLOAT_UNITY) { |
| 5985 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 5986 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5987 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 5988 | if (vrf > GAIN_FLOAT_UNITY) { |
| 5989 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 5990 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5991 | } |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5992 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 5993 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 5994 | /*muteState=*/{masterVolume == 0.f, |
| 5995 | mStreamTypes[track->streamType()].volume == 0.f, |
| 5996 | mStreamTypes[track->streamType()].mute, |
| 5997 | track->isPlaybackRestricted(), |
Vlad Popa | 436c917 | 2022-08-02 15:25:08 +0200 | [diff] [blame] | 5998 | vlf == 0.f && vrf == 0.f, |
| 5999 | vh == 0.f}); |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 6000 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 6001 | // now apply the master volume and stream type volume and shaper volume |
| 6002 | vlf *= v * vh; |
| 6003 | vrf *= v * vh; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6004 | // 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] | 6005 | // then derive vl and vr as U8.24 versions for the effect chain |
| 6006 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 6007 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 6008 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 6009 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6010 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6011 | // send level comes from shared memory and so may be corrupt |
| 6012 | if (sendLevel > MAX_GAIN_INT) { |
| 6013 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 6014 | sendLevel = MAX_GAIN_INT; |
| 6015 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 6016 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 6017 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6018 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6019 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 6020 | if (track->getInternalMute()) { |
| 6021 | vrf = 0.f; |
| 6022 | vlf = 0.f; |
| 6023 | } |
| 6024 | |
Jiabin Huang | 66aa1e3 | 2024-05-13 20:33:29 +0000 | [diff] [blame] | 6025 | track->setFinalVolume(vlf, vrf); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 6026 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6027 | // Delegate volume control to effect in track effect chain if needed |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 6028 | if (chain != 0 && chain->setVolume(&vl, &vr)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6029 | // Do not ramp volume if volume is controlled by effect |
| 6030 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 6031 | // Update remaining floating point volume levels |
| 6032 | vlf = (float)vl / (1 << 24); |
| 6033 | vrf = (float)vr / (1 << 24); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6034 | track->setHasVolumeController(true); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6035 | } else { |
| 6036 | // force no volume ramp when volume controller was just disabled or removed |
| 6037 | // from effect chain to avoid volume spike |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6038 | if (track->hasVolumeController()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6039 | param = AudioMixer::VOLUME; |
| 6040 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6041 | track->setHasVolumeController(false); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6042 | } |
| 6043 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6044 | // XXX: these things DON'T need to be done each time |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6045 | mAudioMixer->setBufferProvider(trackId, track->asExtendedAudioBufferProvider()); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6046 | mAudioMixer->enable(trackId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6047 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6048 | mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf); |
| 6049 | mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf); |
| 6050 | mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6051 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6052 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6053 | AudioMixer::TRACK, |
| 6054 | AudioMixer::FORMAT, (void *)track->format()); |
| 6055 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6056 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6057 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 6058 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6059 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 6060 | if (mType == SPATIALIZER && !track->isSpatialized()) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6061 | mAudioMixer->setParameter( |
| 6062 | trackId, |
| 6063 | AudioMixer::TRACK, |
| 6064 | AudioMixer::MIXER_CHANNEL_MASK, |
| 6065 | (void *)(uintptr_t)(mChannelMask | mHapticChannelMask)); |
| 6066 | } else { |
| 6067 | mAudioMixer->setParameter( |
| 6068 | trackId, |
| 6069 | AudioMixer::TRACK, |
| 6070 | AudioMixer::MIXER_CHANNEL_MASK, |
| 6071 | (void *)(uintptr_t)(mMixerChannelMask | mHapticChannelMask)); |
| 6072 | } |
| 6073 | |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6074 | // 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] | 6075 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6076 | uint32_t reqSampleRate = proxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 6077 | if (reqSampleRate == 0) { |
| 6078 | reqSampleRate = mSampleRate; |
| 6079 | } else if (reqSampleRate > maxSampleRate) { |
| 6080 | reqSampleRate = maxSampleRate; |
| 6081 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6082 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6083 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6084 | AudioMixer::RESAMPLE, |
| 6085 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 6086 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6087 | |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6088 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6089 | trackId, |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6090 | AudioMixer::TIMESTRETCH, |
| 6091 | AudioMixer::PLAYBACK_RATE, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6092 | // cast away constness for this generic API. |
| 6093 | const_cast<void *>(reinterpret_cast<const void *>(&playbackRate))); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 6094 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6095 | /* |
| 6096 | * Select the appropriate output buffer for the track. |
| 6097 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 6098 | * Tracks with effects go into their own effects chain buffer |
| 6099 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6100 | * |
| 6101 | * Other tracks can use mMixerBuffer for higher precision |
| 6102 | * channel accumulation. If this buffer is enabled |
| 6103 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 6104 | * into it. |
| 6105 | * |
| 6106 | */ |
| 6107 | if (mMixerBufferEnabled |
| 6108 | && (track->mainBuffer() == mSinkBuffer |
| 6109 | || track->mainBuffer() == mMixerBuffer)) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 6110 | if (mType == SPATIALIZER && !track->isSpatialized()) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6111 | mAudioMixer->setParameter( |
| 6112 | trackId, |
| 6113 | AudioMixer::TRACK, |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6114 | AudioMixer::MIXER_FORMAT, (void *)mEffectBufferFormat); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6115 | mAudioMixer->setParameter( |
| 6116 | trackId, |
| 6117 | AudioMixer::TRACK, |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6118 | AudioMixer::MAIN_BUFFER, (void *)mPostSpatializerBuffer); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6119 | } else { |
| 6120 | mAudioMixer->setParameter( |
| 6121 | trackId, |
| 6122 | AudioMixer::TRACK, |
| 6123 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
| 6124 | mAudioMixer->setParameter( |
| 6125 | trackId, |
| 6126 | AudioMixer::TRACK, |
| 6127 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 6128 | // TODO: override track->mainBuffer()? |
| 6129 | mMixerBufferValid = true; |
| 6130 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6131 | } else { |
| 6132 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6133 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6134 | AudioMixer::TRACK, |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 6135 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6136 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6137 | trackId, |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6138 | AudioMixer::TRACK, |
| 6139 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 6140 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6141 | mAudioMixer->setParameter( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6142 | trackId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6143 | AudioMixer::TRACK, |
| 6144 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 6145 | mAudioMixer->setParameter( |
| 6146 | trackId, |
| 6147 | AudioMixer::TRACK, |
| 6148 | AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled()); |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 6149 | const os::HapticScale hapticScale = track->getHapticScale(); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 6150 | mAudioMixer->setParameter( |
Ahmad Khalil | 229466a | 2024-02-05 12:15:30 +0000 | [diff] [blame] | 6151 | trackId, |
| 6152 | AudioMixer::TRACK, |
| 6153 | AudioMixer::HAPTIC_SCALE, (void *)&hapticScale); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6154 | const float hapticMaxAmplitude = track->getHapticMaxAmplitude(); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 6155 | mAudioMixer->setParameter( |
| 6156 | trackId, |
| 6157 | AudioMixer::TRACK, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6158 | AudioMixer::HAPTIC_MAX_AMPLITUDE, (void *)&hapticMaxAmplitude); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6159 | |
| 6160 | // reset retry count |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6161 | track->retryCount() = kMaxTrackRetries; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6162 | |
| 6163 | // If one track is ready, set the mixer ready if: |
| 6164 | // - the mixer was not ready during previous round OR |
| 6165 | // - no other track is not ready |
| 6166 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 6167 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 6168 | mixerStatus = MIXER_TRACKS_READY; |
| 6169 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 6170 | |
| 6171 | // Enable the next few lines to instrument a test for underrun log handling. |
| 6172 | // TODO: Remove when we have a better way of testing the underrun log. |
| 6173 | #if 0 |
| 6174 | static int i; |
| 6175 | if ((++i & 0xf) == 0) { |
| 6176 | deferredOperations.tallyUnderrunFrames(track, 10 /* underrunFrames */); |
| 6177 | } |
| 6178 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6179 | } else { |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6180 | size_t underrunFrames = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 6181 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 6182 | ALOGV("track(%d) underrun, track state %s framesReady(%zu) < framesDesired(%zd)", |
| 6183 | trackId, track->getTrackStateAsString(), framesReady, desiredFrames); |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6184 | underrunFrames = desiredFrames; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 6185 | } |
Andy Hung | bd3b2b0 | 2018-05-21 10:53:11 -0700 | [diff] [blame] | 6186 | deferredOperations.tallyUnderrunFrames(track, underrunFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 6187 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6188 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 6189 | // previous audio buffer again to effects |
| 6190 | chain = getEffectChain_l(track->sessionId()); |
| 6191 | if (chain != 0) { |
| 6192 | chain->clearInputBuffer(); |
| 6193 | } |
| 6194 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6195 | 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] | 6196 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 6197 | track->isStopped() || track->isPaused()) { |
| 6198 | // We have consumed all the buffers of this track. |
| 6199 | // Remove it from the list of active tracks. |
| 6200 | // TODO: use actual buffer filling status instead of latency when available from |
| 6201 | // audio HAL |
| 6202 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 6203 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6204 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 6205 | if (track->isStopped()) { |
| 6206 | track->reset(); |
| 6207 | } |
| 6208 | tracksToRemove->add(track); |
| 6209 | } |
| 6210 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6211 | // No buffers for this track. Give it a few chances to |
| 6212 | // fill a buffer, then remove it from active list. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6213 | if (--(track->retryCount()) <= 0) { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 6214 | ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to underrun" |
| 6215 | " on thread %d", __func__, trackId, mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6216 | tracksToRemove->add(track); |
| 6217 | // indicate to client process that the track was disabled because of underrun; |
| 6218 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 6219 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6220 | // If one track is not ready, mark the mixer also not ready if: |
| 6221 | // - the mixer was ready during previous round OR |
| 6222 | // - no other track is ready |
| 6223 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 6224 | mixerStatus != MIXER_TRACKS_READY) { |
| 6225 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 6226 | } |
| 6227 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6228 | mAudioMixer->disable(trackId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6232 | |
| 6233 | } |
| 6234 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 6235 | if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) { |
| 6236 | // When there is no fast track playing haptic and FastMixer exists, |
| 6237 | // enabling the first FastTrack, which provides mixed data from normal |
| 6238 | // tracks, to play haptic data. |
| 6239 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 6240 | if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) { |
| 6241 | fastTrack->mHapticPlaybackEnabled = noFastHapticTrack; |
| 6242 | didModify = true; |
| 6243 | } |
| 6244 | } |
| 6245 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6246 | // Push the new FastMixer state if necessary |
Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 6247 | [[maybe_unused]] bool pauseAudioWatchdog = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6248 | if (didModify) { |
| 6249 | state->mFastTracksGen++; |
| 6250 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 6251 | if (kUseFastMixer == FastMixer_Dynamic && |
| 6252 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 6253 | state->mCommand = FastMixerState::COLD_IDLE; |
| 6254 | state->mColdFutexAddr = &mFastMixerFutex; |
| 6255 | state->mColdGen++; |
| 6256 | mFastMixerFutex = 0; |
| 6257 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 6258 | mNormalSink = mOutputSink; |
| 6259 | } |
| 6260 | // If we go into cold idle, need to wait for acknowledgement |
| 6261 | // so that fast mixer stops doing I/O. |
| 6262 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 6263 | pauseAudioWatchdog = true; |
| 6264 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6265 | } |
| 6266 | if (sq != NULL) { |
| 6267 | sq->end(didModify); |
Andy Hung | f228531 | 2017-02-14 18:31:59 -0800 | [diff] [blame] | 6268 | // No need to block if the FastMixer is in COLD_IDLE as the FastThread |
| 6269 | // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE |
| 6270 | // when bringing the output sink into standby.) |
| 6271 | // |
| 6272 | // We will get the latest FastMixer state when we come out of COLD_IDLE. |
| 6273 | // |
| 6274 | // This occurs with BT suspend when we idle the FastMixer with |
| 6275 | // active tracks, which may be added or removed. |
| 6276 | sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6277 | } |
| 6278 | #ifdef AUDIO_WATCHDOG |
| 6279 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 6280 | mAudioWatchdog->pause(); |
| 6281 | } |
| 6282 | #endif |
| 6283 | |
| 6284 | // Now perform the deferred reset on fast tracks that have stopped |
| 6285 | while (resetMask != 0) { |
| 6286 | size_t i = __builtin_ctz(resetMask); |
| 6287 | ALOG_ASSERT(i < count); |
| 6288 | resetMask &= ~(1 << i); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6289 | sp<IAfTrack> track = mActiveTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6290 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 6291 | track->reset(); |
| 6292 | } |
| 6293 | |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 6294 | // Track destruction may occur outside of threadLoop once it is removed from active tracks. |
| 6295 | // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if |
| 6296 | // it ceases to be active, to allow safe removal from the AudioMixer at the start |
| 6297 | // of prepareTracks_l(); this releases any outstanding buffer back to the track. |
| 6298 | // See also the implementation of destroyTrack_l(). |
| 6299 | for (const auto &track : *tracksToRemove) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6300 | const int trackId = track->id(); |
| 6301 | if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer. |
| 6302 | mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */); |
Andy Hung | 80d03d2 | 2018-04-10 10:32:11 -0700 | [diff] [blame] | 6303 | } |
| 6304 | } |
| 6305 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6306 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6307 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6308 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 6309 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0 || |
| 6310 | getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE) != 0) { |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 6311 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 6315 | // as long as there are effects we should clear the effects buffer, to avoid |
| 6316 | // passing a non-clean buffer to the effect chain |
| 6317 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 6318 | if (mType == SPATIALIZER) { |
| 6319 | memset(mPostSpatializerBuffer, 0, mPostSpatializerBufferSize); |
| 6320 | } |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 6321 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6322 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 6323 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 6324 | // and track effects will accumulate into it |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6325 | // always clear sink buffer for spatializer output as the output of the spatializer |
| 6326 | // effect will be accumulated into it |
| 6327 | if ((mBytesRemaining == 0) && (((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 6328 | (mixedTracks == 0 && fastTracks > 0)) || (mType == SPATIALIZER))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6329 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 6330 | if (mMixerBufferValid) { |
| 6331 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 6332 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 6333 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 6334 | // after mixing. |
| 6335 | // |
| 6336 | // To enforce this guarantee: |
| 6337 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 6338 | // (mixedTracks == 0 && fastTracks > 0)) |
| 6339 | // must imply MIXER_TRACKS_READY. |
| 6340 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 6341 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 6342 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 6343 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | // if any fast tracks, then status is ready |
| 6347 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 6348 | if (fastTracks > 0) { |
| 6349 | mixerStatus = MIXER_TRACKS_READY; |
| 6350 | } |
| 6351 | return mixerStatus; |
| 6352 | } |
| 6353 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6354 | // trackCountForUid_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6355 | uint32_t PlaybackThread::trackCountForUid_l(uid_t uid) const |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6356 | { |
| 6357 | uint32_t trackCount = 0; |
| 6358 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 6359 | if (mTracks[i]->uid() == uid) { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6360 | trackCount++; |
| 6361 | } |
| 6362 | } |
| 6363 | return trackCount; |
| 6364 | } |
| 6365 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6366 | bool PlaybackThread::IsTimestampAdvancing::check(AudioStreamOut* output) |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6367 | { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6368 | // Check the timestamp to see if it's advancing once every 150ms. If we check too frequently, we |
| 6369 | // could falsely detect that the frame position has stalled due to underrun because we haven't |
| 6370 | // given the Audio HAL enough time to update. |
| 6371 | const nsecs_t nowNs = systemTime(); |
| 6372 | if (nowNs - mPreviousNs < mMinimumTimeBetweenChecksNs) { |
| 6373 | return mLatchedValue; |
| 6374 | } |
| 6375 | mPreviousNs = nowNs; |
| 6376 | mLatchedValue = false; |
| 6377 | // Determine if the presentation position is still advancing. |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6378 | uint64_t position = 0; |
| 6379 | struct timespec unused; |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6380 | const status_t ret = output->getPresentationPosition(&position, &unused); |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6381 | if (ret == NO_ERROR) { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6382 | if (position != mPreviousPosition) { |
| 6383 | mPreviousPosition = position; |
| 6384 | mLatchedValue = true; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6385 | } |
| 6386 | } |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6387 | return mLatchedValue; |
| 6388 | } |
| 6389 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6390 | void PlaybackThread::IsTimestampAdvancing::clear() |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6391 | { |
| 6392 | mLatchedValue = true; |
| 6393 | mPreviousPosition = 0; |
| 6394 | mPreviousNs = 0; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6395 | } |
| 6396 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6397 | // isTrackAllowed_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6398 | bool MixerThread::isTrackAllowed_l( |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6399 | audio_channel_mask_t channelMask, audio_format_t format, |
| 6400 | audio_session_t sessionId, uid_t uid) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6401 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6402 | if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 6403 | return false; |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 6404 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6405 | // Check validity as we don't call AudioMixer::create() here. |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 6406 | if (!mAudioMixer->isValidFormat(format)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6407 | ALOGW("%s: invalid format: %#x", __func__, format); |
| 6408 | return false; |
| 6409 | } |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 6410 | if (!mAudioMixer->isValidChannelMask(channelMask)) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6411 | ALOGW("%s: invalid channelMask: %#x", __func__, channelMask); |
| 6412 | return false; |
| 6413 | } |
| 6414 | return true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6415 | } |
| 6416 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 6417 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6418 | bool MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6419 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6420 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6421 | bool reconfig = false; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6422 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6423 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 6424 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6425 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6426 | AudioParameter param = AudioParameter(keyValuePair); |
| 6427 | int value; |
| 6428 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6429 | reconfig = true; |
| 6430 | } |
| 6431 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 6432 | if (!isValidPcmSinkFormat(static_cast<audio_format_t>(value))) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6433 | status = BAD_VALUE; |
| 6434 | } else { |
| 6435 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6436 | reconfig = true; |
| 6437 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6438 | } |
| 6439 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | d21a2ab | 2023-07-20 21:44:14 -0700 | [diff] [blame] | 6440 | if (!isValidPcmSinkChannelMask(static_cast<audio_channel_mask_t>(value))) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6441 | status = BAD_VALUE; |
| 6442 | } else { |
| 6443 | // no need to save value, since it's constant |
| 6444 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6445 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6446 | } |
| 6447 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6448 | // do not accept frame count changes if tracks are open as the track buffer |
| 6449 | // size depends on frame count and correct behavior would not be guaranteed |
| 6450 | // if frame count is changed after track creation |
| 6451 | if (!mTracks.isEmpty()) { |
| 6452 | status = INVALID_OPERATION; |
| 6453 | } else { |
| 6454 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6455 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6456 | } |
| 6457 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 6458 | LOG_FATAL("Should not set routing device in MixerThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6459 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6460 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6461 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6462 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6463 | if (!mStandby && status == INVALID_OPERATION) { |
Andy Hung | dda7aed | 2023-03-27 15:53:06 -0700 | [diff] [blame] | 6464 | ALOGW("%s: setParameters failed with keyValuePair %s, entering standby", |
| 6465 | __func__, keyValuePair.c_str()); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 6466 | mOutput->standby(); |
Andy Hung | dda7aed | 2023-03-27 15:53:06 -0700 | [diff] [blame] | 6467 | mThreadMetrics.logEndInterval(); |
| 6468 | mThreadSnapshot.onEnd(); |
| 6469 | setStandby_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6470 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 6471 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6472 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6473 | if (status == NO_ERROR && reconfig) { |
| 6474 | readOutputParameters_l(); |
| 6475 | delete mAudioMixer; |
| 6476 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6477 | for (const auto &track : mTracks) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6478 | const int trackId = track->id(); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6479 | const status_t createStatus = mAudioMixer->create( |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6480 | trackId, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6481 | track->channelMask(), |
| 6482 | track->format(), |
| 6483 | track->sessionId()); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 6484 | ALOGW_IF(createStatus != NO_ERROR, |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 6485 | "%s(): AudioMixer cannot create track(%d)" |
| 6486 | " mask %#x, format %#x, sessionId %d", |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 6487 | __func__, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6488 | trackId, track->channelMask(), track->format(), track->sessionId()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6489 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6490 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6491 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6492 | } |
| 6493 | |
Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 6494 | return reconfig; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6498 | void MixerThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6499 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6500 | PlaybackThread::dumpInternals_l(fd, args); |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 6501 | dprintf(fd, " Thread throttle time (msecs): %u\n", (uint32_t)mThreadThrottleTimeMs); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 6502 | dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6503 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6504 | dprintf(fd, " Master balance: %f (%s)\n", mMasterBalance.load(), |
| 6505 | (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance()) |
| 6506 | : mBalance.toString()).c_str()); |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6507 | if (hasFastMixer()) { |
| 6508 | dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid()); |
| 6509 | |
| 6510 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
| 6511 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6512 | // This is a large object so we place it on the heap. |
| 6513 | // 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] | 6514 | const std::unique_ptr<FastMixerDumpState> copy = |
| 6515 | std::make_unique<FastMixerDumpState>(mFastMixerDumpState); |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6516 | copy->dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6517 | |
| 6518 | #ifdef STATE_QUEUE_DUMP |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6519 | // Similar for state queue |
| 6520 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 6521 | observerCopy.dump(fd); |
| 6522 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 6523 | mutatorCopy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6524 | #endif |
| 6525 | |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 6526 | #ifdef AUDIO_WATCHDOG |
| 6527 | if (mAudioWatchdog != 0) { |
| 6528 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 6529 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 6530 | wdCopy.dump(fd); |
| 6531 | } |
| 6532 | #endif |
| 6533 | |
| 6534 | } else { |
| 6535 | dprintf(fd, " No FastMixer\n"); |
| 6536 | } |
Eric Laurent | 90cea10 | 2023-05-15 15:08:27 +0200 | [diff] [blame] | 6537 | |
| 6538 | dprintf(fd, "Bluetooth latency modes are %senabled\n", |
| 6539 | mBluetoothLatencyModesEnabled ? "" : "not "); |
| 6540 | dprintf(fd, "HAL does %ssupport Bluetooth latency modes\n", mOutput != nullptr && |
| 6541 | mOutput->audioHwDev->supportsBluetoothVariableLatency() ? "" : "not "); |
| 6542 | dprintf(fd, "Supported latency modes: %s\n", toString(mSupportedLatencyModes).c_str()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6543 | } |
| 6544 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6545 | uint32_t MixerThread::idleSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6546 | { |
| 6547 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 6548 | } |
| 6549 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6550 | uint32_t MixerThread::suspendSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6551 | { |
| 6552 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 6553 | } |
| 6554 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6555 | void MixerThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6556 | { |
| 6557 | PlaybackThread::cacheParameters_l(); |
| 6558 | |
| 6559 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 6560 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 6561 | // increase threshold again due to low power audio mode. The way this warning |
| 6562 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 6563 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 6564 | } |
| 6565 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6566 | void MixerThread::onHalLatencyModesChanged_l() { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6567 | mAfThreadCallback->onSupportedLatencyModesChanged(mId, mSupportedLatencyModes); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6568 | } |
| 6569 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6570 | void MixerThread::setHalLatencyMode_l() { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6571 | // Only handle latency mode if: |
| 6572 | // - mBluetoothLatencyModesEnabled is true |
| 6573 | // - the HAL supports latency modes |
| 6574 | // - the selected device is Bluetooth LE or A2DP |
| 6575 | if (!mBluetoothLatencyModesEnabled.load() || mSupportedLatencyModes.empty()) { |
| 6576 | return; |
| 6577 | } |
| 6578 | if (mOutDeviceTypeAddrs.size() != 1 |
| 6579 | || !(audio_is_a2dp_out_device(mOutDeviceTypeAddrs[0].mType) |
| 6580 | || audio_is_ble_out_device(mOutDeviceTypeAddrs[0].mType))) { |
| 6581 | return; |
| 6582 | } |
| 6583 | |
| 6584 | audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE; |
| 6585 | if (mSupportedLatencyModes.size() == 1) { |
| 6586 | // If the HAL only support one latency mode currently, confirm the choice |
| 6587 | latencyMode = mSupportedLatencyModes[0]; |
| 6588 | } else if (mSupportedLatencyModes.size() > 1) { |
| 6589 | // Request low latency if: |
| 6590 | // - At least one active track is either: |
| 6591 | // - a fast track with gaming usage or |
| 6592 | // - a track with acessibility usage |
| 6593 | for (const auto& track : mActiveTracks) { |
| 6594 | if ((track->isFastTrack() && track->attributes().usage == AUDIO_USAGE_GAME) |
| 6595 | || track->attributes().usage == AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) { |
| 6596 | latencyMode = AUDIO_LATENCY_MODE_LOW; |
| 6597 | break; |
| 6598 | } |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | if (latencyMode != mSetLatencyMode) { |
| 6603 | status_t status = mOutput->stream->setLatencyMode(latencyMode); |
| 6604 | ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d", |
| 6605 | __func__, mId, toString(latencyMode).c_str(), status); |
| 6606 | if (status == NO_ERROR) { |
| 6607 | mSetLatencyMode = latencyMode; |
| 6608 | } |
| 6609 | } |
| 6610 | } |
| 6611 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6612 | void MixerThread::updateHalSupportedLatencyModes_l() { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6613 | |
| 6614 | if (mOutput == nullptr || mOutput->stream == nullptr) { |
| 6615 | return; |
| 6616 | } |
| 6617 | std::vector<audio_latency_mode_t> latencyModes; |
| 6618 | const status_t status = mOutput->stream->getRecommendedLatencyModes(&latencyModes); |
| 6619 | if (status != NO_ERROR) { |
| 6620 | latencyModes.clear(); |
| 6621 | } |
| 6622 | if (latencyModes != mSupportedLatencyModes) { |
| 6623 | ALOGD("%s: thread(%d) status %d supported latency modes: %s", |
| 6624 | __func__, mId, status, toString(latencyModes).c_str()); |
| 6625 | mSupportedLatencyModes.swap(latencyModes); |
| 6626 | sendHalLatencyModesChangedEvent_l(); |
| 6627 | } |
| 6628 | } |
| 6629 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6630 | status_t MixerThread::getSupportedLatencyModes( |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6631 | std::vector<audio_latency_mode_t>* modes) { |
| 6632 | if (modes == nullptr) { |
| 6633 | return BAD_VALUE; |
| 6634 | } |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6635 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6636 | *modes = mSupportedLatencyModes; |
| 6637 | return NO_ERROR; |
| 6638 | } |
| 6639 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6640 | void MixerThread::onRecommendedLatencyModeChanged( |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6641 | std::vector<audio_latency_mode_t> modes) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6642 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6643 | if (modes != mSupportedLatencyModes) { |
| 6644 | ALOGD("%s: thread(%d) supported latency modes: %s", |
| 6645 | __func__, mId, toString(modes).c_str()); |
| 6646 | mSupportedLatencyModes.swap(modes); |
| 6647 | sendHalLatencyModesChangedEvent_l(); |
| 6648 | } |
| 6649 | } |
| 6650 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6651 | status_t MixerThread::setBluetoothVariableLatencyEnabled(bool enabled) { |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 6652 | if (mOutput == nullptr || mOutput->audioHwDev == nullptr |
| 6653 | || !mOutput->audioHwDev->supportsBluetoothVariableLatency()) { |
| 6654 | return INVALID_OPERATION; |
| 6655 | } |
| 6656 | mBluetoothLatencyModesEnabled.store(enabled); |
| 6657 | return NO_ERROR; |
| 6658 | } |
| 6659 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6660 | // ---------------------------------------------------------------------------- |
| 6661 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6662 | /* static */ |
| 6663 | sp<IAfPlaybackThread> IAfPlaybackThread::createDirectOutputThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6664 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6665 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady, |
| 6666 | const audio_offload_info_t& offloadInfo) { |
| 6667 | return sp<DirectOutputThread>::make( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6668 | afThreadCallback, output, id, systemReady, offloadInfo); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6669 | } |
| 6670 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6671 | DirectOutputThread::DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6672 | AudioStreamOut* output, audio_io_handle_t id, ThreadBase::type_t type, bool systemReady, |
| 6673 | const audio_offload_info_t& offloadInfo) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6674 | : PlaybackThread(afThreadCallback, output, id, type, systemReady) |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6675 | , mOffloadInfo(offloadInfo) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6676 | { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6677 | setMasterBalance(afThreadCallback->getMasterBalance_l()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6678 | } |
| 6679 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6680 | DirectOutputThread::~DirectOutputThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6681 | { |
| 6682 | } |
| 6683 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6684 | void DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6685 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 6686 | PlaybackThread::dumpInternals_l(fd, args); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6687 | dprintf(fd, " Master balance: %f Left: %f Right: %f\n", |
| 6688 | mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight); |
| 6689 | } |
| 6690 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6691 | void DirectOutputThread::setMasterBalance(float balance) |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6692 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 6693 | audio_utils::lock_guard _l(mutex()); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 6694 | if (mMasterBalance != balance) { |
| 6695 | mMasterBalance.store(balance); |
| 6696 | mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight); |
| 6697 | broadcast_l(); |
| 6698 | } |
| 6699 | } |
| 6700 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6701 | void DirectOutputThread::processVolume_l(IAfTrack* track, bool lastTrack) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6702 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6703 | float left, right; |
| 6704 | |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6705 | // Ensure volumeshaper state always advances even when muted. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6706 | const sp<AudioTrackServerProxy> proxy = track->audioTrackServerProxy(); |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6707 | |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6708 | const int64_t frames = mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 6709 | const int64_t time = mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 6710 | |
Dean Wheatley | b11b002 | 2023-09-12 04:07:35 +1000 | [diff] [blame] | 6711 | ALOGVV("%s: Direct/Offload bufferConsumed:%zu timestamp frames:%lld time:%lld", |
| 6712 | __func__, proxy->framesReleased(), (long long)frames, (long long)time); |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 6713 | |
| 6714 | const int64_t volumeShaperFrames = |
| 6715 | mMonotonicFrameCounter.updateAndGetMonotonicFrameCount(frames, time); |
| 6716 | const auto [shaperVolume, shaperActive] = |
| 6717 | track->getVolumeHandler()->getVolume(volumeShaperFrames); |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6718 | mVolumeShaperActive = shaperActive; |
| 6719 | |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 6720 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 6721 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 6722 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 6723 | |
| 6724 | const bool clientVolumeMute = (left == 0.f && right == 0.f); |
| 6725 | |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 6726 | if (mMasterMute || mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6727 | left = right = 0; |
| 6728 | } else { |
| 6729 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Andy Hung | 333ab96 | 2019-05-28 20:23:35 -0700 | [diff] [blame] | 6730 | const float v = mMasterVolume * typeVolume * shaperVolume; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 6731 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 6732 | if (left > GAIN_FLOAT_UNITY) { |
| 6733 | left = GAIN_FLOAT_UNITY; |
| 6734 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 6735 | if (right > GAIN_FLOAT_UNITY) { |
| 6736 | right = GAIN_FLOAT_UNITY; |
| 6737 | } |
zhangjincheng | 86cb3bc | 2023-01-16 17:17:38 +0800 | [diff] [blame] | 6738 | left *= v; |
| 6739 | right *= v; |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6740 | if (mAfThreadCallback->getMode() != AUDIO_MODE_IN_COMMUNICATION |
zhangjincheng | 86cb3bc | 2023-01-16 17:17:38 +0800 | [diff] [blame] | 6741 | || audio_channel_count_from_out_mask(mChannelMask) > 1) { |
| 6742 | left *= mMasterBalanceLeft; // DirectOutputThread balance applied as track volume |
| 6743 | right *= mMasterBalanceRight; |
| 6744 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6745 | } |
| 6746 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 6747 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 6748 | /*muteState=*/{mMasterMute, |
| 6749 | mStreamTypes[track->streamType()].volume == 0.f, |
| 6750 | mStreamTypes[track->streamType()].mute, |
Vlad Popa | e2f5aef | 2022-07-25 16:00:20 +0200 | [diff] [blame] | 6751 | track->isPlaybackRestricted(), |
Vlad Popa | 436c917 | 2022-08-02 15:25:08 +0200 | [diff] [blame] | 6752 | clientVolumeMute, |
| 6753 | shaperVolume == 0.f}); |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 6754 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6755 | if (lastTrack) { |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 6756 | track->setFinalVolume(left, right); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6757 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 6758 | mLeftVolFloat = left; |
| 6759 | mRightVolFloat = right; |
| 6760 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6761 | // Delegate volume control to effect in track effect chain if needed |
| 6762 | // only one effect chain can be present on DirectOutputThread, so if |
| 6763 | // there is one, the track is connected to it |
| 6764 | if (!mEffectChains.isEmpty()) { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 6765 | // if effect chain exists, volume is handled by it. |
| 6766 | // Convert volumes from float to 8.24 |
| 6767 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 6768 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 6769 | // Direct/Offload effect chains set output volume in setVolume(). |
| 6770 | (void)mEffectChains[0]->setVolume(&vl, &vr); |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 6771 | } else { |
| 6772 | // otherwise we directly set the volume. |
| 6773 | setVolumeForOutput_l(left, right); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6774 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6775 | } |
| 6776 | } |
| 6777 | } |
| 6778 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6779 | void DirectOutputThread::onAddNewTrack_l() |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6780 | { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6781 | sp<IAfTrack> previousTrack = mPreviousTrack.promote(); |
| 6782 | sp<IAfTrack> latestTrack = mActiveTracks.getLatest(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6783 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6784 | if (previousTrack != 0 && latestTrack != 0) { |
| 6785 | if (mType == DIRECT) { |
| 6786 | if (previousTrack.get() != latestTrack.get()) { |
| 6787 | mFlushPending = true; |
| 6788 | } |
| 6789 | } else /* mType == OFFLOAD */ { |
Dean Wheatley | 0f51fd0 | 2022-08-31 16:41:40 +1000 | [diff] [blame] | 6790 | if (previousTrack->sessionId() != latestTrack->sessionId() || |
| 6791 | previousTrack->isFlushPending()) { |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6792 | mFlushPending = true; |
| 6793 | } |
| 6794 | } |
Revathi Uddaraju | 20413a9 | 2016-10-13 17:16:14 +0800 | [diff] [blame] | 6795 | } else if (previousTrack == 0) { |
| 6796 | // there could be an old track added back during track transition for direct |
| 6797 | // output, so always issues flush to flush data of the previous track if it |
| 6798 | // was already destroyed with HAL paused, then flush can resume the playback |
| 6799 | mFlushPending = true; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6800 | } |
| 6801 | PlaybackThread::onAddNewTrack_l(); |
| 6802 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6803 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 6804 | PlaybackThread::mixer_state DirectOutputThread::prepareTracks_l( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6805 | Vector<sp<IAfTrack>>* tracksToRemove |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6806 | ) |
| 6807 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6808 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6809 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6810 | bool doHwPause = false; |
| 6811 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6812 | |
| 6813 | // find out which tracks need to be processed |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6814 | for (const sp<IAfTrack>& t : mActiveTracks) { |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6815 | if (t->isInvalid()) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6816 | ALOGW("An invalidated track shouldn't be in active list"); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6817 | tracksToRemove->add(t); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6818 | continue; |
| 6819 | } |
| 6820 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6821 | IAfTrack* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6822 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6823 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 6824 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6825 | // Only consider last track started for volume and mixer state control. |
| 6826 | // In theory an older track could underrun and restart after the new one starts |
| 6827 | // but as we only care about the transition phase between two tracks on a |
| 6828 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6829 | sp<IAfTrack> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6830 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6831 | |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 6832 | if (track->isPausePending()) { |
| 6833 | track->pauseAck(); |
| 6834 | // It is possible a track might have been flushed or stopped. |
| 6835 | // Other operations such as flush pending might occur on the next prepare. |
| 6836 | if (track->isPausing()) { |
| 6837 | track->setPaused(); |
| 6838 | } |
| 6839 | // Always perform pause, as an immediate flush will change |
| 6840 | // the pause state to be no longer isPausing(). |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 6841 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6842 | doHwPause = true; |
| 6843 | mHwPaused = true; |
| 6844 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6845 | } else if (track->isFlushPending()) { |
| 6846 | track->flushAck(); |
| 6847 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6848 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6849 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 6850 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6851 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6852 | if (last) { |
| 6853 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 6854 | if (mHwPaused) { |
| 6855 | doHwResume = true; |
| 6856 | mHwPaused = false; |
| 6857 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6858 | } |
| 6859 | } |
| 6860 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6861 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 6862 | // for all its buffers to be filled before processing it. |
| 6863 | // Allow draining the buffer in case the client |
| 6864 | // app does not call stop() and relies on underrun to stop: |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6865 | // hence the test on (track->retryCount() > 1). |
| 6866 | // If track->retryCount() <= 1 then track is about to be disabled, paused, removed, |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 6867 | // so we accept any nonzero amount of data delivered by the AudioTrack (which will |
| 6868 | // reset the retry counter). |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 6869 | // Do not use a high threshold for compressed audio. |
Andy Hung | 455982f | 2021-04-27 17:46:12 -0700 | [diff] [blame] | 6870 | |
| 6871 | // target retry count that we will use is based on the time we wait for retries. |
| 6872 | const int32_t targetRetryCount = kMaxTrackRetriesDirectMs * 1000 / mActiveSleepTimeUs; |
| 6873 | // the retry threshold is when we accept any size for PCM data. This is slightly |
| 6874 | // smaller than the retry count so we can push small bits of data without a glitch. |
| 6875 | const int32_t retryThreshold = targetRetryCount > 2 ? targetRetryCount - 1 : 1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6876 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 6877 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6878 | && (track->retryCount() > retryThreshold) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6879 | minFrames = mNormalFrameCount; |
| 6880 | } else { |
| 6881 | minFrames = 1; |
| 6882 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6883 | |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 6884 | const size_t framesReady = track->framesReady(); |
| 6885 | const int trackId = track->id(); |
| 6886 | if (ATRACE_ENABLED()) { |
| 6887 | std::string traceName("nRdy"); |
| 6888 | traceName += std::to_string(trackId); |
| 6889 | ATRACE_INT(traceName.c_str(), framesReady); |
| 6890 | } |
| 6891 | if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() && |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6892 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6893 | { |
Mikhail Naganov | ddb07bc | 2019-08-15 20:18:47 -0700 | [diff] [blame] | 6894 | ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6895 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6896 | if (track->fillingStatus() == IAfTrack::FS_FILLED) { |
| 6897 | track->fillingStatus() = IAfTrack::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 6898 | if (last) { |
| 6899 | // make sure processVolume_l() will apply new volume even if 0 |
| 6900 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 6901 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6902 | if (!mHwSupportsPause) { |
| 6903 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6904 | } |
| 6905 | } |
| 6906 | |
| 6907 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6908 | processVolume_l(track, last); |
| 6909 | if (last) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6910 | sp<IAfTrack> previousTrack = mPreviousTrack.promote(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6911 | if (previousTrack != 0) { |
| 6912 | if (track != previousTrack.get()) { |
| 6913 | // Flush any data still being written from last track |
| 6914 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 6915 | // Invalidate previous track to force a seek when resuming. |
| 6916 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | mPreviousTrack = track; |
| 6920 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6921 | // reset retry count |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6922 | track->retryCount() = targetRetryCount; |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 6923 | mActiveTrack = t; |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6924 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 6925 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 6926 | doHwResume = true; |
| 6927 | mHwPaused = false; |
| 6928 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6929 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6930 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6931 | // clear effect chain input buffer if the last active track started underruns |
| 6932 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6933 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6934 | mEffectChains[0]->clearInputBuffer(); |
| 6935 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6936 | if (track->isStopping_1()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6937 | track->setState(IAfTrackBase::STOPPING_2); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 6938 | if (last && mHwPaused) { |
| 6939 | doHwResume = true; |
| 6940 | mHwPaused = false; |
| 6941 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6942 | } |
| 6943 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 6944 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6945 | // We have consumed all the buffers of this track. |
| 6946 | // Remove it from the list of active tracks. |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 6947 | bool presComplete = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 6948 | if (mStandby || !last || |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 6949 | (presComplete = track->presentationComplete(latency_l())) || |
Jindong | 32dc26e | 2019-11-11 18:10:01 +0800 | [diff] [blame] | 6950 | track->isPaused() || mHwPaused) { |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 6951 | if (presComplete) { |
| 6952 | mOutput->presentationComplete(); |
| 6953 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6954 | if (track->isStopping_2()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6955 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 6956 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6957 | if (track->isStopped()) { |
| 6958 | track->reset(); |
| 6959 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6960 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6961 | } |
| 6962 | } else { |
| 6963 | // No buffers for this track. Give it a few chances to |
| 6964 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 6965 | // Only consider last track started for mixer state control |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6966 | bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput); |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 6967 | if (!isTunerStream() // tuner streams remain active in underrun |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6968 | && --(track->retryCount()) <= 0) { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 6969 | if (isTimestampAdvancing) { // HAL is still playing audio, give us more time. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 6970 | track->retryCount() = kMaxTrackRetriesOffload; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6971 | } else { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 6972 | ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to" |
| 6973 | " underrun on thread %d", __func__, trackId, mId); |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 6974 | tracksToRemove->add(track); |
| 6975 | // indicate to client process that the track was disabled because of |
| 6976 | // underrun; it will then automatically call start() when data is available |
| 6977 | track->disable(); |
| 6978 | // only do hw pause when track is going to be removed due to BUFFER TIMEOUT. |
| 6979 | // unlike mixerthread, HAL can be paused for direct output |
| 6980 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 6981 | "minFrames = %u, mFormat = %#x", |
| 6982 | framesReady, minFrames, mFormat); |
| 6983 | if (last && mHwSupportsPause && !mHwPaused && !mStandby) { |
| 6984 | doHwPause = true; |
| 6985 | mHwPaused = true; |
| 6986 | } |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 6987 | } |
Haynes Mathew George | 5c6daae | 2017-01-24 20:06:05 -0800 | [diff] [blame] | 6988 | } else if (last) { |
| 6989 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6990 | } |
| 6991 | } |
| 6992 | } |
| 6993 | } |
| 6994 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6995 | // 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] | 6996 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 6997 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6998 | if (mTracks[i]->isFlushPending()) { |
| 6999 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7000 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7001 | } |
| 7002 | } |
| 7003 | } |
| 7004 | |
| 7005 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 7006 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 7007 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 7008 | // if resume is received before pause is executed. |
| 7009 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7010 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7011 | status_t result = mOutput->stream->pause(); |
| 7012 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Gareth Fenn | cd1e162 | 2022-10-05 11:45:45 -0700 | [diff] [blame] | 7013 | doHwResume = !doHwPause; // resume if pause is due to flush. |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7014 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7015 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7016 | flushHw_l(); |
| 7017 | } |
| 7018 | if (mHwSupportsPause && !mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7019 | status_t result = mOutput->stream->resume(); |
| 7020 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7021 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7022 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7023 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7024 | |
| 7025 | return mixerStatus; |
| 7026 | } |
| 7027 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7028 | void DirectOutputThread::threadLoop_mix() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7029 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7030 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 7031 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7032 | // output audio to hardware |
| 7033 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 7034 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7035 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7036 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 7037 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7038 | // no need to pad with 0 for compressed audio |
| 7039 | if (audio_has_proportional_frames(mFormat)) { |
| 7040 | memset(curBuf, 0, frameCount * mFrameSize); |
| 7041 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7042 | break; |
| 7043 | } |
| 7044 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 7045 | frameCount -= buffer.frameCount; |
| 7046 | curBuf += buffer.frameCount * mFrameSize; |
| 7047 | mActiveTrack->releaseBuffer(&buffer); |
| 7048 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 7049 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7050 | mSleepTimeUs = 0; |
| 7051 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7052 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7053 | } |
| 7054 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7055 | void DirectOutputThread::threadLoop_sleepTime() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7056 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7057 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 7058 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7059 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7060 | return; |
| 7061 | } |
Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame] | 7062 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 7063 | mSleepTimeUs = mActiveSleepTimeUs; |
| 7064 | } else { |
| 7065 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7066 | } |
Andy Hung | 85ba333 | 2021-04-27 17:40:26 -0700 | [diff] [blame] | 7067 | // Note: In S or later, we do not write zeroes for |
| 7068 | // linear or proportional PCM direct tracks in underrun. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7069 | } |
| 7070 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7071 | void DirectOutputThread::threadLoop_exit() |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7072 | { |
| 7073 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7074 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7075 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7076 | if (mTracks[i]->isFlushPending()) { |
| 7077 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7078 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7079 | } |
| 7080 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7081 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7082 | flushHw_l(); |
| 7083 | } |
| 7084 | } |
| 7085 | PlaybackThread::threadLoop_exit(); |
| 7086 | } |
| 7087 | |
| 7088 | // must be called with thread mutex locked |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7089 | bool DirectOutputThread::shouldStandby_l() |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7090 | { |
| 7091 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7092 | bool trackStopped = false; |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7093 | bool trackDisabled = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7094 | |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7095 | // do not put the HAL in standby when paused. NuPlayer clear the offloaded AudioTrack |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7096 | // after a timeout and we will enter standby then. |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7097 | // On offload threads, do not enter standby if the main track is still underrunning. |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7098 | if (mTracks.size() > 0) { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7099 | const auto& mainTrack = mTracks[mTracks.size() - 1]; |
| 7100 | |
| 7101 | trackPaused = mainTrack->isPaused(); |
| 7102 | trackStopped = mainTrack->isStopped() || mainTrack->state() == IAfTrackBase::IDLE; |
| 7103 | trackDisabled = (mType == OFFLOAD) && mainTrack->isDisabled(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7104 | } |
| 7105 | |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7106 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped) || trackDisabled); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 7107 | } |
| 7108 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7109 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7110 | bool DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7111 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7112 | { |
| 7113 | bool reconfig = false; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7114 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7115 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7116 | AudioParameter param = AudioParameter(keyValuePair); |
| 7117 | int value; |
| 7118 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 7119 | LOG_FATAL("Should not set routing device in DirectOutputThread"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7120 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7121 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7122 | // do not accept frame count changes if tracks are open as the track buffer |
| 7123 | // size depends on frame count and correct behavior would not be garantied |
| 7124 | // if frame count is changed after track creation |
| 7125 | if (!mTracks.isEmpty()) { |
| 7126 | status = INVALID_OPERATION; |
| 7127 | } else { |
| 7128 | reconfig = true; |
| 7129 | } |
| 7130 | } |
| 7131 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7132 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7133 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7134 | mOutput->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7135 | if (!mStandby) { |
| 7136 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 7137 | mThreadSnapshot.onEnd(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 7138 | setStandby_l(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7139 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7140 | mBytesWritten = 0; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7141 | status = mOutput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7142 | } |
| 7143 | if (status == NO_ERROR && reconfig) { |
| 7144 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7145 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7146 | } |
| 7147 | } |
| 7148 | |
Dean Wheatley | 6891810 | 2021-03-19 22:09:19 +1100 | [diff] [blame] | 7149 | return reconfig; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7150 | } |
| 7151 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7152 | uint32_t DirectOutputThread::activeSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7153 | { |
| 7154 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7155 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7156 | time = PlaybackThread::activeSleepTimeUs(); |
| 7157 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7158 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7159 | } |
| 7160 | return time; |
| 7161 | } |
| 7162 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7163 | uint32_t DirectOutputThread::idleSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7164 | { |
| 7165 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7166 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7167 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 7168 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7169 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7170 | } |
| 7171 | return time; |
| 7172 | } |
| 7173 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7174 | uint32_t DirectOutputThread::suspendSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7175 | { |
| 7176 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7177 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7178 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 7179 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 7180 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7181 | } |
| 7182 | return time; |
| 7183 | } |
| 7184 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7185 | void DirectOutputThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7186 | { |
| 7187 | PlaybackThread::cacheParameters_l(); |
| 7188 | |
| 7189 | // use shorter standby delay as on normal output to release |
| 7190 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 7191 | // no delay on outputs with HW A/V sync |
| 7192 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7193 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 7194 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7195 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7196 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7197 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7198 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7199 | } |
| 7200 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7201 | void DirectOutputThread::flushHw_l() |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7202 | { |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7203 | PlaybackThread::flushHw_l(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 7204 | mOutput->flush(); |
Haofan Wang | 3987e9d | 2024-06-17 21:22:00 +0000 | [diff] [blame] | 7205 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 7206 | mFlushPending = false; |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7207 | mTimestampVerifier.discontinuity(discontinuityForStandbyOrFlush()); |
Sampath Shetty | 999f0e8 | 2020-01-15 10:19:06 +1100 | [diff] [blame] | 7208 | mTimestamp.clear(); |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 7209 | mMonotonicFrameCounter.onFlush(); |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7210 | } |
| 7211 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7212 | int64_t DirectOutputThread::computeWaitTimeNs_l() const { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 7213 | // If a VolumeShaper is active, we must wake up periodically to update volume. |
| 7214 | const int64_t NS_PER_MS = 1000000; |
| 7215 | return mVolumeShaperActive ? |
| 7216 | kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l(); |
| 7217 | } |
| 7218 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7219 | // ---------------------------------------------------------------------------- |
| 7220 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7221 | AsyncCallbackThread::AsyncCallbackThread( |
| 7222 | const wp<PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7223 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7224 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7225 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7226 | mDrainSequence(0), |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7227 | mAsyncError(ASYNC_ERROR_NONE) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7228 | { |
| 7229 | } |
| 7230 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7231 | void AsyncCallbackThread::onFirstRef() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7232 | { |
| 7233 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 7234 | } |
| 7235 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7236 | bool AsyncCallbackThread::threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7237 | { |
| 7238 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7239 | uint32_t writeAckSequence; |
| 7240 | uint32_t drainSequence; |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7241 | AsyncError asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7242 | |
| 7243 | { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7244 | audio_utils::unique_lock _l(mutex()); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7245 | while (!((mWriteAckSequence & 1) || |
| 7246 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7247 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7248 | exitPending())) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7249 | mWaitWorkCV.wait(_l); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 7250 | } |
| 7251 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7252 | if (exitPending()) { |
| 7253 | break; |
| 7254 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7255 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 7256 | mWriteAckSequence, mDrainSequence); |
| 7257 | writeAckSequence = mWriteAckSequence; |
| 7258 | mWriteAckSequence &= ~1; |
| 7259 | drainSequence = mDrainSequence; |
| 7260 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7261 | asyncError = mAsyncError; |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7262 | mAsyncError = ASYNC_ERROR_NONE; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7263 | } |
| 7264 | { |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7265 | const sp<PlaybackThread> playbackThread = mPlaybackThread.promote(); |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7266 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7267 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7268 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7269 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7270 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 7271 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7272 | } |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7273 | if (asyncError != ASYNC_ERROR_NONE) { |
| 7274 | playbackThread->onAsyncError(asyncError == ASYNC_ERROR_HARD); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7275 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7276 | } |
| 7277 | } |
| 7278 | } |
| 7279 | return false; |
| 7280 | } |
| 7281 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7282 | void AsyncCallbackThread::exit() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7283 | { |
| 7284 | ALOGV("AsyncCallbackThread::exit"); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7285 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7286 | requestExit(); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7287 | mWaitWorkCV.notify_all(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7288 | } |
| 7289 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7290 | void AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7291 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7292 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7293 | // bit 0 is cleared |
| 7294 | mWriteAckSequence = sequence << 1; |
| 7295 | } |
| 7296 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7297 | void AsyncCallbackThread::resetWriteBlocked() |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7298 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7299 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7300 | // ignore unexpected callbacks |
| 7301 | if (mWriteAckSequence & 2) { |
| 7302 | mWriteAckSequence |= 1; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7303 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7304 | } |
| 7305 | } |
| 7306 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7307 | void AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7308 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7309 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7310 | // bit 0 is cleared |
| 7311 | mDrainSequence = sequence << 1; |
| 7312 | } |
| 7313 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7314 | void AsyncCallbackThread::resetDraining() |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7315 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7316 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7317 | // ignore unexpected callbacks |
| 7318 | if (mDrainSequence & 2) { |
| 7319 | mDrainSequence |= 1; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7320 | mWaitWorkCV.notify_one(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7321 | } |
| 7322 | } |
| 7323 | |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7324 | void AsyncCallbackThread::setAsyncError(bool isHardError) |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7325 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7326 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | f548cd3 | 2024-05-29 17:06:46 +0000 | [diff] [blame] | 7327 | mAsyncError = isHardError ? ASYNC_ERROR_HARD : ASYNC_ERROR_SOFT; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7328 | mWaitWorkCV.notify_one(); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 7329 | } |
| 7330 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7331 | |
| 7332 | // ---------------------------------------------------------------------------- |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7333 | |
| 7334 | /* static */ |
| 7335 | sp<IAfPlaybackThread> IAfPlaybackThread::createOffloadThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7336 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7337 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady, |
| 7338 | const audio_offload_info_t& offloadInfo) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7339 | return sp<OffloadThread>::make(afThreadCallback, output, id, systemReady, offloadInfo); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7340 | } |
| 7341 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7342 | OffloadThread::OffloadThread(const sp<IAfThreadCallback>& afThreadCallback, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7343 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady, |
| 7344 | const audio_offload_info_t& offloadInfo) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7345 | : DirectOutputThread(afThreadCallback, output, id, OFFLOAD, systemReady, offloadInfo), |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 7346 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7347 | { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 7348 | //FIXME: mStandby should be set to true by ThreadBase constructo |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7349 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 7350 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7351 | } |
| 7352 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7353 | void OffloadThread::threadLoop_exit() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7354 | { |
| 7355 | if (mFlushPending || mHwPaused) { |
| 7356 | // If a flush is pending or track was paused, just discard buffered data |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 7357 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7358 | flushHw_l(); |
| 7359 | } else { |
| 7360 | mMixerStatus = MIXER_DRAIN_ALL; |
| 7361 | threadLoop_drain(); |
| 7362 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 7363 | if (mUseAsyncWrite) { |
| 7364 | ALOG_ASSERT(mCallbackThread != 0); |
| 7365 | mCallbackThread->exit(); |
| 7366 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7367 | PlaybackThread::threadLoop_exit(); |
| 7368 | } |
| 7369 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7370 | PlaybackThread::mixer_state OffloadThread::prepareTracks_l( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7371 | Vector<sp<IAfTrack>>* tracksToRemove |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7372 | ) |
| 7373 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7374 | size_t count = mActiveTracks.size(); |
| 7375 | |
| 7376 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7377 | bool doHwPause = false; |
| 7378 | bool doHwResume = false; |
| 7379 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7380 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 7381 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7382 | // find out which tracks need to be processed |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7383 | for (const sp<IAfTrack>& t : mActiveTracks) { |
| 7384 | IAfTrack* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7385 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7386 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 7387 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7388 | // Only consider last track started for volume and mixer state control. |
| 7389 | // In theory an older track could underrun and restart after the new one starts |
| 7390 | // but as we only care about the transition phase between two tracks on a |
| 7391 | // direct output, it is not a problem to ignore the underrun case. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7392 | sp<IAfTrack> l = mActiveTracks.getLatest(); |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7393 | bool last = l.get() == track; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7394 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7395 | if (track->isInvalid()) { |
| 7396 | ALOGW("An invalidated track shouldn't be in active list"); |
| 7397 | tracksToRemove->add(track); |
| 7398 | continue; |
| 7399 | } |
| 7400 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7401 | if (track->state() == IAfTrackBase::IDLE) { |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7402 | ALOGW("An idle track shouldn't be in active list"); |
| 7403 | continue; |
| 7404 | } |
| 7405 | |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 7406 | if (track->isPausePending()) { |
| 7407 | track->pauseAck(); |
| 7408 | // It is possible a track might have been flushed or stopped. |
| 7409 | // Other operations such as flush pending might occur on the next prepare. |
| 7410 | if (track->isPausing()) { |
| 7411 | track->setPaused(); |
| 7412 | } |
| 7413 | // Always perform pause if last, as an immediate flush will change |
| 7414 | // the pause state to be no longer isPausing(). |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7415 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 7416 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7417 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7418 | mHwPaused = true; |
| 7419 | } |
| 7420 | // If we were part way through writing the mixbuffer to |
| 7421 | // the HAL we must save this until we resume |
| 7422 | // BUG - this will be wrong if a different track is made active, |
| 7423 | // in that case we want to discard the pending data in the |
| 7424 | // mixbuffer and tell the client to present it again when the |
| 7425 | // track is resumed |
| 7426 | mPausedWriteLength = mCurrentWriteLength; |
| 7427 | mPausedBytesRemaining = mBytesRemaining; |
| 7428 | mBytesRemaining = 0; // stop writing |
| 7429 | } |
| 7430 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7431 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7432 | if (track->isStopping_1()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7433 | track->retryCount() = kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7434 | } else { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7435 | track->retryCount() = kMaxTrackRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7436 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 7437 | track->flushAck(); |
| 7438 | if (last) { |
| 7439 | mFlushPending = true; |
| 7440 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7441 | } else if (track->isResumePending()){ |
| 7442 | track->resumeAck(); |
| 7443 | if (last) { |
| 7444 | if (mPausedBytesRemaining) { |
| 7445 | // Need to continue write that was interrupted |
| 7446 | mCurrentWriteLength = mPausedWriteLength; |
| 7447 | mBytesRemaining = mPausedBytesRemaining; |
| 7448 | mPausedBytesRemaining = 0; |
| 7449 | } |
| 7450 | if (mHwPaused) { |
| 7451 | doHwResume = true; |
| 7452 | mHwPaused = false; |
| 7453 | // threadLoop_mix() will handle the case that we need to |
| 7454 | // resume an interrupted write |
| 7455 | } |
| 7456 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7457 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7458 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7459 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 7460 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 7461 | // Do not handle new data in this iteration even if track->framesReady() |
| 7462 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 7463 | } |
| 7464 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7465 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7466 | ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7467 | if (track->fillingStatus() == IAfTrack::FS_FILLED) { |
| 7468 | track->fillingStatus() = IAfTrack::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 7469 | if (last) { |
| 7470 | // make sure processVolume_l() will apply new volume even if 0 |
| 7471 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 7472 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7473 | } |
| 7474 | |
| 7475 | if (last) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7476 | sp<IAfTrack> previousTrack = mPreviousTrack.promote(); |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7477 | if (previousTrack != 0) { |
| 7478 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7479 | // Flush any data still being written from last track |
| 7480 | mBytesRemaining = 0; |
| 7481 | if (mPausedBytesRemaining) { |
| 7482 | // Last track was paused so we also need to flush saved |
| 7483 | // mixbuffer state and invalidate track so that it will |
| 7484 | // re-submit that unwritten data when it is next resumed |
| 7485 | mPausedBytesRemaining = 0; |
| 7486 | // Invalidate is a bit drastic - would be more efficient |
| 7487 | // to have a flag to tell client that some of the |
| 7488 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7489 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7490 | } |
| 7491 | // flush data already sent to the DSP if changing audio session as audio |
| 7492 | // comes from a different source. Also invalidate previous track to force a |
| 7493 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 7494 | if (previousTrack->sessionId() != track->sessionId()) { |
| 7495 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 7496 | } |
| 7497 | } |
| 7498 | } |
| 7499 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7500 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7501 | if (track->isStopping_1()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7502 | track->retryCount() = kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7503 | } else { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7504 | track->retryCount() = kMaxTrackRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7505 | } |
Eric Laurent | 5850c4c | 2016-11-10 13:04:31 -0800 | [diff] [blame] | 7506 | mActiveTrack = t; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7507 | mixerStatus = MIXER_TRACKS_READY; |
| 7508 | } |
| 7509 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7510 | ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7511 | if (track->isStopping_1()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7512 | if (--(track->retryCount()) <= 0) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7513 | // Hardware buffer can hold a large amount of audio so we must |
| 7514 | // wait for all current track's data to drain before we say |
| 7515 | // that the track is stopped. |
| 7516 | if (mBytesRemaining == 0) { |
| 7517 | // Only start draining when all data in mixbuffer |
| 7518 | // has been written |
| 7519 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7520 | track->setState(IAfTrackBase::STOPPING_2); |
| 7521 | // so presentation completes after |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7522 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 7523 | if (last && !mStandby) { |
| 7524 | // do not modify drain sequence if we are already draining. This happens |
| 7525 | // when resuming from pause after drain. |
| 7526 | if ((mDrainSequence & 1) == 0) { |
| 7527 | mSleepTimeUs = 0; |
| 7528 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 7529 | mixerStatus = MIXER_DRAIN_TRACK; |
| 7530 | mDrainSequence += 2; |
| 7531 | } |
| 7532 | if (mHwPaused) { |
| 7533 | // It is possible to move from PAUSED to STOPPING_1 without |
| 7534 | // a resume so we must ensure hardware is running |
| 7535 | doHwResume = true; |
| 7536 | mHwPaused = false; |
| 7537 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7538 | } |
| 7539 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7540 | } else if (last) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7541 | ALOGV("stopping1 underrun retries left %d", track->retryCount()); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 7542 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7543 | } |
| 7544 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 7545 | // Drain has completed or we are in standby, signal presentation complete |
| 7546 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7547 | track->setState(IAfTrackBase::STOPPED); |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 7548 | mOutput->presentationComplete(); |
| 7549 | track->presentationComplete(latency_l()); // always returns true |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7550 | track->reset(); |
| 7551 | tracksToRemove->add(track); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7552 | // OFFLOADED stop resets frame counts. |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 7553 | if (!mUseAsyncWrite) { |
| 7554 | // If we don't get explicit drain notification we must |
| 7555 | // register discontinuity regardless of whether this is |
| 7556 | // the previous (!last) or the upcoming (last) track |
| 7557 | // to avoid skipping the discontinuity. |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 7558 | mTimestampVerifier.discontinuity( |
| 7559 | mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 7560 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7561 | } |
| 7562 | } else { |
| 7563 | // No buffers for this track. Give it a few chances to |
| 7564 | // fill a buffer, then remove it from active list. |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7565 | bool isTimestampAdvancing = mIsTimestampAdvancing.check(mOutput); |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 7566 | if (!isTunerStream() // tuner streams remain active in underrun |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7567 | && --(track->retryCount()) <= 0) { |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 7568 | if (isTimestampAdvancing) { // HAL is still playing audio, give us more time. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7569 | track->retryCount() = kMaxTrackRetriesOffload; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7570 | } else { |
Eric Laurent | 022a513 | 2024-04-12 17:02:51 +0000 | [diff] [blame] | 7571 | ALOGI("%s BUFFER TIMEOUT: remove track(%d) from active list due to" |
| 7572 | " underrun on thread %d", __func__, track->id(), mId); |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7573 | tracksToRemove->add(track); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 7574 | // tell client process that the track was disabled because of underrun; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 7575 | // it will then automatically call start() when data is available |
| 7576 | track->disable(); |
| 7577 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7578 | } else if (last){ |
| 7579 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 7580 | } |
| 7581 | } |
| 7582 | } |
| 7583 | // compute volume for this track |
Wenfeng Sun | 7945833 | 2019-05-29 20:12:43 +0800 | [diff] [blame] | 7584 | if (track->isReady()) { // check ready to prevent premature start. |
| 7585 | processVolume_l(track, last); |
| 7586 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7587 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7588 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 7589 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 7590 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 7591 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 7592 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7593 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7594 | status_t result = mOutput->stream->pause(); |
| 7595 | ALOGE_IF(result != OK, "Error when pausing output stream: %d", result); |
Gareth Fenn | cd1e162 | 2022-10-05 11:45:45 -0700 | [diff] [blame] | 7596 | doHwResume = !doHwPause; // resume if pause is due to flush. |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7597 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7598 | if (mFlushPending) { |
| 7599 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7600 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 7601 | if (!mStandby && doHwResume) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 7602 | status_t result = mOutput->stream->resume(); |
| 7603 | ALOGE_IF(result != OK, "Error when resuming output stream: %d", result); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 7604 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 7605 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7606 | // remove all the tracks that need to be... |
| 7607 | removeTracks_l(*tracksToRemove); |
| 7608 | |
| 7609 | return mixerStatus; |
| 7610 | } |
| 7611 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7612 | // must be called with thread mutex locked |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7613 | bool OffloadThread::waitingAsyncCallback_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7614 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7615 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 7616 | mWriteAckSequence, mDrainSequence); |
| 7617 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7618 | return true; |
| 7619 | } |
| 7620 | return false; |
| 7621 | } |
| 7622 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7623 | bool OffloadThread::waitingAsyncCallback() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7624 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7625 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7626 | return waitingAsyncCallback_l(); |
| 7627 | } |
| 7628 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7629 | void OffloadThread::flushHw_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7630 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 7631 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7632 | // Flush anything still waiting in the mixbuffer |
| 7633 | mCurrentWriteLength = 0; |
| 7634 | mBytesRemaining = 0; |
| 7635 | mPausedWriteLength = 0; |
| 7636 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 7637 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 7638 | mBytesWritten = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 7639 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7640 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7641 | // discard any pending drain or write ack by incrementing sequence |
| 7642 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 7643 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7644 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 7645 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 7646 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7647 | } |
| 7648 | } |
| 7649 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7650 | void OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 7651 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7652 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 7653 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 7654 | mFlushPending = true; |
| 7655 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 7656 | } |
| 7657 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7658 | void OffloadThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7659 | audio_utils::lock_guard _l(mutex()); |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 7660 | if (PlaybackThread::invalidateTracks_l(portIds)) { |
| 7661 | mFlushPending = true; |
| 7662 | } |
| 7663 | } |
| 7664 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 7665 | // ---------------------------------------------------------------------------- |
| 7666 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7667 | /* static */ |
| 7668 | sp<IAfDuplicatingThread> IAfDuplicatingThread::create( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7669 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7670 | IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7671 | return sp<DuplicatingThread>::make(afThreadCallback, mainThread, id, systemReady); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7672 | } |
| 7673 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7674 | DuplicatingThread::DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7675 | IAfPlaybackThread* mainThread, audio_io_handle_t id, bool systemReady) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7676 | : MixerThread(afThreadCallback, mainThread->getOutput(), id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 7677 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7678 | mWaitTimeMs(UINT_MAX) |
| 7679 | { |
| 7680 | addOutputTrack(mainThread); |
| 7681 | } |
| 7682 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7683 | DuplicatingThread::~DuplicatingThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7684 | { |
| 7685 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 7686 | mOutputTracks[i]->destroy(); |
| 7687 | } |
| 7688 | } |
| 7689 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7690 | void DuplicatingThread::threadLoop_mix() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7691 | { |
| 7692 | // mix buffers... |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 7693 | if (outputsReady()) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7694 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7695 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 7696 | if (mMixerBufferValid) { |
| 7697 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 7698 | } else { |
| 7699 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 7700 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7701 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7702 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7703 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7704 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7705 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7706 | } |
| 7707 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7708 | void DuplicatingThread::threadLoop_sleepTime() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7709 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7710 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7711 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7712 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7713 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7714 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7715 | } |
| 7716 | } else if (mBytesWritten != 0) { |
| 7717 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 7718 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7719 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7720 | } else { |
| 7721 | // flush remaining overflow buffers in output tracks |
| 7722 | writeFrames = 0; |
| 7723 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 7724 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7725 | } |
| 7726 | } |
| 7727 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7728 | ssize_t DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7729 | { |
| 7730 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 7731 | const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames); |
| 7732 | |
| 7733 | // Consider the first OutputTrack for timestamp and frame counting. |
| 7734 | |
| 7735 | // The threadLoop() generally assumes writing a full sink buffer size at a time. |
| 7736 | // Here, we correct for writeFrames of 0 (a stop) or underruns because |
| 7737 | // we always claim success. |
| 7738 | if (i == 0) { |
| 7739 | const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten; |
| 7740 | ALOGD_IF(correction != 0 && writeFrames != 0, |
| 7741 | "%s: writeFrames:%u actualWritten:%zd correction:%zd mFramesWritten:%lld", |
| 7742 | __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten); |
| 7743 | mFramesWritten -= correction; |
| 7744 | } |
| 7745 | |
| 7746 | // 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] | 7747 | } |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7748 | if (mStandby) { |
| 7749 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 7750 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 7751 | mStandby = false; |
| 7752 | } |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 7753 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7754 | } |
| 7755 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7756 | void DuplicatingThread::threadLoop_standby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7757 | { |
| 7758 | // DuplicatingThread implements standby by stopping all tracks |
| 7759 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 7760 | outputTracks[i]->stop(); |
| 7761 | } |
| 7762 | } |
| 7763 | |
Andy Hung | 8a5abfd | 2023-12-07 19:35:12 -0800 | [diff] [blame] | 7764 | void DuplicatingThread::threadLoop_exit() |
| 7765 | { |
| 7766 | // Prevent calling the OutputTrack dtor in the DuplicatingThread dtor |
| 7767 | // where other mutexes (i.e. AudioPolicyService_Mutex) may be held. |
| 7768 | // Do so here in the threadLoop_exit(). |
| 7769 | |
| 7770 | SortedVector <sp<IAfOutputTrack>> localTracks; |
| 7771 | { |
| 7772 | audio_utils::lock_guard l(mutex()); |
| 7773 | localTracks = std::move(mOutputTracks); |
| 7774 | mOutputTracks.clear(); |
| 7775 | } |
| 7776 | localTracks.clear(); |
| 7777 | outputTracks.clear(); |
| 7778 | PlaybackThread::threadLoop_exit(); |
| 7779 | } |
| 7780 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7781 | void DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7782 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 7783 | MixerThread::dumpInternals_l(fd, args); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7784 | |
| 7785 | std::stringstream ss; |
| 7786 | const size_t numTracks = mOutputTracks.size(); |
| 7787 | ss << " " << numTracks << " OutputTracks"; |
| 7788 | if (numTracks > 0) { |
| 7789 | ss << ":"; |
| 7790 | for (const auto &track : mOutputTracks) { |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7791 | const auto thread = track->thread().promote(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 7792 | ss << " (" << track->id() << " : "; |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 7793 | if (thread.get() != nullptr) { |
| 7794 | ss << thread.get() << ", " << thread->id(); |
| 7795 | } else { |
| 7796 | ss << "null"; |
| 7797 | } |
| 7798 | ss << ")"; |
| 7799 | } |
| 7800 | } |
| 7801 | ss << "\n"; |
| 7802 | std::string result = ss.str(); |
| 7803 | write(fd, result.c_str(), result.size()); |
| 7804 | } |
| 7805 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7806 | void DuplicatingThread::saveOutputTracks() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7807 | { |
| 7808 | outputTracks = mOutputTracks; |
| 7809 | } |
| 7810 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7811 | void DuplicatingThread::clearOutputTracks() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7812 | { |
| 7813 | outputTracks.clear(); |
| 7814 | } |
| 7815 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7816 | void DuplicatingThread::addOutputTrack(IAfPlaybackThread* thread) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7817 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7818 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 7819 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 7820 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 7821 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 7822 | const size_t frameCount = |
| 7823 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 7824 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 7825 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 7826 | // nearly empty and the other may be dropping data). |
| 7827 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7828 | // TODO b/182392769: use attribution source util, move to server edge |
| 7829 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 7830 | attributionSource.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7831 | IPCThreadState::self()->getCallingUid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7832 | attributionSource.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 7833 | IPCThreadState::self()->getCallingPid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7834 | attributionSource.token = sp<BBinder>::make(); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 7835 | sp<IAfOutputTrack> outputTrack = IAfOutputTrack::create(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7836 | this, |
| 7837 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 7838 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7839 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 7840 | frameCount, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 7841 | attributionSource); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 7842 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 7843 | if (status != NO_ERROR) { |
| 7844 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 7845 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7846 | } |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 7847 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
| 7848 | mOutputTracks.add(outputTrack); |
| 7849 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 7850 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7851 | } |
| 7852 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7853 | void DuplicatingThread::removeOutputTrack(IAfPlaybackThread* thread) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7854 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7855 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7856 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 7857 | if (mOutputTracks[i]->thread() == thread) { |
| 7858 | mOutputTracks[i]->destroy(); |
| 7859 | mOutputTracks.removeAt(i); |
| 7860 | updateWaitTime_l(); |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 7861 | // NO_THREAD_SAFETY_ANALYSIS |
| 7862 | // Lambda workaround: as thread != this |
| 7863 | // we can safely call the remote thread getOutput. |
| 7864 | const bool equalOutput = |
| 7865 | [&](){ return thread->getOutput() == mOutput; }(); |
| 7866 | if (equalOutput) { |
| 7867 | mOutput = nullptr; |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 7868 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7869 | return; |
| 7870 | } |
| 7871 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 7872 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7873 | } |
| 7874 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 7875 | // caller must hold mutex() |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7876 | void DuplicatingThread::updateWaitTime_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7877 | { |
| 7878 | mWaitTimeMs = UINT_MAX; |
| 7879 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7880 | const auto strong = mOutputTracks[i]->thread().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7881 | if (strong != 0) { |
| 7882 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 7883 | if (waitTimeMs < mWaitTimeMs) { |
| 7884 | mWaitTimeMs = waitTimeMs; |
| 7885 | } |
| 7886 | } |
| 7887 | } |
| 7888 | } |
| 7889 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7890 | bool DuplicatingThread::outputsReady() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7891 | { |
| 7892 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7893 | const auto thread = outputTracks[i]->thread().promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7894 | if (thread == 0) { |
| 7895 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 7896 | outputTracks[i].get()); |
| 7897 | return false; |
| 7898 | } |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 7899 | IAfPlaybackThread* const playbackThread = thread->asIAfPlaybackThread().get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7900 | // see note at standby() declaration |
Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 7901 | if (playbackThread->inStandby() && !playbackThread->isSuspended()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7902 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 7903 | thread.get()); |
| 7904 | return false; |
| 7905 | } |
| 7906 | } |
| 7907 | return true; |
| 7908 | } |
| 7909 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7910 | void DuplicatingThread::sendMetadataToBackend_l( |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 7911 | const StreamOutHalInterface::SourceMetadata& metadata) |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 7912 | { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 7913 | for (auto& outputTrack : outputTracks) { // not mOutputTracks |
| 7914 | outputTrack->setMetadatas(metadata.tracks); |
| 7915 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 7916 | } |
| 7917 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7918 | uint32_t DuplicatingThread::activeSleepTimeUs() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7919 | { |
Andy Hung | 7a6a0f0 | 2023-11-29 13:42:08 -0800 | [diff] [blame] | 7920 | // return half the wait time in microseconds. |
| 7921 | return std::min(mWaitTimeMs * 500ULL, (unsigned long long)UINT32_MAX); // prevent overflow. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7922 | } |
| 7923 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7924 | void DuplicatingThread::cacheParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7925 | { |
| 7926 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 7927 | updateWaitTime_l(); |
| 7928 | |
| 7929 | MixerThread::cacheParameters_l(); |
| 7930 | } |
| 7931 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7932 | // ---------------------------------------------------------------------------- |
| 7933 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7934 | /* static */ |
| 7935 | sp<IAfPlaybackThread> IAfPlaybackThread::createSpatializerThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7936 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7937 | AudioStreamOut* output, |
| 7938 | audio_io_handle_t id, |
| 7939 | bool systemReady, |
| 7940 | audio_config_base_t* mixerConfig) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7941 | return sp<SpatializerThread>::make(afThreadCallback, output, id, systemReady, mixerConfig); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7942 | } |
| 7943 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7944 | SpatializerThread::SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7945 | AudioStreamOut* output, |
| 7946 | audio_io_handle_t id, |
| 7947 | bool systemReady, |
| 7948 | audio_config_base_t *mixerConfig) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 7949 | : MixerThread(afThreadCallback, output, id, systemReady, SPATIALIZER, mixerConfig) |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 7950 | { |
| 7951 | } |
| 7952 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7953 | void SpatializerThread::setHalLatencyMode_l() { |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7954 | // if mSupportedLatencyModes is empty, the HAL stream does not support |
| 7955 | // latency mode control and we can exit. |
| 7956 | if (mSupportedLatencyModes.empty()) { |
| 7957 | return; |
| 7958 | } |
Eric Laurent | 4c85e37 | 2024-02-23 16:50:06 +0000 | [diff] [blame] | 7959 | // Do not update the HAL latency mode if no track is active |
| 7960 | if (mActiveTracks.isEmpty()) { |
| 7961 | return; |
| 7962 | } |
| 7963 | |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7964 | audio_latency_mode_t latencyMode = AUDIO_LATENCY_MODE_FREE; |
| 7965 | if (mSupportedLatencyModes.size() == 1) { |
| 7966 | // If the HAL only support one latency mode currently, confirm the choice |
| 7967 | latencyMode = mSupportedLatencyModes[0]; |
| 7968 | } else if (mSupportedLatencyModes.size() > 1) { |
| 7969 | // Request low latency if: |
| 7970 | // - The low latency mode is requested by the spatializer controller |
| 7971 | // (mRequestedLatencyMode = AUDIO_LATENCY_MODE_LOW) |
| 7972 | // AND |
| 7973 | // - At least one active track is spatialized |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7974 | for (const auto& track : mActiveTracks) { |
| 7975 | if (track->isSpatialized()) { |
Eric Laurent | b024157 | 2024-02-01 21:03:49 +0100 | [diff] [blame] | 7976 | latencyMode = mRequestedLatencyMode; |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7977 | break; |
| 7978 | } |
| 7979 | } |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | if (latencyMode != mSetLatencyMode) { |
| 7983 | status_t status = mOutput->stream->setLatencyMode(latencyMode); |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 7984 | ALOGD("%s: thread(%d) setLatencyMode(%s) returned %d", |
| 7985 | __func__, mId, toString(latencyMode).c_str(), status); |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7986 | if (status == NO_ERROR) { |
| 7987 | mSetLatencyMode = latencyMode; |
| 7988 | } |
| 7989 | } |
| 7990 | } |
| 7991 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 7992 | status_t SpatializerThread::setRequestedLatencyMode(audio_latency_mode_t mode) { |
Eric Laurent | b024157 | 2024-02-01 21:03:49 +0100 | [diff] [blame] | 7993 | if (mode < 0 || mode >= AUDIO_LATENCY_MODE_CNT) { |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7994 | return BAD_VALUE; |
| 7995 | } |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 7996 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 7997 | mRequestedLatencyMode = mode; |
| 7998 | return NO_ERROR; |
| 7999 | } |
| 8000 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8001 | void SpatializerThread::checkOutputStageEffects() |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8002 | NO_THREAD_SAFETY_ANALYSIS |
| 8003 | // 'createEffect_l' requires holding mutex 'AudioFlinger_Mutex' exclusively |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8004 | { |
| 8005 | bool hasVirtualizer = false; |
| 8006 | bool hasDownMixer = false; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8007 | sp<IAfEffectHandle> finalDownMixer; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8008 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8009 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8010 | sp<IAfEffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8011 | if (chain != 0) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 8012 | hasVirtualizer = chain->getEffectFromType_l(FX_IID_SPATIALIZER) != nullptr; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8013 | hasDownMixer = chain->getEffectFromType_l(EFFECT_UIID_DOWNMIX) != nullptr; |
| 8014 | } |
| 8015 | |
| 8016 | finalDownMixer = mFinalDownMixer; |
| 8017 | mFinalDownMixer.clear(); |
| 8018 | } |
| 8019 | |
| 8020 | if (hasVirtualizer) { |
| 8021 | if (finalDownMixer != nullptr) { |
| 8022 | int32_t ret; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8023 | finalDownMixer->asIEffect()->disable(&ret); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8024 | } |
| 8025 | finalDownMixer.clear(); |
| 8026 | } else if (!hasDownMixer) { |
| 8027 | std::vector<effect_descriptor_t> descriptors; |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8028 | status_t status = mAfThreadCallback->getEffectsFactoryHal()->getDescriptors( |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8029 | EFFECT_UIID_DOWNMIX, &descriptors); |
| 8030 | if (status != NO_ERROR) { |
| 8031 | return; |
| 8032 | } |
| 8033 | ALOG_ASSERT(!descriptors.empty(), |
| 8034 | "%s getDescriptors() returned no error but empty list", __func__); |
| 8035 | |
| 8036 | finalDownMixer = createEffect_l(nullptr /*client*/, nullptr /*effectClient*/, |
| 8037 | 0 /*priority*/, AUDIO_SESSION_OUTPUT_STAGE, &descriptors[0], nullptr /*enabled*/, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 8038 | &status, false /*pinned*/, false /*probe*/, false /*notifyFramesProcessed*/); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8039 | |
| 8040 | if (finalDownMixer == nullptr || (status != NO_ERROR && status != ALREADY_EXISTS)) { |
| 8041 | ALOGW("%s error creating downmixer %d", __func__, status); |
| 8042 | finalDownMixer.clear(); |
| 8043 | } else { |
| 8044 | int32_t ret; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8045 | finalDownMixer->asIEffect()->enable(&ret); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8046 | } |
| 8047 | } |
| 8048 | |
| 8049 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8050 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 8051 | mFinalDownMixer = finalDownMixer; |
| 8052 | } |
| 8053 | } |
| 8054 | |
Andy Hung | e251446 | 2023-12-06 14:59:24 -0800 | [diff] [blame] | 8055 | void SpatializerThread::threadLoop_exit() |
| 8056 | { |
| 8057 | // The Spatializer EffectHandle must be released on the PlaybackThread |
| 8058 | // threadLoop() to prevent lock inversion in the SpatializerThread dtor. |
| 8059 | mFinalDownMixer.clear(); |
| 8060 | |
| 8061 | PlaybackThread::threadLoop_exit(); |
| 8062 | } |
| 8063 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8064 | // ---------------------------------------------------------------------------- |
| 8065 | // Record |
| 8066 | // ---------------------------------------------------------------------------- |
| 8067 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8068 | sp<IAfRecordThread> IAfRecordThread::create(const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8069 | AudioStreamIn* input, |
| 8070 | audio_io_handle_t id, |
| 8071 | bool systemReady) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8072 | return sp<RecordThread>::make(afThreadCallback, input, id, systemReady); |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 8073 | } |
| 8074 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8075 | RecordThread::RecordThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8076 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8077 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 8078 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 8079 | ) : |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8080 | ThreadBase(afThreadCallback, id, RECORD, systemReady, false /* isOut */), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8081 | mInput(input), |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8082 | mSource(mInput), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 8083 | mActiveTracks(&this->mLocalLog), |
| 8084 | mRsmpInBuffer(NULL), |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8085 | // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8086 | mRsmpInRear(0) |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 8087 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 8088 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8089 | // mFastCapture below |
| 8090 | , mFastCaptureFutex(0) |
| 8091 | // mInputSource |
| 8092 | // mPipeSink |
| 8093 | // mPipeSource |
| 8094 | , mPipeFramesP2(0) |
| 8095 | // mPipeMemory |
| 8096 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8097 | , mFastTrackAvail(false) |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 8098 | , mBtNrecSuspended(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8099 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 8100 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8101 | mNBLogWriter = afThreadCallback->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8102 | |
George Burgess IV | a8f90c1 | 2020-05-14 11:27:19 -0700 | [diff] [blame] | 8103 | if (mInput->audioHwDev != nullptr) { |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8104 | mIsMsdDevice = strcmp( |
| 8105 | mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0; |
| 8106 | } |
| 8107 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 8108 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8109 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8110 | // TODO: We may also match on address as well as device type for |
| 8111 | // 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] | 8112 | // TODO: This property should be ensure that only contains one single device type. |
| 8113 | mTimestampCorrectedDevice = (audio_devices_t)property_get_int64( |
| 8114 | "audio.timestamp.corrected_input_device", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8115 | (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD |
| 8116 | : AUDIO_DEVICE_NONE)); |
| 8117 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8118 | // create an NBAIO source for the HAL input stream, and negotiate |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 8119 | mInputSource = new AudioStreamInSource(input->stream); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8120 | size_t numCounterOffers = 0; |
| 8121 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 8122 | #if !LOG_NDEBUG |
Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 8123 | [[maybe_unused]] ssize_t index = |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 8124 | #else |
| 8125 | (void) |
| 8126 | #endif |
| 8127 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8128 | ALOG_ASSERT(index == 0); |
| 8129 | |
| 8130 | // initialize fast capture depending on configuration |
| 8131 | bool initFastCapture; |
| 8132 | switch (kUseFastCapture) { |
| 8133 | case FastCapture_Never: |
| 8134 | initFastCapture = false; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8135 | ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8136 | break; |
| 8137 | case FastCapture_Always: |
| 8138 | initFastCapture = true; |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8139 | ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8140 | break; |
| 8141 | case FastCapture_Static: |
Sampath | 6fac233 | 2022-12-16 17:34:37 +1100 | [diff] [blame] | 8142 | initFastCapture = !mIsMsdDevice // Disable fast capture for MSD BUS devices. |
Dean Wheatley | 8e5d9e4 | 2023-11-03 12:37:27 +1100 | [diff] [blame] | 8143 | && audio_is_linear_pcm(mFormat) |
Sampath | 6fac233 | 2022-12-16 17:34:37 +1100 | [diff] [blame] | 8144 | && (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Dean Wheatley | 8e5d9e4 | 2023-11-03 12:37:27 +1100 | [diff] [blame] | 8145 | ALOGV("%p kUseFastCapture = Static, format = 0x%x, (%lld * 1000) / %u vs %u, " |
| 8146 | "initFastCapture = %d, mIsMsdDevice = %d", this, mFormat, (long long)mFrameCount, |
| 8147 | mSampleRate, kMinNormalCaptureBufferSizeMs, initFastCapture, mIsMsdDevice); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8148 | break; |
| 8149 | // case FastCapture_Dynamic: |
| 8150 | } |
| 8151 | |
| 8152 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 8153 | // 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] | 8154 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8155 | // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread |
| 8156 | size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8157 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8158 | void *pipeBuffer = nullptr; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8159 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 8160 | sp<IMemory> pipeMemory; |
| 8161 | if ((roHeap == 0) || |
| 8162 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 8163 | (pipeBuffer = pipeMemory->unsecurePointer()) == nullptr) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 8164 | ALOGE("not enough memory for pipe buffer size=%zu; " |
| 8165 | "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld", |
| 8166 | pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer, |
| 8167 | (long long)kRecordThreadReadOnlyHeapSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8168 | goto failed; |
| 8169 | } |
| 8170 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 8171 | memset(pipeBuffer, 0, pipeSize); |
| 8172 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8173 | const NBAIO_Format offersFast[1] = {format}; |
| 8174 | size_t numCounterOffersFast = 0; |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8175 | [[maybe_unused]] ssize_t index2 = pipe->negotiate(offersFast, std::size(offersFast), |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8176 | nullptr /* counterOffers */, numCounterOffersFast); |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8177 | ALOG_ASSERT(index2 == 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8178 | mPipeSink = pipe; |
| 8179 | PipeReader *pipeReader = new PipeReader(*pipe); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8180 | numCounterOffersFast = 0; |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8181 | index2 = pipeReader->negotiate(offersFast, std::size(offersFast), |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8182 | nullptr /* counterOffers */, numCounterOffersFast); |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 8183 | ALOG_ASSERT(index2 == 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8184 | mPipeSource = pipeReader; |
| 8185 | mPipeFramesP2 = pipeFramesP2; |
| 8186 | mPipeMemory = pipeMemory; |
| 8187 | |
| 8188 | // create fast capture |
| 8189 | mFastCapture = new FastCapture(); |
| 8190 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8191 | #ifdef STATE_QUEUE_DUMP |
| 8192 | // FIXME |
| 8193 | #endif |
| 8194 | FastCaptureState *state = sq->begin(); |
| 8195 | state->mCblk = NULL; |
| 8196 | state->mInputSource = mInputSource.get(); |
| 8197 | state->mInputSourceGen++; |
| 8198 | state->mPipeSink = pipe; |
| 8199 | state->mPipeSinkGen++; |
| 8200 | state->mFrameCount = mFrameCount; |
| 8201 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 8202 | // already done in constructor initialization list |
| 8203 | //mFastCaptureFutex = 0; |
| 8204 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 8205 | state->mColdGen++; |
| 8206 | state->mDumpState = &mFastCaptureDumpState; |
| 8207 | #ifdef TEE_SINK |
| 8208 | // FIXME |
| 8209 | #endif |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8210 | mFastCaptureNBLogWriter = |
| 8211 | afThreadCallback->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8212 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 8213 | sq->end(); |
| 8214 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 8215 | |
| 8216 | // start the fast capture |
| 8217 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 8218 | pid_t tid = mFastCapture->getTid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 8219 | sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/); |
Mikhail Naganov | e1c4b5d | 2016-12-22 09:22:45 -0800 | [diff] [blame] | 8220 | stream()->setHalThreadPriority(kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8221 | #ifdef AUDIO_WATCHDOG |
| 8222 | // FIXME |
| 8223 | #endif |
| 8224 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 8225 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8226 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 8227 | #ifdef TEE_SINK |
| 8228 | mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD); |
| 8229 | mTee.setId(std::string("_") + std::to_string(mId) + "_C"); |
| 8230 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8231 | failed: ; |
| 8232 | |
| 8233 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8234 | } |
| 8235 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8236 | RecordThread::~RecordThread() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8237 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8238 | if (mFastCapture != 0) { |
| 8239 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8240 | FastCaptureState *state = sq->begin(); |
| 8241 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 8242 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 8243 | if (old == -1) { |
| 8244 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 8245 | } |
| 8246 | } |
| 8247 | state->mCommand = FastCaptureState::EXIT; |
| 8248 | sq->end(); |
| 8249 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 8250 | mFastCapture->join(); |
| 8251 | mFastCapture.clear(); |
| 8252 | } |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8253 | mAfThreadCallback->unregisterWriter(mFastCaptureNBLogWriter); |
| 8254 | mAfThreadCallback->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8255 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8256 | } |
| 8257 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8258 | void RecordThread::onFirstRef() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8259 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 8260 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8261 | } |
| 8262 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8263 | void RecordThread::preExit() |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8264 | { |
| 8265 | ALOGV(" preExit()"); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8266 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8267 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8268 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8269 | track->invalidate(); |
| 8270 | } |
| 8271 | mActiveTracks.clear(); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8272 | mStartStopCV.notify_all(); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 8273 | } |
| 8274 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8275 | bool RecordThread::threadLoop() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8276 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8277 | nsecs_t lastWarning = 0; |
| 8278 | |
| 8279 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8280 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8281 | reacquire_wakelock: |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8282 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8283 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 8284 | acquireWakeLock_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8285 | } |
| 8286 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8287 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 8288 | uint32_t sleepUs = 0; |
| 8289 | |
Andy Hung | 1381a07 | 2023-10-20 16:41:18 -0700 | [diff] [blame] | 8290 | // timestamp correction enable is determined under lock, used in processing step. |
| 8291 | bool timestampCorrectionEnabled = false; |
| 8292 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8293 | int64_t lastLoopCountRead = -2; // never matches "previous" loop, when loopCount = 0. |
| 8294 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8295 | // loop while there is work to do |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8296 | for (int64_t loopCount = 0;; ++loopCount) { // loopCount used for statistics tracking |
Andy Hung | fdb84b9 | 2024-03-15 10:15:10 -0700 | [diff] [blame] | 8297 | // Note: these sp<> are released at the end of the for loop outside of the mutex() lock. |
| 8298 | sp<IAfRecordTrack> activeTrack; |
Andy Hung | b2b01c6 | 2024-04-23 13:56:19 -0700 | [diff] [blame] | 8299 | std::vector<sp<IAfRecordTrack>> oldActiveTracks; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 8300 | Vector<sp<IAfEffectChain>> effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 8301 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8302 | // activeTracks accumulates a copy of a subset of mActiveTracks |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8303 | Vector<sp<IAfRecordTrack>> activeTracks; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8304 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8305 | // reference to the (first and only) active fast track |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8306 | sp<IAfRecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 8307 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8308 | // reference to a fast track which is about to be removed |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8309 | sp<IAfRecordTrack> fastTrackToRemove; |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8310 | |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8311 | bool silenceFastCapture = false; |
| 8312 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8313 | { // scope for mutex() |
| 8314 | audio_utils::unique_lock _l(mutex()); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8315 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 8316 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8317 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8318 | // check exitPending here because checkForNewParameters_l() and |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8319 | // checkForNewParameters_l() can temporarily release mutex() |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 8320 | if (exitPending()) { |
| 8321 | break; |
| 8322 | } |
| 8323 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8324 | // sleep with mutex unlocked |
| 8325 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 8326 | ATRACE_BEGIN("sleepC"); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8327 | (void)mWaitWorkCV.wait_for(_l, std::chrono::microseconds(sleepUs)); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8328 | ATRACE_END(); |
| 8329 | sleepUs = 0; |
| 8330 | continue; |
| 8331 | } |
| 8332 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8333 | // if no active track(s), then standby and release wakelock |
| 8334 | size_t size = mActiveTracks.size(); |
| 8335 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 8336 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 8337 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8338 | releaseWakeLock_l(); |
| 8339 | ALOGV("RecordThread: loop stopping"); |
| 8340 | // go to sleep |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8341 | mWaitWorkCV.wait(_l); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8342 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 8343 | goto reacquire_wakelock; |
| 8344 | } |
| 8345 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8346 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8347 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8348 | for (size_t i = 0; i < size; ) { |
Andy Hung | b2b01c6 | 2024-04-23 13:56:19 -0700 | [diff] [blame] | 8349 | if (activeTrack) { // ensure track release is outside lock. |
| 8350 | oldActiveTracks.emplace_back(std::move(activeTrack)); |
| 8351 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8352 | activeTrack = mActiveTracks[i]; |
| 8353 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8354 | if (activeTrack->isFastTrack()) { |
| 8355 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 8356 | fastTrackToRemove = activeTrack; |
| 8357 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8358 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8359 | mActiveTracks.remove(activeTrack); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8360 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8361 | continue; |
| 8362 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8363 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8364 | IAfTrackBase::track_state activeTrackState = activeTrack->state(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8365 | switch (activeTrackState) { |
| 8366 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8367 | case IAfTrackBase::PAUSING: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8368 | mActiveTracks.remove(activeTrack); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8369 | activeTrack->setState(IAfTrackBase::PAUSED); |
François Gaffie | 39634e4 | 2023-10-17 12:13:32 +0200 | [diff] [blame] | 8370 | if (activeTrack->isFastTrack()) { |
| 8371 | ALOGV("%s fast track is paused, thus removed from active list", __func__); |
| 8372 | // Keep a ref on fast track to wait for FastCapture thread to get updated |
| 8373 | // state before potential track removal |
| 8374 | fastTrackToRemove = activeTrack; |
| 8375 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8376 | doBroadcast = true; |
| 8377 | size--; |
| 8378 | continue; |
| 8379 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8380 | case IAfTrackBase::STARTING_1: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8381 | sleepUs = 10000; |
| 8382 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8383 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8384 | continue; |
| 8385 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8386 | case IAfTrackBase::STARTING_2: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8387 | doBroadcast = true; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8388 | if (mStandby) { |
| 8389 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 8390 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8391 | mStandby = false; |
| 8392 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8393 | activeTrack->setState(IAfTrackBase::ACTIVE); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8394 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8395 | break; |
| 8396 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8397 | case IAfTrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8398 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8399 | break; |
| 8400 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8401 | case IAfTrackBase::IDLE: // cannot be on ActiveTracks if idle |
| 8402 | case IAfTrackBase::PAUSED: // cannot be on ActiveTracks if paused |
| 8403 | case IAfTrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8404 | default: |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 8405 | LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu", |
| 8406 | __func__, activeTrackState, activeTrack->id(), size); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8407 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8408 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8409 | if (activeTrack->isFastTrack()) { |
| 8410 | ALOG_ASSERT(!mFastTrackAvail); |
| 8411 | ALOG_ASSERT(fastTrack == 0); |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8412 | // if the active fast track is silenced either: |
| 8413 | // 1) silence the whole capture from fast capture buffer if this is |
| 8414 | // the only active track |
| 8415 | // 2) invalidate this track: this will cause the client to reconnect and possibly |
| 8416 | // be invalidated again until unsilenced |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8417 | bool invalidate = false; |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8418 | if (activeTrack->isSilenced()) { |
| 8419 | if (size > 1) { |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8420 | invalidate = true; |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8421 | } else { |
| 8422 | silenceFastCapture = true; |
| 8423 | } |
| 8424 | } |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8425 | // Invalidate fast tracks if access to audio history is required as this is not |
| 8426 | // possible with fast tracks. Once the fast track has been invalidated, no new |
| 8427 | // fast track will be created until mMaxSharedAudioHistoryMs is cleared. |
| 8428 | if (mMaxSharedAudioHistoryMs != 0) { |
| 8429 | invalidate = true; |
| 8430 | } |
| 8431 | if (invalidate) { |
| 8432 | activeTrack->invalidate(); |
| 8433 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 8434 | fastTrackToRemove = activeTrack; |
| 8435 | removeTrack_l(activeTrack); |
| 8436 | mActiveTracks.remove(activeTrack); |
| 8437 | size--; |
| 8438 | continue; |
| 8439 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8440 | fastTrack = activeTrack; |
| 8441 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8442 | |
| 8443 | activeTracks.add(activeTrack); |
| 8444 | i++; |
| 8445 | |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8446 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8447 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 8448 | mActiveTracks.updatePowerState_l(this); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 8449 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 8450 | updateMetadata_l(); |
| 8451 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 8452 | if (allStopped) { |
| 8453 | standbyIfNotAlreadyInStandby(); |
| 8454 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8455 | if (doBroadcast) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8456 | mStartStopCV.notify_all(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8457 | } |
| 8458 | |
| 8459 | // sleep if there are no active tracks to process |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 8460 | if (activeTracks.isEmpty()) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8461 | if (sleepUs == 0) { |
| 8462 | sleepUs = kRecordThreadSleepUs; |
| 8463 | } |
| 8464 | continue; |
| 8465 | } |
| 8466 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 8467 | |
Andy Hung | 1381a07 | 2023-10-20 16:41:18 -0700 | [diff] [blame] | 8468 | timestampCorrectionEnabled = isTimestampCorrectionEnabled_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8469 | lockEffectChains_l(effectChains); |
| 8470 | } |
| 8471 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8472 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 8473 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8474 | size_t size = effectChains.size(); |
| 8475 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8476 | // thread mutex is not locked, but effect chain is locked |
| 8477 | effectChains[i]->process_l(); |
| 8478 | } |
| 8479 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8480 | // 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] | 8481 | if (mFastCapture != 0) { |
| 8482 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8483 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8484 | bool didModify = false; |
| 8485 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8486 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 8487 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 8488 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 8489 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 8490 | if (old == -1) { |
| 8491 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 8492 | } |
| 8493 | } |
| 8494 | state->mCommand = FastCaptureState::READ_WRITE; |
| 8495 | #if 0 // FIXME |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 8496 | mFastCaptureDumpState.increaseSamplingN(mAfThreadCallback->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 8497 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 8498 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8499 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8500 | didModify = true; |
| 8501 | } |
| 8502 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 8503 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 8504 | if (cblkNew != cblkOld) { |
| 8505 | state->mCblk = cblkNew; |
| 8506 | // block until acked if removing a fast track |
| 8507 | if (cblkOld != NULL) { |
| 8508 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 8509 | } |
| 8510 | didModify = true; |
| 8511 | } |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 8512 | AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ? |
| 8513 | reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr; |
| 8514 | if (state->mFastPatchRecordBufferProvider != abp) { |
| 8515 | state->mFastPatchRecordBufferProvider = abp; |
| 8516 | state->mFastPatchRecordFormat = fastTrack == 0 ? |
| 8517 | AUDIO_FORMAT_INVALID : fastTrack->format(); |
| 8518 | didModify = true; |
| 8519 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 8520 | if (state->mSilenceCapture != silenceFastCapture) { |
| 8521 | state->mSilenceCapture = silenceFastCapture; |
| 8522 | didModify = true; |
| 8523 | } |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8524 | sq->end(didModify); |
| 8525 | if (didModify) { |
| 8526 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8527 | #if 0 |
| 8528 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 8529 | mNormalSource = mPipeSource; |
| 8530 | } |
| 8531 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8532 | } |
| 8533 | } |
| 8534 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 8535 | // now run the fast track destructor with thread mutex unlocked |
| 8536 | fastTrackToRemove.clear(); |
| 8537 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8538 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 8539 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 8540 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 8541 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 8542 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8543 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8544 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8545 | ssize_t framesRead = 0; // not needed, remove clang-tidy warning. |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8546 | const int64_t lastIoBeginNs = systemTime(); // start IO timing |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8547 | |
| 8548 | // If an NBAIO source is present, use it to read the normal capture's data |
| 8549 | if (mPipeSource != 0) { |
Andy Hung | 156317a | 2018-08-02 11:49:29 -0700 | [diff] [blame] | 8550 | size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2); |
Andy Hung | d5b638f | 2018-04-30 13:56:10 -0700 | [diff] [blame] | 8551 | |
| 8552 | // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer |
| 8553 | // to the full buffer point (clearing the overflow condition). Upon OVERRUN error, |
| 8554 | // we immediately retry the read() to get data and prevent another overflow. |
| 8555 | for (int retries = 0; retries <= 2; ++retries) { |
| 8556 | ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries); |
| 8557 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
| 8558 | framesToRead); |
| 8559 | if (framesRead != OVERRUN) break; |
| 8560 | } |
| 8561 | |
Andy Hung | 7a3dc6b | 2018-05-01 16:39:51 -0700 | [diff] [blame] | 8562 | const ssize_t availableToRead = mPipeSource->availableToRead(); |
| 8563 | if (availableToRead >= 0) { |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 8564 | mMonopipePipeDepthStats.add(availableToRead); |
Glenn Kasten | a2f59b3 | 2020-08-03 16:37:24 -0700 | [diff] [blame] | 8565 | // 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] | 8566 | LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2, |
| 8567 | "more frames to read than fifo size, %zd > %zu", |
| 8568 | availableToRead, mPipeFramesP2); |
| 8569 | const size_t pipeFramesFree = mPipeFramesP2 - availableToRead; |
| 8570 | const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2; |
| 8571 | ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd", |
| 8572 | mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead); |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 8573 | sleepUs = (sleepFrames * 1000000LL) / mSampleRate; |
| 8574 | } |
| 8575 | if (framesRead < 0) { |
| 8576 | status_t status = (status_t) framesRead; |
| 8577 | switch (status) { |
| 8578 | case OVERRUN: |
| 8579 | ALOGW("overrun on read from pipe"); |
| 8580 | framesRead = 0; |
| 8581 | break; |
| 8582 | case NEGOTIATE: |
| 8583 | ALOGE("re-negotiation is needed"); |
| 8584 | framesRead = -1; // Will cause an attempt to recover. |
| 8585 | break; |
| 8586 | default: |
| 8587 | ALOGE("unknown error %d on read from pipe", status); |
| 8588 | break; |
| 8589 | } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8590 | } |
| 8591 | // otherwise use the HAL / AudioStreamIn directly |
| 8592 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 8593 | ATRACE_BEGIN("read"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8594 | size_t bytesRead; |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8595 | status_t result = mSource->read( |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8596 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 8597 | ATRACE_END(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8598 | if (result < 0) { |
| 8599 | framesRead = result; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8600 | } else { |
| 8601 | framesRead = bytesRead / mFrameSize; |
| 8602 | } |
| 8603 | } |
| 8604 | |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 8605 | const int64_t lastIoEndNs = systemTime(); // end IO timing |
| 8606 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8607 | // Update server timestamp with server stats |
| 8608 | // systemTime() is optional if the hardware supports timestamps. |
Andy Hung | 743f640 | 2020-06-03 13:17:04 -0700 | [diff] [blame] | 8609 | if (framesRead >= 0) { |
| 8610 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 8611 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs; |
| 8612 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8613 | |
| 8614 | // Update server timestamp with kernel stats |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8615 | if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8616 | int64_t position, time; |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 8617 | if (mStandby) { |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 8618 | mTimestampVerifier.discontinuity(audio_is_linear_pcm(mFormat) ? |
| 8619 | mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS : |
| 8620 | mTimestampVerifier.DISCONTINUITY_MODE_ZERO); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8621 | } else if (mSource->getCapturePosition(&position, &time) == NO_ERROR |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8622 | && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) { |
| 8623 | |
| 8624 | mTimestampVerifier.add(position, time, mSampleRate); |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 8625 | if (timestampCorrectionEnabled) { |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 8626 | ALOGVV("TS_BEFORE: %d %lld %lld", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8627 | id(), (long long)time, (long long)position); |
| 8628 | auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp(); |
| 8629 | position = correctedTimestamp.mFrames; |
| 8630 | time = correctedTimestamp.mTimeNs; |
Dean Wheatley | 56a583e | 2020-05-08 15:12:17 +1000 | [diff] [blame] | 8631 | ALOGVV("TS_AFTER: %d %lld %lld", |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 8632 | id(), (long long)time, (long long)position); |
| 8633 | } |
| 8634 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8635 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 8636 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 8637 | // Note: In general record buffers should tend to be empty in |
| 8638 | // a properly running pipeline. |
| 8639 | // |
| 8640 | // Also, it is not advantageous to call get_presentation_position during the read |
| 8641 | // as the read obtains a lock, preventing the timestamp call from executing. |
Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 8642 | } else { |
| 8643 | mTimestampVerifier.error(); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8644 | } |
| 8645 | } |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 8646 | |
| 8647 | // From the timestamp, input read latency is negative output write latency. |
| 8648 | const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8649 | const double latencyMs = IAfRecordTrack::checkServerLatencySupported(mFormat, flags) |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 8650 | ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.; |
| 8651 | if (latencyMs != 0.) { // note 0. means timestamp is empty. |
| 8652 | mLatencyMs.add(latencyMs); |
| 8653 | } |
| 8654 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8655 | // Use this to track timestamp information |
| 8656 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 8657 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8658 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 8659 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8660 | // Force input into standby so that it tries to recover at next read attempt |
| 8661 | inputStandBy(); |
| 8662 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8663 | } |
| 8664 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8665 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8666 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8667 | ALOG_ASSERT(framesRead > 0); |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 8668 | mFramesRead += framesRead; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8669 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 8670 | #ifdef TEE_SINK |
| 8671 | (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
| 8672 | #endif |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8673 | // 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] | 8674 | { |
| 8675 | size_t part1 = mRsmpInFramesP2 - rear; |
| 8676 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 8677 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8678 | (framesRead - part1) * mFrameSize); |
| 8679 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8680 | } |
Dean Wheatley | fd56e81 | 2020-11-06 22:32:21 +1100 | [diff] [blame] | 8681 | mRsmpInRear = audio_utils::safe_add_overflow(mRsmpInRear, (int32_t)framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8682 | |
| 8683 | size = activeTracks.size(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8684 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8685 | // loop over each active track |
| 8686 | for (size_t i = 0; i < size; i++) { |
Andy Hung | 460e10f | 2024-06-17 15:42:48 -0700 | [diff] [blame] | 8687 | if (activeTrack) { // ensure track release is outside lock. |
| 8688 | oldActiveTracks.emplace_back(std::move(activeTrack)); |
| 8689 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8690 | activeTrack = activeTracks[i]; |
| 8691 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8692 | // skip fast tracks, as those are handled directly by FastCapture |
| 8693 | if (activeTrack->isFastTrack()) { |
| 8694 | continue; |
| 8695 | } |
| 8696 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8697 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 8698 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 8699 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8700 | enum { |
| 8701 | OVERRUN_UNKNOWN, |
| 8702 | OVERRUN_TRUE, |
| 8703 | OVERRUN_FALSE |
| 8704 | } overrun = OVERRUN_UNKNOWN; |
| 8705 | |
| 8706 | // loop over getNextBuffer to handle circular sink |
| 8707 | for (;;) { |
| 8708 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8709 | activeTrack->sinkBuffer().frameCount = ~0; |
| 8710 | status_t status = activeTrack->getNextBuffer(&activeTrack->sinkBuffer()); |
| 8711 | size_t framesOut = activeTrack->sinkBuffer().frameCount; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8712 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 8713 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8714 | // check available frames and handle overrun conditions |
| 8715 | // if the record track isn't draining fast enough. |
| 8716 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8717 | size_t framesIn; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8718 | activeTrack->resamplerBufferProvider()->sync(&framesIn, &hasOverrun); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 8719 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8720 | overrun = OVERRUN_TRUE; |
| 8721 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 8722 | if (framesOut == 0 || framesIn == 0) { |
| 8723 | break; |
| 8724 | } |
| 8725 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 8726 | // Don't allow framesOut to be larger than what is possible with resampling |
| 8727 | // from framesIn. |
| 8728 | // This isn't strictly necessary but helps limit buffer resizing in |
| 8729 | // RecordBufferConverter. TODO: remove when no longer needed. |
Dean Wheatley | dea650c | 2023-11-01 22:49:01 +1100 | [diff] [blame] | 8730 | if (audio_is_linear_pcm(activeTrack->format())) { |
| 8731 | framesOut = min(framesOut, |
| 8732 | destinationFramesPossible( |
| 8733 | framesIn, mSampleRate, activeTrack->sampleRate())); |
| 8734 | } |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8735 | |
| 8736 | if (activeTrack->isDirect()) { |
Daniel Van Veen | 9e2376e | 2018-09-27 14:37:58 +1000 | [diff] [blame] | 8737 | // No RecordBufferConverter used for direct streams. Pass |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8738 | // straight from RecordThread buffer to RecordTrack buffer. |
| 8739 | AudioBufferProvider::Buffer buffer; |
| 8740 | buffer.frameCount = framesOut; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8741 | const status_t getNextBufferStatus = |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8742 | activeTrack->resamplerBufferProvider()->getNextBuffer(&buffer); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8743 | if (getNextBufferStatus == OK && buffer.frameCount != 0) { |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8744 | ALOGV_IF(buffer.frameCount != framesOut, |
| 8745 | "%s() read less than expected (%zu vs %zu)", |
| 8746 | __func__, buffer.frameCount, framesOut); |
| 8747 | framesOut = buffer.frameCount; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8748 | memcpy(activeTrack->sinkBuffer().raw, |
| 8749 | buffer.raw, buffer.frameCount * mFrameSize); |
| 8750 | activeTrack->resamplerBufferProvider()->releaseBuffer(&buffer); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8751 | } else { |
| 8752 | framesOut = 0; |
| 8753 | ALOGE("%s() cannot fill request, status: %d, frameCount: %zu", |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 8754 | __func__, getNextBufferStatus, buffer.frameCount); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8755 | } |
| 8756 | } else { |
| 8757 | // process frames from the RecordThread buffer provider to the RecordTrack |
| 8758 | // buffer |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8759 | framesOut = activeTrack->recordBufferConverter()->convert( |
| 8760 | activeTrack->sinkBuffer().raw, |
| 8761 | activeTrack->resamplerBufferProvider(), |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 8762 | framesOut); |
| 8763 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8764 | |
| 8765 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 8766 | overrun = OVERRUN_FALSE; |
| 8767 | } |
| 8768 | |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 8769 | // MediaSyncEvent handling: Synchronize AudioRecord to AudioTrack completion. |
| 8770 | const ssize_t framesToDrop = |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8771 | activeTrack->synchronizedRecordState().updateRecordFrames(framesOut); |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 8772 | if (framesToDrop == 0) { |
| 8773 | // no sync event, process normally, otherwise ignore. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8774 | if (framesOut > 0) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8775 | activeTrack->sinkBuffer().frameCount = framesOut; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8776 | // Sanitize before releasing if the track has no access to the source data |
| 8777 | // An idle UID receives silence from non virtual devices until active |
| 8778 | if (activeTrack->isSilenced()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8779 | memset(activeTrack->sinkBuffer().raw, |
| 8780 | 0, framesOut * activeTrack->frameSize()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 8781 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8782 | activeTrack->releaseBuffer(&activeTrack->sinkBuffer()); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8783 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8784 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8785 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8786 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8787 | } |
| 8788 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8789 | |
| 8790 | switch (overrun) { |
| 8791 | case OVERRUN_TRUE: |
| 8792 | // client isn't retrieving buffers fast enough |
| 8793 | if (!activeTrack->setOverflow()) { |
| 8794 | nsecs_t now = systemTime(); |
| 8795 | // FIXME should lastWarning per track? |
| 8796 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 8797 | ALOGW("RecordThread: buffer overflow"); |
| 8798 | lastWarning = now; |
| 8799 | } |
| 8800 | } |
| 8801 | break; |
| 8802 | case OVERRUN_FALSE: |
| 8803 | activeTrack->clearOverflow(); |
| 8804 | break; |
| 8805 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 8806 | break; |
| 8807 | } |
| 8808 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8809 | // update frame information and push timestamp out |
| 8810 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8811 | activeTrack->serverProxy()->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 8812 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 8813 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 8814 | } |
| 8815 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 8816 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8817 | // enable changes in effect chain |
| 8818 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 8819 | // 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] | 8820 | if (audio_has_proportional_frames(mFormat) |
| 8821 | && loopCount == lastLoopCountRead + 1) { |
| 8822 | const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs; |
| 8823 | const double jitterMs = |
| 8824 | TimestampVerifier<int64_t, int64_t>::computeJitterMs( |
| 8825 | {framesRead, readPeriodNs}, |
| 8826 | {0, 0} /* lastTimestamp */, mSampleRate); |
| 8827 | const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6; |
| 8828 | |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8829 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8830 | mIoJitterMs.add(jitterMs); |
| 8831 | mProcessTimeMs.add(processMs); |
| 8832 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 8833 | mThreadloopExecutor.process(); |
Eric Laurent | cccbc76 | 2019-04-05 14:20:05 -0700 | [diff] [blame] | 8834 | // update timing info. |
| 8835 | mLastIoBeginNs = lastIoBeginNs; |
| 8836 | mLastIoEndNs = lastIoEndNs; |
| 8837 | lastLoopCountRead = loopCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8838 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 8839 | mThreadloopExecutor.process(); // process any remaining deferred actions. |
| 8840 | // deferred actions after this point are ignored. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8841 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 8842 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8843 | |
| 8844 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 8845 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 8846 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8847 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 8848 | track->invalidate(); |
| 8849 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 8850 | mActiveTracks.clear(); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8851 | mStartStopCV.notify_all(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8852 | } |
| 8853 | |
| 8854 | releaseWakeLock(); |
| 8855 | |
| 8856 | ALOGV("RecordThread %p exiting", this); |
| 8857 | return false; |
| 8858 | } |
| 8859 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8860 | void RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8861 | { |
| 8862 | if (!mStandby) { |
| 8863 | inputStandBy(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 8864 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 8865 | mThreadSnapshot.onEnd(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8866 | mStandby = true; |
| 8867 | } |
| 8868 | } |
| 8869 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8870 | void RecordThread::inputStandBy() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8871 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8872 | // Idle the fast capture if it's currently running |
| 8873 | if (mFastCapture != 0) { |
| 8874 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 8875 | FastCaptureState *state = sq->begin(); |
| 8876 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 8877 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 8878 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 8879 | state->mColdGen++; |
| 8880 | mFastCaptureFutex = 0; |
| 8881 | sq->end(); |
| 8882 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 8883 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 8884 | #if 0 |
| 8885 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 8886 | // FIXME |
| 8887 | } |
| 8888 | #endif |
| 8889 | #ifdef AUDIO_WATCHDOG |
| 8890 | // FIXME |
| 8891 | #endif |
| 8892 | } else { |
| 8893 | sq->end(false /*didModify*/); |
| 8894 | } |
| 8895 | } |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 8896 | status_t result = mSource->standby(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 8897 | 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] | 8898 | |
| 8899 | // If going into standby, flush the pipe source. |
| 8900 | if (mPipeSource.get() != nullptr) { |
| 8901 | const ssize_t flushed = mPipeSource->flush(); |
| 8902 | if (flushed > 0) { |
| 8903 | ALOGV("Input standby flushed PipeSource %zd frames", flushed); |
| 8904 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed; |
| 8905 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 8906 | } |
| 8907 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8908 | } |
| 8909 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 8910 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 8911 | sp<IAfRecordTrack> RecordThread::createRecordTrack_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 8912 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 8913 | const audio_attributes_t& attr, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8914 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8915 | audio_format_t format, |
| 8916 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 8917 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 8918 | audio_session_t sessionId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8919 | size_t *pNotificationFrameCount, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 8920 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 8921 | const AttributionSourceState& attributionSource, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8922 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8923 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 8924 | status_t *status, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8925 | audio_port_handle_t portId, |
| 8926 | int32_t maxSharedAudioHistoryMs) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8927 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 8928 | size_t frameCount = *pFrameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8929 | size_t notificationFrameCount = *pNotificationFrameCount; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 8930 | sp<IAfRecordTrack> track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8931 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8932 | audio_input_flags_t inputFlags = mInput->flags; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8933 | audio_input_flags_t requestedFlags = *flags; |
| 8934 | uint32_t sampleRate; |
| 8935 | |
| 8936 | lStatus = initCheck(); |
| 8937 | if (lStatus != NO_ERROR) { |
| 8938 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 8939 | goto Exit; |
| 8940 | } |
| 8941 | |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 8942 | if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) { |
| 8943 | ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT"); |
| 8944 | lStatus = BAD_VALUE; |
| 8945 | goto Exit; |
| 8946 | } |
| 8947 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8948 | if (maxSharedAudioHistoryMs != 0) { |
Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 8949 | if (!captureHotwordAllowed(attributionSource)) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8950 | lStatus = PERMISSION_DENIED; |
| 8951 | goto Exit; |
| 8952 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8953 | if (maxSharedAudioHistoryMs < 0 |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 8954 | || maxSharedAudioHistoryMs > kMaxSharedAudioHistoryMs) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 8955 | lStatus = BAD_VALUE; |
| 8956 | goto Exit; |
| 8957 | } |
| 8958 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8959 | if (*pSampleRate == 0) { |
| 8960 | *pSampleRate = mSampleRate; |
| 8961 | } |
| 8962 | sampleRate = *pSampleRate; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8963 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8964 | // special case for FAST flag considered OK if fast capture is present and access to |
| 8965 | // audio history is not required |
| 8966 | if (hasFastCapture() && mMaxSharedAudioHistoryMs == 0) { |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8967 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 8968 | } |
| 8969 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 8970 | // Check if requested flags are compatible with input stream flags |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 8971 | if ((*flags & inputFlags) != *flags) { |
| 8972 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 8973 | " input flags (%08x)", |
| 8974 | *flags, inputFlags); |
| 8975 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 8976 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 8977 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 8978 | // client expresses a preference for FAST and no access to audio history, |
| 8979 | // but we get the final say |
| 8980 | if (*flags & AUDIO_INPUT_FLAG_FAST && maxSharedAudioHistoryMs == 0) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 8981 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 8982 | // we formerly checked for a callback handler (non-0 tid), |
| 8983 | // but that is no longer required for TRANSFER_OBTAIN mode |
jiabin | b00edc3 | 2021-08-16 16:27:54 +0000 | [diff] [blame] | 8984 | // No need to match hardware format, format conversion will be done in client side. |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 8985 | // |
Phil Burk | 7ed66a1 | 2019-04-18 13:20:30 -0700 | [diff] [blame] | 8986 | // Frame count is not specified (0), or is less than or equal the pipe depth. |
| 8987 | // It is OK to provide a higher capacity than requested. |
| 8988 | // We will force it to mPipeFramesP2 below. |
| 8989 | (frameCount <= mPipeFramesP2) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 8990 | // PCM data |
| 8991 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 8992 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8993 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 8994 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 8995 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 8996 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 8997 | hasFastCapture() && |
| 8998 | // there are sufficient fast track slots available |
| 8999 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9000 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9001 | // check compatibility with audio effects. |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9002 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9003 | // Do not accept FAST flag if the session has software effects |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 9004 | sp<IAfEffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9005 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 9006 | audio_input_flags_t old = *flags; |
| 9007 | chain->checkInputFlagCompatibility(flags); |
| 9008 | if (old != *flags) { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9009 | ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 9010 | this, (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 9011 | } |
| 9012 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 9013 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9014 | "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 9015 | this, frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9016 | } else { |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9017 | ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
| 9018 | "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9019 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Mikhail Naganov | b3cf7e6 | 2017-06-02 10:24:24 -0700 | [diff] [blame] | 9020 | this, frameCount, mFrameCount, mPipeFramesP2, |
| 9021 | format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate, |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9022 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9023 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9024 | } |
| 9025 | } |
| 9026 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9027 | // If FAST or RAW flags were corrected, ask caller to request new input from audio policy |
| 9028 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != |
| 9029 | (requestedFlags & AUDIO_INPUT_FLAG_FAST)) { |
| 9030 | *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW)); |
| 9031 | lStatus = BAD_TYPE; |
| 9032 | goto Exit; |
| 9033 | } |
| 9034 | |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9035 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9036 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9037 | // fast track: frame count is exactly the pipe depth |
| 9038 | frameCount = mPipeFramesP2; |
| 9039 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9040 | notificationFrameCount = mFrameCount; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9041 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 9042 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 9043 | // or 20 ms if there is a fast capture |
| 9044 | // TODO This could be a roundupRatio inline, and const |
| 9045 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 9046 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 9047 | // minimum number of notification periods is at least kMinNotifications, |
| 9048 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 9049 | static const size_t kMinNotifications = 3; |
| 9050 | static const uint32_t kMinMs = 30; |
| 9051 | // TODO This could be a roundupRatio inline |
| 9052 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 9053 | // TODO This could be a roundupRatio inline |
| 9054 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 9055 | maxNotificationFrames; |
| 9056 | const size_t minFrameCount = maxNotificationFrames * |
| 9057 | max(kMinNotifications, minNotificationsByMs); |
| 9058 | frameCount = max(frameCount, minFrameCount); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9059 | if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) { |
| 9060 | notificationFrameCount = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 9061 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9062 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 9063 | *pFrameCount = frameCount; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 9064 | *pNotificationFrameCount = notificationFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9065 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9066 | { // scope for mutex() |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9067 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9068 | int32_t startFrames = -1; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9069 | if (!mSharedAudioPackageName.empty() |
Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9070 | && mSharedAudioPackageName == attributionSource.packageName |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9071 | && mSharedAudioSessionId == sessionId |
Eric Laurent | 9ff3e53 | 2022-11-10 16:04:44 +0100 | [diff] [blame] | 9072 | && captureHotwordAllowed(attributionSource)) { |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9073 | startFrames = mSharedAudioStartFrames; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9074 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9075 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9076 | track = IAfRecordTrack::create(this, client, attr, sampleRate, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 9077 | format, channelMask, frameCount, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 9078 | nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9079 | attributionSource, *flags, IAfTrackBase::TYPE_DEFAULT, portId, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 9080 | startFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9081 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 9082 | lStatus = track->initCheck(); |
| 9083 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 9084 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 9085 | // 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] | 9086 | goto Exit; |
| 9087 | } |
| 9088 | mTracks.add(track); |
| 9089 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 9090 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9091 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 9092 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 9093 | // so ask activity manager to do this on our behalf |
Glenn Kasten | af9a7b5 | 2017-03-29 11:29:39 -0700 | [diff] [blame] | 9094 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 9095 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9096 | |
| 9097 | if (maxSharedAudioHistoryMs != 0) { |
| 9098 | sendResizeBufferConfigEvent_l(maxSharedAudioHistoryMs); |
| 9099 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9100 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 9101 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9102 | lStatus = NO_ERROR; |
| 9103 | |
| 9104 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 9105 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9106 | return track; |
| 9107 | } |
| 9108 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9109 | status_t RecordThread::start(IAfRecordTrack* recordTrack, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9110 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9111 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9112 | { |
| 9113 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 9114 | sp<ThreadBase> strongMe = this; |
| 9115 | status_t status = NO_ERROR; |
| 9116 | |
| 9117 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 9118 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9119 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9120 | recordTrack->synchronizedRecordState().startRecording( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 9121 | mAfThreadCallback->createSyncEvent( |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 9122 | event, triggerSession, |
| 9123 | recordTrack->sessionId(), syncStartEventCallback, recordTrack)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9124 | } |
| 9125 | |
| 9126 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 9127 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9128 | audio_utils::lock_guard lock(mutex()); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9129 | if (recordTrack->isInvalid()) { |
| 9130 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 9131 | ALOGW("%s track %d: invalidated before startInput", __func__, recordTrack->portId()); |
| 9132 | return DEAD_OBJECT; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9133 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9134 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9135 | if (recordTrack->state() == IAfTrackBase::PAUSING) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9136 | // We haven't stopped yet (moved to PAUSED and not in mActiveTracks) |
| 9137 | // so no need to startInput(). |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9138 | ALOGV("active record track PAUSING -> ACTIVE"); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9139 | recordTrack->setState(IAfTrackBase::ACTIVE); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9140 | } else { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9141 | ALOGV("active record track state %d", (int)recordTrack->state()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9142 | } |
| 9143 | return status; |
| 9144 | } |
| 9145 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 9146 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 9147 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 9148 | // or using a separate command thread |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9149 | recordTrack->setState(IAfTrackBase::STARTING_1); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9150 | mActiveTracks.add(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9151 | if (recordTrack->isExternalTrack()) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9152 | mutex().unlock(); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 9153 | status = AudioSystem::startInput(recordTrack->portId()); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9154 | mutex().lock(); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9155 | if (recordTrack->isInvalid()) { |
| 9156 | recordTrack->clearSyncStartEvent(); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9157 | if (status == NO_ERROR && recordTrack->state() == IAfTrackBase::STARTING_1) { |
| 9158 | recordTrack->setState(IAfTrackBase::STARTING_2); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9159 | // STARTING_2 forces destroy to call stopInput. |
| 9160 | } |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 9161 | ALOGW("%s track %d: invalidated after startInput", __func__, recordTrack->portId()); |
| 9162 | return DEAD_OBJECT; |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9163 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9164 | if (recordTrack->state() != IAfTrackBase::STARTING_1) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9165 | ALOGW("%s(%d): unsynchronized mState:%d change", |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9166 | __func__, recordTrack->id(), (int)recordTrack->state()); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9167 | // Someone else has changed state, let them take over, |
| 9168 | // leave mState in the new state. |
| 9169 | recordTrack->clearSyncStartEvent(); |
| 9170 | return INVALID_OPERATION; |
| 9171 | } |
| 9172 | // we're ok, but perhaps startInput has failed |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9173 | if (status != NO_ERROR) { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9174 | ALOGW("%s(%d): startInput failed, status %d", |
| 9175 | __func__, recordTrack->id(), status); |
| 9176 | // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks, |
| 9177 | // leave in STARTING_1, so destroy() will not call stopInput. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9178 | mActiveTracks.remove(recordTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9179 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 9180 | return status; |
| 9181 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9182 | sendIoConfigEvent_l( |
| 9183 | AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9184 | } |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9185 | |
| 9186 | recordTrack->logBeginInterval(patchSourcesToString(&mPatch)); // log to MediaMetrics |
| 9187 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9188 | // Catch up with current buffer indices if thread is already running. |
| 9189 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 9190 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 9191 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 9192 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9193 | recordTrack->resamplerBufferProvider()->reset(); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 9194 | if (!recordTrack->isDirect()) { |
| 9195 | // clear any converter state as new data will be discontinuous |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9196 | recordTrack->recordBufferConverter()->reset(); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 9197 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9198 | recordTrack->setState(IAfTrackBase::STARTING_2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9199 | // signal thread to start |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9200 | mWaitWorkCV.notify_all(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9201 | return status; |
| 9202 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9203 | } |
| 9204 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9205 | void RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9206 | { |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9207 | const sp<SyncEvent> strongEvent = event.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9208 | |
| 9209 | if (strongEvent != 0) { |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 9210 | sp<IAfTrackBase> ptr = |
| 9211 | std::any_cast<const wp<IAfTrackBase>>(strongEvent->cookie()).promote(); |
| 9212 | if (ptr != nullptr) { |
Andy Hung | eb6b5f8 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 9213 | // TODO(b/291317898) handleSyncStartEvent is in IAfTrackBase not IAfRecordTrack. |
Andy Hung | fafbebc | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 9214 | ptr->handleSyncStartEvent(strongEvent); |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 9215 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9216 | } |
| 9217 | } |
| 9218 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9219 | bool RecordThread::stop(IAfRecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9220 | ALOGV("RecordThread::stop"); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9221 | audio_utils::unique_lock _l(mutex()); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9222 | // if we're invalid, we can't be on the ActiveTracks. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9223 | if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->state() == IAfTrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9224 | return false; |
| 9225 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 9226 | // note that threadLoop may still be processing the track at this point [without lock] |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9227 | recordTrack->setState(IAfTrackBase::PAUSING); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9228 | |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 9229 | // NOTE: Waiting here is important to keep stop synchronous. |
| 9230 | // This is needed for proper patchRecord peer release. |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9231 | while (recordTrack->state() == IAfTrackBase::PAUSING && !recordTrack->isInvalid()) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9232 | mWaitWorkCV.notify_all(); // signal thread to stop |
Andy Hung | 77b1bb4 | 2024-05-06 12:16:36 -0700 | [diff] [blame] | 9233 | mStartStopCV.wait(_l, getTid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9234 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9235 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9236 | if (recordTrack->state() == IAfTrackBase::PAUSED) { // successful stop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9237 | ALOGV("Record stopped OK"); |
| 9238 | return true; |
| 9239 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 9240 | |
| 9241 | // don't handle anything - we've been invalidated or restarted and in a different state |
| 9242 | ALOGW_IF("%s(%d): unsynchronized stop, state: %d", |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9243 | __func__, recordTrack->id(), recordTrack->state()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9244 | return false; |
| 9245 | } |
| 9246 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9247 | bool RecordThread::isValidSyncEvent(const sp<SyncEvent>& /* event */) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9248 | { |
| 9249 | return false; |
| 9250 | } |
| 9251 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9252 | status_t RecordThread::setSyncEvent(const sp<SyncEvent>& /* event */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9253 | { |
| 9254 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 9255 | if (!isValidSyncEvent(event)) { |
| 9256 | return BAD_VALUE; |
| 9257 | } |
| 9258 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9259 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9260 | status_t ret = NAME_NOT_FOUND; |
| 9261 | |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9262 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9263 | |
| 9264 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9265 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9266 | if (eventSession == track->sessionId()) { |
| 9267 | (void) track->setSyncEvent(event); |
| 9268 | ret = NO_ERROR; |
| 9269 | } |
| 9270 | } |
| 9271 | return ret; |
| 9272 | #else |
| 9273 | return BAD_VALUE; |
| 9274 | #endif |
| 9275 | } |
| 9276 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9277 | status_t RecordThread::getActiveMicrophones( |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9278 | std::vector<media::MicrophoneInfoFw>* activeMicrophones) const |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 9279 | { |
| 9280 | ALOGV("RecordThread::getActiveMicrophones"); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9281 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9282 | if (!isStreamInitialized()) { |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9283 | return NO_INIT; |
| 9284 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 9285 | status_t status = mInput->stream->getActiveMicrophones(activeMicrophones); |
| 9286 | return status; |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 9287 | } |
| 9288 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9289 | status_t RecordThread::setPreferredMicrophoneDirection( |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9290 | audio_microphone_direction_t direction) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9291 | { |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9292 | ALOGV("setPreferredMicrophoneDirection(%d)", direction); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9293 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9294 | if (!isStreamInitialized()) { |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9295 | return NO_INIT; |
| 9296 | } |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9297 | return mInput->stream->setPreferredMicrophoneDirection(direction); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9298 | } |
| 9299 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9300 | status_t RecordThread::setPreferredMicrophoneFieldDimension(float zoom) |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9301 | { |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9302 | ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom); |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9303 | audio_utils::lock_guard _l(mutex()); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9304 | if (!isStreamInitialized()) { |
Paul McLean | 8a661a3 | 2021-04-12 10:21:42 -0600 | [diff] [blame] | 9305 | return NO_INIT; |
| 9306 | } |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 9307 | return mInput->stream->setPreferredMicrophoneFieldDimension(zoom); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 9308 | } |
| 9309 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9310 | status_t RecordThread::shareAudioHistory( |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9311 | const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, |
| 9312 | int64_t sharedAudioStartMs) { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9313 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9314 | return shareAudioHistory_l(sharedAudioPackageName, sharedSessionId, sharedAudioStartMs); |
| 9315 | } |
| 9316 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9317 | status_t RecordThread::shareAudioHistory_l( |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9318 | const std::string& sharedAudioPackageName, audio_session_t sharedSessionId, |
| 9319 | int64_t sharedAudioStartMs) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9320 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9321 | if ((hasAudioSession_l(sharedSessionId) & ThreadBase::TRACK_SESSION) == 0) { |
| 9322 | return BAD_VALUE; |
| 9323 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9324 | |
| 9325 | if (sharedAudioStartMs < 0 |
| 9326 | || sharedAudioStartMs > INT64_MAX / mSampleRate) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9327 | return BAD_VALUE; |
| 9328 | } |
| 9329 | |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9330 | // Current implementation of the input resampling buffer wraps around indexes at 32 bit. |
| 9331 | // As we cannot detect more than one wraparound, only accept values up current write position |
| 9332 | // after one wraparound |
| 9333 | // We assume recent wraparounds on mRsmpInRear only given it is unlikely that the requesting |
| 9334 | // app waits several hours after the start time was computed. |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9335 | int64_t sharedAudioStartFrames = sharedAudioStartMs * mSampleRate / 1000; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9336 | const int32_t sharedOffset = audio_utils::safe_sub_overflow(mRsmpInRear, |
| 9337 | (int32_t)sharedAudioStartFrames); |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9338 | // Bring the start frame position within the input buffer to match the documented |
| 9339 | // "best effort" behavior of the API. |
| 9340 | if (sharedOffset < 0) { |
| 9341 | sharedAudioStartFrames = mRsmpInRear; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9342 | } else if (sharedOffset > static_cast<signed>(mRsmpInFrames)) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9343 | sharedAudioStartFrames = |
| 9344 | audio_utils::safe_sub_overflow(mRsmpInRear, (int32_t)mRsmpInFrames); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9345 | } |
| 9346 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9347 | mSharedAudioPackageName = sharedAudioPackageName; |
| 9348 | if (mSharedAudioPackageName.empty()) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9349 | resetAudioHistory_l(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9350 | } else { |
| 9351 | mSharedAudioSessionId = sharedSessionId; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9352 | mSharedAudioStartFrames = (int32_t)sharedAudioStartFrames; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9353 | } |
| 9354 | return NO_ERROR; |
| 9355 | } |
| 9356 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9357 | void RecordThread::resetAudioHistory_l() { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9358 | mSharedAudioSessionId = AUDIO_SESSION_NONE; |
| 9359 | mSharedAudioStartFrames = -1; |
| 9360 | mSharedAudioPackageName = ""; |
| 9361 | } |
| 9362 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9363 | ThreadBase::MetadataUpdate RecordThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9364 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 9365 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 9366 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9367 | } |
| 9368 | StreamInHalInterface::SinkMetadata metadata; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 9369 | auto backInserter = std::back_inserter(metadata.tracks); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9370 | for (const sp<IAfRecordTrack>& track : mActiveTracks) { |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 9371 | track->copyMetadataTo(backInserter); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9372 | } |
| 9373 | mInput->stream->updateSinkMetadata(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 9374 | MetadataUpdate change; |
| 9375 | change.recordMetadataUpdate = metadata.tracks; |
| 9376 | return change; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 9377 | } |
| 9378 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9379 | // destroyTrack_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9380 | void RecordThread::destroyTrack_l(const sp<IAfRecordTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9381 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 9382 | track->terminate(); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9383 | track->setState(IAfTrackBase::STOPPED); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9384 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9385 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9386 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9387 | removeTrack_l(track); |
| 9388 | } |
| 9389 | } |
| 9390 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9391 | void RecordThread::removeTrack_l(const sp<IAfRecordTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9392 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9393 | String8 result; |
| 9394 | track->appendDump(result, false /* active */); |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9395 | mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.c_str()); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9396 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9397 | mTracks.remove(track); |
| 9398 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9399 | if (track->isFastTrack()) { |
| 9400 | ALOG_ASSERT(!mFastTrackAvail); |
| 9401 | mFastTrackAvail = true; |
| 9402 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9403 | } |
| 9404 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9405 | void RecordThread::dumpInternals_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9406 | { |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 9407 | AudioStreamIn *input = mInput; |
| 9408 | audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE; |
| 9409 | dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n", |
Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 9410 | input, flags, toString(flags).c_str()); |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 9411 | dprintf(fd, " Frames read: %lld\n", (long long)mFramesRead); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 9412 | if (mActiveTracks.isEmpty()) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 9413 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9414 | } |
Andy Hung | bfa6496 | 2017-06-12 14:43:19 -0700 | [diff] [blame] | 9415 | |
| 9416 | if (input != nullptr) { |
| 9417 | dprintf(fd, " Hal stream dump:\n"); |
| 9418 | (void)input->stream->dump(fd); |
| 9419 | } |
| 9420 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 9421 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 9422 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 9423 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 9424 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 9425 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 9426 | // This is a large object so we place it on the heap. |
| 9427 | // 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] | 9428 | const std::unique_ptr<FastCaptureDumpState> copy = |
| 9429 | std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState); |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 9430 | copy->dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9431 | } |
| 9432 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9433 | void RecordThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9434 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9435 | String8 result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9436 | size_t numtracks = mTracks.size(); |
| 9437 | size_t numactive = mActiveTracks.size(); |
| 9438 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 9439 | dprintf(fd, " %zu Tracks", numtracks); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9440 | const char *prefix = " "; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9441 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 9442 | dprintf(fd, " of which %zu are active\n", numactive); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9443 | result.append(prefix); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 9444 | mTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9445 | for (size_t i = 0; i < numtracks ; ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9446 | sp<IAfRecordTrack> track = mTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9447 | if (track != 0) { |
| 9448 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 9449 | if (active) { |
| 9450 | numactiveseen++; |
| 9451 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9452 | result.append(prefix); |
| 9453 | track->appendDump(result, active); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9454 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9455 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9456 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 9457 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9458 | } |
| 9459 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9460 | if (numactiveseen != numactive) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9461 | result.append(" The following tracks are in the active list but" |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9462 | " not in the track list\n"); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9463 | result.append(prefix); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 9464 | mActiveTracks[0]->appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9465 | for (size_t i = 0; i < numactive; ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9466 | sp<IAfRecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9467 | if (mTracks.indexOf(track) < 0) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 9468 | result.append(prefix); |
| 9469 | track->appendDump(result, true /* active */); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 9470 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 9471 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9472 | |
| 9473 | } |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 9474 | write(fd, result.c_str(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9475 | } |
| 9476 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9477 | void RecordThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9478 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9479 | audio_utils::lock_guard _l(mutex()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9480 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9481 | sp<IAfRecordTrack> track = mTracks[i]; |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 9482 | if (track != 0 && track->portId() == portId) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 9483 | track->setSilenced(silenced); |
| 9484 | } |
| 9485 | } |
| 9486 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9487 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9488 | void ResamplerBufferProvider::reset() |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9489 | { |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9490 | const auto threadBase = mRecordTrack->thread().promote(); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9491 | auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9492 | mRsmpInUnrel = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9493 | const int32_t rear = recordThread->mRsmpInRear; |
| 9494 | ssize_t deltaFrames = 0; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9495 | if (mRecordTrack->startFrames() >= 0) { |
| 9496 | int32_t startFrames = mRecordTrack->startFrames(); |
| 9497 | // Accept a recent wraparound of mRsmpInRear |
| 9498 | if (startFrames <= rear) { |
| 9499 | deltaFrames = rear - startFrames; |
| 9500 | } else { |
| 9501 | deltaFrames = (int32_t)((int64_t)rear + UINT32_MAX + 1 - startFrames); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9502 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9503 | // start frame cannot be further in the past than start of resampling buffer |
| 9504 | if ((size_t) deltaFrames > recordThread->mRsmpInFrames) { |
| 9505 | deltaFrames = recordThread->mRsmpInFrames; |
| 9506 | } |
| 9507 | } |
| 9508 | mRsmpInFront = audio_utils::safe_sub_overflow(rear, static_cast<int32_t>(deltaFrames)); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9509 | } |
| 9510 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9511 | void ResamplerBufferProvider::sync( |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9512 | size_t *framesAvailable, bool *hasOverrun) |
| 9513 | { |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9514 | const auto threadBase = mRecordTrack->thread().promote(); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9515 | auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9516 | const int32_t rear = recordThread->mRsmpInRear; |
| 9517 | const int32_t front = mRsmpInFront; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9518 | const ssize_t filled = audio_utils::safe_sub_overflow(rear, front); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9519 | |
| 9520 | size_t framesIn; |
| 9521 | bool overrun = false; |
| 9522 | if (filled < 0) { |
| 9523 | // should not happen, but treat like a massive overrun and re-sync |
| 9524 | framesIn = 0; |
| 9525 | mRsmpInFront = rear; |
| 9526 | overrun = true; |
| 9527 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 9528 | framesIn = (size_t) filled; |
| 9529 | } else { |
| 9530 | // client is not keeping up with server, but give it latest data |
| 9531 | framesIn = recordThread->mRsmpInFrames; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9532 | mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow( |
| 9533 | rear, static_cast<int32_t>(framesIn)); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9534 | overrun = true; |
| 9535 | } |
| 9536 | if (framesAvailable != NULL) { |
| 9537 | *framesAvailable = framesIn; |
| 9538 | } |
| 9539 | if (hasOverrun != NULL) { |
| 9540 | *hasOverrun = overrun; |
| 9541 | } |
| 9542 | } |
| 9543 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9544 | // AudioBufferProvider interface |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9545 | status_t ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 9546 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9547 | { |
Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 9548 | const auto threadBase = mRecordTrack->thread().promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9549 | if (threadBase == 0) { |
| 9550 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 9551 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9552 | return NOT_ENOUGH_DATA; |
| 9553 | } |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9554 | auto* const recordThread = static_cast<RecordThread *>(threadBase->asIAfRecordThread().get()); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9555 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9556 | int32_t front = mRsmpInFront; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9557 | ssize_t filled = audio_utils::safe_sub_overflow(rear, front); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9558 | // FIXME should not be P2 (don't want to increase latency) |
| 9559 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 9560 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9561 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9562 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9563 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 9564 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9565 | if (part1 > (size_t) filled) { |
| 9566 | part1 = filled; |
| 9567 | } |
| 9568 | size_t ask = buffer->frameCount; |
| 9569 | ALOG_ASSERT(ask > 0); |
| 9570 | if (part1 > ask) { |
| 9571 | part1 = ask; |
| 9572 | } |
| 9573 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9574 | // 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] | 9575 | buffer->raw = NULL; |
| 9576 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9577 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9578 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9579 | } |
| 9580 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 9581 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9582 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9583 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9584 | return NO_ERROR; |
| 9585 | } |
| 9586 | |
| 9587 | // AudioBufferProvider interface |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9588 | void ResamplerBufferProvider::releaseBuffer( |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 9589 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9590 | { |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9591 | int32_t stepCount = static_cast<int32_t>(buffer->frameCount); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9592 | if (stepCount == 0) { |
| 9593 | return; |
| 9594 | } |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 9595 | ALOG_ASSERT(stepCount <= (int32_t)mRsmpInUnrel); |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 9596 | mRsmpInUnrel -= stepCount; |
Hongwei Wang | 95e3768 | 2019-04-12 11:13:36 -0700 | [diff] [blame] | 9597 | mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 9598 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9599 | buffer->frameCount = 0; |
| 9600 | } |
| 9601 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9602 | void RecordThread::checkBtNrec() |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9603 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9604 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9605 | checkBtNrec_l(); |
| 9606 | } |
| 9607 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9608 | void RecordThread::checkBtNrec_l() |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9609 | { |
| 9610 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 9611 | // pre processings |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9612 | bool suspend = audio_is_bluetooth_sco_device(inDeviceType_l()) && |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 9613 | mAfThreadCallback->btNrecIsOff(); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9614 | if (mBtNrecSuspended.exchange(suspend) != suspend) { |
| 9615 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 9616 | setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId()); |
| 9617 | setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId()); |
| 9618 | } |
| 9619 | } |
| 9620 | } |
| 9621 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 9622 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9623 | bool RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9624 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9625 | { |
| 9626 | bool reconfig = false; |
| 9627 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9628 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9629 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9630 | audio_format_t reqFormat = mFormat; |
| 9631 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 9632 | // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs). |
Jing Mike | 537412f | 2023-03-12 11:01:47 +0800 | [diff] [blame] | 9633 | [[maybe_unused]] audio_channel_mask_t channelMask = |
| 9634 | audio_channel_in_mask_from_count(mChannelCount); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9635 | |
| 9636 | AudioParameter param = AudioParameter(keyValuePair); |
| 9637 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 9638 | |
| 9639 | // scope for AutoPark extends to end of method |
| 9640 | AutoPark<FastCapture> park(mFastCapture); |
| 9641 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9642 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 9643 | // channel count change can be requested. Do we mandate the first client defines the |
| 9644 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 9645 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 9646 | samplingRate = value; |
| 9647 | reconfig = true; |
| 9648 | } |
| 9649 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 9650 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9651 | status = BAD_VALUE; |
| 9652 | } else { |
| 9653 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9654 | reconfig = true; |
| 9655 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9656 | } |
| 9657 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 9658 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 9659 | if (!audio_is_input_channel(mask) || |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9660 | audio_channel_count_from_in_mask(mask) > FCC_LIMIT) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9661 | status = BAD_VALUE; |
| 9662 | } else { |
| 9663 | channelMask = mask; |
| 9664 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9665 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9666 | } |
| 9667 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 9668 | // do not accept frame count changes if tracks are open as the track buffer |
| 9669 | // size depends on frame count and correct behavior would not be guaranteed |
| 9670 | // if frame count is changed after track creation |
| 9671 | if (mActiveTracks.size() > 0) { |
| 9672 | status = INVALID_OPERATION; |
| 9673 | } else { |
| 9674 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9675 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9676 | } |
| 9677 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9678 | LOG_FATAL("Should not set routing device in RecordThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9679 | } |
| 9680 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 9681 | mAudioSource != (audio_source_t)value) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9682 | LOG_FATAL("Should not set audio source in RecordThread"); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9683 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 9684 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9685 | if (status == NO_ERROR) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9686 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9687 | if (status == INVALID_OPERATION) { |
| 9688 | inputStandBy(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9689 | status = mInput->stream->setParameters(keyValuePair); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9690 | } |
| 9691 | if (reconfig) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9692 | if (status == BAD_VALUE) { |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 9693 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 9694 | if (mInput->stream->getAudioProperties(&config) == OK && |
| 9695 | audio_is_linear_pcm(config.format) && audio_is_linear_pcm(reqFormat) && |
| 9696 | config.sample_rate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) && |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9697 | audio_channel_count_from_in_mask(config.channel_mask) <= FCC_LIMIT) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9698 | status = NO_ERROR; |
| 9699 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9700 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9701 | if (status == NO_ERROR) { |
| 9702 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9703 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9704 | } |
| 9705 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9706 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 9707 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9708 | return reconfig; |
| 9709 | } |
| 9710 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9711 | String8 RecordThread::getParameters(const String8& keys) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9712 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9713 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9714 | if (initCheck() == NO_ERROR) { |
| 9715 | String8 out_s8; |
| 9716 | if (mInput->stream->getParameters(keys, &out_s8) == OK) { |
| 9717 | return out_s8; |
| 9718 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9719 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9720 | return {}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9721 | } |
| 9722 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9723 | void RecordThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9724 | audio_port_handle_t portId) { |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9725 | sp<AudioIoDescriptor> desc; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9726 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9727 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 9728 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9729 | case AUDIO_INPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9730 | desc = sp<AudioIoDescriptor>::make(mId, mPatch, true /*isInput*/, |
| 9731 | mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9732 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9733 | case AUDIO_CLIENT_STARTED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9734 | desc = sp<AudioIoDescriptor>::make(mId, mPatch, portId); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 9735 | break; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 9736 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9737 | default: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 9738 | desc = sp<AudioIoDescriptor>::make(mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9739 | break; |
| 9740 | } |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 9741 | mAfThreadCallback->ioConfigChanged_l(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9742 | } |
| 9743 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9744 | void RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9745 | { |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9746 | const audio_config_base_t audioConfig = mInput->getAudioProperties(); |
| 9747 | mSampleRate = audioConfig.sample_rate; |
| 9748 | mChannelMask = audioConfig.channel_mask; |
| 9749 | if (!audio_is_input_channel(mChannelMask)) { |
| 9750 | LOG_ALWAYS_FATAL("Channel mask %#x not valid for input", mChannelMask); |
| 9751 | } |
| 9752 | |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9753 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9754 | |
| 9755 | // Get actual HAL format. |
| 9756 | status_t result = mInput->stream->getAudioProperties(nullptr, nullptr, &mHALFormat); |
| 9757 | LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving input stream format: %d", result); |
| 9758 | // Get format from the shim, which will be different than the HAL format |
| 9759 | // if recording compressed audio from IEC61937 wrapped sources. |
| 9760 | mFormat = audioConfig.format; |
| 9761 | if (!audio_is_valid_format(mFormat)) { |
| 9762 | LOG_ALWAYS_FATAL("Format %#x not valid for input", mFormat); |
| 9763 | } |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9764 | if (audio_is_linear_pcm(mFormat)) { |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9765 | LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_LIMIT, "HAL channel count %d > %d", |
| 9766 | mChannelCount, FCC_LIMIT); |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9767 | } else { |
Andy Hung | 936845a | 2021-06-08 00:09:06 -0700 | [diff] [blame] | 9768 | // Can have more that FCC_LIMIT channels in encoded streams. |
Mikhail Naganov | ce9f265 | 2018-07-16 11:09:24 -0700 | [diff] [blame] | 9769 | ALOGI("HAL format %#x is not linear pcm", mFormat); |
| 9770 | } |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 9771 | mFrameSize = mInput->getFrameSize(); |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9772 | LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", |
| 9773 | mFrameSize); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9774 | result = mInput->stream->getBufferSize(&mBufferSize); |
| 9775 | 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] | 9776 | mFrameCount = mBufferSize / mFrameSize; |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 9777 | ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, " |
| 9778 | "mBufferSize=%zu, mFrameCount=%zu", |
| 9779 | this, mChannelCount, mFormat, mFrameSize, mBufferSize, mFrameCount); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 9780 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9781 | // mRsmpInFrames must be 0 before calling resizeInputBuffer_l for the first time |
| 9782 | mRsmpInFrames = 0; |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 9783 | resizeInputBuffer_l(0 /*maxSharedAudioHistoryMs*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9784 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 9785 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 9786 | // 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] | 9787 | |
| 9788 | audio_input_flags_t flags = mInput->flags; |
| 9789 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); |
| 9790 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 9791 | .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9792 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 9793 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 9794 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 9795 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 9796 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 9797 | .record(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9798 | } |
| 9799 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9800 | uint32_t RecordThread::getInputFramesLost() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9801 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9802 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9803 | uint32_t result; |
| 9804 | if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) { |
| 9805 | return result; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9806 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9807 | return 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9808 | } |
| 9809 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9810 | KeyedVector<audio_session_t, bool> RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9811 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9812 | KeyedVector<audio_session_t, bool> ids; |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9813 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9814 | for (size_t j = 0; j < mTracks.size(); ++j) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9815 | sp<IAfRecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 9816 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9817 | if (ids.indexOfKey(sessionId) < 0) { |
| 9818 | ids.add(sessionId, true); |
| 9819 | } |
| 9820 | } |
| 9821 | return ids; |
| 9822 | } |
| 9823 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9824 | AudioStreamIn* RecordThread::clearInput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9825 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9826 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9827 | AudioStreamIn *input = mInput; |
| 9828 | mInput = NULL; |
Mikhail Naganov | 49aecf0 | 2023-09-08 15:14:34 -0700 | [diff] [blame] | 9829 | mInputSource.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9830 | return input; |
| 9831 | } |
| 9832 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 9833 | // this method must always be called either with ThreadBase mutex() held or inside the thread loop |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9834 | sp<StreamHalInterface> RecordThread::stream() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9835 | { |
| 9836 | if (mInput == NULL) { |
| 9837 | return NULL; |
| 9838 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 9839 | return mInput->stream; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9840 | } |
| 9841 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9842 | status_t RecordThread::addEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9843 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9844 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 9845 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9846 | chain->setInBuffer(NULL); |
| 9847 | chain->setOutBuffer(NULL); |
| 9848 | |
| 9849 | checkSuspendOnAddEffectChain_l(chain); |
| 9850 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 9851 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 9852 | // just moved them to a new input stream. |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 9853 | chain->syncHalEffectsState_l(); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 9854 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9855 | mEffectChains.add(chain); |
| 9856 | |
| 9857 | return NO_ERROR; |
| 9858 | } |
| 9859 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9860 | size_t RecordThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9861 | { |
| 9862 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 9863 | |
| 9864 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 9865 | if (chain == mEffectChains[i]) { |
| 9866 | mEffectChains.removeAt(i); |
| 9867 | break; |
| 9868 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9869 | } |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 9870 | return mEffectChains.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 9871 | } |
| 9872 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9873 | status_t RecordThread::createAudioPatch_l(const struct audio_patch* patch, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9874 | audio_patch_handle_t *handle) |
| 9875 | { |
| 9876 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9877 | |
| 9878 | // store new device and send to effects |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9879 | mInDeviceTypeAddr.mType = patch->sources[0].ext.device.type; |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 9880 | mInDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 9881 | audio_port_handle_t deviceId = patch->sources[0].id; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9882 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 9883 | mEffectChains[i]->setInputDevice_l(inDeviceTypeAddr()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9884 | } |
| 9885 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 9886 | checkBtNrec_l(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9887 | |
| 9888 | // store new source and send to effects |
| 9889 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 9890 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9891 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9892 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9893 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9894 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9895 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 9896 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 9897 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 9898 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 9899 | patch->sources, |
| 9900 | patch->num_sinks, |
| 9901 | patch->sinks, |
| 9902 | handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9903 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 9904 | status = mInput->stream->legacyCreateAudioPatch(patch->sources[0], |
| 9905 | patch->sinks[0].ext.mix.usecase.source, |
| 9906 | patch->sources[0].ext.device.type); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9907 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9908 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9909 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9910 | if ((mPatch.num_sources == 0) || (mPatch.sources[0].id != deviceId)) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 9911 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9912 | mPatch = *patch; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 9913 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 9914 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9915 | const std::string pathSourcesAsString = patchSourcesToString(patch); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9916 | mThreadMetrics.logEndInterval(); |
Andy Hung | ea84038 | 2020-05-05 21:50:17 -0700 | [diff] [blame] | 9917 | mThreadMetrics.logCreatePatch(pathSourcesAsString, /* outDevices */ {}); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 9918 | mThreadMetrics.logBeginInterval(); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 9919 | // also dispatch to active AudioRecords |
| 9920 | for (const auto &track : mActiveTracks) { |
| 9921 | track->logEndInterval(); |
| 9922 | track->logBeginInterval(pathSourcesAsString); |
| 9923 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 9924 | // Force meteadata update after a route change |
| 9925 | mActiveTracks.setHasChanged(); |
| 9926 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9927 | return status; |
| 9928 | } |
| 9929 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9930 | status_t RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9931 | { |
| 9932 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9933 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9934 | mPatch = audio_patch{}; |
| 9935 | mInDeviceTypeAddr.reset(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 9936 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 9937 | if (mInput->audioHwDev->supportsAudioPatches()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 9938 | sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice(); |
| 9939 | status = hwDevice->releaseAudioPatch(handle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9940 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 9941 | status = mInput->stream->legacyReleaseAudioPatch(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9942 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 9943 | // Force meteadata update after a route change |
| 9944 | mActiveTracks.setHasChanged(); |
| 9945 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 9946 | return status; |
| 9947 | } |
| 9948 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9949 | void RecordThread::updateOutDevices(const DeviceDescriptorBaseVector& outDevices) |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9950 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 9951 | audio_utils::lock_guard _l(mutex()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9952 | mOutDevices = outDevices; |
| 9953 | mOutDeviceTypeAddrs = deviceTypeAddrsFromDescriptors(mOutDevices); |
| 9954 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 9955 | mEffectChains[i]->setDevices_l(outDeviceTypeAddrs()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 9956 | } |
| 9957 | } |
| 9958 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9959 | int32_t RecordThread::getOldestFront_l() |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9960 | { |
| 9961 | if (mTracks.size() == 0) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9962 | return mRsmpInRear; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9963 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9964 | int32_t oldestFront = mRsmpInRear; |
| 9965 | int32_t maxFilled = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9966 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9967 | int32_t front = mTracks[i]->resamplerBufferProvider()->getFront(); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9968 | int32_t filled; |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9969 | (void)__builtin_sub_overflow(mRsmpInRear, front, &filled); |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9970 | if (filled > maxFilled) { |
| 9971 | oldestFront = front; |
| 9972 | maxFilled = filled; |
| 9973 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9974 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 9975 | if (maxFilled > static_cast<signed>(mRsmpInFrames)) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 9976 | (void)__builtin_sub_overflow(mRsmpInRear, mRsmpInFrames, &oldestFront); |
| 9977 | } |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 9978 | return oldestFront; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9979 | } |
| 9980 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9981 | void RecordThread::updateFronts_l(int32_t offset) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9982 | { |
| 9983 | if (offset == 0) { |
| 9984 | return; |
| 9985 | } |
| 9986 | for (size_t i = 0; i < mTracks.size(); i++) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9987 | int32_t front = mTracks[i]->resamplerBufferProvider()->getFront(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9988 | front = audio_utils::safe_sub_overflow(front, offset); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 9989 | mTracks[i]->resamplerBufferProvider()->setFront(front); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9990 | } |
| 9991 | } |
| 9992 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 9993 | void RecordThread::resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 9994 | { |
| 9995 | // This is the formula for calculating the temporary buffer size. |
| 9996 | // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to |
| 9997 | // 1 full output buffer, regardless of the alignment of the available input. |
| 9998 | // The value is somewhat arbitrary, and could probably be even larger. |
| 9999 | // A larger value should allow more old data to be read after a track calls start(), |
| 10000 | // without increasing latency. |
| 10001 | // |
| 10002 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
| 10003 | size_t minRsmpInFrames = mFrameCount * 7; |
| 10004 | |
| 10005 | // maxSharedAudioHistoryMs != 0 indicates a request to possibly make some part of the audio |
| 10006 | // capture history available to another client using the same session ID: |
| 10007 | // dimension the resampler input buffer accordingly. |
| 10008 | |
| 10009 | // Get oldest client read position: getOldestFront_l() must be called before altering |
| 10010 | // mRsmpInRear, or mRsmpInFrames |
| 10011 | int32_t previousFront = getOldestFront_l(); |
| 10012 | size_t previousRsmpInFramesP2 = mRsmpInFramesP2; |
| 10013 | int32_t previousRear = mRsmpInRear; |
| 10014 | mRsmpInRear = 0; |
| 10015 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 10016 | ALOG_ASSERT(maxSharedAudioHistoryMs >= 0 |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10017 | && maxSharedAudioHistoryMs <= kMaxSharedAudioHistoryMs, |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 10018 | "resizeInputBuffer_l() called with invalid max shared history %d", |
| 10019 | maxSharedAudioHistoryMs); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10020 | if (maxSharedAudioHistoryMs != 0) { |
| 10021 | // resizeInputBuffer_l should never be called with a non zero shared history if the |
| 10022 | // buffer was not already allocated |
| 10023 | ALOG_ASSERT(mRsmpInBuffer != nullptr && mRsmpInFrames != 0, |
| 10024 | "resizeInputBuffer_l() called with shared history and unallocated buffer"); |
| 10025 | size_t rsmpInFrames = (size_t)maxSharedAudioHistoryMs * mSampleRate / 1000; |
| 10026 | // never reduce resampler input buffer size |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 10027 | if (rsmpInFrames <= mRsmpInFrames) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10028 | return; |
| 10029 | } |
| 10030 | mRsmpInFrames = rsmpInFrames; |
| 10031 | } |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 10032 | mMaxSharedAudioHistoryMs = maxSharedAudioHistoryMs; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 10033 | // Note: mRsmpInFrames is 0 when called with maxSharedAudioHistoryMs equals to 0 so it is always |
| 10034 | // initialized |
| 10035 | if (mRsmpInFrames < minRsmpInFrames) { |
| 10036 | mRsmpInFrames = minRsmpInFrames; |
| 10037 | } |
| 10038 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
| 10039 | |
| 10040 | // TODO optimize audio capture buffer sizes ... |
| 10041 | // Here we calculate the size of the sliding buffer used as a source |
| 10042 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 10043 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 10044 | // be better to have it derived from the pipe depth in the long term. |
| 10045 | // The current value is higher than necessary. However it should not add to latency. |
| 10046 | |
| 10047 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
| 10048 | mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1; |
| 10049 | |
| 10050 | void *rsmpInBuffer; |
| 10051 | (void)posix_memalign(&rsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize); |
| 10052 | // if posix_memalign fails, will segv here. |
| 10053 | memset(rsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize); |
| 10054 | |
| 10055 | // Copy audio history if any from old buffer before freeing it |
| 10056 | if (previousRear != 0) { |
| 10057 | ALOG_ASSERT(mRsmpInBuffer != nullptr, |
| 10058 | "resizeInputBuffer_l() called with null buffer but frames already read from HAL"); |
| 10059 | |
| 10060 | ssize_t unread = audio_utils::safe_sub_overflow(previousRear, previousFront); |
| 10061 | previousFront &= previousRsmpInFramesP2 - 1; |
| 10062 | size_t part1 = previousRsmpInFramesP2 - previousFront; |
| 10063 | if (part1 > (size_t) unread) { |
| 10064 | part1 = unread; |
| 10065 | } |
| 10066 | if (part1 != 0) { |
| 10067 | memcpy(rsmpInBuffer, (const uint8_t*)mRsmpInBuffer + previousFront * mFrameSize, |
| 10068 | part1 * mFrameSize); |
| 10069 | mRsmpInRear = part1; |
| 10070 | part1 = unread - part1; |
| 10071 | if (part1 != 0) { |
| 10072 | memcpy((uint8_t*)rsmpInBuffer + mRsmpInRear * mFrameSize, |
| 10073 | (const uint8_t*)mRsmpInBuffer, part1 * mFrameSize); |
| 10074 | mRsmpInRear += part1; |
| 10075 | } |
| 10076 | } |
| 10077 | // Update front for all clients according to new rear |
| 10078 | updateFronts_l(audio_utils::safe_sub_overflow(previousRear, mRsmpInRear)); |
| 10079 | } else { |
| 10080 | mRsmpInRear = 0; |
| 10081 | } |
| 10082 | free(mRsmpInBuffer); |
| 10083 | mRsmpInBuffer = rsmpInBuffer; |
| 10084 | } |
| 10085 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10086 | void RecordThread::addPatchTrack(const sp<IAfPatchRecord>& record) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10087 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10088 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10089 | mTracks.add(record); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 10090 | if (record->getSource()) { |
| 10091 | mSource = record->getSource(); |
| 10092 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10093 | } |
| 10094 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10095 | void RecordThread::deletePatchTrack(const sp<IAfPatchRecord>& record) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10096 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10097 | audio_utils::lock_guard _l(mutex()); |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 10098 | if (mSource == record->getSource()) { |
| 10099 | mSource = mInput; |
| 10100 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10101 | destroyTrack_l(record); |
| 10102 | } |
| 10103 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10104 | void RecordThread::toAudioPortConfig(struct audio_port_config* config) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10105 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 10106 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10107 | config->role = AUDIO_PORT_ROLE_SINK; |
| 10108 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 10109 | config->ext.mix.usecase.source = mAudioSource; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 10110 | if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { |
| 10111 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 10112 | config->flags.input = mInput->flags; |
| 10113 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 10114 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 10115 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10116 | // ---------------------------------------------------------------------------- |
| 10117 | // Mmap |
| 10118 | // ---------------------------------------------------------------------------- |
| 10119 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10120 | // Mmap stream control interface implementation. Each MmapThreadHandle controls one |
| 10121 | // MmapPlaybackThread or MmapCaptureThread instance. |
| 10122 | class MmapThreadHandle : public MmapStreamInterface { |
| 10123 | public: |
| 10124 | explicit MmapThreadHandle(const sp<IAfMmapThread>& thread); |
| 10125 | ~MmapThreadHandle() override; |
| 10126 | |
| 10127 | // MmapStreamInterface virtuals |
| 10128 | status_t createMmapBuffer(int32_t minSizeFrames, |
| 10129 | struct audio_mmap_buffer_info* info) final; |
| 10130 | status_t getMmapPosition(struct audio_mmap_position* position) final; |
| 10131 | status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) final; |
| 10132 | status_t start(const AudioClient& client, |
| 10133 | const audio_attributes_t* attr, audio_port_handle_t* handle) final; |
| 10134 | status_t stop(audio_port_handle_t handle) final; |
| 10135 | status_t standby() final; |
| 10136 | status_t reportData(const void* buffer, size_t frameCount) final; |
| 10137 | private: |
| 10138 | const sp<IAfMmapThread> mThread; |
| 10139 | }; |
| 10140 | |
| 10141 | /* static */ |
| 10142 | sp<MmapStreamInterface> IAfMmapThread::createMmapStreamInterfaceAdapter( |
| 10143 | const sp<IAfMmapThread>& mmapThread) { |
| 10144 | return sp<MmapThreadHandle>::make(mmapThread); |
| 10145 | } |
| 10146 | |
| 10147 | MmapThreadHandle::MmapThreadHandle(const sp<IAfMmapThread>& thread) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10148 | : mThread(thread) |
| 10149 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10150 | assert(thread != 0); // thread must start non-null and stay non-null |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10151 | } |
| 10152 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10153 | // MmapStreamInterface could be directly implemented by MmapThread excepting this |
| 10154 | // special handling on adapter dtor. |
| 10155 | MmapThreadHandle::~MmapThreadHandle() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10156 | { |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10157 | mThread->disconnect(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10158 | } |
| 10159 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10160 | status_t MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10161 | struct audio_mmap_buffer_info *info) |
| 10162 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10163 | return mThread->createMmapBuffer(minSizeFrames, info); |
| 10164 | } |
| 10165 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10166 | status_t MmapThreadHandle::getMmapPosition(struct audio_mmap_position* position) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10167 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10168 | return mThread->getMmapPosition(position); |
| 10169 | } |
| 10170 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10171 | status_t MmapThreadHandle::getExternalPosition(uint64_t* position, |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 10172 | int64_t *timeNanos) { |
| 10173 | return mThread->getExternalPosition(position, timeNanos); |
| 10174 | } |
| 10175 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10176 | status_t MmapThreadHandle::start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10177 | const audio_attributes_t *attr, audio_port_handle_t *handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10178 | { |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10179 | return mThread->start(client, attr, handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10180 | } |
| 10181 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10182 | status_t MmapThreadHandle::stop(audio_port_handle_t handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10183 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10184 | return mThread->stop(handle); |
| 10185 | } |
| 10186 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10187 | status_t MmapThreadHandle::standby() |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10188 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10189 | return mThread->standby(); |
| 10190 | } |
| 10191 | |
Andy Hung | 765de28 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 10192 | status_t MmapThreadHandle::reportData(const void* buffer, size_t frameCount) |
| 10193 | { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 10194 | return mThread->reportData(buffer, frameCount); |
| 10195 | } |
| 10196 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10197 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10198 | MmapThread::MmapThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10199 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10200 | AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady, bool isOut) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10201 | : ThreadBase(afThreadCallback, id, (isOut ? MMAP_PLAYBACK : MMAP_CAPTURE), systemReady, isOut), |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10202 | mSessionId(AUDIO_SESSION_NONE), |
François Gaffie | 0c280aa | 2018-07-25 10:02:15 +0200 | [diff] [blame] | 10203 | mPortId(AUDIO_PORT_HANDLE_NONE), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10204 | mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev), |
Eric Laurent | 67f9729 | 2018-04-20 18:05:41 -0700 | [diff] [blame] | 10205 | mActiveTracks(&this->mLocalLog), |
| 10206 | mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later. |
| 10207 | mNoCallbackWarningCount(0) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10208 | { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10209 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10210 | readHalParameters_l(); |
| 10211 | } |
| 10212 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10213 | void MmapThread::onFirstRef() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10214 | { |
| 10215 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 10216 | } |
| 10217 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10218 | void MmapThread::disconnect() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10219 | { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10220 | ActiveTracks<IAfMmapTrack> activeTracks; |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10221 | audio_port_handle_t localPortId; |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10222 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10223 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10224 | for (const sp<IAfMmapTrack>& t : mActiveTracks) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10225 | activeTracks.add(t); |
| 10226 | } |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10227 | localPortId = mPortId; |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10228 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10229 | for (const sp<IAfMmapTrack>& t : activeTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10230 | stop(t->portId()); |
| 10231 | } |
Phil Burk | 9fabbf8 | 2017-08-03 12:02:00 -0700 | [diff] [blame] | 10232 | // This will decrement references and may cause the destruction of this thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10233 | if (isOutput()) { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10234 | AudioSystem::releaseOutput(localPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10235 | } else { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10236 | AudioSystem::releaseInput(localPortId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10237 | } |
| 10238 | } |
| 10239 | |
| 10240 | |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 10241 | void MmapThread::configure_l(const audio_attributes_t* attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10242 | audio_stream_type_t streamType __unused, |
| 10243 | audio_session_t sessionId, |
| 10244 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10245 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10246 | audio_port_handle_t portId) |
| 10247 | { |
| 10248 | mAttr = *attr; |
| 10249 | mSessionId = sessionId; |
| 10250 | mCallback = callback; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10251 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10252 | mPortId = portId; |
| 10253 | } |
| 10254 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10255 | status_t MmapThread::createMmapBuffer(int32_t minSizeFrames, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10256 | struct audio_mmap_buffer_info *info) |
| 10257 | { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10258 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10259 | if (mHalStream == 0) { |
| 10260 | return NO_INIT; |
| 10261 | } |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10262 | mStandby = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10263 | return mHalStream->createMmapBuffer(minSizeFrames, info); |
| 10264 | } |
| 10265 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10266 | status_t MmapThread::getMmapPosition(struct audio_mmap_position* position) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10267 | { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10268 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10269 | if (mHalStream == 0) { |
| 10270 | return NO_INIT; |
| 10271 | } |
| 10272 | return mHalStream->getMmapPosition(position); |
| 10273 | } |
| 10274 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10275 | status_t MmapThread::exitStandby_l() |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10276 | { |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10277 | // The HAL must receive track metadata before starting the stream |
| 10278 | updateMetadata_l(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10279 | status_t ret = mHalStream->start(); |
| 10280 | if (ret != NO_ERROR) { |
| 10281 | ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret); |
| 10282 | return ret; |
| 10283 | } |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10284 | if (mStandby) { |
| 10285 | mThreadMetrics.logBeginInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 10286 | mThreadSnapshot.onBegin(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10287 | mStandby = false; |
| 10288 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 10289 | return NO_ERROR; |
| 10290 | } |
| 10291 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10292 | status_t MmapThread::start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 10293 | const audio_attributes_t *attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10294 | audio_port_handle_t *handle) |
| 10295 | { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10296 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10297 | ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 10298 | client.attributionSource.uid, mStandby, mPortId, *handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10299 | if (mHalStream == 0) { |
| 10300 | return NO_INIT; |
| 10301 | } |
| 10302 | |
| 10303 | status_t ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10304 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10305 | // For the first track, reuse portId and session allocated when the stream was opened. |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10306 | if (*handle == mPortId) { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10307 | acquireWakeLock_l(); |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10308 | return NO_ERROR; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10309 | } |
| 10310 | |
| 10311 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 10312 | |
| 10313 | audio_io_handle_t io = mId; |
Andy Hung | c3af011 | 2023-07-19 16:56:19 -0700 | [diff] [blame] | 10314 | const AttributionSourceState adjAttributionSource = afutils::checkAttributionSourcePackage( |
Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10315 | client.attributionSource); |
| 10316 | |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10317 | const auto localSessionId = mSessionId; |
| 10318 | auto localAttr = mAttr; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10319 | if (isOutput()) { |
| 10320 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 10321 | config.sample_rate = mSampleRate; |
| 10322 | config.channel_mask = mChannelMask; |
| 10323 | config.format = mFormat; |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10324 | audio_stream_type_t stream = streamType_l(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10325 | audio_output_flags_t flags = |
| 10326 | (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] | 10327 | audio_port_handle_t deviceId = mDeviceId; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10328 | std::vector<audio_io_handle_t> secondaryOutputs; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 10329 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 10330 | bool isBitPerfect; |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10331 | mutex().unlock(); |
| 10332 | ret = AudioSystem::getOutputForAttr(&localAttr, &io, |
| 10333 | localSessionId, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10334 | &stream, |
Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10335 | adjAttributionSource, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10336 | &config, |
| 10337 | flags, |
| 10338 | &deviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10339 | &portId, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 10340 | &secondaryOutputs, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 10341 | &isSpatialized, |
| 10342 | &isBitPerfect); |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10343 | mutex().lock(); |
| 10344 | mAttr = localAttr; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 10345 | ALOGD_IF(!secondaryOutputs.empty(), |
| 10346 | "MmapThread::start does not support secondary outputs, ignoring them"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10347 | } else { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10348 | audio_config_base_t config; |
| 10349 | config.sample_rate = mSampleRate; |
| 10350 | config.channel_mask = mChannelMask; |
| 10351 | config.format = mFormat; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10352 | audio_port_handle_t deviceId = mDeviceId; |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10353 | mutex().unlock(); |
| 10354 | ret = AudioSystem::getInputForAttr(&localAttr, &io, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 10355 | RECORD_RIID_INVALID, |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10356 | localSessionId, |
Atneya Nair | f59db5c | 2023-05-10 21:37:41 -0700 | [diff] [blame] | 10357 | adjAttributionSource, |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10358 | &config, |
| 10359 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, |
| 10360 | &deviceId, |
| 10361 | &portId); |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10362 | mutex().lock(); |
| 10363 | // localAttr is const for getInputForAttr. |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10364 | } |
| 10365 | // APM should not chose a different input or output stream for the same set of attributes |
| 10366 | // and audo configuration |
| 10367 | if (ret != NO_ERROR || io != mId) { |
| 10368 | ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)", |
| 10369 | __FUNCTION__, ret, io, mId); |
| 10370 | return BAD_VALUE; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10371 | } |
| 10372 | |
| 10373 | if (isOutput()) { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10374 | mutex().unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10375 | ret = AudioSystem::startOutput(portId); |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10376 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10377 | } else { |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10378 | { |
| 10379 | // Add the track record before starting input so that the silent status for the |
| 10380 | // client can be cached. |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10381 | setClientSilencedState_l(portId, false /*silenced*/); |
| 10382 | } |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10383 | mutex().unlock(); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10384 | ret = AudioSystem::startInput(portId); |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10385 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10386 | } |
| 10387 | |
| 10388 | // abort if start is rejected by audio policy manager |
| 10389 | if (ret != NO_ERROR) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 10390 | ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10391 | if (!mActiveTracks.isEmpty()) { |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10392 | mutex().unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10393 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10394 | AudioSystem::releaseOutput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10395 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 10396 | AudioSystem::releaseInput(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10397 | } |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10398 | mutex().lock(); |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10399 | } else { |
| 10400 | mHalStream->stop(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10401 | } |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10402 | eraseClientSilencedState_l(portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10403 | return PERMISSION_DENIED; |
| 10404 | } |
| 10405 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 10406 | // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ? |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10407 | sp<IAfMmapTrack> track = IAfMmapTrack::create( |
| 10408 | this, attr == nullptr ? mAttr : *attr, mSampleRate, mFormat, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 10409 | mChannelMask, mSessionId, isOutput(), |
| 10410 | client.attributionSource, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 10411 | IPCThreadState::self()->getCallingPid(), portId); |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10412 | if (!isOutput()) { |
| 10413 | track->setSilenced_l(isClientSilenced_l(portId)); |
| 10414 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10415 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10416 | if (isOutput()) { |
| 10417 | // force volume update when a new track is added |
| 10418 | mHalVolFloat = -1.0f; |
| 10419 | } else if (!track->isSilenced_l()) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10420 | for (const sp<IAfMmapTrack>& t : mActiveTracks) { |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10421 | if (t->isSilenced_l() |
| 10422 | && t->uid() != static_cast<uid_t>(client.attributionSource.uid)) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10423 | t->invalidate(); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10424 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 10425 | } |
| 10426 | } |
| 10427 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10428 | mActiveTracks.add(track); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10429 | sp<IAfEffectChain> chain = getEffectChain_l(mSessionId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10430 | if (chain != 0) { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10431 | chain->setStrategy(getStrategyForStream(streamType_l())); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10432 | chain->incTrackCnt(); |
| 10433 | chain->incActiveTrackCnt(); |
| 10434 | } |
| 10435 | |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10436 | track->logBeginInterval(patchSinksToString(&mPatch)); // log to MediaMetrics |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10437 | *handle = portId; |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10438 | |
| 10439 | if (mActiveTracks.size() == 1) { |
| 10440 | ret = exitStandby_l(); |
| 10441 | } |
| 10442 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10443 | broadcast_l(); |
| 10444 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10445 | ALOGV("%s DONE status %d handle %d stream %p", __FUNCTION__, ret, *handle, mHalStream.get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10446 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10447 | return ret; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10448 | } |
| 10449 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10450 | status_t MmapThread::stop(audio_port_handle_t handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10451 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10452 | ALOGV("%s handle %d", __FUNCTION__, handle); |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10453 | audio_utils::lock_guard l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10454 | |
| 10455 | if (mHalStream == 0) { |
| 10456 | return NO_INIT; |
| 10457 | } |
| 10458 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10459 | if (handle == mPortId) { |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10460 | releaseWakeLock_l(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 10461 | return NO_ERROR; |
| 10462 | } |
| 10463 | |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10464 | sp<IAfMmapTrack> track; |
| 10465 | for (const sp<IAfMmapTrack>& t : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10466 | if (handle == t->portId()) { |
| 10467 | track = t; |
| 10468 | break; |
| 10469 | } |
| 10470 | } |
| 10471 | if (track == 0) { |
| 10472 | return BAD_VALUE; |
| 10473 | } |
| 10474 | |
| 10475 | mActiveTracks.remove(track); |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 10476 | eraseClientSilencedState_l(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10477 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10478 | mutex().unlock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10479 | if (isOutput()) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 10480 | AudioSystem::stopOutput(track->portId()); |
| 10481 | AudioSystem::releaseOutput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10482 | } else { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 10483 | AudioSystem::stopInput(track->portId()); |
| 10484 | AudioSystem::releaseInput(track->portId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10485 | } |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10486 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10487 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10488 | sp<IAfEffectChain> chain = getEffectChain_l(track->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10489 | if (chain != 0) { |
| 10490 | chain->decActiveTrackCnt(); |
| 10491 | chain->decTrackCnt(); |
| 10492 | } |
| 10493 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10494 | if (mActiveTracks.isEmpty()) { |
| 10495 | mHalStream->stop(); |
| 10496 | } |
| 10497 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10498 | broadcast_l(); |
| 10499 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10500 | return NO_ERROR; |
| 10501 | } |
| 10502 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10503 | status_t MmapThread::standby() |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10504 | NO_THREAD_SAFETY_ANALYSIS // clang bug |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10505 | { |
| 10506 | ALOGV("%s", __FUNCTION__); |
Atneya Nair | 97a7388 | 2023-10-30 20:26:21 -0700 | [diff] [blame] | 10507 | audio_utils::lock_guard l_{mutex()}; |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10508 | |
| 10509 | if (mHalStream == 0) { |
| 10510 | return NO_INIT; |
| 10511 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10512 | if (!mActiveTracks.isEmpty()) { |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10513 | return INVALID_OPERATION; |
| 10514 | } |
| 10515 | mHalStream->standby(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10516 | if (!mStandby) { |
| 10517 | mThreadMetrics.logEndInterval(); |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 10518 | mThreadSnapshot.onEnd(); |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10519 | mStandby = true; |
| 10520 | } |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10521 | releaseWakeLock_l(); |
Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 10522 | return NO_ERROR; |
| 10523 | } |
| 10524 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10525 | status_t MmapThread::reportData(const void* /*buffer*/, size_t /*frameCount*/) { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 10526 | // This is a stub implementation. The MmapPlaybackThread overrides this function. |
| 10527 | return INVALID_OPERATION; |
| 10528 | } |
| 10529 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10530 | void MmapThread::readHalParameters_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10531 | { |
| 10532 | status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat); |
| 10533 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result); |
| 10534 | mFormat = mHALFormat; |
| 10535 | LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat); |
| 10536 | result = mHalStream->getFrameSize(&mFrameSize); |
| 10537 | 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] | 10538 | LOG_ALWAYS_FATAL_IF(mFrameSize <= 0, "Error frame size was %zu but must be greater than zero", |
| 10539 | mFrameSize); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10540 | result = mHalStream->getBufferSize(&mBufferSize); |
| 10541 | LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result); |
| 10542 | mFrameCount = mBufferSize / mFrameSize; |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10543 | |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 10544 | // TODO: make a readHalParameters call? |
| 10545 | mediametrics::LogItem item(mThreadMetrics.getMetricsId()); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10546 | item.set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_READPARAMETERS) |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 10547 | .set(AMEDIAMETRICS_PROP_ENCODING, IAfThreadBase::formatToString(mFormat).c_str()) |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10548 | .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate) |
| 10549 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask) |
| 10550 | .set(AMEDIAMETRICS_PROP_CHANNELCOUNT, (int32_t)mChannelCount) |
| 10551 | .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount) |
| 10552 | /* |
| 10553 | .set(AMEDIAMETRICS_PROP_FLAGS, toString(flags).c_str()) |
| 10554 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELMASK, |
| 10555 | (int32_t)mHapticChannelMask) |
| 10556 | .set(AMEDIAMETRICS_PROP_PREFIX_HAPTIC AMEDIAMETRICS_PROP_CHANNELCOUNT, |
| 10557 | (int32_t)mHapticChannelCount) |
| 10558 | */ |
| 10559 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_ENCODING, |
Andy Hung | 409572b | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 10560 | IAfThreadBase::formatToString(mHALFormat).c_str()) |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 10561 | .set(AMEDIAMETRICS_PROP_PREFIX_HAL AMEDIAMETRICS_PROP_FRAMECOUNT, |
| 10562 | (int32_t)mFrameCount) // sic - added HAL |
| 10563 | .record(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10564 | } |
| 10565 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10566 | bool MmapThread::threadLoop() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10567 | { |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10568 | { |
| 10569 | audio_utils::unique_lock _l(mutex()); |
| 10570 | checkSilentMode_l(); |
| 10571 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10572 | |
| 10573 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 10574 | |
| 10575 | while (!exitPending()) |
| 10576 | { |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10577 | Vector<sp<IAfEffectChain>> effectChains; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10578 | |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10579 | { // under Thread lock |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10580 | audio_utils::unique_lock _l(mutex()); |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10581 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10582 | if (mSignalPending) { |
| 10583 | // A signal was raised while we were unlocked |
| 10584 | mSignalPending = false; |
| 10585 | } else { |
| 10586 | if (mConfigEvents.isEmpty()) { |
| 10587 | // we're about to wait, flush the binder command buffer |
| 10588 | IPCThreadState::self()->flushCommands(); |
| 10589 | |
| 10590 | if (exitPending()) { |
| 10591 | break; |
| 10592 | } |
| 10593 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10594 | // wait until we have something to do... |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10595 | ALOGV("%s going to sleep", myName.c_str()); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10596 | mWaitWorkCV.wait(_l); |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10597 | ALOGV("%s waking up", myName.c_str()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10598 | |
| 10599 | checkSilentMode_l(); |
| 10600 | |
| 10601 | continue; |
| 10602 | } |
| 10603 | } |
| 10604 | |
| 10605 | processConfigEvents_l(); |
| 10606 | |
| 10607 | processVolume_l(); |
| 10608 | |
| 10609 | checkInvalidTracks_l(); |
| 10610 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10611 | mActiveTracks.updatePowerState_l(this); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10612 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 10613 | updateMetadata_l(); |
| 10614 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10615 | lockEffectChains_l(effectChains); |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10616 | } // release Thread lock |
| 10617 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10618 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10619 | 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] | 10620 | } |
Andy Hung | 13850be | 2019-03-14 11:33:09 -0700 | [diff] [blame] | 10621 | |
| 10622 | // enable changes in effect chain, including moving to another thread. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10623 | unlockEffectChains(effectChains); |
| 10624 | // Effect chains will be actually deleted here if they were removed from |
| 10625 | // mEffectChains list during mixing or effects processing |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 10626 | mThreadloopExecutor.process(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10627 | } |
Andy Hung | 56ce2ed | 2024-06-12 16:03:16 -0700 | [diff] [blame] | 10628 | mThreadloopExecutor.process(); // process any remaining deferred actions. |
| 10629 | // deferred actions after this point are ignored. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10630 | |
| 10631 | threadLoop_exit(); |
| 10632 | |
| 10633 | if (!mStandby) { |
| 10634 | threadLoop_standby(); |
| 10635 | mStandby = true; |
| 10636 | } |
| 10637 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10638 | ALOGV("Thread %p type %d exiting", this, mType); |
| 10639 | return false; |
| 10640 | } |
| 10641 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10642 | // checkForNewParameter_l() must be called with ThreadBase::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10643 | bool MmapThread::checkForNewParameter_l(const String8& keyValuePair, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10644 | status_t& status) |
| 10645 | { |
| 10646 | AudioParameter param = AudioParameter(keyValuePair); |
| 10647 | int value; |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 10648 | bool sendToHal = true; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10649 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10650 | LOG_FATAL("Should not happen set routing device in MmapThread"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10651 | } |
Eric Laurent | e6e9a48 | 2017-07-25 19:26:02 -0700 | [diff] [blame] | 10652 | if (sendToHal) { |
| 10653 | status = mHalStream->setParameters(keyValuePair); |
| 10654 | } else { |
| 10655 | status = NO_ERROR; |
| 10656 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10657 | |
| 10658 | return false; |
| 10659 | } |
| 10660 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10661 | String8 MmapThread::getParameters(const String8& keys) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10662 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 10663 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10664 | String8 out_s8; |
| 10665 | if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) { |
| 10666 | return out_s8; |
| 10667 | } |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10668 | return {}; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10669 | } |
| 10670 | |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10671 | void MmapThread::ioConfigChanged_l(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 10672 | audio_port_handle_t portId __unused) { |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10673 | sp<AudioIoDescriptor> desc; |
| 10674 | bool isInput = false; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10675 | switch (event) { |
| 10676 | case AUDIO_INPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 10677 | case AUDIO_INPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10678 | case AUDIO_INPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10679 | isInput = true; |
| 10680 | FALLTHROUGH_INTENDED; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10681 | case AUDIO_OUTPUT_OPENED: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 10682 | case AUDIO_OUTPUT_REGISTERED: |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10683 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10684 | desc = sp<AudioIoDescriptor>::make(mId, mPatch, isInput, |
| 10685 | mSampleRate, mFormat, mChannelMask, mFrameCount, mFrameCount); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10686 | break; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10687 | case AUDIO_INPUT_CLOSED: |
| 10688 | case AUDIO_OUTPUT_CLOSED: |
| 10689 | default: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 10690 | desc = sp<AudioIoDescriptor>::make(mId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10691 | break; |
| 10692 | } |
Andy Hung | 94dfbb4 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 10693 | mAfThreadCallback->ioConfigChanged_l(event, desc, pid); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10694 | } |
| 10695 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10696 | status_t MmapThread::createAudioPatch_l(const struct audio_patch* patch, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10697 | audio_patch_handle_t *handle) |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 10698 | NO_THREAD_SAFETY_ANALYSIS // elease and re-acquire mutex() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10699 | { |
| 10700 | status_t status = NO_ERROR; |
| 10701 | |
| 10702 | // store new device and send to effects |
| 10703 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 10704 | audio_port_handle_t deviceId; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10705 | AudioDeviceTypeAddrVector sinkDeviceTypeAddrs; |
| 10706 | AudioDeviceTypeAddr sourceDeviceTypeAddr; |
| 10707 | uint32_t numDevices = 0; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10708 | if (isOutput()) { |
| 10709 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10710 | LOG_ALWAYS_FATAL_IF(popcount(patch->sinks[i].ext.device.type) > 1 |
| 10711 | && !mAudioHwDev->supportsAudioPatches(), |
| 10712 | "Enumerated device type(%#x) must not be used " |
| 10713 | "as it does not support audio patches", |
| 10714 | patch->sinks[i].ext.device.type); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 10715 | type = static_cast<audio_devices_t>(type | patch->sinks[i].ext.device.type); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 10716 | sinkDeviceTypeAddrs.emplace_back(patch->sinks[i].ext.device.type, |
| 10717 | patch->sinks[i].ext.device.address); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10718 | } |
| 10719 | deviceId = patch->sinks[0].id; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10720 | numDevices = mPatch.num_sinks; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10721 | } else { |
| 10722 | type = patch->sources[0].ext.device.type; |
| 10723 | deviceId = patch->sources[0].id; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10724 | numDevices = mPatch.num_sources; |
| 10725 | sourceDeviceTypeAddr.mType = patch->sources[0].ext.device.type; |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 10726 | sourceDeviceTypeAddr.setAddress(patch->sources[0].ext.device.address); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10727 | } |
| 10728 | |
| 10729 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 10730 | if (isOutput()) { |
| 10731 | mEffectChains[i]->setDevices_l(sinkDeviceTypeAddrs); |
| 10732 | } else { |
| 10733 | mEffectChains[i]->setInputDevice_l(sourceDeviceTypeAddr); |
| 10734 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10735 | } |
| 10736 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10737 | if (!isOutput()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10738 | // store new source and send to effects |
| 10739 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 10740 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 10741 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 10742 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 10743 | } |
| 10744 | } |
| 10745 | } |
| 10746 | |
jiabin | 78b86f2 | 2024-02-22 00:39:29 +0000 | [diff] [blame] | 10747 | // For mmap streams, once the routing has changed, they will be disconnected. It should be |
| 10748 | // okay to notify the client earlier before the new patch creation. |
| 10749 | if (mDeviceId != deviceId) { |
| 10750 | if (const sp<MmapStreamCallback> callback = mCallback.promote()) { |
| 10751 | // The aaudioservice handle the routing changed event asynchronously. In that case, |
| 10752 | // it is safe to hold the lock here. |
| 10753 | callback->onRoutingChanged(deviceId); |
| 10754 | } |
| 10755 | } |
| 10756 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10757 | if (mAudioHwDev->supportsAudioPatches()) { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10758 | status = mHalDevice->createAudioPatch(patch->num_sources, patch->sources, patch->num_sinks, |
| 10759 | patch->sinks, handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10760 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10761 | audio_port_config port; |
| 10762 | std::optional<audio_source_t> source; |
| 10763 | if (isOutput()) { |
| 10764 | port = patch->sinks[0]; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10765 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10766 | port = patch->sources[0]; |
| 10767 | source = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10768 | } |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10769 | status = mHalStream->legacyCreateAudioPatch(port, source, type); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10770 | *handle = AUDIO_PATCH_HANDLE_NONE; |
| 10771 | } |
| 10772 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10773 | if (numDevices == 0 || mDeviceId != deviceId) { |
| 10774 | if (isOutput()) { |
| 10775 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 10776 | mOutDeviceTypeAddrs = sinkDeviceTypeAddrs; |
jiabin | 0a957d3 | 2020-04-29 10:56:20 -0700 | [diff] [blame] | 10777 | checkSilentMode_l(); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10778 | } else { |
| 10779 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 10780 | mInDeviceTypeAddr = sourceDeviceTypeAddr; |
| 10781 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10782 | mPatch = *patch; |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 10783 | mDeviceId = deviceId; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10784 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10785 | // Force meteadata update after a route change |
| 10786 | mActiveTracks.setHasChanged(); |
| 10787 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10788 | return status; |
| 10789 | } |
| 10790 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10791 | status_t MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10792 | { |
| 10793 | status_t status = NO_ERROR; |
| 10794 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10795 | mPatch = audio_patch{}; |
| 10796 | mOutDeviceTypeAddrs.clear(); |
| 10797 | mInDeviceTypeAddr.reset(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10798 | |
| 10799 | bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ? |
| 10800 | supportsAudioPatches : false; |
| 10801 | |
| 10802 | if (supportsAudioPatches) { |
| 10803 | status = mHalDevice->releaseAudioPatch(handle); |
| 10804 | } else { |
Ytai Ben-Tsvi | f997ffe | 2022-02-03 16:38:16 -0800 | [diff] [blame] | 10805 | status = mHalStream->legacyReleaseAudioPatch(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10806 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 10807 | // Force meteadata update after a route change |
| 10808 | mActiveTracks.setHasChanged(); |
| 10809 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10810 | return status; |
| 10811 | } |
| 10812 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10813 | void MmapThread::toAudioPortConfig(struct audio_port_config* config) |
Andy Hung | bcfd9e1 | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 10814 | NO_THREAD_SAFETY_ANALYSIS // mAudioHwDev handle access |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10815 | { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 10816 | ThreadBase::toAudioPortConfig(config); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10817 | if (isOutput()) { |
| 10818 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 10819 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 10820 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 10821 | } else { |
| 10822 | config->role = AUDIO_PORT_ROLE_SINK; |
| 10823 | config->ext.mix.hw_module = mAudioHwDev->handle(); |
| 10824 | config->ext.mix.usecase.source = mAudioSource; |
| 10825 | } |
| 10826 | } |
| 10827 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10828 | status_t MmapThread::addEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10829 | { |
| 10830 | audio_session_t session = chain->sessionId(); |
| 10831 | |
| 10832 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 10833 | // Attach all tracks with same session ID to this chain. |
| 10834 | // indicate all active tracks in the chain |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10835 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10836 | if (session == track->sessionId()) { |
| 10837 | chain->incTrackCnt(); |
| 10838 | chain->incActiveTrackCnt(); |
| 10839 | } |
| 10840 | } |
| 10841 | |
| 10842 | chain->setThread(this); |
| 10843 | chain->setInBuffer(nullptr); |
| 10844 | chain->setOutBuffer(nullptr); |
Shunkai Yao | d125e40 | 2024-01-20 03:19:06 +0000 | [diff] [blame] | 10845 | chain->syncHalEffectsState_l(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10846 | |
| 10847 | mEffectChains.add(chain); |
| 10848 | checkSuspendOnAddEffectChain_l(chain); |
| 10849 | return NO_ERROR; |
| 10850 | } |
| 10851 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10852 | size_t MmapThread::removeEffectChain_l(const sp<IAfEffectChain>& chain) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10853 | { |
| 10854 | audio_session_t session = chain->sessionId(); |
| 10855 | |
| 10856 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 10857 | |
| 10858 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 10859 | if (chain == mEffectChains[i]) { |
| 10860 | mEffectChains.removeAt(i); |
| 10861 | // detach all active tracks from the chain |
| 10862 | // detach all tracks with same session ID from this chain |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10863 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10864 | if (session == track->sessionId()) { |
| 10865 | chain->decActiveTrackCnt(); |
| 10866 | chain->decTrackCnt(); |
| 10867 | } |
| 10868 | } |
| 10869 | break; |
| 10870 | } |
| 10871 | } |
| 10872 | return mEffectChains.size(); |
| 10873 | } |
| 10874 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10875 | void MmapThread::threadLoop_standby() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10876 | { |
| 10877 | mHalStream->standby(); |
| 10878 | } |
| 10879 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10880 | void MmapThread::threadLoop_exit() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10881 | { |
Phil Burk | 7dce728 | 2017-09-27 13:51:41 -0700 | [diff] [blame] | 10882 | // Do not call callback->onTearDown() because it is redundant for thread exit |
| 10883 | // and because it can cause a recursive mutex lock on stop(). |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10884 | } |
| 10885 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10886 | status_t MmapThread::setSyncEvent(const sp<SyncEvent>& /* event */) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10887 | { |
| 10888 | return BAD_VALUE; |
| 10889 | } |
| 10890 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10891 | bool MmapThread::isValidSyncEvent( |
| 10892 | const sp<SyncEvent>& /* event */) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10893 | { |
| 10894 | return false; |
| 10895 | } |
| 10896 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10897 | status_t MmapThread::checkEffectCompatibility_l( |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10898 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 10899 | { |
| 10900 | // No global effect sessions on mmap threads |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 10901 | if (audio_is_global_session(sessionId)) { |
| 10902 | ALOGW("checkEffectCompatibility_l(): global effect %s on MMAP thread %s", |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10903 | desc->name, mThreadName); |
| 10904 | return BAD_VALUE; |
| 10905 | } |
| 10906 | |
| 10907 | if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 10908 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread", |
| 10909 | desc->name); |
| 10910 | return BAD_VALUE; |
| 10911 | } |
| 10912 | 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] | 10913 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap " |
| 10914 | "thread", desc->name); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10915 | return BAD_VALUE; |
| 10916 | } |
| 10917 | |
| 10918 | // Only allow effects without processing load or latency |
| 10919 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) { |
| 10920 | return BAD_VALUE; |
| 10921 | } |
| 10922 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 10923 | if (IAfEffectModule::isHapticGenerator(&desc->type)) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 10924 | ALOGE("%s(): HapticGenerator is not supported for MmapThread", __func__); |
| 10925 | return BAD_VALUE; |
| 10926 | } |
| 10927 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10928 | return NO_ERROR; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10929 | } |
| 10930 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10931 | void MmapThread::checkInvalidTracks_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10932 | { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10933 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10934 | if (track->isInvalid()) { |
jiabin | 78b86f2 | 2024-02-22 00:39:29 +0000 | [diff] [blame] | 10935 | if (const sp<MmapStreamCallback> callback = mCallback.promote()) { |
| 10936 | // The aaudioservice handle the routing changed event asynchronously. In that case, |
| 10937 | // it is safe to hold the lock here. |
| 10938 | callback->onRoutingChanged(AUDIO_PORT_HANDLE_NONE); |
| 10939 | } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
Eric Laurent | 039c24a | 2022-10-07 14:01:59 +0200 | [diff] [blame] | 10940 | ALOGW("Could not notify MMAP stream tear down: no onRoutingChanged callback!"); |
| 10941 | mNoCallbackWarningCount++; |
| 10942 | } |
| 10943 | break; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10944 | } |
| 10945 | } |
| 10946 | } |
| 10947 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10948 | void MmapThread::dumpInternals_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10949 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10950 | dprintf(fd, " Attributes: content type %d usage %d source %d\n", |
| 10951 | mAttr.content_type, mAttr.usage, mAttr.source); |
| 10952 | dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId); |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 10953 | if (mActiveTracks.isEmpty()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10954 | dprintf(fd, " No active clients\n"); |
| 10955 | } |
| 10956 | } |
| 10957 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10958 | void MmapThread::dumpTracks_l(int fd, const Vector<String16>& /* args */) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10959 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10960 | String8 result; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10961 | size_t numtracks = mActiveTracks.size(); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10962 | dprintf(fd, " %zu Tracks\n", numtracks); |
| 10963 | const char *prefix = " "; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10964 | if (numtracks) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10965 | result.append(prefix); |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 10966 | mActiveTracks[0]->appendDumpHeader(result); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10967 | for (size_t i = 0; i < numtracks ; ++i) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 10968 | sp<IAfMmapTrack> track = mActiveTracks[i]; |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 10969 | result.append(prefix); |
| 10970 | track->appendDump(result, true /* active */); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10971 | } |
| 10972 | } else { |
| 10973 | dprintf(fd, "\n"); |
| 10974 | } |
Tomasz Wasilczyk | 5b05437 | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 10975 | write(fd, result.c_str(), result.size()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10976 | } |
| 10977 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10978 | /* static */ |
| 10979 | sp<IAfMmapPlaybackThread> IAfMmapPlaybackThread::create( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10980 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10981 | AudioHwDevice* hwDev, AudioStreamOut* output, bool systemReady) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10982 | return sp<MmapPlaybackThread>::make(afThreadCallback, id, hwDev, output, systemReady); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 10983 | } |
| 10984 | |
| 10985 | MmapPlaybackThread::MmapPlaybackThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10986 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 10987 | AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10988 | : MmapThread(afThreadCallback, id, hwDev, output->stream, systemReady, true /* isOut */), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10989 | mStreamType(AUDIO_STREAM_MUSIC), |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 10990 | mOutput(output) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 10991 | { |
| 10992 | snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id); |
| 10993 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 10994 | mMasterVolume = afThreadCallback->masterVolume_l(); |
| 10995 | mMasterMute = afThreadCallback->masterMute_l(); |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 10996 | |
| 10997 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_FOR_POLICY_CNT; ++i) { |
| 10998 | const audio_stream_type_t stream{static_cast<audio_stream_type_t>(i)}; |
| 10999 | mStreamTypes[stream].volume = 0.0f; |
| 11000 | mStreamTypes[stream].mute = mAfThreadCallback->streamMute_l(stream); |
| 11001 | } |
| 11002 | // Audio patch and call assistant volume are always max |
| 11003 | mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f; |
| 11004 | mStreamTypes[AUDIO_STREAM_PATCH].mute = false; |
| 11005 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].volume = 1.0f; |
| 11006 | mStreamTypes[AUDIO_STREAM_CALL_ASSISTANT].mute = false; |
| 11007 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11008 | if (mAudioHwDev) { |
| 11009 | if (mAudioHwDev->canSetMasterVolume()) { |
| 11010 | mMasterVolume = 1.0; |
| 11011 | } |
| 11012 | |
| 11013 | if (mAudioHwDev->canSetMasterMute()) { |
| 11014 | mMasterMute = false; |
| 11015 | } |
| 11016 | } |
| 11017 | } |
| 11018 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11019 | void MmapPlaybackThread::configure(const audio_attributes_t* attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11020 | audio_stream_type_t streamType, |
| 11021 | audio_session_t sessionId, |
| 11022 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 11023 | audio_port_handle_t deviceId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11024 | audio_port_handle_t portId) |
| 11025 | { |
Andy Hung | 160664b | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 11026 | audio_utils::lock_guard l(mutex()); |
| 11027 | MmapThread::configure_l(attr, streamType, sessionId, callback, deviceId, portId); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11028 | mStreamType = streamType; |
| 11029 | } |
| 11030 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11031 | AudioStreamOut* MmapPlaybackThread::clearOutput() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11032 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11033 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11034 | AudioStreamOut *output = mOutput; |
| 11035 | mOutput = NULL; |
| 11036 | return output; |
| 11037 | } |
| 11038 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11039 | void MmapPlaybackThread::setMasterVolume(float value) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11040 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11041 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11042 | // Don't apply master volume in SW if our HAL can do it for us. |
| 11043 | if (mAudioHwDev && |
| 11044 | mAudioHwDev->canSetMasterVolume()) { |
| 11045 | mMasterVolume = 1.0; |
| 11046 | } else { |
| 11047 | mMasterVolume = value; |
| 11048 | } |
| 11049 | } |
| 11050 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11051 | void MmapPlaybackThread::setMasterMute(bool muted) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11052 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11053 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11054 | // Don't apply master mute in SW if our HAL can do it for us. |
| 11055 | if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) { |
| 11056 | mMasterMute = false; |
| 11057 | } else { |
| 11058 | mMasterMute = muted; |
| 11059 | } |
| 11060 | } |
| 11061 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11062 | void MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11063 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11064 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11065 | mStreamTypes[stream].volume = value; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11066 | if (stream == mStreamType) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11067 | broadcast_l(); |
| 11068 | } |
| 11069 | } |
| 11070 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11071 | float MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11072 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11073 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11074 | return mStreamTypes[stream].volume; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11075 | } |
| 11076 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11077 | void MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11078 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11079 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11080 | mStreamTypes[stream].mute = muted; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11081 | if (stream == mStreamType) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11082 | broadcast_l(); |
| 11083 | } |
| 11084 | } |
| 11085 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11086 | void MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11087 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11088 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11089 | if (streamType == mStreamType) { |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11090 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11091 | track->invalidate(); |
| 11092 | } |
| 11093 | broadcast_l(); |
| 11094 | } |
| 11095 | } |
| 11096 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11097 | void MmapPlaybackThread::invalidateTracks(std::set<audio_port_handle_t>& portIds) |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11098 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11099 | audio_utils::lock_guard _l(mutex()); |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11100 | bool trackMatch = false; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11101 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 11102 | if (portIds.find(track->portId()) != portIds.end()) { |
| 11103 | track->invalidate(); |
| 11104 | trackMatch = true; |
| 11105 | portIds.erase(track->portId()); |
| 11106 | } |
| 11107 | if (portIds.empty()) { |
| 11108 | break; |
| 11109 | } |
| 11110 | } |
| 11111 | if (trackMatch) { |
| 11112 | broadcast_l(); |
| 11113 | } |
| 11114 | } |
| 11115 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11116 | void MmapPlaybackThread::processVolume_l() |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 11117 | NO_THREAD_SAFETY_ANALYSIS // access of track->processMuteEvent_l |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11118 | { |
| 11119 | float volume; |
| 11120 | |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11121 | if (mMasterMute || streamMuted_l()) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11122 | volume = 0; |
| 11123 | } else { |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11124 | volume = mMasterVolume * streamVolume_l(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11125 | } |
| 11126 | |
| 11127 | if (volume != mHalVolFloat) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11128 | // Convert volumes from float to 8.24 |
| 11129 | uint32_t vol = (uint32_t)(volume * (1 << 24)); |
| 11130 | |
| 11131 | // Delegate volume control to effect in track effect chain if needed |
| 11132 | // only one effect chain can be present on DirectOutputThread, so if |
| 11133 | // there is one, the track is connected to it |
| 11134 | if (!mEffectChains.isEmpty()) { |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame] | 11135 | mEffectChains[0]->setVolume(&vol, &vol); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11136 | volume = (float)vol / (1 << 24); |
| 11137 | } |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 11138 | // 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] | 11139 | if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) { |
| 11140 | mHalVolFloat = volume; // HW volume control worked, so update value. |
| 11141 | mNoCallbackWarningCount = 0; |
| 11142 | } else { |
Eric Laurent | dff774a | 2017-04-21 15:29:38 -0700 | [diff] [blame] | 11143 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 11144 | if (callback != 0) { |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11145 | mHalVolFloat = volume; // SW volume control worked, so update value. |
| 11146 | mNoCallbackWarningCount = 0; |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11147 | mutex().unlock(); |
Robert Wu | 4389ae6 | 2022-02-17 18:39:41 +0000 | [diff] [blame] | 11148 | callback->onVolumeChanged(volume); |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11149 | mutex().lock(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11150 | } else { |
Phil Burk | 56ecf3e | 2018-03-12 15:38:17 -0700 | [diff] [blame] | 11151 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 11152 | ALOGW("Could not set MMAP stream volume: no volume callback!"); |
| 11153 | mNoCallbackWarningCount++; |
| 11154 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11155 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11156 | } |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11157 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11158 | track->setMetadataHasChanged(); |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11159 | track->processMuteEvent_l(mAfThreadCallback->getOrCreateAudioManager(), |
Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 11160 | /*muteState=*/{mMasterMute, |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11161 | streamVolume_l() == 0.f, |
| 11162 | streamMuted_l(), |
Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 11163 | // TODO(b/241533526): adjust logic to include mute from AppOps |
| 11164 | false /*muteFromPlaybackRestricted*/, |
| 11165 | false /*muteFromClientVolume*/, |
| 11166 | false /*muteFromVolumeShaper*/}); |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11167 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11168 | } |
| 11169 | } |
| 11170 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11171 | ThreadBase::MetadataUpdate MmapPlaybackThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11172 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11173 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11174 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11175 | } |
| 11176 | StreamOutHalInterface::SourceMetadata metadata; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11177 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11178 | // 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] | 11179 | playback_track_metadata_v7_t trackMetadata; |
| 11180 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11181 | .usage = track->attributes().usage, |
| 11182 | .content_type = track->attributes().content_type, |
| 11183 | .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11184 | }; |
| 11185 | trackMetadata.channel_mask = track->channelMask(), |
| 11186 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 11187 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11188 | } |
| 11189 | mOutput->stream->updateSourceMetadata(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11190 | |
| 11191 | MetadataUpdate change; |
| 11192 | change.playbackMetadataUpdate = metadata.tracks; |
| 11193 | return change; |
| 11194 | }; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11195 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11196 | void MmapPlaybackThread::checkSilentMode_l() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11197 | { |
| 11198 | if (!mMasterMute) { |
| 11199 | char value[PROPERTY_VALUE_MAX]; |
| 11200 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 11201 | char *endptr; |
| 11202 | unsigned long ul = strtoul(value, &endptr, 0); |
| 11203 | if (*endptr == '\0' && ul != 0) { |
Andy Hung | 6fb2689 | 2024-02-20 16:32:57 -0800 | [diff] [blame] | 11204 | ALOGW("%s: mute from ro.audio.silent. Silence is golden", __func__); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11205 | // The setprop command will not allow a property to be changed after |
| 11206 | // the first time it is set, so we don't have to worry about un-muting. |
| 11207 | setMasterMute_l(true); |
| 11208 | } |
| 11209 | } |
| 11210 | } |
| 11211 | } |
| 11212 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11213 | void MmapPlaybackThread::toAudioPortConfig(struct audio_port_config* config) |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 11214 | { |
| 11215 | MmapThread::toAudioPortConfig(config); |
| 11216 | if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) { |
| 11217 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 11218 | config->flags.output = mOutput->flags; |
| 11219 | } |
| 11220 | } |
| 11221 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11222 | status_t MmapPlaybackThread::getExternalPosition(uint64_t* position, |
Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 11223 | int64_t* timeNanos) const |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 11224 | { |
| 11225 | if (mOutput == nullptr) { |
| 11226 | return NO_INIT; |
| 11227 | } |
| 11228 | struct timespec timestamp; |
| 11229 | status_t status = mOutput->getPresentationPosition(position, ×tamp); |
| 11230 | if (status == NO_ERROR) { |
| 11231 | *timeNanos = timestamp.tv_sec * NANOS_PER_SECOND + timestamp.tv_nsec; |
| 11232 | } |
| 11233 | return status; |
| 11234 | } |
| 11235 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11236 | status_t MmapPlaybackThread::reportData(const void* buffer, size_t frameCount) { |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11237 | // Send to MelProcessor for sound dose measurement. |
| 11238 | auto processor = mMelProcessor.load(); |
| 11239 | if (processor) { |
| 11240 | processor->process(buffer, frameCount * mFrameSize); |
| 11241 | } |
| 11242 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 11243 | return NO_ERROR; |
| 11244 | } |
| 11245 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11246 | // startMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11247 | void MmapPlaybackThread::startMelComputation_l( |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11248 | const sp<audio_utils::MelProcessor>& processor) |
| 11249 | { |
| 11250 | ALOGV("%s: starting mel processor for thread %d", __func__, id()); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11251 | mMelProcessor.store(processor); |
| 11252 | if (processor) { |
| 11253 | processor->resume(); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11254 | } |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11255 | |
| 11256 | // no need to update output format for MMapPlaybackThread since it is |
| 11257 | // assigned constant for each thread |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11258 | } |
| 11259 | |
Andy Hung | b17d24b | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 11260 | // stopMelComputation_l() must be called with AudioFlinger::mutex() held |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11261 | void MmapPlaybackThread::stopMelComputation_l() |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11262 | { |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 11263 | ALOGV("%s: pausing mel processor for thread %d", __func__, id()); |
| 11264 | auto melProcessor = mMelProcessor.load(); |
| 11265 | if (melProcessor != nullptr) { |
| 11266 | melProcessor->pause(); |
| 11267 | } |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 11268 | } |
| 11269 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11270 | void MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11271 | { |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 11272 | MmapThread::dumpInternals_l(fd, args); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11273 | |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 11274 | dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n", |
Eric Laurent | 1961151 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 11275 | mStreamType, streamVolume_l(), mHalVolFloat, streamMuted_l()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11276 | dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute); |
| 11277 | } |
| 11278 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11279 | /* static */ |
| 11280 | sp<IAfMmapCaptureThread> IAfMmapCaptureThread::create( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11281 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11282 | AudioHwDevice* hwDev, AudioStreamIn* input, bool systemReady) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11283 | return sp<MmapCaptureThread>::make(afThreadCallback, id, hwDev, input, systemReady); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11284 | } |
| 11285 | |
| 11286 | MmapCaptureThread::MmapCaptureThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11287 | const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 11288 | AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11289 | : MmapThread(afThreadCallback, id, hwDev, input->stream, systemReady, false /* isOut */), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11290 | mInput(input) |
| 11291 | { |
| 11292 | snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id); |
| 11293 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
| 11294 | } |
| 11295 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11296 | status_t MmapCaptureThread::exitStandby_l() |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11297 | { |
Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 11298 | { |
| 11299 | // mInput might have been cleared by clearInput() |
Phil Burk | f054fc3 | 2018-12-06 09:45:59 -0800 | [diff] [blame] | 11300 | if (mInput != nullptr && mInput->stream != nullptr) { |
| 11301 | mInput->stream->setGain(1.0f); |
| 11302 | } |
| 11303 | } |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 11304 | return MmapThread::exitStandby_l(); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11305 | } |
| 11306 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11307 | AudioStreamIn* MmapCaptureThread::clearInput() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11308 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11309 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 11310 | AudioStreamIn *input = mInput; |
| 11311 | mInput = NULL; |
| 11312 | return input; |
| 11313 | } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11314 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11315 | void MmapCaptureThread::processVolume_l() |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11316 | { |
| 11317 | bool changed = false; |
| 11318 | bool silenced = false; |
| 11319 | |
| 11320 | sp<MmapStreamCallback> callback = mCallback.promote(); |
| 11321 | if (callback == 0) { |
| 11322 | if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) { |
| 11323 | ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!"); |
| 11324 | mNoCallbackWarningCount++; |
| 11325 | } |
| 11326 | } |
| 11327 | |
| 11328 | // After a change occurred in track silenced state, mute capture in audio DSP if at least one |
| 11329 | // track is silenced and unmute otherwise |
| 11330 | for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) { |
| 11331 | if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) { |
| 11332 | changed = true; |
| 11333 | silenced = mActiveTracks[i]->isSilenced_l(); |
| 11334 | } |
| 11335 | } |
| 11336 | |
| 11337 | if (changed) { |
| 11338 | mInput->stream->setGain(silenced ? 0.0f: 1.0f); |
| 11339 | } |
| 11340 | } |
| 11341 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11342 | ThreadBase::MetadataUpdate MmapCaptureThread::updateMetadata_l() |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11343 | { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 11344 | if (!isStreamInitialized() || !mActiveTracks.readAndClearHasChanged()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11345 | return {}; // nothing to do |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11346 | } |
| 11347 | StreamInHalInterface::SinkMetadata metadata; |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11348 | for (const sp<IAfMmapTrack>& track : mActiveTracks) { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11349 | // 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] | 11350 | record_track_metadata_v7_t trackMetadata; |
| 11351 | trackMetadata.base = { |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11352 | .source = track->attributes().source, |
| 11353 | .gain = 1, // capture tracks do not have volumes |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 11354 | }; |
| 11355 | trackMetadata.channel_mask = track->channelMask(), |
| 11356 | strncpy(trackMetadata.tags, track->attributes().tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 11357 | metadata.tracks.push_back(trackMetadata); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11358 | } |
| 11359 | mInput->stream->updateSinkMetadata(metadata); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 11360 | MetadataUpdate change; |
| 11361 | change.recordMetadataUpdate = metadata.tracks; |
| 11362 | return change; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 11363 | } |
| 11364 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11365 | void MmapCaptureThread::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11366 | { |
Andy Hung | f8635b6 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 11367 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11368 | for (size_t i = 0; i < mActiveTracks.size() ; i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 11369 | if (mActiveTracks[i]->portId() == portId) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11370 | mActiveTracks[i]->setSilenced_l(silenced); |
| 11371 | broadcast_l(); |
| 11372 | } |
| 11373 | } |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 11374 | setClientSilencedIfExists_l(portId, silenced); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 11375 | } |
| 11376 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11377 | void MmapCaptureThread::toAudioPortConfig(struct audio_port_config* config) |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 11378 | { |
| 11379 | MmapThread::toAudioPortConfig(config); |
| 11380 | if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) { |
| 11381 | config->config_mask |= AUDIO_PORT_CONFIG_FLAGS; |
| 11382 | config->flags.input = mInput->flags; |
| 11383 | } |
| 11384 | } |
| 11385 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11386 | status_t MmapCaptureThread::getExternalPosition( |
Andy Hung | 3e4c874 | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 11387 | uint64_t* position, int64_t* timeNanos) const |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 11388 | { |
| 11389 | if (mInput == nullptr) { |
| 11390 | return NO_INIT; |
| 11391 | } |
| 11392 | return mInput->getCapturePosition((int64_t*)position, timeNanos); |
| 11393 | } |
| 11394 | |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11395 | // ---------------------------------------------------------------------------- |
| 11396 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11397 | /* static */ |
| 11398 | sp<IAfPlaybackThread> IAfPlaybackThread::createBitPerfectThread( |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11399 | const sp<IAfThreadCallback>& afThreadCallback, |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11400 | AudioStreamOut* output, audio_io_handle_t id, bool systemReady) { |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11401 | return sp<BitPerfectThread>::make(afThreadCallback, output, id, systemReady); |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11402 | } |
| 11403 | |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11404 | BitPerfectThread::BitPerfectThread(const sp<IAfThreadCallback> &afThreadCallback, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11405 | AudioStreamOut *output, audio_io_handle_t id, bool systemReady) |
Andy Hung | 7535ed9 | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 11406 | : MixerThread(afThreadCallback, output, id, systemReady, BIT_PERFECT) {} |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11407 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11408 | PlaybackThread::mixer_state BitPerfectThread::prepareTracks_l( |
Andy Hung | 11e7424 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 11409 | Vector<sp<IAfTrack>>* tracksToRemove) { |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11410 | mixer_state result = MixerThread::prepareTracks_l(tracksToRemove); |
| 11411 | // If there is only one active track and it is bit-perfect, enable tee buffer. |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 11412 | float volumeLeft = 1.0f; |
| 11413 | float volumeRight = 1.0f; |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11414 | if (sp<IAfTrack> bitPerfectTrack = getTrackToStreamBitPerfectly_l(); |
| 11415 | bitPerfectTrack != nullptr) { |
| 11416 | const int trackId = bitPerfectTrack->id(); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11417 | mAudioMixer->setParameter( |
| 11418 | trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, (void *)mSinkBuffer); |
| 11419 | mAudioMixer->setParameter( |
| 11420 | trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER_FRAME_COUNT, |
| 11421 | (void *)(uintptr_t)mNormalFrameCount); |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11422 | bitPerfectTrack->getFinalVolume(&volumeLeft, &volumeRight); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11423 | mIsBitPerfect = true; |
| 11424 | } else { |
| 11425 | mIsBitPerfect = false; |
| 11426 | // No need to copy bit-perfect data directly to sink buffer given there are multiple tracks |
| 11427 | // active. |
| 11428 | for (const auto& track : mActiveTracks) { |
| 11429 | const int trackId = track->id(); |
| 11430 | mAudioMixer->setParameter( |
| 11431 | trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, nullptr); |
| 11432 | } |
| 11433 | } |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 11434 | if (mVolumeLeft != volumeLeft || mVolumeRight != volumeRight) { |
| 11435 | mVolumeLeft = volumeLeft; |
| 11436 | mVolumeRight = volumeRight; |
| 11437 | setVolumeForOutput_l(volumeLeft, volumeRight); |
| 11438 | } |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11439 | return result; |
| 11440 | } |
| 11441 | |
Andy Hung | 4b17e88 | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 11442 | void BitPerfectThread::threadLoop_mix() { |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 11443 | MixerThread::threadLoop_mix(); |
| 11444 | mHasDataCopiedToSinkBuffer = mIsBitPerfect; |
| 11445 | } |
| 11446 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame] | 11447 | void BitPerfectThread::setTracksInternalMute( |
| 11448 | std::map<audio_port_handle_t, bool>* tracksInternalMute) { |
| 11449 | for (auto& track : mTracks) { |
| 11450 | if (auto it = tracksInternalMute->find(track->portId()); it != tracksInternalMute->end()) { |
| 11451 | track->setInternalMute(it->second); |
| 11452 | tracksInternalMute->erase(it); |
| 11453 | } |
| 11454 | } |
| 11455 | } |
| 11456 | |
| 11457 | sp<IAfTrack> BitPerfectThread::getTrackToStreamBitPerfectly_l() { |
| 11458 | if (com::android::media::audioserver:: |
| 11459 | fix_concurrent_playback_behavior_with_bit_perfect_client()) { |
| 11460 | sp<IAfTrack> bitPerfectTrack = nullptr; |
| 11461 | bool allOtherTracksMuted = true; |
| 11462 | // Return the bit perfect track if all other tracks are muted |
| 11463 | for (const auto& track : mActiveTracks) { |
| 11464 | if (track->isBitPerfect()) { |
| 11465 | bitPerfectTrack = track; |
| 11466 | } else if (track->getFinalVolume() != 0.f) { |
| 11467 | allOtherTracksMuted = false; |
| 11468 | if (bitPerfectTrack != nullptr) { |
| 11469 | break; |
| 11470 | } |
| 11471 | } |
| 11472 | } |
| 11473 | return allOtherTracksMuted ? bitPerfectTrack : nullptr; |
| 11474 | } else { |
| 11475 | if (mActiveTracks.size() == 1 && mActiveTracks[0]->isBitPerfect()) { |
| 11476 | return mActiveTracks[0]; |
| 11477 | } |
| 11478 | } |
| 11479 | return nullptr; |
| 11480 | } |
| 11481 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 11482 | } // namespace android |