4.2.0r10
[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: public iObject
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: public iObject
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: public iObject
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         eSmartPtrList<iDVBAdapter> m_adapter;
154         eSmartPtrList<eDVBRegisteredDemux> m_demux;
155         eSmartPtrList<eDVBRegisteredFrontend> m_frontend, m_simulate_frontend;
156         void addAdapter(iDVBAdapter *adapter);
157
158         struct active_channel
159         {
160                 eDVBChannelID m_channel_id;
161                         /* we don't hold a reference here. */
162                 eDVBChannel *m_channel;
163                 
164                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
165         };
166         
167         std::list<active_channel> m_active_channels, m_active_simulate_channels;
168         
169         ePtr<iDVBChannelList> m_list;
170         ePtr<iDVBSatelliteEquipmentControl> m_sec;
171         static eDVBResourceManager *instance;
172         
173         friend class eDVBChannel;
174         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
175         RESULT removeChannel(eDVBChannel *ch);
176
177         sigc::signal1<void,eDVBChannel*> m_channelAdded;
178
179         eUsePtr<iDVBChannel> m_cached_channel;
180         sigc::connection m_cached_channel_state_changed_conn;
181         ePtr<eTimer> m_releaseCachedChannelTimer;
182         void DVBChannelStateChanged(iDVBChannel*);
183         void feStateChanged();
184 #ifndef SWIG
185 public:
186 #endif
187         void releaseCachedChannel();
188         eDVBResourceManager();
189         virtual ~eDVBResourceManager();
190
191         RESULT setChannelList(iDVBChannelList *list);
192         RESULT getChannelList(ePtr<iDVBChannelList> &list);
193         
194         enum {
195                         /* errNoFrontend = -1 replaced by more spcific messages */
196                 errNoDemux    = -2,
197                 errChidNotFound = -3,
198                 errNoChannelList = -4,
199                 errChannelNotInList = -5,
200                 errAllSourcesBusy = -6,
201                 errNoSourceFound = -7,
202         };
203         
204         RESULT connectChannelAdded(const sigc::slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
205         int canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore, bool simulate=false);
206
207                 /* allocate channel... */
208         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel, bool simulate=false);
209                 /* allocate a pvr channel */
210         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBPVRChannel> &channel);
211
212         static RESULT getInstance(ePtr<eDVBResourceManager> &);
213
214                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
215                            the frontend must be tuned lateron. there is no guarante
216                            that tuning will succeed - it just means that if this frontend
217                            can't tune, no other frontend could do it.
218
219                            there might be a priority given to certain frontend/chid
220                            combinations. this will be evaluated here. */
221         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
222
223         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int slot_index);
224                         /* allocate a demux able to filter on the selected frontend. */
225
226         int canAllocateDemux(const eDVBRegisteredFrontend *fe, int cap);
227
228         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int &cap);
229                         /* allocate a demux able to filter on the selected pvr source. */
230         RESULT allocateDemux(const std::string &pvr_source, ePtr<eDVBAllocatedDemux> &demux, int &cap);
231 #ifdef SWIG
232 public:
233 #endif
234         int canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
235         bool canMeasureFrontendInputPower();
236         eSignal1<void, int> frontendUseMaskChanged;
237         SWIG_VOID(RESULT) allocateRawChannel(eUsePtr<iDVBChannel> &SWIG_OUTPUT, int slot_index);
238         void setFrontendSlotInformations(std::list<std::tuple<int, std::string, bool, int, std::string> >);
239 };
240 SWIG_TEMPLATE_TYPEDEF(ePtr<eDVBResourceManager>, eDVBResourceManager);
241 SWIG_EXTEND(ePtr<eDVBResourceManager>,
242         static ePtr<eDVBResourceManager> getInstance()
243         {
244                 extern ePtr<eDVBResourceManager> NewResourceManagerPtr(void);
245                 return NewResourceManagerPtr();
246         }
247 );
248
249 #ifndef SWIG
250
251 class eDVBChannelFilePush;
252
253         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
254 class eDVBChannel: public iDVBPVRChannel, public iFilePushScatterGather, public sigc::trackable
255 {
256         DECLARE_REF(eDVBChannel);
257         friend class eDVBResourceManager;
258 public:
259         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
260         virtual ~eDVBChannel();
261
262                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
263                 /* cannot be used for PVR channels. */
264         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
265         eDVBChannelID getChannelID() { return m_channel_id; }
266
267         RESULT connectStateChange(const sigc::slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
268         RESULT connectEvent(const sigc::slot2<void,iDVBChannel*,int> &eventChange, ePtr<eConnection> &connection);
269         
270         RESULT getState(int &state);
271
272         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
273         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
274         RESULT getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param);
275
276                 /* iDVBPVRChannel */
277         RESULT setChannel(const eDVBChannelID &id);
278         RESULT playFile(const char *file);
279         void stopFile();
280
281         RESULT playSource(ePtr<iTsSource>& source, const char *priv=NULL);
282         void stopSource();
283
284         void setCueSheet(eCueSheet *cuesheet);
285         
286         RESULT getLength(pts_t &len);
287         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode);
288
289         int getUseCount() { return m_use_count; }
290
291         RESULT requestTsidOnid(eSlot1<void, int> &cb_func);
292         int reserveDemux();
293 private:
294         ePtr<eDVBAllocatedFrontend> m_frontend;
295         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
296         
297         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
298         eDVBChannelID m_channel_id;
299         sigc::signal1<void,iDVBChannel*> m_stateChanged;
300         sigc::signal2<void,iDVBChannel*,int> m_event;
301         int m_state;
302
303                         /* for channel list */
304         ePtr<eDVBResourceManager> m_mgr;
305         
306         void frontendStateChanged(iDVBFrontend*fe);
307         ePtr<eConnection> m_conn_frontendStateChanged;
308         
309                 /* for PVR playback */
310         eDVBChannelFilePush *m_pvr_thread;
311         void pvrEvent(int event);
312         
313         int m_pvr_fd_dst;
314         eDVBTSTools m_tstools;
315         
316         ePtr<eCueSheet> m_cue;
317         
318         void cueSheetEvent(int event);
319         ePtr<eConnection> m_conn_cueSheetEvent;
320         int m_skipmode_m, m_skipmode_n, m_skipmode_frames, m_skipmode_frames_remainder;
321         
322         std::list<std::pair<uint64_t, uint64_t> > m_source_span;
323         void getNextSourceSpan(uint64_t current_offset, size_t bytes_read, uint64_t &start, size_t &size);
324         void flushPVR(iDVBDemux *decoding_demux=0);
325
326         eSingleLock m_cuesheet_lock;
327
328         friend class eUsePtr<eDVBChannel>;
329                 /* use count */
330         oRefCount m_use_count;
331         void AddUse();
332         void ReleaseUse();
333
334                 /* for tsid/onid read */
335         eSignal1<void, int> m_tsid_onid_cb;
336         ePtr<iDVBDemux> m_tsid_onid_demux;
337         ePtr<eTable<ServiceDescriptionSection> > m_SDT;
338         void SDTready(int err);
339 };
340 #endif // SWIG
341
342 #endif