hack to have primary and secondary decoder (for now)
[enigma2.git] / lib / dvb / demux.cpp
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <sys/ioctl.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <signal.h>
7
8 #if HAVE_DVB_API_VERSION < 3
9 #include <ost/dmx.h>
10
11 #ifndef DMX_SET_NEGFILTER_MASK
12         #define DMX_SET_NEGFILTER_MASK   _IOW('o',48,uint8_t *)
13 #endif
14
15 #ifndef DMX_GET_STC
16         struct dmx_stc
17         {
18                 unsigned int num;       /* input : which STC? O..N */
19                 unsigned int base;      /* output: divisor for stc to get 90 kHz clock */
20                 unsigned long long stc; /* output: src in 'base'*90 kHz units */
21         };
22         #define DMX_GET_STC             _IOR('o', 50, struct dmx_stc)
23 #endif
24
25 #else
26 #include <linux/dvb/dmx.h>
27
28 // #define HAVE_ADD_PID
29
30 #ifdef HAVE_ADD_PID
31 #define DMX_ADD_PID              _IO('o', 51)
32 #define DMX_REMOVE_PID           _IO('o', 52)
33
34 typedef enum {
35         DMX_TAP_TS = 0,
36         DMX_TAP_PES = DMX_PES_OTHER, /* for backward binary compat. */
37 } dmx_tap_type_t;
38
39 #endif
40
41 #endif
42
43 #include "crc32.h"
44
45 #include <lib/base/eerror.h>
46 #include <lib/base/filepush.h>
47 #include <lib/dvb/idvb.h>
48 #include <lib/dvb/demux.h>
49 #include <lib/dvb/esection.h>
50 #include <lib/dvb/decoder.h>
51 #include <lib/dvb/pvrparse.h>
52
53 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
54 {
55         m_dvr_busy = 0;
56 }
57
58 eDVBDemux::~eDVBDemux()
59 {
60 }
61
62 int eDVBDemux::openDemux(void)
63 {
64         char filename[128];
65 #if HAVE_DVB_API_VERSION < 3
66         snprintf(filename, 128, "/dev/dvb/card%d/demux%d", adapter, demux);
67 #else
68         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", adapter, demux);
69 #endif
70         return ::open(filename, O_RDWR);
71 }
72
73 DEFINE_REF(eDVBDemux)
74
75 RESULT eDVBDemux::setSourceFrontend(int fenum)
76 {
77 #if HAVE_DVB_API_VERSION >= 3
78         int fd = openDemux();
79         
80         int n = DMX_SOURCE_FRONT0 + fenum;
81         int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
82         if (res)
83                 eDebug("DMX_SET_SOURCE failed! - %m");
84         ::close(fd);
85         return res;
86 #endif
87         return 0;
88 }
89
90 RESULT eDVBDemux::setSourcePVR(int pvrnum)
91 {
92 #if HAVE_DVB_API_VERSION >= 3
93         int fd = openDemux();
94         int n = DMX_SOURCE_DVR0 + pvrnum;
95         int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
96         ::close(fd);
97         return res;
98 #endif
99         return 0;
100 }
101
102 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
103 {
104         RESULT res;
105         reader = new eDVBSectionReader(this, context, res);
106         if (res)
107                 reader = 0;
108         return res;
109 }
110
111 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
112 {
113         if (m_dvr_busy)
114                 return -EBUSY;
115         recorder = new eDVBTSRecorder(this);
116         return 0;
117 }
118
119 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
120 {
121         decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
122         return 0;
123 }
124
125 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
126 {
127         int fd = openDemux();
128         
129         if (fd < 0)
130                 return -ENODEV;
131
132         struct dmx_stc stc;
133         stc.num = num;
134         stc.base = 1;
135         
136         if (ioctl(fd, DMX_GET_STC, &stc) < 0)
137         {
138                 ::close(fd);
139                 return -1;
140         }
141         
142         pts = stc.stc;
143         
144         ::close(fd);
145         return 0;
146 }
147
148 RESULT eDVBDemux::flush()
149 {
150         // FIXME: implement flushing the PVR queue here.
151         
152         m_event(evtFlush);
153         return 0;
154 }
155
156 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
157 {
158         conn = new eConnection(this, m_event.connect(event));
159         return 0;
160 }
161
162 void eDVBSectionReader::data(int)
163 {
164         __u8 data[4096]; // max. section size
165         int r;
166         r = ::read(fd, data, 4096);
167         if(r < 0)
168         {
169                 eWarning("ERROR reading section - %m\n");
170                 return;
171         }
172         if (checkcrc)
173         {
174                         // this check should never happen unless the driver is crappy!
175                 unsigned int c;
176                 if ((c = crc32((unsigned)-1, data, r)))
177                 {
178                         eDebug("crc32 failed! is %x\n", c);
179                         return;
180                 }
181         }
182         if (active)
183                 read(data);
184         else
185                 eDebug("data.. but not active");
186 }
187
188 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
189 {
190         char filename[128];
191         fd = demux->openDemux();
192         
193         if (fd >= 0)
194         {
195                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
196                 CONNECT(notifier->activated, eDVBSectionReader::data);
197                 res = 0;
198         } else
199         {
200                 perror(filename);
201                 res = errno;
202         }
203 }
204
205 DEFINE_REF(eDVBSectionReader)
206
207 eDVBSectionReader::~eDVBSectionReader()
208 {
209         if (notifier)
210                 delete notifier;
211         if (fd >= 0)
212                 ::close(fd);
213 }
214
215 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
216 {
217         RESULT res;
218         if (fd < 0)
219                 return -ENODEV;
220
221         notifier->start();
222 #if HAVE_DVB_API_VERSION < 3
223         dmxSctFilterParams sct;
224 #else
225         dmx_sct_filter_params sct;
226 #endif
227         sct.pid     = mask.pid;
228         sct.timeout = 0;
229 #if HAVE_DVB_API_VERSION < 3
230         sct.flags   = 0;
231 #else
232         sct.flags   = DMX_IMMEDIATE_START;
233 #endif
234         if (mask.flags & eDVBSectionFilterMask::rfCRC)
235         {
236                 sct.flags |= DMX_CHECK_CRC;
237                 checkcrc = 1;
238         } else
239                 checkcrc = 0;
240         
241         memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
242         memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
243 #if HAVE_DVB_API_VERSION >= 3
244         memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
245         if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
246                 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
247 #endif
248         
249         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
250         if (!res)
251         {
252 #if HAVE_DVB_API_VERSION < 3
253                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
254                 if (!res)
255                 {
256                         res = ::ioctl(fd, DMX_START, 0);
257                         if (!res)
258                                 active = 1;
259                 }
260 #else
261                 active = 1;
262 #endif
263         }
264         return res;
265 }
266
267 RESULT eDVBSectionReader::stop()
268 {
269         if (!active)
270                 return -1;
271
272         active=0;
273         ::ioctl(fd, DMX_STOP);
274         notifier->stop();
275
276         return 0;
277 }
278
279 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
280 {
281         conn = new eConnection(this, read.connect(r));
282         return 0;
283 }
284
285 class eDVBRecordFileThread: public eFilePushThread
286 {
287 public:
288         eDVBRecordFileThread();
289         void setTimingPID(int pid);
290         
291         void saveTimingInformation(const std::string &filename);
292 protected:
293         void filterRecordData(const unsigned char *data, int len);
294 private:
295         eMPEGStreamParserTS m_ts_parser;
296         eMPEGStreamInformation m_stream_info;
297         off_t m_current_offset;
298         int m_pid;
299 };
300
301 eDVBRecordFileThread::eDVBRecordFileThread()
302         : m_ts_parser(m_stream_info)
303 {
304         m_current_offset = 0;
305 }
306
307 void eDVBRecordFileThread::setTimingPID(int pid)
308 {
309         m_ts_parser.setPid(pid);
310 }
311
312 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
313 {
314         m_stream_info.save(filename.c_str());
315 }
316
317 void eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len)
318 {
319         m_ts_parser.parseData(m_current_offset, data, len);
320         
321         m_current_offset += len;
322 }
323
324 DEFINE_REF(eDVBTSRecorder);
325
326 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
327 {
328         m_running = 0;
329         m_target_fd = -1;
330         m_thread = new eDVBRecordFileThread();
331 #ifndef HAVE_ADD_PID
332         m_demux->m_dvr_busy = 1;
333 #endif
334 }
335
336 eDVBTSRecorder::~eDVBTSRecorder()
337 {
338         stop();
339         delete m_thread;
340 #ifndef HAVE_ADD_PID
341         m_demux->m_dvr_busy = 0;
342 #endif
343 }
344
345 RESULT eDVBTSRecorder::start()
346 {
347         if (m_running)
348                 return -1;
349         
350         if (m_target_fd == -1)
351                 return -2;
352
353         char filename[128];
354 #ifndef HAVE_ADD_PID
355 #if HAVE_DVB_API_VERSION < 3
356         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
357 #else
358         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
359 #endif
360         m_source_fd = ::open(filename, O_RDONLY);
361         
362         if (m_source_fd < 0)
363         {
364                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
365                 return -3;
366         }
367 #else
368         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
369
370         m_source_fd = ::open(filename, O_RDONLY);
371         
372         if (m_source_fd < 0)
373         {
374                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
375                 return -3;
376         }
377         
378         ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
379
380         dmx_pes_filter_params flt;
381         flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
382         flt.pid     = 0x1234; /* FIXME */
383         flt.input   = DMX_IN_FRONTEND;
384         flt.output  = DMX_OUT_TAP;
385         flt.flags   = 0;
386         int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
387         if (res)
388         {
389                 eDebug("DMX_SET_PES_FILTER: %m");
390                 ::close(m_source_fd);
391                 return -3;
392         }
393         
394         ::ioctl(m_source_fd, DMX_START);
395         
396 #endif
397         
398         m_thread->start(m_source_fd, m_target_fd);
399         m_running = 1;
400         
401         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
402                 startPID(i->first);
403         
404         return 0;
405 }
406
407 RESULT eDVBTSRecorder::addPID(int pid)
408 {
409         if (m_pids.find(pid) != m_pids.end())
410                 return -1;
411         
412         m_pids.insert(std::pair<int,int>(pid, -1));
413         if (m_running)
414                 startPID(pid);
415         return 0;
416 }
417
418 RESULT eDVBTSRecorder::removePID(int pid)
419 {
420         if (m_pids.find(pid) == m_pids.end())
421                 return -1;
422                 
423         if (m_running)
424                 stopPID(pid);
425         
426         m_pids.erase(pid);
427         return 0;
428 }
429
430 RESULT eDVBTSRecorder::setTimingPID(int pid)
431 {
432         if (m_running)
433                 return -1;
434         m_thread->setTimingPID(pid);
435         return 0;
436 }
437
438 RESULT eDVBTSRecorder::setTargetFD(int fd)
439 {
440         m_target_fd = fd;
441         return 0;
442 }
443
444 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
445 {
446         m_target_filename = filename;
447         return 0;
448 }
449
450 RESULT eDVBTSRecorder::setBoundary(off_t max)
451 {
452         return -1; // not yet implemented
453 }
454
455 RESULT eDVBTSRecorder::stop()
456 {
457         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
458                 stopPID(i->first);
459
460         if (!m_running)
461                 return -1;
462         m_thread->stop();
463         
464         close(m_source_fd);
465         
466         if (m_target_filename != "")
467                 m_thread->saveTimingInformation(m_target_filename + ".ap");
468         
469         return 0;
470 }
471
472 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
473 {
474         conn = new eConnection(this, m_event.connect(event));
475         return 0;
476 }
477
478 RESULT eDVBTSRecorder::startPID(int pid)
479 {
480 #ifndef HAVE_ADD_PID
481         int fd = m_demux->openDemux();
482         if (fd < 0)
483         {
484                 eDebug("FAILED to open demux in ts recoder (%m)");
485                 return -1;
486         }
487
488 #if HAVE_DVB_API_VERSION < 3
489         dmxPesFilterParams flt;
490         
491         flt.pesType = DMX_PES_OTHER;
492 #else
493         dmx_pes_filter_params flt;
494         
495         flt.pes_type = DMX_PES_OTHER;
496 #endif
497
498         flt.pid     = pid;
499         flt.input   = DMX_IN_FRONTEND;
500         flt.output  = DMX_OUT_TS_TAP;
501         
502         flt.flags   = DMX_IMMEDIATE_START;
503
504         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
505         if (res < 0)
506         {
507                 eDebug("set pes filter failed!");
508                 ::close(fd);
509                 return -1;
510         }
511         m_pids[pid] = fd;
512 #else
513         eDebug("add pid: %08x", pid);
514         if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
515                 perror("DMX_ADD_PID");
516         eDebug("ok");
517 #endif
518         return 0;
519 }
520
521 void eDVBTSRecorder::stopPID(int pid)
522 {
523 #ifndef HAVE_ADD_PID
524         if (m_pids[pid] != -1)
525                 ::close(m_pids[pid]);
526         m_pids[pid] = -1;
527 #else
528         if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
529                 perror("DMX_REMOVE_PID");
530 #endif
531 }