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