-
Notifications
You must be signed in to change notification settings - Fork 4
/
node.lua
471 lines (413 loc) · 12.1 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
util.no_globals()
-- We need to access files in playlist/
node.make_nested()
-- Start preloading images this many second before
-- they are displayed.
local PREPARE_TIME = 1 -- seconds
-- must be enough time to load a video and have it
-- ready in the paused state. Normally 500ms should
-- be enough.
local VIDEO_PRELOAD_TIME = 0.5 -- seconds
local font = resource.load_font "silkscreen.ttf"
local tags = resource.load_image{
file = "tags.png",
nearest = true,
}
local black = resource.create_colored_texture(0, 0, 0, 1)
local json = require "json"
local matrix = require "matrix2d"
local tagmapper = require "tagmapper"
local scissors = sys.get_ext "scissors"
local serial = sys.get_env "SERIAL"
local assigned = false
local audio = false
local function Screen(screen_no, pos)
local mapped = function() end
local tag_id
local has_mapping
local function update(new_tag_id, homography, snapshot_w, snapshot_h)
tag_id = new_tag_id
if #homography == 9 then
mapped = tagmapper.create(matrix.new(
homography[1], homography[2], homography[3],
homography[4], homography[5], homography[6],
homography[7], homography[8], homography[9]
), snapshot_w, snapshot_h)
has_mapping = true
else
mapped = function(fn)
fn(WIDTH, HEIGHT)
end
has_mapping = false
end
end
local function draw(obj)
scissors.set(pos.x1, pos.y1, pos.x2, pos.y2)
gl.pushMatrix()
-- right now the homography mapping function
-- expects to map based on the full screen
-- size. So it centered to WIDTH/2, HEIGHT/2.
-- We modify the environment in a way so it
-- works across multiple screens.
gl.translate(pos.x1, pos.y1)
WIDTH = pos.x2 - pos.x1
HEIGHT = pos.y2 - pos.y1
mapped(function(width, height)
util.draw_correct(obj, 0, 0, width, height)
end)
gl.popMatrix()
-- reset to previous values. this is required for
-- scissors.disable to work properly.
WIDTH = NATIVE_WIDTH
HEIGHT = NATIVE_HEIGHT
scissors.disable()
end
local function write(msg)
gl.pushMatrix()
-- gl.translate(pos.x1+10, pos.y2-10)
-- gl.rotate(-90, 0, 0, 1)
gl.translate(pos.x1+10, pos.y2-30)
font:write(0, 0, msg, 24, 1,1,1,.5)
gl.popMatrix()
end
local function draw_tag()
black:draw(pos.x1, pos.y1, pos.x2, pos.y2, 0.8)
local ox1, oy1, ox2, oy2 = util.scale_into(
pos.x2 - pos.x1, pos.y2 - pos.y1, 10, 10
)
local tag_x = (tag_id-1) % 16
local tag_y = math.floor((tag_id-1) / 16)
local tag_w, tag_h = tags:size()
tags:draw(
pos.x1 + ox1,
pos.y1 + oy1,
pos.x1 + ox2,
pos.y1 + oy2,
1,
1/tag_w * (10*tag_x ), 1/tag_h * (10*tag_y ),
1/tag_w * (10*tag_x+10), 1/tag_h * (10*tag_y+10)
)
local row_h = (pos.y2-pos.y1) / 12
local screen_w = pos.x2 - pos.x1
local t = "info-beamer hosted"
local w = font:width(t, row_h)
font:write(pos.x1 + (screen_w-w)/2, pos.y1+row_h*0.05, t, row_h, 0,0,0,1)
for i, t in ipairs{
string.format("serial %s, HDMI%d", serial, screen_no-1),
"Take a mapping picture now",
} do
local w = font:width(t, row_h/2.2)
font:write(pos.x1 + (screen_w-w)/2, pos.y2-row_h+(i-1)*row_h/2.2, t, row_h/2.2, 0,0,0,1)
end
end
return {
update = update;
has_mapping = function()
return has_mapping
end;
write = write;
draw = draw;
draw_tag = draw_tag;
}
end
local function overlap(a, b)
return a.x1 < b.x2 and a.x2 > b.x1 and
a.y1 < b.y2 and a.y2 > b.y1
end
local screens = {}
if sys.displays then
-- info-beamer provides display position information?
-- check if we have an overlapping display configuration?
-- Only use primary display in that case.
if #sys.displays == 2 and overlap(sys.displays[1], sys.displays[2]) then
screens[#screens+1] = Screen(1, sys.displays[1])
else
-- Otherwise use all the displays
for i, display in ipairs(sys.displays) do
screens[#screens+1] = Screen(i, display)
end
end
else
-- fallback
screens[#screens+1] = Screen(1, {
x1 = 0,
y1 = 0,
x2 = WIDTH,
y2 = HEIGHT,
})
end
local function msg(str, ...)
for i, screen in ipairs(screens) do
screen.write(("[%s / %d] %s"):format(
serial, i, str:format(...)
))
end
end
local Image = {
slot_time = function(self)
return 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()
for i, screen in ipairs(screens) do
screen.draw(self.obj)
end
end;
stop = function(self)
if self.obj then
self.obj:dispose()
self.obj = nil
end
end;
}
local Video = {
slot_time = function(self)
return VIDEO_PRELOAD_TIME + self.duration
end;
prepare = function(self)
end;
tick = function(self, now)
if not self.obj then
self.obj = resource.load_video{
file = self.file:copy();
audio = audio,
paused = true;
}
end
if now < self.t_start + VIDEO_PRELOAD_TIME then
return
end
self.obj:start()
local state, w, h = self.obj:state()
if state ~= "loaded" and state ~= "finished" then
print[[
.-------------------------------------------.
WARNING:
lost video frame. video is most likely out
of sync. increase VIDEO_PRELOAD_TIME (on all
devices)
'--------------------------------------------'
]]
else
for i, screen in ipairs(screens) do
screen.draw(self.obj)
end
end
end;
stop = function(self)
if self.obj then
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 num_running = 0
local next_running = 99999999999999
if #items == 0 then
msg("no playlist configured")
return
end
for idx = 1, #items do
local item = items[idx]
if item.t_prepare <= now and item.state == "waiting" then
print(now, "preparing ", item.file)
item:prepare()
item.state = "prepared"
elseif item.t_start <= now and item.state == "prepared" then
print(now, "running ", item.file)
item.state = "running"
elseif item.t_end <= now and item.state == "running" then
print(now, "resetting ", item.file)
item:stop()
calc_start(idx, now)
item.state = "waiting"
end
next_running = math.min(next_running, item.t_start)
if item.state == "running" then
item:tick(now)
num_running = num_running + 1
end
end
if num_running == 0 then
local wait = next_running - now
msg("waiting for sync %.1f", 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)
local now = os.time()
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 = "waiting"
total_duration = total_duration + item:slot_time()
end
stop_all()
items = new_items
for idx = 1, #new_items do
calc_start(idx, now)
end
end
return {
set = set;
tick = tick;
}
end
local playlist = Playlist()
local function Stream()
local vid
local url
local function stop()
if vid then
vid:dispose()
end
vid = nil
end
local function start()
vid = resource.load_video{
file = url,
}
end
local function set(stream_url)
if stream_url == "" then
url = nil
stop()
return
end
if stream_url == url then
return
end
stop()
url = stream_url
start()
end
local function tick()
if not vid then
return
end
local state, w, h = vid:state()
if state == "loaded" then
for i, screen in ipairs(screens) do
screen.draw(vid)
end
elseif state == "finished" or state == "error" then
stop()
start()
end
end
local function has_stream()
return not not url
end
return {
set = set;
tick = tick;
has_stream = has_stream;
}
end
local stream = Stream()
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,
}
end
return playlist
end
local tag
util.json_watch("config.json", function(config)
tag = nil
assigned = false
for idx = 1, #config.screens do
local screen_config = config.screens[idx]
if screen_config.serial == serial then
for i, screen in ipairs(screens) do
screen.update(
idx + (i-1)*128,
i == 1 and screen_config.homography or screen_config.homography_secondary,
config.snapshot_w, config.snapshot_h
)
end
assigned = true
return
end
end
end)
util.json_watch("playlist/config.json", function(config)
local items = {}
for idx = 1, #config.playlist do
local item = config.playlist[idx]
items[#items+1] = {
file = resource.open_file('playlist/' .. item.file.asset_name),
type = item.file.type,
duration = item.duration,
}
end
playlist.set(prepare_playlist(items))
stream.set(config.stream)
audio = config.audio
node.gc()
end)
local function all_mapped()
for i, screen in ipairs(screens) do
if not screen.has_mapping() then
return false
end
end
return true
end
function node.render()
gl.clear(0,0,0,1)
if not assigned then
msg("Click on the setup, then 'Save' to start the configuration")
elseif stream.has_stream() then
stream.tick()
else
playlist.tick(os.time())
end
if assigned then
for i, screen in ipairs(screens) do
if not screen.has_mapping() then
screen.draw_tag()
end
end
end
end