fix channel state handling
[enigma2.git] / lib / dvb / epgcache.cpp
index c22bf89..6d81119 100644 (file)
 
 int eventData::CacheSize=0;
 descriptorMap eventData::descriptors;
-
+__u8 eventData::data[4108];
 extern const uint32_t crc32_table[256];
 
 eventData::eventData(const eit_event_struct* e, int size, int type)
-       :ByteSize(size), type(type)
+       :ByteSize(size&0xFF), type(type&0xFF)
 {
        if (!e)
                return;
-       std::list<__u32> d;
+
+       __u32 descr[65];
+       __u32 *pdescr=descr;
+
        __u8 *data = (__u8*)e;
        int ptr=10;
        int descriptors_length = (data[ptr++]&0x0F) << 8;
        descriptors_length |= data[ptr++];
        while ( descriptors_length > 0 )
        {
-               int descr_len = data[ptr+1] + 2;
-               __u8 *descr = new __u8[descr_len];
-               unsigned int crc=0;
+               __u8 *descr = data+ptr;
+               int descr_len = descr[1]+2;
+
+               __u32 crc = 0;
                int cnt=0;
-               while(cnt < descr_len && descriptors_length > 0)
-               {
-                       crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[ptr]) & 0xff];
-                       descr[cnt++] = data[ptr++];
-                       --descriptors_length;
-               }
+               while(cnt++ < descr_len)
+                       crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[ptr++]) & 0xFF];
+
                descriptorMap::iterator it =
                        descriptors.find(crc);
                if ( it == descriptors.end() )
                {
                        CacheSize+=descr_len;
-                       descriptors[crc] = descriptorPair(1, descr);
+                       __u8 *d = new __u8[descr_len];
+                       memcpy(d, descr, descr_len);
+                       descriptors[crc] = descriptorPair(1, d);
                }
                else
-               {
                        ++it->second.first;
-                       delete [] descr;
-               }
-               d.push_back(crc);
+
+               *pdescr++=crc;
+               descriptors_length -= descr_len;
        }
-       ByteSize = 12+d.size()*4;
+       ByteSize = 12+((pdescr-descr)*4);
        EITdata = new __u8[ByteSize];
        CacheSize+=ByteSize;
        memcpy(EITdata, (__u8*) e, 12);
-       __u32 *p = (__u32*)(EITdata+12);
-       for (std::list<__u32>::iterator it(d.begin()); it != d.end(); ++it)
-               *p++ = *it;
+       memcpy(EITdata+12, descr, ByteSize-12);
 }
 
 const eit_event_struct* eventData::get() const
 {
-       static __u8 *data=NULL;
-       if ( data )
-               delete [] data;
-
-       int bytes = 12;
-       std::list<__u8*> d;
-
-// cnt needed bytes
+       int pos = 12;
        int tmp = ByteSize-12;
+
+       memcpy(data, EITdata, 12);
        __u32 *p = (__u32*)(EITdata+12);
        while(tmp>0)
        {
@@ -77,27 +72,14 @@ const eit_event_struct* eventData::get() const
                        descriptors.find(*p++);
                if ( it != descriptors.end() )
                {
-                       d.push_back(it->second.second);
-                       bytes += it->second.second[1];
+                       int b = it->second.second[1]+2;
+                       memcpy(data+pos, it->second.second, b );
+                       pos += b;
                }
-               bytes += 2; // descr_type, descr_len
                tmp-=4;
        }
 
-// copy eit_event header to buffer
-       data = new __u8[bytes];
-       memcpy(data, EITdata, 12);
-
-       tmp=12;
-// copy all descriptors to buffer
-       for(std::list<__u8*>::iterator it(d.begin()); it != d.end(); ++it)
-       {
-               int b=(*it)[1]+2;
-               memcpy(data+tmp, *it, b);
-               tmp+=b;
-       }
-
-       return (eit_event_struct*)data;
+       return (const eit_event_struct*)data;
 }
 
 eventData::~eventData()
