GCC Code Coverage Report


Directory: ./
File: Scheduler.h
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 1 1 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #pragma once
2 /*
3 * This file is part of the Falcon Player (FPP) and is Copyright (C)
4 * 2013-2022 by the Falcon Player Developers.
5 *
6 * The Falcon Player (FPP) is free software, and is covered under
7 * multiple Open Source licenses. Please see the included 'LICENSES'
8 * file for descriptions of what files are covered by each license.
9 *
10 * This source file is covered under the LGPL v2.1 as described in the
11 * included LICENSE.LGPL file.
12 */
13
14 #include <atomic>
15 #include <map>
16 #include "fpp-json.h"
17 #include <mutex>
18 #include <set>
19 #include <vector>
20
21 #include <pthread.h>
22 #include <time.h>
23
24 #include "ScheduleEntry.h"
25
26 #define SCHEDULE_INDEX_INVALID -1
27
28 // The INX_* day index and day mask defines now live in ScheduleEntry.h, which
29 // is included above, since they describe a ScheduleEntry's dayIndex field.
30 // They remain visible to anything including this header.
31
32 class ScheduledItem {
33 public:
34 ScheduleEntry* const entry;
35 int const priority;
36 int const entryIndex;
37 bool ran;
38 time_t const startTime;
39 time_t endTime;
40 std::string command;
41 Json::Value args;
42 // A playlist entry's start (as opposed to an FPP command entry). The
43 // command string can't tell them apart: a command entry can run the
44 // "Start Playlist" command, and was then taken for a playlist entry with
45 // no playlist -- marked ran, never run.
46 bool isPlaylist = false;
47 };
48
49 class Scheduler {
50 public:
51 Scheduler();
52 ~Scheduler();
53
54 void ScheduleProc(void);
55 void CheckIfShouldBePlayingNow(int ignoreRepeat = 0, int forceStopped = -1);
56 void ReloadScheduleFile(void);
57 void SetTimeDelta(int delta, int timeLimit);
58 bool StartNextScheduledItemNow();
59
60 // Scheduled FPP commands run on their own detached threads. At shutdown,
61 // wait (up to timeoutMS) for any still running, before the media, outputs
62 // and command registry they use are torn down.
63 static void WaitForCommandThreads(int timeoutMS);
64
65 std::string GetNextPlaylistName();
66 std::string GetNextPlaylistStartStr();
67 110812 bool IsEnabled() const { return !m_schedulerDisabled; }
68
69 std::string GetPlaylistThatShouldBePlaying(int& repeat);
70 Json::Value GetInfo(void);
71 Json::Value GetSchedule(void);
72
73 // One expanded occurrence of a schedule entry, used while building the
74 // calendar range response. Kept lightweight so a range holding tens of
75 // thousands of occurrences can be collected and aggregated before any of
76 // the much more expensive JSON is built.
77 struct Occurrence {
78 int entryIndex;
79 int dayKey; // YYYYMMDD of the local day the occurrence starts on
80 time_t start;
81 time_t end;
82 bool overridden;
83 };
84
85 Json::Value GetScheduleRange(time_t rangeStart, time_t rangeEnd,
86 bool includeDisabled, bool daySummary);
87
88 private:
89 Json::Value BuildScheduleRangeItem(const ScheduleEntry& entry,
90 const Occurrence& occurrence,
91 const std::string& timeFmt);
92
93 // A player start/stop the scheduler decided on while holding
94 // m_scheduleLock, carried by value so it can be run after the lock is
95 // released. See the m_scheduleLock declaration below.
96 struct PlayerAction {
97 enum class Type {
98 StopUntilIdle,
99 StopNow,
100 StopGracefully,
101 StopGracefullyAfterLoop,
102 StartPlaylist
103 };
104
105 Type type;
106 int forceStop = 0;
107 std::string playlist;
108 int position = 0;
109 int repeat = 0;
110 int entryIndex = 0;
111 int priority = 0;
112 time_t startTime = 0;
113 time_t endTime = 0;
114 int stopType = 0;
115 };
116
117 // A countdown preset to trigger once the lock is released. Presets run
118 // their commands synchronously, so they are subject to the same rule as
119 // PlayerAction.
120 struct CountdownPreset {
121 std::string name;
122 std::string playlist;
123 };
124
125 void RunPlayerActions(const std::vector<PlayerAction>& actions);
126 void RunCountdownPresets(const std::vector<CountdownPreset>& presets);
127
128 void AddScheduledItems(ScheduleEntry* entry, int index, std::time_t loadTime);
129 void DumpScheduledItem(const std::time_t itemTime, const ScheduledItem& item);
130 void DumpScheduledItems();
131 void CheckScheduledItems(bool restarted = false);
132 void ClearScheduledItems();
133 void SetItemRan(ScheduledItem& item, bool ran);
134
135 ScheduledItem* GetNextScheduledPlaylist();
136 std::string NextPlaylistNameFor(ScheduledItem* item);
137 std::string NextPlaylistStartStrFor(ScheduledItem* item, const std::string& timeFmt);
138 void LoadScheduleFromFile(void);
139 void SchedulePrint(void);
140 std::string GetDayTextFromDayIndex(const int index);
141
142 void RegisterCommands();
143
144 void doCountdown(const std::time_t now, const std::time_t itemTime, const std::vector<ScheduledItem>& items,
145 std::vector<CountdownPreset>& presets);
146 void doScheduledCommand(const std::time_t itemTime, const ScheduledItem& item);
147 bool doScheduledPlaylist(const std::time_t now, const std::time_t itemTime, ScheduledItem& item, bool restarted,
148 std::vector<PlayerAction>& actions);
149
150 bool m_schedulerDisabled;
151 // Set lock-free from ScheduleProc, ReloadScheduleFile and SetTimeDelta on
152 // different threads; atomic so those unsynchronized writes/reads are defined.
153 std::atomic<bool> m_loadSchedule;
154 int m_lastLoadDate;
155 int m_timeDelta;
156 time_t m_timeDeltaThreshold;
157
158 time_t m_lastProcTime;
159
160 // The wall-clock second CheckScheduledItems last finished examining for
161 // scheduled FPP commands. A command fires when its second falls in the
162 // (m_lastCommandCheckTime, now] window not yet examined, so a missed second
163 // (a slow midnight reload, an NTP step, a stalled main loop) still fires it
164 // on the next pass instead of dropping it silently. A per-pass catch-up
165 // cap keeps a large forward clock step from replaying a batch of stale
166 // commands. Playlists need no such window - they already tolerate a late
167 // start by checking their own end time.
168 time_t m_lastCommandCheckTime;
169
170 // Lock ordering: m_scheduleLock may be taken while holding no other lock,
171 // or from inside Playlist's m_playlistMutex (a playlist entry can run an
172 // FPP Command inline, and several of those call back into the scheduler).
173 // That makes m_playlistMutex -> m_scheduleLock the established order, so
174 // scheduler code must never start or stop the Player/Playlist while
175 // holding m_scheduleLock - doing so takes the two locks in the opposite
176 // order and deadlocks against a playlist thread. Both mutexes are
177 // recursive, which does not help across threads. Decide under the lock,
178 // copy out what the call needs, release, then call the Player (see
179 // CheckScheduledItems). The read-only Player/Playlist getters do not take
180 // m_playlistMutex and are safe to call under this lock.
181 std::recursive_mutex m_scheduleLock;
182 std::vector<ScheduleEntry> m_Schedule;
183 std::map<std::time_t, std::vector<ScheduledItem>> m_scheduledItems;
184
185 // Scheduled starts decided under m_scheduleLock but not yet carried out
186 // (RunPlayerActions runs unlocked), keyed by start time and entry index.
187 // The player still reads idle while such a start loads, and a concurrent
188 // CheckIfShouldBePlayingNow() -- an API "Start Playlist At Item" runs one
189 // on its own thread -- used to reset the item's ran flag in that window
190 // and start it a second time. Guarded by m_scheduleLock.
191 std::set<std::pair<std::time_t, int>> m_startsInFlight;
192 std::map<std::time_t, std::vector<ScheduledItem>> m_oldItems;
193 std::map<std::time_t, std::vector<ScheduledItem>> m_ranItems;
194
195 // The scheduled occurrence a higher-priority entry pre-empted: allowed to
196 // start again (despite not repeating) once the player goes idle, then
197 // consumed. Keyed by occurrence -- entry index alone matched every later
198 // run of that entry forever, restarting it each time it ended.
199 int m_forcedNextPlaylist;
200 std::time_t m_forcedNextPlaylistStart = 0;
201
202 std::string m_lastBlockedPlaylist;
203 std::time_t m_lastBlockedTime;
204
205 bool m_schedulesExtendBeyondDistance;
206 };
207
208 extern Scheduler* scheduler;
209