-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_runner.cpp
429 lines (367 loc) · 15.8 KB
/
script_runner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "script_runner.h"
#include <mutex>
#include <optional>
#include "display_output.h"
#include "frame_loader.h"
#include "frame_player.h"
#include "logging_policy.h"
namespace pivid {
namespace {
auto const& runner_logger() {
static const auto logger = make_logger("runner");
return logger;
}
int compare(DisplayMode const& a, DisplayMode const& b) {
if (bool(a.nominal_hz) != bool(b.nominal_hz))
return bool(a.nominal_hz) - bool(b.nominal_hz); // Prefer defined!
if (a.doubling.y != b.doubling.y)
return a.doubling.y - b.doubling.y; // Prefer NOT interlaced
return 0; // Agnostic about other things
}
std::map<ScriptMode, DisplayMode> make_mode_map() {
// Start with CTA modes (we use HDMI after all)
std::map<ScriptMode, DisplayMode> modes;
for (auto const& mode : cta_861_modes) {
auto* slot = &modes[{mode.size, mode.nominal_hz}];
if (compare(mode, *slot) > 0) {
*slot = mode;
slot->aspect = {}; // Ignore aspect ratio variants
}
}
// Merge in DMT where there's a gap or it's a better mode
for (auto const& mode : vesa_dmt_modes) {
auto* slot = &modes[{mode.size, mode.nominal_hz}];
if (compare(mode, *slot) > 0) *slot = mode;
}
return modes;
}
std::map<ScriptMode, DisplayMode> const& get_mode_map() {
static auto const map = make_mode_map();
return map;
}
bool matches(ScriptMode const& sm, DisplayMode const& dm) {
return sm.size == dm.size && sm.hz == dm.nominal_hz;
}
class ScriptRunnerDef : public ScriptRunner {
public:
virtual void update(Script const& script) final {
std::unique_lock lock{mutex};
auto const now = cx.sys->clock();
auto const t0 = script.zero_time;
DEBUG(logger, "UPDATE {} (t0+{:.3f}s)", abbrev_realtime(now), now - t0);
for (const auto& [media, tuning] : script.buffer_tuning) {
auto const& file = find_file(lock, media);
auto* input = &input_media[file];
TRACE(logger, " tuning \"{}\"", file);
input->req.decoder_idle_time = tuning.decoder_idle_time;
input->req.seek_scan_time = tuning.seek_scan_time;
TRACE(
logger, " idle={:.3f}s scan={:.3f}s",
input->req.decoder_idle_time,
input->req.seek_scan_time
);
for (auto const& pin : tuning.pin) {
auto const begin = pin.begin.value(now - t0);
auto const end = pin.end.value(now - t0);
if (begin && end) {
Interval want{*begin, *end};
TRACE(logger, " pin {}", debug(want));
input->req.wanted.insert(want);
} else {
TRACE(logger, " pin inactive");
}
}
}
std::vector<DisplayScreen> display_screens;
for (auto const& [connector, script_screen] : script.screens) {
auto *output = &output_screens[connector];
output->defined = true;
if (output->player && matches(script_screen.mode, output->mode)) {
DEBUG(logger, " [{}] {}", connector, debug(output->mode));
} else {
if (display_screens.empty())
display_screens = cx.driver->scan_screens();
uint32_t display_id = 0;
DisplayMode mode = {};
for (auto const& display : display_screens) {
if (display.connector != connector) continue;
display_id = display.id;
// If screen-off is requested, use the zero-init mode.
if (!script_screen.mode.hz) break;
// If the active mode matches the spec, use it.
if (matches(script_screen.mode, display.active_mode)) {
mode = display.active_mode;
break;
}
// Look for a canned mode matching the spec.
auto const& mode_map = get_mode_map();
auto const it = mode_map.find(script_screen.mode);
if (it != mode_map.end()) {
mode = it->second;
break;
}
// Finally, try to synthesize a CVT mode for spec.
// (Assume if we get to this point, RB is OK.)
auto const cvt = vesa_cvt_rb_mode(
script_screen.mode.size,
script_screen.mode.hz
);
if (cvt) mode = *cvt;
break;
}
if (display_id == 0) {
logger->error("Connector not found: \"{}\"", connector);
continue;
}
if (!matches(script_screen.mode, mode)) {
logger->error(
"Mode not found: {}x{} {}Hz",
script_screen.mode.size.x,
script_screen.mode.size.y,
script_screen.mode.hz
);
continue;
}
DEBUG(logger, " [{}] + {}", connector, debug(mode));
if (!output->player)
output->player = cx.player_f(display_id);
output->mode = mode;
}
if (!output->mode.nominal_hz) {
output->player->set_timeline({});
continue;
}
ASSERT(output->mode.actual_hz() > 0);
double const script_hz = script_screen.update_hz;
double const hz = script_hz ? script_hz : output->mode.actual_hz();
double const loop_hz = script.main_loop_hz;
double const begin_t = std::ceil(now * hz) / hz;
double const end_t = now + 2.0 / std::min(hz, loop_hz);
// Create empty timeline elements at each frame time
FramePlayer::Timeline timeline;
for (double t = begin_t; t < end_t + 0.001; t += 1.0 / hz) {
auto* frame = &timeline[t];
frame->mode = output->mode;
frame->layers.reserve(script_screen.layers.size());
}
for (size_t li = 0; li < script_screen.layers.size(); ++li) {
auto const& script_layer = script_screen.layers[li];
auto const& file = find_file(lock, script_layer.media);
auto* input = &input_media[file];
DEBUG(logger, " \"{}\"", short_filename(file));
auto const rt = now - t0;
Interval const buffer_t{rt, rt + script_layer.buffer};
IntervalSet const want = script_layer.play.range(buffer_t);
TRACE(logger, " want {}", debug(want));
input->req.wanted.insert(want);
if (!input->frames) {
if (!input->loader) continue;
input->frames = input->loader->frames();
}
TRACE(logger, " have {}", debug(input->frames->coverage));
for (auto& [t, t_frame] : timeline) {
auto const media_t = script_layer.play.value(t - t0);
if (!media_t) {
TRACE(logger, " {:+.3f}s inactive", t - now);
continue;
}
if (*media_t < 0) {
TRACE(
logger, " {:+.3f}s m{:.3f}s before start",
t - now, *media_t
);
continue;
}
if (input->frames->eof && *media_t >= *input->frames->eof) {
TRACE(
logger, " {:+.3f}s m{:.3f}s after EOF",
t - now, *media_t
);
continue;
}
if (!input->frames->coverage.contains(*media_t)) {
TRACE(
logger, " {:+.3f}s m{:.3f}s not loaded!",
t - now, *media_t
);
t_frame.warnings.push_back(fmt::format(
"Outran buffer (USING BLACK FRAME) @{:.3f}s \"{}\"",
*media_t, file
));
continue;
}
auto fit = input->frames->frames.upper_bound(*media_t);
if (fit == input->frames->frames.begin()) {
TRACE(
logger, " {:+.3f}s m{:.3f}s empty media",
t - now, *media_t
);
continue;
}
auto const bez = [&](BezierSpline const& z, double def) {
return z.value(t - t0).value_or(def);
};
--fit;
auto const frame_t = fit->first;
auto const size = fit->second->content().size;
auto* layer = &t_frame.layers.emplace_back();
layer->from_xy.x = bez(script_layer.from_xy.x, 0);
layer->from_xy.y = bez(script_layer.from_xy.y, 0);
layer->from_size.x = bez(script_layer.from_size.x, size.x);
layer->from_size.y = bez(script_layer.from_size.y, size.y);
layer->to_xy.x = bez(script_layer.to_xy.x, 0);
layer->to_xy.y = bez(script_layer.to_xy.y, 0);
layer->to_size.x = bez(script_layer.to_size.x, size.x);
layer->to_size.y = bez(script_layer.to_size.y, size.y);
layer->opacity = bez(script_layer.opacity, 1);
layer->reflect = script_layer.reflect;
layer->rotate = script_layer.rotate;
TRACE(
logger, " {:+.3f}s m{:.3f} f{:.3f} {}",
t - now, *media_t, frame_t, debug(*layer)
);
layer->image = fit->second; // Not in TRACE above
}
}
output->player->set_timeline(std::move(timeline));
}
auto input_it = input_media.begin();
while (input_it != input_media.end()) {
auto *input = &input_it->second;
if (input->req.wanted.empty()) {
if (input->loader) {
DEBUG(logger, " closing \"{}\"", input_it->first);
} else {
TRACE(logger, " unused \"{}\"", input_it->first);
}
input_it = input_media.erase(input_it);
} else {
if (input->loader) {
TRACE(logger, " refresh \"{}\"", input_it->first);
} else {
DEBUG(logger, " opening \"{}\"", input_it->first);
auto loader_cx = cx.loader_cx;
loader_cx.filename = input_it->first;
input->loader = cx.loader_f(std::move(loader_cx));
}
TRACE(logger, " request {}", debug(input->req.wanted));
input->loader->set_request(std::move(input->req));
input->req = {};
input->frames = {};
++input_it;
}
}
for (auto& [conn, output] : output_screens) {
if (!output.defined) {
DEBUG(logger, " [{}] unspecified, blanking", conn);
output.player->set_timeline({});
} else {
output.defined = false;
}
}
TRACE(logger, " update done");
}
MediaFileInfo const& file_info(std::string const& spec) final {
std::unique_lock lock{mutex};
auto const file = find_file(lock, spec);
auto cache_it = info_cache.find(file);
if (cache_it != info_cache.end()) {
TRACE(logger, "FILE INFO {}", debug(cache_it->second));
} else {
auto loader = input_media[file].loader;
lock.unlock();
if (!loader) {
TRACE(logger, "Opening \"{}\" for info", file);
auto loader_cx = cx.loader_cx;
loader_cx.filename = file;
loader = cx.loader_f(std::move(loader_cx));
}
auto info = loader->file_info();
DEBUG(logger, "FILE INFO {}", debug(info));
lock.lock(); // State may have changed!
auto* input = &input_media[file];
if (!input->loader) input->loader = loader;
cache_it = info_cache.insert({file, std::move(info)}).first;
}
return cache_it->second;
}
void init(ScriptContext c) {
cx = std::move(c);
if (!cx.sys) cx.sys = global_system();
CHECK_ARG(cx.driver, "No driver for ScriptRunner");
if (cx.root_dir.empty()) cx.root_dir = "/";
if (cx.file_base.empty()) cx.file_base = ".";
cx.root_dir = cx.sys->realpath(cx.root_dir).ex(
fmt::format("Root dir ({})", cx.root_dir)
);
cx.file_base = cx.sys->realpath(cx.file_base).ex(
fmt::format("File base ({})", cx.file_base)
);
if (!S_ISDIR(cx.sys->stat(cx.file_base).ex(cx.file_base).st_mode)) {
auto const slash = cx.file_base.rfind('/');
if (slash >= 1) cx.file_base.resize(slash);
}
if (!cx.root_dir.ends_with('/')) cx.root_dir += "/";
if (!cx.file_base.ends_with('/')) cx.file_base += "/";
if (!cx.loader_cx.sys) cx.loader_cx.sys = cx.sys;
if (!cx.loader_cx.driver) cx.loader_cx.driver = cx.driver;
if (!cx.loader_cx.decoder_f)
cx.loader_cx.decoder_f = open_media_decoder;
if (!cx.loader_f)
cx.loader_f = start_frame_loader;
if (!cx.player_f) {
cx.player_f = [this](uint32_t id) {
return start_frame_player(cx.driver, id, cx.sys);
};
}
}
private:
struct InputMedia {
std::shared_ptr<FrameLoader> loader;
std::optional<LoadedFrames> frames;
FrameRequest req;
};
struct OutputScreen {
std::string name;
XY<int> size;
DisplayMode mode;
std::unique_ptr<FramePlayer> player;
bool defined = false;
};
// Constant from init to ~
std::shared_ptr<log::logger> const logger = runner_logger();
ScriptContext cx = {};
// Guarded by mutex
std::mutex mutable mutex;
std::map<std::string, InputMedia> input_media;
std::map<std::string, OutputScreen> output_screens;
std::map<std::string, std::string> path_cache;
std::map<std::string, MediaFileInfo> info_cache;
std::string const& find_file(
std::unique_lock<std::mutex> const&, std::string const& spec
) {
CHECK_ARG(!spec.empty(), "Empty filename");
auto cache_it = path_cache.find(spec);
if (cache_it == path_cache.end()) {
auto const lookup = spec.starts_with('/')
? cx.root_dir + spec.substr(1) : cx.file_base + spec;
auto realpath = cx.sys->realpath(lookup).ex(
fmt::format("Media \"{}\" ({})", spec, lookup)
);
CHECK_ARG(
realpath.starts_with(cx.root_dir),
"Media \"{}\" ({}) outside root ({})",
spec, realpath, cx.root_dir
);
cache_it = path_cache.insert({spec, std::move(realpath)}).first;
}
return cache_it->second;
}
};
} // anonymous namespace
std::unique_ptr<ScriptRunner> make_script_runner(ScriptContext cx) {
auto runner = std::make_unique<ScriptRunnerDef>();
runner->init(std::move(cx));
return runner;
}
} // namespace pivid