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