remove older no more needed events from epgcache
[enigma2.git] / lib / dvb / epgcache.h
index bc25692e0da6171e4c2caed8993ed84b812be9b4..749854a5f8191ac3e0a741b24b2a484d28aeaf97 100644 (file)
@@ -1,17 +1,16 @@
 #ifndef __epgcache_h_
 #define __epgcache_h_
 
+#define ENABLE_PRIVATE_EPG 1
+#define NEED_DEMUX_WORKAROUND 1
+
 #ifndef SWIG
 
 #include <vector>
 #include <list>
 #include <ext/hash_map>
+#include <ext/hash_set>
 
-// check if gcc version >= 3.4
-#if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ == 4 )
-#else
-#include <ext/stl_hash_fun.h>
-#endif
 #include <errno.h>
 
 #include <lib/dvb/eit.h>
@@ -22,6 +21,8 @@
 #include <lib/base/ebase.h>
 #include <lib/base/thread.h>
 #include <lib/base/message.h>
+#include <lib/service/event.h>
+#include <lib/python/python.h>
 
 #define CLEAN_INTERVAL 60000    //  1 min
 #define UPDATE_INTERVAL 3600000  // 60 min
 
 class eventData;
 class eServiceReferenceDVB;
+class eDVBServicePMTHandler;
 
 struct uniqueEPGKey
 {
        int sid, onid, tsid;
-       uniqueEPGKey( const eServiceReferenceDVB &ref )
-               :sid( ref.type != eServiceReference::idInvalid ? ref.getServiceID().get() : -1 )
-               ,onid( ref.type != eServiceReference::idInvalid ? ref.getOriginalNetworkID().get() : -1 )
-               ,tsid( ref.type != eServiceReference::idInvalid ? ref.getTransportStreamID().get() : -1 )
+       uniqueEPGKey( const eServiceReference &ref )
+               :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
+               ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
+               ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
        {
        }
        uniqueEPGKey()
@@ -75,101 +77,131 @@ struct uniqueEPGKey
 //timeMap is sorted by beginTime
 #define timeMap std::map<time_t, eventData*>
 
-#define tmpMap std::map<uniqueEPGKey, std::pair<time_t, int> >
+#define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
+#define updateMap std::map<eDVBChannelID, time_t>
 
-#if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 )  // check if gcc version >= 3.1
-       #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, __gnu_cxx::hash<uniqueEPGKey>, uniqueEPGKey::equal>
-       #define updateMap __gnu_cxx::hash_map<uniqueEPGKey, time_t, __gnu_cxx::hash<uniqueEPGKey>, uniqueEPGKey::equal >
-       namespace __gnu_cxx
-#else // for older gcc use following
-       #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, std::hash<uniqueEPGKey>, uniqueEPGKey::equal >
-       #define updateMap std::hash_map<uniqueEPGKey, time_t, std::hash<uniqueEPGKey>, uniqueEPGKey::equal >
-       namespace std
-#endif
-{
-template<> struct hash<uniqueEPGKey>
+struct hash_uniqueEPGKey
 {
        inline size_t operator()( const uniqueEPGKey &x) const
        {
-               int v=(x.onid^x.sid);
-               v^=v>>8;
-               return v&0xFF;
+               return (x.onid << 16) | x.tsid;
        }
 };
-}
+
+#define tidMap std::set<__u32>
+#if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 )  // check if gcc version >= 3.1
+       #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
+       #ifdef ENABLE_PRIVATE_EPG
+               #define contentTimeMap __gnu_cxx::hash_map<time_t, std::pair<time_t, __u16> >
+               #define contentMap __gnu_cxx::hash_map<int, contentTimeMap >
+               #define contentMaps __gnu_cxx::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal >
+       #endif
+#else // for older gcc use following
+       #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
+       #ifdef ENABLE_PRIVATE_EPG
+               #define contentTimeMap std::hash_map<time_t, std::pair<time_t, __u16> >
+               #define contentMap std::hash_map<int, contentTimeMap >
+               #define contentMaps std::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal>
+       #endif
+#endif
+
+#define descriptorPair std::pair<int,__u8*>
+#define descriptorMap std::map<__u32, descriptorPair >
 
 class eventData
 {
-       friend class eEPGCache;
+       friend class eEPGCache;
 private:
        __u8* EITdata;
-       int ByteSize;
-public:
-       int type;
+       __u8 ByteSize;
+       __u8 type;
+       static descriptorMap descriptors;
+       static __u8 data[4108];
        static int CacheSize;
-       eventData(const eit_event_struct* e, int size, int type)
-       :ByteSize(size), type(type)
-       {
-               CacheSize+=size;
-               EITdata = new __u8[size];
-               if (e)
-                       memcpy(EITdata, (__u8*) e, size);
-       }
-       ~eventData()
-       {
-               CacheSize-=ByteSize;
-               delete [] EITdata;
-       }
+       static void load(FILE *);
+       static void save(FILE *);
+public:
+       eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
+       ~eventData();
+       const eit_event_struct* get() const;
        operator const eit_event_struct*() const
        {
-               return (const eit_event_struct*) EITdata;
-       }
-       const eit_event_struct* get() const
-       {
-               return (const eit_event_struct*) EITdata;
+               return get();
        }
        int getEventID()
        {
-               return HILO( ((const eit_event_struct*) EITdata)->event_id );
+               return (EITdata[0] << 8) | EITdata[1];
        }
        time_t getStartTime()
        {
-               return parseDVBtime(
-                       EITdata[2], EITdata[3],
-                       EITdata[4], EITdata[5], EITdata[6]);
+               return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
+       }
+       int getDuration()
+       {
+               return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
        }
 };
-#else
-       #include <lib/base/smartptr.h>
 #endif
 
 class eEPGCache: public eMainloop, private eThread, public Object
 {
+#ifndef SWIG
        DECLARE_REF(eEPGCache)
+       struct channel_data: public Object
+       {
+               channel_data(eEPGCache*);
+               eEPGCache *cache;
+               eTimer abortTimer, zapTimer;
+               int prevChannelState;
+               __u8 state, isRunning, haveData, can_delete;
+               ePtr<eDVBChannel> channel;
+               ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
+               ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
+               tidMap seenSections[3], calcedSections[3];
+#ifdef ENABLE_PRIVATE_EPG
+               eTimer startPrivateTimer;
+               int m_PrevVersion;
+               int m_PrivatePid;
+               uniqueEPGKey m_PrivateService;
+               ePtr<eConnection> m_PrivateConn;
+               ePtr<iDVBSectionReader> m_PrivateReader;
+               std::set<__u8> seenPrivateSections;
+               void readPrivateData(const __u8 *data);
+               void startPrivateReader();
+#endif
+               void readData(const __u8 *data);
+               void startChannel();
+               void startEPG();
+               bool finishEPG();
+               void abortEPG();
+               void abortNonAvail();
+       };
+       void FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
 public:
-#ifndef SWIG
-       enum {NOWNEXT, SCHEDULE, SCHEDULE_OTHER};
+       enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
        struct Message
        {
                enum
                {
                        flush,
-                       startService,
+                       startChannel,
                        leaveChannel,
                        pause,
                        restart,
                        updated,
                        isavail,
                        quit,
-                       timeChanged,
-                       leaveChannelFinished
+                       got_private_pid,
+                       timeChanged
                };
                int type;
+               iDVBChannel *channel;
                uniqueEPGKey service;
                union {
                        int err;
                        time_t time;
                        bool avail;
+                       int pid;
                };
                Message()
                        :type(0), time(0) {}
@@ -177,124 +209,113 @@ public:
                        :type(type) {}
                Message(int type, bool b)
                        :type(type), avail(b) {}
-               Message(int type, const eServiceReferenceDVB& service, int err=0)
+               Message(int type, iDVBChannel *channel, int err=0)
+                       :type(type), channel(channel), err(err) {}
+               Message(int type, const eServiceReference& service, int err=0)
                        :type(type), service(service), err(err) {}
                Message(int type, time_t time)
                        :type(type), time(time) {}
        };
        eFixedMessagePump<Message> messages;
-       eFixedMessagePump<Message> back_messages;
 private:
-       static pthread_mutex_t cache_lock;
-       eServiceReferenceDVB next_service;
-       uniqueEPGKey current_service;
-       int paused;
-
-       int state;
-       __u8 isRunning, haveData;
-
-       ePtr<iDVBChannel> m_currentChannel;
-       ePtr<eConnection> m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
-       ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
-
-       int sectionRead(const __u8 *data, int source);
-       void readNowNextData(const __u8 *data);
-       void readScheduleData(const __u8 *data);
-       void readScheduleOtherData(const __u8 *data);
-
+       friend class channel_data;
        static eEPGCache *instance;
 
+       eTimer cleanTimer;
+       std::map<iDVBChannel*, channel_data*> m_knownChannels;
+       ePtr<eConnection> m_chanAddedConn;
+
        eventCache eventDB;
-       updateMap serviceLastUpdated;
-       tmpMap temp;
-       eTimer CleanTimer;
-       eTimer zapTimer;
-       eTimer abortTimer;
-       bool finishEPG();
-       void abortNonAvail();
-       void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
-       void startEPG();
+       updateMap channelLastUpdated;
+       static pthread_mutex_t cache_lock, channel_map_lock;
 
-       void changedService(const uniqueEPGKey &);
-       void abortEPG();
+#ifdef ENABLE_PRIVATE_EPG
+       contentMaps content_time_tables;
+#endif
 
-       void cleanLoop();
-       void pauseEPG();
-       void restartEPG();
-       void thread();
-       void gotMessage(const Message &message);
-       void gotBackMessage(const Message &message);
-       void timeUpdated();
+       void thread();  // thread function
+
+// called from epgcache thread
        void save();
        void load();
-       void leaveChannel(iDVBChannel *);
+#ifdef ENABLE_PRIVATE_EPG
+       void privateSectionRead(const uniqueEPGKey &, const __u8 *);
 #endif
+       void sectionRead(const __u8 *data, int source, channel_data *channel);
+       void gotMessage(const Message &message);
+       void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
+       void cleanLoop();
+
+// called from main thread
+       void timeUpdated();
+       void DVBChannelAdded(eDVBChannel*);
+       void DVBChannelStateChanged(iDVBChannel*);
+       void DVBChannelRunning(iDVBChannel *);
+
+       timeMap::iterator m_timemap_cursor, m_timemap_end;
+       int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
+#else
+       eEPGCache();
+       ~eEPGCache();
+#endif // SWIG
 public:
-       static RESULT getInstance(ePtr<eEPGCache> &ptr);
-       // called from other thread context !!
-       void startCache(const eServiceReferenceDVB &);
-       Event *lookupEvent(const eServiceReferenceDVB &service, int event_id, bool plain=false );
-       Event *lookupEvent(const eServiceReferenceDVB &service, time_t=0, bool plain=false );
+       static eEPGCache *getInstance() { return instance; }
 #ifndef SWIG
        eEPGCache();
        ~eEPGCache();
+#endif
 
+       // called from main thread
        inline void Lock();
        inline void Unlock();
+#ifdef ENABLE_PRIVATE_EPG
+       void PMTready(eDVBServicePMTHandler *pmthandler);
+#else
+       void PMTready(eDVBServicePMTHandler *pmthandler) {}
+#endif
 
-       const eventMap* getEventMap(const eServiceReferenceDVB &service);
-       const timeMap* getTimeMap(const eServiceReferenceDVB &service);
-       tmpMap* getUpdatedMap() { return &temp; }
+       // at moment just for one service..
+       RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
 
-       Signal1<void, bool> EPGAvail;
-       Signal0<void> EPGUpdated;
+#ifndef SWIG
+       // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
+       // but its faster in use... its not allowed to delete this pointers via delete or free..
+       SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
+       SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT, int direction=0);
+       SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
+
+       // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
+       // its not allowed to delete this pointers via delete or free..
+       RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
+       RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
+       RESULT getNextTimeEntry(const eit_event_struct *&);
+
+       // Event's are parsed epg events.. it's safe to use them after cache unlock
+       // after use this Events must be deleted (memleaks)
+       RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
+       RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
+       RESULT getNextTimeEntry(Event *&);
 #endif
+       enum {
+               SIMILAR_BROADCASTINGS_SEARCH,
+               EXAKT_TITLE_SEARCH,
+               PARTIAL_TITLE_SEARCH
+       };
+       enum {
+               CASE_CHECK,
+               NO_CASE_CHECK
+       };
+       PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
+       PyObject *search(PyObject *);
+
+       // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
+       // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
+       SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
+       SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
+       SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
 };
 
-TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
-
-inline const eventMap* eEPGCache::getEventMap(const eServiceReferenceDVB &service)
-{
-       eventCache::iterator It = eventDB.find( service );
-       if ( It != eventDB.end() && It->second.first.size() )
-               return &(It->second.first);
-       else
-               return 0;
-}
-
-inline const timeMap* eEPGCache::getTimeMap(const eServiceReferenceDVB &service)
-{
-       eventCache::iterator It = eventDB.find( service );
-       if ( It != eventDB.end() && It->second.second.size() )
-               return &(It->second.second);
-       else
-               return 0;
-}
-
-inline void eEPGCache::readNowNextData( const __u8 *data)
-{
-       if ( !data || state == 2 )
-               m_NowNextReader->stop();
-       else
-               sectionRead(data, eEPGCache::NOWNEXT);
-}
-
-inline void eEPGCache::readScheduleData(const __u8 *data)
-{
-       if ( !data || state == 2 )
-               m_ScheduleReader->stop();
-       else
-               sectionRead(data, eEPGCache::SCHEDULE);
-}
-
-inline void eEPGCache::readScheduleOtherData(const __u8 *data)
-{
-       if ( !data || state == 2 )
-               m_ScheduleOtherReader->stop();
-       else
-               sectionRead(data, eEPGCache::SCHEDULE_OTHER);
-}
-
+#ifndef SWIG
 inline void eEPGCache::Lock()
 {
        pthread_mutex_lock(&cache_lock);
@@ -304,5 +325,6 @@ inline void eEPGCache::Unlock()
 {
        pthread_mutex_unlock(&cache_lock);
 }
+#endif
 
 #endif