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
10 #include <lib/base/eerror.h>
12 int eventData::CacheSize=0;
14 eEPGCache* eEPGCache::instance;
15 pthread_mutex_t eEPGCache::cache_lock=
16 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
17 pthread_mutex_t eEPGCache::channel_map_lock=
18 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
22 eEPGCache::eEPGCache()
23 :messages(this,1), cleanTimer(this)//, paused(0)
25 eDebug("[EPGC] Initialized EPGCache");
27 CONNECT(messages.recv_msg, eEPGCache::gotMessage);
28 CONNECT(eDVBLocalTimeHandler::getInstance()->m_timeUpdated, eEPGCache::timeUpdated);
29 CONNECT(cleanTimer.timeout, eEPGCache::cleanLoop);
31 ePtr<eDVBResourceManager> res_mgr;
32 eDVBResourceManager::getInstance(res_mgr);
34 eDebug("[eEPGCache] no resource manager !!!!!!!");
36 res_mgr->connectChannelAdded(slot(*this,&eEPGCache::DVBChannelAdded), m_chanAddedConn);
40 void eEPGCache::timeUpdated()
42 if ( !thread_running() )
44 eDebug("[EPGC] time updated.. start EPG Mainloop");
48 messages.send(Message(Message::timeChanged));
51 void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
55 eDebug("[eEPGCache] add channel %p", chan);
56 channel_data *data = new channel_data(this);
58 singleLock s(channel_map_lock);
59 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
60 chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
64 void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
66 singleLock s(channel_map_lock);
67 channelMapIterator it =
68 m_knownChannels.find(chan);
69 if ( it == m_knownChannels.end() )
70 eDebug("[eEPGCache] will start non existing channel %p !!!", chan);
73 channel_data &data = *it->second;
74 ePtr<eDVBResourceManager> res_mgr;
75 if ( eDVBResourceManager::getInstance( res_mgr ) )
76 eDebug("[eEPGCache] no res manager!!");
79 ePtr<iDVBDemux> demux;
80 if ( data.channel->getDemux(demux) )
82 eDebug("[eEPGCache] no demux!!");
88 data.m_NowNextReader = new eDVBSectionReader( demux, this, res );
91 eDebug("[eEPGCache] couldnt initialize nownext reader!!");
94 data.m_NowNextReader->connectRead(slot(data, &eEPGCache::channel_data::readData), data.m_NowNextConn);
95 data.m_ScheduleReader = new eDVBSectionReader( demux, this, res );
98 eDebug("[eEPGCache] couldnt initialize schedule reader!!");
101 data.m_ScheduleReader->connectRead(slot(data, &eEPGCache::channel_data::readData), data.m_ScheduleConn);
102 data.m_ScheduleOtherReader = new eDVBSectionReader( demux, this, res );
105 eDebug("[eEPGCache] couldnt initialize schedule other reader!!");
108 data.m_ScheduleOtherReader->connectRead(slot(data, &eEPGCache::channel_data::readData), data.m_ScheduleOtherConn);
109 messages.send(Message(Message::startChannel, chan));
110 // -> gotMessage -> changedService
116 void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
118 channelMapIterator it =
119 m_knownChannels.find(chan);
120 if ( it != m_knownChannels.end() )
123 chan->getState(state);
126 case iDVBChannel::state_idle:
128 case iDVBChannel::state_tuning:
130 case iDVBChannel::state_unavailable:
132 case iDVBChannel::state_ok:
134 eDebug("[eEPGCache] channel %p running", chan);
135 DVBChannelRunning(chan);
138 case iDVBChannel::state_release:
140 eDebug("[eEPGCache] remove channel %p", chan);
141 messages.send(Message(Message::leaveChannel, chan));
142 while(!it->second->can_delete)
145 m_knownChannels.erase(it);
146 // -> gotMessage -> abortEPG
153 int eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
155 eit_t *eit = (eit_t*) data;
157 int len=HILO(eit->section_length)-1;//+3-4;
163 // This fixed the EPG on the Multichoice irdeto systems
164 // the EIT packet is non-compliant.. their EIT packet stinks
165 if ( data[ptr-1] < 0x40 )
168 uniqueEPGKey service( HILO(eit->service_id), HILO(eit->original_network_id), HILO(eit->transport_stream_id) );
169 eit_event_struct* eit_event = (eit_event_struct*) (data+ptr);
173 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);
174 // FIXME !!! TIME CORRECTION !
175 time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
177 if ( TM != 3599 && TM > -1)
178 channel->haveData |= source;
180 singleLock s(cache_lock);
181 // hier wird immer eine eventMap zurück gegeben.. entweder eine vorhandene..
182 // oder eine durch [] erzeugte
183 std::pair<eventMap,timeMap> &servicemap = eventDB[service];
184 eventMap::iterator prevEventIt = servicemap.first.end();
185 timeMap::iterator prevTimeIt = servicemap.second.end();
189 eit_event_size = HILO(eit_event->descriptors_loop_length)+EIT_LOOP_SIZE;
191 duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
193 eit_event->start_time_1,
194 eit_event->start_time_2,
195 eit_event->start_time_3,
196 eit_event->start_time_4,
197 eit_event->start_time_5);
202 if ( TM != 3599 && (TM+duration < now || TM > now+14*24*60*60) )
205 if ( now <= (TM+duration) || TM == 3599 /*NVOD Service*/ ) // old events should not be cached
207 __u16 event_id = HILO(eit_event->event_id);
208 // eDebug("event_id is %d sid is %04x", event_id, service.sid);
211 int ev_erase_count = 0;
212 int tm_erase_count = 0;
214 // search in eventmap
215 eventMap::iterator ev_it =
216 servicemap.first.find(event_id);
218 // entry with this event_id is already exist ?
219 if ( ev_it != servicemap.first.end() )
221 if ( source > ev_it->second->type ) // update needed ?
222 goto next; // when not.. the skip this entry
224 // search this event in timemap
225 timeMap::iterator tm_it_tmp =
226 servicemap.second.find(ev_it->second->getStartTime());
228 if ( tm_it_tmp != servicemap.second.end() )
230 if ( tm_it_tmp->first == TM ) // correct eventData
233 delete ev_it->second;
234 evt = new eventData(eit_event, eit_event_size, source);
236 tm_it_tmp->second=evt;
242 // delete the found record from timemap
243 servicemap.second.erase(tm_it_tmp);
244 prevTimeIt=servicemap.second.end();
249 // search in timemap, for check of a case if new time has coincided with time of other event
250 // or event was is not found in eventmap
251 timeMap::iterator tm_it =
252 servicemap.second.find(TM);
254 if ( tm_it != servicemap.second.end() )
256 // i think, if event is not found on eventmap, but found on timemap updating nevertheless demands
258 if ( source > tm_it->second->type && tm_erase_count == 0 ) // update needed ?
259 goto next; // when not.. the skip this entry
262 // search this time in eventmap
263 eventMap::iterator ev_it_tmp =
264 servicemap.first.find(tm_it->second->getEventID());
266 if ( ev_it_tmp != servicemap.first.end() )
269 // delete the found record from eventmap
270 servicemap.first.erase(ev_it_tmp);
271 prevEventIt=servicemap.first.end();
275 evt = new eventData(eit_event, eit_event_size, source);
277 bool consistencyCheck=true;
279 if (ev_erase_count > 0 && tm_erase_count > 0) // 2 different pairs have been removed
282 delete ev_it->second;
283 delete tm_it->second;
287 else if (ev_erase_count == 0 && tm_erase_count > 0)
290 delete ev_it->second;
291 tm_it=prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
294 else if (ev_erase_count > 0 && tm_erase_count == 0)
297 delete tm_it->second;
298 ev_it=prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
301 else // added new eventData
304 consistencyCheck=false;
306 prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
307 prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
310 if ( consistencyCheck )
312 if ( tm_it->second != evt || ev_it->second != evt )
313 eFatal("tm_it->second != ev_it->second");
314 else if ( tm_it->second->getStartTime() != tm_it->first )
315 eFatal("event start_time(%d) non equal timemap key(%d)",
316 tm_it->second->getStartTime(), tm_it->first );
317 else if ( tm_it->first != TM )
318 eFatal("timemap key(%d) non equal TM(%d)",
320 else if ( ev_it->second->getEventID() != ev_it->first )
321 eFatal("event_id (%d) non equal event_map key(%d)",
322 ev_it->second->getEventID(), ev_it->first);
323 else if ( ev_it->first != event_id )
324 eFatal("eventmap key(%d) non equal event_id(%d)",
325 ev_it->first, event_id );
331 if ( servicemap.first.size() != servicemap.second.size() )
333 FILE *f = fopen("/hdd/event_map.txt", "w+");
335 for (eventMap::iterator it(servicemap.first.begin())
336 ; it != servicemap.first.end(); ++it )
337 fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n",
338 i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
340 f = fopen("/hdd/time_map.txt", "w+");
342 for (timeMap::iterator it(servicemap.second.begin())
343 ; it != servicemap.second.end(); ++it )
344 fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n",
345 i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
348 eFatal("(1)map sizes not equal :( sid %04x tsid %04x onid %04x size %d size2 %d",
349 service.sid, service.tsid, service.onid,
350 servicemap.first.size(), servicemap.second.size() );
353 ptr += eit_event_size;
354 eit_event=(eit_event_struct*)(((__u8*)eit_event)+eit_event_size);
360 void eEPGCache::flushEPG(const uniqueEPGKey & s)
362 eDebug("[EPGC] flushEPG %d", (int)(bool)s);
363 singleLock l(cache_lock);
364 if (s) // clear only this service
366 eventCache::iterator it = eventDB.find(s);
367 if ( it != eventDB.end() )
369 eventMap &evMap = it->second.first;
370 timeMap &tmMap = it->second.second;
372 for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
377 // TODO .. search corresponding channel for removed service and remove this channel from lastupdated map
380 else // clear complete EPG Cache
382 for (eventCache::iterator it(eventDB.begin());
383 it != eventDB.end(); ++it)
385 eventMap &evMap = it->second.first;
386 timeMap &tmMap = it->second.second;
387 for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
393 channelLastUpdated.clear();
394 singleLock m(channel_map_lock);
395 for (channelMapIterator it(m_knownChannels.begin()); it != m_knownChannels.end(); ++it)
396 it->second->startEPG();
398 eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
401 void eEPGCache::cleanLoop()
403 singleLock s(cache_lock);
404 if (!eventDB.empty())
406 eDebug("[EPGC] start cleanloop");
407 const eit_event_struct* cur_event;
410 // FIXME !!! TIME_CORRECTION
411 time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
413 for (eventCache::iterator DBIt = eventDB.begin(); DBIt != eventDB.end(); DBIt++)
415 for (timeMap::iterator It = DBIt->second.second.begin(); It != DBIt->second.second.end() && It->first < now;)
417 cur_event = (*It->second).get();
418 duration = fromBCD( cur_event->duration_1)*3600 + fromBCD(cur_event->duration_2)*60 + fromBCD(cur_event->duration_3);
420 if ( now > (It->first+duration) ) // outdated normal entry (nvod references to)
422 // remove entry from eventMap
423 eventMap::iterator b(DBIt->second.first.find(It->second->getEventID()));
424 if ( b != DBIt->second.first.end() )
426 // release Heap Memory for this entry (new ....)
427 // eDebug("[EPGC] delete old event (evmap)");
428 DBIt->second.first.erase(b);
431 // remove entry from timeMap
432 // eDebug("[EPGC] release heap mem");
434 DBIt->second.second.erase(It++);
435 // eDebug("[EPGC] delete old event (timeMap)");
441 eDebug("[EPGC] stop cleanloop");
442 eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
444 cleanTimer.start(CLEAN_INTERVAL,true);
447 eEPGCache::~eEPGCache()
449 messages.send(Message::quit);
450 kill(); // waiting for thread shutdown
451 singleLock s(cache_lock);
452 for (eventCache::iterator evIt = eventDB.begin(); evIt != eventDB.end(); evIt++)
453 for (eventMap::iterator It = evIt->second.first.begin(); It != evIt->second.first.end(); It++)
457 Event *eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, bool plain)
459 singleLock s(cache_lock);
460 uniqueEPGKey key( service );
462 eventCache::iterator It = eventDB.find( key );
463 if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
465 eventMap::iterator i( It->second.first.find( event_id ));
466 if ( i != It->second.first.end() )
468 if ( service.getServiceType() == 4 ) // nvod ref
469 return lookupEvent( service, i->second->getStartTime(), plain );
471 // get plain data... not in Event Format !!!
472 // before use .. cast it to eit_event_struct*
473 return (Event*) i->second->get();
475 return new Event( (uint8_t*)i->second->get() /*, (It->first.tsid<<16)|It->first.onid*/ );
478 eDebug("event %04x not found in epgcache", event_id);
483 Event *eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, bool plain )
484 // if t == 0 we search the current event...
486 singleLock s(cache_lock);
487 uniqueEPGKey key(service);
489 // check if EPG for this service is ready...
490 eventCache::iterator It = eventDB.find( key );
491 if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached ?
494 t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
496 timeMap::iterator i = It->second.second.lower_bound(t);
497 if ( i != It->second.second.end() )
500 if ( i != It->second.second.end() )
502 const eit_event_struct* eit_event = i->second->get();
503 int duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
504 if ( t <= i->first+duration )
507 // get plain data... not in Event Format !!!
508 // before use .. cast it to eit_event_struct*
509 return (Event*) i->second->get();
510 return new Event( (uint8_t*)i->second->get() /*, (It->first.tsid<<16)|It->first.onid */ );
515 for ( eventMap::iterator i( It->second.first.begin() ); i != It->second.first.end(); i++)
517 const eit_event_struct* eit_event = i->second->get();
518 int duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
519 time_t begTime = 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);
520 if ( t >= begTime && t <= begTime+duration) // then we have found
523 // get plain data... not in Event Format !!!
524 // before use .. cast it to eit_event_struct*
525 return (Event*) i->second->get();
526 return new Event( (uint8_t*)i->second->get()/*, (It->first.tsid<<16)|It->first.onid*/ );
533 void eEPGCache::gotMessage( const Message &msg )
538 flushEPG(msg.service);
540 case Message::startChannel:
542 singleLock s(channel_map_lock);
543 channelMapIterator channel =
544 m_knownChannels.find(msg.channel);
545 if ( channel != m_knownChannels.end() )
546 channel->second->startChannel();
549 case Message::leaveChannel:
551 singleLock s(channel_map_lock);
552 channelMapIterator channel =
553 m_knownChannels.find(msg.channel);
554 if ( channel != m_knownChannels.end() )
555 channel->second->abortEPG();
561 case Message::timeChanged:
565 eDebug("unhandled EPGCache Message!!");
570 void eEPGCache::thread()
579 void eEPGCache::load()
581 FILE *f = fopen("/hdd/epg.dat", "r");
584 unsigned char md5_saved[16];
585 unsigned char md5[16];
589 if (!md5_file("/hdd/epg.dat", 1, md5))
591 FILE *f = fopen("/hdd/epg.dat.md5", "r");
594 fread( md5_saved, 16, 1, f);
596 if ( !memcmp(md5_saved, md5, 16) )
602 fread( &size, sizeof(int), 1, f);
609 fread( &key, sizeof(uniqueEPGKey), 1, f);
610 fread( &size, sizeof(int), 1, f);
616 fread( &type, sizeof(int), 1, f);
617 fread( &len, sizeof(int), 1, f);
618 event = new eventData(0, len, type);
619 fread( event->EITdata, len, 1, f);
620 evMap[ event->getEventID() ]=event;
621 tmMap[ event->getStartTime() ]=event;
624 eventDB[key]=std::pair<eventMap,timeMap>(evMap,tmMap);
626 eDebug("%d events read from /hdd/epg.dat.md5", cnt);
632 void eEPGCache::save()
636 if (statfs("/hdd", &s)<0)
644 // prevent writes to builtin flash
645 if ( tmp < 1024*1024*50 ) // storage size < 50MB
648 // check for enough free space on storage
651 if ( tmp < (eventData::CacheSize*12)/10 ) // 20% overhead
654 FILE *f = fopen("/hdd/epg.dat", "w");
658 int size = eventDB.size();
659 fwrite( &size, sizeof(int), 1, f );
660 for (eventCache::iterator service_it(eventDB.begin()); service_it != eventDB.end(); ++service_it)
662 timeMap &timemap = service_it->second.second;
663 fwrite( &service_it->first, sizeof(uniqueEPGKey), 1, f);
664 size = timemap.size();
665 fwrite( &size, sizeof(int), 1, f);
666 for (timeMap::iterator time_it(timemap.begin()); time_it != timemap.end(); ++time_it)
668 int len = time_it->second->ByteSize;
669 fwrite( &time_it->second->type, sizeof(int), 1, f );
670 fwrite( &len, sizeof(int), 1, f);
671 fwrite( time_it->second->EITdata, len, 1, f);
675 eDebug("%d events written to /hdd/epg.dat", cnt);
677 unsigned char md5[16];
678 if (!md5_file("/hdd/epg.dat", 1, md5))
680 FILE *f = fopen("/hdd/epg.dat.md5", "w");
683 fwrite( md5, 16, 1, f);
690 RESULT eEPGCache::getInstance(ePtr<eEPGCache> &ptr)
698 eEPGCache::channel_data::channel_data(eEPGCache *ml)
700 ,abortTimer(ml), zapTimer(ml)
701 ,state(0), isRunning(0), haveData(0), can_delete(1)
703 CONNECT(zapTimer.timeout, eEPGCache::channel_data::startEPG);
704 CONNECT(abortTimer.timeout, eEPGCache::channel_data::abortNonAvail);
707 bool eEPGCache::channel_data::finishEPG()
709 if (!isRunning) // epg ready
711 eDebug("[EPGC] stop caching events");
712 zapTimer.start(UPDATE_INTERVAL, 1);
713 eDebug("[EPGC] next update in %i min", UPDATE_INTERVAL / 60000);
715 singleLock l(cache->cache_lock);
716 cache->channelLastUpdated[channel->getChannelID()] = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
723 void eEPGCache::channel_data::startEPG()
725 eDebug("[EPGC] start caching events");
730 eDVBSectionFilterMask mask;
731 memset(&mask, 0, sizeof(mask));
733 mask.flags = eDVBSectionFilterMask::rfCRC;
737 m_NowNextReader->start(mask);
738 isRunning |= NOWNEXT;
742 m_ScheduleReader->start(mask);
743 isRunning |= SCHEDULE;
747 m_ScheduleOtherReader->start(mask);
748 isRunning |= SCHEDULE_OTHER;
750 abortTimer.start(5000,true);
753 void eEPGCache::channel_data::abortNonAvail()
757 if ( !(haveData&eEPGCache::NOWNEXT) && (isRunning&eEPGCache::NOWNEXT) )
759 eDebug("[EPGC] abort non avail nownext reading");
760 isRunning &= ~eEPGCache::NOWNEXT;
761 if ( m_NowNextReader )
762 m_NowNextReader->stop();
764 if ( !(haveData&eEPGCache::SCHEDULE) && (isRunning&eEPGCache::SCHEDULE) )
766 eDebug("[EPGC] abort non avail schedule reading");
767 isRunning &= ~SCHEDULE;
768 m_ScheduleReader->stop();
770 if ( !(haveData&eEPGCache::SCHEDULE_OTHER) && (isRunning&eEPGCache::SCHEDULE_OTHER) )
772 eDebug("[EPGC] abort non avail schedule_other reading");
773 isRunning &= ~SCHEDULE_OTHER;
774 m_ScheduleOtherReader->stop();
777 abortTimer.start(20000, true);
784 void eEPGCache::channel_data::startChannel()
786 updateMap::iterator It = cache->channelLastUpdated.find( channel->getChannelID() );
788 int update = ( It != cache->channelLastUpdated.end() ? ( UPDATE_INTERVAL - ( (time(0)+eDVBLocalTimeHandler::getInstance()->difference()-It->second) * 1000 ) ) : ZAP_DELAY );
790 if (update < ZAP_DELAY)
793 zapTimer.start(update, 1);
795 eDebug("[EPGC] next update in %i min", update/60000);
796 else if (update >= 1000)
797 eDebug("[EPGC] next update in %i sec", update/1000);
800 void eEPGCache::channel_data::abortEPG()
806 eDebug("[EPGC] abort caching events !!");
807 if (isRunning & eEPGCache::SCHEDULE)
809 isRunning &= eEPGCache::SCHEDULE;
810 if ( m_ScheduleReader )
811 m_ScheduleReader->stop();
813 if (isRunning & eEPGCache::NOWNEXT)
815 isRunning &= ~eEPGCache::NOWNEXT;
816 if ( m_NowNextReader )
817 m_NowNextReader->stop();
819 if (isRunning & SCHEDULE_OTHER)
821 isRunning &= ~eEPGCache::SCHEDULE_OTHER;
822 if ( m_ScheduleOtherReader )
823 m_ScheduleOtherReader->stop();
829 void eEPGCache::channel_data::readData( const __u8 *data)
832 eDebug("get Null pointer from section reader !!");
835 int source = data[0] > 0x5F ? eEPGCache::SCHEDULE_OTHER : data[0] > 0x4F ? eEPGCache::SCHEDULE : eEPGCache::NOWNEXT;
838 iDVBSectionReader *reader=NULL;
841 case eEPGCache::SCHEDULE_OTHER:
842 reader=m_ScheduleOtherReader;
844 case eEPGCache::SCHEDULE:
845 reader=m_ScheduleReader;
847 case eEPGCache::NOWNEXT:
848 reader=m_NowNextReader;
852 isRunning &= ~source;
857 cache->sectionRead(data, source, this);