-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebosdpage.cpp
427 lines (340 loc) · 12.3 KB
/
webosdpage.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
#include <vdr/device.h>
#include <vdr/remote.h>
#include <Magick++/Blob.h>
#include <Magick++/Image.h>
#include <chrono>
#include "webosdpage.h"
#include "browserclient.h"
// #define MEASURE_SCALE_TIME 1
#define QOI_IMPLEMENTATION
#include "qoi.h"
WebOSDPage *webOsdPage;
WebOSDPage *playerWebOsdPage;
OSD_MODE currentOsdMode = CLOSED;
// initialize keyMap
std::map<int, std::string> keyMap({
{kUp, "VK_UP"},
{kDown, "VK_DOWN"},
{kLeft, "VK_LEFT"},
{kRight, "VK_RIGHT"},
{kOk, "VK_ENTER"},
{kBack, "VK_BACK"},
{kRed, "VK_RED"},
{kGreen, "VK_GREEN"},
{kYellow, "VK_YELLOW"},
{kBlue, "VK_BLUE"},
{k0, "VK_0"},
{k1, "VK_1"},
{k2, "VK_2"},
{k3, "VK_3"},
{k4, "VK_4"},
{k5, "VK_5"},
{k6, "VK_6"},
{k7, "VK_7"},
{k8, "VK_8"},
{k9, "VK_9"},
{kPlay, "VK_PLAY"},
{kPause, "VK_PAUSE"},
{kStop, "VK_STOP"},
{kFastFwd, "VK_FAST_FWD"},
{kFastRew, "VK_REWIND"},
{kNext, "VK_PAGE_DOWN"},
{kPrev, "VK_PAGE_UP"},
});
WebOSDPage *WebOSDPage::Create(bool useOutputDeviceScale, OSD_MODE osdMode) {
switch(osdMode) {
case OSD:
if (webOsdPage == nullptr) {
webOsdPage = new WebOSDPage(useOutputDeviceScale, osdMode);
}
currentOsdMode = OSD;
return webOsdPage;
case PLAYER:
if (playerWebOsdPage == nullptr) {
playerWebOsdPage = new WebOSDPage(useOutputDeviceScale, osdMode);
}
currentOsdMode = PLAYER;
return playerWebOsdPage;
case CLOSED:
return nullptr;
}
return nullptr;
}
WebOSDPage *WebOSDPage::Get() {
if (currentOsdMode == OSD) {
// dsyslog("[vdrweb] WebOSDPage::Get(%d): Return webOsdPage %p", (int)currentOsdMode, webOsdPage);
return webOsdPage;
} else if (currentOsdMode == PLAYER) {
// dsyslog("[vdrweb] WebOSDPage::Get(%d): Return playerWebOsdPage %p", (int)currentOsdMode, playerWebOsdPage);
return playerWebOsdPage;
}
// dsyslog("[vdrweb] WebOSDPage::Get(-1): Return nullptr");
return nullptr;
}
bool WebOSDPage::IsOpen() {
return (currentOsdMode == OSD && webOsdPage != nullptr)
|| (currentOsdMode == PLAYER && playerWebOsdPage != nullptr);
}
WebOSDPage::WebOSDPage(bool useOutputDeviceScale, OSD_MODE osdMode)
: cControl(nullptr), useOutputDeviceScale(useOutputDeviceScale)
{
dsyslog("[vdrweb] Create WebOSDPage, osdMode %d", (int)osdMode);
osd = nullptr;
pixmap = nullptr;
disp_height = 1920;
disp_width = 1080;
lastVolume = -1;
lastVolumeTime = time(NULL);
currentMode = osdMode;
}
WebOSDPage::~WebOSDPage() {
dsyslog("[vdrweb] Destruct WebOSDPage, osdMode %d", (int)currentMode);
if (osd != nullptr) {
cOsd *osdtmp = osd;
osd = nullptr;
if (pixmap != nullptr) {
osdtmp->DestroyPixmap(pixmap);
}
delete osdtmp;
}
pixmap = nullptr;
switch (currentMode) {
case OSD:
webOsdPage = nullptr;
break;
case PLAYER:
playerWebOsdPage = nullptr;
break;
case CLOSED:
break;
}
// cDevice::PrimaryDevice()->ScaleVideo(cRect::Null);
}
void WebOSDPage::Show() {
dsyslog("[vdrweb] WebOSDPage show\n");
Display();
}
void WebOSDPage::Display() {
dsyslog("[vdrweb] WebOSDPage Display\n");
if (osd) {
if (pixmap != nullptr) {
osd->DestroyPixmap(pixmap);
pixmap = nullptr;
}
delete osd;
}
osd = cOsdProvider::NewOsd(0, 0, 5);
double ph;
cDevice::PrimaryDevice()->GetOsdSize(disp_width, disp_height, ph);
tArea area {0, 0, disp_width - 1, disp_height - 1, 32};
auto areaResult = osd->SetAreas(&area, 1);
if (areaResult == oeOk) {
isyslog("[vdrweb] Area size set to %d:%d - %d:%d", 0, 0, disp_width - 1, disp_height - 1);
}
SetOsdSize();
browserClient->ReloadOSD();
}
void WebOSDPage::SetOsdSize() {
dsyslog("[vdrweb] webospage SetOsdSize()");
if (pixmap != nullptr) {
dsyslog("[vdrweb] webosdpage SetOsdSize, Destroy old pixmap");
osd->DestroyPixmap(pixmap);
pixmap = nullptr;
}
dsyslog("[vdrweb] webosdpage SetOsdSize, Get new OSD size");
double ph;
cDevice::PrimaryDevice()->GetOsdSize(disp_width, disp_height, ph);
if (disp_width <= 0 || disp_height <= 0 || disp_width > 3840 || disp_height > 2160) {
esyslog("[vdrweb] Got illegal OSD size %dx%d", disp_width, disp_height);
return;
}
cRect rect(0, 0, disp_width, disp_height);
// try to get a pixmap
dsyslog("[vdrweb] webosdpage SetOsdSize, Create pixmap %dx%d", disp_width, disp_height);
pixmap = osd->CreatePixmap(5, rect, rect);
dsyslog("[vdrweb] webosdpage SetOsdSize, Clear Pixmap");
LOCK_PIXMAPS;
pixmap->Clear();
}
bool WebOSDPage::drawImage(uint8_t* image, int render_width, int render_height, int x, int y, int width, int height) {
#ifdef DEBUG_SAVE_OSD_IMAGE
static int osd_image_number = 0;
// the qoi encoder expects the RGBA pixel format.
// For debugging purposes make a copy of the buffer and change the pixel format
uint32_t* image_copy = new uint32_t[width * height];
uint32_t* image_u32 = (uint32_t*) image;
for (int i = 0; i < width * height; ++i) {
// Source: BGRA = 0xAARRGGBB
// Dest: RGBA = 0xAABBGGRR
image_copy[i] =
((image_u32[i] & 0xFF00FF00) ) | // AA__GG__
((image_u32[i] & 0x00FF0000) >> 16) | // __RR____ -> ______RR
((image_u32[i] & 0x000000FF) << 16); // ______BB -> __BB____
}
std::string filename = std::string("/tmp/osd_image_") + std::to_string(osd_image_number) + ".qoi";
qoi_desc desc {
.width = static_cast<unsigned int>(width),
.height = static_cast<unsigned int>(height),
.channels = 4,
.colorspace = QOI_LINEAR
};
qoi_write(filename.c_str(), image_copy, &desc);
osd_image_number++;
delete(image_copy);
#endif
return scaleAndPaint(image, render_width, render_height, x, y, width, height);
}
bool WebOSDPage::drawImageQOI(const std::string& qoibuffer) {
std::string::size_type pos1 = 0, pos2 = 0, pos_rw = 0, pos_rh = 0;
// extract render_width, render_height
pos_rw = qoibuffer.find(':', 0);
int render_width = std::atoi(qoibuffer.substr(0, pos_rw).c_str());
pos_rh = qoibuffer.find(':', pos_rw + 1);
int render_height = std::atoi(qoibuffer.substr(pos_rw + 1, pos_rh).c_str());
// extract x,y
pos1 = qoibuffer.find(':', pos_rh + 1);
int x = std::atoi(qoibuffer.substr(pos_rh + 1, pos1).c_str());
pos2 = qoibuffer.find(':', pos1 + 1);
int y = std::atoi(qoibuffer.substr(pos1 + 1, pos2).c_str());
// decode image data
qoi_desc desc;
void *image = qoi_decode(qoibuffer.c_str() + pos2 + 1, (int)qoibuffer.size() - pos2 - 1, &desc, 4);
if (image == nullptr) {
// something failed
esyslog("[vdrweb] failed to decode qoi OSD image");
return false;
}
bool retValue = scaleAndPaint(static_cast<uint8_t *>(image), render_width, render_height, x, y, (int)desc.width, (int)desc.height);
free(image);
return retValue;
}
bool WebOSDPage::scaleAndPaint(uint8_t* image, int render_width, int render_height, int x, int y, int width, int height) {
// sanity check
if (width > 3840 || height > 2160 || width <= 0 || height <= 0) {
return false;
}
bool scaleRequired = (disp_width != render_width) || (disp_height != render_height);
// dsyslog("RenderSize: %d x %d, DisplaySize: %d x %d, x,y=%d,%d, Scaling required: %s", render_width, render_height, disp_width, disp_height, x, y, (scaleRequired ? "yes" : "no"));
if (!scaleRequired) {
// dsyslog("[vdrweb] no scaling required");
// scaling is not needed. Draw the image as is.
cPoint recPoint(x, y);
const cImage recImage(cSize(width, height), (const tColor *)image);
if (pixmap != nullptr) {
LOCK_PIXMAPS;
pixmap->DrawImage(recPoint, recImage);
} else {
esyslog("[vdrweb] Pixmap is null. OSD not available");
}
if (osd != nullptr) {
osd->Flush();
}
return true;
}
// calculate scale factor
double scalex = disp_width / (double)render_width;
double scaley = disp_height / (double)render_height;
// calculate coordinates
int osd_x = (int)lround(scalex * x);
int osd_y = (int)lround(scaley * y);
int osd_width = (int)lround(scalex * width);
int osd_height = (int)lround(scaley * height);
cPoint recPoint(osd_x, osd_y);
#ifdef ENABLE_FAST_SCALE
if (useOutputDeviceScale) {
// dsyslog("[vdrweb] scaling using outputdevice");
const cImage recImage(cSize(width, height), (const tColor *)image);
if (pixmap != nullptr) {
LOCK_PIXMAPS;
pixmap->DrawScaledImage(recPoint, recImage, scalex, scaley, true);
} else {
esyslog("[vdrweb] Pixmap is null. OSD not available");
}
} else {
#endif // ENABLE_FAST_SCALE
// dsyslog("[vdrweb] scaling using GraphicsMagick");
// create image buffer for scaled image
cSize recImageSize(osd_width, osd_height);
const cImage recImage(recImageSize);
auto *scaled = (uint8_t *) (recImage.Data());
if (scaled == nullptr) {
esyslog("[vdrweb] Out of memory reading OSD image");
return true;
}
#ifdef MEASURE_SCALE_TIME
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
#endif
Magick::Image mimage;
mimage.read(width, height, "RGBA", static_cast<const MagickLib::StorageType>(0), image);
mimage.sample(Magick::Geometry(osd_width, osd_height));
mimage.write ( 0, 0, osd_width, osd_height, "RGBA", static_cast<const MagickLib::StorageType>(0), scaled);
#ifdef MEASURE_SCALE_TIME
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
dsyslog("[vdrweb] Scale time = %lu [ms]", std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count());
#endif
if (pixmap != nullptr) {
LOCK_PIXMAPS;
pixmap->DrawImage(recPoint, recImage);
} else {
esyslog("[vdrweb] Pixmap is null. OSD not available");
}
#ifdef ENABLE_FAST_SCALE
}
#endif
if (osd != nullptr) {
osd->Flush();
}
return true;
}
eOSState WebOSDPage::ProcessKey(eKeys Key) {
// check if volumebar is displayed and kill it after a timeout
// a volume change resets the timeout
if (pixmapVol && (time(NULL) - lastVolumeTime > 3))
DeleteVolume();
auto search = ::keyMap.find(Key);
if (search != ::keyMap.end()) {
browserClient->ProcessKey(search->second);
return osContinue;
}
return osContinue;
}
void WebOSDPage::Hide() {
dsyslog("[vdrweb] WebOSDPage::Hide...");
}
void WebOSDPage::DrawVolume(int volume) {
dsyslog("[vdrweb] WebOSDPage::DrawVolume...");
if (volume != lastVolume) {
int x = 0; // volumebar x
int y = 20; // volumebar y
int w = disp_width; // volumebar width
int h = 20; // volumebar height
if (!pixmapVol) {
pixmapVol = osd->CreatePixmap(7, cRect(x, y, w, h));
pixmapVol->Fill(clrTransparent);
}
if (volume) {
int p = (w - 1) * volume / MAXVOLUME;
pixmapVol->DrawRectangle(cRect(0, 0, p - 1, h - 1), clrVolumeBarLower);
pixmapVol->DrawRectangle(cRect(p, 0, w - 1, h - 1), clrVolumeBarUpper);
} else {
pixmapVol->DrawRectangle(cRect(0, 0, w - 1, h - 1), clrVolumeBarUpper);
}
lastVolume = volume;
// (re)set the display volumebar timeout
lastVolumeTime = time(NULL);
if (osd)
osd->Flush();
}
}
void WebOSDPage::DeleteVolume(void) {
dsyslog("[vdrweb] WebOSDPage::DeleteVolume...: %s", pixmapVol != nullptr ? "pixmapVol != null" : "pixmapVol == null");
if (pixmapVol) {
osd->DestroyPixmap(pixmapVol);
osd->Flush();
pixmapVol = nullptr;
}
}
cOsdObject *WebOSDPage::GetInfo() {
dsyslog("[vdrweb] WebOSDPage::GetInfo...");
return this;
}