add possibility to read detailed epg from Digital+
[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         }
316 }
317
318 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
319 {
320         char filename[128];
321         m_fd = m_demux->openDemux();
322         
323         if (m_fd >= 0)
324         {
325                 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
326                 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
327                 m_notifier = new eSocketNotifier(context, m_fd, eSocketNotifier::Read, false);
328                 CONNECT(m_notifier->activated, eDVBPESReader::data);
329                 res = 0;
330         } else
331         {
332                 perror(filename);
333                 res = errno;
334         }
335 }
336
337 DEFINE_REF(eDVBPESReader)
338
339 eDVBPESReader::~eDVBPESReader()
340 {
341         if (m_notifier)
342                 delete m_notifier;
343         if (m_fd >= 0)
344                 ::close(m_fd);
345 }
346
347 RESULT eDVBPESReader::start(int pid)
348 {
349         RESULT res;
350         if (m_fd < 0)
351                 return -ENODEV;
352
353         m_notifier->start();
354
355 #if HAVE_DVB_API_VERSION < 3
356         dmxPesFilterParams flt;
357         
358         flt.pesType = DMX_PES_OTHER;
359 #else
360         dmx_pes_filter_params flt;
361         
362         flt.pes_type = DMX_PES_OTHER;
363 #endif
364
365         flt.pid     = pid;
366         flt.input   = DMX_IN_FRONTEND;
367         flt.output  = DMX_OUT_TAP;
368         
369         flt.flags   = DMX_IMMEDIATE_START;
370
371         res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
372         
373         if (res)
374                 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
375         if (!res)
376                 m_active = 1;
377         return res;
378 }
379
380 RESULT eDVBPESReader::stop()
381 {
382         if (!m_active)
383                 return -1;
384
385         m_active=0;
386         ::ioctl(m_fd, DMX_STOP);
387         m_notifier->stop();
388
389         return 0;
390 }
391
392 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
393 {
394         conn = new eConnection(this, m_read.connect(r));
395         return 0;
396 }
397
398 class eDVBRecordFileThread: public eFilePushThread
399 {
400 public:
401         eDVBRecordFileThread();
402         void setTimingPID(int pid);
403         
404         void saveTimingInformation(const std::string &filename);
405 protected:
406         void filterRecordData(const unsigned char *data, int len);
407 private:
408         eMPEGStreamParserTS m_ts_parser;
409         eMPEGStreamInformation m_stream_info;
410         off_t m_current_offset;
411         int m_pid;
412 };
413
414 eDVBRecordFileThread::eDVBRecordFileThread()
415         :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
416 {
417         m_current_offset = 0;
418 }
419
420 void eDVBRecordFileThread::setTimingPID(int pid)
421 {
422         m_ts_parser.setPid(pid);
423 }
424
425 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
426 {
427         m_stream_info.save(filename.c_str());
428 }
429
430 void eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len)
431 {
432         m_ts_parser.parseData(m_current_offset, data, len);
433         
434         m_current_offset += len;
435 }
436
437 DEFINE_REF(eDVBTSRecorder);
438
439 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
440 {
441         m_running = 0;
442         m_target_fd = -1;
443         m_thread = new eDVBRecordFileThread();
444 #ifndef HAVE_ADD_PID
445         m_demux->m_dvr_busy = 1;
446 #endif
447 }
448
449 eDVBTSRecorder::~eDVBTSRecorder()
450 {
451         stop();
452         delete m_thread;
453 #ifndef HAVE_ADD_PID
454         m_demux->m_dvr_busy = 0;
455 #endif
456 }
457
458 RESULT eDVBTSRecorder::start()
459 {
460         if (m_running)
461                 return -1;
462         
463         if (m_target_fd == -1)
464                 return -2;
465
466         char filename[128];
467 #ifndef HAVE_ADD_PID
468 #if HAVE_DVB_API_VERSION < 3
469         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
470 #else
471         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
472 #endif
473         m_source_fd = ::open(filename, O_RDONLY);
474         
475         if (m_source_fd < 0)
476         {
477                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
478                 return -3;
479         }
480 #else
481         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
482
483         m_source_fd = ::open(filename, O_RDONLY);
484         
485         if (m_source_fd < 0)
486         {
487                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
488                 return -3;
489         }
490         
491         ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
492
493         dmx_pes_filter_params flt;
494         flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
495         flt.pid     = (__u16)-1;
496         flt.input   = DMX_IN_FRONTEND;
497         flt.output  = DMX_OUT_TAP;
498         flt.flags   = 0;
499         int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
500         if (res)
501         {
502                 eDebug("DMX_SET_PES_FILTER: %m");
503                 ::close(m_source_fd);
504                 return -3;
505         }
506         
507         ::ioctl(m_source_fd, DMX_START);
508         
509 #endif
510         
511         m_thread->start(m_source_fd, m_target_fd);
512         m_running = 1;
513         
514         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
515                 startPID(i->first);
516         
517         return 0;
518 }
519
520 RESULT eDVBTSRecorder::addPID(int pid)
521 {
522         if (m_pids.find(pid) != m_pids.end())
523                 return -1;
524         
525         m_pids.insert(std::pair<int,int>(pid, -1));
526         if (m_running)
527                 startPID(pid);
528         return 0;
529 }
530
531 RESULT eDVBTSRecorder::removePID(int pid)
532 {
533         if (m_pids.find(pid) == m_pids.end())
534                 return -1;
535                 
536         if (m_running)
537                 stopPID(pid);
538         
539         m_pids.erase(pid);
540         return 0;
541 }
542
543 RESULT eDVBTSRecorder::setTimingPID(int pid)
544 {
545         if (m_running)
546                 return -1;
547         m_thread->setTimingPID(pid);
548         return 0;
549 }
550
551 RESULT eDVBTSRecorder::setTargetFD(int fd)
552 {
553         m_target_fd = fd;
554         return 0;
555 }
556
557 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
558 {
559         m_target_filename = filename;
560         return 0;
561 }
562
563 RESULT eDVBTSRecorder::setBoundary(off_t max)
564 {
565         return -1; // not yet implemented
566 }
567
568 RESULT eDVBTSRecorder::stop()
569 {
570         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
571                 stopPID(i->first);
572
573         if (!m_running)
574                 return -1;
575         m_thread->stop();
576         
577         close(m_source_fd);
578         m_source_fd = -1;
579         
580         if (m_target_filename != "")
581                 m_thread->saveTimingInformation(m_target_filename + ".ap");
582         
583         return 0;
584 }
585
586 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
587 {
588         conn = new eConnection(this, m_event.connect(event));
589         return 0;
590 }
591
592 RESULT eDVBTSRecorder::startPID(int pid)
593 {
594 #ifndef HAVE_ADD_PID
595         int fd = m_demux->openDemux();
596         if (fd < 0)
597         {
598                 eDebug("FAILED to open demux in ts recoder (%m)");
599                 return -1;
600         }
601
602 #if HAVE_DVB_API_VERSION < 3
603         dmxPesFilterParams flt;
604         
605         flt.pesType = DMX_PES_OTHER;
606 #else
607         dmx_pes_filter_params flt;
608         
609         flt.pes_type = DMX_PES_OTHER;
610 #endif
611
612         flt.pid     = pid;
613         flt.input   = DMX_IN_FRONTEND;
614         flt.output  = DMX_OUT_TS_TAP;
615         
616         flt.flags   = DMX_IMMEDIATE_START;
617
618         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
619         if (res < 0)
620         {
621                 eDebug("set pes filter failed!");
622                 ::close(fd);
623                 return -1;
624         }
625         m_pids[pid] = fd;
626 #else
627         if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
628                 perror("DMX_ADD_PID");
629         else
630                 m_pids[pid] = 1;
631 #endif
632         return 0;
633 }
634
635 void eDVBTSRecorder::stopPID(int pid)
636 {
637 #ifndef HAVE_ADD_PID
638         if (m_pids[pid] != -1)
639                 ::close(m_pids[pid]);
640 #else
641         if (m_pids[pid] != -1)
642         {
643                 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
644                         perror("DMX_REMOVE_PID");
645         }
646 #endif
647         m_pids[pid] = -1;
648 }