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