small private epg fix
[vuplus_dvbapp] / 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                 eTimer startPrivateTimer;
163                 int m_PrevVersion;
164                 int m_PrivatePid;
165                 uniqueEPGKey m_PrivateService;
166                 ePtr<eConnection> m_PrivateConn;
167                 ePtr<iDVBSectionReader> m_PrivateReader;
168                 std::set<__u8> seenPrivateSections;
169                 void readPrivateData(const __u8 *data);
170                 void startPrivateReader();
171 #endif
172                 void readData(const __u8 *data);
173                 void startChannel();
174                 void startEPG();
175                 bool finishEPG();
176                 void abortEPG();
177                 void abortNonAvail();
178         };
179 public:
180         enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
181         struct Message
182         {
183                 enum
184                 {
185                         flush,
186                         startChannel,
187                         leaveChannel,
188                         pause,
189                         restart,
190                         updated,
191                         isavail,
192                         quit,
193                         got_private_pid,
194                         timeChanged
195                 };
196                 int type;
197                 iDVBChannel *channel;
198                 uniqueEPGKey service;
199                 union {
200                         int err;
201                         time_t time;
202                         bool avail;
203                         int pid;
204                 };
205                 Message()
206                         :type(0), time(0) {}
207                 Message(int type)
208                         :type(type) {}
209                 Message(int type, bool b)
210                         :type(type), avail(b) {}
211                 Message(int type, iDVBChannel *channel, int err=0)
212                         :type(type), channel(channel), err(err) {}
213                 Message(int type, const eServiceReference& service, int err=0)
214                         :type(type), service(service), err(err) {}
215                 Message(int type, time_t time)
216                         :type(type), time(time) {}
217         };
218         eFixedMessagePump<Message> messages;
219 private:
220         friend class channel_data;
221         static eEPGCache *instance;
222
223         eTimer cleanTimer;
224         std::map<iDVBChannel*, channel_data*> m_knownChannels;
225         ePtr<eConnection> m_chanAddedConn;
226
227         eventCache eventDB;
228         updateMap channelLastUpdated;
229         static pthread_mutex_t cache_lock, channel_map_lock;
230
231 #ifdef ENABLE_PRIVATE_EPG
232         contentMaps content_time_tables;
233 #endif
234
235         void thread();  // thread function
236
237 // called from epgcache thread
238         void save();
239         void load();
240 #ifdef ENABLE_PRIVATE_EPG
241         void privateSectionRead(const uniqueEPGKey &, const __u8 *);
242 #endif
243         void sectionRead(const __u8 *data, int source, channel_data *channel);
244         void gotMessage(const Message &message);
245         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
246         void cleanLoop();
247
248 // called from main thread
249         void timeUpdated();
250         void DVBChannelAdded(eDVBChannel*);
251         void DVBChannelStateChanged(iDVBChannel*);
252         void DVBChannelRunning(iDVBChannel *);
253
254         timeMap::iterator m_timemap_cursor, m_timemap_end;
255         int currentQueryTsidOnid; // needed for getNextTimeEntry.. only valid until next startTimeQuery call
256 #else
257         eEPGCache();
258         ~eEPGCache();
259 #endif // SWIG
260 public:
261         static eEPGCache *getInstance() { return instance; }
262 #ifndef SWIG
263         eEPGCache();
264         ~eEPGCache();
265 #endif
266
267         // called from main thread
268         inline void Lock();
269         inline void Unlock();
270 #ifdef ENABLE_PRIVATE_EPG
271         void PMTready(eDVBServicePMTHandler *pmthandler);
272 #else
273         void PMTready(eDVBServicePMTHandler *pmthandler) {}
274 #endif
275
276         // at moment just for one service..
277         RESULT startTimeQuery(const eServiceReference &service, time_t begin=-1, int minutes=-1);
278
279 #ifndef SWIG
280         // eventData's are plain entrys out of the cache.. it's not safe to use them after cache unlock
281         // but its faster in use... its not allowed to delete this pointers via delete or free..
282         SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, const eventData *&SWIG_OUTPUT);
283         SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, const eventData *&SWIG_OUTPUT, int direction=0);
284         SWIG_VOID(RESULT) getNextTimeEntry(const eventData *&SWIG_OUTPUT);
285
286         // eit_event_struct's are plain dvb eit_events .. it's not safe to use them after cache unlock
287         // its not allowed to delete this pointers via delete or free..
288         RESULT lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&);
289         RESULT lookupEventTime(const eServiceReference &service, time_t , const eit_event_struct *&, int direction=0);
290         RESULT getNextTimeEntry(const eit_event_struct *&);
291
292         // Event's are parsed epg events.. it's safe to use them after cache unlock
293         // after use this Events must be deleted (memleaks)
294         RESULT lookupEventId(const eServiceReference &service, int event_id, Event* &);
295         RESULT lookupEventTime(const eServiceReference &service, time_t, Event* &, int direction=0);
296         RESULT getNextTimeEntry(Event *&);
297 #endif
298         enum {
299                 SIMILAR_BROADCASTINGS_SEARCH,
300                 EXAKT_TITLE_SEARCH,
301                 PARTIAL_TITLE_SEARCH
302         };
303         enum {
304                 CASE_CHECK,
305                 NO_CASE_CHECK
306         };
307         PyObject *lookupEvent(PyObject *list, PyObject *convertFunc=NULL);
308         PyObject *search(PyObject *);
309
310         // eServiceEvent are parsed epg events.. it's safe to use them after cache unlock
311         // for use from python ( members: m_start_time, m_duration, m_short_description, m_extended_description )
312         SWIG_VOID(RESULT) lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &SWIG_OUTPUT);
313         SWIG_VOID(RESULT) lookupEventTime(const eServiceReference &service, time_t, ePtr<eServiceEvent> &SWIG_OUTPUT, int direction=0);
314         SWIG_VOID(RESULT) getNextTimeEntry(ePtr<eServiceEvent> &SWIG_OUTPUT);
315 };
316
317 #ifndef SWIG
318 inline void eEPGCache::Lock()
319 {
320         pthread_mutex_lock(&cache_lock);
321 }
322
323 inline void eEPGCache::Unlock()
324 {
325         pthread_mutex_unlock(&cache_lock);
326 }
327 #endif
328
329 #endif