- factor out the filepusher from ts recorder
[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_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 class eventData
108 {
109         friend class eEPGCache;
110 private:
111         __u8* EITdata;
112         int ByteSize;
113 public:
114         int type;
115         static int CacheSize;
116         eventData(const eit_event_struct* e, int size, int type)
117         :ByteSize(size), type(type)
118         {
119                 CacheSize+=size;
120                 EITdata = new __u8[size];
121                 if (e)
122                         memcpy(EITdata, (__u8*) e, size);
123         }
124         ~eventData()
125         {
126                 CacheSize-=ByteSize;
127                 delete [] EITdata;
128         }
129         operator const eit_event_struct*() const
130         {
131                 return (const eit_event_struct*) EITdata;
132         }
133         const eit_event_struct* get() const
134         {
135                 return (const eit_event_struct*) EITdata;
136         }
137         int getEventID()
138         {
139                 return HILO( ((const eit_event_struct*) EITdata)->event_id );
140         }
141         time_t getStartTime()
142         {
143                 return parseDVBtime(
144                         EITdata[2], EITdata[3],
145                         EITdata[4], EITdata[5], EITdata[6]);
146         }
147 };
148
149 class eEPGCache: public eMainloop, private eThread, public Object
150 {
151         DECLARE_REF(eEPGCache)
152         struct channel_data: public Object
153         {
154                 channel_data(eEPGCache*);
155                 eEPGCache *cache;
156                 eTimer abortTimer, zapTimer;
157                 __u8 state, isRunning, haveData, can_delete;
158                 ePtr<eDVBChannel> channel;
159                 ePtr<eConnection> m_stateChangedConn, m_NowNextConn, m_ScheduleConn, m_ScheduleOtherConn;
160                 ePtr<iDVBSectionReader> m_NowNextReader, m_ScheduleReader, m_ScheduleOtherReader;
161                 tidMap seenSections, calcedSections;
162                 void readData(const __u8 *data);
163                 void startChannel();
164                 void startEPG();
165                 bool finishEPG();
166                 void abortEPG();
167                 void abortNonAvail();
168         };
169 public:
170         enum {NOWNEXT=1, SCHEDULE=2, SCHEDULE_OTHER=4};
171         struct Message
172         {
173                 enum
174                 {
175                         flush,
176                         startChannel,
177                         leaveChannel,
178                         pause,
179                         restart,
180                         updated,
181                         isavail,
182                         quit,
183                         timeChanged
184                 };
185                 int type;
186                 iDVBChannel *channel;
187                 uniqueEPGKey service;
188                 union {
189                         int err;
190                         time_t time;
191                         bool avail;
192                 };
193                 Message()
194                         :type(0), time(0) {}
195                 Message(int type)
196                         :type(type) {}
197                 Message(int type, bool b)
198                         :type(type), avail(b) {}
199                 Message(int type, iDVBChannel *channel, int err=0)
200                         :type(type), channel(channel), err(err) {}
201                 Message(int type, const eServiceReferenceDVB& service, int err=0)
202                         :type(type), service(service), err(err) {}
203                 Message(int type, time_t time)
204                         :type(type), time(time) {}
205         };
206         eFixedMessagePump<Message> messages;
207 private:
208         friend class channel_data;
209         static eEPGCache *instance;
210
211         eTimer cleanTimer;
212         std::map<iDVBChannel*, channel_data*> m_knownChannels;
213         ePtr<eConnection> m_chanAddedConn;
214
215         eventCache eventDB;
216         updateMap channelLastUpdated;
217         static pthread_mutex_t cache_lock, channel_map_lock;
218
219         void thread();  // thread function
220
221 // called from epgcache thread
222         void save();
223         void load();
224         void sectionRead(const __u8 *data, int source, channel_data *channel);
225         void gotMessage(const Message &message);
226         void flushEPG(const uniqueEPGKey & s=uniqueEPGKey());
227         void cleanLoop();
228
229 // called from main thread
230         void timeUpdated();
231         void DVBChannelAdded(eDVBChannel*);
232         void DVBChannelStateChanged(iDVBChannel*);
233         void DVBChannelRunning(iDVBChannel *);
234 public:
235         static RESULT getInstance(ePtr<eEPGCache> &ptr);
236         eEPGCache();
237         ~eEPGCache();
238
239         // called from main thread
240         inline void Lock();
241         inline void Unlock();
242         Event *lookupEvent(const eServiceReferenceDVB &service, int event_id, bool plain=false );
243         Event *lookupEvent(const eServiceReferenceDVB &service, time_t=0, bool plain=false );
244         const eventMap* getEventMap(const eServiceReferenceDVB &service);
245         const timeMap* getTimeMap(const eServiceReferenceDVB &service);
246 };
247
248 TEMPLATE_TYPEDEF(ePtr<eEPGCache>,eEPGCachePtr);
249
250 inline const eventMap* eEPGCache::getEventMap(const eServiceReferenceDVB &service)
251 {
252         eventCache::iterator It = eventDB.find( service );
253         if ( It != eventDB.end() && It->second.first.size() )
254                 return &(It->second.first);
255         else
256                 return 0;
257 }
258
259 inline const timeMap* eEPGCache::getTimeMap(const eServiceReferenceDVB &service)
260 {
261         eventCache::iterator It = eventDB.find( service );
262         if ( It != eventDB.end() && It->second.second.size() )
263                 return &(It->second.second);
264         else
265                 return 0;
266 }
267
268 inline void eEPGCache::Lock()
269 {
270         pthread_mutex_lock(&cache_lock);
271 }
272
273 inline void eEPGCache::Unlock()
274 {
275         pthread_mutex_unlock(&cache_lock);
276 }
277
278 #endif