fix error handling
[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 slot_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 slot_index);
213         PyObject *setFrontendSlotInformations(SWIG_PYOBJECT(ePyObject) list);
214 };
215 SWIG_TEMPLATE_TYPEDEF(ePtr<eDVBResourceManager>, eDVBResourceManager);
216 SWIG_EXTEND(ePtr<eDVBResourceManager>,
217         static ePtr<eDVBResourceManager> getInstance()
218         {
219                 extern ePtr<eDVBResourceManager> NewResourceManagerPtr(void);
220                 return NewResourceManagerPtr();
221         }
222 );
223
224 #ifndef SWIG
225
226 class eDVBChannelFilePush;
227
228         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
229 class eDVBChannel: public iDVBPVRChannel, public iFilePushScatterGather, public Object
230 {
231         DECLARE_REF(eDVBChannel);
232         friend class eDVBResourceManager;
233 public:
234         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
235         virtual ~eDVBChannel();
236
237                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
238                 /* cannot be used for PVR channels. */
239         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
240         eDVBChannelID getChannelID() { return m_channel_id; }
241
242         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
243         RESULT connectEvent(const Slot2<void,iDVBChannel*,int> &eventChange, ePtr<eConnection> &connection);
244         
245         RESULT getState(int &state);
246
247         RESULT setCIRouting(const eDVBCIRouting &routing);
248         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
249         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
250         RESULT getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param);
251
252                 /* iDVBPVRChannel */
253         RESULT playFile(const char *file);
254         void stopFile();
255         
256         void setCueSheet(eCueSheet *cuesheet);
257         
258         RESULT getLength(pts_t &len);
259         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode);
260
261         int getUseCount() { return m_use_count; }
262 private:
263         ePtr<eDVBAllocatedFrontend> m_frontend;
264         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
265         
266         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
267         eDVBChannelID m_channel_id;
268         Signal1<void,iDVBChannel*> m_stateChanged;
269         Signal2<void,iDVBChannel*,int> m_event;
270         int m_state;
271
272                         /* for channel list */
273         ePtr<eDVBResourceManager> m_mgr;
274         
275         void frontendStateChanged(iDVBFrontend*fe);
276         ePtr<eConnection> m_conn_frontendStateChanged;
277         
278                 /* for PVR playback */
279         eDVBChannelFilePush *m_pvr_thread;
280         void pvrEvent(int event);
281         
282         int m_pvr_fd_dst;
283         eDVBTSTools m_tstools;
284         
285         ePtr<eCueSheet> m_cue;
286         
287         void cueSheetEvent(int event);
288         ePtr<eConnection> m_conn_cueSheetEvent;
289         int m_skipmode_m, m_skipmode_n;
290         
291         std::list<std::pair<off_t, off_t> > m_source_span;
292         void getNextSourceSpan(off_t current_offset, size_t bytes_read, off_t &start, size_t &size);
293         void flushPVR(iDVBDemux *decoding_demux=0);
294         
295         eSingleLock m_cuesheet_lock;
296
297         friend class eUsePtr<eDVBChannel>;
298                 /* use count */
299         oRefCount m_use_count;
300         void AddUse();
301         void ReleaseUse();
302 };
303 #endif // SWIG
304
305 #endif