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;
181 __u8 isRunning, haveData;
182 ePtr<eDVBChannel> channel;
183 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn, m_ViasatConn;
184 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader, m_ViasatReader;
185 tidMap seenSections[4], calcedSections[4];
186 #ifdef ENABLE_PRIVATE_EPG
187 ePtr<eTimer> startPrivateTimer;
190 uniqueEPGKey m_PrivateService;
191 ePtr<eConnection> m_PrivateConn;
192 ePtr<iDVBSectionReader> m_PrivateReader;
193 std::set<__u8> seenPrivateSections;
194 void readPrivateData(const __u8 *data);
195 void startPrivateReader();
197 #ifdef ENABLE_MHW_EPG
198 std::vector<mhw_channel_name_t> m_channels;
199 std::map<__u8, mhw_theme_name_t> m_themes;
200 std::map<__u32, mhw_title_t> m_titles;
201 std::multimap<__u32, __u32> m_program_ids;
202 ePtr<eConnection> m_MHWConn, m_MHWConn2;
203 ePtr<iDVBSectionReader> m_MHWReader, m_MHWReader2;
204 eDVBSectionFilterMask m_MHWFilterMask, m_MHWFilterMask2;
205 ePtr<eTimer> m_MHWTimeoutTimer;
206 __u16 m_mhw2_channel_pid, m_mhw2_title_pid, m_mhw2_summary_pid;
208 void MHWTimeout() { m_MHWTimeoutet=true; }
209 void readMHWData(const __u8 *data);
210 void readMHWData2(const __u8 *data);
211 void startMHWReader(__u16 pid, __u8 tid);
212 void startMHWReader2(__u16 pid, __u8 tid, int ext=-1);
213 void startTimeout(int msek);
214 bool checkTimeout() { return m_MHWTimeoutet; }
216 __u8 *delimitName( __u8 *in, __u8 *out, int len_in );
217 void timeMHW2DVB( u_char hours, u_char minutes, u_char *return_time);
218 void timeMHW2DVB( int minutes, u_char *return_time);
219 void timeMHW2DVB( u_char day, u_char hours, u_char minutes, u_char *return_time);
220 void storeTitle(std::map<__u32, mhw_title_t>::iterator itTitle, std::string sumText, const __u8 *data);
222 void readData(const __u8 *data);
223 void readDataViasat(const __u8 *data);
228 void abortNonAvail();
230 bool FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
232 enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4
233 #ifdef ENABLE_MHW_EPG
247 got_mhw2_channel_pid,
249 got_mhw2_summary_pid,
253 iDVBChannel *channel;
254 uniqueEPGKey service;
265 Message(int type, bool b)
266 :type(type), avail(b) {}
267 Message(int type, iDVBChannel *channel, int err=0)
268 :type(type), channel(channel), err(err) {}
269 Message(int type, const eServiceReference& service, int err=0)
270 :type(type), service(service), err(err) {}
271 Message(int type, time_t time)
272 :type(type), time(time) {}
274 eFixedMessagePump<Message> messages;
276 friend class channel_data;
277 static eEPGCache *instance;
279 ePtr<eTimer> cleanTimer;
280 std::map<iDVBChannel*, channel_data*> m_knownChannels;
281 ePtr<eConnection> m_chanAddedConn;
284 updateMap channelLastUpdated;
285 static pthread_mutex_t cache_lock, channel_map_lock;
287 #ifdef ENABLE_PRIVATE_EPG
288 contentMaps content_time_tables;
291 void thread(); // thread function
293 // called from epgcache thread
295 char m_filename[1024];
298 #ifdef ENABLE_PRIVATE_EPG
299 void privateSectionRead(const uniqueEPGKey &, const __u8 *);
301 void sectionRead(const __u8 *data, int source, channel_data *channel);
302 void gotMessage(const Message &message);
303 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
306 // called from main thread
308 void DVBChannelAdded(eDVBChannel*);
309 void DVBChannelStateChanged(iDVBChannel*);
310 void DVBChannelRunning(iDVBChannel *);
312 timeMap::iterator m_timemap_cursor, m_timemap_end;
313 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
319 static eEPGCache *getInstance() { return instance; }
324 #ifdef ENABLE_PRIVATE_EPG
325 void PMTready(eDVBServicePMTHandler *pmthandler);
327 void PMTready(eDVBServicePMTHandler *pmthandler) {}
331 // must be called once!
332 void setCacheFile(const char *filename);
334 // called from main thread
336 inline void Unlock();
338 // at moment just for one service..
339 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
342 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
343 // but its faster in use... its not allowed to delete this pointers via delete or free..
344 RESULT lookupEventId(const eServiceReference &service, int event_id, const eventData *&);
345 RESULT lookupEventTime(const eServiceReference &service, time_t, const eventData *&, int direction=0);
346 RESULT getNextTimeEntry(const eventData *&);
348 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
349 // its not allowed to delete this pointers via delete or free..
350 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
351 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
352 RESULT getNextTimeEntry(const eit_event_struct *&);
354 // Event's are parsed epg events.. it's safe to use them after cache unlock
355 // after use this Events must be deleted (memleaks)
356 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
357 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
358 RESULT getNextTimeEntry(Event *&);
361 SIMILAR_BROADCASTINGS_SEARCH,
369 PyObject *lookupEvent(SWIG_PYOBJECT(ePyObject) list, SWIG_PYOBJECT(ePyObject) convertFunc=(PyObject*)0);
370 PyObject *search(SWIG_PYOBJECT(ePyObject));
372 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
373 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
374 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
375 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
376 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
380 inline void eEPGCache::Lock()
382 pthread_mutex_lock(&cache_lock);
385 inline void eEPGCache::Unlock()
387 pthread_mutex_unlock(&cache_lock);