performance fix
[enigma2.git] / lib / dvb / epgcache.cpp
index 34bd9f2f33da0984fc1b309c84f4167b9efe6508..7a1f78ddebc68df7d18979833684d5b62dcf433f 100644 (file)
@@ -6,7 +6,7 @@
 #include <time.h>
 #include <unistd.h>  // for usleep
 #include <sys/vfs.h> // for statfs
-#include <libmd5sum.h>
+// #include <libmd5sum.h>
 #include <lib/base/eerror.h>
 
 int eventData::CacheSize=0;
@@ -84,22 +84,21 @@ void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
                        }
                        else
                        {
-                               RESULT res;
-                               data.m_NowNextReader = new eDVBSectionReader( demux, this, res );
+                               RESULT res = demux->createSectionReader( this, data.m_NowNextReader );
                                if ( res )
                                {
                                        eDebug("[eEPGCache] couldnt initialize nownext reader!!");
                                        return;
                                }
                                data.m_NowNextReader->connectRead(slot(data, &eEPGCache::channel_data::readData), data.m_NowNextConn);
-                               data.m_ScheduleReader = new eDVBSectionReader( demux, this, res );
+                               res = demux->createSectionReader( this, data.m_ScheduleReader );
                                if ( res )
                                {
                                        eDebug("[eEPGCache] couldnt initialize schedule reader!!");
                                        return;
                                }
                                data.m_ScheduleReader->connectRead(slot(data, &eEPGCache::channel_data::readData), data.m_ScheduleConn);
-                               data.m_ScheduleOtherReader = new eDVBSectionReader( demux, this, res );
+                               res = demux->createSectionReader( this, data.m_ScheduleOtherReader );
                                if ( res )
                                {
                                        eDebug("[eEPGCache] couldnt initialize schedule other reader!!");
@@ -150,16 +149,15 @@ void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
        }
 }
 
-int eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
+void eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
 {
        eit_t *eit = (eit_t*) data;
 
        int len=HILO(eit->section_length)-1;//+3-4;
        int ptr=EIT_SIZE;
        if ( ptr >= len )
-               return 0;
+               return;
 
-       //
        // This fixed the EPG on the Multichoice irdeto systems
        // the EIT packet is non-compliant.. their EIT packet stinks
        if ( data[ptr-1] < 0x40 )
@@ -171,7 +169,6 @@ int eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
        int duration;
 
        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);
-// FIXME !!! TIME CORRECTION !
        time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
 
        if ( TM != 3599 && TM > -1)
@@ -211,7 +208,7 @@ int eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
                        int ev_erase_count = 0;
                        int tm_erase_count = 0;
 
-// search in eventmap
+                       // search in eventmap
                        eventMap::iterator ev_it =
                                servicemap.first.find(event_id);
 
@@ -221,7 +218,7 @@ int eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
                                if ( source > ev_it->second->type )  // update needed ?
                                        goto next; // when not.. the skip this entry
 
-// search this event in timemap
+                               // search this event in timemap
                                timeMap::iterator tm_it_tmp = 
                                        servicemap.second.find(ev_it->second->getStartTime());
 
@@ -246,20 +243,20 @@ int eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
                                }
                        }
 
-// 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
+                       // 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.second.find(TM);
 
                        if ( tm_it != servicemap.second.end() )
                        {
-// i think, if event is not found on eventmap, but found on timemap updating nevertheless demands
+                               // i think, if event is not found on eventmap, but found on timemap updating nevertheless demands
 #if 0
                                if ( source > tm_it->second->type && tm_erase_count == 0 ) // update needed ?
                                        goto next; // when not.. the skip this entry
 #endif
 
-// search this time in eventmap
+                               // search this time in eventmap
                                eventMap::iterator ev_it_tmp = 
                                        servicemap.first.find(tm_it->second->getEventID());
 
@@ -341,8 +338,8 @@ next:
                        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 );
+                                       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 );
                        fclose(f);
 
                        eFatal("(1)map sizes not equal :( sid %04x tsid %04x onid %04x size %d size2 %d", 
@@ -353,8 +350,6 @@ next:
                ptr += eit_event_size;
                eit_event=(eit_event_struct*)(((__u8*)eit_event)+eit_event_size);
        }
-
-       return 0;
 }
 
 void eEPGCache::flushEPG(const uniqueEPGKey & s)
@@ -578,6 +573,7 @@ void eEPGCache::thread()
 
 void eEPGCache::load()
 {
+#if 0
        FILE *f = fopen("/hdd/epg.dat", "r");
        if (f)
        {
@@ -627,10 +623,12 @@ void eEPGCache::load()
                }
                fclose(f);
        }
+#endif
 }
 
 void eEPGCache::save()
 {
+#if 0
        struct statfs s;
        off64_t tmp;
        if (statfs("/hdd", &s)<0)
@@ -685,6 +683,7 @@ void eEPGCache::save()
                        }
                }
        }
+#endif
 }
 
 RESULT eEPGCache::getInstance(ePtr<eEPGCache> &ptr)
