better solution to add possibility to delete eSocketNotifiers,
[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 protected:
409         int filterRecordData(const unsigned char *data, int len, size_t &current_span_remaining);
410 private:
411         eMPEGStreamParserTS m_ts_parser;
412         eMPEGStreamInformation m_stream_info;
413         off_t m_current_offset;
414         int m_pid;
415 };
416
417 eDVBRecordFileThread::eDVBRecordFileThread()
418         :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
419 {
420         m_current_offset = 0;
421 }
422
423 void eDVBRecordFileThread::setTimingPID(int pid)
424 {
425         m_ts_parser.setPid(pid);
426 }
427
428 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
429 {
430         m_stream_info.save(filename.c_str());
431 }
432
433 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t &current_span_remaining)
434 {
435         m_ts_parser.parseData(m_current_offset, data, len);
436         
437         m_current_offset += len;
438         
439         return len;
440 }
441
442 DEFINE_REF(eDVBTSRecorder);
443
444 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
445 {
446         m_running = 0;
447         m_target_fd = -1;
448         m_thread = new eDVBRecordFileThread();
449   CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
450 #ifndef HAVE_ADD_PID
451         m_demux->m_dvr_busy = 1;
452 #endif
453 }
454
455 eDVBTSRecorder::~eDVBTSRecorder()
456 {
457         stop();
458         delete m_thread;
459 #ifndef HAVE_ADD_PID
460         m_demux->m_dvr_busy = 0;
461 #endif
462 }
463
464 RESULT eDVBTSRecorder::start()
465 {
466         if (m_running)
467                 return -1;
468         
469         if (m_target_fd == -1)
470                 return -2;
471
472         char filename[128];
473 #ifndef HAVE_ADD_PID
474 #if HAVE_DVB_API_VERSION < 3
475         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
476 #else
477         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
478 #endif
479         m_source_fd = ::open(filename, O_RDONLY);
480         
481         if (m_source_fd < 0)
482         {
483                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
484                 return -3;
485         }
486 #else
487         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
488
489         m_source_fd = ::open(filename, O_RDONLY);
490         
491         if (m_source_fd < 0)
492         {
493                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
494                 return -3;
495         }
496         
497         ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
498
499         dmx_pes_filter_params flt;
500         flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
501         flt.pid     = (__u16)-1;
502         flt.input   = DMX_IN_FRONTEND;
503         flt.output  = DMX_OUT_TAP;
504         flt.flags   = 0;
505         int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
506         if (res)
507         {
508                 eDebug("DMX_SET_PES_FILTER: %m");
509                 ::close(m_source_fd);
510                 return -3;
511         }
512         
513         ::ioctl(m_source_fd, DMX_START);
514         
515 #endif
516         
517         m_thread->start(m_source_fd, m_target_fd);
518         m_running = 1;
519         
520         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
521                 startPID(i->first);
522         
523         return 0;
524 }
525
526 RESULT eDVBTSRecorder::addPID(int pid)
527 {
528         if (m_pids.find(pid) != m_pids.end())
529                 return -1;
530         
531         m_pids.insert(std::pair<int,int>(pid, -1));
532         if (m_running)
533                 startPID(pid);
534         return 0;
535 }
536
537 RESULT eDVBTSRecorder::removePID(int pid)
538 {
539         if (m_pids.find(pid) == m_pids.end())
540                 return -1;
541                 
542         if (m_running)
543                 stopPID(pid);
544         
545         m_pids.erase(pid);
546         return 0;
547 }
548
549 RESULT eDVBTSRecorder::setTimingPID(int pid)
550 {
551         if (m_running)
552                 return -1;
553         m_thread->setTimingPID(pid);
554         return 0;
555 }
556
557 RESULT eDVBTSRecorder::setTargetFD(int fd)
558 {
559         m_target_fd = fd;
560         return 0;
561 }
562
563 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
564 {
565         m_target_filename = filename;
566         return 0;
567 }
568
569 RESULT eDVBTSRecorder::setBoundary(off_t max)
570 {
571         return -1; // not yet implemented
572 }
573
574 RESULT eDVBTSRecorder::stop()
575 {
576         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
577                 stopPID(i->first);
578
579         if (!m_running)
580                 return -1;
581         m_thread->stop();
582         
583         close(m_source_fd);
584         m_source_fd = -1;
585         
586         if (m_target_filename != "")
587                 m_thread->saveTimingInformation(m_target_filename + ".ap");
588         
589         return 0;
590 }
591
592 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
593 {
594         conn = new eConnection(this, m_event.connect(event));
595         return 0;
596 }
597
598 RESULT eDVBTSRecorder::startPID(int pid)
599 {
600 #ifndef HAVE_ADD_PID
601         int fd = m_demux->openDemux();
602         if (fd < 0)
603         {
604                 eDebug("FAILED to open demux in ts recoder (%m)");
605                 return -1;
606         }
607
608 #if HAVE_DVB_API_VERSION < 3
609         dmxPesFilterParams flt;
610         
611         flt.pesType = DMX_PES_OTHER;
612 #else
613         dmx_pes_filter_params flt;
614         
615         flt.pes_type = DMX_PES_OTHER;
616 #endif
617
618         flt.pid     = pid;
619         flt.input   = DMX_IN_FRONTEND;
620         flt.output  = DMX_OUT_TS_TAP;
621         
622         flt.flags   = DMX_IMMEDIATE_START;
623
624         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
625         if (res < 0)
626         {
627                 eDebug("set pes filter failed!");
628                 ::close(fd);
629                 return -1;
630         }
631         m_pids[pid] = fd;
632 #else
633         while(true) {
634                 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
635                         perror("DMX_ADD_PID");
636                         if (errno == EAGAIN || errno == EINTR) {
637                                 eDebug("retry!");
638                                 continue;
639                         }
640                 } else
641                         m_pids[pid] = 1;
642                 break;
643         }
644 #endif
645         return 0;
646 }
647
648 void eDVBTSRecorder::stopPID(int pid)
649 {
650 #ifndef HAVE_ADD_PID
651         if (m_pids[pid] != -1)
652                 ::close(m_pids[pid]);
653 #else
654         if (m_pids[pid] != -1)
655         {
656                 while(true) {
657                         if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
658                                 perror("DMX_REMOVE_PID");
659                                 if (errno == EAGAIN || errno == EINTR) {
660                                         eDebug("retry!");
661                                         continue;
662                                 }
663                         }
664                         break;
665                 }
666         }
667 #endif
668         m_pids[pid] = -1;
669 }
670
671 void eDVBTSRecorder::filepushEvent(int event)
672 {
673         switch (event)
674         {
675         case eFilePushThread::evtWriteError:
676                 m_event(eventWriteError);
677                 break;
678         }
679 }