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