fix
[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 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
19 {
20 DECLARE_REF(eDVBRegisteredFrontend);
21 public:
22         iDVBAdapter *m_adapter;
23         ePtr<eDVBFrontend> m_frontend;
24         int m_inuse;
25         eDVBRegisteredFrontend(eDVBFrontend *fe, iDVBAdapter *adap): m_adapter(adap), m_frontend(fe), m_inuse(0) { }
26 };
27
28 struct eDVBRegisteredDemux
29 {
30 DECLARE_REF(eDVBRegisteredDemux);
31 public:
32         iDVBAdapter *m_adapter;
33         ePtr<eDVBDemux> m_demux;
34         int m_inuse;
35         eDVBRegisteredDemux(eDVBDemux *demux, iDVBAdapter *adap): m_adapter(adap), m_demux(demux), m_inuse(0) { }
36 };
37
38 class eDVBAllocatedFrontend
39 {
40 DECLARE_REF(eDVBAllocatedFrontend);
41 public:
42         
43         eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe);
44         ~eDVBAllocatedFrontend();
45         eDVBFrontend &get() { return *m_fe->m_frontend; }
46         operator eDVBRegisteredFrontend*() { return m_fe; }
47         operator eDVBFrontend*() { return m_fe->m_frontend; }
48
49 private:
50         eDVBRegisteredFrontend *m_fe;
51 };
52
53 class eDVBAllocatedDemux
54 {
55 DECLARE_REF(eDVBAllocatedDemux);
56 public:
57         
58         eDVBAllocatedDemux(eDVBRegisteredDemux *demux);
59         ~eDVBAllocatedDemux();
60         eDVBDemux &get() { return *m_demux->m_demux; }
61         operator eDVBRegisteredDemux*() { return m_demux; }
62         operator eDVBDemux*() { return m_demux->m_demux; }
63         
64 private:
65         eDVBRegisteredDemux *m_demux;
66 };
67
68 class iDVBAdapter: public iObject
69 {
70 public:
71         virtual int getNumDemux() = 0;
72         virtual RESULT getDemux(ePtr<eDVBDemux> &demux, int nr) = 0;
73         
74         virtual int getNumFrontends() = 0;
75         virtual RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr) = 0;
76 };
77
78 class eDVBAdapterLinux: public iDVBAdapter
79 {
80 DECLARE_REF(eDVBAdapterLinux);
81 public:
82         eDVBAdapterLinux(int nr);
83
84         int getNumDemux();
85         RESULT getDemux(ePtr<eDVBDemux> &demux, int nr);
86         
87         int getNumFrontends();
88         RESULT getFrontend(ePtr<eDVBFrontend> &fe, int nr);
89         
90         static int exist(int nr);
91 private:
92         int m_nr;
93         eSmartPtrList<eDVBFrontend> m_frontend;
94         eSmartPtrList<eDVBDemux>    m_demux;
95 };
96
97 class eDVBResourceManager: public iObject
98 {
99         DECLARE_REF(eDVBResourceManager);
100         int avail, busy;
101         
102         eSmartPtrList<iDVBAdapter> m_adapter;
103         
104         eSmartPtrList<eDVBRegisteredDemux> m_demux;
105         eSmartPtrList<eDVBRegisteredFrontend> m_frontend;
106         
107         void addAdapter(iDVBAdapter *adapter);
108         
109                         /* allocates a frontend able to tune to frontend paramters 'feperm'.
110                            the frontend must be tuned lateron. there is no guarante
111                            that tuning will succeed - it just means that if this frontend
112                            can't tune, no other frontend could do it.
113                            
114                            there might be a priority given to certain frontend/chid 
115                            combinations. this will be evaluated here. */
116                            
117         RESULT allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm);
118         RESULT allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int index);
119         
120                         /* allocate a demux able to filter on the selected frontend. */
121         RESULT allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int cap);
122         
123         struct active_channel
124         {
125                 eDVBChannelID m_channel_id;
126                         /* we don't hold a reference here. */
127                 eDVBChannel *m_channel;
128                 
129                 active_channel(const eDVBChannelID &chid, eDVBChannel *ch) : m_channel_id(chid), m_channel(ch) { }
130         };
131         
132         std::list<active_channel> m_active_channels;
133         
134         ePtr<iDVBChannelList> m_list;
135         ePtr<iDVBSatelliteEquipmentControl> m_sec;
136         static eDVBResourceManager *instance;
137         
138         friend class eDVBChannel;
139         RESULT addChannel(const eDVBChannelID &chid, eDVBChannel *ch);
140         RESULT removeChannel(eDVBChannel *ch);
141         
142         Signal1<void,eDVBChannel*> m_channelAdded;
143         Signal1<void,eDVBChannel*> m_channelRemoved;
144         Signal1<void,iDVBChannel*> m_channelRunning;
145 public:
146         eDVBResourceManager();
147         virtual ~eDVBResourceManager();
148         
149         static RESULT getInstance(ePtr<eDVBResourceManager> &ptr) { if (instance) { ptr = instance; return 0; } return -1; }
150         
151         RESULT setChannelList(iDVBChannelList *list);
152         RESULT getChannelList(ePtr<iDVBChannelList> &list);
153         
154         enum {
155                 errNoFrontend = -1,
156                 errNoDemux    = -2,
157                 errChidNotFound = -3
158         };
159         
160                 /* allocate channel... */
161         RESULT allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel);
162         RESULT allocateRawChannel(eUsePtr<iDVBChannel> &channel, int frontend_index);
163         RESULT allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel);
164
165         RESULT connectChannelAdded(const Slot1<void,eDVBChannel*> &channelAdded, ePtr<eConnection> &connection);
166         RESULT connectChannelRemoved(const Slot1<void,eDVBChannel*> &channelRemoved, ePtr<eConnection> &connection);
167         RESULT connectChannelRunning(const Slot1<void,iDVBChannel*> &channelRemoved, ePtr<eConnection> &connection);
168 };
169
170 class eFilePushThread;
171
172         /* iDVBPVRChannel includes iDVBChannel. don't panic. */
173 class eDVBChannel: public iDVBPVRChannel, public Object
174 {
175         DECLARE_REF(eDVBChannel);
176 public:
177         eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend);
178         virtual ~eDVBChannel();
179
180                 /* only for managed channels - effectively tunes to the channelid. should not be used... */
181                 /* cannot be used for PVR channels. */
182         RESULT setChannel(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &feparam);
183         eDVBChannelID getChannelID() { return m_channel_id; }
184
185         RESULT connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection);
186         RESULT getState(int &state);
187
188         RESULT setCIRouting(const eDVBCIRouting &routing);
189         RESULT getDemux(ePtr<iDVBDemux> &demux, int cap);
190         RESULT getFrontend(ePtr<iDVBFrontend> &frontend);
191         
192                 /* iDVBPVRChannel */
193         RESULT playFile(const char *file);
194         RESULT getLength(pts_t &len);
195         RESULT getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos);
196         RESULT seekTo(iDVBDemux *decoding_demux, int relative, pts_t &pts);
197                         /* seeking to relative positions won't work - 
198                            there is an unknown amount of buffers in between */
199         RESULT seekToPosition(iDVBDemux *decoding_demux, const off_t &off);
200
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