4 #define ENABLE_PRIVATE_EPG 1
5 //#define ENABLE_MHW_EPG 1
9 /* Test for gcc >= maj.min, as per __GNUC_PREREQ in glibc */
10 #if defined (__GNUC__) && defined (__GNUC_MINOR__)
11 #define __GNUC_PREREQ(maj, min) \
12 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
14 #define __GNUC_PREREQ(maj, min) 0
19 #if 0 && __GNUC_PREREQ(4,3)
20 #include <unordered_map>
21 #include <unordered_set>
23 #include <ext/hash_map>
24 #include <ext/hash_set>
29 #include <lib/dvb/eit.h>
30 #include <lib/dvb/lowlevel/eit.h>
32 #include <lib/dvb/lowlevel/mhw.h>
34 #include <lib/dvb/idvb.h>
35 #include <lib/dvb/demux.h>
36 #include <lib/dvb/dvbtime.h>
37 #include <lib/base/ebase.h>
38 #include <lib/base/thread.h>
39 #include <lib/base/message.h>
40 #include <lib/service/event.h>
41 #include <lib/python/python.h>
43 #define CLEAN_INTERVAL 60000 // 1 min
44 #define UPDATE_INTERVAL 3600000 // 60 min
45 #define ZAP_DELAY 2000 // 2 sek
47 #define HILO(x) (x##_hi << 8 | x##_lo)
50 class eServiceReferenceDVB;
51 class eDVBServicePMTHandler;
56 uniqueEPGKey( const eServiceReference &ref )
57 :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
58 ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
59 ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
63 :sid(-1), onid(-1), tsid(-1)
66 uniqueEPGKey( int sid, int onid, int tsid )
67 :sid(sid), onid(onid), tsid(tsid)
70 bool operator <(const uniqueEPGKey &a) const
72 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
76 return !(sid == -1 && onid == -1 && tsid == -1);
78 bool operator==(const uniqueEPGKey &a) const
80 return !memcmp( &sid, &a.sid, sizeof(int)*3);
84 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
86 return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
91 //eventMap is sorted by event_id
92 #define eventMap std::map<__u16, eventData*>
93 //timeMap is sorted by beginTime
94 #define timeMap std::map<time_t, eventData*>
96 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
97 #define updateMap std::map<eDVBChannelID, time_t>
99 struct hash_uniqueEPGKey
101 inline size_t operator()( const uniqueEPGKey &x) const
103 return (x.onid << 16) | x.tsid;
107 #define tidMap std::set<__u32>
108 #if 0 && __GNUC_PREREQ(4,3)
109 #define eventCache std::unordered_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
110 #ifdef ENABLE_PRIVATE_EPG
111 #define contentTimeMap std::unordered_map<time_t, std::pair<time_t, __u16> >
112 #define contentMap std::unordered_map<int, contentTimeMap >
113 #define contentMaps std::unordered_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal >
115 #elif __GNUC_PREREQ(3,1)
116 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
117 #ifdef ENABLE_PRIVATE_EPG
118 #define contentTimeMap __gnu_cxx::hash_map<time_t, std::pair<time_t, __u16> >
119 #define contentMap __gnu_cxx::hash_map<int, contentTimeMap >
120 #define contentMaps __gnu_cxx::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal >
122 #else // for older gcc use following
123 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
124 #ifdef ENABLE_PRIVATE_EPG
125 #define contentTimeMap std::hash_map<time_t, std::pair<time_t, __u16> >
126 #define contentMap std::hash_map<int, contentTimeMap >
127 #define contentMaps std::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal>
131 #define descriptorPair std::pair<int,__u8*>
132 #define descriptorMap std::map<__u32, descriptorPair >
136 friend class eEPGCache;
141 static descriptorMap descriptors;
142 static __u8 data[4108];
143 static int CacheSize;
144 static void load(FILE *);
145 static void save(FILE *);
147 eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
149 const eit_event_struct* get() const;
150 operator const eit_event_struct*() const
156 return (EITdata[0] << 8) | EITdata[1];
158 time_t getStartTime()
160 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
164 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
169 class eEPGCache: public eMainloop, private eThread, public Object
172 DECLARE_REF(eEPGCache)
173 struct channel_data: public Object
175 pthread_mutex_t channel_active;
176 channel_data(eEPGCache*);
178 ePtr<eTimer> abortTimer, zapTimer;
179 int prevChannelState;
180 __u8 state, isRunning, haveData;
181 ePtr<eDVBChannel> channel;
182 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn, m_ViasatConn;
183 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader, m_ViasatReader;
184 tidMap seenSections[4], calcedSections[4];
185 #ifdef ENABLE_PRIVATE_EPG
186 ePtr<eTimer> startPrivateTimer;
189 uniqueEPGKey m_PrivateService;
190 ePtr<eConnection> m_PrivateConn;
191 ePtr<iDVBSectionReader> m_PrivateReader;
192 std::set<__u8> seenPrivateSections;
193 void readPrivateData(const __u8 *data);
194 void startPrivateReader();
196 #ifdef ENABLE_MHW_EPG
197 std::vector<mhw_channel_name_t> m_channels;
198 std::map<__u8, mhw_theme_name_t> m_themes;
199 std::map<__u32, mhw_title_t> m_titles;
200 std::multimap<__u32, __u32> m_program_ids;
201 ePtr<eConnection> m_MHWConn, m_MHWConn2;
202 ePtr<iDVBSectionReader> m_MHWReader, m_MHWReader2;
203 eDVBSectionFilterMask m_MHWFilterMask, m_MHWFilterMask2;
204 ePtr<eTimer> m_MHWTimeoutTimer;
205 __u16 m_mhw2_channel_pid, m_mhw2_title_pid, m_mhw2_summary_pid;
207 void MHWTimeout() { m_MHWTimeoutet=true; }
208 void readMHWData(const __u8 *data);
209 void readMHWData2(const __u8 *data);
210 void startMHWReader(__u16 pid, __u8 tid);
211 void startMHWReader2(__u16 pid, __u8 tid, int ext=-1);
212 void startTimeout(int msek);
213 bool checkTimeout() { return m_MHWTimeoutet; }
215 __u8 *delimitName( __u8 *in, __u8 *out, int len_in );
216 void timeMHW2DVB( u_char hours, u_char minutes, u_char *return_time);
217 void timeMHW2DVB( int minutes, u_char *return_time);
218 void timeMHW2DVB( u_char day, u_char hours, u_char minutes, u_char *return_time);
219 void storeTitle(std::map<__u32, mhw_title_t>::iterator itTitle, std::string sumText, const __u8 *data);
221 void readData(const __u8 *data);
222 void readDataViasat(const __u8 *data);
227 void abortNonAvail();
229 bool FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
231 enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4
232 #ifdef ENABLE_MHW_EPG
246 got_mhw2_channel_pid,
248 got_mhw2_summary_pid,
252 iDVBChannel *channel;
253 uniqueEPGKey service;
264 Message(int type, bool b)
265 :type(type), avail(b) {}
266 Message(int type, iDVBChannel *channel, int err=0)
267 :type(type), channel(channel), err(err) {}
268 Message(int type, const eServiceReference& service, int err=0)
269 :type(type), service(service), err(err) {}
270 Message(int type, time_t time)
271 :type(type), time(time) {}
273 eFixedMessagePump<Message> messages;
275 friend class channel_data;
276 static eEPGCache *instance;
278 ePtr<eTimer> cleanTimer;
279 std::map<iDVBChannel*, channel_data*> m_knownChannels;
280 ePtr<eConnection> m_chanAddedConn;
283 updateMap channelLastUpdated;
284 static pthread_mutex_t cache_lock, channel_map_lock;
286 #ifdef ENABLE_PRIVATE_EPG
287 contentMaps content_time_tables;
290 void thread(); // thread function
292 // called from epgcache thread
295 #ifdef ENABLE_PRIVATE_EPG
296 void privateSectionRead(const uniqueEPGKey &, const __u8 *);
298 void sectionRead(const __u8 *data, int source, channel_data *channel);
299 void gotMessage(const Message &message);
300 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
303 // called from main thread
305 void DVBChannelAdded(eDVBChannel*);
306 void DVBChannelStateChanged(iDVBChannel*);
307 void DVBChannelRunning(iDVBChannel *);
309 timeMap::iterator m_timemap_cursor, m_timemap_end;
310 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
316 static eEPGCache *getInstance() { return instance; }
321 #ifdef ENABLE_PRIVATE_EPG
322 void PMTready(eDVBServicePMTHandler *pmthandler);
324 void PMTready(eDVBServicePMTHandler *pmthandler) {}
328 // called from main thread
330 inline void Unlock();
332 // at moment just for one service..
333 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
336 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
337 // but its faster in use... its not allowed to delete this pointers via delete or free..
338 RESULT lookupEventId(const eServiceReference &service, int event_id, const eventData *&);
339 RESULT lookupEventTime(const eServiceReference &service, time_t, const eventData *&, int direction=0);
340 RESULT getNextTimeEntry(const eventData *&);
342 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
343 // its not allowed to delete this pointers via delete or free..
344 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
345 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
346 RESULT getNextTimeEntry(const eit_event_struct *&);
348 // Event's are parsed epg events.. it's safe to use them after cache unlock
349 // after use this Events must be deleted (memleaks)
350 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
351 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
352 RESULT getNextTimeEntry(Event *&);
355 SIMILAR_BROADCASTINGS_SEARCH,
363 PyObject *lookupEvent(SWIG_PYOBJECT(ePyObject) list, SWIG_PYOBJECT(ePyObject) convertFunc=(PyObject*)0);
364 PyObject *search(SWIG_PYOBJECT(ePyObject));
366 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
367 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
368 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
369 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
370 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
374 inline void eEPGCache::Lock()
376 pthread_mutex_lock(&cache_lock);
379 inline void eEPGCache::Unlock()
381 pthread_mutex_unlock(&cache_lock);