6 #include <ext/hash_map>
8 // check if gcc version >= 3.4
9 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ == 4 )
11 #include <ext/stl_hash_fun.h>
15 #include <lib/dvb/eit.h>
16 #include <lib/dvb/lowlevel/eit.h>
17 #include <lib/dvb/idvb.h>
18 #include <lib/dvb/demux.h>
19 #include <lib/dvb/dvbtime.h>
20 #include <lib/base/ebase.h>
21 #include <lib/base/thread.h>
22 #include <lib/base/message.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 eServiceReferenceDVB &ref )
37 :sid( ref.type != eServiceReference::idInvalid ? ref.getServiceID().get() : -1 )
38 ,onid( ref.type != eServiceReference::idInvalid ? ref.getOriginalNetworkID().get() : -1 )
39 ,tsid( ref.type != eServiceReference::idInvalid ? 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
78 #define tmpMap std::map<uniqueEPGKey, std::pair<time_t, int> >
79 #define updateMap std::map<eDVBChannelID, time_t>
81 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
82 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, __gnu_cxx::hash<uniqueEPGKey>, uniqueEPGKey::equal>
84 #else // for older gcc use following
85 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, std::hash<uniqueEPGKey>, uniqueEPGKey::equal >
89 template<> struct hash<uniqueEPGKey>
91 inline size_t operator()( const uniqueEPGKey &x) const
93 return (x.tsid << 16) | x.onid;
100 friend class eEPGCache;
106 static int CacheSize;
107 eventData(const eit_event_struct* e, int size, int type)
108 :ByteSize(size), type(type)
111 EITdata = new __u8[size];
113 memcpy(EITdata, (__u8*) e, size);
120 operator const eit_event_struct*() const
122 return (const eit_event_struct*) EITdata;
124 const eit_event_struct* get() const
126 return (const eit_event_struct*) EITdata;
130 return HILO( ((const eit_event_struct*) EITdata)->event_id );
132 time_t getStartTime()
135 EITdata[2], EITdata[3],
136 EITdata[4], EITdata[5], EITdata[6]);
140 class eEPGCache: public eMainloop, private eThread, public Object
142 DECLARE_REF(eEPGCache)
143 struct channel_data: public Object
145 channel_data(eEPGCache*);
147 eTimer abortTimer, zapTimer;
148 __u8 state, isRunning, haveData, can_delete;
149 ePtr<eDVBChannel> channel;
150 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
151 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
152 void readData(const __u8 *data);
157 void abortNonAvail();
160 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
176 iDVBChannel *channel;
177 uniqueEPGKey service;
187 Message(int type, bool b)
188 :type(type), avail(b) {}
189 Message(int type, iDVBChannel *channel, int err=0)
190 :type(type), channel(channel), err(err) {}
191 Message(int type, const eServiceReferenceDVB& service, int err=0)
192 :type(type), service(service), err(err) {}
193 Message(int type, time_t time)
194 :type(type), time(time) {}
196 eFixedMessagePump<Message> messages;
198 friend class channel_data;
199 static eEPGCache *instance;
202 std::map<iDVBChannel*, channel_data*> m_knownChannels;
203 ePtr<eConnection> m_chanAddedConn;
206 updateMap channelLastUpdated;
207 static pthread_mutex_t cache_lock, channel_map_lock;
209 void thread(); // thread function
211 // called from epgcache thread
214 int sectionRead(const __u8 *data, int source, channel_data *channel);
215 void gotMessage(const Message &message);
216 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
219 // called from main thread
221 void DVBChannelAdded(eDVBChannel*);
222 void DVBChannelStateChanged(iDVBChannel*);
223 void DVBChannelRunning(iDVBChannel *);
225 static RESULT getInstance(ePtr<eEPGCache> &ptr);
229 // called from main thread
231 inline void Unlock();
232 Event *lookupEvent(const eServiceReferenceDVB &service, int event_id, bool plain=false );
233 Event *lookupEvent(const eServiceReferenceDVB &service, time_t=0, bool plain=false );
234 const eventMap* getEventMap(const eServiceReferenceDVB &service);
235 const timeMap* getTimeMap(const eServiceReferenceDVB &service);
238 TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
240 inline const eventMap* eEPGCache::getEventMap(const eServiceReferenceDVB &service)
242 eventCache::iterator It = eventDB.find( service );
243 if ( It != eventDB.end() && It->second.first.size() )
244 return &(It->second.first);
249 inline const timeMap* eEPGCache::getTimeMap(const eServiceReferenceDVB &service)
251 eventCache::iterator It = eventDB.find( service );
252 if ( It != eventDB.end() && It->second.second.size() )
253 return &(It->second.second);
258 inline void eEPGCache::Lock()
260 pthread_mutex_lock(&cache_lock);
263 inline void eEPGCache::Unlock()
265 pthread_mutex_unlock(&cache_lock);