-
Notifications
You must be signed in to change notification settings - Fork 1
/
VPXDisplayServer.cpp
591 lines (470 loc) · 17.8 KB
/
VPXDisplayServer.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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "VPXDisplayServer.h"
#include <plog/Log.h>
#include "inc/mongoose/mongoose.h"
#include "inc/mINI/ini.h"
#include "inc/subprocess/subprocess.h"
#include "inc/lodepng/lodepng.h"
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <X11/Xlib.h>
#include <libimagequant.h>
static VPXDisplayServer* g_pServer = NULL;
void VPXDisplayServer::Forward(struct mg_http_message *hm, struct mg_connection *c)
{
size_t i, max = sizeof(hm->headers) / sizeof(hm->headers[0]);
struct mg_str host = mg_url_host(g_pServer->m_szESUrl.c_str());
mg_printf(c, "%.*s ", (int) (hm->method.len), hm->method.ptr);
if (hm->uri.len > 0)
mg_printf(c, "%.*s ", (int) (hm->uri.len - 3), hm->uri.ptr + 3);
mg_printf(c, "%.*s\r\n", (int) (hm->proto.len), hm->proto.ptr);
for (i = 0; i < max && hm->headers[i].name.len > 0; i++) {
struct mg_str *k = &hm->headers[i].name, *v = &hm->headers[i].value;
if (mg_strcmp(*k, mg_str("Host")) == 0)
v = &host;
mg_printf(c, "%.*s: %.*s\r\n", (int) k->len, k->ptr, (int) v->len, v->ptr);
}
mg_send(c, "\r\n", 2);
mg_send(c, hm->body.ptr, hm->body.len);
}
void VPXDisplayServer::HandleEvent2(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
{
struct mg_connection *c2 = (struct mg_connection *) fn_data;
if (ev == MG_EV_READ) {
if (c2 != NULL)
mg_send(c2, c->recv.buf, c->recv.len);
mg_iobuf_del(&c->recv, 0, c->recv.len);
}
else if (ev == MG_EV_CLOSE) {
if (c2 != NULL)
c2->fn_data = NULL;
}
(void) ev_data;
}
void VPXDisplayServer::HandleEvent(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
{
struct mg_connection *c2 = (struct mg_connection *)fn_data;
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
if (mg_http_match_uri(hm, "/update"))
g_pServer->Update(c, ev_data);
else if (mg_http_match_uri(hm, "/reset"))
g_pServer->Reset(c, ev_data);
else if (mg_http_match_uri(hm, "/capture"))
g_pServer->Capture(c, ev_data);
else if (mg_http_match_uri(hm, "/capture-es"))
g_pServer->CaptureES(c, ev_data);
else if (!strncmp(hm->uri.ptr, "/es", 3)) {
c2 = mg_connect(c->mgr, g_pServer->m_szESUrl.c_str(), VPXDisplayServer::HandleEvent2, c);
if (c2) {
c->fn_data = c2;
Forward(hm, c2);
c->is_resp = 0;
c2->is_hexdumping = 0;
}
else {
mg_error(c, "Cannot create backend connection");
}
}
else {
string szHtmlFile = g_pServer->m_szBasePath + "assets/vpxds.html";
struct mg_http_serve_opts opts = {};
mg_http_serve_file(c, hm, szHtmlFile.c_str(), &opts);
}
return;
}
else if (ev == MG_EV_CLOSE) {
if (c2 != NULL) {
c2->is_closing = 1;
c2->fn_data = NULL;
}
}
}
VPXDisplayServer::VPXDisplayServer()
{
m_pBackglassDisplay = NULL;
m_pDMDDisplay = NULL;
}
VPXDisplayServer::~VPXDisplayServer()
{
if (m_pBackglassDisplay) {
CloseDisplay(m_pBackglassDisplay);
delete m_pBackglassDisplay;
}
if (m_pDMDDisplay) {
CloseDisplay(m_pDMDDisplay);
delete m_pDMDDisplay;
}
SDL_Quit();
}
void VPXDisplayServer::Update(struct mg_connection *c, void *ev_data)
{
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
char szDisplay[10];
mg_http_get_var(&hm->query, "display", szDisplay, sizeof(szDisplay));
VPXDisplay* pDisplay = NULL;
if (!strncasecmp("backglass", szDisplay, 9))
pDisplay = m_pBackglassDisplay;
else if (!strncasecmp("dmd", szDisplay, 3))
pDisplay = m_pDMDDisplay;
if (!pDisplay) {
PLOGE.printf("Invalid display: %s", szDisplay);
mg_http_reply(c, 400, "", "Bad request");
return;
}
char szPath[1024];
mg_http_get_var(&hm->query, "path", szPath, sizeof(szPath));
if (*szPath == '\0') {
mg_http_reply(c, 400, "", "Bad request");
return;
}
std::filesystem::path path = string(szPath);
string szImage = m_szCachePath + path.stem().string() + "-" + szDisplay + ".png";
if (pDisplay->pTexture) {
SDL_DestroyTexture(pDisplay->pTexture);
pDisplay->pTexture = NULL;
}
SDL_Surface* pSurface = IMG_Load(szImage.c_str());
if (!pSurface) {
PLOGE.printf("Invalid image: %s", szImage.c_str());
mg_http_reply(c, 400, "", "Bad request");
return;
}
pDisplay->pTexture = SDL_CreateTextureFromSurface(pDisplay->pRenderer, pSurface);
SDL_FreeSurface(pSurface);
PLOGI.printf("Display %s image set to %s", szDisplay, szImage.c_str());
mg_http_reply(c, 200, "", "display: %s, image: %s", szDisplay, szImage.c_str());
}
void VPXDisplayServer::Reset(struct mg_connection *c, void *ev_data)
{
if (m_pBackglassDisplay && m_pBackglassDisplay->pTexture) {
SDL_DestroyTexture(m_pBackglassDisplay->pTexture);
m_pBackglassDisplay->pTexture = NULL;
}
if (m_pDMDDisplay && m_pDMDDisplay->pTexture) {
SDL_DestroyTexture(m_pBackglassDisplay->pTexture);
m_pBackglassDisplay->pTexture = NULL;
}
mg_http_reply(c, 200, "", "");
}
void VPXDisplayServer::Capture(struct mg_connection *c, void *ev_data)
{
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
char szDisplay[10];
mg_http_get_var(&hm->query, "display", szDisplay, sizeof(szDisplay));
if (*szDisplay == '\0') {
mg_http_reply(c, 400, "", "Bad request");
return;
}
VPXDisplay* pDisplay = NULL;
if (!strncasecmp("backglass", szDisplay, 9))
pDisplay = m_pBackglassDisplay;
else if (!strncasecmp("dmd", szDisplay, 3))
pDisplay = m_pDMDDisplay;
if (!pDisplay) {
PLOGE.printf("Invalid display: %s", szDisplay);
mg_http_reply(c, 400, "", "Bad request");
return;
}
char szPath[1024];
mg_http_get_var(&hm->query, "path", szPath, sizeof(szPath));
if (*szPath == '\0') {
mg_http_reply(c, 400, "", "Bad request");
return;
}
std::filesystem::path path = string(szPath);
string szCapture = m_szCachePath + path.stem().string() + "-" + szDisplay + ".png";
// ffmpeg -f x11grab -video_size 1024x768 -i :0.0+1080,0 -vframes 1 "vpxfile-backglass.png"
string szSize = std::to_string(pDisplay->width) + "x" + std::to_string(pDisplay->height);
string szPosition = ":0.0+" + std::to_string(m_tableWidth) + ",0";
const char* command_line[] = {"/usr/bin/ffmpeg",
"-y",
"-f", "x11grab",
"-video_size", szSize.c_str(),
"-i", szPosition.c_str(),
"-vframes", "1",
szCapture.c_str(),
NULL};
const char* environment[] = {"DISPLAY=:0.0", NULL};
struct subprocess_s subprocess;
if (!subprocess_create_ex(command_line, 0, environment, &subprocess)) {
int process_return;
if (!subprocess_join(&subprocess, &process_return)) {
PLOGI.printf("process return: %d", process_return);
if (!process_return && ProcessPNG(szCapture)) {
mg_http_reply(c, 200, "", "");
return;
}
}
}
mg_http_reply(c, 500, "", "Server error");
}
void VPXDisplayServer::CaptureES(struct mg_connection *c, void *ev_data)
{
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
char type[128];
mg_http_get_var(&hm->query, "type", type, sizeof(type));
if (*type == '\0') {
mg_http_reply(c, 400, "", "Bad request");
return;
}
PLOGI.printf("ES Capture Requested : type=%s", type);
if (!strcmp(type, "image")) {
string szPath = m_szBasePath + "vpxds-tmp-image.png";
// ffmpeg -f x11grab -video_size 1080x1920 -i :0.0 -vframes 1 "vpxds-tmp-image.png"
string szSize = std::to_string(m_tableWidth) + "x" + std::to_string(m_tableHeight);
const char* command_line[] = {"/usr/bin/ffmpeg",
"-y",
"-f", "x11grab",
"-video_size", szSize.c_str(),
"-i", ":0.0",
"-vframes", "1",
szPath.c_str(),
NULL};
const char* environment[] = {"DISPLAY=:0.0", NULL};
struct subprocess_s subprocess;
if (!subprocess_create_ex(command_line, 0, environment, &subprocess)) {
int process_return;
if (!subprocess_join(&subprocess, &process_return)) {
PLOGI.printf("process returned: %d", process_return);
if (!process_return && ProcessPNG(szPath)) {
struct mg_http_serve_opts opts = {};
mg_http_serve_file(c, hm, szPath.c_str(), &opts);
return;
}
}
}
mg_http_reply(c, 500, "", "Server error");
}
else if (!strcmp(type, "video")) {
string szPath = m_szBasePath + "vpxds-tmp-video.mp4";
// ffmpeg -f x11grab -video_size 1080x1920 -framerate 30 -i :0.0 -c:v libx264 -preset ultrafast -profile:v main -level 3.1 -pix_fmt yuv420p -t 10 "vpxds-tmp-video.mp4"
string szSize = std::to_string(m_tableWidth) + "x" + std::to_string(m_tableHeight);
const char* command_line[] = {"/usr/bin/ffmpeg",
"-y",
"-f", "x11grab",
"-video_size", szSize.c_str(),
"-framerate", "30",
"-i", ":0.0",
"-c:v", "libx264",
"-preset", "slow",
"-profile:v", "main",
"-level", "3.1",
"-pix_fmt", "yuv420p",
"-t", "5",
szPath.c_str(),
NULL};
const char* environment[] = {"DISPLAY=:0.0", NULL};
struct subprocess_s subprocess;
if (!subprocess_create_ex(command_line, 0, environment, &subprocess)) {
int process_return;
if (!subprocess_join(&subprocess, &process_return)) {
PLOGI.printf("process returned: %d", process_return);
if (!process_return) {
struct mg_http_serve_opts opts = {};
mg_http_serve_file(c, hm, szPath.c_str(), &opts);
return;
}
}
}
mg_http_reply(c, 500, "", "Server error");
}
else {
mg_http_reply(c, 400, "", "Bad request");
}
}
int VPXDisplayServer::OpenDisplay(const string& szName, VPXDisplay* pDisplay)
{
PLOGI.printf("Creating %s window, x=%d, y=%d, width=%d, height=%d", szName.c_str(),
pDisplay->x, pDisplay->y, pDisplay->width, pDisplay->height);
pDisplay->pWindow = SDL_CreateWindow(szName.c_str(), pDisplay->x, pDisplay->y, pDisplay->width, pDisplay->height,
SDL_WINDOW_SKIP_TASKBAR | SDL_WINDOW_BORDERLESS | SDL_WINDOW_UTILITY | SDL_WINDOW_SHOWN);
if (!pDisplay->pWindow) {
PLOGE << "SDL_CreateWindow Error: " << SDL_GetError();
return 1;
}
pDisplay->pRenderer = SDL_CreateRenderer(pDisplay->pWindow, -1, SDL_RENDERER_ACCELERATED);
if (!pDisplay->pRenderer) {
PLOGE << "SDL_CreateRenderer Error: " << SDL_GetError();
return 1;
}
return 0;
}
void VPXDisplayServer::CloseDisplay(VPXDisplay* pDisplay)
{
if (pDisplay->pTexture) {
SDL_DestroyTexture(pDisplay->pTexture);
pDisplay->pTexture = NULL;
}
if (pDisplay->pRenderer) {
SDL_DestroyRenderer(pDisplay->pRenderer);
pDisplay->pRenderer = NULL;
}
if (pDisplay->pWindow) {
SDL_DestroyWindow(pDisplay->pWindow);
pDisplay->pWindow = NULL;
}
}
void VPXDisplayServer::RenderDisplay(VPXDisplay* pDisplay)
{
if (!pDisplay)
return;
SDL_SetRenderDrawColor(pDisplay->pRenderer, 0, 0, 0, 255);
SDL_RenderClear(pDisplay->pRenderer);
if (pDisplay->pTexture)
SDL_RenderCopy(pDisplay->pRenderer, pDisplay->pTexture, NULL, NULL);
SDL_RenderPresent(pDisplay->pRenderer);
}
void VPXDisplayServer::LoadINI()
{
string szIniFile = m_szBasePath + "vpxds.ini";
PLOGI.printf("Loading ini at: %s", szIniFile.c_str());
mINI::INIFile file(szIniFile);
mINI::INIStructure ini;
file.read(ini);
if (!ini.has("Settings")) {
PLOGE << "Missing settings entry";
return;
}
if (ini["Settings"].has("ESURL"))
m_szESUrl = ini["Settings"]["ESURL"];
if (!ini["Settings"].has("TableWidth") ||
!ini["Settings"].has("TableHeight")) {
PLOGE << "Missing table width or height";
}
m_tableWidth = atoll(ini["Settings"]["TableWidth"].c_str());
m_tableHeight = atoll(ini["Settings"]["TableHeight"].c_str());
if (ini["Settings"].has("CachePath")) {
m_szCachePath = ini["settings"]["CachePath"];
if (!m_szCachePath.ends_with("/"))
m_szCachePath += "/";
PLOGI.printf("Cache path set to: %s", m_szCachePath.c_str());
}
if (ini["Settings"].has("Backglass")) {
if (atoll(ini["settings"]["Backglass"].c_str()) == 1) {
if (ini["Settings"].has("BackglassX") ||
ini["Settings"].has("BackglassY") ||
ini["Settings"].has("BackglassWidth") ||
ini["Settings"].has("BackglassHeight")) {
m_pBackglassDisplay = new VPXDisplay();
memset(m_pBackglassDisplay, 0, sizeof(VPXDisplay));
m_pBackglassDisplay->x = atoll(ini["Settings"]["BackglassX"].c_str());
m_pBackglassDisplay->y = atoll(ini["Settings"]["BackglassY"].c_str());
m_pBackglassDisplay->width = atoll(ini["Settings"]["BackglassWidth"].c_str());
m_pBackglassDisplay->height = atoll(ini["Settings"]["BackglassHeight"].c_str());
}
}
}
if (ini["Settings"].has("DMD")) {
if (atoll(ini["settings"]["DMD"].c_str()) == 1) {
if (ini["Settings"].has("DMDX") ||
ini["Settings"].has("DMDY") ||
ini["Settings"].has("DMDWidth") ||
ini["Settings"].has("DMDHeight")) {
m_pDMDDisplay = new VPXDisplay();
memset(m_pDMDDisplay, 0, sizeof(VPXDisplay));
m_pDMDDisplay->x = atoll(ini["Settings"]["DMDX"].c_str());
m_pDMDDisplay->y = atoll(ini["Settings"]["DMDY"].c_str());
m_pDMDDisplay->width = atoll(ini["Settings"]["DMDWidth"].c_str());
m_pDMDDisplay->height = atoll(ini["Settings"]["DMDHeight"].c_str());
}
}
}
}
bool VPXDisplayServer::ProcessPNG(const std::string& filename)
{
unsigned int width;
unsigned int height;
unsigned char* pPixels;
if (lodepng_decode32_file(&pPixels, &width, &height, filename.c_str()))
return false;
liq_attr* pHandle = liq_attr_create();
liq_image* pImage = liq_image_create_rgba(pHandle, pPixels, width, height, 0);
liq_result* pQuantizationResult;
if (liq_image_quantize(pImage, pHandle, &pQuantizationResult) != LIQ_OK)
return false;
size_t pixelsSize = width * height;
unsigned char* pPixels8 = (unsigned char *)malloc(pixelsSize);
liq_set_dithering_level(pQuantizationResult, 1.0);
liq_write_remapped_image(pQuantizationResult, pImage, pPixels8, pixelsSize);
const liq_palette* pPalette = liq_get_palette(pQuantizationResult);
LodePNGState state;
lodepng_state_init(&state);
state.info_raw.colortype = LCT_PALETTE;
state.info_raw.bitdepth = 8;
state.info_png.color.colortype = LCT_PALETTE;
state.info_png.color.bitdepth = 8;
for (int i = 0; i < pPalette->count; i++) {
lodepng_palette_add(&state.info_png.color, pPalette->entries[i].r, pPalette->entries[i].g, pPalette->entries[i].b, pPalette->entries[i].a);
lodepng_palette_add(&state.info_raw, pPalette->entries[i].r, pPalette->entries[i].g, pPalette->entries[i].b, pPalette->entries[i].a);
}
unsigned char* pOutput;
size_t outputSize;
if (lodepng_encode(&pOutput, &outputSize, pPixels8, width, height, &state))
return false;
FILE *fp = fopen(filename.c_str(), "wb");
if (!fp)
return false;
fwrite(pOutput, 1, outputSize, fp);
fclose(fp);
liq_result_destroy(pQuantizationResult);
liq_image_destroy(pImage);
liq_attr_destroy(pHandle);
free(pPixels8);
lodepng_state_cleanup(&state);
return true;
}
int VPXDisplayServer::Start()
{
PLOGI << "Starting VPXDS...";
LoadINI();
Window focusedWindow;
Display* pXDisplay = XOpenDisplay(NULL);
if (pXDisplay) {
int revertTo;
XGetInputFocus(pXDisplay, &focusedWindow, &revertTo);
PLOGI.printf("Currently focused window: %d", focusedWindow);
}
if (m_pBackglassDisplay) {
if (OpenDisplay("vpxds_backglass", m_pBackglassDisplay)) {
CloseDisplay(m_pBackglassDisplay);
delete m_pBackglassDisplay;
m_pBackglassDisplay = NULL;
}
}
if (m_pDMDDisplay) {
if (OpenDisplay("vpxds_dmd", m_pDMDDisplay)) {
CloseDisplay(m_pDMDDisplay);
delete m_pDMDDisplay;
m_pDMDDisplay = NULL;
}
}
if (pXDisplay) {
PLOGI.printf("Returning focus to window: %d", focusedWindow);
XSetInputFocus(pXDisplay, focusedWindow, RevertToParent, CurrentTime);
XCloseDisplay(pXDisplay);
}
PLOGI << "Starting server started on port 8111";
g_pServer = this;
mg_log_set(MG_LL_NONE);
struct mg_mgr mgr;
struct mg_connection* conn;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0:8111", VPXDisplayServer::HandleEvent, NULL);
if (m_szESUrl.empty())
m_szESUrl = "http://127.0.0.1:1234";
PLOGI << "ES URL: " << m_szESUrl;
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
quit = true;
}
RenderDisplay(m_pBackglassDisplay);
RenderDisplay(m_pDMDDisplay);
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}