make retransmit rotor diseqc command retries changable
[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::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
112 {
113         RESULT res;
114         reader = new eDVBPESReader(this, context, res);
115         if (res)
116                 reader = 0;
117         return res;
118 }
119
120 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
121 {
122         if (m_dvr_busy)
123                 return -EBUSY;
124         recorder = new eDVBTSRecorder(this);
125         return 0;
126 }
127
128 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
129 {
130         decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
131         return 0;
132 }
133
134 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
135 {
136         int fd = openDemux();
137         
138         if (fd < 0)
139                 return -ENODEV;
140
141         struct dmx_stc stc;
142         stc.num = num;
143         stc.base = 1;
144         
145         if (ioctl(fd, DMX_GET_STC, &stc) < 0)
146         {
147                 ::close(fd);
148                 return -1;
149         }
150         
151         pts = stc.stc;
152         
153         ::close(fd);
154         return 0;
155 }
156
157 RESULT eDVBDemux::flush()
158 {
159         // FIXME: implement flushing the PVR queue here.
160         
161         m_event(evtFlush);
162         return 0;
163 }
164
165 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
166 {
167         conn = new eConnection(this, m_event.connect(event));
168         return 0;
169 }
170
171 void eDVBSectionReader::data(int)
172 {
173         __u8 data[4096]; // max. section size
174         int r;
175         r = ::read(fd, data, 4096);
176         if(r < 0)
177         {
178                 eWarning("ERROR reading section - %m\n");
179                 return;
180         }
181         if (checkcrc)
182         {
183                         // this check should never happen unless the driver is crappy!
184                 unsigned int c;
185                 if ((c = crc32((unsigned)-1, data, r)))
186                 {
187                         eDebug("crc32 failed! is %x\n", c);
188                         return;
189                 }
190         }
191         if (active)
192                 read(data);
193         else
194                 eDebug("data.. but not active");
195 }
196
197 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
198 {
199         char filename[128];
200         fd = demux->openDemux();
201         
202         if (fd >= 0)
203         {
204                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
205                 CONNECT(notifier->activated, eDVBSectionReader::data);
206                 res = 0;
207         } else
208         {
209                 perror(filename);
210                 res = errno;
211         }
212 }
213
214 DEFINE_REF(eDVBSectionReader)
215
216 eDVBSectionReader::~eDVBSectionReader()
217 {
218         if (notifier)
219                 delete notifier;
220         if (fd >= 0)
221                 ::close(fd);
222 }
223
224 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
225 {
226         RESULT res;
227         if (fd < 0)
228                 return -ENODEV;
229
230         notifier->start();
231 #if HAVE_DVB_API_VERSION < 3
232         dmxSctFilterParams sct;
233 #else
234         dmx_sct_filter_params sct;
235 #endif
236         sct.pid     = mask.pid;
237         sct.timeout = 0;
238 #if HAVE_DVB_API_VERSION < 3
239         sct.flags   = 0;
240 #else
241         sct.flags   = DMX_IMMEDIATE_START;
242 #endif
243         if (mask.flags & eDVBSectionFilterMask::rfCRC)
244         {
245                 sct.flags |= DMX_CHECK_CRC;
246                 checkcrc = 1;
247         } else
248                 checkcrc = 0;
249         
250         memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
251         memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
252 #if HAVE_DVB_API_VERSION >= 3
253         memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
254         if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
255                 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
256 #endif
257         
258         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
259         if (!res)
260         {
261 #if HAVE_DVB_API_VERSION < 3
262                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
263                 if (!res)
264                 {
265                         res = ::ioctl(fd, DMX_START, 0);
266                         if (!res)
267                                 active = 1;
268                 }
269 #else
270                 active = 1;
271 #endif
272         }
273         return res;
274 }
275
276 RESULT eDVBSectionReader::stop()
277 {
278         if (!active)
279                 return -1;
280
281         active=0;
282         ::ioctl(fd, DMX_STOP);
283         notifier->stop();
284
285         return 0;
286 }
287
288 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
289 {
290         conn = new eConnection(this, read.connect(r));
291         return 0;
292 }
293
294 void eDVBPESReader::data(int)
295 {
296         while (1)
297         {
298                 __u8 buffer[16384];
299                 int r;
300                 r = ::read(m_fd, buffer, 16384);
301                 if (!r)
302                         return;
303                 if(r < 0)
304                 {
305                         if (errno == EAGAIN) /* ok */
306                                 return;
307                         eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
308                         return;
309                 }
310
311                 if (m_active)
312                         m_read(buffer, r);
313                 else
314                         eWarning("PES reader not active");
315                 if (r != 16384)
316                         break;
317         }
318 }
319
320 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
321 {
322         char filename[128];
323         m_fd = m_demux->openDemux();
324         
325         if (m_fd >= 0)
326         {
327                 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
328                 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
329                 m_notifier = new eSocketNotifier(context, m_fd, eSocketNotifier::Read, false);
330                 CONNECT(m_notifier->activated, eDVBPESReader::data);
331                 res = 0;
332         } else
333         {
334                 perror(filename);
335                 res = errno;
336         }
337 }
338
339 DEFINE_REF(eDVBPESReader)
340
341 eDVBPESReader::~eDVBPESReader()
342 {
343         if (m_notifier)
344                 delete m_notifier;
345         if (m_fd >= 0)
346                 ::close(m_fd);
347 }
348
349 RESULT eDVBPESReader::start(int pid)
350 {
351         RESULT res;
352         if (m_fd < 0)
353                 return -ENODEV;
354
355         m_notifier->start();
356
357 #if HAVE_DVB_API_VERSION < 3
358         dmxPesFilterParams flt;
359         
360         flt.pesType = DMX_PES_OTHER;
361 #else
362         dmx_pes_filter_params flt;
363         
364         flt.pes_type = DMX_PES_OTHER;
365 #endif
366
367         flt.pid     = pid;
368         flt.input   = DMX_IN_FRONTEND;
369         flt.output  = DMX_OUT_TAP;
370         
371         flt.flags   = DMX_IMMEDIATE_START;
372
373         res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
374         
375         if (res)
376                 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
377         if (!res)
378                 m_active = 1;
379         return res;
380 }
381
382 RESULT eDVBPESReader::stop()
383 {
384         if (!m_active)
385                 return -1;
386
387         m_active=0;
388         ::ioctl(m_fd, DMX_STOP);
389         m_notifier->stop();
390
391         return 0;
392 }
393
394 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
395 {
396         conn = new eConnection(this, m_read.connect(r));
397         return 0;
398 }
399
400 class eDVBRecordFileThread: public eFilePushThread
401 {
402 public:
403         eDVBRecordFileThread();
404         void setTimingPID(int pid);
405         
406         void saveTimingInformation(const std::string &filename);
407 protected:
408         void filterRecordData(const unsigned char *data, int len);
409 private:
410         eMPEGStreamParserTS m_ts_parser;
411         eMPEGStreamInformation m_stream_info;
412         off_t m_current_offset;
413         int m_pid;
414 };
415
416 eDVBRecordFileThread::eDVBRecordFileThread()
417         :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
418 {
419         m_current_offset = 0;
420 }
421
422 void eDVBRecordFileThread::setTimingPID(int pid)
423 {
424         m_ts_parser.setPid(pid);
425 }
426
427 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
428 {
429         m_stream_info.save(filename.c_str());
430 }
431
432 void eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len)
433 {
434         m_ts_parser.parseData(m_current_offset, data, len);
435         
436         m_current_offset += len;
437 }
438
439 DEFINE_REF(eDVBTSRecorder);
440
441 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
442 {
443         m_running = 0;
444         m_target_fd = -1;
445         m_thread = new eDVBRecordFileThread();
446 #ifndef HAVE_ADD_PID
447         m_demux->m_dvr_busy = 1;
448 #endif
449 }
450
451 eDVBTSRecorder::~eDVBTSRecorder()
452 {
453         stop();
454         delete m_thread;
455 #ifndef HAVE_ADD_PID
456         m_demux->m_dvr_busy = 0;
457 #endif
458 }
459
460 RESULT eDVBTSRecorder::start()
461 {
462         if (m_running)
463                 return -1;
464         
465         if (m_target_fd == -1)
466                 return -2;
467
468         char filename[128];
469 #ifndef HAVE_ADD_PID
470 #if HAVE_DVB_API_VERSION < 3
471         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
472 #else
473         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
474 #endif
475         m_source_fd = ::open(filename, O_RDONLY);
476         
477         if (m_source_fd < 0)
478         {
479                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
480                 return -3;
481         }
482 #else
483         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
484
485         m_source_fd = ::open(filename, O_RDONLY);
486         
487         if (m_source_fd < 0)
488         {
489                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
490                 return -3;
491         }
492         
493         ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
494
495         dmx_pes_filter_params flt;
496         flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
497         flt.pid     = (__u16)-1;
498         flt.input   = DMX_IN_FRONTEND;
499         flt.output  = DMX_OUT_TAP;
500         flt.flags   = 0;
501         int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
502         if (res)
503         {
504                 eDebug("DMX_SET_PES_FILTER: %m");
505                 ::close(m_source_fd);
506                 return -3;
507         }
508         
509         ::ioctl(m_source_fd, DMX_START);
510         
511 #endif
512         
513         m_thread->start(m_source_fd, m_target_fd);
514         m_running = 1;
515         
516         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
517                 startPID(i->first);
518         
519         return 0;
520 }
521
522 RESULT eDVBTSRecorder::addPID(int pid)
523 {
524         if (m_pids.find(pid) != m_pids.end())
525                 return -1;
526         
527         m_pids.insert(std::pair<int,int>(pid, -1));
528         if (m_running)
529                 startPID(pid);
530         return 0;
531 }
532
533 RESULT eDVBTSRecorder::removePID(int pid)
534 {
535         if (m_pids.find(pid) == m_pids.end())
536                 return -1;
537                 
538         if (m_running)
539                 stopPID(pid);
540         
541         m_pids.erase(pid);
542         return 0;
543 }
544
545 RESULT eDVBTSRecorder::setTimingPID(int pid)
546 {
547         if (m_running)
548                 return -1;
549         m_thread->setTimingPID(pid);
550         return 0;
551 }
552
553 RESULT eDVBTSRecorder::setTargetFD(int fd)
554 {
555         m_target_fd = fd;
556         return 0;
557 }
558
559 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
560 {
561         m_target_filename = filename;
562         return 0;
563 }
564
565 RESULT eDVBTSRecorder::setBoundary(off_t max)
566 {
567         return -1; // not yet implemented
568 }
569
570 RESULT eDVBTSRecorder::stop()
571 {
572         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
573                 stopPID(i->first);
574
575         if (!m_running)
576                 return -1;
577         m_thread->stop();
578         
579         close(m_source_fd);
580         m_source_fd = -1;
581         
582         if (m_target_filename != "")
583                 m_thread->saveTimingInformation(m_target_filename + ".ap");
584         
585         return 0;
586 }
587
588 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
589 {
590         conn = new eConnection(this, m_event.connect(event));
591         return 0;
592 }
593
594 RESULT eDVBTSRecorder::startPID(int pid)
595 {
596 #ifndef HAVE_ADD_PID
597         int fd = m_demux->openDemux();
598         if (fd < 0)
599         {
600                 eDebug("FAILED to open demux in ts recoder (%m)");
601                 return -1;
602         }
603
604 #if HAVE_DVB_API_VERSION < 3
605         dmxPesFilterParams flt;
606         
607         flt.pesType = DMX_PES_OTHER;
608 #else
609         dmx_pes_filter_params flt;
610         
611         flt.pes_type = DMX_PES_OTHER;
612 #endif
613
614         flt.pid     = pid;
615         flt.input   = DMX_IN_FRONTEND;
616         flt.output  = DMX_OUT_TS_TAP;
617         
618         flt.flags   = DMX_IMMEDIATE_START;
619
620         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
621         if (res < 0)
622         {
623                 eDebug("set pes filter failed!");
624                 ::close(fd);
625                 return -1;
626         }
627         m_pids[pid] = fd;
628 #else
629         if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
630                 perror("DMX_ADD_PID");
631         else
632                 m_pids[pid] = 1;
633 #endif
634         return 0;
635 }
636
637 void eDVBTSRecorder::stopPID(int pid)
638 {
639 #ifndef HAVE_ADD_PID
640         if (m_pids[pid] != -1)
641                 ::close(m_pids[pid]);
642 #else
643         if (m_pids[pid] != -1)
644         {
645                 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
646                         perror("DMX_REMOVE_PID");
647         }
648 #endif
649         m_pids[pid] = -1;
650 }