-
Notifications
You must be signed in to change notification settings - Fork 14
/
PVRIptvData.cpp
1255 lines (1086 loc) · 37.6 KB
/
PVRIptvData.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018~now Palo Kisa <palo.kisa@gmail.com>
*
* Copyright (C) 2014 Josef Rokos
* http://github.com/PepaRokos/xbmc-pvr-addons/
*
* Copyright (C) 2011 Pulse-Eight
* http://www.pulse-eight.com/
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this addon; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include <set>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <json/json.h>
#include <chrono>
#include <algorithm>
#include <functional>
#include "PVRIptvData.h"
#include "apimanager.h"
#include "CallLimiter.hh"
#if defined(TARGET_WINDOWS)
# define LOCALTIME_R(src, dst) localtime_s(dst, src)
# define GMTIME_R(src, dst) gmtime_s(dst, src)
#else
# define LOCALTIME_R(src, dst) localtime_r(src, dst)
# define GMTIME_R(src, dst) gmtime_r(src, dst)
#endif
using namespace std;
using namespace ADDON;
template <int N> void strAssign(char (&dst)[N], const std::string & src)
{
strncpy(dst, src.c_str(), N - 1);
dst[N - 1] = '\0'; // just to be sure
}
static void xbmcStrFree(char * str)
{
XBMC->FreeString(str);
}
static unsigned DiffBetweenPragueAndLocalTime(const time_t * when = nullptr)
{
time_t tloc;
if (0 == when)
time(&tloc);
else
tloc = *when;
struct tm tm1;
LOCALTIME_R(&tloc, &tm1);
auto isdst = tm1.tm_isdst;
GMTIME_R(&tloc, &tm1);
tm1.tm_isdst = isdst;
time_t t2 = mktime(&tm1);
// Note: Prague(Czech) is in Central Europe Time -> CET or CEST == UTC+1 or UTC+2 == +3600 or +7200
return tloc - t2 - (isdst > 0 ? 7200 : 3600);
}
PVRIptvData::PVRIptvData(PVRIptvConfiguration cfg)
: m_bKeepAlive{true}
, m_bLoadRecordings{true}
, m_bChannelsLoaded{false}
, m_groups{std::make_shared<group_container_t>()}
, m_channels{std::make_shared<channel_container_t>()}
, m_epg{std::make_shared<epg_container_t>()}
, m_recordings{std::make_shared<recording_container_t>()}
, m_timers{std::make_shared<timer_container_t>()}
, m_recordingAvailableDuration{0}
, m_recordingRecordedDuration{0}
, m_epgMinTime{time(nullptr)}
, m_epgMaxTime{time(nullptr) + 3600}
, m_epgMaxDays{cfg.epgMaxDays}
, m_bEGPLoaded{false}
, m_iLastStart{0}
, m_iLastEnd{0}
, m_streamQuality{static_cast<ApiManager::StreamQuality_t>(cfg.streamQuality)}
, m_fullChannelEpgRefresh{cfg.fullChannelEpgRefresh}
, m_loadingsRefresh{cfg.loadingsRefresh}
, m_keepAliveDelay{cfg.keepAliveDelay}
, m_epgCheckDelay{cfg.epgCheckDelay}
, m_useH265{cfg.useH265}
, m_useAdaptive{cfg.useAdaptive}
, m_showLockedChannels{cfg.showLockedChannels}
, m_manager{std::move(cfg.userName), std::move(cfg.password)}
{
SetEPGTimeFrame(m_epgMaxDays);
CreateThread();
}
bool PVRIptvData::LoadRecordingsJob()
{
if (!KeepAlive())
return false;
bool load = false;
{
std::lock_guard<std::mutex> critical(m_mutex);
if (m_bLoadRecordings)
{
load = true;
m_bLoadRecordings = false;
}
}
if (load)
{
LoadRecordings();
}
return load;
}
void PVRIptvData::SetLoadRecordings()
{
std::lock_guard<std::mutex> critical(m_mutex);
m_bLoadRecordings = true;
}
void PVRIptvData::TriggerFullRefresh()
{
XBMC->Log(LOG_INFO, "%s triggering channels/EGP full refresh", __FUNCTION__);
m_iLastEnd = 0;
m_iLastStart = 0;
int epg_max_days = 0;
{
std::lock_guard<std::mutex> critical(m_mutex);
epg_max_days = m_epgMaxDays;
}
SetEPGTimeFrame(epg_max_days);
LoadPlayList();
}
bool PVRIptvData::LoadEPGJob()
{
XBMC->Log(LOG_INFO, "%s will check if EGP loading needed", __FUNCTION__);
time_t min_epg, max_epg;
{
std::lock_guard<std::mutex> critical(m_mutex);
min_epg = m_epgMinTime;
max_epg = m_epgMaxTime;
}
bool updated = false;
if (KeepAlive() && 0 == m_iLastEnd)
{
// the first run...load just needed data as soon as posible
LoadEPG(time(nullptr), true);
updated = true;
} else
{
if (KeepAlive() && max_epg > m_iLastEnd)
{
LoadEPG(m_iLastEnd, max_epg - m_iLastEnd <= 3600);
updated = true;
}
if (KeepAlive() && min_epg < m_iLastStart)
{
LoadEPG(m_iLastStart - 86400, false);
updated = true;
}
}
if (KeepAlive())
ReleaseUnneededEPG();
return updated;
}
void PVRIptvData::ReleaseUnneededEPG()
{
decltype (m_epg) epg;
time_t min_epg, max_epg;
{
std::lock_guard<std::mutex> critical(m_mutex);
min_epg = m_epgMinTime;
max_epg = m_epgMaxTime;
epg = m_epg;
}
auto epg_copy = std::make_shared<epg_container_t>();
XBMC->Log(LOG_DEBUG, "%s min_epg=%s max_epg=%s", __FUNCTION__, ApiManager::formatTime(min_epg).c_str(), ApiManager::formatTime(max_epg).c_str());
for (const auto & epg_channel : *epg)
{
auto & epg_data = epg_channel.second.epg;
std::vector<time_t> to_delete;
for (auto entry_i = epg_data.cbegin(); entry_i != epg_data.cend(); ++entry_i)
{
const PVRIptvEpgEntry & entry = entry_i->second;
if (entry_i->second.startTime > max_epg || entry_i->second.endTime < min_epg)
{
XBMC->Log(LOG_DEBUG, "Removing TV show: %s - %s, start=%s end=%s", epg_channel.second.strName.c_str(), entry.strTitle.c_str()
, ApiManager::formatTime(entry.startTime).c_str(), ApiManager::formatTime(entry.endTime).c_str());
// notify about the epg change...and delete it
EPG_TAG tag;
memset(&tag, 0, sizeof(EPG_TAG));
tag.iUniqueBroadcastId = entry.iBroadcastId;
tag.iUniqueChannelId = entry.iChannelId;
PVR->EpgEventStateChange(&tag, EPG_EVENT_DELETED);
to_delete.push_back(entry_i->first);
}
}
if (!to_delete.empty())
{
auto & epg_copy_channel = (*epg_copy)[epg_channel.first];
epg_copy_channel = epg_channel.second;
for (const auto key_delete : to_delete)
{
epg_copy_channel.epg.erase(key_delete);
}
}
}
// check if something deleted, if so make copy and atomically reassign
if (!epg_copy->empty())
{
for (const auto & epg_channel : *epg)
{
if (epg_copy->count(epg_channel.first) <= 0)
(*epg_copy)[epg_channel.first] = epg_channel.second;
}
{
std::lock_guard<std::mutex> critical(m_mutex);
m_epg = std::move(epg_copy);
}
}
// narrow the loaded time info (if needed)
m_iLastStart = std::max(m_iLastStart, min_epg);
m_iLastEnd = std::min(m_iLastEnd, max_epg);
}
void PVRIptvData::KeepAliveJob()
{
if (!KeepAlive())
return;
XBMC->Log(LOG_DEBUG, "keepAlive:: trigger");
if (!m_manager.keepAlive())
{
LoginLoop();
}
}
void PVRIptvData::LoginLoop()
{
unsigned login_delay = 0;
for (bool should_try = true; KeepAlive() && should_try; --login_delay)
{
if (0 >= login_delay)
{
if (m_manager.login())
should_try = false;
else
login_delay = 30; // try in 30 seconds
}
Sleep(1000);
}
}
bool PVRIptvData::WaitForChannels() const
{
std::unique_lock<std::mutex> critical(m_mutex);
return m_waitCond.wait_for(critical, std::chrono::seconds{5}, [this] { return m_bChannelsLoaded; });
}
void *PVRIptvData::Process(void)
{
XBMC->Log(LOG_DEBUG, "keepAlive:: thread started");
LoginLoop();
LoadPlayList();
bool epg_updated = false;
auto keep_alive_job = getCallLimiter(std::bind(&PVRIptvData::KeepAliveJob, this), std::chrono::seconds{m_keepAliveDelay}, true);
auto trigger_full_refresh = getCallLimiter(std::bind(&PVRIptvData::TriggerFullRefresh, this), std::chrono::seconds{m_fullChannelEpgRefresh}, true);
auto trigger_load_recordings = getCallLimiter(std::bind(&PVRIptvData::SetLoadRecordings, this), std::chrono::seconds{m_loadingsRefresh}, true);
auto epg_dummy_trigger = getCallLimiter([] {}, std::chrono::seconds{m_epgCheckDelay}, false); // using the CallLimiter just to test if the epg should be done
bool work_done = true;
while (KeepAlive())
{
if (!work_done)
Sleep(1000);
work_done = false;
work_done |= LoadRecordingsJob();
// trigger full refresh once a time
work_done |= trigger_full_refresh.Call();
// trigger loading of recordings once a time
work_done |= trigger_load_recordings.Call();
if (epg_dummy_trigger.Call() || epg_updated)
{
// perform epg loading in next cycle if something updated in this one
epg_updated = LoadEPGJob();
work_done = true;
} else
{
epg_updated = false;
}
// do keep alive call once a time
work_done |= keep_alive_job.Call();
}
XBMC->Log(LOG_DEBUG, "keepAlive:: thread stopped");
return NULL;
}
PVRIptvData::~PVRIptvData(void)
{
{
std::lock_guard<std::mutex> critical(m_mutex);
m_bKeepAlive = false;
}
if (IsRunning())
{
StopThread(0);
}
XBMC->Log(LOG_DEBUG, "%s destructed", __FUNCTION__);
}
bool PVRIptvData::KeepAlive()
{
std::lock_guard<std::mutex> critical(m_mutex);
return m_bKeepAlive;
}
bool PVRIptvData::LoadEPG(time_t iStart, bool bSmallStep)
{
const int step = bSmallStep ? 3600 : 86400;
XBMC->Log(LOG_DEBUG, "%s last start %s, start %s, last end %s, end %s", __FUNCTION__, ApiManager::formatTime(m_iLastStart).c_str()
, ApiManager::formatTime(iStart).c_str(), ApiManager::formatTime(m_iLastEnd).c_str(), ApiManager::formatTime(iStart + step).c_str());
if (m_bEGPLoaded && m_iLastStart != 0 && iStart >= m_iLastStart && iStart + step <= m_iLastEnd)
return false;
Json::Value root;
if (!m_manager.getEpg(iStart, bSmallStep, ChannelsList(), root))
{
XBMC->Log(LOG_NOTICE, "Cannot parse EPG data. EPG not loaded.");
m_bEGPLoaded = true;
return false;
}
if (m_iLastStart == 0 || m_iLastStart > iStart)
m_iLastStart = iStart;
if (iStart + step > m_iLastEnd)
m_iLastEnd = iStart + step;
decltype (m_channels) channels;
decltype (m_epg) epg;
time_t min_epg, max_epg;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
epg = m_epg;
min_epg = m_epgMinTime;
max_epg = m_epgMaxTime;
}
// narrow the loaded time info (if needed)
m_iLastStart = std::max(m_iLastStart, min_epg);
m_iLastEnd = std::min(m_iLastEnd, max_epg);
auto epg_copy = std::make_shared<epg_container_t>(*epg);
Json::Value json_channels = root["channels"];
Json::Value::Members chIds = json_channels.getMemberNames();
for (Json::Value::Members::iterator i = chIds.begin(); i != chIds.end(); i++)
{
std::string strChId = *i;
const auto channel_i = std::find_if(channels->cbegin(), channels->cend(), [&strChId] (const PVRIptvChannel & ch) { return ch.strId == strChId; });
if (channel_i != channels->cend())
{
PVRIptvEpgChannel & epgChannel = (*epg_copy)[strChId];
epgChannel.strId = strChId;
Json::Value epgData = json_channels[strChId];
for (unsigned int j = 0; j < epgData.size(); j++)
{
Json::Value epgEntry = epgData[j];
const time_t start_time = ParseDateTime(epgEntry.get("startTime", "").asString());
const time_t end_time = ParseDateTime(epgEntry.get("endTime", "").asString());
// skip unneeded EPGs
if (start_time > max_epg || end_time < min_epg)
continue;
PVRIptvEpgEntry iptventry;
iptventry.iBroadcastId = start_time; // unique id for channel (even if time_t is wider, int should be enough for short period of time)
iptventry.iGenreType = 0;
iptventry.iGenreSubType = 0;
iptventry.iChannelId = channel_i->iUniqueId;
iptventry.strTitle = epgEntry.get("title", "").asString();
iptventry.strPlot = epgEntry.get("description", "").asString();
iptventry.startTime = start_time;
iptventry.endTime = end_time;
iptventry.strEventId = epgEntry.get("eventId", "").asString();
std::string availability = epgEntry.get("availability", "none").asString();
iptventry.availableTimeshift = availability == "timeshift" || availability == "pvr";
iptventry.strRecordId = epgEntry["recordId"].asString();
XBMC->Log(LOG_DEBUG, "Loading TV show: %s - %s, start=%s(epoch=%llu)", strChId.c_str(), iptventry.strTitle.c_str()
, epgEntry.get("startTime", "").asString().c_str(), static_cast<long long unsigned>(start_time));
// notify about the epg change...and store it
EPG_TAG tag;
memset(&tag, 0, sizeof(EPG_TAG));
tag.iUniqueBroadcastId = iptventry.iBroadcastId;
tag.iUniqueChannelId = iptventry.iChannelId;
tag.strTitle = strdup(iptventry.strTitle.c_str());
tag.startTime = iptventry.startTime;
tag.endTime = iptventry.endTime;
tag.strPlotOutline = strdup(iptventry.strPlotOutline.c_str());
tag.strPlot = strdup(iptventry.strPlot.c_str());
tag.strIconPath = strdup(iptventry.strIconPath.c_str());
tag.iGenreType = EPG_GENRE_USE_STRING; //iptventry.iGenreType;
tag.iGenreSubType = 0; //iptventry.iGenreSubType;
tag.strGenreDescription = strdup(iptventry.strGenreString.c_str());
auto result = epgChannel.epg.emplace(iptventry.startTime, iptventry);
bool value_changed = !result.second;
if (value_changed)
{
epgChannel.epg[iptventry.startTime] = std::move(iptventry);
}
PVR->EpgEventStateChange(&tag, value_changed ? EPG_EVENT_UPDATED : EPG_EVENT_CREATED);
free(const_cast<char *>(tag.strTitle));
free(const_cast<char *>(tag.strPlotOutline));
free(const_cast<char *>(tag.strPlot));
free(const_cast<char *>(tag.strIconPath));
free(const_cast<char *>(tag.strGenreDescription));
}
}
}
// atomic assign new version of the epg all epgs
{
std::lock_guard<std::mutex> critical(m_mutex);
m_epg = epg_copy;
}
m_bEGPLoaded = true;
XBMC->Log(LOG_NOTICE, "EPG Loaded.");
return true;
}
bool PVRIptvData::LoadRecordings()
{
decltype (m_channels) channels;
decltype (m_recordings) recordings;
decltype (m_timers) timers;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
recordings = m_recordings;
timers = m_timers;
}
auto new_recordings = std::make_shared<recording_container_t>();
auto new_timers = std::make_shared<timer_container_t>();
long long available_duration = 0;
long long recorded_duration = 0;
Json::Value root;
if (!m_manager.getPvr(root))
{
XBMC->Log(LOG_NOTICE, "Cannot parse recordings.");
return false;
}
available_duration = root["summary"].get("availableDuration", 0).asInt() / 60 * 1024; //report minutes as MB
recorded_duration = root["summary"].get("recordedDuration", 0).asInt() / 60 * 1024;
Json::Value records = root["records"];
for (unsigned int i = 0; i < records.size(); i++)
{
Json::Value record = records[i];
const std::string title = record.get("title", "").asString();
const std::string locked = record.get("channelLocked", "none").asString();
std::string directory;
if (locked != "none")
{
//Note: std::make_unique is available from c++14
std::unique_ptr<char, decltype (&xbmcStrFree)> loc{XBMC->GetLocalizedString(30201), &xbmcStrFree};
directory = loc.get();
directory += " - ";
directory += locked;
XBMC->Log(LOG_INFO, "Timer/recording '%s' is locked(%s)", title.c_str(), locked.c_str());
}
std::string str_ch_id = record.get("channel", "").asString();
const auto channel_i = std::find_if(channels->cbegin(), channels->cend(), [&str_ch_id] (const PVRIptvChannel & ch) { return ch.strId == str_ch_id; });
PVRIptvRecording iptvrecording;
PVRIptvTimer iptvtimer;
time_t startTime = ParseDateTime(record.get("startTime", "").asString());
int duration = record.get("duration", 0).asInt();
time_t now;
time(&now);
if ((startTime + duration) < now)
{
char buf[256];
sprintf(buf, "%d", record.get("id", 0).asInt());
iptvrecording.strRecordId = buf;
iptvrecording.strTitle = std::move(title);
if (channel_i != channels->cend())
{
iptvrecording.strChannelName = channel_i->strChannelName;
iptvrecording.iChannelUid = channel_i->iUniqueId;
} else
{
iptvrecording.iChannelUid = PVR_CHANNEL_INVALID_UID;
}
iptvrecording.startTime = startTime;
iptvrecording.strPlotOutline = record.get("event", "").get("description", "").asString();
iptvrecording.duration = duration;
iptvrecording.bRadio = channel_i->bIsRadio;
iptvrecording.iLifeTime = (ParseDateTime(record.get("expires", "").asString() + "00:00") - now) / 86400;
iptvrecording.strDirectory = std::move(directory);
XBMC->Log(LOG_DEBUG, "Loading recording '%s'", iptvrecording.strTitle.c_str());
new_recordings->push_back(iptvrecording);
}
else
{
iptvtimer.iClientIndex = record.get("id", 0).asInt();
if (channel_i != channels->cend())
{
iptvtimer.iClientChannelUid = channel_i->iUniqueId;
}
iptvtimer.startTime = ParseDateTime(record.get("startTime", "").asString());
iptvtimer.endTime = iptvtimer.startTime + record.get("duration", 0).asInt();
if (startTime < now && (startTime + duration) >= now)
{
iptvtimer.state = PVR_TIMER_STATE_RECORDING;
}
else
{
iptvtimer.state = PVR_TIMER_STATE_SCHEDULED;
}
iptvtimer.strTitle = std::move(title);
iptvtimer.iLifeTime = (ParseDateTime(record.get("expires", "").asString() + "00:00") - now) / 86400;
iptvtimer.strDirectory = std::move(directory);
XBMC->Log(LOG_DEBUG, "Loading timer '%s'", iptvtimer.strTitle.c_str());
new_timers->push_back(iptvtimer);
}
}
bool changed_r = new_recordings->size() != recordings->size();
for (size_t i = 0; !changed_r && i < new_recordings->size(); ++i)
{
const auto & old_rec = (*recordings)[i];
const auto & new_rec = (*new_recordings)[i];
if (new_rec.strRecordId != old_rec.strRecordId)
{
changed_r = true;
break;
}
}
if (changed_r)
{
for (auto & recording : *new_recordings)
{
std::string channel_id;
recording.strStreamUrl = m_manager.getRecordingUrl(recording.strRecordId, channel_id);
// get the stream type based on channel
recording.strStreamType = ChannelStreamType(channel_id);
}
}
bool changed_t = new_timers->size() != timers->size();
for (size_t i = 0; !changed_t && i < new_timers->size(); ++i)
{
const auto & old_timer = (*timers)[i];
const auto & new_timer = (*new_timers)[i];
if (new_timer.iClientIndex != old_timer.iClientIndex)
{
changed_t = true;
break;
}
}
{
std::lock_guard<std::mutex> critical(m_mutex);
if (changed_r)
{
m_recordings = std::move(new_recordings);
PVR->TriggerRecordingUpdate();
}
if (changed_t)
{
m_timers = std::move(new_timers);
PVR->TriggerTimerUpdate();
}
m_recordingAvailableDuration = available_duration;
m_recordingRecordedDuration = recorded_duration;
}
return true;
}
bool PVRIptvData::LoadPlayList(void)
{
if (!KeepAlive())
return false;
Json::Value root;
if (!m_manager.getPlaylist(m_streamQuality, m_useH265, m_useAdaptive, root))
{
XBMC->Log(LOG_NOTICE, "Cannot get/parse playlist.");
return false;
}
/*
std::string qualities = m_manager.getStreamQualities();
XBMC->Log(LOG_DEBUG, "Stream qualities: %s", qualities.c_str());
*/
//channels
auto new_channels = std::make_shared<channel_container_t>();
Json::Value channels = root["channels"];
for (unsigned int i = 0; i < channels.size(); i++)
{
Json::Value channel = channels[i];
if (!m_showLockedChannels)
{
const std::string locked = channel.get("locked", "none").asString();
if (locked != "none")
{
XBMC->Log(LOG_INFO, "Skipping locked(%s) channel %s", locked.c_str(), channel.get("name", "").asString().c_str());
continue;
}
}
PVRIptvChannel iptvchan;
iptvchan.strId = channel.get("id", "").asString();
iptvchan.strChannelName = channel.get("name", "").asString();
iptvchan.strGroupId = channel.get("group", "").asString();
iptvchan.strStreamURL = channel.get("url", "").asString();
iptvchan.strStreamType = channel.get("streamType", "").asString();
XBMC->Log(LOG_DEBUG, "Channel %s, URL: %s", iptvchan.strChannelName.c_str(), iptvchan.strStreamURL.c_str());
iptvchan.iUniqueId = i + 1;
iptvchan.iChannelNumber = i + 1;
iptvchan.strIconPath = channel.get("logoUrl", "").asString();
iptvchan.bIsRadio = channel.get("type", "").asString() != "tv";
new_channels->push_back(iptvchan);
}
auto new_groups = std::make_shared<group_container_t>();
Json::Value groups = root["groups"];
for (const auto & group_id : groups.getMemberNames())
{
PVRIptvChannelGroup group;
group.bRadio = false; // currently there is no way to distinguish group types in the returned json
group.strGroupId = group_id;
group.strGroupName = groups[group_id].asString();
for (const auto & channel : *new_channels)
{
if (channel.strGroupId == group_id)
group.members.push_back(channel.iUniqueId);
}
new_groups->push_back(std::move(group));
}
XBMC->Log(LOG_NOTICE, "Loaded %d channels.", new_channels->size());
XBMC->QueueNotification(QUEUE_INFO, "%d channels loaded.", new_channels->size());
{
std::lock_guard<std::mutex> critical(m_mutex);
m_channels = std::move(new_channels);
m_groups = std::move(new_groups);
m_bChannelsLoaded = true;
}
m_waitCond.notify_all();
PVR->TriggerChannelUpdate();
PVR->TriggerChannelGroupsUpdate();
return true;
}
int PVRIptvData::GetChannelsAmount(void)
{
decltype (m_channels) channels;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
}
return channels->size();
}
PVR_ERROR PVRIptvData::GetChannels(ADDON_HANDLE handle, bool bRadio)
{
XBMC->Log(LOG_DEBUG, "%s %s", __FUNCTION__, bRadio ? "radio" : "tv");
WaitForChannels();
decltype (m_channels) channels;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
}
std::vector<PVR_CHANNEL> xbmc_channels;
for (const auto & channel : *channels)
{
if (channel.bIsRadio == bRadio)
{
PVR_CHANNEL xbmcChannel;
memset(&xbmcChannel, 0, sizeof(PVR_CHANNEL));
xbmcChannel.iUniqueId = channel.iUniqueId;
xbmcChannel.bIsRadio = channel.bIsRadio;
xbmcChannel.iChannelNumber = channel.iChannelNumber;
strAssign(xbmcChannel.strChannelName, channel.strChannelName);
xbmcChannel.iEncryptionSystem = channel.iEncryptionSystem;
strAssign(xbmcChannel.strIconPath, channel.strIconPath);
xbmcChannel.bIsHidden = false;
xbmc_channels.push_back(std::move(xbmcChannel));
}
}
for (const auto & xbmcChannel : xbmc_channels)
{
PVR->TransferChannelEntry(handle, &xbmcChannel);
}
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::GetChannelStreamUrl(const PVR_CHANNEL* channel, std::string & streamUrl, std::string & streamType) const
{
decltype (m_channels) channels;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
}
auto channel_i = std::find_if(channels->cbegin(), channels->cend(), [channel] (const PVRIptvChannel & c) { return c.iChannelNumber == channel->iUniqueId; });
if (channels->cend() == channel_i)
{
XBMC->Log(LOG_NOTICE, "%s can't find channel %d", __FUNCTION__, channel->iUniqueId);
return PVR_ERROR_INVALID_PARAMETERS;
}
streamUrl = channel_i->strStreamURL;
streamType = channel_i->strStreamType;
return PVR_ERROR_NO_ERROR;
}
int PVRIptvData::GetChannelGroupsAmount(void)
{
decltype (m_groups) groups;
{
std::lock_guard<std::mutex> critical(m_mutex);
groups = m_groups;
}
return groups->size();
}
PVR_ERROR PVRIptvData::GetChannelGroups(ADDON_HANDLE handle, bool bRadio)
{
WaitForChannels();
decltype (m_groups) groups;
{
std::lock_guard<std::mutex> critical(m_mutex);
groups = m_groups;
}
std::vector<PVR_CHANNEL_GROUP> xbmc_groups;
for (const auto & group : *groups)
{
if (group.bRadio == bRadio)
{
PVR_CHANNEL_GROUP xbmcGroup;
memset(&xbmcGroup, 0, sizeof(PVR_CHANNEL_GROUP));
xbmcGroup.bIsRadio = bRadio;
strAssign(xbmcGroup.strGroupName, group.strGroupName);
xbmc_groups.push_back(std::move(xbmcGroup));
}
}
for (const auto & xbmcGroup : xbmc_groups)
{
PVR->TransferChannelGroup(handle, &xbmcGroup);
}
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::GetChannelGroupMembers(ADDON_HANDLE handle, const PVR_CHANNEL_GROUP &group)
{
WaitForChannels();
decltype (m_groups) groups;
decltype (m_channels) channels;
{
std::lock_guard<std::mutex> critical(m_mutex);
groups = m_groups;
channels = m_channels;
}
std::vector<PVR_CHANNEL_GROUP_MEMBER> xbmc_group_members;
auto group_i = std::find_if(groups->cbegin(), groups->cend(), [&group] (PVRIptvChannelGroup const & g) { return g.strGroupName == group.strGroupName; });
if (group_i != groups->cend())
{
for (const auto & member : group_i->members)
{
if (member < 0 || member >= channels->size())
continue;
const PVRIptvChannel &channel = (*channels)[member];
PVR_CHANNEL_GROUP_MEMBER xbmcGroupMember;
memset(&xbmcGroupMember, 0, sizeof(PVR_CHANNEL_GROUP_MEMBER));
strncpy(xbmcGroupMember.strGroupName, group.strGroupName, sizeof(xbmcGroupMember.strGroupName) - 1);
xbmcGroupMember.iChannelUniqueId = channel.iUniqueId;
xbmcGroupMember.iChannelNumber = channel.iChannelNumber;
xbmc_group_members.push_back(std::move(xbmcGroupMember));
}
}
for (const auto & xbmcGroupMember : xbmc_group_members)
{
PVR->TransferChannelGroupMember(handle, &xbmcGroupMember);
}
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::GetEPGForChannel(ADDON_HANDLE handle, const PVR_CHANNEL &channel, time_t iStart, time_t iEnd)
{
XBMC->Log(LOG_DEBUG, "%s %s, from=%s to=%s", __FUNCTION__, channel.strChannelName, ApiManager::formatTime(iStart).c_str(), ApiManager::formatTime(iEnd).c_str());
std::lock_guard<std::mutex> critical(m_mutex);
// Note: For future scheduled timers Kodi requests EPG (this function) with
// iStart & iEnd as given by the timer timespan. But we don't want to narrow
// our EPG interval in such cases.
m_epgMinTime = iStart < m_epgMinTime ? iStart : m_epgMinTime;
m_epgMaxTime = iEnd > m_epgMaxTime ? iEnd : m_epgMaxTime;
return PVR_ERROR_NO_ERROR;
}
static PVR_ERROR GetEPGData(const EPG_TAG* tag
, const channel_container_t * channels
, const epg_container_t * epg
, epg_entry_container_t::const_iterator & epg_i
)
{
auto channel_i = std::find_if(channels->cbegin(), channels->cend(), [tag] (const PVRIptvChannel & c) { return c.iChannelNumber == tag->iUniqueChannelId; });
if (channels->cend() == channel_i)
{
XBMC->Log(LOG_NOTICE, "%s can't find channel %d", __FUNCTION__, tag->iUniqueChannelId);
return PVR_ERROR_INVALID_PARAMETERS;
}
auto ch_epg_i = epg->find(channel_i->strId);
if (epg->cend() == ch_epg_i || (epg_i = ch_epg_i->second.epg.find(tag->iUniqueBroadcastId)) == ch_epg_i->second.epg.cend())
{
XBMC->Log(LOG_NOTICE, "%s can't EPG data for find channel %s, time %d", __FUNCTION__, channel_i->strId.c_str(), tag->iUniqueBroadcastId);
return PVR_ERROR_INVALID_PARAMETERS;
}
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::IsEPGTagPlayable(const EPG_TAG* tag, bool* bIsPlayable) const
{
decltype (m_channels) channels;
decltype (m_epg) epg;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
epg = m_epg;
}
epg_entry_container_t::const_iterator epg_i;
PVR_ERROR ret = GetEPGData(tag, channels.get(), epg.get(), epg_i);
if (PVR_ERROR_NO_ERROR != ret)
return ret;
*bIsPlayable = epg_i->second.availableTimeshift && tag->startTime < time(nullptr);
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::IsEPGTagRecordable(const EPG_TAG* tag, bool* bIsRecordable) const
{
decltype (m_channels) channels;
decltype (m_epg) epg;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
epg = m_epg;
}
epg_entry_container_t::const_iterator epg_i;
PVR_ERROR ret = GetEPGData(tag, channels.get(), epg.get(), epg_i);
if (PVR_ERROR_NO_ERROR != ret)
return ret;
*bIsRecordable = epg_i->second.availableTimeshift && !RecordingExists(epg_i->second.strRecordId) && tag->startTime < time(nullptr);
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::GetEPGStreamUrl(const EPG_TAG* tag, std::string & streamUrl, std::string & streamType) const
{
decltype (m_channels) channels;
decltype (m_epg) epg;
{
std::lock_guard<std::mutex> critical(m_mutex);
channels = m_channels;
epg = m_epg;
}
epg_entry_container_t::const_iterator epg_i;
PVR_ERROR ret = GetEPGData(tag, channels.get(), epg.get(), epg_i);
if (PVR_ERROR_NO_ERROR != ret)
return ret;
if (RecordingExists(epg_i->second.strRecordId))
return GetRecordingStreamUrl(epg_i->second.strRecordId, streamUrl, streamType);
std::string channel_id;
int duration;
if (!m_manager.getTimeShiftInfo(epg_i->second.strEventId, streamUrl, channel_id, duration))
return PVR_ERROR_INVALID_PARAMETERS;
// get the stream type based on channel
streamType = ChannelStreamType(channel_id);
return PVR_ERROR_NO_ERROR;
}
PVR_ERROR PVRIptvData::SetEPGTimeFrame(int iDays)
{
XBMC->Log(LOG_DEBUG, "%s iDays=%d", __FUNCTION__, iDays);
time_t now = time(nullptr);
std::lock_guard<std::mutex> critical(m_mutex);
m_epgMinTime = now;
m_epgMaxTime = now + iDays * 86400;
m_epgMaxDays = iDays;
return PVR_ERROR_NO_ERROR;
}
int PVRIptvData::ParseDateTime(std::string strDate)
{
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(tm));
sscanf(strDate.c_str(), "%04d-%02d-%02d %02d:%02d", &timeinfo.tm_year, &timeinfo.tm_mon, &timeinfo.tm_mday, &timeinfo.tm_hour, &timeinfo.tm_min);
timeinfo.tm_sec = 0;
timeinfo.tm_mon -= 1;
timeinfo.tm_year -= 1900;
timeinfo.tm_isdst = -1;
time_t t = mktime(&timeinfo);