4 #define ENABLE_PRIVATE_EPG 1
10 #include <ext/hash_map>
11 #include <ext/hash_set>
15 #include <lib/dvb/eit.h>
16 #include <lib/dvb/lowlevel/eit.h>
17 #include <lib/dvb/idvb.h>
18 #include <lib/dvb/demux.h>
19 #include <lib/dvb/dvbtime.h>
20 #include <lib/base/ebase.h>
21 #include <lib/base/thread.h>
22 #include <lib/base/message.h>
23 #include <lib/service/event.h>
24 #include <lib/python/python.h>
26 #define CLEAN_INTERVAL 60000 // 1 min
27 #define UPDATE_INTERVAL 3600000 // 60 min
28 #define ZAP_DELAY 2000 // 2 sek
30 #define HILO(x) (x##_hi << 8 | x##_lo)
33 class eServiceReferenceDVB;
34 class eDVBServicePMTHandler;
39 uniqueEPGKey( const eServiceReference &ref )
40 :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
41 ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
42 ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
46 :sid(-1), onid(-1), tsid(-1)
49 uniqueEPGKey( int sid, int onid, int tsid )
50 :sid(sid), onid(onid), tsid(tsid)
53 bool operator <(const uniqueEPGKey &a) const
55 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
59 return !(sid == -1 && onid == -1 && tsid == -1);
61 bool operator==(const uniqueEPGKey &a) const
63 return !memcmp( &sid, &a.sid, sizeof(int)*3);
67 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
69 return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
74 //eventMap is sorted by event_id
75 #define eventMap std::map<__u16, eventData*>
76 //timeMap is sorted by beginTime
77 #define timeMap std::map<time_t, eventData*>
79 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
80 #define updateMap std::map<eDVBChannelID, time_t>
82 struct hash_uniqueEPGKey
84 inline size_t operator()( const uniqueEPGKey &x) const
86 return (x.onid << 16) | x.tsid;
90 #define tidMap std::set<__u32>
91 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 ) // check if gcc version >= 3.1
92 #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
93 #ifdef ENABLE_PRIVATE_EPG
94 #define contentTimeMap __gnu_cxx::hash_map<time_t, std::pair<time_t, __u16> >
95 #define contentMap __gnu_cxx::hash_map<int, contentTimeMap >
96 #define contentMaps __gnu_cxx::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal >
98 #else // for older gcc use following
99 #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
100 #ifdef ENABLE_PRIVATE_EPG
101 #define contentTimeMap std::hash_map<time_t, std::pair<time_t, __u16> >
102 #define contentMap std::hash_map<int, contentTimeMap >
103 #define contentMaps std::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal>
107 #define descriptorPair std::pair<int,__u8*>
108 #define descriptorMap std::map<__u32, descriptorPair >
112 friend class eEPGCache;
117 static descriptorMap descriptors;
118 static __u8 data[4108];
119 static int CacheSize;
120 static void load(FILE *);
121 static void save(FILE *);
123 eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
125 const eit_event_struct* get() const;
126 operator const eit_event_struct*() const
132 return (EITdata[0] << 8) | EITdata[1];
134 time_t getStartTime()
136 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
140 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
145 class eEPGCache: public eMainloop, private eThread, public Object
148 DECLARE_REF(eEPGCache)
149 struct channel_data: public Object
151 channel_data(eEPGCache*);
153 eTimer abortTimer, zapTimer;
154 int prevChannelState;
155 __u8 state, isRunning, haveData;
156 ePtr<eDVBChannel> channel;
157 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
158 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
159 tidMap seenSections[3], calcedSections[3];
160 #ifdef ENABLE_PRIVATE_EPG
161 eTimer startPrivateTimer;
164 uniqueEPGKey m_PrivateService;
165 ePtr<eConnection> m_PrivateConn;
166 ePtr<iDVBSectionReader> m_PrivateReader;
167 std::set<__u8> seenPrivateSections;
168 void readPrivateData(const __u8 *data);
169 void startPrivateReader();
170 bool canDelete() { return !isRunning && !m_PrivateConn; }
172 bool canDelete() { return !isRunning; }
174 void readData(const __u8 *data);
179 void abortNonAvail();
181 void FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
183 enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
200 iDVBChannel *channel;
201 uniqueEPGKey service;
212 Message(int type, bool b)
213 :type(type), avail(b) {}
214 Message(int type, iDVBChannel *channel, int err=0)
215 :type(type), channel(channel), err(err) {}
216 Message(int type, const eServiceReference& service, int err=0)
217 :type(type), service(service), err(err) {}
218 Message(int type, time_t time)
219 :type(type), time(time) {}
221 eFixedMessagePump<Message> messages;
223 friend class channel_data;
224 static eEPGCache *instance;
227 std::map<iDVBChannel*, channel_data*> m_knownChannels;
228 ePtr<eConnection> m_chanAddedConn;
231 updateMap channelLastUpdated;
232 static pthread_mutex_t cache_lock, channel_map_lock;
234 #ifdef ENABLE_PRIVATE_EPG
235 contentMaps content_time_tables;
238 void thread(); // thread function
240 // called from epgcache thread
243 #ifdef ENABLE_PRIVATE_EPG
244 void privateSectionRead(const uniqueEPGKey &, const __u8 *);
246 void sectionRead(const __u8 *data, int source, channel_data *channel);
247 void gotMessage(const Message &message);
248 void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
251 // called from main thread
253 void DVBChannelAdded(eDVBChannel*);
254 void DVBChannelStateChanged(iDVBChannel*);
255 void DVBChannelRunning(iDVBChannel *);
257 timeMap::iterator m_timemap_cursor, m_timemap_end;
258 int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
264 static eEPGCache *getInstance() { return instance; }
270 // called from main thread
272 inline void Unlock();
273 #ifdef ENABLE_PRIVATE_EPG
274 void PMTready(eDVBServicePMTHandler *pmthandler);
276 void PMTready(eDVBServicePMTHandler *pmthandler) {}
279 // at moment just for one service..
280 RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
283 // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
284 // but its faster in use... its not allowed to delete this pointers via delete or free..
285 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
286 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT, int direction=0);
287 SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
289 // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
290 // its not allowed to delete this pointers via delete or free..
291 RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
292 RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
293 RESULT getNextTimeEntry(const eit_event_struct *&);
295 // Event's are parsed epg events.. it's safe to use them after cache unlock
296 // after use this Events must be deleted (memleaks)
297 RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
298 RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
299 RESULT getNextTimeEntry(Event *&);
302 SIMILAR_BROADCASTINGS_SEARCH,
310 PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
311 PyObject *search(PyObject *);
313 // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
314 // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
315 SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
316 SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
317 SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
321 inline void eEPGCache::Lock()
323 pthread_mutex_lock(&cache_lock);
326 inline void eEPGCache::Unlock()
328 pthread_mutex_unlock(&cache_lock);