fix standby
[enigma2.git] / lib / dvb / demux.cpp
1 #include <config.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <signal.h>
8
9
10 #if HAVE_DVB_API_VERSION < 3
11 #include <ost/dmx.h>
12 #ifndef DMX_SET_NEGFILTER_MASK
13         #define DMX_SET_NEGFILTER_MASK   _IOW('o',48,uint8_t *)
14 #endif
15 #else
16 #include <linux/dvb/dmx.h>
17 #endif
18
19 #include "crc32.h"
20
21 #include <lib/base/eerror.h>
22 #include <lib/base/filepush.h>
23 #include <lib/dvb/idvb.h>
24 #include <lib/dvb/demux.h>
25 #include <lib/dvb/esection.h>
26 #include <lib/dvb/decoder.h>
27
28 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
29 {
30         m_dvr_busy = 0;
31 }
32
33 eDVBDemux::~eDVBDemux()
34 {
35 }
36
37 DEFINE_REF(eDVBDemux)
38
39 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
40 {
41         RESULT res;
42         reader = new eDVBSectionReader(this, context, res);
43         if (res)
44                 reader = 0;
45         return res;
46 }
47
48 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
49 {
50         if (m_dvr_busy)
51                 return -EBUSY;
52         recorder = new eDVBTSRecorder(this);
53         return 0;
54 }
55
56 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder)
57 {
58         decoder = new eTSMPEGDecoder(this, 0);
59         return 0;
60 }
61
62 RESULT eDVBDemux::getSTC(pts_t &pts)
63 {
64         char filename[128];
65 #if HAVE_DVB_API_VERSION < 3
66         sprintf(filename, "/dev/dvb/card%d/demux%d", adapter, demux);
67 #else
68         sprintf(filename, "/dev/dvb/adapter%d/demux%d", adapter, demux);
69 #endif
70         int fd = ::open(filename, O_RDWR);
71         
72         if (fd < 0)
73                 return -ENODEV;
74
75         struct dmx_stc stc;
76         stc.num = 0;
77         stc.base = 1;
78         
79         if (ioctl(fd, DMX_GET_STC, &stc) < 0)
80         {
81                 ::close(fd);
82                 return -1;
83         }
84         
85         pts = stc.stc;
86         
87         ::close(fd);
88         return 0;
89 }
90
91 RESULT eDVBDemux::flush()
92 {
93         // FIXME: implement flushing the PVR queue here.
94         
95         m_event(evtFlush);
96         return 0;
97 }
98
99 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
100 {
101         conn = new eConnection(this, m_event.connect(event));
102         return 0;
103 }
104
105 void eDVBSectionReader::data(int)
106 {
107         __u8 data[4096]; // max. section size
108         int r;
109         r = ::read(fd, data, 4096);
110         if(r < 0)
111         {
112                 eWarning("ERROR reading section - %m\n");
113                 return;
114         }
115         if (checkcrc)
116         {
117                         // this check should never happen unless the driver is crappy!
118                 unsigned int c;
119                 if ((c = crc32((unsigned)-1, data, r)))
120                 {
121                         eDebug("crc32 failed! is %x\n", c);
122                         return;
123                 }
124         }
125         if (active)
126                 read(data);
127         else
128                 eDebug("data.. but not active");
129 }
130
131 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
132 {
133         char filename[128];
134 #if HAVE_DVB_API_VERSION < 3
135         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
136 #else
137         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
138 #endif
139         fd = ::open(filename, O_RDWR);
140         
141         eDebug("eDVBSectionReader has fd %d", fd);
142         
143         if (fd >= 0)
144         {
145                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
146                 CONNECT(notifier->activated, eDVBSectionReader::data);
147                 res = 0;
148         } else
149         {
150                 perror(filename);
151                 res = errno;
152         }
153 }
154
155 DEFINE_REF(eDVBSectionReader)
156
157 eDVBSectionReader::~eDVBSectionReader()
158 {
159         if (notifier)
160                 delete notifier;
161         if (fd >= 0)
162                 ::close(fd);
163 }
164
165 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
166 {
167         RESULT res;
168         if (fd < 0)
169                 return -ENODEV;
170
171         notifier->start();
172 #if HAVE_DVB_API_VERSION < 3
173         dmxSctFilterParams sct;
174 #else
175         dmx_sct_filter_params sct;
176 #endif
177         sct.pid     = mask.pid;
178         sct.timeout = 0;
179 #if HAVE_DVB_API_VERSION < 3
180         sct.flags   = 0;
181 #else
182         sct.flags   = DMX_IMMEDIATE_START;
183 #endif
184         if (mask.flags & eDVBSectionFilterMask::rfCRC)
185         {
186                 sct.flags |= DMX_CHECK_CRC;
187                 checkcrc = 1;
188         } else
189                 checkcrc = 0;
190         
191         memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
192         memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
193 #if HAVE_DVB_API_VERSION >= 3
194         memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
195         if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
196                 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
197 #endif
198         
199         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
200         if (!res)
201         {
202 #if HAVE_DVB_API_VERSION < 3
203                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
204                 if (!res)
205                 {
206                         res = ::ioctl(fd, DMX_START, 0);
207                         if (!res)
208                                 active = 1;
209                 }
210 #else
211                 active = 1;
212 #endif
213         }
214         return res;
215 }
216
217 RESULT eDVBSectionReader::stop()
218 {
219         if (!active)
220                 return -1;
221
222         active=0;
223         ::ioctl(fd, DMX_STOP);
224         notifier->stop();
225
226         return 0;
227 }
228
229 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
230 {
231         conn = new eConnection(this, read.connect(r));
232         return 0;
233 }
234
235 DEFINE_REF(eDVBTSRecorder);
236
237 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
238 {
239         m_running = 0;
240         m_format = 0;
241         m_target_fd = -1;
242         m_thread = new eFilePushThread();
243         m_demux->m_dvr_busy = 1;
244 }
245
246 eDVBTSRecorder::~eDVBTSRecorder()
247 {
248         stop();
249         delete m_thread;
250         m_demux->m_dvr_busy = 0;
251 }
252
253 RESULT eDVBTSRecorder::start()
254 {
255         if (m_running)
256                 return -1;
257         
258         if (m_target_fd == -1)
259                 return -2;
260                 
261         char filename[128];
262 #if HAVE_DVB_API_VERSION < 3
263         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
264 #else
265         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
266 #endif
267         m_source_fd = ::open(filename, O_RDONLY);
268         
269         if (m_source_fd < 0)
270         {
271                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
272                 return -3;
273         }
274         
275         m_thread->start(m_source_fd, m_target_fd);
276         m_running = 1;
277         
278         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
279                 startPID(i->first);
280         
281         return 0;
282 }
283
284 RESULT eDVBTSRecorder::addPID(int pid)
285 {
286         if (m_pids.find(pid) != m_pids.end())
287                 return -1;
288         
289         m_pids.insert(std::pair<int,int>(pid, -1));
290         if (m_running)
291                 startPID(pid);
292         return 0;
293 }
294
295 RESULT eDVBTSRecorder::removePID(int pid)
296 {
297         if (m_pids.find(pid) == m_pids.end())
298                 return -1;
299                 
300         if (m_running)
301                 stopPID(pid);
302         
303         m_pids.erase(pid);
304         return 0;
305 }
306
307 RESULT eDVBTSRecorder::setFormat(int format)
308 {
309         if (m_running)
310                 return -1;
311         m_format = format;
312         return 0;
313 }
314
315 RESULT eDVBTSRecorder::setTargetFD(int fd)
316 {
317         m_target_fd = fd;
318         return 0;
319 }
320
321 RESULT eDVBTSRecorder::setBoundary(off_t max)
322 {
323         return -1; // not yet implemented
324 }
325
326 RESULT eDVBTSRecorder::stop()
327 {
328         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
329                 stopPID(i->first);
330
331         if (!m_running)
332                 return -1;
333         m_thread->stop();
334         
335         close(m_source_fd);
336         
337         return 0;
338 }
339
340 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
341 {
342         conn = new eConnection(this, m_event.connect(event));
343         return 0;
344 }
345
346 RESULT eDVBTSRecorder::startPID(int pid)
347 {
348         char filename[128];
349 #if HAVE_DVB_API_VERSION < 3
350         snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
351 #else
352         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
353 #endif
354         int fd = ::open(filename, O_RDWR);
355         if (fd < 0)
356         {
357                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
358                 return -1;
359         }
360
361 #if HAVE_DVB_API_VERSION < 3
362         dmxPesFilterParams flt;
363         
364         flt.pesType = DMX_PES_OTHER;
365 #else
366         dmx_pes_filter_params flt;
367         
368         flt.pes_type = DMX_PES_OTHER;
369 #endif
370
371         flt.pid     = pid;
372         flt.input   = DMX_IN_FRONTEND;
373         flt.output  = DMX_OUT_TS_TAP;
374         
375         flt.flags   = DMX_IMMEDIATE_START;
376
377         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
378         if (res < 0)
379         {
380                 eDebug("set pes filter failed!");
381                 ::close(fd);
382                 return -1;
383         }
384         m_pids[pid] = fd;
385
386         return 0;
387 }
388
389 void eDVBTSRecorder::stopPID(int pid)
390 {
391         ::close(m_pids[pid]);
392         m_pids[pid] = -1;
393 }