8 #include <ext/hash_map>
9 #include <ext/hash_set>
13 #include <lib/dvb/eit.h>
14 #include <lib/dvb/lowlevel/eit.h>
15 #include <lib/dvb/idvb.h>
16 #include <lib/dvb/demux.h>
17 #include <lib/dvb/dvbtime.h>
18 #include <lib/base/ebase.h>
19 #include <lib/base/thread.h>
20 #include <lib/base/message.h>
21 #include <lib/service/event.h>
22 #include <lib/python/python.h>
24 #define CLEAN_INTERVAL 60000 // 1 min
25 #define UPDATE_INTERVAL 3600000 // 60 min
26 #define ZAP_DELAY 2000 // 2 sek
28 #define HILO(x) (x##_hi << 8 | x##_lo)
31 class eServiceReferenceDVB;
36 uniqueEPGKey( const eServiceReference &ref )
37 :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
38 ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
39 ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
43 :sid(-1), onid(-1), tsid(-1)
46 uniqueEPGKey( int sid, int onid, int tsid )
47 :sid(sid), onid(onid), tsid(tsid)
50 bool operator <(const uniqueEPGKey &a) const
52 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
56 return !(sid == -1 && onid == -1 && tsid == -1);
58 bool operator==(const uniqueEPGKey &a) const
60 return !memcmp( &sid, &a.sid, sizeof(int)*3);
64 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
66 return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
71 //eventMap is sorted by event_id
72 #define eventMap std::map<__u16, eventData*>
73 //timeMap is sorted by beginTime
74 #define timeMap std::map<time_t, eventData*>
76 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
77 #define updateMap std::map<eDVBChannelID, time_t>
79 struct hash_uniqueEPGKey
81 inline size_t operator()( const uniqueEPGKey &x) const
83 return (x.onid << 16) | x.tsid;
87 #define tidMap std::set<__u32>
88 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
89 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
90 #else // for older gcc use following
91 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
94 #define descriptorPair std::pair<int,__u8*>
95 #define descriptorMap std::map<__u32, descriptorPair >
99 friend class eEPGCache;
104 static descriptorMap descriptors;
105 static __u8 data[4108];
106 static int CacheSize;
107 static void load(FILE *);
108 static void save(FILE *);
110 eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
112 const eit_event_struct* get() const;
113 operator const eit_event_struct*() const
119 return (EITdata[0] << 8) | EITdata[1];
121 time_t getStartTime()
123 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
127 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
132 class eEPGCache: public eMainloop, private eThread, public Object
135 DECLARE_REF(eEPGCache)
136 struct channel_data: public Object
138 channel_data(eEPGCache*);
140 eTimer abortTimer, zapTimer;
141 int prevChannelState;
142 __u8 state, isRunning, haveData, can_delete;
143 ePtr<eDVBChannel> channel;
144 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
145 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
146 tidMap seenSections[3], calcedSections[3];
147 void readData(const __u8 *data);
152 void abortNonAvail();
155 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
171 iDVBChannel *channel;
172 uniqueEPGKey service;
182 Message(int type, bool b)
183 :type(type), avail(b) {}
184 Message(int type, iDVBChannel *channel, int err=0)
185 :type(type), channel(channel), err(err) {}
186 Message(int type, const eServiceReference& service, int err=0)
187 :type(type), service(service), err(err) {}
188 Message(int type, time_t time)
189 :type(type), time(time) {}
191 eFixedMessagePump<Message> messages;
193 friend class channel_data;
194 static eEPGCache *instance;
197 std::map<iDVBChannel*, channel_data*> m_knownChannels;
198 ePtr<eConnection> m_chanAddedConn;
201 updateMap channelLastUpdated;
202 static pthread_mutex_t cache_lock, channel_map_lock;
204 void thread(); // thread function
206 // called from epgcache thread
209 void sectionRead(const __u8 *data, int source, channel_data *channel);
210 void gotMessage(const Message &message);
211 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
214 // called from main thread
216 void DVBChannelAdded(eDVBChannel*);
217 void DVBChannelStateChanged(iDVBChannel*);
218 void DVBChannelRunning(iDVBChannel *);
220 timeMap::iterator m_timemap_cursor, m_timemap_end;
221 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
227 static eEPGCache *getInstance() { return instance; }
233 // called from main thread
235 inline void Unlock();
237 // at moment just for one service..
238 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
241 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
242 // but its faster in use... its not allowed to delete this pointers via delete or free..
243 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
244 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT, int direction=0);
245 SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
247 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
248 // its not allowed to delete this pointers via delete or free..
249 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
250 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
251 RESULT getNextTimeEntry(const eit_event_struct *&);
253 // Event's are parsed epg events.. it's safe to use them after cache unlock
254 // after use this Events must be deleted (memleaks)
255 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
256 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
257 RESULT getNextTimeEntry(Event *&);
259 PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
261 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
262 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
263 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
264 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
265 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
269 inline void eEPGCache::Lock()
271 pthread_mutex_lock(&cache_lock);
274 inline void eEPGCache::Unlock()
276 pthread_mutex_unlock(&cache_lock);