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