fix channel state handling
[enigma2.git] / lib / dvb / epgcache.cpp
index f8ffb97..6d81119 100644 (file)
@@ -15,7 +15,7 @@ __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;
@@ -191,6 +191,7 @@ void eEPGCache::DVBChannelAdded(eDVBChannel *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);
@@ -256,25 +257,31 @@ void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
        {
                int state=0;
                chan->getState(state);
-               switch (state)
+               if ( it->second->prevChannelState != state )
                {
-                       case iDVBChannel::state_ok:
+                       switch (state)
                        {
-                               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;
+                               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;
                }
        }
 }
@@ -747,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)
@@ -1004,7 +1003,7 @@ void eEPGCache::channel_data::readData( const __u8 *data)
        }
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, const eventData *&result )
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eventData *&result )
 // if t == 0 we search the current event...
 {
        singleLock s(cache_lock);
@@ -1016,70 +1015,68 @@ RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, con
        {
                if (!t)
                        t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
-
-// TODO: optimize this.. why we here search first in timemap.. and then in eventmap??
-               timeMap::iterator i = It->second.second.lower_bound(t);
+               timeMap::iterator i = It->second.second.lower_bound(t);  // find > or equal
                if ( i != It->second.second.end() )
                {
-                       if ( i != It->second.second.end() )
+                       if ( i->second->getStartTime() != t )
                        {
-                               if ( t <= i->first+i->second->getDuration() )
+                               timeMap::iterator x = i;
+                               --x;
+                               if ( x != It->second.second.end() )
                                {
-                                       result = i->second;
-                                       return 0;
+                                       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;
                        }
-               }
-
-               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;
-                       }
+                       result = i->second;
+                       return 0;
                }
        }
        return -1;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, const eit_event_struct *&result )
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eit_event_struct *&result )
 {
        singleLock s(cache_lock);
        const eventData *data=0;
-       RESULT ret = lookupEvent(service, t, data);
+       RESULT ret = lookupEventTime(service, t, data);
        if ( !ret && data )
                result = data->get();
        return ret;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, Event *& result )
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, Event *& result )
 {
        singleLock s(cache_lock);
        const eventData *data=0;
-       RESULT ret = lookupEvent(service, t, data);
+       RESULT ret = lookupEventTime(service, t, data);
        if ( !ret && data )
                result = new Event((uint8_t*)data->get());
        return ret;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, ePtr<eServiceEvent> &result )
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, ePtr<eServiceEvent> &result )
 {
        singleLock s(cache_lock);
        const eventData *data=0;
-       RESULT ret = lookupEvent(service, t, data);
+       RESULT ret = lookupEventTime(service, t, data);
        if ( !ret && data )
        {
                Event ev((uint8_t*)data->get());
                result = new eServiceEvent();
-               ret = result->parseFrom(&ev);
+               const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+               ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
        }
        return ret;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, const eventData *&result )
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eventData *&result )
 {
        singleLock s(cache_lock);
        uniqueEPGKey key( service );
@@ -1102,41 +1099,42 @@ RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id,
        return -1;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, const eit_event_struct *&result)
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&result)
 {
        singleLock s(cache_lock);
        const eventData *data=0;
-       RESULT ret = lookupEvent(service, event_id, data);
+       RESULT ret = lookupEventId(service, event_id, data);
        if ( !ret && data )
                result = data->get();
        return ret;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& result)
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, Event *& result)
 {
        singleLock s(cache_lock);
        const eventData *data=0;
-       RESULT ret = lookupEvent(service, event_id, data);
+       RESULT ret = lookupEventId(service, event_id, data);
        if ( !ret && data )
                result = new Event((uint8_t*)data->get());
        return ret;
 }
 
-RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, ePtr<eServiceEvent> &result)
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &result)
 {
        singleLock s(cache_lock);
        const eventData *data=0;
-       RESULT ret = lookupEvent(service, event_id, data);
+       RESULT ret = lookupEventId(service, event_id, data);
        if ( !ret && data )
        {
                Event ev((uint8_t*)data->get());
                result = new eServiceEvent();
-               ret = result->parseFrom(&ev);
+               const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+               ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
        }
        return ret;
 }
 
-RESULT eEPGCache::startTimeQuery(const eServiceReferenceDVB &service, time_t begin, int minutes)
+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() )
@@ -1145,14 +1143,25 @@ RESULT eEPGCache::startTimeQuery(const eServiceReferenceDVB &service, time_t beg
                if ( begin != -1 )
                {
                        m_timemap_cursor = It->second.second.lower_bound(begin);
-                       if ( m_timemap_cursor != It->second.second.end() && m_timemap_cursor != It->second.second.begin() )
+                       if ( m_timemap_cursor != It->second.second.end() )
                        {
-                               timeMap::iterator it = m_timemap_cursor;
-                               --it;
-                               if ( (it->second->getStartTime() + it->second->getDuration()) > begin )
-                                       m_timemap_cursor = it;
+                               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;
@@ -1194,7 +1203,7 @@ RESULT eEPGCache::getNextTimeEntry(ePtr<eServiceEvent> &result)
        {
                Event ev((uint8_t*)m_timemap_cursor++->second->get());
                result = new eServiceEvent();
-               return result->parseFrom(&ev);
+               return result->parseFrom(&ev, currentQueryTsidOnid);
        }
        return -1;
 }