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