@@ -708,10 +707,14 @@ bool eEPGCache::channel_data::finishEPG()
 {
        if (!isRunning)  // epg ready
        {
-               eDebug("[EPGC] stop caching events");
+               eDebug("[EPGC] stop caching events(%d)", time(0)+eDVBLocalTimeHandler::getInstance()->difference());
                zapTimer.start(UPDATE_INTERVAL, 1);
                eDebug("[EPGC] next update in %i min", UPDATE_INTERVAL / 60000);
-
+               for (int i=0; i < 3; ++i)
+               {
+                       seenSections[i].clear();
+                       calcedSections[i].clear();
+               }
                singleLock l(cache->cache_lock);
                cache->channelLastUpdated[channel->getChannelID()] = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
                can_delete=1;
@@ -722,10 +725,15 @@ bool eEPGCache::channel_data::finishEPG()
 
 void eEPGCache::channel_data::startEPG()
 {
-       eDebug("[EPGC] start caching events");
+       eDebug("[EPGC] start caching events(%d)", eDVBLocalTimeHandler::getInstance()->difference()+time(0));
        state=0;
        haveData=0;
        can_delete=0;
+       for (int i=0; i < 3; ++i)
+       {
+               seenSections[i].clear();
+               calcedSections[i].clear();
+       }
 
        eDVBSectionFilterMask mask;
        memset(&mask, 0, sizeof(mask));
@@ -747,7 +755,7 @@ void eEPGCache::channel_data::startEPG()
        m_ScheduleOtherReader->start(mask);
        isRunning |= SCHEDULE_OTHER;
 
-       abortTimer.start(5000,true);
+       abortTimer.start(7000,true);
 }
 
 void eEPGCache::channel_data::abortNonAvail()
@@ -774,9 +782,17 @@ void eEPGCache::channel_data::abortNonAvail()
                        m_ScheduleOtherReader->stop();
                }
                if ( isRunning )
-                       abortTimer.start(20000, true);
+                       abortTimer.start(50000, true);
                else
-                       finishEPG();
+               {
+                       ++state;
+                       for (int i=0; i < 3; ++i)
+                       {
+                               seenSections[i].clear();
+                               calcedSections[i].clear();
+                       }
+                       can_delete=1;
+               }
        }
        ++state;
 }
@@ -799,6 +815,11 @@ void eEPGCache::channel_data::startChannel()
 
 void eEPGCache::channel_data::abortEPG()
 {
+       for (int i=0; i < 3; ++i)
+       {
+               seenSections[i].clear();
+               calcedSections[i].clear();
+       }
        abortTimer.stop();
        zapTimer.stop();
        if (isRunning)
@@ -832,28 +853,73 @@ void eEPGCache::channel_data::readData( const __u8 *data)
                eDebug("get Null pointer from section reader !!");
        else
        {
-               int source = data[0] > 0x5F ? eEPGCache::SCHEDULE_OTHER : data[0] > 0x4F ? eEPGCache::SCHEDULE : eEPGCache::NOWNEXT;
-               if ( state == 2 )
+               int source;
+               int map;
+               iDVBSectionReader *reader=NULL;
+               switch(data[0])
                {
-                       iDVBSectionReader *reader=NULL;
+                       case 0x4E ... 0x4F:
+                               reader=m_NowNextReader;
+                               source=eEPGCache::NOWNEXT;
+                               map=0;
+                               break;
+                       case 0x50 ... 0x5F:
+                               reader=m_ScheduleReader;
+                               source=eEPGCache::SCHEDULE;
+                               map=1;
+                               break;
+                       case 0x60 ... 0x6F:
+                               reader=m_ScheduleOtherReader;
+                               source=eEPGCache::SCHEDULE_OTHER;
+                               map=2;
+                               break;
+               }
+               tidMap &seenSections = this->seenSections[map];
+               tidMap &calcedSections = this->calcedSections[map];
+               if ( state == 1 && calcedSections == seenSections || state > 1 )
+               {
+                       eDebugNoNewLine("[EPGC] ");
                        switch (source)
                        {
-                               case eEPGCache::SCHEDULE_OTHER:
-                                       reader=m_ScheduleOtherReader;
-                                       break;
-                               case eEPGCache::SCHEDULE:
-                                       reader=m_ScheduleReader;
-                                       break;
-                               case eEPGCache::NOWNEXT:
-                                       reader=m_NowNextReader;
-                                       break;
+                               case eEPGCache::NOWNEXT: eDebugNoNewLine("nownext");break;
+                               case eEPGCache::SCHEDULE: eDebugNoNewLine("schedule");break;
+                               case eEPGCache::SCHEDULE_OTHER: eDebugNoNewLine("schedule other");break;
+                               default: eDebugNoNewLine("unknown");break;
                        }
+                       eDebug(" finished(%d)", time(0)+eDVBLocalTimeHandler::getInstance()->difference());
                        reader->stop();
                        isRunning &= ~source;
                        if (!isRunning)
                                finishEPG();
                }
                else
-                       cache->sectionRead(data, source, this);
+               {
+                       eit_t *eit = (eit_t*) data;
+                       __u32 sectionNo = data[0] << 24;
+                       sectionNo |= data[3] << 16;
+                       sectionNo |= data[4] << 8;
+                       sectionNo |= eit->section_number;
+
+                       tidMap::iterator it =
+                               seenSections.find(sectionNo);
+
+                       if ( it == seenSections.end() )
+                       {
+                               seenSections.insert(sectionNo);
+                               __u32 tmpval = sectionNo & 0xFFFFFF00;
+                               __u8 incr = source == NOWNEXT ? 1 : 8;
+                               for ( int i = 0; i <= eit->last_section_number; i+=incr )
+                               {
+                                       if ( i == eit->section_number )
+                                       {
+                                               for (int x=i; x <= eit->segment_last_section_number; ++x)
+                                                       calcedSections.insert(tmpval|(x&0xFF));
+                                       }
+                                       else
+                                               calcedSections.insert(tmpval|(i&0xFF));
+                               }
+                               cache->sectionRead(data, source, this);
+                       }
+               }
        }
 }