user xml.sax instead of xml.dom for satellites.xml parsing
[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
92 void eDVBSectionReader::data(int)
93 {
94         __u8 data[4096]; // max. section size
95         int r;
96         r = ::read(fd, data, 4096);
97         if(r < 0)
98         {
99                 eWarning("ERROR reading section - %m\n");
100                 return;
101         }
102         if (checkcrc)
103         {
104                         // this check should never happen unless the driver is crappy!
105                 unsigned int c;
106                 if ((c = crc32((unsigned)-1, data, r)))
107                 {
108                         eDebug("crc32 failed! is %x\n", c);
109                         return;
110                 }
111         }
112         if (active)
113                 read(data);
114         else
115                 eDebug("data.. but not active");
116 }
117
118 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
119 {
120         char filename[128];
121 #if HAVE_DVB_API_VERSION < 3
122         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
123 #else
124         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
125 #endif
126         fd = ::open(filename, O_RDWR);
127         
128         eDebug("eDVBSectionReader has fd %d", fd);
129         
130         if (fd >= 0)
131         {
132                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
133                 CONNECT(notifier->activated, eDVBSectionReader::data);
134                 res = 0;
135         } else
136         {
137                 perror(filename);
138                 res = errno;
139         }
140 }
141
142 DEFINE_REF(eDVBSectionReader)
143
144 eDVBSectionReader::~eDVBSectionReader()
145 {
146         if (notifier)
147                 delete notifier;
148         if (fd >= 0)
149                 ::close(fd);
150 }
151
152 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
153 {
154         RESULT res;
155         if (fd < 0)
156                 return -ENODEV;
157
158         notifier->start();
159 #if HAVE_DVB_API_VERSION < 3
160         dmxSctFilterParams sct;
161 #else
162         dmx_sct_filter_params sct;
163 #endif
164         sct.pid     = mask.pid;
165         sct.timeout = 0;
166 #if HAVE_DVB_API_VERSION < 3
167         sct.flags   = 0;
168 #else
169         sct.flags   = DMX_IMMEDIATE_START;
170 #endif
171         if (mask.flags & eDVBSectionFilterMask::rfCRC)
172         {
173                 sct.flags |= DMX_CHECK_CRC;
174                 checkcrc = 1;
175         } else
176                 checkcrc = 0;
177         
178         memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
179         memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
180 #if HAVE_DVB_API_VERSION >= 3
181         memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
182 #endif
183         
184         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
185         if (!res)
186         {
187 #if HAVE_DVB_API_VERSION < 3
188                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
189                 if (!res)
190                 {
191                         res = ::ioctl(fd, DMX_START, 0);
192                         if (!res)
193                                 active = 1;
194                 }
195 #else
196                 active = 1;
197 #endif
198         }
199         return res;
200 }
201
202 RESULT eDVBSectionReader::stop()
203 {
204         if (!active)
205                 return -1;
206
207         active=0;
208         ::ioctl(fd, DMX_STOP);
209         notifier->stop();
210
211         return 0;
212 }
213
214 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
215 {
216         conn = new eConnection(this, read.connect(r));
217         return 0;
218 }
219
220 DEFINE_REF(eDVBTSRecorder);
221
222 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
223 {
224         m_running = 0;
225         m_format = 0;
226         m_target_fd = -1;
227         m_thread = new eFilePushThread();
228         m_demux->m_dvr_busy = 1;
229 }
230
231 eDVBTSRecorder::~eDVBTSRecorder()
232 {
233         stop();
234         delete m_thread;
235         m_demux->m_dvr_busy = 0;
236 }
237
238 RESULT eDVBTSRecorder::start()
239 {
240         if (m_running)
241                 return -1;
242         
243         if (m_target_fd == -1)
244                 return -2;
245                 
246         char filename[128];
247 #if HAVE_DVB_API_VERSION < 3
248         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
249 #else
250         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
251 #endif
252         m_source_fd = ::open(filename, O_RDONLY);
253         
254         if (m_source_fd < 0)
255         {
256                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
257                 return -3;
258         }
259         
260         m_thread->start(m_source_fd, m_target_fd);
261         m_running = 1;
262         
263         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
264                 startPID(i->first);
265         
266         return 0;
267 }
268
269 RESULT eDVBTSRecorder::addPID(int pid)
270 {
271         if (m_pids.find(pid) != m_pids.end())
272                 return -1;
273         
274         m_pids.insert(std::pair<int,int>(pid, -1));
275         if (m_running)
276                 startPID(pid);
277         return 0;
278 }
279
280 RESULT eDVBTSRecorder::removePID(int pid)
281 {
282         if (m_pids.find(pid) == m_pids.end())
283                 return -1;
284                 
285         if (m_running)
286                 stopPID(pid);
287         
288         m_pids.erase(pid);
289         return 0;
290 }
291
292 RESULT eDVBTSRecorder::setFormat(int format)
293 {
294         if (m_running)
295                 return -1;
296         m_format = format;
297         return 0;
298 }
299
300 RESULT eDVBTSRecorder::setTargetFD(int fd)
301 {
302         m_target_fd = fd;
303         return 0;
304 }
305
306 RESULT eDVBTSRecorder::setBoundary(off_t max)
307 {
308         return -1; // not yet implemented
309 }
310
311 RESULT eDVBTSRecorder::stop()
312 {
313         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
314                 stopPID(i->first);
315
316         if (!m_running)
317                 return -1;
318         m_thread->stop();
319         
320         close(m_source_fd);
321         
322         return 0;
323 }
324
325 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
326 {
327         conn = new eConnection(this, m_event.connect(event));
328         return 0;
329 }
330
331 RESULT eDVBTSRecorder::startPID(int pid)
332 {
333         char filename[128];
334 #if HAVE_DVB_API_VERSION < 3
335         snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
336 #else
337         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
338 #endif
339         int fd = ::open(filename, O_RDWR);
340         if (fd < 0)
341         {
342                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
343                 return -1;
344         }
345
346 #if HAVE_DVB_API_VERSION < 3
347         dmxPesFilterParams flt;
348         
349         flt.pesType = DMX_PES_OTHER;
350 #else
351         dmx_pes_filter_params flt;
352         
353         flt.pes_type = DMX_PES_OTHER;
354 #endif
355
356         flt.pid     = pid;
357         flt.input   = DMX_IN_FRONTEND;
358         flt.output  = DMX_OUT_TS_TAP;
359         
360         flt.flags   = DMX_IMMEDIATE_START;
361
362         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
363         if (res < 0)
364         {
365                 eDebug("set pes filter failed!");
366                 ::close(fd);
367                 return -1;
368         }
369         m_pids[pid] = fd;
370
371         return 0;
372 }
373
374 void eDVBTSRecorder::stopPID(int pid)
375 {
376         ::close(m_pids[pid]);
377         m_pids[pid] = -1;
378 }