- add flushing support in demux / decoder
[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 #endif
196         
197         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
198         if (!res)
199         {
200 #if HAVE_DVB_API_VERSION < 3
201                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
202                 if (!res)
203                 {
204                         res = ::ioctl(fd, DMX_START, 0);
205                         if (!res)
206                                 active = 1;
207                 }
208 #else
209                 active = 1;
210 #endif
211         }
212         return res;
213 }
214
215 RESULT eDVBSectionReader::stop()
216 {
217         if (!active)
218                 return -1;
219
220         active=0;
221         ::ioctl(fd, DMX_STOP);
222         notifier->stop();
223
224         return 0;
225 }
226
227 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
228 {
229         conn = new eConnection(this, read.connect(r));
230         return 0;
231 }
232
233 DEFINE_REF(eDVBTSRecorder);
234
235 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
236 {
237         m_running = 0;
238         m_format = 0;
239         m_target_fd = -1;
240         m_thread = new eFilePushThread();
241         m_demux->m_dvr_busy = 1;
242 }
243
244 eDVBTSRecorder::~eDVBTSRecorder()
245 {
246         stop();
247         delete m_thread;
248         m_demux->m_dvr_busy = 0;
249 }
250
251 RESULT eDVBTSRecorder::start()
252 {
253         if (m_running)
254                 return -1;
255         
256         if (m_target_fd == -1)
257                 return -2;
258                 
259         char filename[128];
260 #if HAVE_DVB_API_VERSION < 3
261         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
262 #else
263         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
264 #endif
265         m_source_fd = ::open(filename, O_RDONLY);
266         
267         if (m_source_fd < 0)
268         {
269                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
270                 return -3;
271         }
272         
273         m_thread->start(m_source_fd, m_target_fd);
274         m_running = 1;
275         
276         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
277                 startPID(i->first);
278         
279         return 0;
280 }
281
282 RESULT eDVBTSRecorder::addPID(int pid)
283 {
284         if (m_pids.find(pid) != m_pids.end())
285                 return -1;
286         
287         m_pids.insert(std::pair<int,int>(pid, -1));
288         if (m_running)
289                 startPID(pid);
290         return 0;
291 }
292
293 RESULT eDVBTSRecorder::removePID(int pid)
294 {
295         if (m_pids.find(pid) == m_pids.end())
296                 return -1;
297                 
298         if (m_running)
299                 stopPID(pid);
300         
301         m_pids.erase(pid);
302         return 0;
303 }
304
305 RESULT eDVBTSRecorder::setFormat(int format)
306 {
307         if (m_running)
308                 return -1;
309         m_format = format;
310         return 0;
311 }
312
313 RESULT eDVBTSRecorder::setTargetFD(int fd)
314 {
315         m_target_fd = fd;
316         return 0;
317 }
318
319 RESULT eDVBTSRecorder::setBoundary(off_t max)
320 {
321         return -1; // not yet implemented
322 }
323
324 RESULT eDVBTSRecorder::stop()
325 {
326         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
327                 stopPID(i->first);
328
329         if (!m_running)
330                 return -1;
331         m_thread->stop();
332         
333         close(m_source_fd);
334         
335         return 0;
336 }
337
338 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
339 {
340         conn = new eConnection(this, m_event.connect(event));
341         return 0;
342 }
343
344 RESULT eDVBTSRecorder::startPID(int pid)
345 {
346         char filename[128];
347 #if HAVE_DVB_API_VERSION < 3
348         snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
349 #else
350         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
351 #endif
352         int fd = ::open(filename, O_RDWR);
353         if (fd < 0)
354         {
355                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
356                 return -1;
357         }
358
359 #if HAVE_DVB_API_VERSION < 3
360         dmxPesFilterParams flt;
361         
362         flt.pesType = DMX_PES_OTHER;
363 #else
364         dmx_pes_filter_params flt;
365         
366         flt.pes_type = DMX_PES_OTHER;
367 #endif
368
369         flt.pid     = pid;
370         flt.input   = DMX_IN_FRONTEND;
371         flt.output  = DMX_OUT_TS_TAP;
372         
373         flt.flags   = DMX_IMMEDIATE_START;
374
375         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
376         if (res < 0)
377         {
378                 eDebug("set pes filter failed!");
379                 ::close(fd);
380                 return -1;
381         }
382         m_pids[pid] = fd;
383
384         return 0;
385 }
386
387 void eDVBTSRecorder::stopPID(int pid)
388 {
389         ::close(m_pids[pid]);
390         m_pids[pid] = -1;
391 }