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