-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.lua
447 lines (385 loc) · 12.5 KB
/
node.lua
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
-- prevent accidental global variables
util.no_globals()
-- We need to access files in screens/
node.make_nested()
-- Start preloading images this many second before
-- they are displayed.
local PREPARE_TIME = 1 -- seconds
-- There is only one HEVC decoder slot. So videos
-- cannot be preloaded. Instead we reserve the
-- following number of seconds at each play slot
-- for loading the video.
local HEVC_LOAD_TIME = 0.5 -- seconds
-- Each playslot must be at least this long.
-- Otherwise prepare time completely skips the
-- previous slot.
local MIN_DURATION = 1.2 -- seconds
local json = require "json"
local matrix = require "matrix2d"
local font = resource.load_font "silkscreen.ttf"
local serial = sys.get_env "SERIAL"
local min, max = math.min, math.max
local assigned = false
local delta_t = 0
local adjust = false
local effect = "none"
local preload_time = 0.5
local audio = false
local status_text = "top_left"
local function msg(str, ...)
local text = str:format(...)
print(text)
if status_text == "off" then
return
end
local size = 24
local width = font:width(text, size)
local x, y = unpack(({
top_left = {10, 10},
top_right = {NATIVE_WIDTH - 10 - width, 10},
bottom_left = {10, NATIVE_HEIGHT - 10 - size},
bottom_right = {NATIVE_WIDTH - 10 - width, NATIVE_HEIGHT - 10 - size},
})[status_text])
font:write(x, y, text, size, 1,1,1,.5)
end
local function get_walltime()
return os.time() + max(-1000, min(1000, delta_t))/1000
end
local function ramp(t_s, t_e, t_c, ramp_time)
if ramp_time == 0 then return 1 end
local delta_s = t_c - t_s
local delta_e = t_e - t_c
return math.min(1, delta_s * 1/ramp_time, delta_e * 1/ramp_time)
end
local function VirtualScreen()
local screen, virtual2pixel, pixel2virtual
local virtual_w, virtual_h
local translate_0_x, translate_0_y
local scale_x, scale_y
local function update(new_screen)
screen = new_screen
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
virtual_w, virtual_h = screen.res_x or NATIVE_WIDTH,
screen.res_y or NATIVE_HEIGHT
scale_x = NATIVE_WIDTH / virtual_w
scale_y = NATIVE_HEIGHT / virtual_h
if screen.rotation == 0 then
translate_0_x, translate_0_y = 0, 0
elseif screen.rotation == 90 then
translate_0_x, translate_0_y = WIDTH, 0
virtual_w, virtual_h = virtual_h, virtual_w
elseif screen.rotation == 180 then
translate_0_x, translate_0_y = WIDTH, HEIGHT
elseif screen.rotation == 270 then
translate_0_x, translate_0_y = 0, HEIGHT
virtual_w, virtual_h = virtual_h, virtual_w
end
WIDTH = virtual_w
HEIGHT = virtual_h
virtual2pixel = matrix.trans(translate_0_x, translate_0_y)
* matrix.scale(scale_x, scale_y)
* matrix.rotate_deg(screen.rotation)
pixel2virtual = -virtual2pixel
end
local function project(x1, y1, x2, y2)
x1, y1 = virtual2pixel(x1, y1)
x2, y2 = virtual2pixel(x2, y2)
return math.floor(math.min(x1, x2)), math.floor(math.min(y1, y2)),
math.floor(math.max(x1, x2)), math.floor(math.max(y1, y2))
end
local function unproject(x, y)
x, y = pixel2virtual(x, y)
return math.floor(x), math.floor(y)
end
local function video(vid, x1, y1, x2, y2, layer, alpha)
layer = layer or 1
x1, y1, x2, y2 = project(x1, y1, x2, y2)
return vid:alpha(alpha):place(
x1, y1, x2, y2, screen.rotation
):layer(layer)
end
local function setup()
gl.translate(translate_0_x, translate_0_y)
gl.scale(scale_x, scale_y)
gl.rotate(screen.rotation, 0, 0, 1)
end
return {
update = update;
unproject = unproject;
setup = setup;
video = video;
}
end
local function ContentArea(screen)
local content_w, content_h
local content_area
local content2virtual
local function update(new_content_area, new_content_w, new_content_h)
screen.update{
rotation = new_content_area.rotation,
}
content_area = new_content_area
content_w, content_h = new_content_w, new_content_h
content2virtual = matrix.scale(WIDTH / content_w,
HEIGHT / content_h)
* matrix.scale(content_w / content_area.width,
content_h / content_area.height)
* matrix.trans(-content_area.x, -content_area.y)
end
local function virtual_coordinates(obj)
local x1, y1 = 0, 0
local x2, y2 = content_w, content_h
if adjust then
local _, w, h = obj:state()
x1, y1, x2, y2 = util.scale_into(content_w, content_h, w, h)
end
x1, y1 = content2virtual(x1, y1)
x2, y2 = content2virtual(x2, y2)
return x1, y1, x2, y2
end
local function draw_image(obj, a)
local x1, y1, x2, y2 = virtual_coordinates(obj)
gl.pushMatrix()
screen.setup()
obj:draw(x1, y1, x2, y2, a)
gl.popMatrix()
end
local function draw_video(obj, a)
local x1, y1, x2, y2 = virtual_coordinates(obj)
screen.video(obj, x1, y1, x2, y2, 1, a)
end
return {
update = update;
draw_image = draw_image;
draw_video = draw_video;
}
end
local content_area = ContentArea(VirtualScreen())
local function get_effect_vars(starts, ends, now)
local alpha
if effect == "none" then
alpha = 1
elseif effect == "fade_02" then
alpha = ramp(starts, ends, now, 0.2)
elseif effect == "fade_05" then
alpha = ramp(starts, ends, now, 0.5)
end
return alpha
end
local Image = {
slot_time = function(self)
return max(MIN_DURATION, self.duration)
end;
prepare = function(self)
self.obj = resource.load_image(self.file:copy())
end;
tick = function(self, now)
local state, w, h = self.obj:state()
local alpha = get_effect_vars(
self.t_start, self.t_end, now
)
if state == "loaded" then
content_area.draw_image(self.obj, alpha)
end
end;
stop = function(self)
if self.obj then
self.obj:dispose()
self.obj = nil
end
end;
}
local Video = {
slot_time = function(self)
self.preload_time = self.is_hevc and HEVC_LOAD_TIME or preload_time
return self.preload_time + max(MIN_DURATION, self.duration)
end;
prepare = function(self)
if self.preload_time == 0 then
print "preloading video"
self.obj = resource.load_video{
file = self.file:copy(),
raw = true,
paused = true,
audio = audio,
}:alpha(0):layer(-1)
end
end;
tick = function(self, now)
if not self.obj then
print "late loading video"
self.obj = resource.load_video{
file = self.file:copy(),
raw = true,
paused = true,
audio = audio,
}:alpha(0):layer(-1)
end
if now < self.t_start + self.preload_time then
return
end
self.obj:start()
local state, w, h = self.obj:state()
if state ~= "loaded" and state ~= "finished" then
print "lost video frame"
else
local alpha = get_effect_vars(
self.t_start + self.preload_time, self.t_end, now
)
content_area.draw_video(self.obj, alpha)
end
end;
stop = function(self)
if self.obj then
self.obj:layer(-1)
self.obj:dispose()
self.obj = nil
end
end;
}
local function Playlist()
local items = {}
local total_duration = 0
local function calc_start(idx, now)
local item = items[idx]
local epoch_offset = now % total_duration
local epoch_start = now - epoch_offset
item.t_start = epoch_start + item.epoch_offset
if item.t_start - PREPARE_TIME < now then
item.t_start = item.t_start + total_duration
end
item.t_prepare = item.t_start - PREPARE_TIME
item.t_end = item.t_start + item:slot_time()
-- pp(item)
end
local function tick(now)
local is_synced = false
local next_running = 999999999999
if not assigned then
msg("[%s] screen not configured for this setup", serial)
return
end
if #items == 0 then
msg("[%s] no playlist configured", serial)
return
end
for idx = #items, 1, -1 do
local item = items[idx]
if item.state == "new" then
print(now, "state: waiting", item.file)
calc_start(idx, now)
item.state = "waiting"
end
if now >= item.t_prepare and item.state == "waiting" then
print(now, "state: preparing ", item.file)
item:prepare()
item.state = "prepared"
elseif now >= item.t_start and item.state == "prepared" then
print(now, "state: running ", item.file)
item.state = "running"
elseif now >= item.t_end and item.state == "running" then
print(now, "state: resetting ", item.file)
item:stop()
calc_start(idx, now)
item.state = "waiting"
end
next_running = min(next_running, item.t_start)
if item.state == "running" then
item:tick(now)
is_synced = true
end
end
if not is_synced then
local wait = next_running - now
msg("[%s] waiting for sync %.1f", serial, wait)
end
end
local function stop_all()
for idx = 1, #items do
local item = items[idx]
item:stop()
end
end
local function set(new_items)
total_duration = 0
for idx = 1, #new_items do
local item = new_items[idx]
if item.type == "image" then
setmetatable(item, {__index = Image})
elseif item.type == "video" then
setmetatable(item, {__index = Video})
else
return error("unsupported type" .. item.type)
end
item.epoch_offset = total_duration
item.state = "new"
total_duration = total_duration + item:slot_time()
end
stop_all()
items = new_items
end
return {
set = set;
tick = tick;
}
end
local playlist = Playlist()
local function prepare_playlist(playlist)
if #playlist >= 2 then
return playlist
elseif #playlist == 1 then
-- only a single item? Copy it
local item = playlist[1]
playlist[#playlist+1] = {
file = item.file,
type = item.type,
duration = item.duration,
is_hevc = item.is_hevc,
}
end
return playlist
end
util.file_watch("screens/config.json", function(raw)
local config = json.decode(raw)
adjust = config.adjust
status_text = config.status_text
delta_t = 0
assigned = false
for idx = 1, #config.screens do
local screen_config = config.screens[idx]
if screen_config.serial == serial then
content_area.update(screen_config, config.width, config.height)
delta_t = screen_config.delta_t
assigned = true
return
end
end
end)
util.file_watch("config.json", function(raw)
local config = json.decode(raw)
local items = {}
for idx = 1, #config.playlist do
local item = config.playlist[idx]
local is_hevc = item.file.metadata and item.file.metadata.format == "hevc"
items[#items+1] = {
file = resource.open_file(item.file.asset_name),
type = item.file.type,
duration = item.duration,
is_hevc = is_hevc,
}
end
effect = config.effect
preload_time = config.video_mode == "seamless" and 0 or 0.5
audio = config.audio
playlist.set(prepare_playlist(items))
node.gc()
end)
function node.render()
gl.clear(0,0,0,1)
local now = get_walltime()
if now < 1000000 then
msg "waiting for correct time"
else
playlist.tick(now)
end
end