handle case of empty text
[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                 eDebug("DMX_GET_STC failed!");
148                 ::close(fd);
149                 return -1;
150         }
151         
152         pts = stc.stc;
153         
154         eDebug("DMX_GET_STC - %lld", pts);
155         
156         ::close(fd);
157         return 0;
158 }
159
160 RESULT eDVBDemux::flush()
161 {
162         // FIXME: implement flushing the PVR queue here.
163         
164         m_event(evtFlush);
165         return 0;
166 }
167
168 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
169 {
170         conn = new eConnection(this, m_event.connect(event));
171         return 0;
172 }
173
174 void eDVBSectionReader::data(int)
175 {
176         __u8 data[4096]; // max. section size
177         int r;
178         r = ::read(fd, data, 4096);
179         if(r < 0)
180         {
181                 eWarning("ERROR reading section - %m\n");
182                 return;
183         }
184         if (checkcrc)
185         {
186                         // this check should never happen unless the driver is crappy!
187                 unsigned int c;
188                 if ((c = crc32((unsigned)-1, data, r)))
189                 {
190                         eDebug("crc32 failed! is %x\n", c);
191                         return;
192                 }
193         }
194         if (active)
195                 read(data);
196         else
197                 eDebug("data.. but not active");
198 }
199
200 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
201 {
202         char filename[128];
203         fd = demux->openDemux();
204         
205         if (fd >= 0)
206         {
207                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
208                 CONNECT(notifier->activated, eDVBSectionReader::data);
209                 res = 0;
210         } else
211         {
212                 perror(filename);
213                 res = errno;
214         }
215 }
216
217 DEFINE_REF(eDVBSectionReader)
218
219 eDVBSectionReader::~eDVBSectionReader()
220 {
221         if (notifier)
222                 delete notifier;
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 = new eSocketNotifier(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_notifier)
347                 delete m_notifier;
348         if (m_fd >= 0)
349                 ::close(m_fd);
350 }
351
352 RESULT eDVBPESReader::start(int pid)
353 {
354         RESULT res;
355         if (m_fd < 0)
356                 return -ENODEV;
357
358         m_notifier->start();
359
360 #if HAVE_DVB_API_VERSION < 3
361         dmxPesFilterParams flt;
362         
363         flt.pesType = DMX_PES_OTHER;
364 #else
365         dmx_pes_filter_params flt;
366         
367         flt.pes_type = DMX_PES_OTHER;
368 #endif
369
370         flt.pid     = pid;
371         flt.input   = DMX_IN_FRONTEND;
372         flt.output  = DMX_OUT_TAP;
373         
374         flt.flags   = DMX_IMMEDIATE_START;
375
376         res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
377         
378         if (res)
379                 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
380         if (!res)
381                 m_active = 1;
382         return res;
383 }
384
385 RESULT eDVBPESReader::stop()
386 {
387         if (!m_active)
388                 return -1;
389
390         m_active=0;
391         ::ioctl(m_fd, DMX_STOP);
392         m_notifier->stop();
393
394         return 0;
395 }
396
397 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
398 {
399         conn = new eConnection(this, m_read.connect(r));
400         return 0;
401 }
402
403 class eDVBRecordFileThread: public eFilePushThread
404 {
405 public:
406         eDVBRecordFileThread();
407         void setTimingPID(int pid);
408         
409         void saveTimingInformation(const std::string &filename);
410 protected:
411         int filterRecordData(const unsigned char *data, int len, size_t &current_span_remaining);
412 private:
413         eMPEGStreamParserTS m_ts_parser;
414         eMPEGStreamInformation m_stream_info;
415         off_t m_current_offset;
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::filterRecordData(const unsigned char *data, int len, size_t &current_span_remaining)
436 {
437         m_ts_parser.parseData(m_current_offset, data, len);
438         
439         m_current_offset += len;
440         
441         return len;
442 }
443
444 DEFINE_REF(eDVBTSRecorder);
445
446 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
447 {
448         m_running = 0;
449         m_target_fd = -1;
450         m_thread = new eDVBRecordFileThread();
451   CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
452 #ifndef HAVE_ADD_PID
453         m_demux->m_dvr_busy = 1;
454 #endif
455 }
456
457 eDVBTSRecorder::~eDVBTSRecorder()
458 {
459         stop();
460         delete m_thread;
461 #ifndef HAVE_ADD_PID
462         m_demux->m_dvr_busy = 0;
463 #endif
464 }
465
466 RESULT eDVBTSRecorder::start()
467 {
468         if (m_running)
469                 return -1;
470         
471         if (m_target_fd == -1)
472                 return -2;
473
474         char filename[128];
475 #ifndef HAVE_ADD_PID
476 #if HAVE_DVB_API_VERSION < 3
477         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
478 #else
479         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
480 #endif
481         m_source_fd = ::open(filename, O_RDONLY);
482         
483         if (m_source_fd < 0)
484         {
485                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
486                 return -3;
487         }
488 #else
489         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
490
491         m_source_fd = ::open(filename, O_RDONLY);
492         
493         if (m_source_fd < 0)
494         {
495                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
496                 return -3;
497         }
498         
499         ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
500
501         dmx_pes_filter_params flt;
502         flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
503         flt.pid     = (__u16)-1;
504         flt.input   = DMX_IN_FRONTEND;
505         flt.output  = DMX_OUT_TAP;
506         flt.flags   = 0;
507         int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
508         if (res)
509         {
510                 eDebug("DMX_SET_PES_FILTER: %m");
511                 ::close(m_source_fd);
512                 return -3;
513         }
514         
515         ::ioctl(m_source_fd, DMX_START);
516         
517 #endif
518         
519         m_thread->start(m_source_fd, m_target_fd);
520         m_running = 1;
521         
522         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
523                 startPID(i->first);
524         
525         return 0;
526 }
527
528 RESULT eDVBTSRecorder::addPID(int pid)
529 {
530         if (m_pids.find(pid) != m_pids.end())
531                 return -1;
532         
533         m_pids.insert(std::pair<int,int>(pid, -1));
534         if (m_running)
535                 startPID(pid);
536         return 0;
537 }
538
539 RESULT eDVBTSRecorder::removePID(int pid)
540 {
541         if (m_pids.find(pid) == m_pids.end())
542                 return -1;
543                 
544         if (m_running)
545                 stopPID(pid);
546         
547         m_pids.erase(pid);
548         return 0;
549 }
550
551 RESULT eDVBTSRecorder::setTimingPID(int pid)
552 {
553         if (m_running)
554                 return -1;
555         m_thread->setTimingPID(pid);
556         return 0;
557 }
558
559 RESULT eDVBTSRecorder::setTargetFD(int fd)
560 {
561         m_target_fd = fd;
562         return 0;
563 }
564
565 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
566 {
567         m_target_filename = filename;
568         return 0;
569 }
570
571 RESULT eDVBTSRecorder::setBoundary(off_t max)
572 {
573         return -1; // not yet implemented
574 }
575
576 RESULT eDVBTSRecorder::stop()
577 {
578         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
579                 stopPID(i->first);
580
581         if (!m_running)
582                 return -1;
583         m_thread->stop();
584         
585         close(m_source_fd);
586         m_source_fd = -1;
587         
588         if (m_target_filename != "")
589                 m_thread->saveTimingInformation(m_target_filename + ".ap");
590         
591         return 0;
592 }
593
594 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
595 {
596         conn = new eConnection(this, m_event.connect(event));
597         return 0;
598 }
599
600 RESULT eDVBTSRecorder::startPID(int pid)
601 {
602 #ifndef HAVE_ADD_PID
603         int fd = m_demux->openDemux();
604         if (fd < 0)
605         {
606                 eDebug("FAILED to open demux in ts recoder (%m)");
607                 return -1;
608         }
609
610 #if HAVE_DVB_API_VERSION < 3
611         dmxPesFilterParams flt;
612         
613         flt.pesType = DMX_PES_OTHER;
614 #else
615         dmx_pes_filter_params flt;
616         
617         flt.pes_type = DMX_PES_OTHER;
618 #endif
619
620         flt.pid     = pid;
621         flt.input   = DMX_IN_FRONTEND;
622         flt.output  = DMX_OUT_TS_TAP;
623         
624         flt.flags   = DMX_IMMEDIATE_START;
625
626         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
627         if (res < 0)
628         {
629                 eDebug("set pes filter failed!");
630                 ::close(fd);
631                 return -1;
632         }
633         m_pids[pid] = fd;
634 #else
635         while(true) {
636                 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
637                         perror("DMX_ADD_PID");
638                         if (errno == EAGAIN || errno == EINTR) {
639                                 eDebug("retry!");
640                                 continue;
641                         }
642                 } else
643                         m_pids[pid] = 1;
644                 break;
645         }
646 #endif
647         return 0;
648 }
649
650 void eDVBTSRecorder::stopPID(int pid)
651 {
652 #ifndef HAVE_ADD_PID
653         if (m_pids[pid] != -1)
654                 ::close(m_pids[pid]);
655 #else
656         if (m_pids[pid] != -1)
657         {
658                 while(true) {
659                         if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
660                                 perror("DMX_REMOVE_PID");
661                                 if (errno == EAGAIN || errno == EINTR) {
662                                         eDebug("retry!");
663                                         continue;
664                                 }
665                         }
666                         break;
667                 }
668         }
669 #endif
670         m_pids[pid] = -1;
671 }
672
673 void eDVBTSRecorder::filepushEvent(int event)
674 {
675         switch (event)
676         {
677         case eFilePushThread::evtWriteError:
678                 m_event(eventWriteError);
679                 break;
680         }
681 }