missing commit
[enigma2.git] / lib / dvb / dvb.h
1 #ifndef __dvb_dvb_h
2 #define __dvb_dvb_h
3
4 #ifndef SWIG
5
6 #include <lib/base/ebase.h>
7 #include <lib/base/filepush.h>
8 #include <lib/base/elock.h>
9 #include <lib/dvb/idvb.h>
10 #include <lib/dvb/demux.h>
11 #include <lib/dvb/frontend.h>
12 #include <lib/dvb/tstools.h>
13 #include <connection.h>
14
15 class eDVBChannel;
16
17         /* we do NOT handle resource conflicts here. instead, the allocateChannel
18            fails, and the application has to see why the channel is allocated
19            (and how to deallocate it). */
20 class iDVBAdapter;
21
22 class eDVBRegisteredFrontend: public iObject, public Object
23 {
24         DECLARE_REF(eDVBRegisteredFrontend);
25         eTimer *disable;
26         Signal0<void> stateChanged;
27         void closeFrontend()
28         {
29                 if (!m_inuse && m_frontend->closeFrontend()) // frontend busy
30                         disable->start(60000, true);  // retry close in 60secs
31         }
32 public:
33         eDVBRegisteredFrontend(eDVBFrontend *fe, iDVBAdapter *adap)
34                 :disable(new eTimer(eApp)), m_adapter(adap), m_frontend(fe), m_inuse(0)
35         {
36                 disable = new eTimer(eApp);
37                 CONNECT(disable->timeout, eDVBRegisteredFrontend::closeFrontend);
38         }
39         void dec_use()
40         {
41                 if (!--m_inuse)
42                 {
43                         /* emit */ stateChanged();
44                         disable->start(3000, true);
45                 }
46         }
47         void inc_use()
48         {
49                 if (++m_inuse == 1)
50                 {
51                         m_frontend->openFrontend();
52                         /* emit */ stateChanged();
53                 }
54         }
55         iDVBAdapter *m_adapter;
56         ePtr<eDVBFrontend> m_frontend;
57         int m_inuse;
58 };
59
60 struct eDVBRegisteredDemux
61 {
62 DECLARE_REF(eDVBRegisteredDemux);
63 public:
64         iDVBAdapter *m_adapter;
65         ePtr<eDVBDemux> m_demux;
66         int m_inuse;
67         eDVBRegisteredDemux(eDVBDemux *demux, iDVBAdapter *adap): m_adapter(adap), m_demux(demux), m_inuse(0) { }
68 };
69
70 class eDVBAllocatedFrontend
71 {
72 DECLARE_REF(eDVBAllocatedFrontend);
73 public:
74         
75         eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe);
76         ~eDVBAllocatedFrontend();
77         eDVBFrontend &get() { return *m_fe->m_frontend; }
78         operator eDVBRegisteredFrontend*() { return m_fe; }
79         operator eDVBFrontend*() { return m_fe->m_frontend; }
80
81 private:
82         eDVBRegisteredFrontend *m_fe;
83 };
84
85 class eDVBAllocatedDemux
86 {
87 DECLARE_REF(eDVBAllocatedDemux);
88 public:
89         
90         eDVBAllocatedDemux(eDVBRegisteredDemux *demux);
91         ~eDVBAllocatedDemux();
92         eDVBDemux &get() { return *m_demux->m_demux; }
93         operator eDVBRegisteredDemux*() { return m_demux; }
94         operator eDVBDemux*() { return m_demux->m_demux; }
95         
96 private:
97         eDVBRegisteredDemux *m_demux;
98 };
99
100 class iDVBAdapter: public iObject
101 {
102 public:
103         virtual int getNumDemux() = 0;
104         virtual RESULT getDemux(ePtr<eDVBDemux> &demux, int nr) = 0;
105         
106         virtual int getNumFrontends() = 0;
107         virtual RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr) = 0;
108 };
109
110 class eDVBAdapterLinux: public iDVBAdapter
111 {
112 DECLARE_REF(eDVBAdapterLinux);
113 public:
114         eDVBAdapterLinux(int nr);
115
116         int getNumDemux();
117         RESULT getDemux(ePtr<eDVBDemux> &demux, int nr);
118         
119         int getNumFrontends();
120         RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr);
121         
122         static int exist(int nr);
123 private:
124         int m_nr;
125         eSmartPtrList<eDVBFrontend> m_frontend;
126         eSmartPtrList<eDVBDemux>    m_demux;
127 };
128 #endif // SWIG
129
130 SWIG_IGNORE(eDVBResourceManager);
131 class eDVBResourceManager: public iObject, public Object
132 {
133         DECLARE_REF(eDVBResourceManager);
134         int avail, busy;
135
136         eSmartPtrList<iDVBAdapter> m_adapter;
137         
138         eSmartPtrList<eDVBRegisteredDemux> m_demux;
139         eSmartPtrList<eDVBRegisteredFrontend> m_frontend;
140         
141         void addAdapter(iDVBAdapter *adapter);
142         
143                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
144                            the frontend must be tuned lateron. there is no guarante
145                            that tuning will succeed - it just means that if this frontend
146                            can't tune, no other frontend could do it.
147                            
148                            there might be a priority given to certain frontend/chid 
149                            combinations. this will be evaluated here. */
150                            
151         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm);
152         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int index);
153         
154                         /* allocate a demux able to filter on the selected frontend. */
155         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int cap);
156         
157         struct active_channel
158         {
159                 eDVBChannelID m_channel_id;
160                         /* we don't hold a reference here. */
161                 eDVBChannel *m_channel;
162                 
163                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
164         };
165         
166         std::list<active_channel> m_active_channels;
167         
168         ePtr<iDVBChannelList> m_list;
169         ePtr<iDVBSatelliteEquipmentControl> m_sec;
170         static eDVBResourceManager *instance;
171         
172         friend class eDVBChannel;
173         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
174         RESULT removeChannel(eDVBChannel *ch);
175
176         Signal1<void,eDVBChannel*> m_channelAdded;
177
178         int canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm);
179
180         eUsePtr<iDVBChannel> m_cached_channel;
181         Connection m_cached_channel_state_changed_conn;
182         eTimer m_releaseCachedChannelTimer;
183         void DVBChannelStateChanged(iDVBChannel*);
184         void releaseCachedChannel();
185         void feStateChanged();
186 #ifndef SWIG
187 public:
188 #endif
189         eDVBResourceManager();
190         virtual ~eDVBResourceManager();
191
192         RESULT setChannelList(iDVBChannelList *list);
193         RESULT getChannelList(ePtr<iDVBChannelList> &list);
194         
195         enum {
196                 errNoFrontend = -1,
197                 errNoDemux    = -2,
198                 errChidNotFound = -3
199         };
200         
201         RESULT connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
202         int canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore);
203
204                 /* allocate channel... */
205         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel);
206         RESULT allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel);
207         static RESULT getInstance(ePtr<eDVBResourceManager> &);
208 #ifdef SWIG
209 public:
210 #endif
211         PSignal1<void,int> frontendUseMaskChanged;
212         SWIG_VOID(RESULT) allocateRawChannel(eUsePtr<iDVBChannel> &SWIG_OUTPUT, int frontend_index);
213 };
214 SWIG_TEMPLATE_TYPEDEF(ePtr<eDVBResourceManager>, eDVBResourceManager);
215 SWIG_EXTEND(ePtr<eDVBResourceManager>,
216         static ePtr<eDVBResourceManager> getInstance()
217         {
218                 extern ePtr<eDVBResourceManager> NewResourceManagerPtr(void);
219                 return NewResourceManagerPtr();
220         }
221 );
222
223 #ifndef SWIG
224
225 class eDVBChannelFilePush;
226
227         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
228 class eDVBChannel: public iDVBPVRChannel, public iFilePushScatterGather, public Object
229 {
230         DECLARE_REF(eDVBChannel);
231         friend class eDVBResourceManager;
232 public:
233         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
234         virtual ~eDVBChannel();
235
236                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
237                 /* cannot be used for PVR channels. */
238         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
239         eDVBChannelID getChannelID() { return m_channel_id; }
240
241         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
242         RESULT connectEvent(const Slot2<void,iDVBChannel*,int> &eventChange, ePtr<eConnection> &connection);
243         
244         RESULT getState(int &state);
245
246         RESULT setCIRouting(const eDVBCIRouting &routing);
247         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
248         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
249         RESULT getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param);
250
251                 /* iDVBPVRChannel */
252         RESULT playFile(const char *file);
253         void stopFile();
254         
255         void setCueSheet(eCueSheet *cuesheet);
256         
257         RESULT getLength(pts_t &len);
258         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode);
259
260         int getUseCount() { return m_use_count; }
261 private:
262         ePtr<eDVBAllocatedFrontend> m_frontend;
263         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
264         
265         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
266         eDVBChannelID m_channel_id;
267         Signal1<void,iDVBChannel*> m_stateChanged;
268         Signal2<void,iDVBChannel*,int> m_event;
269         int m_state;
270
271                         /* for channel list */
272         ePtr<eDVBResourceManager> m_mgr;
273         
274         void frontendStateChanged(iDVBFrontend*fe);
275         ePtr<eConnection> m_conn_frontendStateChanged;
276         
277                 /* for PVR playback */
278         eDVBChannelFilePush *m_pvr_thread;
279         void pvrEvent(int event);
280         
281         int m_pvr_fd_dst;
282         eDVBTSTools m_tstools;
283         
284         ePtr<eCueSheet> m_cue;
285         
286         void cueSheetEvent(int event);
287         ePtr<eConnection> m_conn_cueSheetEvent;
288         int m_skipmode_m, m_skipmode_n;
289         
290         std::list<std::pair<off_t, off_t> > m_source_span;
291         void getNextSourceSpan(off_t current_offset, size_t bytes_read, off_t &start, size_t &size);
292         void flushPVR(iDVBDemux *decoding_demux=0);
293         
294         eSingleLock m_cuesheet_lock;
295
296         friend class eUsePtr<eDVBChannel>;
297                 /* use count */
298         oRefCount m_use_count;
299         void AddUse();
300         void ReleaseUse();
301 };
302 #endif // SWIG
303
304 #endif