-
Notifications
You must be signed in to change notification settings - Fork 203
/
epgcache.cpp
4540 lines (4233 loc) · 129 KB
/
epgcache.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
#include <lib/dvb/epgcache.h>
#include <lib/dvb/dvb.h>
#include <lib/dvb/lowlevel/eit.h>
#undef EPG_DEBUG
#ifdef EPG_DEBUG
#include <lib/service/event.h>
#endif
#include <deque>
#include <fstream>
#include <ios>
#include <sstream>
#include <iomanip>
#include <string>
#include <time.h>
#include <unistd.h> // for usleep
#include <sys/vfs.h> // for statfs
#include <lib/base/cfile.h>
#include <lib/base/eerror.h>
#include <lib/base/encoding.h>
#include <lib/base/estring.h>
#include <lib/dvb/pmt.h>
#include <lib/dvb/db.h>
#include <lib/python/python.h>
#include <lib/base/nconfig.h>
#include <dvbsi++/descriptor_tag.h>
#define HILO(x) (x##_hi << 8 | x##_lo)
/* Interval between "garbage collect" cycles */
#define CLEAN_INTERVAL 60000 // 1 min
/* Restart EPG data capture */
#define UPDATE_INTERVAL 3600000 // 60 min
/* Time to wait after tuning in before EPG data capturing starts */
#define ZAP_DELAY 2000 // 2 sec
struct DescriptorPair
{
int reference_count;
__u8* data;
DescriptorPair() {}
DescriptorPair(int c, __u8* d): reference_count(c), data(d) {}
};
typedef std::tr1::unordered_map<uint32_t, DescriptorPair> DescriptorMap;
struct eventData
{
uint8_t rawEITdata[10];
uint8_t n_crc;
uint8_t type;
uint32_t *crc_list;
static DescriptorMap descriptors;
static uint8_t data[];
static unsigned int CacheSize;
static bool isCacheCorrupt;
eventData(const eit_event_struct* e = NULL, int size = 0, int type = 0, int tsidonid = 0);
~eventData();
static void load(FILE *);
static void save(FILE *);
static void cacheCorrupt(const char* context);
const eit_event_struct* get() const;
int getEventID() const
{
return (rawEITdata[0] << 8) | rawEITdata[1];
}
time_t getStartTime() const
{
return parseDVBtime(&rawEITdata[2]);
}
int getDuration() const
{
return fromBCD(rawEITdata[7])*3600+fromBCD(rawEITdata[8])*60+fromBCD(rawEITdata[9]);
}
};
unsigned int eventData::CacheSize = 0;
bool eventData::isCacheCorrupt = 0;
DescriptorMap eventData::descriptors;
uint8_t eventData::data[2 * 4096 + 12];
extern const uint32_t crc32_table[256];
const eServiceReference &handleGroup(const eServiceReference &ref)
{
if (ref.flags & eServiceReference::isGroup)
{
ePtr<eDVBResourceManager> res;
if (!eDVBResourceManager::getInstance(res))
{
ePtr<iDVBChannelList> db;
if (!res->getChannelList(db))
{
eBouquet *bouquet = NULL;
if (!db->getBouquet(ref, bouquet))
{
std::list<eServiceReference>::iterator it(bouquet->m_services.begin());
if (it != bouquet->m_services.end())
return *it;
}
}
}
}
return ref;
}
static uint32_t calculate_crc_hash(const uint8_t *data, int size)
{
uint32_t crc = 0;
for (int i = 0; i < size; ++i)
crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[i]) & 0xFF];
return crc;
}
eventData::eventData(const eit_event_struct* e, int size, int _type, int tsidonid)
:n_crc(0), type(_type & 0xFF), crc_list(NULL)
{
if (!e)
return; /* Used when loading from file */
uint32_t descr[65];
uint32_t *pdescr=descr;
uint8_t *data = (uint8_t*)e;
int ptr=12;
size -= 12;
while(size > 1)
{
uint8_t *descr = data + ptr;
int descr_len = descr[1];
descr_len += 2;
if (size >= descr_len)
{
switch (descr[0])
{
case EXTENDED_EVENT_DESCRIPTOR:
case LINKAGE_DESCRIPTOR:
case COMPONENT_DESCRIPTOR:
case CONTENT_DESCRIPTOR:
case PARENTAL_RATING_DESCRIPTOR:
case PDC_DESCRIPTOR:
{
uint32_t crc = calculate_crc_hash(descr, descr_len);
DescriptorMap::iterator it = descriptors.find(crc);
if ( it == descriptors.end() )
{
CacheSize+=descr_len;
uint8_t *d = new uint8_t[descr_len];
memcpy(d, descr, descr_len);
descriptors[crc] = DescriptorPair(1, d);
}
else
++it->second.reference_count;
*pdescr++ = crc;
break;
}
case SHORT_EVENT_DESCRIPTOR:
{
//parse the data out from the short event descriptor
//get the country code, which will be used for converting to UTF8
std::string cc( (const char*)&descr[2], 3);
std::transform(cc.begin(), cc.end(), cc.begin(), tolower);
int table = encodingHandler.getCountryCodeDefaultMapping(cc);
int eventNameLen = descr[5];
int eventTextLen = descr[6 + eventNameLen];
//convert our strings to UTF8
std::string eventNameUTF8 = convertDVBUTF8((const unsigned char*)&descr[6], eventNameLen, table, tsidonid);
std::string textUTF8 = convertDVBUTF8((const unsigned char*)&descr[7 + eventNameLen], eventTextLen, table, tsidonid);
unsigned int eventNameUTF8len = eventNameUTF8.length();
unsigned int textUTF8len = textUTF8.length();
//Rebuild the short event descriptor with UTF-8 strings
//Save the title first
if( eventNameUTF8len > 0 ) //only store the data if there is something to store
{
/*this will actually cause us to save some memory
previously some descriptors didnt match because there text was different and titles the same.
Now that we store them seperatly we can save some space on title data some rough calculation show anywhere from 20 - 40% savings
*/
eventNameUTF8len = truncateUTF8(eventNameUTF8, 255 - 6);
int title_len = 6 + eventNameUTF8len;
uint8_t *title_data = new uint8_t[title_len + 2];
title_data[0] = SHORT_EVENT_DESCRIPTOR;
title_data[1] = title_len;
title_data[2] = descr[2];
title_data[3] = descr[3];
title_data[4] = descr[4];
title_data[5] = eventNameUTF8len + 1;
title_data[6] = 0x15; //identify event name as UTF-8
memcpy(&title_data[7], eventNameUTF8.data(), eventNameUTF8len);
title_data[7 + eventNameUTF8len] = 0;
//Calculate the CRC, based on our new data
title_len += 2; //add 2 the length to include the 2 bytes in the header
uint32_t title_crc = calculate_crc_hash(title_data, title_len);
DescriptorMap::iterator it = descriptors.find(title_crc);
if ( it == descriptors.end() )
{
CacheSize += title_len;
descriptors[title_crc] = DescriptorPair(1, title_data);
}
else
{
++it->second.reference_count;
delete [] title_data;
}
*pdescr++ = title_crc;
}
//save the text
if( textUTF8len > 0 ) //only store the data if there is something to store
{
textUTF8len = truncateUTF8(textUTF8, 255 - 6);
int text_len = 6 + textUTF8len;
uint8_t *text_data = new uint8_t[text_len + 2];
text_data[0] = SHORT_EVENT_DESCRIPTOR;
text_data[1] = text_len;
text_data[2] = descr[2];
text_data[3] = descr[3];
text_data[4] = descr[4];
text_data[5] = 0;
text_data[6] = textUTF8len + 1; //identify text as UTF-8
text_data[7] = 0x15; //identify text as UTF-8
memcpy(&text_data[8], textUTF8.data(), textUTF8len);
text_len += 2; //add 2 the length to include the 2 bytes in the header
uint32_t text_crc = calculate_crc_hash(text_data, text_len);
DescriptorMap::iterator it = descriptors.find(text_crc);
if ( it == descriptors.end() )
{
CacheSize += text_len;
descriptors[text_crc] = DescriptorPair(1, text_data);
}
else
{
++it->second.reference_count;
delete [] text_data;
}
*pdescr++ = text_crc;
}
break;
}
default: // do not cache all other descriptors
break;
}
ptr += descr_len;
size -= descr_len;
}
else
break;
}
memcpy(rawEITdata, (uint8_t*)e, 10);
ASSERT(pdescr <= &descr[65]);
n_crc = pdescr - descr;
if (n_crc)
{
crc_list = new uint32_t[n_crc];
memcpy(crc_list, descr, n_crc * sizeof(uint32_t));
}
CacheSize += sizeof(*this) + n_crc * sizeof(uint32_t);
}
const eit_event_struct* eventData::get() const
{
unsigned int pos = 12;
memcpy(data, rawEITdata, 10);
unsigned int descriptors_length = 0;
for (uint8_t i = 0; i < n_crc; ++i)
{
DescriptorMap::iterator it = descriptors.find(crc_list[i]);
if (it != descriptors.end())
{
unsigned int b = it->second.data[1] + 2;
if (pos + b < sizeof(data))
{
memcpy(data + pos, it->second.data, b);
pos += b;
descriptors_length += b;
}
}
else
cacheCorrupt("eventData::get");
}
data[10] = (descriptors_length >> 8) & 0x0F;
data[11] = descriptors_length & 0xFF;
return (eit_event_struct*)data;
}
eventData::~eventData()
{
for ( uint8_t i = 0; i < n_crc; ++i )
{
DescriptorMap::iterator it = descriptors.find(crc_list[i]);
if ( it != descriptors.end() )
{
DescriptorPair &p = it->second;
if (!--p.reference_count) // no more used descriptor
{
CacheSize -= it->second.data[1];
delete [] it->second.data; // free descriptor memory
descriptors.erase(it); // remove entry from descriptor map
}
}
else
{
cacheCorrupt("eventData::~eventData");
}
}
delete [] crc_list;
CacheSize -= sizeof(*this) + n_crc * sizeof(uint32_t);
}
void eventData::load(FILE *f)
{
int size = 0;
int id=0;
DescriptorPair p;
uint8_t header[2];
fread(&size, sizeof(int), 1, f);
descriptors.rehash(size);
while(size)
{
fread(&id, sizeof(uint32_t), 1, f);
fread(&p.reference_count, sizeof(int), 1, f);
fread(header, 2, 1, f);
int bytes = header[1]+2;
p.data = new uint8_t[bytes];
p.data[0] = header[0];
p.data[1] = header[1];
fread(p.data+2, bytes-2, 1, f);
descriptors[id] = p;
--size;
}
}
void eventData::save(FILE *f)
{
if (isCacheCorrupt)
return;
int size=descriptors.size();
DescriptorMap::iterator it(descriptors.begin());
fwrite(&size, sizeof(int), 1, f);
while(size)
{
fwrite(&it->first, sizeof(uint32_t), 1, f);
fwrite(&it->second.reference_count, sizeof(int), 1, f);
fwrite(it->second.data, it->second.data[1]+2, 1, f);
++it;
--size;
}
}
void eventData::cacheCorrupt(const char* context)
{
eDebug("[eventData] EPG Cache is corrupt (%s), you should restart Enigma!", context);
if (!isCacheCorrupt)
{
isCacheCorrupt = true;
if (!eEPGCache::instance->m_filename.empty())
unlink(eEPGCache::instance->m_filename.c_str()); // Remove corrupt EPG data
}
}
eEPGCache* eEPGCache::instance;
static pthread_mutex_t cache_lock =
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static pthread_mutex_t channel_map_lock =
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
DEFINE_REF(eEPGCache)
eEPGCache::eEPGCache()
:messages(this,1), cleanTimer(eTimer::create(this)), m_running(false)
{
eDebug("[eEPGCache] Initialized EPGCache (wait for setCacheFile call now)");
enabledSources = 0;
historySeconds = 0;
CONNECT(messages.recv_msg, eEPGCache::gotMessage);
CONNECT(eDVBLocalTimeHandler::getInstance()->m_timeUpdated, eEPGCache::timeUpdated);
CONNECT(cleanTimer->timeout, eEPGCache::cleanLoop);
std::ifstream onid_file;
onid_file.open("/etc/enigma2/blacklist.onid");
int tmp_onid;
while (onid_file >> std::hex >>tmp_onid)
onid_blacklist.insert(onid_blacklist.end(),1,tmp_onid);
onid_file.close();
std::ifstream pid_file ("/etc/enigma2/epgpids.custom");
if (pid_file.is_open())
{
eDebug("[eEPGCache] Custom pidfile found, parsing...");
std::string line;
char optsidonid[12];
int op, tsid, onid, eitpid;
while (!pid_file.eof())
{
getline(pid_file, line);
if (line[0] == '#' || sscanf(line.c_str(), "%i %i %i %i", &op, &tsid, &onid, &eitpid) != 4)
continue;
if (op < 0)
op += 3600;
if (eitpid != 0)
{
sprintf (optsidonid, "%x%04x%04x", op, tsid, onid);
customeitpids[std::string(optsidonid)] = eitpid;
eDebug("[eEPGCache] %s --> %#x", optsidonid, eitpid);
}
}
pid_file.close();
eDebug("[eEPGCache] Done");
}
ePtr<eDVBResourceManager> res_mgr;
eDVBResourceManager::getInstance(res_mgr);
if (!res_mgr)
eDebug("[eEPGCache] no resource manager !!!!!!!");
else
res_mgr->connectChannelAdded(slot(*this,&eEPGCache::DVBChannelAdded), m_chanAddedConn);
instance=this;
}
void eEPGCache::setCacheFile(const char *path)
{
bool inited = !m_filename.empty();
m_filename = path;
if (!inited)
{
eDebug("[eEPGCache] setCacheFile read/write epg data from/to '%s'", m_filename.c_str());
if (eDVBLocalTimeHandler::getInstance()->ready())
timeUpdated();
}
}
void eEPGCache::timeUpdated()
{
if (!m_filename.empty())
{
if (!m_running)
{
eDebug("[eEPGCache] time updated.. start EPG Mainloop");
run();
m_running = true;
singleLock s(channel_map_lock);
for (ChannelMap::const_iterator it = m_knownChannels.begin();
it != m_knownChannels.end(); ++it)
{
if (it->second->state == -1) {
it->second->state=0;
messages.send(Message(Message::startChannel, it->first));
}
}
} else
messages.send(Message(Message::timeChanged));
}
else
eDebug("[eEPGCache] time updated.. but cache file not set yet.. dont start epg!!");
}
void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
{
if ( chan )
{
// eDebug("[eEPGCache] add channel %p", chan);
channel_data *data = new channel_data(this);
data->channel = chan;
data->prevChannelState = -1;
#ifdef ENABLE_PRIVATE_EPG
data->m_PrivatePid = -1;
#endif
#ifdef ENABLE_MHW_EPG
data->m_mhw2_channel_pid = 0x231; // defaults for astra 19.2 D+
data->m_mhw2_title_pid = 0x234; // defaults for astra 19.2 D+
data->m_mhw2_summary_pid = 0x236; // defaults for astra 19.2 D+
#endif
singleLock s(channel_map_lock);
m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
}
}
void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
{
ChannelMap::const_iterator it =
m_knownChannels.find(chan);
if ( it == m_knownChannels.end() )
eDebug("[eEPGCache] will start non existing channel %p !!!", chan);
else
{
channel_data &data = *it->second;
ePtr<eDVBResourceManager> res_mgr;
if ( eDVBResourceManager::getInstance( res_mgr ) )
eDebug("[eEPGCache] no res manager!!");
else
{
ePtr<iDVBDemux> demux;
if ( data.channel->getDemux(demux, 0) )
{
eDebug("[eEPGCache] no demux!!");
return;
}
else
{
RESULT res = demux->createSectionReader( this, data.m_NowNextReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize nownext reader!!");
return;
}
res = demux->createSectionReader( this, data.m_ScheduleReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize schedule reader!!");
return;
}
res = demux->createSectionReader( this, data.m_ScheduleOtherReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize schedule other reader!!");
return;
}
#ifdef ENABLE_VIRGIN
res = demux->createSectionReader( this, data.m_VirginNowNextReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize virgin nownext reader!!");
return;
}
res = demux->createSectionReader( this, data.m_VirginScheduleReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize virgin schedule reader!!");
return;
}
#endif
#ifdef ENABLE_NETMED
res = demux->createSectionReader( this, data.m_NetmedScheduleReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize netmed schedule reader!!");
return;
}
res = demux->createSectionReader( this, data.m_NetmedScheduleOtherReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize netmed schedule other reader!!");
return;
}
#endif
res = demux->createSectionReader( this, data.m_ViasatReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize viasat reader!!");
return;
}
#ifdef ENABLE_PRIVATE_EPG
res = demux->createSectionReader( this, data.m_PrivateReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize private reader!!");
return;
}
#endif
#ifdef ENABLE_MHW_EPG
res = demux->createSectionReader( this, data.m_MHWReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize mhw reader!!");
return;
}
res = demux->createSectionReader( this, data.m_MHWReader2 );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize mhw reader!!");
return;
}
#endif
#if ENABLE_FREESAT
res = demux->createSectionReader( this, data.m_FreeSatScheduleOtherReader );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize FreeSat reader!!");
return;
}
res = demux->createSectionReader( this, data.m_FreeSatScheduleOtherReader2 );
if ( res )
{
eDebug("[eEPGCache] couldnt initialize FreeSat reader 2!!");
return;
}
#endif
if (m_running)
{
data.state = 0;
messages.send(Message(Message::startChannel, chan));
// -> gotMessage -> changedService
}
else
data.state=-1;
}
}
}
}
void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
{
ChannelMap::iterator it =
m_knownChannels.find(chan);
if ( it != m_knownChannels.end() )
{
int state=0;
chan->getState(state);
if ( it->second->prevChannelState != state )
{
switch (state)
{
case iDVBChannel::state_ok:
{
eDebug("[eEPGCache] channel %p running", chan);
DVBChannelRunning(chan);
break;
}
case iDVBChannel::state_release:
{
eDebug("[eEPGCache] remove channel %p", chan);
if (it->second->state >= 0)
messages.send(Message(Message::leaveChannel, chan));
channel_data* cd = it->second;
pthread_mutex_lock(&cd->channel_active);
{
singleLock s(channel_map_lock);
m_knownChannels.erase(it);
}
pthread_mutex_unlock(&cd->channel_active);
delete cd;
// -> gotMessage -> abortEPG
return;
}
default: // ignore all other events
return;
}
if (it->second)
it->second->prevChannelState = state;
}
}
}
bool eEPGCache::FixOverlapping(EventCacheItem &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service)
{
bool ret = false;
timeMap::iterator tmp = tm_it;
while ((tmp->first + tmp->second->getDuration() - 300) > TM)
{
if(tmp->first != TM
#ifdef ENABLE_PRIVATE_EPG
&& tmp->second->type != PRIVATE
#endif
#ifdef ENABLE_MHW_EPG
&& tmp->second->type != MHW
#endif
)
{
uint16_t event_id = tmp->second->getEventID();
servicemap.byEvent.erase(event_id);
#ifdef EPG_DEBUG
Event evt((uint8_t*)tmp->second->get());
eServiceEvent event;
event.parseFrom(&evt, service.sid<<16|service.onid);
eDebug("[eEPGCache] (1)erase no more used event %04x %d\n%s %s\n%s",
service.sid, event_id,
event.getBeginTimeString().c_str(),
event.getEventName().c_str(),
event.getExtendedDescription().c_str());
#endif
delete tmp->second;
if (tmp == servicemap.byTime.begin())
{
servicemap.byTime.erase(tmp);
break;
}
else
servicemap.byTime.erase(tmp--);
ret = true;
}
else
{
if (tmp == servicemap.byTime.begin())
break;
--tmp;
}
}
tmp = tm_it;
while(tmp->first < (TM+duration-300))
{
if (tmp->first != TM && tmp->second->type != PRIVATE)
{
uint16_t event_id = tmp->second->getEventID();
servicemap.byEvent.erase(event_id);
#ifdef EPG_DEBUG
Event evt((uint8_t*)tmp->second->get());
eServiceEvent event;
event.parseFrom(&evt, service.sid<<16|service.onid);
eDebug("[eEPGCache] (2)erase no more used event %04x %d\n%s %s\n%s",
service.sid, event_id,
event.getBeginTimeString().c_str(),
event.getEventName().c_str(),
event.getExtendedDescription().c_str());
#endif
delete tmp->second;
servicemap.byTime.erase(tmp++);
ret = true;
}
else
++tmp;
if (tmp == servicemap.byTime.end())
break;
}
return ret;
}
void eEPGCache::sectionRead(const uint8_t *data, int source, channel_data *channel)
{
const eit_t *eit = (const eit_t*) data;
int len = HILO(eit->section_length) - 1;
int ptr = EIT_SIZE;
if ( ptr >= len )
return;
#if 0
/*
* disable for now, as this hack breaks EIT parsing for
* services with a low segment_last_table_id
*
* Multichoice should be the exception, not the rule...
*/
// This fixed the EPG on the Multichoice irdeto systems
// the EIT packet is non-compliant.. their EIT packet stinks
if ( data[ptr-1] < 0x40 )
--ptr;
#endif
int onid = HILO(eit->original_network_id);
int tsid = HILO(eit->transport_stream_id);
// Cablecom HACK .. tsid / onid in eit data are incorrect.. so we use
// it from running channel (just for current transport stream eit data)
/*
* Make an exception for BEV (onid 0x100, 0x101), which doesn't use
* SCHEDULE_OTHER. As a result SCHEDULE will contain data for different tsid's,
* so we should not replace it with the current tsid.
*/
bool use_transponder_chid = onid != 0x101 && onid != 0x100 && (source == SCHEDULE || (source == NOWNEXT && data[0] == 0x4E));
if (use_transponder_chid && channel)
{
eDVBChannelID chid = channel->channel->getChannelID();
onid = chid.original_network_id.get();
tsid = chid.transport_stream_id.get();
}
uniqueEPGKey service( HILO(eit->service_id), onid, tsid);
eit_event_struct* eit_event = (eit_event_struct*) (data+ptr);
int eit_event_size;
int duration;
time_t TM = parseDVBtime((const uint8_t*)eit_event + 2);
time_t now = ::time(0);
if ( TM != 3599 && TM > -1 && channel)
channel->haveData |= source;
singleLock s(cache_lock);
// hier wird immer eine eventMap zurck gegeben.. entweder eine vorhandene..
// oder eine durch [] erzeugte
EventCacheItem &servicemap = eventDB[service];
eventMap::iterator prevEventIt = servicemap.byEvent.end();
timeMap::iterator prevTimeIt = servicemap.byTime.end();
while (ptr<len)
{
uint16_t event_hash;
eit_event_size = HILO(eit_event->descriptors_loop_length)+EIT_LOOP_SIZE;
duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
TM = parseDVBtime((const uint8_t*)eit_event + 2, &event_hash);
std::vector<int>::iterator m_it=find(onid_blacklist.begin(),onid_blacklist.end(),onid);
if (m_it != onid_blacklist.end())
goto next;
if ( (TM != 3599) && // NVOD Service
(now <= (TM+duration)) && // skip old events
(TM < (now+28*24*60*60)) && // no more than 4 weeks in future
( (onid != 1714) || (duration != (24*3600-1)) ) // PlatformaHD invalid event
)
{
uint16_t event_id = HILO(eit_event->event_id);
eventData *evt = 0;
int ev_erase_count = 0;
int tm_erase_count = 0;
if (event_id == 0) {
// hack for some polsat services on 13.0E..... but this also replaces other valid event_ids with value 0..
// but we dont care about it...
event_id = event_hash;
eit_event->event_id_hi = event_hash >> 8;
eit_event->event_id_lo = event_hash & 0xFF;
}
// search in eventmap
eventMap::iterator ev_it =
servicemap.byEvent.find(event_id);
// eDebug("[eEPGCache] event_id is %d sid is %04x", event_id, service.sid);
// entry with this event_id is already exist ?
if ( ev_it != servicemap.byEvent.end() )
{
if ( source > ev_it->second->type ) // update needed ?
goto next; // when not.. then skip this entry
// search this event in timemap
timeMap::iterator tm_it_tmp =
servicemap.byTime.find(ev_it->second->getStartTime());
if ( tm_it_tmp != servicemap.byTime.end() )
{
if ( tm_it_tmp->first == TM ) // just update eventdata
{
// exempt memory
eventData *tmp = ev_it->second;
ev_it->second = tm_it_tmp->second =
new eventData(eit_event, eit_event_size, source, (tsid<<16)|onid);
if (FixOverlapping(servicemap, TM, duration, tm_it_tmp, service))
{
prevEventIt = servicemap.byEvent.end();
prevTimeIt = servicemap.byTime.end();
}
delete tmp;
goto next;
}
else // event has new event begin time
{
tm_erase_count++;
// delete the found record from timemap
servicemap.byTime.erase(tm_it_tmp);
prevTimeIt = servicemap.byTime.end();
}
}
}
// search in timemap, for check of a case if new time has coincided with time of other event
// or event was is not found in eventmap
timeMap::iterator tm_it =
servicemap.byTime.find(TM);
if ( tm_it != servicemap.byTime.end() )
{
// event with same start time but another event_id...
if ( source > tm_it->second->type &&
ev_it == servicemap.byEvent.end() )
goto next; // when not.. then skip this entry
// search this time in eventmap
eventMap::iterator ev_it_tmp =
servicemap.byEvent.find(tm_it->second->getEventID());
if ( ev_it_tmp != servicemap.byEvent.end() )
{
ev_erase_count++;
// delete the found record from eventmap
servicemap.byEvent.erase(ev_it_tmp);
prevEventIt = servicemap.byEvent.end();
}
}
evt = new eventData(eit_event, eit_event_size, source, (tsid<<16)|onid);
#ifdef EPG_DEBUG
bool consistencyCheck=true;
#endif
if (ev_erase_count > 0 && tm_erase_count > 0) // 2 different pairs have been removed
{
// exempt memory
delete ev_it->second;
delete tm_it->second;
ev_it->second=evt;
tm_it->second=evt;
}
else if (ev_erase_count == 0 && tm_erase_count > 0)
{
// exempt memory
delete ev_it->second;
tm_it = prevTimeIt = servicemap.byTime.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
ev_it->second = evt;
}
else if (ev_erase_count > 0 && tm_erase_count == 0)
{
// exempt memory
delete tm_it->second;
ev_it = prevEventIt = servicemap.byEvent.insert( prevEventIt, std::pair<const uint16_t, eventData*>( event_id, evt) );
tm_it->second = evt;
}
else // added new eventData
{
#ifdef EPG_DEBUG
consistencyCheck=false;
#endif
ev_it = prevEventIt = servicemap.byEvent.insert( prevEventIt, std::pair<const uint16_t, eventData*>( event_id, evt) );
tm_it = prevTimeIt = servicemap.byTime.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
}
#ifdef EPG_DEBUG
if ( consistencyCheck )
{
if ( tm_it->second != evt || ev_it->second != evt )
eFatal("[eEPGCache] tm_it->second != ev_it->second");
else if ( tm_it->second->getStartTime() != tm_it->first )
eFatal("[eEPGCache] event start_time(%d) non equal timemap key(%d)",
tm_it->second->getStartTime(), tm_it->first );
else if ( tm_it->first != TM )
eFatal("[eEPGCache] timemap key(%d) non equal TM(%d)",
tm_it->first, TM);
else if ( ev_it->second->getEventID() != ev_it->first )
eFatal("[eEPGCache] event_id (%d) non equal event_map key(%d)",
ev_it->second->getEventID(), ev_it->first);
else if ( ev_it->first != event_id )
eFatal("[eEPGCache] eventmap key(%d) non equal event_id(%d)",
ev_it->first, event_id );
}
#endif
if (FixOverlapping(servicemap, TM, duration, tm_it, service))
{
prevEventIt = servicemap.byEvent.end();
prevTimeIt = servicemap.byTime.end();
}
}
next:
#ifdef EPG_DEBUG
if ( servicemap.byEvent.size() != servicemap.byTime.size() )
{
{
CFile f("/hdd/event_map.txt", "w+");
int i = 0;
for (eventMap::iterator it(servicemap.first.begin()); it != servicemap.first.end(); ++it )
{
fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n",
i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
}
}
{
CFile f("/hdd/time_map.txt", "w+");
int i = 0;
for (timeMap::iterator it(servicemap.second.begin()); it != servicemap.second.end(); ++it )
{
fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n",
i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
}
}
eFatal("[eEPGCache] (1)map sizes not equal :( sid %04x tsid %04x onid %04x size %d size2 %d",
service.sid, service.tsid, service.onid,
servicemap.first.size(), servicemap.second.size() );
}
#endif
ptr += eit_event_size;
eit_event = (eit_event_struct*)(((uint8_t*)eit_event) + eit_event_size);
}
}
void eEPGCache::flushEPG(const uniqueEPGKey & s)
{
eDebug("[eEPGCache] flushEPG %d", (int)(bool)s);
singleLock l(cache_lock);
if (s) // clear only this service
{
eventCache::iterator it = eventDB.find(s);
if ( it != eventDB.end() )
{
eventMap &evMap = it->second.byEvent;