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