fb63399eede10ab9b9603792d14f8a0a904351f2
[openblackhole/openblackhole-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 <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         void scanDevices();
119
120         int getNumDemux();
121         RESULT getDemux(ePtr<eDVBDemux> &demux, int nr);
122
123         int getNumFrontends();
124         RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr, bool simulate=false);
125
126         static int exist(int nr);
127         static bool isusb(int nr);
128 private:
129         int m_nr;
130         eSmartPtrList<eDVBFrontend> m_frontend, m_simulate_frontend;
131         eSmartPtrList<eDVBDemux>    m_demux;
132 protected:
133         static std::map<std::string, std::string> mappedFrontendName;
134 };
135
136 class eDVBUsbAdapter: public eDVBAdapterLinux
137 {
138         DECLARE_REF(eDVBUsbAdapter);
139 private:
140         int vtunerFd;
141         int demuxFd;
142         int pipeFd[2];
143         std::string usbFrontendName;
144         std::string virtualFrontendName;
145         bool running;
146         unsigned short int pidList[30];
147         unsigned char buffer[(188 / 4) * 4096];
148         pthread_t pumpThread;
149         static void *threadproc(void *arg);
150         void *vtunerPump();
151
152 public:
153         eDVBUsbAdapter(int nr);
154         ~eDVBUsbAdapter();
155 };
156 #endif // SWIG
157
158 SWIG_IGNORE(eDVBResourceManager);
159 class eDVBResourceManager: public iObject, public Object
160 {
161         DECLARE_REF(eDVBResourceManager);
162         int avail, busy;
163
164         enum { DM7025, DM800, DM500HD, DM800SE, DM8000, DM7020HD };
165
166         int m_boxtype;
167
168         eSmartPtrList<iDVBAdapter> m_adapter;
169         eSmartPtrList<eDVBRegisteredDemux> m_demux;
170         eSmartPtrList<eDVBRegisteredFrontend> m_frontend, m_simulate_frontend;
171         void addAdapter(iDVBAdapter *adapter, bool front = false);
172
173         struct active_channel
174         {
175                 eDVBChannelID m_channel_id;
176                         /* we don't hold a reference here. */
177                 eDVBChannel *m_channel;
178
179                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
180         };
181
182         std::list<active_channel> m_active_channels, m_active_simulate_channels;
183
184         ePtr<iDVBChannelList> m_list;
185         ePtr<iDVBSatelliteEquipmentControl> m_sec;
186         static eDVBResourceManager *instance;
187
188         friend class eDVBChannel;
189         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
190         RESULT removeChannel(eDVBChannel *ch);
191
192         Signal1<void,eDVBChannel*> m_channelAdded;
193
194         eUsePtr<iDVBChannel> m_cached_channel;
195         Connection m_cached_channel_state_changed_conn;
196         ePtr<eTimer> m_releaseCachedChannelTimer;
197         void DVBChannelStateChanged(iDVBChannel*);
198         void feStateChanged();
199 #ifndef SWIG
200 public:
201 #endif
202         void releaseCachedChannel();
203         eDVBResourceManager();
204         virtual ~eDVBResourceManager();
205
206         RESULT setChannelList(iDVBChannelList *list);
207         RESULT getChannelList(ePtr<iDVBChannelList> &list);
208
209         enum {
210                         /* errNoFrontend = -1 replaced by more spcific messages */
211                 errNoDemux    = -2,
212                 errChidNotFound = -3,
213                 errNoChannelList = -4,
214                 errChannelNotInList = -5,
215                 errAllSourcesBusy = -6,
216                 errNoSourceFound = -7,
217         };
218
219         RESULT connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
220         int canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore, int &system, bool simulate=false);
221
222                 /* allocate channel... */
223         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel, bool simulate=false);
224         RESULT allocatePVRChannel(const eDVBChannelID &channelid, eUsePtr<iDVBPVRChannel> &channel);
225         static RESULT getInstance(ePtr<eDVBResourceManager> &);
226
227                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
228                            the frontend must be tuned lateron. there is no guarante
229                            that tuning will succeed - it just means that if this frontend
230                            can't tune, no other frontend could do it.
231
232                            there might be a priority given to certain frontend/chid
233                            combinations. this will be evaluated here. */
234         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
235
236         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int slot_index);
237                         /* allocate a demux able to filter on the selected frontend. */
238         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int &cap);
239 #ifdef SWIG
240 public:
241 #endif
242         int canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
243         bool canMeasureFrontendInputPower();
244         PSignal1<void,int> frontendUseMaskChanged;
245         SWIG_VOID(RESULT) allocateRawChannel(eUsePtr<iDVBChannel> &SWIG_OUTPUT, int slot_index);
246         PyObject *setFrontendSlotInformations(SWIG_PYOBJECT(ePyObject) list);
247         bool frontendIsCompatible(int index, const char *type);
248         void setFrontendType(int index, const char *type);
249 };
250 SWIG_TEMPLATE_TYPEDEF(ePtr<eDVBResourceManager>, eDVBResourceManager);
251 SWIG_EXTEND(ePtr<eDVBResourceManager>,
252         static ePtr<eDVBResourceManager> getInstance()
253         {
254                 extern ePtr<eDVBResourceManager> NewResourceManagerPtr(void);
255                 return NewResourceManagerPtr();
256         }
257 );
258
259 #ifndef SWIG
260
261 class eDVBChannelFilePush;
262
263         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
264 class eDVBChannel: public iDVBPVRChannel, public iFilePushScatterGather, public Object
265 {
266         DECLARE_REF(eDVBChannel);
267         friend class eDVBResourceManager;
268 public:
269         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
270         virtual ~eDVBChannel();
271
272                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
273                 /* cannot be used for PVR channels. */
274         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
275         eDVBChannelID getChannelID() { return m_channel_id; }
276
277         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
278         RESULT connectEvent(const Slot2<void,iDVBChannel*,int> &eventChange, ePtr<eConnection> &connection);
279
280         RESULT getState(int &state);
281
282         RESULT setCIRouting(const eDVBCIRouting &routing);
283         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
284         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
285         RESULT getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param);
286
287                 /* iDVBPVRChannel */
288         RESULT playFile(const char *file);
289         RESULT playSource(ePtr<iTsSource>& source, const char *priv=NULL);
290         void stop();
291
292         void setCueSheet(eCueSheet *cuesheet);
293         void setOfflineDecodeMode(int parityswitchdelay);
294
295         RESULT getLength(pts_t &len);
296         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode);
297
298         int getUseCount() { return m_use_count; }
299
300         RESULT requestTsidOnid();
301         int reserveDemux();
302 private:
303         ePtr<eDVBAllocatedFrontend> m_frontend;
304         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
305
306         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
307         eDVBChannelID m_channel_id;
308         Signal1<void,iDVBChannel*> m_stateChanged;
309         Signal2<void,iDVBChannel*,int> m_event;
310         int m_state;
311         ePtr<iTsSource> m_source;
312
313                         /* for channel list */
314         ePtr<eDVBResourceManager> m_mgr;
315
316         void frontendStateChanged(iDVBFrontend*fe);
317         ePtr<eConnection> m_conn_frontendStateChanged;
318
319                 /* for PVR playback */
320         eDVBChannelFilePush *m_pvr_thread;
321         void pvrEvent(int event);
322
323         int m_pvr_fd_dst;
324         eSingleLock m_tstools_lock;
325         eDVBTSTools m_tstools;
326
327         ePtr<eCueSheet> m_cue;
328
329         void cueSheetEvent(int event);
330         ePtr<eConnection> m_conn_cueSheetEvent;
331         int m_skipmode_m, m_skipmode_n, m_skipmode_frames, m_skipmode_frames_remainder;
332
333         std::list<std::pair<off_t, off_t> > m_source_span;
334         void getNextSourceSpan(off_t current_offset, size_t bytes_read, off_t &start, size_t &size);
335         void flushPVR(iDVBDemux *decoding_demux=0);
336
337         eSingleLock m_cuesheet_lock;
338
339         friend class eUsePtr<eDVBChannel>;
340                 /* use count */
341         oRefCount m_use_count;
342         void AddUse();
343         void ReleaseUse();
344
345                 /* for tsid/onid read */
346         ePtr<iDVBDemux> m_tsid_onid_demux;
347         ePtr<eTable<ServiceDescriptionSection> > m_SDT;
348         void SDTready(int err);
349 };
350 #endif // SWIG
351
352 #endif