| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2022 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "../fpp-pch.h" | ||
| 14 | |||
| 15 | #include <sys/time.h> | ||
| 16 | #include <algorithm> | ||
| 17 | #include <atomic> | ||
| 18 | #include <chrono> | ||
| 19 | #include <cmath> | ||
| 20 | #include <condition_variable> | ||
| 21 | #include <cstring> | ||
| 22 | #include <ctime> | ||
| 23 | #include <deque> | ||
| 24 | #include <errno.h> | ||
| 25 | #include <mutex> | ||
| 26 | #include <thread> | ||
| 27 | |||
| 28 | #include "../MultiSync.h" | ||
| 29 | #include "../Sequence.h" | ||
| 30 | #include "../Warnings.h" | ||
| 31 | #include "../channeltester/ChannelTester.h" | ||
| 32 | #include "../commands/Commands.h" | ||
| 33 | #include "../common.h" | ||
| 34 | #include "../effects.h" | ||
| 35 | #include "../log.h" | ||
| 36 | #include "../mediaoutput/GStreamerOut.h" | ||
| 37 | #include "../overlays/PixelOverlay.h" | ||
| 38 | #include "../settings.h" | ||
| 39 | |||
| 40 | #include "ChannelOutputSetup.h" | ||
| 41 | #include "channeloutputthread.h" | ||
| 42 | |||
| 43 | /* used by external sync code */ | ||
| 44 | // Atomic: the output thread, media sync (on the media or MultiSync thread) | ||
| 45 | // and sequence start all read and write these. | ||
| 46 | std::atomic<float> RefreshRate{ 20 }; | ||
| 47 | std::atomic_int SequenceLightDelay{ 50000 }; | ||
| 48 | std::atomic_int BridgeLightDelay{ 0 }; | ||
| 49 | std::atomic_int LightDelay{ 50000 }; | ||
| 50 | std::atomic_int FrameSkip{ 0 }; | ||
| 51 | std::atomic_int MasterFramesPlayed{ -1 }; | ||
| 52 | std::atomic_int OutputFrames{ 1 }; | ||
| 53 | // Set by every StartChannelOutputThread(), which an overlay model switched on | ||
| 54 | // from an API thread calls too, and read on the playlist's thread. | ||
| 55 | std::atomic<float> mediaOffset{ 0.0 }; | ||
| 56 | |||
| 57 | /* local variables */ | ||
| 58 | // Atomic, not volatile: volatile orders nothing between threads, and the main | ||
| 59 | // loop, HTTP threads and MultiSync read and set these while the output thread | ||
| 60 | // runs. | ||
| 61 | std::atomic_int RunThread{ 0 }; | ||
| 62 | std::atomic_int ThreadIsRunning{ 0 }; | ||
| 63 | std::atomic_int ThreadIsExiting{ 0 }; | ||
| 64 | std::atomic_int outputForced{ 0 }; | ||
| 65 | std::atomic_int alwaysTransmit{ 0 }; | ||
| 66 | |||
| 67 | // declared in channeloutputthread.h, which documents what it protects | ||
| 68 | std::mutex outputThreadLock; | ||
| 69 | // Threads blocked in LockOutputThread(); the output thread hands the lock over | ||
| 70 | // at the end of a pass while this is non-zero. | ||
| 71 | static std::atomic_int outputThreadLockWaiters{ 0 }; | ||
| 72 | std::recursive_mutex inlineOutputLock; | ||
| 73 | std::mutex outputThreadStatusLock; | ||
| 74 | // Bumped under outputThreadStatusLock by every StartChannelOutputThread(), | ||
| 75 | // which its callers make after setting up what they want output for (an | ||
| 76 | // effect, an active overlay, a video). The thread reads it at the top of each | ||
| 77 | // pass, before it samples what needs output, and won't idle past a change. | ||
| 78 | static std::atomic<uint64_t> outputStartRequests{ 0 }; | ||
| 79 | // Set (under outputThreadStatusLock) by StopChannelOutputThread(), which only | ||
| 80 | // fppd's shutdown calls, and never cleared. The teardown after it closes | ||
| 81 | // outputs and removes overlay models, and a model state change restarts the | ||
| 82 | // thread; one restarted there outlived the sequence it reads, which main() | ||
| 83 | // deletes next. So once stopped, the thread stays stopped. | ||
| 84 | static bool outputThreadShutDown = false; | ||
| 85 | std::condition_variable outputThreadCond; | ||
| 86 | std::condition_variable outputThreadSatusCond; | ||
| 87 | |||
| 88 | // The pace of the media clock the sequence follows, relative to the system | ||
| 89 | // clock, and when it was last reported (GetMonotonicTimeMicros()). A card's crystal is | ||
| 90 | // tens to hundreds of ppm off; frames run at its pace so the lights don't walk | ||
| 91 | // across media sync's whole-frame dead band over a song. It goes stale (back | ||
| 92 | // to 1.0) a second after the media stops reporting it. | ||
| 93 | static std::atomic<double> mediaClockRate{ 1.0 }; | ||
| 94 | static std::atomic<long long> mediaClockRateAt{ 0 }; | ||
| 95 | |||
| 96 | // Relaxed: two independent values read once a frame. A reader that pairs a | ||
| 97 | // new time with the previous rate uses a rate one report old for one frame. | ||
| 98 | // On an AM335x each seq_cst access is a pair of barriers (~0.1 us), and the | ||
| 99 | // 64-bit and double ones are ldrexd/strexd as well. | ||
| 100 | 162352 | void SetMediaClockRate(double rate) { | |
| 101 | 162352 | mediaClockRate.store(rate, std::memory_order_relaxed); | |
| 102 | 162352 | mediaClockRateAt.store(GetMonotonicTimeMicros(), std::memory_order_relaxed); | |
| 103 | 162352 | } | |
| 104 | |||
| 105 | 312269 | static double CurrentMediaClockRate(long long now) { | |
| 106 | // A remote follows the master's frame numbers, not its own media. | ||
| 107 |
6/6✓ Branch 0 taken 162665 times.
✓ Branch 1 taken 149604 times.
✓ Branch 2 taken 161820 times.
✓ Branch 3 taken 845 times.
✓ Branch 4 taken 150449 times.
✓ Branch 5 taken 161820 times.
|
474089 | if (now - mediaClockRateAt.load(std::memory_order_relaxed) > 1000000 || !sequence->IsSequenceRunning() || |
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 161820 times.
|
161820 | getFPPmode() == REMOTE_MODE) { |
| 109 | 150449 | return 1.0; | |
| 110 | } | ||
| 111 | 161820 | return mediaClockRate.load(std::memory_order_relaxed); | |
| 112 | } | ||
| 113 | |||
| 114 | // Media phase servo. Media sync used to compare whole frame counts once a | ||
| 115 | // second and leave anything within a frame alone, so the lights sat anywhere | ||
| 116 | // up to two frames (100 ms at 20 fps) from the sound. Now the output thread | ||
| 117 | // publishes which frame it sends next and when that is due, each media | ||
| 118 | // position report works out when the media says it should be due, and the | ||
| 119 | // difference is closed a little each frame. Whole-frame skips and holds | ||
| 120 | // (CalculateNewChannelOutputDelayForFrame()) still handle anything larger. | ||
| 121 | // | ||
| 122 | // A MultiSync remote has no media of its own to follow; it holds its frames | ||
| 123 | // to the master's sync packets the same way (UpdateMasterPosition()). | ||
| 124 | // | ||
| 125 | // Pending correction in us: > 0 delays the frames to come, < 0 brings them | ||
| 126 | // forward. The output thread takes at most kMaxPhaseStep of a period from it | ||
| 127 | // per frame, so the cadence never visibly jumps. It goes stale a second after | ||
| 128 | // the media (or the master) stops reporting, like the clock rate. | ||
| 129 | static constexpr double kMaxPhaseStep = 0.1; | ||
| 130 | static std::atomic<long long> mediaPhaseCorrUs{ 0 }; | ||
| 131 | static std::atomic<long long> mediaPhaseCorrAt{ 0 }; | ||
| 132 | // The next frame the thread sends and when it is due, published together. | ||
| 133 | static std::mutex nextSendLock; | ||
| 134 | static long nextSendFrame = -1; | ||
| 135 | static long long nextSendDueUs = 0; | ||
| 136 | |||
| 137 | 100194 | static void PublishNextSend(long frame, long long dueUs) { | |
| 138 |
1/2✓ Branch 0 taken 100194 times.
✗ Branch 1 not taken.
|
100194 | std::unique_lock<std::mutex> l(nextSendLock); |
| 139 | 100194 | nextSendFrame = frame; | |
| 140 | 100194 | nextSendDueUs = dueUs; | |
| 141 | 100194 | } | |
| 142 | |||
| 143 | 156550 | static double TakeMediaPhaseStep(long long now, double period) { | |
| 144 |
7/8✓ Branch 0 taken 42857 times.
✓ Branch 1 taken 113693 times.
✓ Branch 2 taken 42857 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 237 times.
✓ Branch 5 taken 42620 times.
✓ Branch 6 taken 113930 times.
✓ Branch 7 taken 42620 times.
|
156550 | if (now - mediaPhaseCorrAt.load(std::memory_order_relaxed) > 1000000 || !sequence->IsSequenceRunning()) { |
| 145 | 113930 | return 0.0; | |
| 146 | } | ||
| 147 | 42620 | long long pending = mediaPhaseCorrUs.load(std::memory_order_relaxed); | |
| 148 | 42620 | long long maxStep = (long long)(period * kMaxPhaseStep); | |
| 149 |
1/2✓ Branch 0 taken 42620 times.
✗ Branch 1 not taken.
|
42620 | long long step = std::clamp(pending, -maxStep, maxStep); |
| 150 |
2/2✓ Branch 0 taken 34669 times.
✓ Branch 1 taken 7951 times.
|
42620 | if (step) { |
| 151 | mediaPhaseCorrUs.fetch_sub(step, std::memory_order_relaxed); | ||
| 152 | } | ||
| 153 | 42620 | return (double)step; | |
| 154 | } | ||
| 155 | |||
| 156 | /* prototypes for functions below */ | ||
| 157 | void CalculateNewChannelOutputDelayForFrame(int expectedFramesSent); | ||
| 158 | static void AdjustForFrame(int expectedFramesSent, bool phaseServo); | ||
| 159 | |||
| 160 | /* | ||
| 161 | * Check to see if the channel output thread is running | ||
| 162 | */ | ||
| 163 | 852553 | int ChannelOutputThreadIsRunning(void) { | |
| 164 | // Acquire, not relaxed: callers wait on this for the thread's startup | ||
| 165 | // (StartingOutput()) to have happened. The main loop asks every pass. | ||
| 166 | 852553 | return ThreadIsRunning.load(std::memory_order_acquire); | |
| 167 | } | ||
| 168 | |||
| 169 | static std::atomic<std::thread::id> outputThreadId{}; | ||
| 170 | 15118 | std::unique_lock<std::mutex> LockOutputThread(void) { | |
| 171 | 15118 | ++outputThreadLockWaiters; | |
| 172 | 15118 | std::unique_lock<std::mutex> lock(outputThreadLock); | |
| 173 | 15118 | --outputThreadLockWaiters; | |
| 174 | 15118 | return lock; | |
| 175 | } | ||
| 176 | |||
| 177 | 1122 | bool IsChannelOutputThread(void) { | |
| 178 | 1122 | return outputThreadId.load(std::memory_order_relaxed) == std::this_thread::get_id(); | |
| 179 | } | ||
| 180 | |||
| 181 | 16540 | int ChannelOutputThreadIsEnabled() { | |
| 182 | 16540 | return OutputFrames.load(std::memory_order_relaxed); | |
| 183 | } | ||
| 184 | |||
| 185 | /* | ||
| 186 | * | ||
| 187 | */ | ||
| 188 | 2 | void DisableChannelOutput(void) { | |
| 189 | 2 | LogDebug(VB_CHANNELOUT, "DisableChannelOutput()\n"); | |
| 190 | 2 | OutputFrames = 0; | |
| 191 | 2 | } | |
| 192 | |||
| 193 | /* | ||
| 194 | * | ||
| 195 | */ | ||
| 196 | 1 | void EnableChannelOutput(void) { | |
| 197 | 1 | LogDebug(VB_CHANNELOUT, "EnableChannelOutput()\n"); | |
| 198 | 1 | OutputFrames = 1; | |
| 199 | 1 | } | |
| 200 | |||
| 201 | // Bumped under outputThreadStatusLock by every ForceChannelOutputNow(). The | ||
| 202 | // thread sends a frame for each change it sees, whichever wait it is in. A | ||
| 203 | // bare notify was lost whenever the thread wasn't waiting on that condition | ||
| 204 | // at that moment -- and the idle wait never listened for it at all -- so a | ||
| 205 | // Stop Now landing within a frame of a sequence hand-over blanked the buffer | ||
| 206 | // and nothing ever sent it: the last frame stayed lit. | ||
| 207 | static std::atomic<uint64_t> outputForceRequests{ 0 }; | ||
| 208 | // Set with a force request by ForceChannelOutputSendNow(): the pass that picks | ||
| 209 | // the request up sends even while output is disabled. | ||
| 210 | static std::atomic<bool> sendWhileDisabledRequested{ false }; | ||
| 211 | |||
| 212 | 36091 | static bool RequestForcedOutput(bool evenIfDisabled) { | |
| 213 | { | ||
| 214 |
1/2✓ Branch 0 taken 36091 times.
✗ Branch 1 not taken.
|
36091 | std::unique_lock<std::mutex> statusLock(outputThreadStatusLock); |
| 215 |
5/6✓ Branch 0 taken 24081 times.
✓ Branch 1 taken 12010 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24081 times.
✓ Branch 4 taken 12010 times.
✓ Branch 5 taken 24081 times.
|
36091 | if (!ThreadIsRunning || !RunThread) { |
| 216 | // Not running, or on its way out: it won't send again. | ||
| 217 | 12010 | return false; | |
| 218 | } | ||
| 219 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 23901 times.
|
24081 | if (evenIfDisabled) { |
| 220 | // Before the request it rides on: the thread reads it on seeing | ||
| 221 | // the request (acquire on outputForceRequests). | ||
| 222 | 180 | sendWhileDisabledRequested.store(true, std::memory_order_relaxed); | |
| 223 | } | ||
| 224 | outputForceRequests.fetch_add(1, std::memory_order_release); | ||
| 225 |
2/2✓ Branch 0 taken 24081 times.
✓ Branch 1 taken 12010 times.
|
36091 | } |
| 226 | 24081 | outputThreadSatusCond.notify_all(); | |
| 227 | 24081 | outputThreadCond.notify_all(); | |
| 228 | 24081 | return true; | |
| 229 | } | ||
| 230 | |||
| 231 | 35911 | bool ForceChannelOutputNow(void) { | |
| 232 | 35911 | LogDebug(VB_CHANNELOUT, "ForceChannelOutputNow()\n"); | |
| 233 | 35911 | return RequestForcedOutput(false); | |
| 234 | } | ||
| 235 | |||
| 236 | 180 | bool ForceChannelOutputSendNow(void) { | |
| 237 | 180 | LogDebug(VB_CHANNELOUT, "ForceChannelOutputSendNow()\n"); | |
| 238 | 180 | return RequestForcedOutput(true); | |
| 239 | } | ||
| 240 | |||
| 241 | 297639 | static inline bool forceOutput() { | |
| 242 | 297639 | return IsEffectRunning() || | |
| 243 |
2/2✓ Branch 0 taken 268043 times.
✓ Branch 1 taken 27850 times.
|
295893 | PixelOverlayManager::INSTANCE.hasActiveOverlays() || |
| 244 | #ifdef HAS_GSTREAMER | ||
| 245 |
2/2✓ Branch 0 taken 268019 times.
✓ Branch 1 taken 24 times.
|
268043 | GStreamerOutput::IsOverlayingVideo() || |
| 246 | #endif | ||
| 247 |
2/2✓ Branch 0 taken 226917 times.
✓ Branch 1 taken 41102 times.
|
268019 | ChannelTester::INSTANCE.Testing() || |
| 248 |
1/2✓ Branch 0 taken 226917 times.
✗ Branch 1 not taken.
|
226917 | alwaysTransmit.load(std::memory_order_relaxed) || |
| 249 |
4/4✓ Branch 0 taken 295893 times.
✓ Branch 1 taken 1746 times.
✓ Branch 2 taken 220411 times.
✓ Branch 3 taken 6506 times.
|
813943 | sequence->hasBridgeData() || |
| 250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220411 times.
|
518050 | outputForced.load(std::memory_order_relaxed); |
| 251 | } | ||
| 252 | |||
| 253 | /* | ||
| 254 | * Main loop in channel output thread | ||
| 255 | */ | ||
| 256 | 2744 | void RunChannelOutputThread(void) { | |
| 257 |
2/4✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2744 times.
✗ Branch 3 not taken.
|
2744 | SetThreadName("FPP-ChannelOut"); |
| 258 | |||
| 259 | // Frame timing must stay consistent under load; a delayed frame bunches | ||
| 260 | // with the next one and doubles the size of the UDP burst hitting the | ||
| 261 | // network. The loop sleeps every frame (and kernel RT throttling caps | ||
| 262 | // RT CPU at ~95%) so this will not starve the rest of the system. | ||
| 263 |
2/4✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2744 times.
✗ Branch 3 not taken.
|
2744 | if (SetThreadRealtimePriority(10)) { |
| 264 |
1/2✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
|
2744 | LogInfo(VB_CHANNELOUT, "Channel output thread running with real-time priority\n"); |
| 265 | } else { | ||
| 266 | ✗ | LogInfo(VB_CHANNELOUT, "Could not set real-time priority on channel output thread; frame timing may jitter under load\n"); | |
| 267 | } | ||
| 268 | |||
| 269 | static long long lastStatTime = 0; | ||
| 270 | long long startTime; | ||
| 271 | long long sendTime; | ||
| 272 | long long readTime; | ||
| 273 | long long processTime; | ||
| 274 |
3/4✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 2677 times.
|
2744 | int onceMore = (getFPPmode() == REMOTE_MODE) ? 20 : 1; |
| 275 | // When the next frame is due (GetMonotonicTimeMicros()), 0 before the | ||
| 276 | // first. Every time in this loop is on the monotonic clock: the cadence | ||
| 277 | // carries nextFrameTime from frame to frame, so on the wall clock a step | ||
| 278 | // back (NTP, an RTC that boots in the past) left the next frame due that | ||
| 279 | // much later, and output stopped for as long as the step. | ||
| 280 | 2744 | long long nextFrameTime = 0; | |
| 281 | 2744 | double frameTimeCarry = 0.0; | |
| 282 | struct timespec ts; | ||
| 283 | struct timeval tv; | ||
| 284 | 2744 | int slowFrameCount = 0; | |
| 285 | |||
| 286 |
1/2✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
|
2744 | LogDebug(VB_CHANNELOUT, "RunChannelOutputThread() starting\n"); |
| 287 | |||
| 288 |
1/2✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
|
2744 | std::unique_lock<std::mutex> lock(outputThreadLock); |
| 289 |
1/2✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
|
2744 | std::unique_lock<std::mutex> statusLock(outputThreadStatusLock); |
| 290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2744 times.
|
2744 | if (outputThreadShutDown) { |
| 291 | // created just before shutdown stopped the output; touch nothing | ||
| 292 | ✗ | return; | |
| 293 | } | ||
| 294 | 2744 | outputThreadId = std::this_thread::get_id(); | |
| 295 | struct ClearThreadId { | ||
| 296 | // Only our own id: this runs after outputThreadLock is released, by | ||
| 297 | // which time a replacement thread may already have stored its id. | ||
| 298 | 2720 | ~ClearThreadId() { | |
| 299 | 2720 | std::thread::id self = std::this_thread::get_id(); | |
| 300 | 2720 | outputThreadId.compare_exchange_strong(self, std::thread::id()); | |
| 301 | 2720 | } | |
| 302 | } clearThreadId; | ||
| 303 | 2744 | ThreadIsRunning = 1; | |
| 304 | 2744 | ThreadIsExiting = 0; | |
| 305 |
1/2✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
|
2744 | statusLock.unlock(); |
| 306 | |||
| 307 |
1/2✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
|
2744 | StartingOutput(); |
| 308 | |||
| 309 |
8/10✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 2677 times.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 65 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 65 times.
✓ Branch 9 taken 2679 times.
|
2744 | if ((getFPPmode() == REMOTE_MODE) && !forceOutput()) { |
| 310 | // Sleep about 2 seconds waiting for the master | ||
| 311 | 65 | int loops = 0; | |
| 312 |
9/10✓ Branch 0 taken 109960 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 109915 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 109915 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 109914 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 109914 times.
✓ Branch 9 taken 65 times.
|
109979 | while ((MasterFramesPlayed < 0) && (loops < 2000) && !forceOutput()) { |
| 313 |
1/2✓ Branch 0 taken 109914 times.
✗ Branch 1 not taken.
|
109914 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 314 | 109914 | loops++; | |
| 315 | } | ||
| 316 | |||
| 317 | // Stop playback if the master hasn't sent any sync packets yet | ||
| 318 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 19 times.
|
65 | if (MasterFramesPlayed < 0) |
| 319 | 46 | RunThread = 0; | |
| 320 | } | ||
| 321 | |||
| 322 | 2744 | bool doForceOutput = false; | |
| 323 | 2744 | bool sendWhileDisabled = false; | |
| 324 | 2744 | uint64_t forceSeen = outputForceRequests.load(); | |
| 325 | // Every frame reads these shared values several times. They are plain | ||
| 326 | // values (frame number, rate, delay, on/off flags) that publish no other | ||
| 327 | // data, so the per-frame reads below are relaxed: each seq_cst access is a | ||
| 328 | // pair of barriers on an AM335x, about 0.1 us apiece. | ||
| 329 | 2744 | constexpr auto relaxed = std::memory_order_relaxed; | |
| 330 |
2/2✓ Branch 0 taken 187657 times.
✓ Branch 1 taken 2720 times.
|
193121 | while (RunThread.load(relaxed)) { |
| 331 |
1/2✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
|
187657 | startTime = GetMonotonicTimeMicros(); |
| 332 | // A forced frame asked for before this pass's send is that send. | ||
| 333 | // Acquire: the request is made after the state it wants sent. | ||
| 334 | 187657 | uint64_t forceNow = outputForceRequests.load(std::memory_order_acquire); | |
| 335 |
2/2✓ Branch 0 taken 23997 times.
✓ Branch 1 taken 163660 times.
|
187657 | if (forceNow != forceSeen) { |
| 336 | 23997 | forceSeen = forceNow; | |
| 337 | 23997 | doForceOutput = true; | |
| 338 | 23997 | sendWhileDisabled = sendWhileDisabledRequested.exchange(false, std::memory_order_relaxed); | |
| 339 | } | ||
| 340 | // Before forceOutput() below: what a start counted by now set up is | ||
| 341 | // seen there, and the idle wait catches any start counted after. | ||
| 342 | // Taken at the wait, it missed a start made while this pass sent and | ||
| 343 | // processed: the effect was set up too late for forceOutput(), the | ||
| 344 | // starter saw a running thread and returned, and the thread idled | ||
| 345 | // out under the effect. Acquire, as for outputForceRequests. | ||
| 346 | 187657 | const uint64_t startsSeen = outputStartRequests.load(std::memory_order_acquire); | |
| 347 |
7/8✓ Branch 0 taken 6028 times.
✓ Branch 1 taken 181629 times.
✓ Branch 2 taken 6028 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5734 times.
✓ Branch 5 taken 294 times.
✓ Branch 6 taken 5734 times.
✓ Branch 7 taken 181923 times.
|
187657 | if (multiSync->isMultiSyncEnabled() && sequence->IsSequenceRunning()) { |
| 348 | // Per-frame string copy under an uncontended leaf lock: the name is | ||
| 349 | // short and the only writers are the open/close paths, so this costs | ||
| 350 | // far less than the sync packet it feeds. | ||
| 351 | 5734 | const unsigned long frame = channelOutputFrame.load(relaxed); | |
| 352 |
2/4✓ Branch 0 taken 5734 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5734 times.
✗ Branch 3 not taken.
|
5734 | multiSync->SendSeqSyncPacket(sequence->GetSeqFilenameCopy(), frame, 1.0 * ((float)frame) / RefreshRate.load(relaxed)); |
| 353 | } | ||
| 354 | |||
| 355 |
1/2✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
|
187657 | doForceOutput |= forceOutput(); |
| 356 |
6/6✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 186586 times.
✓ Branch 2 taken 180 times.
✓ Branch 3 taken 891 times.
✓ Branch 4 taken 186766 times.
✓ Branch 5 taken 891 times.
|
187657 | if (OutputFrames.load(relaxed) || sendWhileDisabled) { |
| 357 | 186766 | sendWhileDisabled = false; | |
| 358 |
7/8✓ Branch 0 taken 171656 times.
✓ Branch 1 taken 15110 times.
✓ Branch 2 taken 171656 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 171653 times.
✓ Branch 6 taken 15113 times.
✓ Branch 7 taken 171653 times.
|
186766 | if (!sequence->isDataProcessed() || sequence->hasBridgeData()) { |
| 359 | // first time through or immediately after sequence load, the data might not be | ||
| 360 | // processed yet, need to do it | ||
| 361 | 15113 | int msTime = 1000.0 * channelOutputFrame.load(relaxed) / RefreshRate.load(relaxed); | |
| 362 |
3/4✓ Branch 0 taken 15113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12406 times.
✓ Branch 3 taken 2707 times.
|
15113 | if (!sequence->IsSequenceRunning()) { |
| 363 | 12406 | msTime = mediaElapsedSeconds.load(relaxed) * 1000; | |
| 364 | } | ||
| 365 |
1/2✓ Branch 0 taken 15113 times.
✗ Branch 1 not taken.
|
15113 | sequence->ProcessSequenceData(msTime); |
| 366 | } | ||
| 367 |
7/8✓ Branch 0 taken 186766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5804 times.
✓ Branch 3 taken 180962 times.
✓ Branch 4 taken 5197 times.
✓ Branch 5 taken 607 times.
✓ Branch 6 taken 5197 times.
✓ Branch 7 taken 181569 times.
|
186766 | if (getFPPmode() == REMOTE_MODE && !doForceOutput) { |
| 368 | // Sleep about 1 seconds waiting for the master | ||
| 369 | 5197 | int loops = 0; | |
| 370 |
8/10✓ Branch 0 taken 3614 times.
✓ Branch 1 taken 5194 times.
✓ Branch 2 taken 3611 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3611 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3611 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3611 times.
✓ Branch 9 taken 5197 times.
|
14005 | while ((MasterFramesPlayed.load(relaxed) < 0) && (loops < 1000) && !sequence->hasBridgeData()) { |
| 371 |
1/2✓ Branch 0 taken 3611 times.
✗ Branch 1 not taken.
|
3611 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 372 | 3611 | loops++; | |
| 373 | } | ||
| 374 | } | ||
| 375 |
1/2✓ Branch 0 taken 186766 times.
✗ Branch 1 not taken.
|
186766 | sequence->SendSequenceData(); |
| 376 | } | ||
| 377 | |||
| 378 |
1/2✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
|
187657 | sendTime = GetMonotonicTimeMicros(); |
| 379 | |||
| 380 |
7/8✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86357 times.
✓ Branch 3 taken 101300 times.
✓ Branch 4 taken 79903 times.
✓ Branch 5 taken 6454 times.
✓ Branch 6 taken 181203 times.
✓ Branch 7 taken 6454 times.
|
187657 | if (sequence->IsSequenceRunning() || (onceMore >= 1)) { |
| 381 |
3/4✓ Branch 0 taken 181203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101300 times.
✓ Branch 3 taken 79903 times.
|
181203 | if (sequence->IsSequenceRunning()) { |
| 382 | // Taken in one step: media sync sets it from another thread, | ||
| 383 | // and a skip it set between a read and a reset was lost. | ||
| 384 | // The plain load first keeps the exchange (a locked RMW) | ||
| 385 | // off the frames with nothing to skip, which is nearly all. | ||
| 386 |
2/2✓ Branch 0 taken 15476 times.
✓ Branch 1 taken 85824 times.
|
101300 | if (FrameSkip.load(relaxed) != 0) { |
| 387 |
1/2✓ Branch 0 taken 15476 times.
✗ Branch 1 not taken.
|
15476 | sequence->SkipFrames(FrameSkip); |
| 388 | } | ||
| 389 | } | ||
| 390 |
1/2✓ Branch 0 taken 181203 times.
✗ Branch 1 not taken.
|
181203 | sequence->ReadSequenceData(); |
| 391 | } | ||
| 392 |
1/2✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
|
187657 | readTime = GetMonotonicTimeMicros(); |
| 393 | |||
| 394 | 187657 | int msTime = 1000.0 * channelOutputFrame.load(relaxed) / RefreshRate.load(relaxed); | |
| 395 |
3/4✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87461 times.
✓ Branch 3 taken 100196 times.
|
187657 | if (!sequence->IsSequenceRunning()) { |
| 396 | 87461 | msTime = mediaElapsedSeconds.load(relaxed) * 1000; | |
| 397 | } | ||
| 398 |
3/4✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 181118 times.
✓ Branch 3 taken 6539 times.
|
187657 | if (!sequence->hasBridgeData()) { |
| 399 | // if bridging, we'll have to process later | ||
| 400 |
1/2✓ Branch 0 taken 181118 times.
✗ Branch 1 not taken.
|
181118 | sequence->ProcessSequenceData(msTime); |
| 401 | } | ||
| 402 |
1/2✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
|
187657 | processTime = GetMonotonicTimeMicros(); |
| 403 | |||
| 404 | 187657 | long long totalTime = processTime - startTime; | |
| 405 |
2/2✓ Branch 0 taken 262 times.
✓ Branch 1 taken 187395 times.
|
187657 | if (totalTime > 150000) { |
| 406 | // very slow, log immediately (this was 3, which the "> 3" test | ||
| 407 | // below didn't pass, so it waited for the next slow frame) | ||
| 408 | 262 | slowFrameCount = 4; | |
| 409 |
2/2✓ Branch 0 taken 1277 times.
✓ Branch 1 taken 186118 times.
|
187395 | } else if (totalTime > 50000) { |
| 410 | // could be a very transient blip, we'll log if | ||
| 411 | // it happens 3 frames in a row | ||
| 412 | 1277 | slowFrameCount++; | |
| 413 | } else { | ||
| 414 | 186118 | slowFrameCount = 0; | |
| 415 | } | ||
| 416 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 187317 times.
|
187657 | if (slowFrameCount > 3) { |
| 417 | // Once slow, every frame is slow: logging each one wrote a line | ||
| 418 | // per frame to the SD card for as long as it lasted. Log when it | ||
| 419 | // starts and then every 10 s, counting the slow frames between. | ||
| 420 | static long long lastSlowLogUS = 0; | ||
| 421 | static int slowFramesSinceLog = 0; | ||
| 422 | 340 | slowFramesSinceLog++; | |
| 423 |
4/4✓ Branch 0 taken 330 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 322 times.
|
340 | if (!lastSlowLogUS || processTime - lastSlowLogUS >= 10000000) { |
| 424 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
|
54 | LogWarn(VB_CHANNELOUT, |
| 425 | "SLOW Output Thread: Loop: %dus, Send: %lldus, Read: %lldus, Process: %lldus, FrameNum: %ld (%d slow frames%s)\n", | ||
| 426 | LightDelay.load(), | ||
| 427 | sendTime - startTime, | ||
| 428 | readTime - sendTime, | ||
| 429 | processTime - readTime, | ||
| 430 | channelOutputFrame.load(), | ||
| 431 | slowFramesSinceLog, lastSlowLogUS ? " in the last 10 s" : ""); | ||
| 432 | 18 | lastSlowLogUS = processTime; | |
| 433 | 18 | slowFramesSinceLog = 0; | |
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 |
1/2✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
|
187657 | statusLock.lock(); |
| 438 |
9/12✓ Branch 0 taken 187657 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87464 times.
✓ Branch 3 taken 100193 times.
✓ Branch 4 taken 11219 times.
✓ Branch 5 taken 76245 times.
✓ Branch 6 taken 11219 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 11219 times.
✓ Branch 10 taken 176438 times.
✓ Branch 11 taken 11219 times.
|
187657 | if (sequence->IsSequenceRunning() || doForceOutput || sequence->hasBridgeData()) { |
| 439 | // REMOTE mode keeps looping a few extra times before we blank | ||
| 440 |
3/4✓ Branch 0 taken 176438 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5642 times.
✓ Branch 3 taken 170796 times.
|
176438 | onceMore = (getFPPmode() == REMOTE_MODE) ? 20 : 1; |
| 441 | 176438 | int sleepTime = LightDelay.load(relaxed) - (processTime - startTime); | |
| 442 |
8/8✓ Branch 0 taken 99298 times.
✓ Branch 1 taken 77140 times.
✓ Branch 2 taken 99265 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 2807 times.
✓ Branch 5 taken 96458 times.
✓ Branch 6 taken 79980 times.
✓ Branch 7 taken 96458 times.
|
176438 | if ((channelOutputFrame.load(relaxed) <= 1) || (sleepTime <= 0) || (startTime > (lastStatTime + 1000000))) { |
| 443 |
2/2✓ Branch 0 taken 3163 times.
✓ Branch 1 taken 76817 times.
|
79980 | if (sleepTime < 0) |
| 444 | 3163 | sleepTime = 0; | |
| 445 |
2/2✓ Branch 0 taken 8718 times.
✓ Branch 1 taken 71262 times.
|
79980 | if (startTime > (lastStatTime + 1000000)) { |
| 446 | 8718 | lastStatTime = startTime; | |
| 447 | } | ||
| 448 |
1/2✓ Branch 0 taken 79980 times.
✗ Branch 1 not taken.
|
159960 | LogDebug(VB_CHANNELOUT, |
| 449 | "Output Thread: Loop: %dus, Send: %lldus, Read: %lldus, Process: %lldus, Sleep: %dus, FrameNum: %ld\n", | ||
| 450 | LightDelay.load(), | ||
| 451 | sendTime - startTime, | ||
| 452 | readTime - sendTime, | ||
| 453 | processTime - readTime, | ||
| 454 | sleepTime, channelOutputFrame.load()); | ||
| 455 | } | ||
| 456 | } else { | ||
| 457 | 11219 | LightDelay = BridgeLightDelay.load(); | |
| 458 |
2/2✓ Branch 0 taken 7744 times.
✓ Branch 1 taken 3475 times.
|
11219 | if (onceMore) { |
| 459 | 7744 | onceMore--; | |
| 460 | } else { | ||
| 461 | // we will wait up to 2500ms to see if the thread is still needed | ||
| 462 | 3475 | ThreadIsExiting = 1; | |
| 463 | // On a start request, counted under statusLock, not on any | ||
| 464 | // notify: StartChannelOutputThread() used to notify without | ||
| 465 | // the lock, and a notify that landed before this wait began | ||
| 466 | // was lost - the start then spawned a replacement and this | ||
| 467 | // thread's timeout below cleared RunThread under it. | ||
| 468 | // Shutdown ends the wait too: left to time out, every fppd | ||
| 469 | // stop took the rest of the 2.5 s. | ||
| 470 | // | ||
| 471 | // outputThreadLock is let go for the wait: nothing walks the | ||
| 472 | // output list or sends while idle, and an output reload | ||
| 473 | // (LockOutputThread() on the main loop) landing just after a | ||
| 474 | // test or sequence stopped otherwise held the main loop for | ||
| 475 | // the rest of the 2.5 s, so the UI's command-socket calls got | ||
| 476 | // no answer. ThreadIsRunning stays set, so outputs added | ||
| 477 | // meanwhile get their StartingOutput() and the StoppingOutput() | ||
| 478 | // at exit still pairs with it. Retaken below, after statusLock | ||
| 479 | // is let go: outputThreadLock is always taken first. | ||
| 480 |
1/2✓ Branch 0 taken 3475 times.
✗ Branch 1 not taken.
|
3475 | lock.unlock(); |
| 481 | 7412 | auto wanted = [startsSeen, forceSeen]() { | |
| 482 | 6245 | return outputThreadShutDown || | |
| 483 |
4/4✓ Branch 0 taken 6245 times.
✓ Branch 1 taken 1167 times.
✓ Branch 2 taken 5139 times.
✓ Branch 3 taken 1106 times.
|
18796 | outputStartRequests.load() != startsSeen || |
| 484 |
2/2✓ Branch 0 taken 681 times.
✓ Branch 1 taken 4458 times.
|
12551 | outputForceRequests.load() != forceSeen; |
| 485 | 3475 | }; | |
| 486 |
3/4✓ Branch 0 taken 3453 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2954 times.
✓ Branch 3 taken 499 times.
|
6429 | bool keepGoing = outputThreadSatusCond.wait_for(statusLock, std::chrono::milliseconds(2500), wanted) && |
| 487 |
2/2✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 1167 times.
|
2954 | !outputThreadShutDown; |
| 488 |
2/2✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 1787 times.
|
3453 | if (!keepGoing) { |
| 489 | // Timed out. Retake outputThreadLock before deciding to | ||
| 490 | // exit, and count as running meanwhile: a start in this | ||
| 491 | // gap then leaves its request to this thread rather than | ||
| 492 | // spawning a replacement, which could take the lock first | ||
| 493 | // and run while this one goes on to StoppingOutput(). | ||
| 494 | 1666 | ThreadIsExiting = 0; | |
| 495 |
1/2✓ Branch 0 taken 1666 times.
✗ Branch 1 not taken.
|
1666 | statusLock.unlock(); |
| 496 |
1/2✓ Branch 0 taken 1666 times.
✗ Branch 1 not taken.
|
1666 | lock.lock(); |
| 497 |
1/2✓ Branch 0 taken 1666 times.
✗ Branch 1 not taken.
|
1666 | statusLock.lock(); |
| 498 |
3/4✓ Branch 0 taken 499 times.
✓ Branch 1 taken 1167 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 499 times.
|
1666 | keepGoing = !outputThreadShutDown && wanted(); |
| 499 | } | ||
| 500 |
2/2✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 1666 times.
|
3453 | if (keepGoing) { |
| 501 | // signal to keep going | ||
| 502 | 1787 | ThreadIsExiting = 0; | |
| 503 |
1/2✓ Branch 0 taken 1787 times.
✗ Branch 1 not taken.
|
1787 | statusLock.unlock(); |
| 504 | 1787 | outputThreadSatusCond.notify_all(); | |
| 505 |
1/2✓ Branch 0 taken 1787 times.
✗ Branch 1 not taken.
|
1787 | if (!lock.owns_lock()) { |
| 506 |
1/2✓ Branch 0 taken 1787 times.
✗ Branch 1 not taken.
|
1787 | lock.lock(); |
| 507 | } | ||
| 508 | 1787 | onceMore = 1; | |
| 509 | // Back from idle: the cadence starts again at this wake. | ||
| 510 | // Left to the "more than a frame behind" test below, a | ||
| 511 | // start within a frame of the last idle pass (a playlist's | ||
| 512 | // next sequence, as the last one's blanking ends) kept the | ||
| 513 | // old phase: the first frame went out at the wake and the | ||
| 514 | // second at the old tick, a few ms later, and the | ||
| 515 | // sequence's timing sync then slowed the next frames by up | ||
| 516 | // to 15 ms each to give the lead back. | ||
| 517 | 1787 | nextFrameTime = 0; | |
| 518 | 1787 | continue; | |
| 519 | } else { | ||
| 520 | 1666 | RunThread = 0; | |
| 521 | 1666 | ThreadIsExiting = 1; | |
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 |
1/2✓ Branch 0 taken 185848 times.
✗ Branch 1 not taken.
|
185848 | statusLock.unlock(); |
| 526 | 185848 | doForceOutput = false; | |
| 527 | // Frames are due on a fixed cadence: each one LightDelay after the | ||
| 528 | // previous one was due, not after this pass happened to start. Waking | ||
| 529 | // is always a little late, and measuring from the wake-up added that | ||
| 530 | // to every frame: on a Pi 5 the lights ran 125 ppm slow, and media | ||
| 531 | // sync, which only acts on whole frames, let them fall behind the | ||
| 532 | // sound by tens of ms over a song before correcting. | ||
| 533 | // The period is LightDelay of media time (see SetMediaClockRate()), | ||
| 534 | // kept to a fraction of a microsecond so 100 fps doesn't round it off. | ||
| 535 | 185848 | const int lightDelay = LightDelay.load(relaxed); | |
| 536 |
1/2✓ Branch 0 taken 185848 times.
✗ Branch 1 not taken.
|
185848 | double period = lightDelay / CurrentMediaClockRate(startTime); |
| 537 |
4/4✓ Branch 0 taken 180167 times.
✓ Branch 1 taken 5681 times.
✓ Branch 2 taken 457 times.
✓ Branch 3 taken 179710 times.
|
185848 | if (nextFrameTime == 0 || startTime >= nextFrameTime + lightDelay) { |
| 538 | // First frame, or more than a frame behind (back from idle, or a | ||
| 539 | // stall): start the cadence again here rather than racing to catch up. | ||
| 540 | 6138 | nextFrameTime = startTime + lightDelay; | |
| 541 | 6138 | frameTimeCarry = 0.0; | |
| 542 |
2/2✓ Branch 0 taken 156550 times.
✓ Branch 1 taken 23160 times.
|
179710 | } else if (startTime >= nextFrameTime) { |
| 543 |
1/2✓ Branch 0 taken 156550 times.
✗ Branch 1 not taken.
|
156550 | frameTimeCarry += period + TakeMediaPhaseStep(startTime, period); |
| 544 | 156550 | long long whole = (long long)std::floor(frameTimeCarry); | |
| 545 | 156550 | frameTimeCarry -= whole; | |
| 546 | 156550 | nextFrameTime += whole; | |
| 547 | } | ||
| 548 | // else: a forced wake ran this pass early; the regular frame stays due | ||
| 549 | // when it was. | ||
| 550 |
3/4✓ Branch 0 taken 185848 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 100194 times.
✓ Branch 3 taken 85654 times.
|
185848 | if (sequence->IsSequenceRunning()) { |
| 551 | // The frame just read goes out at the next wake. | ||
| 552 |
1/2✓ Branch 0 taken 100194 times.
✗ Branch 1 not taken.
|
100194 | PublishNextSend((long)channelOutputFrame.load(relaxed), nextFrameTime); |
| 553 | } | ||
| 554 |
1/2✓ Branch 0 taken 185848 times.
✗ Branch 1 not taken.
|
185848 | long dt = (nextFrameTime - GetMonotonicTimeMicros()) * 1000; |
| 555 |
6/6✓ Branch 0 taken 184160 times.
✓ Branch 1 taken 1688 times.
✓ Branch 2 taken 178599 times.
✓ Branch 3 taken 5561 times.
✓ Branch 4 taken 178599 times.
✓ Branch 5 taken 7249 times.
|
185848 | if (RunThread.load(relaxed) && dt > 0) { |
| 556 | // With no sequence running the wait is the bridging interval | ||
| 557 | // (50 ms by default). A sequence starting meanwhile ends it: its | ||
| 558 | // first frame goes out now, not at the end of that interval. A | ||
| 559 | // playlist's next sequence starts just after the last one ends (as | ||
| 560 | // its song's sound comes in right behind the last one's), and | ||
| 561 | // left to the interval its lights started up to 50 ms behind | ||
| 562 | // their sound. While a sequence runs this never fires. | ||
| 563 |
1/2✓ Branch 0 taken 178599 times.
✗ Branch 1 not taken.
|
178599 | const bool runningAtSleep = sequence->IsSequenceRunning(); |
| 564 |
3/4✓ Branch 0 taken 178597 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24457 times.
✓ Branch 3 taken 154140 times.
|
178599 | if (outputThreadCond.wait_for(lock, std::chrono::nanoseconds(dt), |
| 565 | 569332 | [&forceSeen, runningAtSleep]() { | |
| 566 |
2/2✓ Branch 0 taken 546081 times.
✓ Branch 1 taken 23251 times.
|
1115413 | return outputForceRequests.load() != forceSeen || |
| 567 |
4/4✓ Branch 0 taken 354297 times.
✓ Branch 1 taken 191784 times.
✓ Branch 2 taken 1206 times.
✓ Branch 3 taken 353091 times.
|
1115413 | (!runningAtSleep && sequence->IsSequenceRunning()); |
| 568 | })) { | ||
| 569 |
7/8✓ Branch 0 taken 8512 times.
✓ Branch 1 taken 15945 times.
✓ Branch 2 taken 8512 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1206 times.
✓ Branch 5 taken 7306 times.
✓ Branch 6 taken 1206 times.
✓ Branch 7 taken 23251 times.
|
24457 | if (!runningAtSleep && sequence->IsSequenceRunning()) { |
| 570 | // The sequence's cadence starts with this pass. | ||
| 571 | 1206 | nextFrameTime = 0; | |
| 572 | } else { | ||
| 573 |
1/2✓ Branch 0 taken 23251 times.
✗ Branch 1 not taken.
|
23251 | LogDebug(VB_CHANNELOUT, "Forced output\n"); |
| 574 | } | ||
| 575 | } | ||
| 576 | } | ||
| 577 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 185842 times.
|
185846 | if (outputThreadLockWaiters.load(relaxed)) { |
| 578 | // Another thread is waiting in LockOutputThread() (an output | ||
| 579 | // reload). The wait above lets it in, but not while the next | ||
| 580 | // frame is already due, and while every cycle runs over its frame | ||
| 581 | // time that is every pass. Let go until it has the lock: a bare | ||
| 582 | // unlock() and lock() would most likely take it straight back. | ||
| 583 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | lock.unlock(); |
| 584 |
5/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
|
16 | for (int i = 0; i < 100 && outputThreadLockWaiters.load(); ++i) { |
| 585 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | std::this_thread::sleep_for(std::chrono::microseconds(500)); |
| 586 | } | ||
| 587 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | lock.lock(); |
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | // StoppingOutput() fires the OUTPUTS_DISABLED presets, which run arbitrary | ||
| 592 | // commands on this thread. It must run while outputThreadLock is still held | ||
| 593 | // so a replacement thread's StartingOutput() cannot interleave with it (a | ||
| 594 | // disable landing after the new thread's enable would leave outputs dark), | ||
| 595 | // but ThreadIsRunning must still be set and statusLock must be released. | ||
| 596 | // Previously ThreadIsRunning was cleared first: a preset that reached | ||
| 597 | // StartChannelOutputThread() spawned a replacement that blocked on | ||
| 598 | // outputThreadLock (held here) while the preset spun on | ||
| 599 | // ChannelOutputThreadIsRunning() -- which only the blocked replacement | ||
| 600 | // could set -- wedging this thread, the replacement, and then the main | ||
| 601 | // loop at the next config reload. With ThreadIsRunning still set, such a | ||
| 602 | // preset queues its replacement on outputThreadLock (it runs once this | ||
| 603 | // thread releases) and its startup wait returns immediately. | ||
| 604 | // ThreadIsExiting is set so a Start during this window falls through to | ||
| 605 | // spawn rather than early-returning against a thread already on its way | ||
| 606 | // out. | ||
| 607 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | statusLock.lock(); |
| 608 | 2720 | ThreadIsExiting = 1; | |
| 609 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | statusLock.unlock(); |
| 610 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | StoppingOutput(); |
| 611 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | statusLock.lock(); |
| 612 | 2720 | ThreadIsRunning = 0; | |
| 613 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | statusLock.unlock(); |
| 614 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | lock.unlock(); |
| 615 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | statusLock.lock(); |
| 616 | 2720 | ThreadIsExiting = 0; | |
| 617 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | statusLock.unlock(); |
| 618 | 2720 | outputThreadSatusCond.notify_all(); | |
| 619 | |||
| 620 |
1/2✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
|
2720 | LogDebug(VB_CHANNELOUT, "RunChannelOutputThread() completed\n"); |
| 621 |
2/4✓ Branch 0 taken 2720 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2720 times.
✗ Branch 3 not taken.
|
2720 | } |
| 622 | |||
| 623 | /* | ||
| 624 | * Set the step time | ||
| 625 | */ | ||
| 626 | 3530 | void SetChannelOutputRefreshRate(float rate) { | |
| 627 | // Under the status lock, because the output thread sets LightDelay to the | ||
| 628 | // bridging interval, under that lock, when it finds no sequence running. | ||
| 629 | // Unlocked, that store could land just after this one when a sequence was | ||
| 630 | // starting, and the whole sequence then ran at the bridging interval | ||
| 631 | // (50ms by default) instead of its own frame time. StartSequence() marks | ||
| 632 | // the sequence running before calling this, so the thread either sees it | ||
| 633 | // running or finishes its store first. | ||
| 634 |
1/2✓ Branch 0 taken 3530 times.
✗ Branch 1 not taken.
|
3530 | std::unique_lock<std::mutex> statusLock(outputThreadStatusLock); |
| 635 | 3530 | RefreshRate = rate; | |
| 636 | 3530 | SequenceLightDelay = 1000000 / RefreshRate; | |
| 637 | 3530 | LightDelay = SequenceLightDelay.load(); | |
| 638 | 3530 | } | |
| 639 | 28153 | float GetChannelOutputRefreshRate() { | |
| 640 | 28153 | return RefreshRate; | |
| 641 | } | ||
| 642 | |||
| 643 | /* | ||
| 644 | * Kick off the channel output thread | ||
| 645 | */ | ||
| 646 | 234188 | void StartChannelOutputThread(void) { | |
| 647 | 234188 | LogDebug(VB_CHANNELOUT, "StartChannelOutputThread()\n"); | |
| 648 | |||
| 649 |
2/2✓ Branch 0 taken 2649 times.
✓ Branch 1 taken 231539 times.
|
234188 | if (BridgeLightDelay == 0) { |
| 650 | 2649 | alwaysTransmit = getSettingInt("alwaysTransmit"); | |
| 651 | 2649 | BridgeLightDelay = getSettingInt("E131BridgingInterval", 50) * 1000; | |
| 652 |
3/6✓ Branch 0 taken 2649 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2649 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2649 times.
✗ Branch 5 not taken.
|
12950 | registerSettingsListener("ChannelOutputThread", "alwaysTransmit", [](const std::string& value) { |
| 653 | ✗ | alwaysTransmit = getSettingInt("alwaysTransmit"); | |
| 654 | ✗ | }); | |
| 655 |
3/6✓ Branch 0 taken 2649 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2649 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2649 times.
✗ Branch 5 not taken.
|
12950 | registerSettingsListener("ChannelOutputThread", "E131BridgingInterval", [](const std::string& value) { |
| 656 | 1 | BridgeLightDelay = getSettingInt("E131BridgingInterval", 50) * 1000; | |
| 657 | 1 | }); | |
| 658 | } | ||
| 659 |
2/2✓ Branch 0 taken 234062 times.
✓ Branch 1 taken 126 times.
|
234188 | if (getFPPmode() & PLAYER_MODE) { |
| 660 | 234062 | int mediaOffsetInt = getSettingInt("mediaOffset"); | |
| 661 |
2/2✓ Branch 0 taken 19602 times.
✓ Branch 1 taken 214460 times.
|
234062 | if (mediaOffsetInt) { |
| 662 | 19602 | mediaOffset = (float)mediaOffsetInt * 0.001; | |
| 663 | } else { | ||
| 664 | 214460 | mediaOffset = 0.0; | |
| 665 | } | ||
| 666 | 234062 | LogDebug(VB_MEDIAOUT, "Using mediaOffset of %.3f\n", mediaOffset.load()); | |
| 667 | } | ||
| 668 | |||
| 669 | { | ||
| 670 |
1/2✓ Branch 0 taken 234188 times.
✗ Branch 1 not taken.
|
234188 | std::unique_lock<std::mutex> lock(outputThreadStatusLock); |
| 671 | 234188 | ++outputStartRequests; | |
| 672 | 234188 | } | |
| 673 | 234188 | outputThreadSatusCond.notify_all(); | |
| 674 | // And out of the frame wait, if a sequence has just started (see there). | ||
| 675 | // Not under outputThreadLock: callers may hold the sequence lock, which | ||
| 676 | // the thread takes while holding that one. A notify that slips in just | ||
| 677 | // before the thread waits only leaves the wait to run its course. | ||
| 678 | 234188 | outputThreadCond.notify_all(); | |
| 679 |
2/2✓ Branch 0 taken 231440 times.
✓ Branch 1 taken 2748 times.
|
234188 | if (ChannelOutputThreadIsRunning()) { |
| 680 | // Give a little time in case we were shutting down | ||
| 681 |
1/2✓ Branch 0 taken 231440 times.
✗ Branch 1 not taken.
|
231440 | std::unique_lock<std::mutex> lock(outputThreadStatusLock); |
| 682 |
2/2✓ Branch 0 taken 895 times.
✓ Branch 1 taken 230545 times.
|
231440 | if (ThreadIsExiting) { |
| 683 |
1/2✓ Branch 0 taken 895 times.
✗ Branch 1 not taken.
|
895 | outputThreadSatusCond.wait_for(lock, std::chrono::milliseconds(10)); |
| 684 | } | ||
| 685 |
5/6✓ Branch 0 taken 231440 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 231439 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 231439 times.
✓ Branch 5 taken 1 times.
|
231440 | if (ThreadIsRunning && !ThreadIsExiting) { |
| 686 |
1/2✓ Branch 0 taken 231439 times.
✗ Branch 1 not taken.
|
231439 | LogDebug(VB_CHANNELOUT, "Channel Output thread is already running\n"); |
| 687 | 231439 | return; | |
| 688 | } | ||
| 689 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 231439 times.
|
231440 | } |
| 690 | |||
| 691 | { | ||
| 692 | // Checked and started under the lock StopChannelOutputThread() latches | ||
| 693 | // outputThreadShutDown under, so a start can't slip in after it. | ||
| 694 |
1/2✓ Branch 0 taken 2749 times.
✗ Branch 1 not taken.
|
2749 | std::unique_lock<std::mutex> lock(outputThreadStatusLock); |
| 695 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2744 times.
|
2749 | if (outputThreadShutDown) { |
| 696 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | LogDebug(VB_CHANNELOUT, "Not starting the channel output thread: fppd is shutting down\n"); |
| 697 | 5 | return; | |
| 698 | } | ||
| 699 | 2744 | RunThread = 1; | |
| 700 | 2744 | ThreadIsExiting = 0; | |
| 701 | // The thread is detached immediately (as the pthread version was via | ||
| 702 | // pthread_detach) — its lifecycle is coordinated through the | ||
| 703 | // RunThread/ThreadIsRunning/ThreadIsExiting flags and condition | ||
| 704 | // variables, never by joining. std::thread reports creation failure | ||
| 705 | // by throwing std::system_error rather than returning an errno. | ||
| 706 | try { | ||
| 707 |
2/4✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2744 times.
✗ Branch 3 not taken.
|
2744 | std::thread(RunChannelOutputThread).detach(); |
| 708 | ✗ | } catch (const std::system_error& ex) { | |
| 709 | ✗ | RunThread = 0; | |
| 710 | ✗ | LogErr(VB_CHANNELOUT, "ERROR creating channel output thread: %s (code %d)\n", | |
| 711 | ex.what(), ex.code().value()); | ||
| 712 | ✗ | return; | |
| 713 | ✗ | } | |
| 714 |
2/2✓ Branch 0 taken 2744 times.
✓ Branch 1 taken 5 times.
|
2749 | } |
| 715 | |||
| 716 | // Wait for thread to start, but bounded (mirrors StopChannelOutputThread's | ||
| 717 | // cap): if the new thread cannot come up — e.g. it is queued on | ||
| 718 | // outputThreadLock behind an exiting thread that is stuck in a slow | ||
| 719 | // OUTPUTS_DISABLED command — spinning here forever would pin the calling | ||
| 720 | // thread (which can be an HTTP handler or a command preset) as well. | ||
| 721 | 2744 | int waitLoops = 0; | |
| 722 |
5/6✓ Branch 0 taken 2693 times.
✓ Branch 1 taken 2744 times.
✓ Branch 2 taken 2693 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2693 times.
✓ Branch 5 taken 2744 times.
|
5437 | while (!ChannelOutputThreadIsRunning() && (waitLoops++ < 1000)) |
| 723 |
1/2✓ Branch 0 taken 2693 times.
✗ Branch 1 not taken.
|
2693 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 724 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2744 times.
|
2744 | if (!ChannelOutputThreadIsRunning()) |
| 725 | ✗ | LogErr(VB_CHANNELOUT, "Channel output thread did not start within 5 seconds\n"); | |
| 726 | } | ||
| 727 | |||
| 728 | /* | ||
| 729 | * | ||
| 730 | */ | ||
| 731 | 2620 | int StopChannelOutputThread(void) { | |
| 732 |
1/2✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
|
2620 | LogDebug(VB_CHANNELOUT, "StopChannelOutputThread()\n"); |
| 733 | |||
| 734 | // Stop the thread for good (see outputThreadShutDown), wake it from | ||
| 735 | // whichever wait it is in, and wait a few seconds for it to exit (it | ||
| 736 | // notifies outputThreadSatusCond once ThreadIsRunning is clear). | ||
| 737 |
1/2✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
|
2620 | std::unique_lock<std::mutex> lock(outputThreadStatusLock); |
| 738 | 2620 | outputThreadShutDown = true; | |
| 739 | 2620 | RunThread = 0; | |
| 740 | 2620 | outputThreadSatusCond.notify_all(); | |
| 741 | 2620 | outputThreadCond.notify_all(); | |
| 742 |
2/4✓ Branch 0 taken 2620 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2620 times.
|
7422 | if (!outputThreadSatusCond.wait_for(lock, std::chrono::seconds(5), []() { return !ThreadIsRunning; })) { |
| 743 | // Didn't stop for some reason, so it was hung somewhere | ||
| 744 | ✗ | return -1; | |
| 745 | } | ||
| 746 | 2620 | return 0; | |
| 747 | 2620 | } | |
| 748 | |||
| 749 | ✗ | void StartForcingChannelOutput(void) { | |
| 750 | ✗ | outputForced = outputForced + 1; | |
| 751 | |||
| 752 | ✗ | StartChannelOutputThread(); | |
| 753 | ✗ | } | |
| 754 | |||
| 755 | ✗ | void StopForcingChannelOutput(void) { | |
| 756 | ✗ | outputForced = outputForced - 1; | |
| 757 | |||
| 758 | ✗ | if (outputForced < 0) | |
| 759 | ✗ | outputForced = 0; | |
| 760 | ✗ | } | |
| 761 | |||
| 762 | // Where the master's recent sync packets put its frame 0 on our clock. A | ||
| 763 | // packet can arrive late -- the master's own loop, the network, this box's | ||
| 764 | // scheduling -- but never early, so the earliest of the last couple of | ||
| 765 | // seconds is the master's timeline; following each packet as it came put | ||
| 766 | // half of every delay on the lights, frame to frame. | ||
| 767 | static constexpr long long kMasterWindowUs = 2000000; | ||
| 768 | struct MasterEpoch { | ||
| 769 | long long at; | ||
| 770 | double frame0Us; | ||
| 771 | }; | ||
| 772 | static std::mutex masterEpochLock; | ||
| 773 | static std::deque<MasterEpoch> masterEpochs; | ||
| 774 | static int masterLastFrame = -1; | ||
| 775 | static double masterFrameUs = 0.0; | ||
| 776 | |||
| 777 | /* | ||
| 778 | * Reset the master frames played position | ||
| 779 | */ | ||
| 780 | 103 | void ResetMasterPosition(void) { | |
| 781 | 103 | MasterFramesPlayed = -1; | |
| 782 |
1/2✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
|
103 | std::unique_lock<std::mutex> l(masterEpochLock); |
| 783 | 103 | masterEpochs.clear(); | |
| 784 | 103 | } | |
| 785 | |||
| 786 | 2447 | void CancelFrameSkip(void) { | |
| 787 | 2447 | FrameSkip = 0; | |
| 788 | 2447 | } | |
| 789 | |||
| 790 | // The master's frame 0 on our clock, from the packet that put it at | ||
| 791 | // framePosition (frameNumber rounded) arriving now, and the ones before it. | ||
| 792 | 722 | static double MasterFrame0(int frameNumber, double framePosition, long long now, double frameUs) { | |
| 793 | 722 | double frame0 = now - framePosition * frameUs; | |
| 794 |
1/2✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
|
722 | std::unique_lock<std::mutex> l(masterEpochLock); |
| 795 |
4/4✓ Branch 0 taken 703 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 698 times.
|
722 | if (frameUs != masterFrameUs || frameNumber < masterLastFrame) { |
| 796 | // Another rate, or the master went back: a new timeline. | ||
| 797 | 24 | masterEpochs.clear(); | |
| 798 | } | ||
| 799 | 722 | masterFrameUs = frameUs; | |
| 800 | 722 | masterLastFrame = frameNumber; | |
| 801 |
6/6✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 584 times.
✓ Branch 3 taken 696 times.
✓ Branch 4 taken 584 times.
✓ Branch 5 taken 722 times.
|
1306 | while (!masterEpochs.empty() && now - masterEpochs.front().at > kMasterWindowUs) { |
| 802 | 584 | masterEpochs.pop_front(); | |
| 803 | } | ||
| 804 | 722 | double best = frame0; | |
| 805 |
2/2✓ Branch 0 taken 9688 times.
✓ Branch 1 taken 722 times.
|
10410 | for (auto& e : masterEpochs) { |
| 806 | 9688 | best = std::min(best, e.frame0Us); | |
| 807 | } | ||
| 808 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 722 times.
|
722 | if (frame0 - best > 3 * frameUs) { |
| 809 | // Far later than all of them: the master held or paused, or this | ||
| 810 | // packet is very late. Start over from it; a late one is replaced | ||
| 811 | // by the next packet, and is too far off to steer by meanwhile. | ||
| 812 | ✗ | masterEpochs.clear(); | |
| 813 | ✗ | best = frame0; | |
| 814 | } | ||
| 815 |
1/2✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
|
722 | masterEpochs.push_back({ now, frame0 }); |
| 816 | 722 | return best; | |
| 817 | 722 | } | |
| 818 | |||
| 819 | /* | ||
| 820 | * Update the count of frames that the master has played so we can sync to it | ||
| 821 | */ | ||
| 822 | ✗ | void UpdateMasterPosition(int frameNumber) { | |
| 823 | ✗ | UpdateMasterPosition(frameNumber, (double)frameNumber); | |
| 824 | ✗ | } | |
| 825 | |||
| 826 | 722 | void UpdateMasterPosition(int frameNumber, double framePosition) { | |
| 827 | 722 | MasterFramesPlayed = frameNumber; | |
| 828 | // The master sends frame F's sync packet just before it puts F on the | ||
| 829 | // wire, so F goes out there about when the packet arrives (the earliest | ||
| 830 | // arrival of the last few, MasterFrame0()). The frame this thread sends | ||
| 831 | // next is due that many frames on; the difference from when it is | ||
| 832 | // actually due is closed by the phase servo, as a player's lights follow | ||
| 833 | // its media. Frame counts alone left a remote's lights most of a frame | ||
| 834 | // late at 40 fps, and one also playing the show's audio settled a | ||
| 835 | // different distance late each start, up to most of a frame at 20 fps. | ||
| 836 | // | ||
| 837 | // A packet comes every ten frames, so the correction left from the last | ||
| 838 | // one has been taken by the next; each takes half the error it measures, | ||
| 839 | // which closes it without overshoot. | ||
| 840 | 722 | bool phaseServo = false; | |
| 841 |
3/6✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 722 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 722 times.
✗ Branch 5 not taken.
|
722 | if (sequence->IsSequenceRunning() && RefreshRate > 0) { |
| 842 | 722 | long long now = GetMonotonicTimeMicros(); | |
| 843 | 722 | double frameUs = 1000000.0 / RefreshRate; | |
| 844 | 722 | double frame0 = MasterFrame0(frameNumber, framePosition, now, frameUs); | |
| 845 | long frame; | ||
| 846 | long long due; | ||
| 847 | { | ||
| 848 |
1/2✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
|
722 | std::unique_lock<std::mutex> l(nextSendLock); |
| 849 | 722 | frame = nextSendFrame; | |
| 850 | 722 | due = nextSendDueUs; | |
| 851 | 722 | } | |
| 852 |
3/4✓ Branch 0 taken 702 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 702 times.
✗ Branch 3 not taken.
|
722 | if (frame >= 0 && due > 0) { |
| 853 | 702 | double ideal = frame0 + (double)frame * frameUs; | |
| 854 | 702 | double err = (double)due - ideal; // > 0: our frames are late | |
| 855 |
2/2✓ Branch 0 taken 684 times.
✓ Branch 1 taken 18 times.
|
702 | if (std::fabs(err) < 3 * frameUs) { |
| 856 | 684 | mediaPhaseCorrUs.store(llround(-0.5 * err), std::memory_order_relaxed); | |
| 857 | mediaPhaseCorrAt.store(now, std::memory_order_relaxed); | ||
| 858 | 684 | phaseServo = true; | |
| 859 | } | ||
| 860 | } | ||
| 861 | } | ||
| 862 | 722 | AdjustForFrame(frameNumber, phaseServo); | |
| 863 | 722 | } | |
| 864 | |||
| 865 | /* | ||
| 866 | * Calculate the new sync offset based on the current position reported | ||
| 867 | * by the media player. | ||
| 868 | */ | ||
| 869 | 174427 | void CalculateNewChannelOutputDelay(float mediaPosition) { | |
| 870 | static float nextSyncCheck = 0.5; | ||
| 871 | static double phaseErrUs = 0.0; | ||
| 872 | static long long phaseErrAt = 0; | ||
| 873 | |||
| 874 | 174427 | const float offset = mediaOffset; | |
| 875 | 174427 | float offsetMediaPosition = mediaPosition - offset; | |
| 876 |
2/2✓ Branch 0 taken 1027 times.
✓ Branch 1 taken 173400 times.
|
174427 | mediaElapsedSeconds = offsetMediaPosition < 0.0 ? 0.0 : offsetMediaPosition; |
| 877 | |||
| 878 |
6/6✓ Branch 0 taken 160551 times.
✓ Branch 1 taken 13876 times.
✓ Branch 2 taken 32925 times.
✓ Branch 3 taken 127626 times.
✓ Branch 4 taken 46801 times.
✓ Branch 5 taken 127626 times.
|
174427 | if (getFPPmode() == REMOTE_MODE || !sequence->IsSequenceRunning()) |
| 879 | 46801 | return; | |
| 880 | |||
| 881 | // Frame F belongs on the wire when the media reaches F / RefreshRate. | ||
| 882 | 127626 | long long now = GetMonotonicTimeMicros(); | |
| 883 | long frame; | ||
| 884 | long long due; | ||
| 885 | { | ||
| 886 |
1/2✓ Branch 0 taken 127626 times.
✗ Branch 1 not taken.
|
127626 | std::unique_lock<std::mutex> l(nextSendLock); |
| 887 | 127626 | frame = nextSendFrame; | |
| 888 | 127626 | due = nextSendDueUs; | |
| 889 | 127626 | } | |
| 890 | 127626 | double frameUs = 1000000.0 / RefreshRate; | |
| 891 | 127626 | bool phaseServo = false; | |
| 892 |
5/6✓ Branch 0 taken 127618 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 127618 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 126421 times.
✓ Branch 5 taken 1197 times.
|
127626 | if (frame >= 0 && due > 0 && offsetMediaPosition > 0.0) { |
| 893 | 126421 | double rate = CurrentMediaClockRate(now); | |
| 894 | 126421 | double ideal = now + ((double)frame / RefreshRate - offsetMediaPosition) * 1000000.0 / rate; | |
| 895 | 126421 | double err = (double)due - ideal; // > 0: the lights are late | |
| 896 |
2/2✓ Branch 0 taken 124433 times.
✓ Branch 1 taken 1988 times.
|
126421 | if (std::fabs(err) < 3 * frameUs) { |
| 897 | // Smoothed: each report is only as good as the position query. | ||
| 898 |
2/2✓ Branch 0 taken 124375 times.
✓ Branch 1 taken 58 times.
|
124433 | phaseErrUs = (now - phaseErrAt > 1000000) ? err : phaseErrUs + 0.2 * (err - phaseErrUs); |
| 899 | 124433 | phaseErrAt = now; | |
| 900 | 124433 | mediaPhaseCorrUs.store(llround(-phaseErrUs), std::memory_order_relaxed); | |
| 901 | mediaPhaseCorrAt.store(now, std::memory_order_relaxed); | ||
| 902 | 124433 | phaseServo = true; | |
| 903 | } else { | ||
| 904 | 1988 | phaseErrAt = 0; | |
| 905 | } | ||
| 906 | } | ||
| 907 | |||
| 908 |
2/2✓ Branch 0 taken 125783 times.
✓ Branch 1 taken 1843 times.
|
127626 | if ((mediaPosition <= nextSyncCheck) && |
| 909 |
2/2✓ Branch 0 taken 125747 times.
✓ Branch 1 taken 36 times.
|
125783 | (nextSyncCheck < (mediaPosition + 2.0))) { |
| 910 | 125747 | return; | |
| 911 | } | ||
| 912 | |||
| 913 | 1879 | nextSyncCheck = mediaPosition + (20.0 / RefreshRate); | |
| 914 | |||
| 915 | 1879 | int expectedFramesSent = (int)(offsetMediaPosition * RefreshRate); | |
| 916 | |||
| 917 | 3758 | LogDebug(VB_CHANNELOUT, | |
| 918 | "Media Position: %.2f, Offset: %.3f, Frames Sent: %d, Expected: %d, Diff: %d\n", | ||
| 919 | mediaPosition, offset, channelOutputFrame.load(), expectedFramesSent, | ||
| 920 | channelOutputFrame - expectedFramesSent); | ||
| 921 | |||
| 922 | 1879 | AdjustForFrame(expectedFramesSent, phaseServo); | |
| 923 | } | ||
| 924 | |||
| 925 | /* | ||
| 926 | * Calculate the new sync offset based on a desired frame number | ||
| 927 | */ | ||
| 928 | 170636 | void CalculateNewChannelOutputDelayForFrame(int expectedFramesSent) { | |
| 929 | 170636 | AdjustForFrame(expectedFramesSent, false); | |
| 930 | 170636 | } | |
| 931 | |||
| 932 | 173237 | static void AdjustForFrame(int expectedFramesSent, bool phaseServo) { | |
| 933 | 173237 | int diff = channelOutputFrame - expectedFramesSent; | |
| 934 | 173237 | bool allowFrameSkip = !multiSync->isMultiSyncEnabled(); | |
| 935 |
4/6✓ Branch 0 taken 13016 times.
✓ Branch 1 taken 160221 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13016 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 173237 times.
|
173237 | if (!allowFrameSkip && diff <= -(RefreshRate / 2)) { |
| 936 | // A player sending multisync normally only slews LightDelay so the | ||
| 937 | // remotes aren't yanked around by frame jumps, but the slew is capped | ||
| 938 | // at 15ms per frame. If the output is persistently slower than the | ||
| 939 | // step time, slewing can never catch back up to the media clock and | ||
| 940 | // playback drifts out of sync without bound. Once we are more than | ||
| 941 | // half a second behind, jump — the remotes follow the frame numbers | ||
| 942 | // in the sync packets, so they jump with us. | ||
| 943 | ✗ | LogWarn(VB_CHANNELOUT, "Sequence is %d frames behind the media position and cannot catch up by slewing; skipping ahead. Channel output may be too slow for %d fps.\n", | |
| 944 | -diff, (int)RefreshRate); | ||
| 945 | ✗ | WarningHolder::AddWarningTimeout("Sequence playback cannot keep up with the media; skipping frames to stay in sync", 30); | |
| 946 | ✗ | allowFrameSkip = true; | |
| 947 | } | ||
| 948 |
2/2✓ Branch 0 taken 160221 times.
✓ Branch 1 taken 13016 times.
|
173237 | if (allowFrameSkip) { |
| 949 |
2/2✓ Branch 0 taken 393 times.
✓ Branch 1 taken 159828 times.
|
160221 | if (diff < -4) { |
| 950 | // pretty far behind master, lets just skip forward | ||
| 951 |
2/2✓ Branch 0 taken 141 times.
✓ Branch 1 taken 252 times.
|
393 | if (diff > -(RefreshRate / 2)) { |
| 952 | 141 | LogDebug(VB_CHANNELOUT, "Skipping a few frames - We are at %d, master is at: %d\n", (int)channelOutputFrame.load(), expectedFramesSent); | |
| 953 | // off, but not super off, we'll skip a few frames, but not too much to try and keep using | ||
| 954 | // the frames in the cache and avoid hitting the storage, we'll then have the OS preload | ||
| 955 | // the next bunch and we can skip a few more next time | ||
| 956 | 141 | FrameSkip = 4; | |
| 957 | 141 | diff += 4; | |
| 958 | } else { | ||
| 959 | 252 | LogDebug(VB_CHANNELOUT, "Skipping many frames - We are at %d, master is at: %d\n", (int)channelOutputFrame.load(), expectedFramesSent); | |
| 960 | // more than 1/2 second behind, just jump | ||
| 961 | 252 | FrameSkip = expectedFramesSent - channelOutputFrame; | |
| 962 |
1/2✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
|
504 | LightDelay = sequence->IsSequenceRunning() ? SequenceLightDelay.load() : BridgeLightDelay.load(); |
| 963 | 252 | return; | |
| 964 | } | ||
| 965 |
2/2✓ Branch 0 taken 30846 times.
✓ Branch 1 taken 128982 times.
|
159828 | } else if (diff > 2) { |
| 966 | // hold the last frame | ||
| 967 | 30846 | FrameSkip = -1; | |
| 968 | 30846 | diff--; | |
| 969 | } | ||
| 970 | } | ||
| 971 |
1/2✓ Branch 0 taken 172985 times.
✗ Branch 1 not taken.
|
345970 | int DefaultLightDelay = sequence->IsSequenceRunning() ? SequenceLightDelay.load() : BridgeLightDelay.load(); |
| 972 |
4/6✓ Branch 0 taken 2520 times.
✓ Branch 1 taken 170465 times.
✓ Branch 2 taken 2520 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2520 times.
✗ Branch 5 not taken.
|
172985 | if (phaseServo && diff >= -3 && diff <= 3) { |
| 973 | // Within a few frames the phase servo has it, to well under a frame. | ||
| 974 | 2520 | LightDelay = DefaultLightDelay; | |
| 975 | 2520 | return; | |
| 976 | } | ||
| 977 |
4/4✓ Branch 0 taken 138933 times.
✓ Branch 1 taken 31532 times.
✓ Branch 2 taken 230 times.
✓ Branch 3 taken 138703 times.
|
170465 | if (diff > 1 || diff < -1) { |
| 978 | 31762 | int timerOffset = diff * (DefaultLightDelay / 100); | |
| 979 | 31762 | int newLightDelay = LightDelay; | |
| 980 |
2/2✓ Branch 0 taken 31522 times.
✓ Branch 1 taken 240 times.
|
31762 | if (channelOutputFrame > expectedFramesSent) { |
| 981 | // correct if we slingshot past 0, otherwise offset further | ||
| 982 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31522 times.
|
31522 | if (LightDelay < DefaultLightDelay) |
| 983 | ✗ | newLightDelay = DefaultLightDelay; | |
| 984 | else | ||
| 985 | 31522 | newLightDelay += timerOffset; | |
| 986 | } else { | ||
| 987 | // correct if we slingshot past 0, otherwise offset further | ||
| 988 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 235 times.
|
240 | if (LightDelay > DefaultLightDelay) |
| 989 | 5 | newLightDelay = DefaultLightDelay; | |
| 990 | else | ||
| 991 | 235 | newLightDelay += timerOffset; | |
| 992 | } | ||
| 993 | |||
| 994 | // Don't let us go more than 15ms out from the default. If we | ||
| 995 | // can't keep up using that delta then we probably won't be able to. | ||
| 996 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 31629 times.
|
31762 | if ((DefaultLightDelay - 15000) > newLightDelay) |
| 997 | 133 | newLightDelay = DefaultLightDelay - 15000; | |
| 998 | // Same bound when we are ahead. The offset added above scales with | ||
| 999 | // the frame difference and accumulates on every check, so without it a | ||
| 1000 | // large lead (a media position that fell back to 0 while the sequence | ||
| 1001 | // resumed minutes in) grew LightDelay to seconds. The output thread | ||
| 1002 | // sleeps for LightDelay, so the lights froze until the media caught | ||
| 1003 | // up. At +15ms per frame a lead still closes steadily. | ||
| 1004 |
2/2✓ Branch 0 taken 31494 times.
✓ Branch 1 taken 268 times.
|
31762 | if ((DefaultLightDelay + 15000) < newLightDelay) |
| 1005 | 31494 | newLightDelay = DefaultLightDelay + 15000; | |
| 1006 | |||
| 1007 | 63524 | LogDebug(VB_CHANNELOUT, "LightDelay: %d, newLightDelay: %d, DiffFrames: %d %d/%d\n", | |
| 1008 | LightDelay.load(), newLightDelay, diff, channelOutputFrame.load(), expectedFramesSent); | ||
| 1009 | 31762 | LightDelay = newLightDelay; | |
| 1010 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 138617 times.
|
170465 | } else if (diff == -1) { |
| 1011 | // for the one frame off cases, keep the existing light delay unless the | ||
| 1012 | // previous "off" frame was on the other side of default | ||
| 1013 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
|
86 | if (LightDelay > DefaultLightDelay) { |
| 1014 | ✗ | LightDelay = DefaultLightDelay; | |
| 1015 | } | ||
| 1016 |
2/2✓ Branch 0 taken 133486 times.
✓ Branch 1 taken 5131 times.
|
138617 | } else if (diff == 1) { |
| 1017 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 133486 times.
|
133486 | if (LightDelay < DefaultLightDelay) { |
| 1018 | ✗ | LightDelay = DefaultLightDelay; | |
| 1019 | } | ||
| 1020 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5129 times.
|
5131 | } else if (LightDelay != DefaultLightDelay) { |
| 1021 | 2 | LightDelay = DefaultLightDelay; | |
| 1022 | } | ||
| 1023 | } | ||
| 1024 |