optimize epg for less memory consumption,
[vuplus_dvbapp] / 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_32
76 {
77         inline size_t operator()( const __u32 &x) const
78         {
79                 return (x >> 8)&0xFFFF;
80         }
81 };
82
83 struct equal_32
84 {
85         inline size_t operator()(const __u32 &x, const __u32 &y) const
86         {
87                 return x == y;
88         }
89 };
90
91 struct hash_uniqueEPGKey
92 {
93         inline size_t operator()( const uniqueEPGKey &x) const
94         {
95                 return (x.onid << 16) | x.tsid;
96         }
97 };
98
99 #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ == 4 )  // check if gcc version >= 3.1
100         #define eventCache __gnu_cxx::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal>
101         #define tidMap __gnu_cxx::hash_set<__u32, hash_32, equal_32>
102 #else // for older gcc use following
103         #define eventCache std::hash_map<uniqueEPGKey, std::pair<eventMap, timeMap>, hash_uniqueEPGKey, uniqueEPGKey::equal >
104         #define tidMap std::hash_map<__u32, hash_32, equal_32>
105 #endif
106
107 #define descriptorPair std::pair<int,__u8*>
108 #define descriptorMap std::map<__u32, descriptorPair >
109
110 class eventData
111 {
112         friend class eEPGCache;
113 private:
114         __u8* EITdata;
115         int ByteSize;
116         static descriptorMap descriptors;
117 public:
118         int type;
119         static int CacheSize;
120         static void load(FILE *);
121         static void save(FILE *);
122         eventData(const eit_event_struct* e, int size, int type);
123         ~eventData();
124         const eit_event_struct* get() const;
125         operator const eit_event_struct*() const
126         {
127                 return get();
128         }
129         int getEventID()
130         {
131                 return (EITdata[0] << 8) | EITdata[1];
132         }
133         time_t getStartTime()
134         {
135                 return parseDVBtime(EITdata[2], EITdata[3], EITdata[4], EITdata[5], EITdata[6]);
136         }
137         int getDuration()
138         {
139                 return fromBCD(EITdata[7])*3600+fromBCD(EITdata[8])*60+fromBCD(EITdata[9]);
140         }
141 };
142
143 class eEPGCache: public eMainloop, private eThread, public Object
144 {
145         DECLARE_REF(eEPGCache)
146         struct channel_data: public Object
147         {
148                 channel_data(eEPGCache*);
149                 eEPGCache *cache;
150                 eTimer abortTimer, zapTimer;
151                 __u8 state, isRunning, haveData, can_delete;
152                 ePtr<eDVBChannel> channel;
153                 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
154                 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
155                 tidMap seenSections[3], calcedSections[3];
156                 void readData(const __u8 *data);
157                 void startChannel();
158                 void startEPG();
159                 bool finishEPG();
160                 void abortEPG();
161                 void abortNonAvail();
162         };
163 public:
164         enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
165         struct Message
166         {
167                 enum
168                 {
169                         flush,
170                         startChannel,
171                         leaveChannel,
172                         pause,
173                         restart,
174                         updated,
175                         isavail,
176                         quit,
177                         timeChanged
178                 };
179                 int type;
180                 iDVBChannel *channel;
181                 uniqueEPGKey service;
182                 union {
183                         int err;
184                         time_t time;
185                         bool avail;
186                 };
187                 Message()
188                         :type(0), time(0) {}
189                 Message(int type)
190                         :type(type) {}
191                 Message(int type, bool b)
192                         :type(type), avail(b) {}
193                 Message(int type, iDVBChannel *channel, int err=0)
194                         :type(type), channel(channel), err(err) {}
195                 Message(int type, const eServiceReferenceDVB& service, int err=0)
196                         :type(type), service(service), err(err) {}
197                 Message(int type, time_t time)
198                         :type(type), time(time) {}
199         };
200         eFixedMessagePump<Message> messages;
201 private:
202         friend class channel_data;
203         static eEPGCache *instance;
204
205         eTimer cleanTimer;
206         std::map<iDVBChannel*, channel_data*> m_knownChannels;
207         ePtr<eConnection> m_chanAddedConn;
208
209         eventCache eventDB;
210         updateMap channelLastUpdated;
211         static pthread_mutex_t cache_lock, channel_map_lock;
212
213         void thread();  // thread function
214
215 // called from epgcache thread
216         void save();
217         void load();
218         void sectionRead(const __u8 *data, int source, channel_data *channel);
219         void gotMessage(const Message &message);
220         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
221         void cleanLoop();
222
223 // called from main thread
224         void timeUpdated();
225         void DVBChannelAdded(eDVBChannel*);
226         void DVBChannelStateChanged(iDVBChannel*);
227         void DVBChannelRunning(iDVBChannel *);
228 public:
229         static RESULT getInstance(ePtr<eEPGCache> &ptr);
230         eEPGCache();
231         ~eEPGCache();
232
233         // called from main thread
234         inline void Lock();
235         inline void Unlock();
236
237         // result Event * must be deleted by caller of lookupEvent
238         inline RESULT lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& );
239         inline RESULT lookupEvent(const eServiceReferenceDVB &service, time_t , Event *& );
240
241         // methods for faster use.. but not thread save ..
242         // Lock and Unlock should be used !!
243         RESULT lookupEvent(const eServiceReferenceDVB &service, int event_id, const eventData *& );
244         RESULT lookupEvent(const eServiceReferenceDVB &service, time_t , const eventData *& );
245
246         inline RESULT getEventMap(const eServiceReferenceDVB &service, const eventMap *& );
247         inline RESULT getTimeMap(const eServiceReferenceDVB &service, const timeMap *& );
248 };
249
250 TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
251
252 inline RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, time_t t, Event *& result )
253 {
254         const eventData *data=0;
255         RESULT ret = lookupEvent(service, t, data);
256         if ( !ret && data )
257                 result = new Event((uint8_t*)data->get());
258         return ret;
259 }
260
261 inline RESULT eEPGCache::lookupEvent(const eServiceReferenceDVB &service, int event_id, Event *& result)
262 {
263         const eventData *data=0;
264         RESULT ret = lookupEvent(service, event_id, data);
265         if ( !ret && data )
266                 result = new Event((uint8_t*)data->get());
267         return ret;
268 }
269
270 inline RESULT eEPGCache::getEventMap(const eServiceReferenceDVB &service, const eventMap *& result)
271 {
272         eventCache::iterator It = eventDB.find( service );
273         if ( It != eventDB.end() && It->second.first.size() )
274         {
275                 result=&(It->second.first);
276                 return 0;
277         }
278         else
279                 result=0;
280         return -1;
281 }
282
283 inline RESULT eEPGCache::getTimeMap(const eServiceReferenceDVB &service, const timeMap *& result)
284 {
285         eventCache::iterator It = eventDB.find( service );
286         if ( It != eventDB.end() && It->second.second.size() )
287         {
288                 result=&(It->second.second);
289                 return 0;
290         }
291         else
292                 result=0;
293         return -1;
294 }
295
296 inline void eEPGCache::Lock()
297 {
298         pthread_mutex_lock(&cache_lock);
299 }
300
301 inline void eEPGCache::Unlock()
302 {
303         pthread_mutex_unlock(&cache_lock);
304 }
305
306 #endif