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