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>
75 struct hash_uniqueEPGKey
77 inline size_t operator()( const uniqueEPGKey &x) const
79 return (x.onid << 16) | x.tsid;
83 #define tidMap std::set<__u32>
84 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
85 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
86 #else // for older gcc use following
87 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
90 #define descriptorPair std::pair<int,__u8*>
91 #define descriptorMap std::map<__u32, descriptorPair >
95 friend class eEPGCache;
99 static descriptorMap descriptors;
102 static int CacheSize;
103 static void load(FILE *);
104 static void save(FILE *);
105 eventData(const eit_event_struct* e, int size, int type);
107 const eit_event_struct* get() const;
108 operator const eit_event_struct*() const
114 return (EITdata[0] << 8) | EITdata[1];
116 time_t getStartTime()
118 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
122 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
126 class eEPGCache: public eMainloop, private eThread, public Object
128 DECLARE_REF(eEPGCache)
129 struct channel_data: public Object
131 channel_data(eEPGCache*);
133 eTimer abortTimer, zapTimer;
134 __u8 state, isRunning, haveData, can_delete;
135 ePtr<eDVBChannel> channel;
136 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
137 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
138 tidMap seenSections[3], calcedSections[3];
139 void readData(const __u8 *data);
144 void abortNonAvail();
147 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
163 iDVBChannel *channel;
164 uniqueEPGKey service;
174 Message(int type, bool b)
175 :type(type), avail(b) {}
176 Message(int type, iDVBChannel *channel, int err=0)
177 :type(type), channel(channel), err(err) {}
178 Message(int type, const eServiceReferenceDVB& service, int err=0)
179 :type(type), service(service), err(err) {}
180 Message(int type, time_t time)
181 :type(type), time(time) {}
183 eFixedMessagePump<Message> messages;
185 friend class channel_data;
186 static eEPGCache *instance;
189 std::map<iDVBChannel*, channel_data*> m_knownChannels;
190 ePtr<eConnection> m_chanAddedConn;
193 updateMap channelLastUpdated;
194 static pthread_mutex_t cache_lock, channel_map_lock;
196 void thread(); // thread function
198 // called from epgcache thread
201 void sectionRead(const __u8 *data, int source, channel_data *channel);
202 void gotMessage(const Message &message);
203 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
206 // called from main thread
208 void DVBChannelAdded(eDVBChannel*);
209 void DVBChannelStateChanged(iDVBChannel*);
210 void DVBChannelRunning(iDVBChannel *);
212 static RESULT getInstance(ePtr<eEPGCache> &ptr);
216 // called from main thread
218 inline void Unlock();
220 // result Event * must be deleted by caller of lookupEvent
221 inline RESULT lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& );
222 inline RESULT lookupEvent(const eServiceReferenceDVB &service, time_t , Event *& );
224 // methods for faster use.. but not thread save ..
225 // Lock and Unlock should be used !!
226 RESULT lookupEvent(const eServiceReferenceDVB &service, int event_id, const eventData *& );
227 RESULT lookupEvent(const eServiceReferenceDVB &service, time_t , const eventData *& );
229 inline RESULT getEventMap(const eServiceReferenceDVB &service, const eventMap *& );
230 inline RESULT getTimeMap(const eServiceReferenceDVB &service, const timeMap *& );
233 TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
235 inline RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, Event *& result )
237 const eventData *data=0;
238 RESULT ret = lookupEvent(service, t, data);
240 result = new Event((uint8_t*)data->get());
244 inline RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& result)
246 const eventData *data=0;
247 RESULT ret = lookupEvent(service, event_id, data);
249 result = new Event((uint8_t*)data->get());
253 inline RESULT eEPGCache::getEventMap(const eServiceReferenceDVB &service, const eventMap *& result)
255 eventCache::iterator It = eventDB.find( service );
256 if ( It != eventDB.end() && It->second.first.size() )
258 result=&(It->second.first);
266 inline RESULT eEPGCache::getTimeMap(const eServiceReferenceDVB &service, const timeMap *& result)
268 eventCache::iterator It = eventDB.find( service );
269 if ( It != eventDB.end() && It->second.second.size() )
271 result=&(It->second.second);
279 inline void eEPGCache::Lock()
281 pthread_mutex_lock(&cache_lock);
284 inline void eEPGCache::Unlock()
286 pthread_mutex_unlock(&cache_lock);