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 eTimer startPrivateTimer;
165 uniqueEPGKey m_PrivateService;
166 ePtr<eConnection> m_PrivateConn;
167 ePtr<iDVBSectionReader> m_PrivateReader;
168 std::set<__u8> seenPrivateSections;
169 void readPrivateData(const __u8 *data);
170 void startPrivateReader();
172 void readData(const __u8 *data);
177 void abortNonAvail();
180 enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
197 iDVBChannel *channel;
198 uniqueEPGKey service;
209 Message(int type, bool b)
210 :type(type), avail(b) {}
211 Message(int type, iDVBChannel *channel, int err=0)
212 :type(type), channel(channel), err(err) {}
213 Message(int type, const eServiceReference& service, int err=0)
214 :type(type), service(service), err(err) {}
215 Message(int type, time_t time)
216 :type(type), time(time) {}
218 eFixedMessagePump<Message> messages;
220 friend class channel_data;
221 static eEPGCache *instance;
224 std::map<iDVBChannel*, channel_data*> m_knownChannels;
225 ePtr<eConnection> m_chanAddedConn;
228 updateMap channelLastUpdated;
229 static pthread_mutex_t cache_lock, channel_map_lock;
231 #ifdef ENABLE_PRIVATE_EPG
232 contentMaps content_time_tables;
235 void thread(); // thread function
237 // called from epgcache thread
240 #ifdef ENABLE_PRIVATE_EPG
241 void privateSectionRead(const uniqueEPGKey &, const __u8 *);
243 void sectionRead(const __u8 *data, int source, channel_data *channel);
244 void gotMessage(const Message &message);
245 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
248 // called from main thread
250 void DVBChannelAdded(eDVBChannel*);
251 void DVBChannelStateChanged(iDVBChannel*);
252 void DVBChannelRunning(iDVBChannel *);
254 timeMap::iterator m_timemap_cursor, m_timemap_end;
255 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
261 static eEPGCache *getInstance() { return instance; }
267 // called from main thread
269 inline void Unlock();
270 #ifdef ENABLE_PRIVATE_EPG
271 void PMTready(eDVBServicePMTHandler *pmthandler);
273 void PMTready(eDVBServicePMTHandler *pmthandler) {}
276 // at moment just for one service..
277 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
280 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
281 // but its faster in use... its not allowed to delete this pointers via delete or free..
282 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
283 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT, int direction=0);
284 SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
286 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
287 // its not allowed to delete this pointers via delete or free..
288 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
289 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
290 RESULT getNextTimeEntry(const eit_event_struct *&);
292 // Event's are parsed epg events.. it's safe to use them after cache unlock
293 // after use this Events must be deleted (memleaks)
294 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
295 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
296 RESULT getNextTimeEntry(Event *&);
299 SIMILAR_BROADCASTINGS_SEARCH,
307 PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
308 PyObject *search(PyObject *);
310 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
311 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
312 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
313 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
314 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
318 inline void eEPGCache::Lock()
320 pthread_mutex_lock(&cache_lock);
323 inline void eEPGCache::Unlock()
325 pthread_mutex_unlock(&cache_lock);