| 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 "fpp-json.h" | ||
| 16 | |||
| 17 | #include "../common.h" | ||
| 18 | #include "../log.h" | ||
| 19 | #include "../settings.h" | ||
| 20 | |||
| 21 | #include "../Sequence.h" | ||
| 22 | #include "../channeloutput/ChannelOutputSetup.h" | ||
| 23 | #include "../channeloutput/channeloutputthread.h" | ||
| 24 | #include "../fseq/FSEQFile.h" | ||
| 25 | |||
| 26 | #include "PlaylistEntrySequence.h" | ||
| 27 | |||
| 28 | #include "Events.h" | ||
| 29 | #include "Timers.h" | ||
| 30 | |||
| 31 | #include <chrono> | ||
| 32 | |||
| 33 | // Monotonic millisecond stopwatch used for sequence frame-timing sync. | ||
| 34 | // Must NOT use wall-clock time (gettimeofday/GetTimeMS): RTC-less boards (e.g. a | ||
| 35 | // Pi with no RTC) start a playlist via FPPD_STARTED with an incorrect clock, and | ||
| 36 | // the NTP correction that lands mid-playback would jump the wall clock and wedge | ||
| 37 | // the channel output thread into a permanent hold/skip (see issue #2678). | ||
| 38 | 173015 | static inline long long SeqMonotonicMS() { | |
| 39 |
1/2✓ Branch 0 taken 173015 times.
✗ Branch 1 not taken.
|
173015 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 40 | 346030 | std::chrono::steady_clock::now().time_since_epoch()) | |
| 41 | 346030 | .count(); | |
| 42 | } | ||
| 43 | |||
| 44 | 757 | PlaylistEntrySequence::PlaylistEntrySequence(Playlist* playlist, PlaylistEntryBase* parent) : | |
| 45 | PlaylistEntryBase(playlist, parent), | ||
| 46 | 757 | m_duration(0), | |
| 47 | 757 | m_prepared(false), | |
| 48 | 757 | m_adjustTiming(true), | |
| 49 | 757 | m_sequenceFrameTime(0), | |
| 50 | 757 | m_pausedFrame(-1) { | |
| 51 |
1/2✓ Branch 0 taken 757 times.
✗ Branch 1 not taken.
|
757 | LogDebug(VB_PLAYLIST, "PlaylistEntrySequence::PlaylistEntrySequence()\n"); |
| 52 | |||
| 53 |
1/2✓ Branch 0 taken 757 times.
✗ Branch 1 not taken.
|
757 | m_type = "sequence"; |
| 54 | 757 | } | |
| 55 | |||
| 56 | 1876 | PlaylistEntrySequence::~PlaylistEntrySequence() { | |
| 57 | 1876 | } | |
| 58 | |||
| 59 | /* | ||
| 60 | * | ||
| 61 | */ | ||
| 62 | 757 | int PlaylistEntrySequence::Init(Json::Value& config) { | |
| 63 | 757 | LogDebug(VB_PLAYLIST, "PlaylistEntrySequence::Init()\n"); | |
| 64 | |||
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 757 times.
|
757 | if (!config.isMember("sequenceName")) { |
| 66 | ✗ | LogErr(VB_PLAYLIST, "Missing sequenceName entry\n"); | |
| 67 | ✗ | return 0; | |
| 68 | } | ||
| 69 | |||
| 70 |
2/4✓ Branch 0 taken 757 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 757 times.
✗ Branch 3 not taken.
|
757 | m_sequenceName = config["sequenceName"].asString(); |
| 71 | 757 | m_pausedFrame = -1; | |
| 72 | 757 | return PlaylistEntryBase::Init(config); | |
| 73 | } | ||
| 74 | |||
| 75 | 2379 | int PlaylistEntrySequence::PreparePlay(int frame) { | |
| 76 |
2/2✓ Branch 0 taken 690 times.
✓ Branch 1 taken 1689 times.
|
2379 | if (sequence->OpenSequenceFile(m_sequenceName, frame) <= 0) { |
| 77 | // OpenSequenceFile() has logged why (once per name); a playlist that | ||
| 78 | // keeps coming round to a missing sequence must not log it each time. | ||
| 79 | 690 | LogDebug(VB_PLAYLIST, "Error opening sequence %s\n", m_sequenceName.c_str()); | |
| 80 | 690 | return 0; | |
| 81 | } | ||
| 82 | 1689 | m_prepared = true; | |
| 83 | 1689 | m_duration = sequence->m_seqMSDuration; | |
| 84 | 1689 | m_sequenceFrameTime = sequence->GetSeqStepTime(); | |
| 85 | 1689 | return 1; | |
| 86 | } | ||
| 87 | |||
| 88 | /* | ||
| 89 | * | ||
| 90 | */ | ||
| 91 | 2375 | int PlaylistEntrySequence::StartPlaying(void) { | |
| 92 | 2375 | LogDebug(VB_PLAYLIST, "PlaylistEntrySequence::StartPlaying()\n"); | |
| 93 | |||
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2375 times.
|
2375 | if (!CanPlay()) { |
| 95 | ✗ | m_prepared = false; | |
| 96 | ✗ | FinishPlay(); | |
| 97 | ✗ | return 0; | |
| 98 | } | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 2364 times.
✓ Branch 1 taken 11 times.
|
2375 | if (!m_prepared) { |
| 101 | 2364 | PreparePlay(); | |
| 102 | } | ||
| 103 | 2375 | m_pausedFrame = -1; | |
| 104 | 2375 | ResetChannelOutputFrameNumber(); | |
| 105 | 2375 | sequence->StartSequence(); | |
| 106 | 2375 | m_startTme = SeqMonotonicMS(); | |
| 107 | 2375 | LogDebug(VB_PLAYLIST, "Started Sequence, ID: %s\n", m_sequenceName.c_str()); | |
| 108 | |||
| 109 |
2/4✓ Branch 0 taken 2375 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2375 times.
✗ Branch 3 not taken.
|
4690 | Events::Publish("playlist/sequence/status", m_sequenceName); |
| 110 |
2/2✓ Branch 0 taken 1685 times.
✓ Branch 1 taken 690 times.
|
2375 | if (m_duration > 0) { |
| 111 |
2/4✓ Branch 0 taken 1685 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1685 times.
✗ Branch 3 not taken.
|
4995 | Events::Publish("playlist/sequence/secondsTotal", m_duration / 1000); |
| 112 | } else { | ||
| 113 |
3/6✓ Branch 0 taken 690 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 690 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 690 times.
✗ Branch 5 not taken.
|
2760 | Events::Publish("playlist/sequence/secondsTotal", ""); |
| 114 | } | ||
| 115 | |||
| 116 | 2375 | return PlaylistEntryBase::StartPlaying(); | |
| 117 | } | ||
| 118 | |||
| 119 | /* | ||
| 120 | * | ||
| 121 | */ | ||
| 122 | 300196 | int PlaylistEntrySequence::Process(void) { | |
| 123 |
2/2✓ Branch 0 taken 1745 times.
✓ Branch 1 taken 298451 times.
|
300196 | if (!sequence->IsSequenceRunning()) { |
| 124 | 1745 | FinishPlay(); | |
| 125 | 1745 | m_prepared = false; | |
| 126 | |||
| 127 |
3/6✓ Branch 0 taken 1745 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1745 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1745 times.
✗ Branch 5 not taken.
|
6980 | Events::Publish("playlist/sequence/status", ""); |
| 128 |
3/6✓ Branch 0 taken 1745 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1745 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1745 times.
✗ Branch 5 not taken.
|
6980 | Events::Publish("playlist/sequence/secondsTotal", ""); |
| 129 |
2/2✓ Branch 0 taken 170636 times.
✓ Branch 1 taken 127815 times.
|
298451 | } else if (m_adjustTiming) { |
| 130 | 170636 | long long now = SeqMonotonicMS(); | |
| 131 | 170636 | int total = (now - m_startTme); | |
| 132 | 170636 | int frame = total / sequence->GetSeqStepTime(); | |
| 133 | 170636 | CalculateNewChannelOutputDelayForFrame(frame); | |
| 134 | } | ||
| 135 | |||
| 136 | 300196 | return PlaylistEntryBase::Process(); | |
| 137 | } | ||
| 138 | |||
| 139 | /* | ||
| 140 | * | ||
| 141 | */ | ||
| 142 | 53 | int PlaylistEntrySequence::StopAfter(int ms) { | |
| 143 | 53 | LogDebug(VB_PLAYLIST, "PlaylistEntrySequence::StopAfter(%d)\n", ms); | |
| 144 | 53 | uint64_t generation = sequence->OpenGeneration(); | |
| 145 | // Named for its generation: a timer of the same name replaces the last | ||
| 146 | // (Timers::addTimer()), and two tails within one would lose the first. | ||
| 147 |
4/10✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
53 | Timers::INSTANCE.addTimer("SequenceSoundTail" + std::to_string(generation), GetTimeMS() + ms, [generation]() { |
| 148 | 53 | sequence->CloseSequenceFileIfStill(generation); | |
| 149 | 53 | }); | |
| 150 | 53 | m_prepared = false; | |
| 151 |
3/6✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
|
212 | Events::Publish("playlist/sequence/status", ""); |
| 152 |
3/6✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
|
159 | Events::Publish("playlist/sequence/secondsTotal", ""); |
| 153 | |||
| 154 | 53 | return PlaylistEntryBase::Stop(); | |
| 155 | } | ||
| 156 | |||
| 157 | 382 | int PlaylistEntrySequence::Stop(void) { | |
| 158 | 382 | LogDebug(VB_PLAYLIST, "PlaylistEntrySequence::Stop()\n"); | |
| 159 | |||
| 160 | 382 | sequence->CloseSequenceFile(); | |
| 161 | 382 | m_prepared = false; | |
| 162 |
3/6✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
✗ Branch 5 not taken.
|
1528 | Events::Publish("playlist/sequence/status", ""); |
| 163 |
3/6✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
✗ Branch 5 not taken.
|
1146 | Events::Publish("playlist/sequence/secondsTotal", ""); |
| 164 | |||
| 165 | 382 | return PlaylistEntryBase::Stop(); | |
| 166 | } | ||
| 167 | |||
| 168 | 314201 | uint64_t PlaylistEntrySequence::GetLengthInMS() { | |
| 169 |
2/2✓ Branch 0 taken 762 times.
✓ Branch 1 taken 313439 times.
|
314201 | if (m_duration == 0) { |
| 170 |
4/8✓ Branch 0 taken 762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 762 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 762 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 762 times.
✗ Branch 7 not taken.
|
1477 | std::string n = FPP_DIR_SEQUENCE("/" + m_sequenceName); |
| 171 |
3/4✓ Branch 0 taken 762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 687 times.
|
762 | if (FileExists(n)) { |
| 172 |
1/2✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
|
75 | FSEQFile* fs = FSEQFile::openFSEQFile(n); |
| 173 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 3 times.
|
75 | if (fs) { |
| 174 | 72 | m_duration = fs->getTotalTimeMS(); | |
| 175 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | delete fs; |
| 176 | } | ||
| 177 | } | ||
| 178 | 762 | } | |
| 179 | 314201 | return m_duration; | |
| 180 | } | ||
| 181 | 195408 | uint64_t PlaylistEntrySequence::GetElapsedMS() { | |
| 182 |
2/2✓ Branch 0 taken 193658 times.
✓ Branch 1 taken 1750 times.
|
195408 | if (m_prepared) { |
| 183 | 193658 | return sequence->m_seqMSElapsed; | |
| 184 | } | ||
| 185 | 1750 | return 0; | |
| 186 | } | ||
| 187 | |||
| 188 | /* | ||
| 189 | * | ||
| 190 | */ | ||
| 191 | ✗ | void PlaylistEntrySequence::Dump(void) { | |
| 192 | ✗ | PlaylistEntryBase::Dump(); | |
| 193 | |||
| 194 | ✗ | LogDebug(VB_PLAYLIST, "Sequence Filename: %s\n", m_sequenceName.c_str()); | |
| 195 | ✗ | } | |
| 196 | |||
| 197 | /* | ||
| 198 | * | ||
| 199 | */ | ||
| 200 | 15252 | Json::Value PlaylistEntrySequence::GetConfig(void) { | |
| 201 | 15252 | Json::Value result = PlaylistEntryBase::GetConfig(); | |
| 202 | |||
| 203 |
2/4✓ Branch 0 taken 15252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15252 times.
✗ Branch 3 not taken.
|
15252 | result["sequenceName"] = m_sequenceName; |
| 204 |
3/4✓ Branch 0 taken 15252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13389 times.
✓ Branch 3 taken 1863 times.
|
15252 | if (sequence->GetSeqFilenameCopy() == m_sequenceName) { |
| 205 |
2/4✓ Branch 0 taken 13389 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13389 times.
|
13389 | if (IsPaused()) { |
| 206 | ✗ | int pos = m_pausedFrame * m_sequenceFrameTime; | |
| 207 | ✗ | result["millisecondsElapsed"] = pos; | |
| 208 | ✗ | result["secondsElapsed"] = pos / 1000; | |
| 209 | ✗ | result["secondsRemaining"] = (m_duration - pos) / 1000; | |
| 210 | } else { | ||
| 211 |
2/4✓ Branch 0 taken 13389 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13389 times.
✗ Branch 3 not taken.
|
26778 | result["millisecondsElapsed"] = sequence->m_seqMSElapsed.load(); |
| 212 |
2/4✓ Branch 0 taken 13389 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13389 times.
✗ Branch 3 not taken.
|
13389 | result["secondsElapsed"] = sequence->m_seqMSElapsed / 1000; |
| 213 |
2/4✓ Branch 0 taken 13389 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13389 times.
✗ Branch 3 not taken.
|
13389 | result["secondsRemaining"] = sequence->m_seqMSRemaining / 1000; |
| 214 | } | ||
| 215 | } | ||
| 216 | 15252 | return result; | |
| 217 | ✗ | } | |
| 218 | ✗ | Json::Value PlaylistEntrySequence::GetMqttStatus(void) { | |
| 219 | ✗ | Json::Value result = PlaylistEntryBase::GetMqttStatus(); | |
| 220 | ✗ | result["sequenceName"] = m_sequenceName; | |
| 221 | ✗ | if (IsPaused()) { | |
| 222 | ✗ | int pos = m_pausedFrame * m_sequenceFrameTime; | |
| 223 | ✗ | result["millisecondsElapsed"] = pos; | |
| 224 | ✗ | result["secondsElapsed"] = pos / 1000; | |
| 225 | ✗ | result["secondsRemaining"] = (m_duration - pos) / 1000; | |
| 226 | ✗ | result["secondsTotal"] = m_duration / 1000; | |
| 227 | } else { | ||
| 228 | ✗ | result["secondsElapsed"] = sequence->m_seqMSElapsed / 1000; | |
| 229 | ✗ | result["secondsRemaining"] = sequence->m_seqMSRemaining / 1000; | |
| 230 | ✗ | result["secondsTotal"] = sequence->m_seqMSDuration / 1000; | |
| 231 | ✗ | result["millisecondsElapsed"] = sequence->m_seqMSElapsed.load(); | |
| 232 | } | ||
| 233 | ✗ | return result; | |
| 234 | ✗ | } | |
| 235 | |||
| 236 | 4 | void PlaylistEntrySequence::Pause() { | |
| 237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (sequence->GetSeqStepTime() == 0) { |
| 238 | ✗ | m_pausedFrame = 0; | |
| 239 | } else { | ||
| 240 | 4 | m_pausedFrame = sequence->m_seqMSElapsed / sequence->GetSeqStepTime(); | |
| 241 | } | ||
| 242 | 4 | sequence->CloseSequenceFile(); | |
| 243 | 4 | sequence->SendBlankingData(); | |
| 244 | 4 | m_prepared = false; | |
| 245 | 4 | } | |
| 246 | 358640 | bool PlaylistEntrySequence::IsPaused() { | |
| 247 | 358640 | return m_pausedFrame != -1; | |
| 248 | } | ||
| 249 | 4 | void PlaylistEntrySequence::Resume() { | |
| 250 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (m_pausedFrame >= 0) { |
| 251 | 4 | PreparePlay(m_pausedFrame); | |
| 252 | 4 | sequence->StartSequence(); | |
| 253 | 4 | m_startTme = SeqMonotonicMS() - m_pausedFrame * sequence->GetSeqStepTime(); | |
| 254 | 4 | LogDebug(VB_PLAYLIST, "Started Sequence, ID: %s\n", m_sequenceName.c_str()); | |
| 255 | 4 | m_pausedFrame = -1; | |
| 256 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
10 | Events::Publish("playlist/sequence/status", m_sequenceName); |
| 257 | } | ||
| 258 | 4 | } | |
| 259 |