fix newline
[enigma2.git] / lib / dvb / dvb.h
1 #ifndef __dvb_dvb_h
2 #define __dvb_dvb_h
3
4 #include <lib/base/ebase.h>
5 #include <lib/dvb/idvb.h>
6 #include <lib/dvb/demux.h>
7 #include <lib/dvb/frontend.h>
8 #include <lib/dvb/tstools.h>
9 #include <connection.h>
10
11 class eDVBChannel;
12
13         /* we do NOT handle resource conflicts here. instead, the allocateChannel
14            fails, and the application has to see why the channel is allocated
15            (and how to deallocate it). */
16 class iDVBAdapter;
17
18 class eDVBRegisteredFrontend: public iObject, public Object
19 {
20         DECLARE_REF(eDVBRegisteredFrontend);
21         eTimer *disable;
22         void closeFrontend()
23         {
24                 if (!m_inuse && m_frontend->closeFrontend()) // frontend busy
25                         disable->start(60000, true);  // retry close in 60secs
26         }
27 public:
28         eDVBRegisteredFrontend(eDVBFrontend *fe, iDVBAdapter *adap)
29                 :disable(new eTimer(eApp)), m_adapter(adap), m_frontend(fe), m_inuse(0)
30         {
31                 disable = new eTimer(eApp);
32                 CONNECT(disable->timeout, eDVBRegisteredFrontend::closeFrontend);
33         }
34         void dec_use()
35         {
36                 if (!--m_inuse)
37                         disable->start(3000, true);
38         }
39         void inc_use()
40         {
41                 if (++m_inuse == 1)
42                         m_frontend->openFrontend();
43         }
44         iDVBAdapter *m_adapter;
45         ePtr<eDVBFrontend> m_frontend;
46         int m_inuse;
47 };
48
49 struct eDVBRegisteredDemux
50 {
51 DECLARE_REF(eDVBRegisteredDemux);
52 public:
53         iDVBAdapter *m_adapter;
54         ePtr<eDVBDemux> m_demux;
55         int m_inuse;
56         eDVBRegisteredDemux(eDVBDemux *demux, iDVBAdapter *adap): m_adapter(adap), m_demux(demux), m_inuse(0) { }
57 };
58
59 class eDVBAllocatedFrontend
60 {
61 DECLARE_REF(eDVBAllocatedFrontend);
62 public:
63         
64         eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe);
65         ~eDVBAllocatedFrontend();
66         eDVBFrontend &get() { return *m_fe->m_frontend; }
67         operator eDVBRegisteredFrontend*() { return m_fe; }
68         operator eDVBFrontend*() { return m_fe->m_frontend; }
69
70 private:
71         eDVBRegisteredFrontend *m_fe;
72 };
73
74 class eDVBAllocatedDemux
75 {
76 DECLARE_REF(eDVBAllocatedDemux);
77 public:
78         
79         eDVBAllocatedDemux(eDVBRegisteredDemux *demux);
80         ~eDVBAllocatedDemux();
81         eDVBDemux &get() { return *m_demux->m_demux; }
82         operator eDVBRegisteredDemux*() { return m_demux; }
83         operator eDVBDemux*() { return m_demux->m_demux; }
84         
85 private:
86         eDVBRegisteredDemux *m_demux;
87 };
88
89 class iDVBAdapter: public iObject
90 {
91 public:
92         virtual int getNumDemux() = 0;
93         virtual RESULT getDemux(ePtr<eDVBDemux> &demux, int nr) = 0;
94         
95         virtual int getNumFrontends() = 0;
96         virtual RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr) = 0;
97 };
98
99 class eDVBAdapterLinux: public iDVBAdapter
100 {
101 DECLARE_REF(eDVBAdapterLinux);
102 public:
103         eDVBAdapterLinux(int nr);
104
105         int getNumDemux();
106         RESULT getDemux(ePtr<eDVBDemux> &demux, int nr);
107         
108         int getNumFrontends();
109         RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr);
110         
111         static int exist(int nr);
112 private:
113         int m_nr;
114         eSmartPtrList<eDVBFrontend> m_frontend;
115         eSmartPtrList<eDVBDemux>    m_demux;
116 };
117
118 class eDVBResourceManager: public iObject
119 {
120         DECLARE_REF(eDVBResourceManager);
121         int avail, busy;
122
123         eUsePtr<iDVBChannel> m_cached_channel;
124
125         eSmartPtrList<iDVBAdapter> m_adapter;
126         
127         eSmartPtrList<eDVBRegisteredDemux> m_demux;
128         eSmartPtrList<eDVBRegisteredFrontend> m_frontend;
129         
130         void addAdapter(iDVBAdapter *adapter);
131         
132                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
133                            the frontend must be tuned lateron. there is no guarante
134                            that tuning will succeed - it just means that if this frontend
135                            can't tune, no other frontend could do it.
136                            
137                            there might be a priority given to certain frontend/chid 
138                            combinations. this will be evaluated here. */
139                            
140         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm);
141         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int index);
142         
143                         /* allocate a demux able to filter on the selected frontend. */
144         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int cap);
145         
146         struct active_channel
147         {
148                 eDVBChannelID m_channel_id;
149                         /* we don't hold a reference here. */
150                 eDVBChannel *m_channel;
151                 
152                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
153         };
154         
155         std::list<active_channel> m_active_channels;
156         
157         ePtr<iDVBChannelList> m_list;
158         ePtr<iDVBSatelliteEquipmentControl> m_sec;
159         static eDVBResourceManager *instance;
160         
161         friend class eDVBChannel;
162         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
163         RESULT removeChannel(eDVBChannel *ch);
164
165         Signal1<void,eDVBChannel*> m_channelAdded;
166
167         bool canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm);
168 public:
169         eDVBResourceManager();
170         virtual ~eDVBResourceManager();
171         
172         static RESULT getInstance(ePtr<eDVBResourceManager> &ptr) { if (instance) { ptr = instance; return 0; } return -1; }
173         
174         RESULT setChannelList(iDVBChannelList *list);
175         RESULT getChannelList(ePtr<iDVBChannelList> &list);
176         
177         enum {
178                 errNoFrontend = -1,
179                 errNoDemux    = -2,
180                 errChidNotFound = -3
181         };
182
183                 /* allocate channel... */
184         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel);
185         RESULT allocateRawChannel(eUsePtr<iDVBChannel> &channel, int frontend_index);
186         RESULT allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel);
187
188         RESULT connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
189
190         bool canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore);
191 };
192
193 class eFilePushThread;
194
195         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
196 class eDVBChannel: public iDVBPVRChannel, public Object
197 {
198         DECLARE_REF(eDVBChannel);
199 public:
200         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
201         virtual ~eDVBChannel();
202
203                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
204                 /* cannot be used for PVR channels. */
205         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
206         eDVBChannelID getChannelID() { return m_channel_id; }
207
208         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
209         RESULT getState(int &state);
210
211         RESULT setCIRouting(const eDVBCIRouting &routing);
212         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
213         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
214         
215                 /* iDVBPVRChannel */
216         RESULT playFile(const char *file);
217         RESULT getLength(pts_t &len);
218         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos);
219         RESULT seekTo(iDVBDemux *decoding_demux, int relative, pts_t &pts);
220                         /* seeking to relative positions won't work - 
221                            there is an unknown amount of buffers in between */
222         RESULT seekToPosition(iDVBDemux *decoding_demux, const off_t &off);
223
224         int getUseCount() { return m_use_count; }
225 private:
226         ePtr<eDVBAllocatedFrontend> m_frontend;
227         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
228         
229         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
230         eDVBChannelID m_channel_id;
231         Signal1<void,iDVBChannel*> m_stateChanged;
232         int m_state;
233
234                         /* for channel list */
235         ePtr<eDVBResourceManager> m_mgr;
236         
237         void frontendStateChanged(iDVBFrontend*fe);
238         ePtr<eConnection> m_conn_frontendStateChanged;
239         
240                 /* for PVR playback */
241         eFilePushThread *m_pvr_thread;
242         int m_pvr_fd_src, m_pvr_fd_dst;
243         eDVBTSTools m_tstools;
244
245         friend class eUsePtr<eDVBChannel>;
246                 /* use count */
247         oRefCount m_use_count;
248         void AddUse();
249         void ReleaseUse();
250 };
251
252 #endif