4 #define ENABLE_PRIVATE_EPG 1
5 #define NEED_DEMUX_WORKAROUND 1
11 #include <ext/hash_map>
12 #include <ext/hash_set>
16 #include <lib/dvb/eit.h>
17 #include <lib/dvb/lowlevel/eit.h>
18 #include <lib/dvb/idvb.h>
19 #include <lib/dvb/demux.h>
20 #include <lib/dvb/dvbtime.h>
21 #include <lib/base/ebase.h>
22 #include <lib/base/thread.h>
23 #include <lib/base/message.h>
24 #include <lib/service/event.h>
25 #include <lib/python/python.h>
27 #define CLEAN_INTERVAL 60000 // 1 min
28 #define UPDATE_INTERVAL 3600000 // 60 min
29 #define ZAP_DELAY 2000 // 2 sek
31 #define HILO(x) (x##_hi << 8 | x##_lo)
34 class eServiceReferenceDVB;
35 class eDVBServicePMTHandler;
40 uniqueEPGKey( const eServiceReference &ref )
41 :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
42 ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
43 ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
47 :sid(-1), onid(-1), tsid(-1)
50 uniqueEPGKey( int sid, int onid, int tsid )
51 :sid(sid), onid(onid), tsid(tsid)
54 bool operator <(const uniqueEPGKey &a) const
56 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
60 return !(sid == -1 && onid == -1 && tsid == -1);
62 bool operator==(const uniqueEPGKey &a) const
64 return !memcmp( &sid, &a.sid, sizeof(int)*3);
68 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
70 return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
75 //eventMap is sorted by event_id
76 #define eventMap std::map<__u16, eventData*>
77 //timeMap is sorted by beginTime
78 #define timeMap std::map<time_t, eventData*>
80 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
81 #define updateMap std::map<eDVBChannelID, time_t>
83 struct hash_uniqueEPGKey
85 inline size_t operator()( const uniqueEPGKey &x) const
87 return (x.onid << 16) | x.tsid;
91 #define tidMap std::set<__u32>
92 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
93 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
94 #ifdef ENABLE_PRIVATE_EPG
95 #define contentTimeMap __gnu_cxx::hash_map<time_t, std::pair<time_t, __u16> >
96 #define contentMap __gnu_cxx::hash_map<int, contentTimeMap >
97 #define contentMaps __gnu_cxx::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal >
99 #else // for older gcc use following
100 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
101 #ifdef ENABLE_PRIVATE_EPG
102 #define contentTimeMap std::hash_map<time_t, std::pair<time_t, __u16> >
103 #define contentMap std::hash_map<int, contentTimeMap >
104 #define contentMaps std::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal>
108 #define descriptorPair std::pair<int,__u8*>
109 #define descriptorMap std::map<__u32, descriptorPair >
113 friend class eEPGCache;
118 static descriptorMap descriptors;
119 static __u8 data[4108];
120 static int CacheSize;
121 static void load(FILE *);
122 static void save(FILE *);
124 eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
126 const eit_event_struct* get() const;
127 operator const eit_event_struct*() const
133 return (EITdata[0] << 8) | EITdata[1];
135 time_t getStartTime()
137 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
141 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
146 class eEPGCache: public eMainloop, private eThread, public Object
149 DECLARE_REF(eEPGCache)
150 struct channel_data: public Object
152 channel_data(eEPGCache*);
154 eTimer abortTimer, zapTimer;
155 int prevChannelState;
156 __u8 state, isRunning, haveData, can_delete;
157 ePtr<eDVBChannel> channel;
158 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
159 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
160 tidMap seenSections[3], calcedSections[3];
161 #ifdef ENABLE_PRIVATE_EPG
162 #ifdef NEED_DEMUX_WORKAROUND
166 uniqueEPGKey m_PrivateService;
167 ePtr<eConnection> m_PrivateConn;
168 ePtr<iDVBSectionReader> m_PrivateReader;
169 std::set<__u8> seenPrivateSections;
170 void readPrivateData(const __u8 *data);
171 void startPrivateReader(int pid, int version);
173 void readData(const __u8 *data);
178 void abortNonAvail();
181 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
198 iDVBChannel *channel;
199 uniqueEPGKey service;
210 Message(int type, bool b)
211 :type(type), avail(b) {}
212 Message(int type, iDVBChannel *channel, int err=0)
213 :type(type), channel(channel), err(err) {}
214 Message(int type, const eServiceReference& service, int err=0)
215 :type(type), service(service), err(err) {}
216 Message(int type, time_t time)
217 :type(type), time(time) {}
219 eFixedMessagePump<Message> messages;
221 friend class channel_data;
222 static eEPGCache *instance;
225 std::map<iDVBChannel*, channel_data*> m_knownChannels;
226 ePtr<eConnection> m_chanAddedConn;
229 updateMap channelLastUpdated;
230 static pthread_mutex_t cache_lock, channel_map_lock;
232 #ifdef ENABLE_PRIVATE_EPG
233 contentMaps content_time_tables;
236 void thread(); // thread function
238 // called from epgcache thread
241 #ifdef ENABLE_PRIVATE_EPG
242 void privateSectionRead(const uniqueEPGKey &, const __u8 *);
244 void sectionRead(const __u8 *data, int source, channel_data *channel);
245 void gotMessage(const Message &message);
246 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
249 // called from main thread
251 void DVBChannelAdded(eDVBChannel*);
252 void DVBChannelStateChanged(iDVBChannel*);
253 void DVBChannelRunning(iDVBChannel *);
255 timeMap::iterator m_timemap_cursor, m_timemap_end;
256 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
262 static eEPGCache *getInstance() { return instance; }
268 // called from main thread
270 inline void Unlock();
271 #ifdef ENABLE_PRIVATE_EPG
272 void PMTready(eDVBServicePMTHandler *pmthandler);
274 void PMTready(eDVBServicePMTHandler *pmthandler) {}
277 // at moment just for one service..
278 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
281 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
282 // but its faster in use... its not allowed to delete this pointers via delete or free..
283 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
284 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT, int direction=0);
285 SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
287 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
288 // its not allowed to delete this pointers via delete or free..
289 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
290 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
291 RESULT getNextTimeEntry(const eit_event_struct *&);
293 // Event's are parsed epg events.. it's safe to use them after cache unlock
294 // after use this Events must be deleted (memleaks)
295 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
296 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
297 RESULT getNextTimeEntry(Event *&);
299 PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
301 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
302 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
303 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
304 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
305 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
309 inline void eEPGCache::Lock()
311 pthread_mutex_lock(&cache_lock);
314 inline void eEPGCache::Unlock()
316 pthread_mutex_unlock(&cache_lock);