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