@@ -116,8 +98,8 @@ eventData::~eventData()
                                descriptorPair &p = it->second;
                                if (!--p.first) // no more used descriptor
                                {
-                                       CacheSize -= p.second[1]+2;
-                                       delete [] p.second;     // free descriptor memory
+                                       CacheSize -= it->second.second[1];
+                                       delete [] it->second.second;    // free descriptor memory
                                        descriptors.erase(it);  // remove entry from descriptor map
                                }
                        }
@@ -206,9 +188,10 @@ void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
 {
        if ( chan )
        {
-               eDebug("[eEPGCache] add channel %p", chan);
+//             eDebug("[eEPGCache] add channel %p", chan);
                channel_data *data = new channel_data(this);
                data->channel = chan;
+               data->prevChannelState = -1;
                singleLock s(channel_map_lock);
                m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
                chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
@@ -231,7 +214,7 @@ void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
                else
                {
                        ePtr<iDVBDemux> demux;
-                       if ( data.channel->getDemux(demux) )
+                       if ( data.channel->getDemux(demux, 0) )
                        {
                                eDebug("[eEPGCache] no demux!!");
                                return;
@@ -274,31 +257,31 @@ void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
        {
                int state=0;
                chan->getState(state);
-               switch (state)
+               if ( it->second->prevChannelState != state )
                {
-                       case iDVBChannel::state_idle:
-                               break;
-                       case iDVBChannel::state_tuning:
-                               break;
-                       case iDVBChannel::state_unavailable:
-                               break;
-                       case iDVBChannel::state_ok:
-                       {
-                               eDebug("[eEPGCache] channel %p running", chan);
-                               DVBChannelRunning(chan);
-                               break;
-                       }
-                       case iDVBChannel::state_release:
+                       switch (state)
                        {
-                               eDebug("[eEPGCache] remove channel %p", chan);
-                               messages.send(Message(Message::leaveChannel, chan));
-                               while(!it->second->can_delete)
-                                       usleep(1000);
-                               delete it->second;
-                               m_knownChannels.erase(it);
-                               // -> gotMessage -> abortEPG
-                               break;
+                               case iDVBChannel::state_ok:
+                               {
+                                       eDebug("[eEPGCache] channel %p running", chan);
+                                       DVBChannelRunning(chan);
+                                       break;
+                               }
+                               case iDVBChannel::state_release:
+                               {
+                                       eDebug("[eEPGCache] remove channel %p", chan);
+                                       messages.send(Message(Message::leaveChannel, chan));
+                                       while(!it->second->can_delete)
+                                               usleep(1000);
+                                       delete it->second;
+                                       m_knownChannels.erase(it);
+                                       // -> gotMessage -> abortEPG
+                                       break;
+                               }
+                               default: // ignore all other events
+                                       return;
                        }
+                       it->second->prevChannelState = state;
                }
        }
 }
@@ -597,70 +580,6 @@ eEPGCache::~eEPGCache()
                        delete It->second;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, const eventData *&result )
-{
-       singleLock s(cache_lock);
-       uniqueEPGKey key( service );
-
-       eventCache::iterator It = eventDB.find( key );
-       if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
-       {
-               eventMap::iterator i( It->second.first.find( event_id ));
-               if ( i != It->second.first.end() )
-               {
-                       result = i->second;
-                       return 0;
-               }
-               else
-               {
-                       result = 0;
-                       eDebug("event %04x not found in epgcache", event_id);
-               }
-       }
-       return -1;
-}
-
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, const eventData *&result )
-// if t == 0 we search the current event...
-{
-       singleLock s(cache_lock);
-       uniqueEPGKey key(service);
-
-       // check if EPG for this service is ready...
-       eventCache::iterator It = eventDB.find( key );
-       if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached ?
-       {
-               if (!t)
-                       t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
-
-               timeMap::iterator i = It->second.second.lower_bound(t);
-               if ( i != It->second.second.end() )
-               {
-                       i--;
-                       if ( i != It->second.second.end() )
-                       {
-                               if ( t <= i->first+i->second->getDuration() )
-                               {
-                                       result = i->second;
-                                       return 0;
-                               }
-                       }
-               }
-
-               for ( eventMap::iterator i( It->second.first.begin() ); i != It->second.first.end(); i++)
-               {
-                       int duration = i->second->getDuration();
-                       time_t begTime = i->second->getStartTime();
-                       if ( t >= begTime && t <= begTime+duration) // then we have found
-                       {
-                               result = i->second;
-                               return 0;
-                       }
-               }
-       }
-       return -1;
-}
-
 void eEPGCache::gotMessage( const Message &msg )
 {
        switch (msg.type)
@@ -733,7 +652,7 @@ void eEPGCache::load()
                {
                        char text1[13];
                        fread( text1, 13, 1, f);
-                       if ( !strncmp( text1, "ENIGMA_EPG_V2", 13) )
+                       if ( !strncmp( text1, "ENIGMA_EPG_V4", 13) )
                        {
                                fread( &size, sizeof(int), 1, f);
                                while(size--)
@@ -746,11 +665,11 @@ void eEPGCache::load()
                                        fread( &size, sizeof(int), 1, f);
                                        while(size--)
                                        {
-                                               int len=0;
-                                               int type=0;
+                                               __u8 len=0;
+                                               __u8 type=0;
                                                eventData *event=0;
-                                               fread( &type, sizeof(int), 1, f);
-                                               fread( &len, sizeof(int), 1, f);
+                                               fread( &type, sizeof(__u8), 1, f);
+                                               fread( &len, sizeof(__u8), 1, f);
                                                event = new eventData(0, len, type);
                                                event->EITdata = new __u8[len];
                                                eventData::CacheSize+=len;
@@ -799,7 +718,7 @@ void eEPGCache::save()
        int cnt=0;
        if ( f )
        {
-               const char *text = "ENIGMA_EPG_V2";
+               const char *text = "ENIGMA_EPG_V4";
                fwrite( text, 13, 1, f );
                int size = eventDB.size();
                fwrite( &size, sizeof(int), 1, f );
@@ -811,9 +730,9 @@ void eEPGCache::save()
                        fwrite( &size, sizeof(int), 1, f);
                        for (timeMap::iterator time_it(timemap.begin()); time_it != timemap.end(); ++time_it)
                        {
-                               int len = time_it->second->ByteSize;
-                               fwrite( &time_it->second->type, sizeof(int), 1, f );
-                               fwrite( &len, sizeof(int), 1, f);
+                               __u8 len = time_it->second->ByteSize;
+                               fwrite( &time_it->second->type, sizeof(__u8), 1, f );
+                               fwrite( &len, sizeof(__u8), 1, f);
                                fwrite( time_it->second->EITdata, len, 1, f);
                                ++cnt;
                        }
@@ -835,14 +754,6 @@ void eEPGCache::save()
 #endif
 }
 
-RESULT eEPGCache::getInstance(ePtr<eEPGCache> &ptr)
-{
-       ptr = instance;
-       if (!ptr)
-               return -1;
-       return 0;
-}
-
 eEPGCache::channel_data::channel_data(eEPGCache *ml)
        :cache(ml)
        ,abortTimer(ml), zapTimer(ml)
@@ -1091,3 +1002,208 @@ void eEPGCache::channel_data::readData( const __u8 *data)
                }
        }
 }
+
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eventData *&result )
+// if t == 0 we search the current event...
+{
+       singleLock s(cache_lock);
+       uniqueEPGKey key(service);
+
+       // check if EPG for this service is ready...
+       eventCache::iterator It = eventDB.find( key );
+       if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached ?
+       {
+               if (!t)
+                       t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
+               timeMap::iterator i = It->second.second.lower_bound(t);  // find > or equal
+               if ( i != It->second.second.end() )
+               {
+                       if ( i->second->getStartTime() != t )
+                       {
+                               timeMap::iterator x = i;
+                               --x;
+                               if ( x != It->second.second.end() )
+                               {
+                                       time_t start_time = x->second->getStartTime();
+                                       if (t < start_time)
+                                               return -1;
+                                       if (t > (start_time+x->second->getDuration()))
+                                               return -1;
+                                       i = x;
+                               }
+                               else
+                                       return -1;
+                       }
+                       result = i->second;
+                       return 0;
+               }
+       }
+       return -1;
+}
+
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eit_event_struct *&result )
+{
+       singleLock s(cache_lock);
+       const eventData *data=0;
+       RESULT ret = lookupEventTime(service, t, data);
+       if ( !ret && data )
+               result = data->get();
+       return ret;
+}
+
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, Event *& result )
+{
+       singleLock s(cache_lock);
+       const eventData *data=0;
+       RESULT ret = lookupEventTime(service, t, data);
+       if ( !ret && data )
+               result = new Event((uint8_t*)data->get());
+       return ret;
+}
+
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, ePtr<eServiceEvent> &result )
+{
+       singleLock s(cache_lock);
+       const eventData *data=0;
+       RESULT ret = lookupEventTime(service, t, data);
+       if ( !ret && data )
+       {
+               Event ev((uint8_t*)data->get());
+               result = new eServiceEvent();
+               const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+               ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
+       }
+       return ret;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eventData *&result )
+{
+       singleLock s(cache_lock);
+       uniqueEPGKey key( service );
+
+       eventCache::iterator It = eventDB.find( key );
+       if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
+       {
+               eventMap::iterator i( It->second.first.find( event_id ));
+               if ( i != It->second.first.end() )
+               {
+                       result = i->second;
+                       return 0;
+               }
+               else
+               {
+                       result = 0;
+                       eDebug("event %04x not found in epgcache", event_id);
+               }
+       }
+       return -1;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&result)
+{
+       singleLock s(cache_lock);
+       const eventData *data=0;
+       RESULT ret = lookupEventId(service, event_id, data);
+       if ( !ret && data )
+               result = data->get();
+       return ret;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, Event *& result)
+{
+       singleLock s(cache_lock);
+       const eventData *data=0;
+       RESULT ret = lookupEventId(service, event_id, data);
+       if ( !ret && data )
+               result = new Event((uint8_t*)data->get());
+       return ret;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &result)
+{
+       singleLock s(cache_lock);
+       const eventData *data=0;
+       RESULT ret = lookupEventId(service, event_id, data);
+       if ( !ret && data )
+       {
+               Event ev((uint8_t*)data->get());
+               result = new eServiceEvent();
+               const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+               ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
+       }
+       return ret;
+}
+
+RESULT eEPGCache::startTimeQuery(const eServiceReference &service, time_t begin, int minutes)
+{
+       eventCache::iterator It = eventDB.find( service );
+       if ( It != eventDB.end() && It->second.second.size() )
+       {
+               m_timemap_end = minutes != -1 ? It->second.second.upper_bound(begin+minutes*60) : It->second.second.end();
+               if ( begin != -1 )
+               {
+                       m_timemap_cursor = It->second.second.lower_bound(begin);
+                       if ( m_timemap_cursor != It->second.second.end() )
+                       {
+                               if ( m_timemap_cursor->second->getStartTime() != begin )
+                               {
+                                       timeMap::iterator x = m_timemap_cursor;
+                                       --x;
+                                       if ( x != It->second.second.end() )
+                                       {
+                                               time_t start_time = x->second->getStartTime();
+                                               if ( begin > start_time && begin < (start_time+x->second->getDuration()))
+                                                       m_timemap_cursor = x;
+                                       }
+                               }
+                       }
+               }
+               else
+                       m_timemap_cursor = It->second.second.begin();
+               const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+               currentQueryTsidOnid = (ref.getTransportStreamID().get()<<16) | ref.getOriginalNetworkID().get();
+               return 0;
+       }
+       return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(const eventData *& result)
+{
+       if ( m_timemap_cursor != m_timemap_end )
+       {
+               result = m_timemap_cursor++->second;
+               return 0;
+       }
+       return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(const eit_event_struct *&result)
+{
+       if ( m_timemap_cursor != m_timemap_end )
+       {
+               result = m_timemap_cursor++->second->get();
+               return 0;
+       }
+       return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(Event *&result)
+{
+       if ( m_timemap_cursor != m_timemap_end )
+       {
+               result = new Event((uint8_t*)m_timemap_cursor++->second->get());
+               return 0;
+       }
+       return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(ePtr<eServiceEvent> &result)
+{
+       if ( m_timemap_cursor != m_timemap_end )
+       {
+               Event ev((uint8_t*)m_timemap_cursor++->second->get());
+               result = new eServiceEvent();
+               return result->parseFrom(&ev, currentQueryTsidOnid);
+       }
+       return -1;
+}