-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjobject.c
665 lines (515 loc) · 18.7 KB
/
jobject.c
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/**
* osd2web plugin for the Video Disk Recorder
*
* jobject.c
*
* (c) 2017 Jörg Wendel
*
* This code is distributed under the terms and conditions of the
* GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
*
**/
//***************************************************************************
// Includes
//***************************************************************************
#include <vdr/plugin.h>
#include "lib/xml.h"
#include "update.h"
#include "scraper2vdr.h"
#include "radio.h"
//***************************************************************************
// Event To Json
//***************************************************************************
int isBigField(const char* name)
{
if (strstr(name, "description")) return yes;
if (strcmp(name, "actor") == 0) return yes;
if (strcmp(name, "other") == 0) return yes;
return no;
}
int event2Json(json_t* obj, const cEvent* event, const cChannel* channel,
eTimerMatch timerMatch, int current, cOsdService::ObjectShape shape)
{
if (channel && event)
{
json_t* oChannel = json_object();
channel2Json(oChannel, channel);
addToJson(obj, "channel", oChannel);
}
if (!event)
{
addToJson(obj, "eventid", na);
return done;
}
addToJson(obj, "eventid", event->EventID());
if (event->Schedule())
addToJson(obj, "channelid", event->ChannelID().ToString());
addToJson(obj, "title", event->Title());
addToJson(obj, "shorttext", event->ShortText());
addToJson(obj, "starttime", event->StartTime());
addToJson(obj, "endtime", event->EndTime());
addToJson(obj, "duration", event->Duration());
addToJson(obj, "runningstatus", event->RunningStatus());
addToJson(obj, "isrunning", event->IsRunning());
addToJson(obj, "parentalrating", event->ParentalRating());
addToJson(obj, "vps", event->Vps());
addToJson(obj, "hastimer", event->HasTimer());
addToJson(obj, "seen", event->Seen());
#if (defined (APIVERSNUM) && (APIVERSNUM >= 20304)) || (PATCHED)
if (!isEmpty(event->Aux()))
{
cXml xml;
int count = 0;
json_t* oEpg2Vdr = json_object();
if (xml.set(event->Aux()) == success)
{
for (XMLElement* e = xml.getFirst(); e; e = xml.getNext(e))
{
if (!isBigField(e->Name()) || current || shape & cOsdService::osLarge)
{
count++;
addToJson(oEpg2Vdr, e->Name(), e->GetText());
}
}
if (count)
addToJson(obj, "epg2vdr", oEpg2Vdr);
}
else
{
tell(0, "Info: Parsing of xml data in events 'aux' value failed");
}
}
#endif
if (current || shape & cOsdService::osLarge)
{
addToJson(obj, "description", event->Description());
}
// components
const cComponents* components = event->Components();
if (components && components->NumComponents())
{
json_t* oComponents = json_array();
for (int i = 0; i < components->NumComponents(); i++)
{
json_t* oComp = json_object();
addToJson(oComp, "stream", components->Component(i)->stream);
addToJson(oComp, "type", components->Component(i)->type);
addToJson(oComp, "language", components->Component(i)->language);
addToJson(oComp, "description", components->Component(i)->description);
json_array_append_new(oComponents, oComp);
}
addToJson(obj, "components", oComponents);
}
if (timerMatch == tmFull)
addToJson(obj, "timermatch", "full");
else if (timerMatch == tmPartial)
addToJson(obj, "timermatch", "partial");
else
addToJson(obj, "timermatch", "none");
// scraper data
std::string mediaPath;
std::string posterPath;
if (getScraperMediaPath(event, 0, mediaPath, posterPath) == success)
{
json_t* oScraper = json_object();
tell(4, "DEBUG: Got from scraper2vdr: banner path '%s'; poster path '%s'",
mediaPath.c_str(), posterPath.c_str());
if (mediaPath.length())
addToJson(oScraper, "banner", mediaPath.c_str());
if (posterPath.length())
addToJson(oScraper, "poster", posterPath.c_str());
if (mediaPath.length() || posterPath.length())
addToJson(obj, "scraper2vdr", oScraper);
}
return done;
}
//***************************************************************************
// Radio To Json
//***************************************************************************
int radio2Json(json_t* obj, std::list<std::string>* rdsTextList, json_t* oEvent)
{
static std::string lastRdsText = "";
cPlugin* radio = cPluginManager::GetPlugin("radio");
if (!radio)
return fail;
RadioTextService_v1_1 data;
if (!radio->Service(RADIO_TEXT_SERVICE1, &data))
return fail;
std::string description;
time_t lStart = time(0);
if (!data.rds_info)
return fail;
allTrim(data.rds_text);
allTrim(data.rds_pty_info);
// workaround, ignore text from radio plugin with invalid charset!
if (!jStringValid(data.rds_text.c_str()))
data.rds_text = lastRdsText; // invalid (wrong charset?), use last
// append to RDS text list
if (data.rds_text.length() && (!rdsTextList->size() || lastRdsText != data.rds_text))
{
lastRdsText = data.rds_text;
rdsTextList->push_back(lastRdsText);
}
if (data.title_start > 0)
lStart = data.title_start;
addToJson(obj, "starttime", lStart);
// did we have to fill the event also?
if (oEvent)
{
addToJson(oEvent, "title", data.rds_text.c_str());
addToJson(oEvent, "starttime", lStart);
}
// delete old lines from RDS text list (keep only last 20)
for (auto it = rdsTextList->begin(); rdsTextList->size() > 20 && it != rdsTextList->end();)
it = rdsTextList->erase(it);
// create description of RDS text lines
for (auto it = rdsTextList->rbegin(); it != rdsTextList->rend(); it++)
{
std::string s = *it;
description += s + "\n";
}
if (data.rds_info > 1)
{
allTrim(data.rds_title);
allTrim(data.rds_artist);
if (data.rds_title.length())
addToJson(obj, "title", data.rds_title.c_str());
if (data.rds_artist.length() && data.rds_artist != "---")
addToJson(obj, "artist", data.rds_artist.c_str());
}
addToJson(obj, "rdstext", description.c_str());
addToJson(obj, "category", "Radio");
if (data.rds_pty_info.length())
addToJson(obj, "genre", data.rds_pty_info.c_str());
if (data.bitrate.length())
addToJson(obj, "bitrate", data.bitrate.c_str());
return success;
}
//***************************************************************************
// Channel To Json
//***************************************************************************
int channel2Json(json_t* obj, const cChannel* channel)
{
// { "channelid": "S19.2E-1-1019-10301", "channelname": "Das Erste HD", "channelnumber": 1 }
addToJson(obj, "channelid", channel->GetChannelID().ToString());
addToJson(obj, "channelname", channel->Name());
addToJson(obj, "channelnumber", channel->Number());
addToJson(obj, "provider", channel->Provider());
return done;
}
//***************************************************************************
// Info of current Stream To Json
//***************************************************************************
int stream2Json(json_t* obj, const cChannel* channel)
{
addToJson(obj, "dolbidigital", (bool)channel->Dpid(0));
addToJson(obj, "multilang", (bool)channel->Apid(1));
addToJson(obj, "vtx", (bool)channel->Tpid());
addToJson(obj, "encrypted", (bool)channel->Ca());
addToJson(obj, "radio", channel->Vpid() == 0 || channel->Vpid() == 1 || channel->Vpid() == 0x1fff);
if (cDevice::PrimaryDevice())
{
int width = 0, height = 0; double aspect;
// only set with active output device, otherwise 0
cDevice::PrimaryDevice()->GetVideoSize(width, height, aspect);
addToJson(obj, "videoheight", height);
addToJson(obj, "videowidth", width);
}
return done;
}
//***************************************************************************
// Channels To Json
//***************************************************************************
int channels2Json(json_t* obj)
{
GET_CHANNELS_READ(channels);
for (const cChannel* channel = channels->First(); channel; channel = channels->Next(channel))
{
json_t* oChannel = json_object();
channel2Json(oChannel, channel);
json_array_append_new(obj, oChannel);
}
return success;
}
//***************************************************************************
// Recording To Json
//***************************************************************************
int recording2Json(json_t* obj, const cTimers* timers, const cRecording* recording,
cOsdService::cCuttingMarks* cuttingMarks,
cOsdService::ObjectShape shape)
{
int fps = 1;
if (!recording)
{
addToJson(obj, "name", "");
return done;
}
addToJson(obj, "name", recording->Name());
addToJson(obj, "folder", recording->Folder());
addToJson(obj, "basename", recording->BaseName());
addToJson(obj, "filename", recording->FileName());
addToJson(obj, "title", recording->Title());
addToJson(obj, "lengthinseconds", recording->LengthInSeconds());
addToJson(obj, "filesizemb", recording->FileSizeMB());
addToJson(obj, "isnew", recording->IsNew());
addToJson(obj, "isedited", recording->IsEdited());
addToJson(obj, "hasmarks", ((cRecording*)recording)->HasMarks()); // cast due to vdr 2.2.0 'const' issue
addToJson(obj, "start", recording->Start());
if (const cRecordingInfo* info = recording->Info())
{
eTimerMatch timerMatch = tmNone;
json_t* oInfo = json_object();
json_t* oEvent = json_object();
fps = info->FramesPerSecond();
addToJson(oInfo, "channelid", info->ChannelID().ToString());
addToJson(oInfo, "channelname", info->ChannelName());
addToJson(oInfo, "framespersecond", info->FramesPerSecond());
if (shape == cOsdService::ObjectShape::osLarge)
{
addToJson(oInfo, "description", info->Description());
addToJson(oInfo, "aux", info->Aux());
}
if (timers)
getTimerMatch(timers, info->GetEvent(), &timerMatch);
event2Json(oEvent, info->GetEvent(), 0, timerMatch, no, shape);
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
getRecordingDetails2Json(oEvent, recording->Id());
#endif
json_t* oImages = json_array();
imagePaths2Json(oImages, recording->FileName());
addToJson(obj, "images", oImages);
addToJson(obj, "info", oInfo);
addToJson(obj, "event", oEvent);
}
if (cuttingMarks && cuttingMarks->isSet)
{
json_t* oMarks = json_array();
cMutexLock lock(&cuttingMarks->mutex);
while (!cuttingMarks->queue.empty())
{
json_t* oMark = json_object();
addToJson(oMark, "position", cuttingMarks->queue.front().position / fps);
addToJson(oMark, "comment", cuttingMarks->queue.front().comment.c_str());
json_array_append_new(oMarks, oMark);
cuttingMarks->queue.pop();
}
addToJson(obj, "marks", oMarks);
cuttingMarks->isSet = no;
}
else if (((cRecording*)recording)->HasMarks())
{
json_t* oMarks = json_array();
cMarks marks;
marks.Load(recording->FileName(), recording->FramesPerSecond(), recording->IsPesRecording());
marks2Jason(&marks, oMarks, fps);
addToJson(obj, "marks", oMarks);
}
// scraper data
std::string mediaPath;
std::string posterPath;
if (getScraperMediaPath(0, recording, mediaPath, posterPath) == success)
{
json_t* oScraper = json_object();
tell(3, "DEBUG: Got from scraper2vdr: banner path '%s'; poster path '%s' recording '%s'",
mediaPath.c_str(), posterPath.c_str(), recording->Name());
if (mediaPath.length())
addToJson(oScraper, "banner", mediaPath.c_str());
if (posterPath.length())
addToJson(oScraper, "poster", posterPath.c_str());
if (mediaPath.length() || posterPath.length())
addToJson(obj, "scraper2vdr", oScraper);
}
else
{
tell(3, "DEBUG: No scraper2vdr data found for recording '%s'", recording->Name());
}
return success;
}
//***************************************************************************
// Marks To Jason
//***************************************************************************
int marks2Jason(const cMarks* marks, json_t* oMarks, int fps)
{
for (const cMark* mark = marks->First(); mark; mark = marks->Next(mark))
{
json_t* oMark = json_object();
addToJson(oMark, "position", mark->Position() / fps);
addToJson(oMark, "comment", mark->Comment());
json_array_append_new(oMarks, oMark);
}
return success;
}
//***************************************************************************
// Add Recording Details via epg2vdr Service
//***************************************************************************
int getRecordingDetails2Json(json_t* obj, int recId)
{
cPlugin* pEpg2Vdr = cPluginManager::GetPlugin("epg2vdr");
if (!pEpg2Vdr)
return done;
cEpgRecording_Details_Service_V1 data(recId);
if (pEpg2Vdr->Service(EPG2VDR_REC_DETAIL_SERVICE, &data))
{
cXml xml;
if (xml.set(data.details.c_str()) == success)
{
json_t* oEpg2Vdr = json_object();
for (XMLElement* e = xml.getFirst(); e; e = xml.getNext(e))
{
char* tmp = strdup(e->Name());
addToJson(oEpg2Vdr, toCase(cLower, tmp), e->GetText());
free(tmp);
}
if (xml.getFirst())
addToJson(obj, "epg2vdr", oEpg2Vdr);
}
else
{
tell(0, "Info: Parsing xml data got by 'recording detail service' failed");
}
}
return done;
}
//***************************************************************************
// Timer To Json
//***************************************************************************
int timer2Json(json_t* obj, const cTimer* timer)
{
if (!timer)
{
addToJson(obj, "id", na);
return done;
}
#if defined (APIVERSNUM) && (APIVERSNUM >= 20301)
addToJson(obj, "id", timer->Id());
addToJson(obj, "remote", timer->Remote());
#endif
addToJson(obj, "recording", timer->Recording());
addToJson(obj, "pending", timer->Pending());
addToJson(obj, "invpsmargin", timer->InVpsMargin());
addToJson(obj, "day", timer->Day());
addToJson(obj, "file", timer->File());
addToJson(obj, "aux", timer->Aux());
addToJson(obj, "expired", timer->Expired());
addToJson(obj, "starttime", timer->StartTime());
addToJson(obj, "stoptime", timer->StopTime());
addToJson(obj, "weekdays", timer->WeekDays());
addToJson(obj, "firstday", timer->FirstDay());
addToJson(obj, "flags", timer->Flags());
if (timer->Channel())
{
json_t* oChannel = json_object();
channel2Json(oChannel, timer->Channel());
addToJson(obj, "channel", oChannel);
}
if (timer->Event())
{
json_t* oEvent = json_object();
event2Json(oEvent, timer->Event(), 0, tmFull, no, cOsdService::osLarge);
addToJson(obj, "event", oEvent);
}
const cEpgTimer_Interface_V1* extendetTimer = dynamic_cast<const cEpgTimer_Interface_V1*>((cTimer*)timer);
if (extendetTimer)
{
char cStr[1+TB];
json_t* oEpg2Vdr = json_object();
addToJson(oEpg2Vdr, "id", extendetTimer->TimerId());
addToJson(oEpg2Vdr, "vdrname", extendetTimer->VdrName());
addToJson(oEpg2Vdr, "vdruuid", extendetTimer->VdrUuid());
addToJson(oEpg2Vdr, "vdrrunning", extendetTimer->isVdrRunning());
addToJson(oEpg2Vdr, "local", extendetTimer->isLocal());
addToJson(oEpg2Vdr, "state", c2s(extendetTimer->State(), cStr));
addToJson(oEpg2Vdr, "stateinfo", extendetTimer->StateInfo());
addToJson(oEpg2Vdr, "action", c2s(extendetTimer->Action(), cStr));
addToJson(obj, "epg2vdr", oEpg2Vdr);
}
else
{
tell(0, "Info: Cast to cEpgTimer_Interface_V1 failed - aussume epg2vdr not loaded");
}
return success;
}
//***************************************************************************
// Image Path To Json
//***************************************************************************
int imagePaths2Json(json_t* obj, const char* path, const char* suffixFilter)
{
DIR* dir;
if (!(dir = opendir(path)))
{
tell(1, "Can't open directory '%s', '%s'", path, strerror(errno));
return fail;
}
#ifndef HAVE_READDIR_R
dirent* pEntry;
while ((pEntry = readdir(dir)))
#else
dirent entry;
dirent* pEntry = &entry;
dirent* res;
// deprecated but the only reentrant with old libc!
while (readdir_r(dir, pEntry, &res) == 0 && res)
#endif
{
char* jpegPath = 0;
const char* suffix = suffixOf(pEntry->d_name);
if (dirTypeOf(path, pEntry) != DT_REG || pEntry->d_name[0] == '.')
continue;
if (isEmpty(suffix) || !strstr(suffixFilter, suffix))
continue;
asprintf(&jpegPath, "%s/%s", path, pEntry->d_name);
json_array_append_new(obj, json_string(jpegPath));
free(jpegPath);
}
closedir(dir);
return done;
}
//***************************************************************************
// Squeezebox Track to Json
//***************************************************************************
int squeezeboxTrack2Json(json_t* obj, TrackInfo* trackInfo)
{
addToJson(obj, "index", trackInfo->index);
addToJson(obj, "title", trackInfo->title);
addToJson(obj, "artist", trackInfo->artist);
addToJson(obj, "duration", trackInfo->duration);
addToJson(obj, "lyrics", trackInfo->lyrics);
// remote track?
if (trackInfo->remote)
{
addToJson(obj, "remote", trackInfo->remote);
addToJson(obj, "remoteTitle", trackInfo->remoteTitle);
addToJson(obj, "bitrate", trackInfo->bitrate);
}
else
{
addToJson(obj, "genre", trackInfo->genre);
addToJson(obj, "year", trackInfo->year);
addToJson(obj, "album", trackInfo->album);
}
return done;
}
//***************************************************************************
// Commands To Json
//***************************************************************************
int commands2Json(json_t*& obj)
{
const char* file {"/var/lib/vdr/plugins/osd2web/commands.json"};
json_error_t error;
if (fileExists(file))
{
tell(2, "Loading commands from file '%s'", file);
if (!(obj = json_load_file(file, 0, &error)))
{
tell(0, "Error: Ignoring invalid json in '%s'", file);
tell(0, "Error decoding json: %s (%s, line %d column %d, position %d)",
error.text, error.source, error.line, error.column, error.position);
obj = json_loads("{}", 0, &error);
}
}
else
{
obj = json_loads("{}", 0, &error);
}
return done;
}