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>
107 #define descriptorPair std::pair<int,__u8*>
108 #define descriptorMap std::map<__u32, descriptorPair >
112 friend class eEPGCache;
116 static descriptorMap descriptors;
119 static int CacheSize;
120 static void load(FILE *);
121 static void save(FILE *);
122 eventData(const eit_event_struct* e, int size, int type);
124 const eit_event_struct* get() const;
125 operator const eit_event_struct*() const
131 return (EITdata[0] << 8) | EITdata[1];
133 time_t getStartTime()
135 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
139 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
143 class eEPGCache: public eMainloop, private eThread, public Object
145 DECLARE_REF(eEPGCache)
146 struct channel_data: public Object
148 channel_data(eEPGCache*);
150 eTimer abortTimer, zapTimer;
151 __u8 state, isRunning, haveData, can_delete;
152 ePtr<eDVBChannel> channel;
153 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
154 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
155 tidMap seenSections[3], calcedSections[3];
156 void readData(const __u8 *data);
161 void abortNonAvail();
164 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
180 iDVBChannel *channel;
181 uniqueEPGKey service;
191 Message(int type, bool b)
192 :type(type), avail(b) {}
193 Message(int type, iDVBChannel *channel, int err=0)
194 :type(type), channel(channel), err(err) {}
195 Message(int type, const eServiceReferenceDVB& service, int err=0)
196 :type(type), service(service), err(err) {}
197 Message(int type, time_t time)
198 :type(type), time(time) {}
200 eFixedMessagePump<Message> messages;
202 friend class channel_data;
203 static eEPGCache *instance;
206 std::map<iDVBChannel*, channel_data*> m_knownChannels;
207 ePtr<eConnection> m_chanAddedConn;
210 updateMap channelLastUpdated;
211 static pthread_mutex_t cache_lock, channel_map_lock;
213 void thread(); // thread function
215 // called from epgcache thread
218 void sectionRead(const __u8 *data, int source, channel_data *channel);
219 void gotMessage(const Message &message);
220 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
223 // called from main thread
225 void DVBChannelAdded(eDVBChannel*);
226 void DVBChannelStateChanged(iDVBChannel*);
227 void DVBChannelRunning(iDVBChannel *);
229 static RESULT getInstance(ePtr<eEPGCache> &ptr);
233 // called from main thread
235 inline void Unlock();
237 // result Event * must be deleted by caller of lookupEvent
238 inline RESULT lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& );
239 inline RESULT lookupEvent(const eServiceReferenceDVB &service, time_t , Event *& );
241 // methods for faster use.. but not thread save ..
242 // Lock and Unlock should be used !!
243 RESULT lookupEvent(const eServiceReferenceDVB &service, int event_id, const eventData *& );
244 RESULT lookupEvent(const eServiceReferenceDVB &service, time_t , const eventData *& );
246 inline RESULT getEventMap(const eServiceReferenceDVB &service, const eventMap *& );
247 inline RESULT getTimeMap(const eServiceReferenceDVB &service, const timeMap *& );
250 TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
252 inline RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, Event *& result )
254 const eventData *data=0;
255 RESULT ret = lookupEvent(service, t, data);
257 result = new Event((uint8_t*)data->get());
261 inline RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& result)
263 const eventData *data=0;
264 RESULT ret = lookupEvent(service, event_id, data);
266 result = new Event((uint8_t*)data->get());
270 inline RESULT eEPGCache::getEventMap(const eServiceReferenceDVB &service, const eventMap *& result)
272 eventCache::iterator It = eventDB.find( service );
273 if ( It != eventDB.end() && It->second.first.size() )
275 result=&(It->second.first);
283 inline RESULT eEPGCache::getTimeMap(const eServiceReferenceDVB &service, const timeMap *& result)
285 eventCache::iterator It = eventDB.find( service );
286 if ( It != eventDB.end() && It->second.second.size() )
288 result=&(It->second.second);
296 inline void eEPGCache::Lock()
298 pthread_mutex_lock(&cache_lock);
301 inline void eEPGCache::Unlock()
303 pthread_mutex_unlock(&cache_lock);