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>
23 #define CLEAN_INTERVAL 60000 // 1 min
24 #define UPDATE_INTERVAL 3600000 // 60 min
25 #define ZAP_DELAY 2000 // 2 sek
27 #define HILO(x) (x##_hi << 8 | x##_lo)
30 class eServiceReferenceDVB;
35 uniqueEPGKey( const eServiceReference &ref )
36 :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
37 ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
38 ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
42 :sid(-1), onid(-1), tsid(-1)
45 uniqueEPGKey( int sid, int onid, int tsid )
46 :sid(sid), onid(onid), tsid(tsid)
49 bool operator <(const uniqueEPGKey &a) const
51 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
55 return !(sid == -1 && onid == -1 && tsid == -1);
57 bool operator==(const uniqueEPGKey &a) const
59 return !memcmp( &sid, &a.sid, sizeof(int)*3);
63 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
65 return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
70 //eventMap is sorted by event_id
71 #define eventMap std::map<__u16, eventData*>
72 //timeMap is sorted by beginTime
73 #define timeMap std::map<time_t, eventData*>
75 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
76 #define updateMap std::map<eDVBChannelID, time_t>
78 struct hash_uniqueEPGKey
80 inline size_t operator()( const uniqueEPGKey &x) const
82 return (x.onid << 16) | x.tsid;
86 #define tidMap std::set<__u32>
87 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
88 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
89 #else // for older gcc use following
90 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
93 #define descriptorPair std::pair<int,__u8*>
94 #define descriptorMap std::map<__u32, descriptorPair >
101 friend class eEPGCache;
106 static descriptorMap descriptors;
107 static __u8 data[4108];
108 static int CacheSize;
109 static void load(FILE *);
110 static void save(FILE *);
113 eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
116 const eit_event_struct* get() const;
117 operator const eit_event_struct*() const
124 return (EITdata[0] << 8) | EITdata[1];
126 time_t getStartTime()
128 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
132 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
136 class eEPGCache: public eMainloop, private eThread, public Object
139 DECLARE_REF(eEPGCache)
140 struct channel_data: public Object
142 channel_data(eEPGCache*);
144 eTimer abortTimer, zapTimer;
145 int prevChannelState;
146 __u8 state, isRunning, haveData, can_delete;
147 ePtr<eDVBChannel> channel;
148 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
149 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
150 tidMap seenSections[3], calcedSections[3];
151 void readData(const __u8 *data);
156 void abortNonAvail();
159 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
175 iDVBChannel *channel;
176 uniqueEPGKey service;
186 Message(int type, bool b)
187 :type(type), avail(b) {}
188 Message(int type, iDVBChannel *channel, int err=0)
189 :type(type), channel(channel), err(err) {}
190 Message(int type, const eServiceReference& service, int err=0)
191 :type(type), service(service), err(err) {}
192 Message(int type, time_t time)
193 :type(type), time(time) {}
195 eFixedMessagePump<Message> messages;
197 friend class channel_data;
198 static eEPGCache *instance;
201 std::map<iDVBChannel*, channel_data*> m_knownChannels;
202 ePtr<eConnection> m_chanAddedConn;
205 updateMap channelLastUpdated;
206 static pthread_mutex_t cache_lock, channel_map_lock;
208 void thread(); // thread function
210 // called from epgcache thread
213 void sectionRead(const __u8 *data, int source, channel_data *channel);
214 void gotMessage(const Message &message);
215 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
218 // called from main thread
220 void DVBChannelAdded(eDVBChannel*);
221 void DVBChannelStateChanged(iDVBChannel*);
222 void DVBChannelRunning(iDVBChannel *);
224 timeMap::iterator m_timemap_cursor, m_timemap_end;
225 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
228 static eEPGCache *getInstance() { return instance; }
232 // called from main thread
234 inline void Unlock();
236 // at moment just for one service..
237 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
239 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
240 // but its faster in use... its not allowed to delete this pointers via delete or free..
241 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
242 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT);
243 SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
246 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
247 // its not allowed to delete this pointers via delete or free..
248 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
249 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&);
250 RESULT getNextTimeEntry(const eit_event_struct *&);
252 // Event's are parsed epg events.. it's safe to use them after cache unlock
253 // after use this Events must be deleted (memleaks)
254 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
255 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &);
256 RESULT getNextTimeEntry(Event *&);
259 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
260 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
261 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
262 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT);
263 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
267 inline void eEPGCache::Lock()
269 pthread_mutex_lock(&cache_lock);
272 inline void eEPGCache::Unlock()
274 pthread_mutex_unlock(&cache_lock);