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