Merge remote branch 'origin/bug_135_dvdburn_ts_only'
[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 /* 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))
13 #else
14 #define __GNUC_PREREQ(maj, min)  0
15 #endif
16
17 #include <vector>
18 #include <list>
19 #if 0 && __GNUC_PREREQ(4,3)
20 #include <unordered_map>
21 #include <unordered_set>
22 #else
23 #include <ext/hash_map>
24 #include <ext/hash_set>
25 #endif
26
27 #include <errno.h>
28
29 #include <lib/dvb/eit.h>
30 #include <lib/dvb/lowlevel/eit.h>
31 #ifdef ENABLE_MHW_EPG
32 #include <lib/dvb/lowlevel/mhw.h>
33 #endif
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>
42
43 #define CLEAN_INTERVAL 60000    //  1 min
44 #define UPDATE_INTERVAL 3600000  // 60 min
45 #define ZAP_DELAY 2000          // 2 sek
46
47 #define HILO(x) (x##_hi << 8 | x##_lo)
48
49 class eventData;
50 class eServiceReferenceDVB;
51 class eDVBServicePMTHandler;
52
53 struct uniqueEPGKey
54 {
55         int sid, onid, tsid;
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 )
60         {
61         }
62         uniqueEPGKey()
63                 :sid(-1), onid(-1), tsid(-1)
64         {
65         }
66         uniqueEPGKey( int sid, int onid, int tsid )
67                 :sid(sid), onid(onid), tsid(tsid)
68         {
69         }
70         bool operator <(const uniqueEPGKey &a) const
71         {
72                 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
73         }
74         operator bool() const
75         { 
76                 return !(sid == -1 && onid == -1 && tsid == -1); 
77         }
78         bool operator==(const uniqueEPGKey &a) const
79         {
80                 return !memcmp( &sid, &a.sid, sizeof(int)*3);
81         }
82         struct equal
83         {
84                 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
85                 {
86                         return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
87                 }
88         };
89 };
90
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*>
95
96 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
97 #define updateMap std::map<eDVBChannelID, time_t>
98
99 struct hash_uniqueEPGKey
100 {
101         inline size_t operator()( const uniqueEPGKey &x) const
102         {
103                 return (x.onid << 16) | x.tsid;
104         }
105 };
106
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 >
114         #endif
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 >
121         #endif
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>
128         #endif
129 #endif
130
131 #define descriptorPair std::pair<int,__u8*>
132 #define descriptorMap std::map<__u32, descriptorPair >
133
134 class eventData
135 {
136         friend class eEPGCache;
137 private:
138         __u8* EITdata;
139         __u8 ByteSize;
140         __u8 type;
141         static descriptorMap descriptors;
142         static __u8 data[4108];
143         static int CacheSize;
144         static void load(FILE *);
145         static void save(FILE *);
146 public:
147         eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
148         ~eventData();
149         const eit_event_struct* get() const;
150         operator const eit_event_struct*() const
151         {
152                 return get();
153         }
154         int getEventID()
155         {
156                 return (EITdata[0] << 8) | EITdata[1];
157         }
158         time_t getStartTime()
159         {
160                 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
161         }
162         int getDuration()
163         {
164                 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
165         }
166 };
167 #endif
168
169 class eEPGCache: public eMainloop, private eThread, public Object
170 {
171 #ifndef SWIG
172         DECLARE_REF(eEPGCache)
173         struct channel_data: public Object
174         {
175                 pthread_mutex_t channel_active;
176                 channel_data(eEPGCache*);
177                 eEPGCache *cache;
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;
187                 int m_PrevVersion;
188                 int m_PrivatePid;
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();
195 #endif
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;
206                 bool m_MHWTimeoutet;
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; }
214                 void cleanup();
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);
220 #endif
221                 void readData(const __u8 *data);
222                 void readDataViasat(const __u8 *data);
223                 void startChannel();
224                 void startEPG();
225                 bool finishEPG();
226                 void abortEPG();
227                 void abortNonAvail();
228         };
229         bool FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
230 public:
231         enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4
232 #ifdef ENABLE_MHW_EPG
233         ,MHW=8
234 #endif
235         ,VIASAT=16
236         };
237         struct Message
238         {
239                 enum
240                 {
241                         flush,
242                         startChannel,
243                         leaveChannel,
244                         quit,
245                         got_private_pid,
246                         got_mhw2_channel_pid,
247                         got_mhw2_title_pid,
248                         got_mhw2_summary_pid,
249                         timeChanged
250                 };
251                 int type;
252                 iDVBChannel *channel;
253                 uniqueEPGKey service;
254                 union {
255                         int err;
256                         time_t time;
257                         bool avail;
258                         int pid;
259                 };
260                 Message()
261                         :type(0), time(0) {}
262                 Message(int type)
263                         :type(type) {}
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) {}
272         };
273         eFixedMessagePump<Message> messages;
274 private:
275         friend class channel_data;
276         static eEPGCache *instance;
277
278         ePtr<eTimer> cleanTimer;
279         std::map<iDVBChannel*, channel_data*> m_knownChannels;
280         ePtr<eConnection> m_chanAddedConn;
281
282         eventCache eventDB;
283         updateMap channelLastUpdated;
284         static pthread_mutex_t cache_lock, channel_map_lock;
285
286 #ifdef ENABLE_PRIVATE_EPG
287         contentMaps content_time_tables;
288 #endif
289
290         void thread();  // thread function
291
292 // called from epgcache thread
293         void save();
294         void load();
295 #ifdef ENABLE_PRIVATE_EPG
296         void privateSectionRead(const uniqueEPGKey &, const __u8 *);
297 #endif
298         void sectionRead(const __u8 *data, int source, channel_data *channel);
299         void gotMessage(const Message &message);
300         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
301         void cleanLoop();
302
303 // called from main thread
304         void timeUpdated();
305         void DVBChannelAdded(eDVBChannel*);
306         void DVBChannelStateChanged(iDVBChannel*);
307         void DVBChannelRunning(iDVBChannel *);
308
309         timeMap::iterator m_timemap_cursor, m_timemap_end;
310         int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
311 #else
312         eEPGCache();
313         ~eEPGCache();
314 #endif // SWIG
315 public:
316         static eEPGCache *getInstance() { return instance; }
317 #ifndef SWIG
318         eEPGCache();
319         ~eEPGCache();
320
321 #ifdef ENABLE_PRIVATE_EPG
322         void PMTready(eDVBServicePMTHandler *pmthandler);
323 #else
324         void PMTready(eDVBServicePMTHandler *pmthandler) {}
325 #endif
326
327 #endif
328         // called from main thread
329         inline void Lock();
330         inline void Unlock();
331
332         // at moment just for one service..
333         RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
334
335 #ifndef SWIG
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 *&);
341
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 *&);
347
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 *&);
353 #endif
354         enum {
355                 SIMILAR_BROADCASTINGS_SEARCH,
356                 EXAKT_TITLE_SEARCH,
357                 PARTIAL_TITLE_SEARCH
358         };
359         enum {
360                 CASE_CHECK,
361                 NO_CASE_CHECK
362         };
363         PyObject *lookupEvent(SWIG_PYOBJECT(ePyObject) list, SWIG_PYOBJECT(ePyObject) convertFunc=(PyObject*)0);
364         PyObject *search(SWIG_PYOBJECT(ePyObject));
365
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);
371 };
372
373 #ifndef SWIG
374 inline void eEPGCache::Lock()
375 {
376         pthread_mutex_lock(&cache_lock);
377 }
378
379 inline void eEPGCache::Unlock()
380 {
381         pthread_mutex_unlock(&cache_lock);
382 }
383 #endif
384
385 #endif