6 #include <ext/hash_map>
7 #include <ext/hash_set>
11 #include <lib/dvb/eit.h>
12 #include <lib/dvb/lowlevel/eit.h>
13 #include <lib/dvb/idvb.h>
14 #include <lib/dvb/demux.h>
15 #include <lib/dvb/dvbtime.h>
16 #include <lib/base/ebase.h>
17 #include <lib/base/thread.h>
18 #include <lib/base/message.h>
20 #define CLEAN_INTERVAL 60000 // 1 min
21 #define UPDATE_INTERVAL 3600000 // 60 min
22 #define ZAP_DELAY 2000 // 2 sek
24 #define HILO(x) (x##_hi << 8 | x##_lo)
27 class eServiceReferenceDVB;
32 uniqueEPGKey( const eServiceReferenceDVB &ref )
33 :sid( ref.type != eServiceReference::idInvalid ? ref.getServiceID().get() : -1 )
34 ,onid( ref.type != eServiceReference::idInvalid ? ref.getOriginalNetworkID().get() : -1 )
35 ,tsid( ref.type != eServiceReference::idInvalid ? ref.getTransportStreamID().get() : -1 )
39 :sid(-1), onid(-1), tsid(-1)
42 uniqueEPGKey( int sid, int onid, int tsid )
43 :sid(sid), onid(onid), tsid(tsid)
46 bool operator <(const uniqueEPGKey &a) const
48 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
52 return !(sid == -1 && onid == -1 && tsid == -1);
54 bool operator==(const uniqueEPGKey &a) const
56 return !memcmp( &sid, &a.sid, sizeof(int)*3);
60 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
62 return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
67 //eventMap is sorted by event_id
68 #define eventMap std::map<__u16, eventData*>
69 //timeMap is sorted by beginTime
70 #define timeMap std::map<time_t, eventData*>
72 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
73 #define updateMap std::map<eDVBChannelID, time_t>
77 inline size_t operator()( const __u32 &x) const
79 return (x >> 8)&0xFFFF;
85 inline size_t operator()(const __u32 &x, const __u32 &y) const
91 struct hash_uniqueEPGKey
93 inline size_t operator()( const uniqueEPGKey &x) const
95 return (x.onid << 16) | x.tsid;
99 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
100 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
101 #define tidMap __gnu_cxx::hash_set<__u32, hash_32, equal_32>
102 #else // for older gcc use following
103 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
104 #define tidMap std::hash_map<__u32, hash_32, equal_32>
109 friend class eEPGCache;
115 static int CacheSize;
116 eventData(const eit_event_struct* e, int size, int type)
117 :ByteSize(size), type(type)
120 EITdata = new __u8[size];
122 memcpy(EITdata, (__u8*) e, size);
129 operator const eit_event_struct*() const
131 return (const eit_event_struct*) EITdata;
133 const eit_event_struct* get() const
135 return (const eit_event_struct*) EITdata;
139 return HILO( ((const eit_event_struct*) EITdata)->event_id );
141 time_t getStartTime()
144 EITdata[2], EITdata[3],
145 EITdata[4], EITdata[5], EITdata[6]);
149 class eEPGCache: public eMainloop, private eThread, public Object
151 DECLARE_REF(eEPGCache)
152 struct channel_data: public Object
154 channel_data(eEPGCache*);
156 eTimer abortTimer, zapTimer;
157 __u8 state, isRunning, haveData, can_delete;
158 ePtr<eDVBChannel> channel;
159 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
160 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
161 tidMap seenSections, calcedSections;
162 void readData(const __u8 *data);
167 void abortNonAvail();
170 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
186 iDVBChannel *channel;
187 uniqueEPGKey service;
197 Message(int type, bool b)
198 :type(type), avail(b) {}
199 Message(int type, iDVBChannel *channel, int err=0)
200 :type(type), channel(channel), err(err) {}
201 Message(int type, const eServiceReferenceDVB& service, int err=0)
202 :type(type), service(service), err(err) {}
203 Message(int type, time_t time)
204 :type(type), time(time) {}
206 eFixedMessagePump<Message> messages;
208 friend class channel_data;
209 static eEPGCache *instance;
212 std::map<iDVBChannel*, channel_data*> m_knownChannels;
213 ePtr<eConnection> m_chanAddedConn;
216 updateMap channelLastUpdated;
217 static pthread_mutex_t cache_lock, channel_map_lock;
219 void thread(); // thread function
221 // called from epgcache thread
224 void sectionRead(const __u8 *data, int source, channel_data *channel);
225 void gotMessage(const Message &message);
226 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
229 // called from main thread
231 void DVBChannelAdded(eDVBChannel*);
232 void DVBChannelStateChanged(iDVBChannel*);
233 void DVBChannelRunning(iDVBChannel *);
235 static RESULT getInstance(ePtr<eEPGCache> &ptr);
239 // called from main thread
241 inline void Unlock();
242 Event *lookupEvent(const eServiceReferenceDVB &service, int event_id, bool plain=false );
243 Event *lookupEvent(const eServiceReferenceDVB &service, time_t=0, bool plain=false );
244 const eventMap* getEventMap(const eServiceReferenceDVB &service);
245 const timeMap* getTimeMap(const eServiceReferenceDVB &service);
248 TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
250 inline const eventMap* eEPGCache::getEventMap(const eServiceReferenceDVB &service)
252 eventCache::iterator It = eventDB.find( service );
253 if ( It != eventDB.end() && It->second.first.size() )
254 return &(It->second.first);
259 inline const timeMap* eEPGCache::getTimeMap(const eServiceReferenceDVB &service)
261 eventCache::iterator It = eventDB.find( service );
262 if ( It != eventDB.end() && It->second.second.size() )
263 return &(It->second.second);
268 inline void eEPGCache::Lock()
270 pthread_mutex_lock(&cache_lock);
273 inline void eEPGCache::Unlock()
275 pthread_mutex_unlock(&cache_lock);