lib/dvb/frontend.cpp: DTV frontend api support is now working (DVB-S2 is also working...
[enigma2.git] / 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                 bool m_MHWTimeoutet;
206                 void MHWTimeout() { m_MHWTimeoutet=true; }
207                 void readMHWData(const __u8 *data);
208                 void readMHWData2(const __u8 *data);
209                 void startMHWReader(__u16 pid, __u8 tid);
210                 void startMHWReader2(__u16 pid, __u8 tid, int ext=-1);
211                 void startTimeout(int msek);
212                 bool checkTimeout() { return m_MHWTimeoutet; }
213                 void cleanup();
214                 __u8 *delimitName( __u8 *in, __u8 *out, int len_in );
215                 void timeMHW2DVB( u_char hours, u_char minutes, u_char *return_time);
216                 void timeMHW2DVB( int minutes, u_char *return_time);
217                 void timeMHW2DVB( u_char day, u_char hours, u_char minutes, u_char *return_time);
218                 void storeTitle(std::map<__u32, mhw_title_t>::iterator itTitle, std::string sumText, const __u8 *data);
219 #endif
220                 void readData(const __u8 *data);
221                 void readDataViasat(const __u8 *data);
222                 void startChannel();
223                 void startEPG();
224                 bool finishEPG();
225                 void abortEPG();
226                 void abortNonAvail();
227         };
228         bool FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
229 public:
230         enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4
231 #ifdef ENABLE_MHW_EPG
232         ,MHW=8
233 #endif
234         ,VIASAT=16
235         };
236         struct Message
237         {
238                 enum
239                 {
240                         flush,
241                         startChannel,
242                         leaveChannel,
243                         quit,
244                         got_private_pid,
245                         timeChanged
246                 };
247                 int type;
248                 iDVBChannel *channel;
249                 uniqueEPGKey service;
250                 union {
251                         int err;
252                         time_t time;
253                         bool avail;
254                         int pid;
255                 };
256                 Message()
257                         :type(0), time(0) {}
258                 Message(int type)
259                         :type(type) {}
260                 Message(int type, bool b)
261                         :type(type), avail(b) {}
262                 Message(int type, iDVBChannel *channel, int err=0)
263                         :type(type), channel(channel), err(err) {}
264                 Message(int type, const eServiceReference& service, int err=0)
265                         :type(type), service(service), err(err) {}
266                 Message(int type, time_t time)
267                         :type(type), time(time) {}
268         };
269         eFixedMessagePump<Message> messages;
270 private:
271         friend class channel_data;
272         static eEPGCache *instance;
273
274         ePtr<eTimer> cleanTimer;
275         std::map<iDVBChannel*, channel_data*> m_knownChannels;
276         ePtr<eConnection> m_chanAddedConn;
277
278         eventCache eventDB;
279         updateMap channelLastUpdated;
280         static pthread_mutex_t cache_lock, channel_map_lock;
281
282 #ifdef ENABLE_PRIVATE_EPG
283         contentMaps content_time_tables;
284 #endif
285
286         void thread();  // thread function
287
288 // called from epgcache thread
289         void save();
290         void load();
291 #ifdef ENABLE_PRIVATE_EPG
292         void privateSectionRead(const uniqueEPGKey &, const __u8 *);
293 #endif
294         void sectionRead(const __u8 *data, int source, channel_data *channel);
295         void gotMessage(const Message &message);
296         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
297         void cleanLoop();
298
299 // called from main thread
300         void timeUpdated();
301         void DVBChannelAdded(eDVBChannel*);
302         void DVBChannelStateChanged(iDVBChannel*);
303         void DVBChannelRunning(iDVBChannel *);
304
305         timeMap::iterator m_timemap_cursor, m_timemap_end;
306         int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
307 #else
308         eEPGCache();
309         ~eEPGCache();
310 #endif // SWIG
311 public:
312         static eEPGCache *getInstance() { return instance; }
313 #ifndef SWIG
314         eEPGCache();
315         ~eEPGCache();
316
317 #ifdef ENABLE_PRIVATE_EPG
318         void PMTready(eDVBServicePMTHandler *pmthandler);
319 #else
320         void PMTready(eDVBServicePMTHandler *pmthandler) {}
321 #endif
322
323 #endif
324         // called from main thread
325         inline void Lock();
326         inline void Unlock();
327
328         // at moment just for one service..
329         RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
330
331 #ifndef SWIG
332         // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
333         // but its faster in use... its not allowed to delete this pointers via delete or free..
334         RESULT lookupEventId(const eServiceReference &service, int event_id, const eventData *&);
335         RESULT lookupEventTime(const eServiceReference &service, time_t, const eventData *&, int direction=0);
336         RESULT getNextTimeEntry(const eventData *&);
337
338         // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
339         // its not allowed to delete this pointers via delete or free..
340         RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
341         RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
342         RESULT getNextTimeEntry(const eit_event_struct *&);
343
344         // Event's are parsed epg events.. it's safe to use them after cache unlock
345         // after use this Events must be deleted (memleaks)
346         RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
347         RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
348         RESULT getNextTimeEntry(Event *&);
349 #endif
350         enum {
351                 SIMILAR_BROADCASTINGS_SEARCH,
352                 EXAKT_TITLE_SEARCH,
353                 PARTIAL_TITLE_SEARCH
354         };
355         enum {
356                 CASE_CHECK,
357                 NO_CASE_CHECK
358         };
359         PyObject *lookupEvent(SWIG_PYOBJECT(ePyObject) list, SWIG_PYOBJECT(ePyObject) convertFunc=(PyObject*)0);
360         PyObject *search(SWIG_PYOBJECT(ePyObject));
361
362         // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
363         // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
364         SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
365         SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
366         SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
367 };
368
369 #ifndef SWIG
370 inline void eEPGCache::Lock()
371 {
372         pthread_mutex_lock(&cache_lock);
373 }
374
375 inline void eEPGCache::Unlock()
376 {
377         pthread_mutex_unlock(&cache_lock);
378 }
379 #endif
380
381 #endif