Merge branch 'master' of git.opendreambox.org:/git/enigma2
[vuplus_dvbapp] / lib / dvb / epgcache.h
1 #ifndef __epgcache_h_
2 #define __epgcache_h_
3
4 #define ENABLE_PRIVATE_EPG 1
5 //#define ENABLE_MHW_EPG 1
6
7 #ifndef SWIG
8
9 #include <vector>
10 #include <list>
11 #include <ext/hash_map>
12 #include <ext/hash_set>
13
14 #include <errno.h>
15
16 #include <lib/dvb/eit.h>
17 #include <lib/dvb/lowlevel/eit.h>
18 #ifdef ENABLE_MHW_EPG
19 #include <lib/dvb/lowlevel/mhw.h>
20 #endif
21 #include <lib/dvb/idvb.h>
22 #include <lib/dvb/demux.h>
23 #include <lib/dvb/dvbtime.h>
24 #include <lib/base/ebase.h>
25 #include <lib/base/thread.h>
26 #include <lib/base/message.h>
27 #include <lib/service/event.h>
28 #include <lib/python/python.h>
29
30 #define CLEAN_INTERVAL 60000    //  1 min
31 #define UPDATE_INTERVAL 3600000  // 60 min
32 #define ZAP_DELAY 2000          // 2 sek
33
34 #define HILO(x) (x##_hi << 8 | x##_lo)
35
36 class eventData;
37 class eServiceReferenceDVB;
38 class eDVBServicePMTHandler;
39
40 struct uniqueEPGKey
41 {
42         int sid, onid, tsid;
43         uniqueEPGKey( const eServiceReference &ref )
44                 :sid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getServiceID().get() : -1 )
45                 ,onid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getOriginalNetworkID().get() : -1 )
46                 ,tsid( ref.type != eServiceReference::idInvalid ? ((eServiceReferenceDVB&)ref).getTransportStreamID().get() : -1 )
47         {
48         }
49         uniqueEPGKey()
50                 :sid(-1), onid(-1), tsid(-1)
51         {
52         }
53         uniqueEPGKey( int sid, int onid, int tsid )
54                 :sid(sid), onid(onid), tsid(tsid)
55         {
56         }
57         bool operator <(const uniqueEPGKey &a) const
58         {
59                 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
60         }
61         operator bool() const
62         { 
63                 return !(sid == -1 && onid == -1 && tsid == -1); 
64         }
65         bool operator==(const uniqueEPGKey &a) const
66         {
67                 return !memcmp( &sid, &a.sid, sizeof(int)*3);
68         }
69         struct equal
70         {
71                 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
72                 {
73                         return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
74                 }
75         };
76 };
77
78 //eventMap is sorted by event_id
79 #define eventMap std::map<__u16, eventData*>
80 //timeMap is sorted by beginTime
81 #define timeMap std::map<time_t, eventData*>
82
83 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
84 #define updateMap std::map<eDVBChannelID, time_t>
85
86 struct hash_uniqueEPGKey
87 {
88         inline size_t operator()( const uniqueEPGKey &x) const
89         {
90                 return (x.onid << 16) | x.tsid;
91         }
92 };
93
94 #define tidMap std::set<__u32>
95 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 )  // check if gcc version >= 3.1
96         #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
97         #ifdef ENABLE_PRIVATE_EPG
98                 #define contentTimeMap __gnu_cxx::hash_map<time_t, std::pair<time_t, __u16> >
99                 #define contentMap __gnu_cxx::hash_map<int, contentTimeMap >
100                 #define contentMaps __gnu_cxx::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal >
101         #endif
102 #else // for older gcc use following
103         #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
104         #ifdef ENABLE_PRIVATE_EPG
105                 #define contentTimeMap std::hash_map<time_t, std::pair<time_t, __u16> >
106                 #define contentMap std::hash_map<int, contentTimeMap >
107                 #define contentMaps std::hash_map<uniqueEPGKey, contentMap, hash_uniqueEPGKey, uniqueEPGKey::equal>
108         #endif
109 #endif
110
111 #define descriptorPair std::pair<int,__u8*>
112 #define descriptorMap std::map<__u32, descriptorPair >
113
114 class eventData
115 {
116         friend class eEPGCache;
117 private:
118         __u8* EITdata;
119         __u8 ByteSize;
120         __u8 type;
121         static descriptorMap descriptors;
122         static __u8 data[4108];
123         static int CacheSize;
124         static void load(FILE *);
125         static void save(FILE *);
126 public:
127         eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
128         ~eventData();
129         const eit_event_struct* get() const;
130         operator const eit_event_struct*() const
131         {
132                 return get();
133         }
134         int getEventID()
135         {
136                 return (EITdata[0] << 8) | EITdata[1];
137         }
138         time_t getStartTime()
139         {
140                 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
141         }
142         int getDuration()
143         {
144                 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
145         }
146 };
147 #endif
148
149 class eEPGCache: public eMainloop, private eThread, public Object
150 {
151 #ifndef SWIG
152         DECLARE_REF(eEPGCache)
153         struct channel_data: public Object
154         {
155                 pthread_mutex_t channel_active;
156                 channel_data(eEPGCache*);
157                 eEPGCache *cache;
158                 ePtr<eTimer> abortTimer, zapTimer;
159                 int prevChannelState;
160                 __u8 state, isRunning, haveData;
161                 ePtr<eDVBChannel> channel;
162                 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn, m_ViasatConn;
163                 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader, m_ViasatReader;
164                 tidMap seenSections[4], calcedSections[4];
165 #ifdef ENABLE_PRIVATE_EPG
166                 ePtr<eTimer> startPrivateTimer;
167                 int m_PrevVersion;
168                 int m_PrivatePid;
169                 uniqueEPGKey m_PrivateService;
170                 ePtr<eConnection> m_PrivateConn;
171                 ePtr<iDVBSectionReader> m_PrivateReader;
172                 std::set<__u8> seenPrivateSections;
173                 void readPrivateData(const __u8 *data);
174                 void startPrivateReader();
175 #endif
176 #ifdef ENABLE_MHW_EPG
177                 std::vector<mhw_channel_name_t> m_channels;
178                 std::map<__u8, mhw_theme_name_t> m_themes;
179                 std::map<__u32, mhw_title_t> m_titles;
180                 std::multimap<__u32, __u32> m_program_ids;
181                 ePtr<eConnection> m_MHWConn, m_MHWConn2;
182                 ePtr<iDVBSectionReader> m_MHWReader, m_MHWReader2;
183                 eDVBSectionFilterMask m_MHWFilterMask, m_MHWFilterMask2;
184                 ePtr<eTimer> m_MHWTimeoutTimer;
185                 bool m_MHWTimeoutet;
186                 void MHWTimeout() { m_MHWTimeoutet=true; }
187                 void readMHWData(const __u8 *data);
188                 void readMHWData2(const __u8 *data);
189                 void startMHWReader(__u16 pid, __u8 tid);
190                 void startMHWReader2(__u16 pid, __u8 tid, int ext=-1);
191                 void startTimeout(int msek);
192                 bool checkTimeout() { return m_MHWTimeoutet; }
193                 void cleanup();
194                 __u8 *delimitName( __u8 *in, __u8 *out, int len_in );
195                 void timeMHW2DVB( u_char hours, u_char minutes, u_char *return_time);
196                 void timeMHW2DVB( int minutes, u_char *return_time);
197                 void timeMHW2DVB( u_char day, u_char hours, u_char minutes, u_char *return_time);
198                 void storeTitle(std::map<__u32, mhw_title_t>::iterator itTitle, std::string sumText, const __u8 *data);
199 #endif
200                 void readData(const __u8 *data);
201                 void readDataViasat(const __u8 *data);
202                 void startChannel();
203                 void startEPG();
204                 bool finishEPG();
205                 void abortEPG();
206                 void abortNonAvail();
207         };
208         bool FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
209 public:
210         enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4
211 #ifdef ENABLE_MHW_EPG
212         ,MHW=8
213 #endif
214         ,VIASAT=16
215         };
216         struct Message
217         {
218                 enum
219                 {
220                         flush,
221                         startChannel,
222                         leaveChannel,
223                         quit,
224                         got_private_pid,
225                         timeChanged
226                 };
227                 int type;
228                 iDVBChannel *channel;
229                 uniqueEPGKey service;
230                 union {
231                         int err;
232                         time_t time;
233                         bool avail;
234                         int pid;
235                 };
236                 Message()
237                         :type(0), time(0) {}
238                 Message(int type)
239                         :type(type) {}
240                 Message(int type, bool b)
241                         :type(type), avail(b) {}
242                 Message(int type, iDVBChannel *channel, int err=0)
243                         :type(type), channel(channel), err(err) {}
244                 Message(int type, const eServiceReference& service, int err=0)
245                         :type(type), service(service), err(err) {}
246                 Message(int type, time_t time)
247                         :type(type), time(time) {}
248         };
249         eFixedMessagePump<Message> messages;
250 private:
251         friend class channel_data;
252         static eEPGCache *instance;
253
254         ePtr<eTimer> cleanTimer;
255         std::map<iDVBChannel*, channel_data*> m_knownChannels;
256         ePtr<eConnection> m_chanAddedConn;
257
258         eventCache eventDB;
259         updateMap channelLastUpdated;
260         static pthread_mutex_t cache_lock, channel_map_lock;
261
262 #ifdef ENABLE_PRIVATE_EPG
263         contentMaps content_time_tables;
264 #endif
265
266         void thread();  // thread function
267
268 // called from epgcache thread
269         void save();
270         void load();
271 #ifdef ENABLE_PRIVATE_EPG
272         void privateSectionRead(const uniqueEPGKey &, const __u8 *);
273 #endif
274         void sectionRead(const __u8 *data, int source, channel_data *channel);
275         void gotMessage(const Message &message);
276         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
277         void cleanLoop();
278
279 // called from main thread
280         void timeUpdated();
281         void DVBChannelAdded(eDVBChannel*);
282         void DVBChannelStateChanged(iDVBChannel*);
283         void DVBChannelRunning(iDVBChannel *);
284
285         timeMap::iterator m_timemap_cursor, m_timemap_end;
286         int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
287 #else
288         eEPGCache();
289         ~eEPGCache();
290 #endif // SWIG
291 public:
292         static eEPGCache *getInstance() { return instance; }
293 #ifndef SWIG
294         eEPGCache();
295         ~eEPGCache();
296
297 #ifdef ENABLE_PRIVATE_EPG
298         void PMTready(eDVBServicePMTHandler *pmthandler);
299 #else
300         void PMTready(eDVBServicePMTHandler *pmthandler) {}
301 #endif
302
303 #endif
304         // called from main thread
305         inline void Lock();
306         inline void Unlock();
307
308         // at moment just for one service..
309         RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
310
311 #ifndef SWIG
312         // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
313         // but its faster in use... its not allowed to delete this pointers via delete or free..
314         RESULT lookupEventId(const eServiceReference &service, int event_id, const eventData *&);
315         RESULT lookupEventTime(const eServiceReference &service, time_t, const eventData *&, int direction=0);
316         RESULT getNextTimeEntry(const eventData *&);
317
318         // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
319         // its not allowed to delete this pointers via delete or free..
320         RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
321         RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
322         RESULT getNextTimeEntry(const eit_event_struct *&);
323
324         // Event's are parsed epg events.. it's safe to use them after cache unlock
325         // after use this Events must be deleted (memleaks)
326         RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
327         RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
328         RESULT getNextTimeEntry(Event *&);
329 #endif
330         enum {
331                 SIMILAR_BROADCASTINGS_SEARCH,
332                 EXAKT_TITLE_SEARCH,
333                 PARTIAL_TITLE_SEARCH
334         };
335         enum {
336                 CASE_CHECK,
337                 NO_CASE_CHECK
338         };
339         PyObject *lookupEvent(SWIG_PYOBJECT(ePyObject) list, SWIG_PYOBJECT(ePyObject) convertFunc=(PyObject*)0);
340         PyObject *search(SWIG_PYOBJECT(ePyObject));
341
342         // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
343         // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
344         SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
345         SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
346         SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
347 };
348
349 #ifndef SWIG
350 inline void eEPGCache::Lock()
351 {
352         pthread_mutex_lock(&cache_lock);
353 }
354
355 inline void eEPGCache::Unlock()
356 {
357         pthread_mutex_unlock(&cache_lock);
358 }
359 #endif
360
361 #endif