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