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