enigma2 (20120327 rel32 -> 20120430 master)
[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 <lib/base/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 sigc::trackable
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         sigc::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                         m_frontend->preClose();
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         bool m_have_descrambler;
71         eDVBRegisteredDemux(eDVBDemux *demux, iDVBAdapter *adap, bool have_descrambler)
72                 :m_adapter(adap), m_demux(demux), m_inuse(0), m_have_descrambler(have_descrambler)
73         { }
74 };
75
76 class eDVBAllocatedFrontend
77 {
78         DECLARE_REF(eDVBAllocatedFrontend);
79 public:
80         
81         eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe);
82         ~eDVBAllocatedFrontend();
83         eDVBFrontend &get() { return *m_fe->m_frontend; }
84         operator eDVBRegisteredFrontend*() { return m_fe; }
85         operator eDVBFrontend*() { return m_fe->m_frontend; }
86
87 private:
88         eDVBRegisteredFrontend *m_fe;
89 };
90
91 class eDVBAllocatedDemux
92 {
93         DECLARE_REF(eDVBAllocatedDemux);
94 public:
95         
96         eDVBAllocatedDemux(eDVBRegisteredDemux *demux);
97         ~eDVBAllocatedDemux();
98         eDVBDemux &get() { return *m_demux->m_demux; }
99         operator eDVBRegisteredDemux*() { return m_demux; }
100         operator eDVBDemux*() { return m_demux->m_demux; }
101         
102 private:
103         eDVBRegisteredDemux *m_demux;
104 };
105
106 class iDVBAdapter: public iObject
107 {
108 public:
109         virtual int getNumDemux() = 0;
110         virtual RESULT getDemux(ePtr<eDVBDemux> &demux, int nr) = 0;
111
112         virtual int getNumFrontends() = 0;
113         virtual RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr, bool simulate=false) = 0;
114
115         virtual int getNumDecodersAudio() = 0;
116         virtual int getNumDecodersVideo() = 0;
117         virtual int getNumDescramblers() = 0;
118 };
119
120 class eDVBAdapterLinux: public iDVBAdapter
121 {
122         DECLARE_REF(eDVBAdapterLinux);
123 public:
124         eDVBAdapterLinux(int nr);
125
126         int getNumDemux();
127         RESULT getDemux(ePtr<eDVBDemux> &demux, int nr);
128         
129         int getNumFrontends();
130         RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr, bool simulate=false);
131         
132         int getNumDecodersAudio();
133         int getNumDecodersVideo();
134         int getNumDescramblers();
135         
136         static int exist(int nr);
137 private:
138         int m_nr;
139         int m_nr_decoders_audio;
140         int m_nr_decoders_video;
141         int m_nr_descramblers;
142         eSmartPtrList<eDVBFrontend> m_frontend, m_simulate_frontend;
143         eSmartPtrList<eDVBDemux>    m_demux;
144 };
145 #endif // SWIG
146
147 SWIG_IGNORE(eDVBResourceManager);
148 class eDVBResourceManager: public iObject, public sigc::trackable
149 {
150         DECLARE_REF(eDVBResourceManager);
151         int avail, busy;
152
153         enum { DM7025, DM800, DM500HD, DM800SE, DM8000, DM7020HD };
154
155         int m_boxtype;
156
157         eSmartPtrList<iDVBAdapter> m_adapter;
158         eSmartPtrList<eDVBRegisteredDemux> m_demux;
159         eSmartPtrList<eDVBRegisteredFrontend> m_frontend, m_simulate_frontend;
160         void addAdapter(iDVBAdapter *adapter);
161
162         struct active_channel
163         {
164                 eDVBChannelID m_channel_id;
165                         /* we don't hold a reference here. */
166                 eDVBChannel *m_channel;
167                 
168                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
169         };
170         
171         std::list<active_channel> m_active_channels, m_active_simulate_channels;
172         
173         ePtr<iDVBChannelList> m_list;
174         ePtr<iDVBSatelliteEquipmentControl> m_sec;
175         static eDVBResourceManager *instance;
176         
177         friend class eDVBChannel;
178         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
179         RESULT removeChannel(eDVBChannel *ch);
180
181         sigc::signal1<void,eDVBChannel*> m_channelAdded;
182
183         eUsePtr<iDVBChannel> m_cached_channel;
184         sigc::connection m_cached_channel_state_changed_conn;
185         ePtr<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 sigc::slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
209         int canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore, bool simulate=false);
210
211                 /* allocate channel... */
212         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel, bool simulate=false);
213                 /* allocate a pvr channel */
214         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBPVRChannel> &channel);
215
216         static RESULT getInstance(ePtr<eDVBResourceManager> &);
217
218                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
219                            the frontend must be tuned lateron. there is no guarante
220                            that tuning will succeed - it just means that if this frontend
221                            can't tune, no other frontend could do it.
222
223                            there might be a priority given to certain frontend/chid
224                            combinations. this will be evaluated here. */
225         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
226
227         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int slot_index);
228                         /* allocate a demux able to filter on the selected frontend. */
229
230         int canAllocateDemux(const eDVBRegisteredFrontend *fe, int cap);
231
232         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int &cap);
233                         /* allocate a demux able to filter on the selected pvr source. */
234         RESULT allocateDemux(const std::string &pvr_source, ePtr<eDVBAllocatedDemux> &demux, int &cap);
235 #ifdef SWIG
236 public:
237 #endif
238         int canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
239         bool canMeasureFrontendInputPower();
240         PSignal1<void,int> frontendUseMaskChanged;
241         SWIG_VOID(RESULT) allocateRawChannel(eUsePtr<iDVBChannel> &SWIG_OUTPUT, int slot_index);
242         PyObject *setFrontendSlotInformations(SWIG_PYOBJECT(ePyObject) list);
243 };
244 SWIG_TEMPLATE_TYPEDEF(ePtr<eDVBResourceManager>, eDVBResourceManager);
245 SWIG_EXTEND(ePtr<eDVBResourceManager>,
246         static ePtr<eDVBResourceManager> getInstance()
247         {
248                 extern ePtr<eDVBResourceManager> NewResourceManagerPtr(void);
249                 return NewResourceManagerPtr();
250         }
251 );
252
253 #ifndef SWIG
254
255 class eDVBChannelFilePush;
256
257         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
258 class eDVBChannel: public iDVBPVRChannel, public iFilePushScatterGather, public sigc::trackable
259 {
260         DECLARE_REF(eDVBChannel);
261         friend class eDVBResourceManager;
262 public:
263         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
264         virtual ~eDVBChannel();
265
266                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
267                 /* cannot be used for PVR channels. */
268         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
269         eDVBChannelID getChannelID() { return m_channel_id; }
270
271         RESULT connectStateChange(const sigc::slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
272         RESULT connectEvent(const sigc::slot2<void,iDVBChannel*,int> &eventChange, ePtr<eConnection> &connection);
273         
274         RESULT getState(int &state);
275
276         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
277         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
278         RESULT getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param);
279
280                 /* iDVBPVRChannel */
281         RESULT setChannel(const eDVBChannelID &id);
282         RESULT playFile(const char *file);
283         void stopFile();
284
285         RESULT playSource(ePtr<iTsSource>& source, const char *priv=NULL);
286         void stopSource();
287
288         void setCueSheet(eCueSheet *cuesheet);
289         
290         RESULT getLength(pts_t &len);
291         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode);
292
293         int getUseCount() { return m_use_count; }
294
295         RESULT requestTsidOnid(ePyObject callback);
296         int reserveDemux();
297 private:
298         ePtr<eDVBAllocatedFrontend> m_frontend;
299         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
300         
301         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
302         eDVBChannelID m_channel_id;
303         sigc::signal1<void,iDVBChannel*> m_stateChanged;
304         sigc::signal2<void,iDVBChannel*,int> m_event;
305         int m_state;
306
307                         /* for channel list */
308         ePtr<eDVBResourceManager> m_mgr;
309         
310         void frontendStateChanged(iDVBFrontend*fe);
311         ePtr<eConnection> m_conn_frontendStateChanged;
312         
313                 /* for PVR playback */
314         eDVBChannelFilePush *m_pvr_thread;
315         void pvrEvent(int event);
316         
317         int m_pvr_fd_dst;
318         eDVBTSTools m_tstools;
319         
320         ePtr<eCueSheet> m_cue;
321         
322         void cueSheetEvent(int event);
323         ePtr<eConnection> m_conn_cueSheetEvent;
324         int m_skipmode_m, m_skipmode_n, m_skipmode_frames, m_skipmode_frames_remainder;
325         
326         std::list<std::pair<uint64_t, uint64_t> > m_source_span;
327         void getNextSourceSpan(uint64_t current_offset, size_t bytes_read, uint64_t &start, size_t &size);
328         void flushPVR(iDVBDemux *decoding_demux=0);
329
330         eSingleLock m_cuesheet_lock;
331
332         friend class eUsePtr<eDVBChannel>;
333                 /* use count */
334         oRefCount m_use_count;
335         void AddUse();
336         void ReleaseUse();
337
338                 /* for tsid/onid read */
339         ePyObject m_tsid_onid_callback;
340         ePtr<iDVBDemux> m_tsid_onid_demux;
341         ePtr<eTable<ServiceDescriptionSection> > m_SDT;
342         void SDTready(int err);
343 };
344 #endif // SWIG
345
346 #endif