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