add (tune)simulate support to some functions
[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 <connection.h>
14
15 class eDVBChannel;
16
17         /* we do NOT handle resource conflicts here. instead, the allocateChannel
18            fails, and the application has to see why the channel is allocated
19            (and how to deallocate it). */
20 class iDVBAdapter;
21
22 class eDVBRegisteredFrontend: public iObject, public Object
23 {
24         DECLARE_REF(eDVBRegisteredFrontend);
25         ePtr<eTimer> disable;
26         void closeFrontend()
27         {
28                 if (!m_inuse && m_frontend->closeFrontend()) // frontend busy
29                         disable->start(60000, true);  // retry close in 60secs
30         }
31 public:
32         Signal0<void> stateChanged;
33         eDVBRegisteredFrontend(eDVBFrontend *fe, iDVBAdapter *adap)
34                 :disable(eTimer::create(eApp)), m_adapter(adap), m_frontend(fe), m_inuse(0)
35         {
36                 CONNECT(disable->timeout, eDVBRegisteredFrontend::closeFrontend);
37         }
38         void dec_use()
39         {
40                 if (!--m_inuse)
41                 {
42                         /* emit */ stateChanged();
43                         disable->start(3000, true);
44                 }
45         }
46         void inc_use()
47         {
48                 if (++m_inuse == 1)
49                 {
50                         m_frontend->openFrontend();
51                         /* emit */ stateChanged();
52                 }
53         }
54         iDVBAdapter *m_adapter;
55         ePtr<eDVBFrontend> m_frontend;
56         int m_inuse;
57 };
58
59 struct eDVBRegisteredDemux
60 {
61         DECLARE_REF(eDVBRegisteredDemux);
62 public:
63         iDVBAdapter *m_adapter;
64         ePtr<eDVBDemux> m_demux;
65         int m_inuse;
66         eDVBRegisteredDemux(eDVBDemux *demux, iDVBAdapter *adap): m_adapter(adap), m_demux(demux), m_inuse(0) { }
67 };
68
69 class eDVBAllocatedFrontend
70 {
71         DECLARE_REF(eDVBAllocatedFrontend);
72 public:
73         
74         eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe);
75         ~eDVBAllocatedFrontend();
76         eDVBFrontend &get() { return *m_fe->m_frontend; }
77         operator eDVBRegisteredFrontend*() { return m_fe; }
78         operator eDVBFrontend*() { return m_fe->m_frontend; }
79
80 private:
81         eDVBRegisteredFrontend *m_fe;
82 };
83
84 class eDVBAllocatedDemux
85 {
86         DECLARE_REF(eDVBAllocatedDemux);
87 public:
88         
89         eDVBAllocatedDemux(eDVBRegisteredDemux *demux);
90         ~eDVBAllocatedDemux();
91         eDVBDemux &get() { return *m_demux->m_demux; }
92         operator eDVBRegisteredDemux*() { return m_demux; }
93         operator eDVBDemux*() { return m_demux->m_demux; }
94         
95 private:
96         eDVBRegisteredDemux *m_demux;
97 };
98
99 class iDVBAdapter: public iObject
100 {
101 public:
102         virtual int getNumDemux() = 0;
103         virtual RESULT getDemux(ePtr<eDVBDemux> &demux, int nr) = 0;
104         
105         virtual int getNumFrontends() = 0;
106         virtual RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr, bool simulate=false) = 0;
107 };
108
109 class eDVBAdapterLinux: public iDVBAdapter
110 {
111         DECLARE_REF(eDVBAdapterLinux);
112 public:
113         eDVBAdapterLinux(int nr);
114
115         int getNumDemux();
116         RESULT getDemux(ePtr<eDVBDemux> &demux, int nr);
117         
118         int getNumFrontends();
119         RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr, bool simulate=false);
120         
121         static int exist(int nr);
122 private:
123         int m_nr;
124         eSmartPtrList<eDVBFrontend> m_frontend, m_simulate_frontend;
125         eSmartPtrList<eDVBDemux>    m_demux;
126 };
127 #endif // SWIG
128
129 SWIG_IGNORE(eDVBResourceManager);
130 class eDVBResourceManager: public iObject, public Object
131 {
132         DECLARE_REF(eDVBResourceManager);
133         int avail, busy;
134
135         eSmartPtrList<iDVBAdapter> m_adapter;
136         eSmartPtrList<eDVBRegisteredDemux> m_demux;
137         eSmartPtrList<eDVBRegisteredFrontend> m_frontend, m_simulate_frontend;
138         void addAdapter(iDVBAdapter *adapter);
139
140         struct active_channel
141         {
142                 eDVBChannelID m_channel_id;
143                         /* we don't hold a reference here. */
144                 eDVBChannel *m_channel;
145                 
146                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
147         };
148         
149         std::list<active_channel> m_active_channels, m_active_simulate_channels;
150         
151         ePtr<iDVBChannelList> m_list;
152         ePtr<iDVBSatelliteEquipmentControl> m_sec;
153         static eDVBResourceManager *instance;
154         
155         friend class eDVBChannel;
156         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
157         RESULT removeChannel(eDVBChannel *ch);
158
159         Signal1<void,eDVBChannel*> m_channelAdded;
160
161         eUsePtr<iDVBChannel> m_cached_channel;
162         Connection m_cached_channel_state_changed_conn;
163         ePtr<eTimer> m_releaseCachedChannelTimer;
164         void DVBChannelStateChanged(iDVBChannel*);
165         void feStateChanged();
166 #ifndef SWIG
167 public:
168 #endif
169         void releaseCachedChannel();
170         eDVBResourceManager();
171         virtual ~eDVBResourceManager();
172
173         RESULT setChannelList(iDVBChannelList *list);
174         RESULT getChannelList(ePtr<iDVBChannelList> &list);
175         
176         enum {
177                         /* errNoFrontend = -1 replaced by more spcific messages */
178                 errNoDemux    = -2,
179                 errChidNotFound = -3,
180                 errNoChannelList = -4,
181                 errChannelNotInList = -5,
182                 errAllSourcesBusy = -6,
183                 errNoSourceFound = -7,
184         };
185         
186         RESULT connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
187         int canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore, bool simulate=false);
188
189                 /* allocate channel... */
190         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel, bool simulate=false);
191         RESULT allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel);
192         static RESULT getInstance(ePtr<eDVBResourceManager> &);
193
194                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
195                            the frontend must be tuned lateron. there is no guarante
196                            that tuning will succeed - it just means that if this frontend
197                            can't tune, no other frontend could do it.
198
199                            there might be a priority given to certain frontend/chid
200                            combinations. this will be evaluated here. */
201         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
202
203         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int slot_index);
204                         /* allocate a demux able to filter on the selected frontend. */
205         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int cap);
206 #ifdef SWIG
207 public:
208 #endif
209         int canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm, bool simulate=false);
210         bool canMeasureFrontendInputPower();
211         PSignal1<void,int> frontendUseMaskChanged;
212         SWIG_VOID(RESULT) allocateRawChannel(eUsePtr<iDVBChannel> &SWIG_OUTPUT, int slot_index);
213         PyObject *setFrontendSlotInformations(SWIG_PYOBJECT(ePyObject) list);
214 };
215 SWIG_TEMPLATE_TYPEDEF(ePtr<eDVBResourceManager>, eDVBResourceManager);
216 SWIG_EXTEND(ePtr<eDVBResourceManager>,
217         static ePtr<eDVBResourceManager> getInstance()
218         {
219                 extern ePtr<eDVBResourceManager> NewResourceManagerPtr(void);
220                 return NewResourceManagerPtr();
221         }
222 );
223
224 #ifndef SWIG
225
226 class eDVBChannelFilePush;
227
228         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
229 class eDVBChannel: public iDVBPVRChannel, public iFilePushScatterGather, public Object
230 {
231         DECLARE_REF(eDVBChannel);
232         friend class eDVBResourceManager;
233 public:
234         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
235         virtual ~eDVBChannel();
236
237                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
238                 /* cannot be used for PVR channels. */
239         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
240         eDVBChannelID getChannelID() { return m_channel_id; }
241
242         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
243         RESULT connectEvent(const Slot2<void,iDVBChannel*,int> &eventChange, ePtr<eConnection> &connection);
244         
245         RESULT getState(int &state);
246
247         RESULT setCIRouting(const eDVBCIRouting &routing);
248         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
249         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
250         RESULT getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param);
251
252                 /* iDVBPVRChannel */
253         RESULT playFile(const char *file);
254         void stopFile();
255         
256         void setCueSheet(eCueSheet *cuesheet);
257         
258         RESULT getLength(pts_t &len);
259         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode);
260
261         int getUseCount() { return m_use_count; }
262 private:
263         ePtr<eDVBAllocatedFrontend> m_frontend;
264         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
265         
266         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
267         eDVBChannelID m_channel_id;
268         Signal1<void,iDVBChannel*> m_stateChanged;
269         Signal2<void,iDVBChannel*,int> m_event;
270         int m_state;
271
272                         /* for channel list */
273         ePtr<eDVBResourceManager> m_mgr;
274         
275         void frontendStateChanged(iDVBFrontend*fe);
276         ePtr<eConnection> m_conn_frontendStateChanged;
277         
278                 /* for PVR playback */
279         eDVBChannelFilePush *m_pvr_thread;
280         void pvrEvent(int event);
281         
282         int m_pvr_fd_dst;
283         eDVBTSTools m_tstools;
284         
285         ePtr<eCueSheet> m_cue;
286         
287         void cueSheetEvent(int event);
288         ePtr<eConnection> m_conn_cueSheetEvent;
289         int m_skipmode_m, m_skipmode_n;
290         
291         std::list<std::pair<off_t, off_t> > m_source_span;
292         void getNextSourceSpan(off_t current_offset, size_t bytes_read, off_t &start, size_t &size);
293         void flushPVR(iDVBDemux *decoding_demux=0);
294         
295         eSingleLock m_cuesheet_lock;
296
297         friend class eUsePtr<eDVBChannel>;
298                 /* use count */
299         oRefCount m_use_count;
300         void AddUse();
301         void ReleaseUse();
302 };
303 #endif // SWIG
304
305 #endif