send write error messages down to service events
[vuplus_dvbapp] / 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   CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
447 #ifndef HAVE_ADD_PID
448         m_demux->m_dvr_busy = 1;
449 #endif
450 }
451
452 eDVBTSRecorder::~eDVBTSRecorder()
453 {
454         stop();
455         delete m_thread;
456 #ifndef HAVE_ADD_PID
457         m_demux->m_dvr_busy = 0;
458 #endif
459 }
460
461 RESULT eDVBTSRecorder::start()
462 {
463         if (m_running)
464                 return -1;
465         
466         if (m_target_fd == -1)
467                 return -2;
468
469         char filename[128];
470 #ifndef HAVE_ADD_PID
471 #if HAVE_DVB_API_VERSION < 3
472         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
473 #else
474         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
475 #endif
476         m_source_fd = ::open(filename, O_RDONLY);
477         
478         if (m_source_fd < 0)
479         {
480                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
481                 return -3;
482         }
483 #else
484         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
485
486         m_source_fd = ::open(filename, O_RDONLY);
487         
488         if (m_source_fd < 0)
489         {
490                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
491                 return -3;
492         }
493         
494         ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
495
496         dmx_pes_filter_params flt;
497         flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
498         flt.pid     = (__u16)-1;
499         flt.input   = DMX_IN_FRONTEND;
500         flt.output  = DMX_OUT_TAP;
501         flt.flags   = 0;
502         int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
503         if (res)
504         {
505                 eDebug("DMX_SET_PES_FILTER: %m");
506                 ::close(m_source_fd);
507                 return -3;
508         }
509         
510         ::ioctl(m_source_fd, DMX_START);
511         
512 #endif
513         
514         m_thread->start(m_source_fd, m_target_fd);
515         m_running = 1;
516         
517         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
518                 startPID(i->first);
519         
520         return 0;
521 }
522
523 RESULT eDVBTSRecorder::addPID(int pid)
524 {
525         if (m_pids.find(pid) != m_pids.end())
526                 return -1;
527         
528         m_pids.insert(std::pair<int,int>(pid, -1));
529         if (m_running)
530                 startPID(pid);
531         return 0;
532 }
533
534 RESULT eDVBTSRecorder::removePID(int pid)
535 {
536         if (m_pids.find(pid) == m_pids.end())
537                 return -1;
538                 
539         if (m_running)
540                 stopPID(pid);
541         
542         m_pids.erase(pid);
543         return 0;
544 }
545
546 RESULT eDVBTSRecorder::setTimingPID(int pid)
547 {
548         if (m_running)
549                 return -1;
550         m_thread->setTimingPID(pid);
551         return 0;
552 }
553
554 RESULT eDVBTSRecorder::setTargetFD(int fd)
555 {
556         m_target_fd = fd;
557         return 0;
558 }
559
560 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
561 {
562         m_target_filename = filename;
563         return 0;
564 }
565
566 RESULT eDVBTSRecorder::setBoundary(off_t max)
567 {
568         return -1; // not yet implemented
569 }
570
571 RESULT eDVBTSRecorder::stop()
572 {
573         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
574                 stopPID(i->first);
575
576         if (!m_running)
577                 return -1;
578         m_thread->stop();
579         
580         close(m_source_fd);
581         m_source_fd = -1;
582         
583         if (m_target_filename != "")
584                 m_thread->saveTimingInformation(m_target_filename + ".ap");
585         
586         return 0;
587 }
588
589 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
590 {
591         conn = new eConnection(this, m_event.connect(event));
592         return 0;
593 }
594
595 RESULT eDVBTSRecorder::startPID(int pid)
596 {
597 #ifndef HAVE_ADD_PID
598         int fd = m_demux->openDemux();
599         if (fd < 0)
600         {
601                 eDebug("FAILED to open demux in ts recoder (%m)");
602                 return -1;
603         }
604
605 #if HAVE_DVB_API_VERSION < 3
606         dmxPesFilterParams flt;
607         
608         flt.pesType = DMX_PES_OTHER;
609 #else
610         dmx_pes_filter_params flt;
611         
612         flt.pes_type = DMX_PES_OTHER;
613 #endif
614
615         flt.pid     = pid;
616         flt.input   = DMX_IN_FRONTEND;
617         flt.output  = DMX_OUT_TS_TAP;
618         
619         flt.flags   = DMX_IMMEDIATE_START;
620
621         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
622         if (res < 0)
623         {
624                 eDebug("set pes filter failed!");
625                 ::close(fd);
626                 return -1;
627         }
628         m_pids[pid] = fd;
629 #else
630         if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
631                 perror("DMX_ADD_PID");
632         else
633                 m_pids[pid] = 1;
634 #endif
635         return 0;
636 }
637
638 void eDVBTSRecorder::stopPID(int pid)
639 {
640 #ifndef HAVE_ADD_PID
641         if (m_pids[pid] != -1)
642                 ::close(m_pids[pid]);
643 #else
644         if (m_pids[pid] != -1)
645         {
646                 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
647                         perror("DMX_REMOVE_PID");
648         }
649 #endif
650         m_pids[pid] = -1;
651 }
652
653 void eDVBTSRecorder::filepushEvent(int event)
654 {
655         switch (event)
656         {
657         case eFilePushThread::evtWriteError:
658                 m_event(eventWriteError);
659                 break;
660         }
661 }