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