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