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