ee8ea8ef5afe3df05c53a7db0e63f4bee0a6e13e
[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
143         bool canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm);
144 public:
145         eDVBResourceManager();
146         virtual ~eDVBResourceManager();
147         
148         static RESULT getInstance(ePtr<eDVBResourceManager> &ptr) { if (instance) { ptr = instance; return 0; } return -1; }
149         
150         RESULT setChannelList(iDVBChannelList *list);
151         RESULT getChannelList(ePtr<iDVBChannelList> &list);
152         
153         enum {
154                 errNoFrontend = -1,
155                 errNoDemux    = -2,
156                 errChidNotFound = -3
157         };
158
159                 /* allocate channel... */
160         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel);
161         RESULT allocateRawChannel(eUsePtr<iDVBChannel> &channel, int frontend_index);
162         RESULT allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel);
163
164         RESULT connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
165
166         bool canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID &ignore);
167 };
168
169 class eFilePushThread;
170
171         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
172 class eDVBChannel: public iDVBPVRChannel, public Object
173 {
174         DECLARE_REF(eDVBChannel);
175 public:
176         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
177         virtual ~eDVBChannel();
178
179                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
180                 /* cannot be used for PVR channels. */
181         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
182         eDVBChannelID getChannelID() { return m_channel_id; }
183
184         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
185         RESULT getState(int &state);
186
187         RESULT setCIRouting(const eDVBCIRouting &routing);
188         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
189         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
190         
191                 /* iDVBPVRChannel */
192         RESULT playFile(const char *file);
193         RESULT getLength(pts_t &len);
194         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos);
195         RESULT seekTo(iDVBDemux *decoding_demux, int relative, pts_t &pts);
196                         /* seeking to relative positions won't work - 
197                            there is an unknown amount of buffers in between */
198         RESULT seekToPosition(iDVBDemux *decoding_demux, const off_t &off);
199
200         int getUseCount() { return m_use_count; }
201 private:
202         ePtr<eDVBAllocatedFrontend> m_frontend;
203         ePtr<eDVBAllocatedDemux> m_demux, m_decoder_demux;
204         
205         ePtr<iDVBFrontendParameters> m_current_frontend_parameters;
206         eDVBChannelID m_channel_id;
207         Signal1<void,iDVBChannel*> m_stateChanged;
208         int m_state;
209
210                         /* for channel list */
211         ePtr<eDVBResourceManager> m_mgr;
212         
213         void frontendStateChanged(iDVBFrontend*fe);
214         ePtr<eConnection> m_conn_frontendStateChanged;
215         
216                 /* for PVR playback */
217         eFilePushThread *m_pvr_thread;
218         int m_pvr_fd_src, m_pvr_fd_dst;
219         eDVBTSTools m_tstools;
220
221         friend class eUsePtr<eDVBChannel>;
222                 /* use count */
223         oRefCount m_use_count;
224         void AddUse();
225         void ReleaseUse();
226 };
227
228 #endif