1 #include <lib/dvb/epgcache.h>
2 #include <lib/dvb/dvb.h>
7 #include <unistd.h> // for usleep
8 #include <sys/vfs.h> // for statfs
9 // #include <libmd5sum.h>
10 #include <lib/base/eerror.h>
12 int eventData::CacheSize=0;
13 descriptorMap eventData::descriptors;
14 __u8 eventData::data[4108];
15 extern const uint32_t crc32_table[256];
17 eventData::eventData(const eit_event_struct* e, int size, int type)
18 :ByteSize(size&0xFF), type(type&0xFF)
26 __u8 *data = (__u8*)e;
28 int descriptors_length = (data[ptr++]&0x0F) << 8;
29 descriptors_length |= data[ptr++];
30 while ( descriptors_length > 0 )
32 __u8 *descr = data+ptr;
33 int descr_len = descr[1]+2;
37 while(cnt++ < descr_len)
38 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[ptr++]) & 0xFF];
40 descriptorMap::iterator it =
41 descriptors.find(crc);
42 if ( it == descriptors.end() )
45 __u8 *d = new __u8[descr_len];
46 memcpy(d, descr, descr_len);
47 descriptors[crc] = descriptorPair(1, d);
53 descriptors_length -= descr_len;
55 ByteSize = 12+((pdescr-descr)*4);
56 EITdata = new __u8[ByteSize];
58 memcpy(EITdata, (__u8*) e, 12);
59 memcpy(EITdata+12, descr, ByteSize-12);
62 const eit_event_struct* eventData::get() const
65 int tmp = ByteSize-12;
67 memcpy(data, EITdata, 12);
68 __u32 *p = (__u32*)(EITdata+12);
71 descriptorMap::iterator it =
72 descriptors.find(*p++);
73 if ( it != descriptors.end() )
75 int b = it->second.second[1]+2;
76 memcpy(data+pos, it->second.second, b );
82 return (const eit_event_struct*)data;
85 eventData::~eventData()
91 __u32 *d = (__u32*)(EITdata+12);
94 descriptorMap::iterator it =
95 descriptors.find(*d++);
96 if ( it != descriptors.end() )
98 descriptorPair &p = it->second;
99 if (!--p.first) // no more used descriptor
101 CacheSize -= it->second.second[1];
102 delete [] it->second.second; // free descriptor memory
103 descriptors.erase(it); // remove entry from descriptor map
112 void eventData::load(FILE *f)
118 fread(&size, sizeof(int), 1, f);
121 fread(&id, sizeof(__u32), 1, f);
122 fread(&p.first, sizeof(int), 1, f);
123 fread(header, 2, 1, f);
124 int bytes = header[1]+2;
125 p.second = new __u8[bytes];
126 p.second[0] = header[0];
127 p.second[1] = header[1];
128 fread(p.second+2, bytes-2, 1, f);
135 void eventData::save(FILE *f)
137 int size=descriptors.size();
138 descriptorMap::iterator it(descriptors.begin());
139 fwrite(&size, sizeof(int), 1, f);
142 fwrite(&it->first, sizeof(__u32), 1, f);
143 fwrite(&it->second.first, sizeof(int), 1, f);
144 fwrite(it->second.second, it->second.second[1]+2, 1, f);
150 eEPGCache* eEPGCache::instance;
151 pthread_mutex_t eEPGCache::cache_lock=
152 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
153 pthread_mutex_t eEPGCache::channel_map_lock=
154 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
156 DEFINE_REF(eEPGCache)
158 eEPGCache::eEPGCache()
159 :messages(this,1), cleanTimer(this)//, paused(0)
161 eDebug("[EPGC] Initialized EPGCache");
163 CONNECT(messages.recv_msg, eEPGCache::gotMessage);
164 CONNECT(eDVBLocalTimeHandler::getInstance()->m_timeUpdated, eEPGCache::timeUpdated);
165 CONNECT(cleanTimer.timeout, eEPGCache::cleanLoop);
167 ePtr<eDVBResourceManager> res_mgr;
168 eDVBResourceManager::getInstance(res_mgr);
170 eDebug("[eEPGCache] no resource manager !!!!!!!");
172 res_mgr->connectChannelAdded(slot(*this,&eEPGCache::DVBChannelAdded), m_chanAddedConn);
176 void eEPGCache::timeUpdated()
178 if ( !thread_running() )
180 eDebug("[EPGC] time updated.. start EPG Mainloop");
184 messages.send(Message(Message::timeChanged));
187 void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
191 // eDebug("[eEPGCache] add channel %p", chan);
192 channel_data *data = new channel_data(this);
193 data->channel = chan;
194 singleLock s(channel_map_lock);
195 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
196 chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
200 void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
202 singleLock s(channel_map_lock);
203 channelMapIterator it =
204 m_knownChannels.find(chan);
205 if ( it == m_knownChannels.end() )
206 eDebug("[eEPGCache] will start non existing channel %p !!!", chan);
209 channel_data &data = *it->second;
210 ePtr<eDVBResourceManager> res_mgr;
211 if ( eDVBResourceManager::getInstance( res_mgr ) )
212 eDebug("[eEPGCache] no res manager!!");
215 ePtr<iDVBDemux> demux;
216 if ( data.channel->getDemux(demux, 0) )
218 eDebug("[eEPGCache] no demux!!");
223 RESULT res = demux->createSectionReader( this, data.m_NowNextReader );
226 eDebug("[eEPGCache] couldnt initialize nownext reader!!");
230 res = demux->createSectionReader( this, data.m_ScheduleReader );
233 eDebug("[eEPGCache] couldnt initialize schedule reader!!");
237 res = demux->createSectionReader( this, data.m_ScheduleOtherReader );
240 eDebug("[eEPGCache] couldnt initialize schedule other reader!!");
244 messages.send(Message(Message::startChannel, chan));
245 // -> gotMessage -> changedService
251 void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
253 channelMapIterator it =
254 m_knownChannels.find(chan);
255 if ( it != m_knownChannels.end() )
258 chan->getState(state);
261 case iDVBChannel::state_ok:
263 eDebug("[eEPGCache] channel %p running", chan);
264 DVBChannelRunning(chan);
267 case iDVBChannel::state_release:
269 eDebug("[eEPGCache] remove channel %p", chan);
270 messages.send(Message(Message::leaveChannel, chan));
271 while(!it->second->can_delete)
274 m_knownChannels.erase(it);
275 // -> gotMessage -> abortEPG
282 void eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
284 eit_t *eit = (eit_t*) data;
286 int len=HILO(eit->section_length)-1;//+3-4;
291 // This fixed the EPG on the Multichoice irdeto systems
292 // the EIT packet is non-compliant.. their EIT packet stinks
293 if ( data[ptr-1] < 0x40 )
296 uniqueEPGKey service( HILO(eit->service_id), HILO(eit->original_network_id), HILO(eit->transport_stream_id) );
297 eit_event_struct* eit_event = (eit_event_struct*) (data+ptr);
301 time_t TM = parseDVBtime( eit_event->start_time_1, eit_event->start_time_2, eit_event->start_time_3, eit_event->start_time_4, eit_event->start_time_5);
302 time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
304 if ( TM != 3599 && TM > -1)
305 channel->haveData |= source;
307 singleLock s(cache_lock);
308 // hier wird immer eine eventMap zurück gegeben.. entweder eine vorhandene..
309 // oder eine durch [] erzeugte
310 std::pair<eventMap,timeMap> &servicemap = eventDB[service];
311 eventMap::iterator prevEventIt = servicemap.first.end();
312 timeMap::iterator prevTimeIt = servicemap.second.end();
316 eit_event_size = HILO(eit_event->descriptors_loop_length)+EIT_LOOP_SIZE;
318 duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
320 eit_event->start_time_1,
321 eit_event->start_time_2,
322 eit_event->start_time_3,
323 eit_event->start_time_4,
324 eit_event->start_time_5);
329 if ( TM != 3599 && (TM+duration < now || TM > now+14*24*60*60) )
332 if ( now <= (TM+duration) || TM == 3599 /*NVOD Service*/ ) // old events should not be cached
334 __u16 event_id = HILO(eit_event->event_id);
335 // eDebug("event_id is %d sid is %04x", event_id, service.sid);
338 int ev_erase_count = 0;
339 int tm_erase_count = 0;
341 // search in eventmap
342 eventMap::iterator ev_it =
343 servicemap.first.find(event_id);
345 // entry with this event_id is already exist ?
346 if ( ev_it != servicemap.first.end() )
348 if ( source > ev_it->second->type ) // update needed ?
349 goto next; // when not.. the skip this entry
351 // search this event in timemap
352 timeMap::iterator tm_it_tmp =
353 servicemap.second.find(ev_it->second->getStartTime());
355 if ( tm_it_tmp != servicemap.second.end() )
357 if ( tm_it_tmp->first == TM ) // correct eventData
360 delete ev_it->second;
361 evt = new eventData(eit_event, eit_event_size, source);
363 tm_it_tmp->second=evt;
369 // delete the found record from timemap
370 servicemap.second.erase(tm_it_tmp);
371 prevTimeIt=servicemap.second.end();
376 // search in timemap, for check of a case if new time has coincided with time of other event
377 // or event was is not found in eventmap
378 timeMap::iterator tm_it =
379 servicemap.second.find(TM);
381 if ( tm_it != servicemap.second.end() )
383 // i think, if event is not found on eventmap, but found on timemap updating nevertheless demands
385 if ( source > tm_it->second->type && tm_erase_count == 0 ) // update needed ?
386 goto next; // when not.. the skip this entry
389 // search this time in eventmap
390 eventMap::iterator ev_it_tmp =
391 servicemap.first.find(tm_it->second->getEventID());
393 if ( ev_it_tmp != servicemap.first.end() )
396 // delete the found record from eventmap
397 servicemap.first.erase(ev_it_tmp);
398 prevEventIt=servicemap.first.end();
402 evt = new eventData(eit_event, eit_event_size, source);
404 bool consistencyCheck=true;
406 if (ev_erase_count > 0 && tm_erase_count > 0) // 2 different pairs have been removed
409 delete ev_it->second;
410 delete tm_it->second;
414 else if (ev_erase_count == 0 && tm_erase_count > 0)
417 delete ev_it->second;
418 tm_it=prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
421 else if (ev_erase_count > 0 && tm_erase_count == 0)
424 delete tm_it->second;
425 ev_it=prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
428 else // added new eventData
431 consistencyCheck=false;
433 prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
434 prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
437 if ( consistencyCheck )
439 if ( tm_it->second != evt || ev_it->second != evt )
440 eFatal("tm_it->second != ev_it->second");
441 else if ( tm_it->second->getStartTime() != tm_it->first )
442 eFatal("event start_time(%d) non equal timemap key(%d)",
443 tm_it->second->getStartTime(), tm_it->first );
444 else if ( tm_it->first != TM )
445 eFatal("timemap key(%d) non equal TM(%d)",
447 else if ( ev_it->second->getEventID() != ev_it->first )
448 eFatal("event_id (%d) non equal event_map key(%d)",
449 ev_it->second->getEventID(), ev_it->first);
450 else if ( ev_it->first != event_id )
451 eFatal("eventmap key(%d) non equal event_id(%d)",
452 ev_it->first, event_id );
458 if ( servicemap.first.size() != servicemap.second.size() )
460 FILE *f = fopen("/hdd/event_map.txt", "w+");
462 for (eventMap::iterator it(servicemap.first.begin())
463 ; it != servicemap.first.end(); ++it )
464 fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n",
465 i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
467 f = fopen("/hdd/time_map.txt", "w+");
469 for (timeMap::iterator it(servicemap.second.begin())
470 ; it != servicemap.second.end(); ++it )
471 fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n",
472 i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
475 eFatal("(1)map sizes not equal :( sid %04x tsid %04x onid %04x size %d size2 %d",
476 service.sid, service.tsid, service.onid,
477 servicemap.first.size(), servicemap.second.size() );
480 ptr += eit_event_size;
481 eit_event=(eit_event_struct*)(((__u8*)eit_event)+eit_event_size);
485 void eEPGCache::flushEPG(const uniqueEPGKey & s)
487 eDebug("[EPGC] flushEPG %d", (int)(bool)s);
488 singleLock l(cache_lock);
489 if (s) // clear only this service
491 eventCache::iterator it = eventDB.find(s);
492 if ( it != eventDB.end() )
494 eventMap &evMap = it->second.first;
495 timeMap &tmMap = it->second.second;
497 for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
502 // TODO .. search corresponding channel for removed service and remove this channel from lastupdated map
505 else // clear complete EPG Cache
507 for (eventCache::iterator it(eventDB.begin());
508 it != eventDB.end(); ++it)
510 eventMap &evMap = it->second.first;
511 timeMap &tmMap = it->second.second;
512 for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
518 channelLastUpdated.clear();
519 singleLock m(channel_map_lock);
520 for (channelMapIterator it(m_knownChannels.begin()); it != m_knownChannels.end(); ++it)
521 it->second->startEPG();
523 eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
526 void eEPGCache::cleanLoop()
528 singleLock s(cache_lock);
529 if (!eventDB.empty())
531 eDebug("[EPGC] start cleanloop");
533 time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
535 for (eventCache::iterator DBIt = eventDB.begin(); DBIt != eventDB.end(); DBIt++)
537 for (timeMap::iterator It = DBIt->second.second.begin(); It != DBIt->second.second.end() && It->first < now;)
539 if ( now > (It->first+It->second->getDuration()) ) // outdated normal entry (nvod references to)
541 // remove entry from eventMap
542 eventMap::iterator b(DBIt->second.first.find(It->second->getEventID()));
543 if ( b != DBIt->second.first.end() )
545 // release Heap Memory for this entry (new ....)
546 // eDebug("[EPGC] delete old event (evmap)");
547 DBIt->second.first.erase(b);
550 // remove entry from timeMap
551 // eDebug("[EPGC] release heap mem");
553 DBIt->second.second.erase(It++);
554 // eDebug("[EPGC] delete old event (timeMap)");
560 eDebug("[EPGC] stop cleanloop");
561 eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
563 cleanTimer.start(CLEAN_INTERVAL,true);
566 eEPGCache::~eEPGCache()
568 messages.send(Message::quit);
569 kill(); // waiting for thread shutdown
570 singleLock s(cache_lock);
571 for (eventCache::iterator evIt = eventDB.begin(); evIt != eventDB.end(); evIt++)
572 for (eventMap::iterator It = evIt->second.first.begin(); It != evIt->second.first.end(); It++)
576 void eEPGCache::gotMessage( const Message &msg )
581 flushEPG(msg.service);
583 case Message::startChannel:
585 singleLock s(channel_map_lock);
586 channelMapIterator channel =
587 m_knownChannels.find(msg.channel);
588 if ( channel != m_knownChannels.end() )
589 channel->second->startChannel();
592 case Message::leaveChannel:
594 singleLock s(channel_map_lock);
595 channelMapIterator channel =
596 m_knownChannels.find(msg.channel);
597 if ( channel != m_knownChannels.end() )
598 channel->second->abortEPG();
604 case Message::timeChanged:
608 eDebug("unhandled EPGCache Message!!");
613 void eEPGCache::thread()
622 void eEPGCache::load()
625 FILE *f = fopen("/hdd/epg.dat", "r");
628 unsigned char md5_saved[16];
629 unsigned char md5[16];
633 if (!md5_file("/hdd/epg.dat", 1, md5))
635 FILE *f = fopen("/hdd/epg.dat.md5", "r");
638 fread( md5_saved, 16, 1, f);
640 if ( !memcmp(md5_saved, md5, 16) )
647 fread( text1, 13, 1, f);
648 if ( !strncmp( text1, "ENIGMA_EPG_V4", 13) )
650 fread( &size, sizeof(int), 1, f);
657 fread( &key, sizeof(uniqueEPGKey), 1, f);
658 fread( &size, sizeof(int), 1, f);
664 fread( &type, sizeof(__u8), 1, f);
665 fread( &len, sizeof(__u8), 1, f);
666 event = new eventData(0, len, type);
667 event->EITdata = new __u8[len];
668 eventData::CacheSize+=len;
669 fread( event->EITdata, len, 1, f);
670 evMap[ event->getEventID() ]=event;
671 tmMap[ event->getStartTime() ]=event;
674 eventDB[key]=std::pair<eventMap,timeMap>(evMap,tmMap);
677 eDebug("%d events read from /hdd/epg.dat", cnt);
680 eDebug("[EPGC] don't read old epg database");
687 void eEPGCache::save()
692 if (statfs("/hdd", &s)<0)
700 // prevent writes to builtin flash
701 if ( tmp < 1024*1024*50 ) // storage size < 50MB
704 // check for enough free space on storage
707 if ( tmp < (eventData::CacheSize*12)/10 ) // 20% overhead
710 FILE *f = fopen("/hdd/epg.dat", "w");
714 const char *text = "ENIGMA_EPG_V4";
715 fwrite( text, 13, 1, f );
716 int size = eventDB.size();
717 fwrite( &size, sizeof(int), 1, f );
718 for (eventCache::iterator service_it(eventDB.begin()); service_it != eventDB.end(); ++service_it)
720 timeMap &timemap = service_it->second.second;
721 fwrite( &service_it->first, sizeof(uniqueEPGKey), 1, f);
722 size = timemap.size();
723 fwrite( &size, sizeof(int), 1, f);
724 for (timeMap::iterator time_it(timemap.begin()); time_it != timemap.end(); ++time_it)
726 __u8 len = time_it->second->ByteSize;
727 fwrite( &time_it->second->type, sizeof(__u8), 1, f );
728 fwrite( &len, sizeof(__u8), 1, f);
729 fwrite( time_it->second->EITdata, len, 1, f);
733 eDebug("%d events written to /hdd/epg.dat", cnt);
736 unsigned char md5[16];
737 if (!md5_file("/hdd/epg.dat", 1, md5))
739 FILE *f = fopen("/hdd/epg.dat.md5", "w");
742 fwrite( md5, 16, 1, f);
750 eEPGCache::channel_data::channel_data(eEPGCache *ml)
752 ,abortTimer(ml), zapTimer(ml)
753 ,state(0), isRunning(0), haveData(0), can_delete(1)
755 CONNECT(zapTimer.timeout, eEPGCache::channel_data::startEPG);
756 CONNECT(abortTimer.timeout, eEPGCache::channel_data::abortNonAvail);
759 bool eEPGCache::channel_data::finishEPG()
761 if (!isRunning) // epg ready
763 eDebug("[EPGC] stop caching events(%d)", time(0)+eDVBLocalTimeHandler::getInstance()->difference());
764 zapTimer.start(UPDATE_INTERVAL, 1);
765 eDebug("[EPGC] next update in %i min", UPDATE_INTERVAL / 60000);
766 for (int i=0; i < 3; ++i)
768 seenSections[i].clear();
769 calcedSections[i].clear();
771 singleLock l(cache->cache_lock);
772 cache->channelLastUpdated[channel->getChannelID()] = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
779 void eEPGCache::channel_data::startEPG()
781 eDebug("[EPGC] start caching events(%d)", eDVBLocalTimeHandler::getInstance()->difference()+time(0));
785 for (int i=0; i < 3; ++i)
787 seenSections[i].clear();
788 calcedSections[i].clear();
791 eDVBSectionFilterMask mask;
792 memset(&mask, 0, sizeof(mask));
794 mask.flags = eDVBSectionFilterMask::rfCRC;
798 m_NowNextReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_NowNextConn);
799 m_NowNextReader->start(mask);
800 isRunning |= NOWNEXT;
804 m_ScheduleReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_ScheduleConn);
805 m_ScheduleReader->start(mask);
806 isRunning |= SCHEDULE;
810 m_ScheduleOtherReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_ScheduleOtherConn);
811 m_ScheduleOtherReader->start(mask);
812 isRunning |= SCHEDULE_OTHER;
814 abortTimer.start(7000,true);
817 void eEPGCache::channel_data::abortNonAvail()
821 if ( !(haveData&eEPGCache::NOWNEXT) && (isRunning&eEPGCache::NOWNEXT) )
823 eDebug("[EPGC] abort non avail nownext reading");
824 isRunning &= ~eEPGCache::NOWNEXT;
825 m_NowNextReader->stop();
828 if ( !(haveData&eEPGCache::SCHEDULE) && (isRunning&eEPGCache::SCHEDULE) )
830 eDebug("[EPGC] abort non avail schedule reading");
831 isRunning &= ~SCHEDULE;
832 m_ScheduleReader->stop();
835 if ( !(haveData&eEPGCache::SCHEDULE_OTHER) && (isRunning&eEPGCache::SCHEDULE_OTHER) )
837 eDebug("[EPGC] abort non avail schedule_other reading");
838 isRunning &= ~SCHEDULE_OTHER;
839 m_ScheduleOtherReader->stop();
840 m_ScheduleOtherConn=0;
843 abortTimer.start(90000, true);
847 for (int i=0; i < 3; ++i)
849 seenSections[i].clear();
850 calcedSections[i].clear();
858 void eEPGCache::channel_data::startChannel()
860 updateMap::iterator It = cache->channelLastUpdated.find( channel->getChannelID() );
862 int update = ( It != cache->channelLastUpdated.end() ? ( UPDATE_INTERVAL - ( (time(0)+eDVBLocalTimeHandler::getInstance()->difference()-It->second) * 1000 ) ) : ZAP_DELAY );
864 if (update < ZAP_DELAY)
867 zapTimer.start(update, 1);
869 eDebug("[EPGC] next update in %i min", update/60000);
870 else if (update >= 1000)
871 eDebug("[EPGC] next update in %i sec", update/1000);
874 void eEPGCache::channel_data::abortEPG()
876 for (int i=0; i < 3; ++i)
878 seenSections[i].clear();
879 calcedSections[i].clear();
885 eDebug("[EPGC] abort caching events !!");
886 if (isRunning & eEPGCache::SCHEDULE)
888 isRunning &= ~eEPGCache::SCHEDULE;
889 m_ScheduleReader->stop();
892 if (isRunning & eEPGCache::NOWNEXT)
894 isRunning &= ~eEPGCache::NOWNEXT;
895 m_NowNextReader->stop();
898 if (isRunning & SCHEDULE_OTHER)
900 isRunning &= ~eEPGCache::SCHEDULE_OTHER;
901 m_ScheduleOtherReader->stop();
902 m_ScheduleOtherConn=0;
908 void eEPGCache::channel_data::readData( const __u8 *data)
911 eDebug("get Null pointer from section reader !!");
916 iDVBSectionReader *reader=NULL;
920 reader=m_NowNextReader;
921 source=eEPGCache::NOWNEXT;
925 reader=m_ScheduleReader;
926 source=eEPGCache::SCHEDULE;
930 reader=m_ScheduleOtherReader;
931 source=eEPGCache::SCHEDULE_OTHER;
935 eDebug("[EPGC] unknown table_id !!!");
938 tidMap &seenSections = this->seenSections[map];
939 tidMap &calcedSections = this->calcedSections[map];
940 if ( state == 1 && calcedSections == seenSections || state > 1 )
942 eDebugNoNewLine("[EPGC] ");
945 case eEPGCache::NOWNEXT:
947 eDebugNoNewLine("nownext");
949 case eEPGCache::SCHEDULE:
951 eDebugNoNewLine("schedule");
953 case eEPGCache::SCHEDULE_OTHER:
954 m_ScheduleOtherConn=0;
955 eDebugNoNewLine("schedule other");
957 default: eDebugNoNewLine("unknown");break;
959 eDebug(" finished(%d)", time(0)+eDVBLocalTimeHandler::getInstance()->difference());
962 isRunning &= ~source;
968 eit_t *eit = (eit_t*) data;
969 __u32 sectionNo = data[0] << 24;
970 sectionNo |= data[3] << 16;
971 sectionNo |= data[4] << 8;
972 sectionNo |= eit->section_number;
974 tidMap::iterator it =
975 seenSections.find(sectionNo);
977 if ( it == seenSections.end() )
979 seenSections.insert(sectionNo);
980 calcedSections.insert(sectionNo);
981 __u32 tmpval = sectionNo & 0xFFFFFF00;
982 __u8 incr = source == NOWNEXT ? 1 : 8;
983 for ( int i = 0; i <= eit->last_section_number; i+=incr )
985 if ( i == eit->section_number )
987 for (int x=i; x <= eit->segment_last_section_number; ++x)
988 calcedSections.insert(tmpval|(x&0xFF));
991 calcedSections.insert(tmpval|(i&0xFF));
993 cache->sectionRead(data, source, this);
999 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eventData *&result )
1000 // if t == 0 we search the current event...
1002 singleLock s(cache_lock);
1003 uniqueEPGKey key(service);
1005 // check if EPG for this service is ready...
1006 eventCache::iterator It = eventDB.find( key );
1007 if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached ?
1010 t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
1012 // TODO: optimize this.. why we here search first in timemap.. and then in eventmap??
1013 timeMap::iterator i = It->second.second.lower_bound(t);
1014 if ( i != It->second.second.end() )
1016 if ( i != It->second.second.end() )
1018 if ( t <= i->first+i->second->getDuration() )
1026 for ( eventMap::iterator i( It->second.first.begin() ); i != It->second.first.end(); i++)
1028 int duration = i->second->getDuration();
1029 time_t begTime = i->second->getStartTime();
1030 if ( t >= begTime && t <= begTime+duration) // then we have found
1040 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eit_event_struct *&result )
1042 singleLock s(cache_lock);
1043 const eventData *data=0;
1044 RESULT ret = lookupEventTime(service, t, data);
1046 result = data->get();
1050 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, Event *& result )
1052 singleLock s(cache_lock);
1053 const eventData *data=0;
1054 RESULT ret = lookupEventTime(service, t, data);
1056 result = new Event((uint8_t*)data->get());
1060 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, ePtr<eServiceEvent> &result )
1062 singleLock s(cache_lock);
1063 const eventData *data=0;
1064 RESULT ret = lookupEventTime(service, t, data);
1067 Event ev((uint8_t*)data->get());
1068 result = new eServiceEvent();
1069 ret = result->parseFrom(&ev);
1074 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eventData *&result )
1076 singleLock s(cache_lock);
1077 uniqueEPGKey key( service );
1079 eventCache::iterator It = eventDB.find( key );
1080 if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
1082 eventMap::iterator i( It->second.first.find( event_id ));
1083 if ( i != It->second.first.end() )
1091 eDebug("event %04x not found in epgcache", event_id);
1097 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&result)
1099 singleLock s(cache_lock);
1100 const eventData *data=0;
1101 RESULT ret = lookupEventId(service, event_id, data);
1103 result = data->get();
1107 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, Event *& result)
1109 singleLock s(cache_lock);
1110 const eventData *data=0;
1111 RESULT ret = lookupEventId(service, event_id, data);
1113 result = new Event((uint8_t*)data->get());
1117 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &result)
1119 singleLock s(cache_lock);
1120 const eventData *data=0;
1121 RESULT ret = lookupEventId(service, event_id, data);
1124 Event ev((uint8_t*)data->get());
1125 result = new eServiceEvent();
1126 ret = result->parseFrom(&ev);
1131 RESULT eEPGCache::startTimeQuery(const eServiceReference &service, time_t begin, int minutes)
1133 eventCache::iterator It = eventDB.find( service );
1134 if ( It != eventDB.end() && It->second.second.size() )
1136 m_timemap_end = minutes != -1 ? It->second.second.upper_bound(begin+minutes*60) : It->second.second.end();
1139 m_timemap_cursor = It->second.second.lower_bound(begin);
1140 if ( m_timemap_cursor != It->second.second.end() && m_timemap_cursor != It->second.second.begin() )
1142 timeMap::iterator it = m_timemap_cursor;
1144 if ( (it->second->getStartTime() + it->second->getDuration()) > begin )
1145 m_timemap_cursor = it;
1149 m_timemap_cursor = It->second.second.begin();
1155 RESULT eEPGCache::getNextTimeEntry(const eventData *& result)
1157 if ( m_timemap_cursor != m_timemap_end )
1159 result = m_timemap_cursor++->second;
1165 RESULT eEPGCache::getNextTimeEntry(const eit_event_struct *&result)
1167 if ( m_timemap_cursor != m_timemap_end )
1169 result = m_timemap_cursor++->second->get();
1175 RESULT eEPGCache::getNextTimeEntry(Event *&result)
1177 if ( m_timemap_cursor != m_timemap_end )
1179 result = new Event((uint8_t*)m_timemap_cursor++->second->get());
1185 RESULT eEPGCache::getNextTimeEntry(ePtr<eServiceEvent> &result)
1187 if ( m_timemap_cursor != m_timemap_end )
1189 Event ev((uint8_t*)m_timemap_cursor++->second->get());
1190 result = new eServiceEvent();
1191 return result->parseFrom(&ev);