code cleanup
[enigma2.git] / lib / dvb / epgcache.h
1 #ifndef __epgcache_h_
2 #define __epgcache_h_
3
4 #define ENABLE_PRIVATE_EPG 1
5
6 #ifndef SWIG
7
8 #include <vector>
9 #include <list>
10 #include <ext/hash_map>
11 #include <ext/hash_set>
12
13 #include <errno.h>
14
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>
25
26 #define CLEAN_INTERVAL 60000    //  1 min
27 #define UPDATE_INTERVAL 3600000  // 60 min
28 #define ZAP_DELAY 2000          // 2 sek
29
30 #define HILO(x) (x##_hi << 8 | x##_lo)
31
32 class eventData;
33 class eServiceReferenceDVB;
34 class eDVBServicePMTHandler;
35
36 struct uniqueEPGKey
37 {
38         int sid, onid, tsid;
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 )
43         {
44         }
45         uniqueEPGKey()
46                 :sid(-1), onid(-1), tsid(-1)
47         {
48         }
49         uniqueEPGKey( int sid, int onid, int tsid )
50                 :sid(sid), onid(onid), tsid(tsid)
51         {
52         }
53         bool operator <(const uniqueEPGKey &a) const
54         {
55                 return memcmp( &sid, &a.sid, sizeof(int)*3)<0;
56         }
57         operator bool() const
58         { 
59                 return !(sid == -1 && onid == -1 && tsid == -1); 
60         }
61         bool operator==(const uniqueEPGKey &a) const
62         {
63                 return !memcmp( &sid, &a.sid, sizeof(int)*3);
64         }
65         struct equal
66         {
67                 bool operator()(const uniqueEPGKey &a, const uniqueEPGKey &b) const
68                 {
69                         return !memcmp( &a.sid, &b.sid, sizeof(int)*3);
70                 }
71         };
72 };
73
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*>
78
79 #define channelMapIterator std::map<iDVBChannel*, channel_data*>::iterator
80 #define updateMap std::map<eDVBChannelID, time_t>
81
82 struct hash_uniqueEPGKey
83 {
84         inline size_t operator()( const uniqueEPGKey &x) const
85         {
86                 return (x.onid << 16) | x.tsid;
87         }
88 };
89
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 >
97         #endif
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>
104         #endif
105 #endif
106
107 #define descriptorPair std::pair<int,__u8*>
108 #define descriptorMap std::map<__u32, descriptorPair >
109
110 class eventData
111 {
112         friend class eEPGCache;
113 private:
114         __u8* EITdata;
115         __u8 ByteSize;
116         __u8 type;
117         static descriptorMap descriptors;
118         static __u8 data[4108];
119         static int CacheSize;
120         static void load(FILE *);
121         static void save(FILE *);
122 public:
123         eventData(const eit_event_struct* e=NULL, int size=0, int type=0);
124         ~eventData();
125         const eit_event_struct* get() const;
126         operator const eit_event_struct*() const
127         {
128                 return get();
129         }
130         int getEventID()
131         {
132                 return (EITdata[0] << 8) | EITdata[1];
133         }
134         time_t getStartTime()
135         {
136                 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
137         }
138         int getDuration()
139         {
140                 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
141         }
142 };
143 #endif
144
145 class eEPGCache: public eMainloop, private eThread, public Object
146 {
147 #ifndef SWIG
148         DECLARE_REF(eEPGCache)
149         struct channel_data: public Object
150         {
151                 channel_data(eEPGCache*);
152                 eEPGCache *cache;
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;
162                 int m_PrevVersion;
163                 int m_PrivatePid;
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; }
171 #else
172                 bool canDelete() { return !isRunning; }
173 #endif
174                 void readData(const __u8 *data);
175                 void startChannel();
176                 void startEPG();
177                 bool finishEPG();
178                 void abortEPG();
179                 void abortNonAvail();
180         };
181         void FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service);
182 public:
183         enum {PRIVATE=0, NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
184         struct Message
185         {
186                 enum
187                 {
188                         flush,
189                         startChannel,
190                         leaveChannel,
191                         pause,
192                         restart,
193                         updated,
194                         isavail,
195                         quit,
196                         got_private_pid,
197                         timeChanged
198                 };
199                 int type;
200                 iDVBChannel *channel;
201                 uniqueEPGKey service;
202                 union {
203                         int err;
204                         time_t time;
205                         bool avail;
206                         int pid;
207                 };
208                 Message()
209                         :type(0), time(0) {}
210                 Message(int type)
211                         :type(type) {}
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) {}
220         };
221         eFixedMessagePump<Message> messages;
222 private:
223         friend class channel_data;
224         static eEPGCache *instance;
225
226         eTimer cleanTimer;
227         std::map<iDVBChannel*, channel_data*> m_knownChannels;
228         ePtr<eConnection> m_chanAddedConn;
229
230         eventCache eventDB;
231         updateMap channelLastUpdated;
232         static pthread_mutex_t cache_lock, channel_map_lock;
233
234 #ifdef ENABLE_PRIVATE_EPG
235         contentMaps content_time_tables;
236 #endif
237
238         void thread();  // thread function
239
240 // called from epgcache thread
241         void save();
242         void load();
243 #ifdef ENABLE_PRIVATE_EPG
244         void privateSectionRead(const uniqueEPGKey &, const __u8 *);
245 #endif
246         void sectionRead(const __u8 *data, int source, channel_data *channel);
247         void gotMessage(const Message &message);
248         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
249         void cleanLoop();
250
251 // called from main thread
252         void timeUpdated();
253         void DVBChannelAdded(eDVBChannel*);
254         void DVBChannelStateChanged(iDVBChannel*);
255         void DVBChannelRunning(iDVBChannel *);
256
257         timeMap::iterator m_timemap_cursor, m_timemap_end;
258         int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
259 #else
260         eEPGCache();
261         ~eEPGCache();
262 #endif // SWIG
263 public:
264         static eEPGCache *getInstance() { return instance; }
265 #ifndef SWIG
266         eEPGCache();
267         ~eEPGCache();
268 #endif
269
270         // called from main thread
271         inline void Lock();
272         inline void Unlock();
273 #ifdef ENABLE_PRIVATE_EPG
274         void PMTready(eDVBServicePMTHandler *pmthandler);
275 #else
276         void PMTready(eDVBServicePMTHandler *pmthandler) {}
277 #endif
278
279         // at moment just for one service..
280         RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
281
282 #ifndef SWIG
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);
288
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 *&);
294
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 *&);
300 #endif
301         enum {
302                 SIMILAR_BROADCASTINGS_SEARCH,
303                 EXAKT_TITLE_SEARCH,
304                 PARTIAL_TITLE_SEARCH
305         };
306         enum {
307                 CASE_CHECK,
308                 NO_CASE_CHECK
309         };
310         PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
311         PyObject *search(PyObject *);
312
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);
318 };
319
320 #ifndef SWIG
321 inline void eEPGCache::Lock()
322 {
323         pthread_mutex_lock(&cache_lock);
324 }
325
326 inline void eEPGCache::Unlock()
327 {
328         pthread_mutex_unlock(&cache_lock);
329 }
330 #endif
331
332 #endif