11 /* change every 1:FUZZING_PROPABILITY byte */
12 #define FUZZING_PROPABILITY 100
15 #if HAVE_DVB_API_VERSION < 3
18 #ifndef DMX_SET_NEGFILTER_MASK
19 #define DMX_SET_NEGFILTER_MASK _IOW('o',48,uint8_t *)
25 unsigned int num; /* input : which STC? O..N */
26 unsigned int base; /* output: divisor for stc to get 90 kHz clock */
27 unsigned long long stc; /* output: src in 'base'*90 kHz units */
29 #define DMX_GET_STC _IOR('o', 50, struct dmx_stc)
33 #include <linux/dvb/dmx.h>
39 #if HAVE_DVB_API_VERSION > 3
41 #define DMX_ADD_PID _IOW('o', 51, __u16)
42 #define DMX_REMOVE_PID _IOW('o', 52, __u16)
45 #define DMX_ADD_PID _IO('o', 51)
46 #define DMX_REMOVE_PID _IO('o', 52)
50 DMX_TAP_PES = DMX_PES_OTHER, /* for backward binary compat. */
60 #include <lib/base/eerror.h>
61 #include <lib/base/filepush.h>
62 #include <lib/dvb/idvb.h>
63 #include <lib/dvb/demux.h>
64 #include <lib/dvb/esection.h>
65 #include <lib/dvb/decoder.h>
66 #include <lib/dvb/pvrparse.h>
68 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
73 eDVBDemux::~eDVBDemux()
77 int eDVBDemux::openDemux(void)
80 #if HAVE_DVB_API_VERSION < 3
81 snprintf(filename, 128, "/dev/dvb/card%d/demux%d", adapter, demux);
83 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", adapter, demux);
85 return ::open(filename, O_RDWR);
90 RESULT eDVBDemux::setSourceFrontend(int fenum)
92 #if HAVE_DVB_API_VERSION >= 3
94 int n = DMX_SOURCE_FRONT0 + fenum;
95 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
97 eDebug("DMX_SET_SOURCE failed! - %m");
106 RESULT eDVBDemux::setSourcePVR(int pvrnum)
108 #if HAVE_DVB_API_VERSION >= 3
109 int fd = openDemux();
110 int n = DMX_SOURCE_DVR0 + pvrnum;
111 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
119 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
122 reader = new eDVBSectionReader(this, context, res);
128 RESULT eDVBDemux::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
131 reader = new eDVBPESReader(this, context, res);
137 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
141 recorder = new eDVBTSRecorder(this);
145 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
147 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
151 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
153 int fd = openDemux();
162 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
164 eDebug("DMX_GET_STC failed!");
171 eDebug("DMX_GET_STC - %lld", pts);
177 RESULT eDVBDemux::flush()
179 // FIXME: implement flushing the PVR queue here.
185 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
187 conn = new eConnection(this, m_event.connect(event));
191 void eDVBSectionReader::data(int)
193 __u8 data[4096]; // max. section size
195 r = ::read(fd, data, 4096);
198 for (j = 0; j < r; ++j)
200 if (!(rand()%FUZZING_PROPABILITY))
206 eWarning("ERROR reading section - %m\n");
211 // this check should never happen unless the driver is crappy!
213 if ((c = crc32((unsigned)-1, data, r)))
215 eDebug("crc32 failed! is %x\n", c);
222 eDebug("data.. but not active");
225 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
228 fd = demux->openDemux();
232 notifier=eSocketNotifier::create(context, fd, eSocketNotifier::Read, false);
233 CONNECT(notifier->activated, eDVBSectionReader::data);
242 DEFINE_REF(eDVBSectionReader)
244 eDVBSectionReader::~eDVBSectionReader()
250 RESULT eDVBSectionReader::setBufferSize(int size)
252 int res=::ioctl(fd, DMX_SET_BUFFER_SIZE, size);
254 eDebug("eDVBSectionReader DMX_SET_BUFFER_SIZE failed(%m)");
258 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
265 #if HAVE_DVB_API_VERSION < 3
266 dmxSctFilterParams sct;
268 dmx_sct_filter_params sct;
272 #if HAVE_DVB_API_VERSION < 3
275 sct.flags = DMX_IMMEDIATE_START;
278 if (mask.flags & eDVBSectionFilterMask::rfCRC)
280 sct.flags |= DMX_CHECK_CRC;
286 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
287 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
288 #if HAVE_DVB_API_VERSION >= 3
289 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
290 setBufferSize(8192*8);
293 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
296 #if HAVE_DVB_API_VERSION < 3
297 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
300 res = ::ioctl(fd, DMX_START, 0);
311 RESULT eDVBSectionReader::stop()
317 ::ioctl(fd, DMX_STOP);
323 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
325 conn = new eConnection(this, read.connect(r));
329 void eDVBPESReader::data(int)
335 r = ::read(m_fd, buffer, 16384);
340 if (errno == EAGAIN || errno == EINTR) /* ok */
342 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
349 eWarning("PES reader not active");
355 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
358 m_fd = m_demux->openDemux();
362 setBufferSize(64*1024);
363 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
364 m_notifier = eSocketNotifier::create(context, m_fd, eSocketNotifier::Read, false);
365 CONNECT(m_notifier->activated, eDVBPESReader::data);
374 RESULT eDVBPESReader::setBufferSize(int size)
376 int res = ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, size);
378 eDebug("eDVBPESReader DMX_SET_BUFFER_SIZE failed(%m)");
382 DEFINE_REF(eDVBPESReader)
384 eDVBPESReader::~eDVBPESReader()
390 RESULT eDVBPESReader::start(int pid)
398 #if HAVE_DVB_API_VERSION < 3
399 dmxPesFilterParams flt;
401 flt.pesType = DMX_PES_OTHER;
403 dmx_pes_filter_params flt;
405 flt.pes_type = DMX_PES_OTHER;
409 flt.input = DMX_IN_FRONTEND;
410 flt.output = DMX_OUT_TAP;
412 flt.flags = DMX_IMMEDIATE_START;
414 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
417 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
423 RESULT eDVBPESReader::stop()
429 ::ioctl(m_fd, DMX_STOP);
435 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
437 conn = new eConnection(this, m_read.connect(r));
441 class eDVBRecordFileThread: public eFilePushThread
444 eDVBRecordFileThread();
445 void setTimingPID(int pid, int type);
447 void startSaveMetaInformation(const std::string &filename);
448 void stopSaveMetaInformation();
449 int getLastPTS(pts_t &pts);
451 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
453 eMPEGStreamParserTS m_ts_parser;
454 eMPEGStreamInformation m_stream_info;
455 off_t m_current_offset;
456 pts_t m_last_pcr; /* very approximate.. */
460 eDVBRecordFileThread::eDVBRecordFileThread()
461 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
463 m_current_offset = 0;
466 void eDVBRecordFileThread::setTimingPID(int pid, int type)
468 m_ts_parser.setPid(pid, type);
471 void eDVBRecordFileThread::startSaveMetaInformation(const std::string &filename)
473 m_stream_info.startSave(filename.c_str());
476 void eDVBRecordFileThread::stopSaveMetaInformation()
478 m_stream_info.stopSave();
481 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
483 return m_ts_parser.getLastPTS(pts);
486 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
488 m_ts_parser.parseData(m_current_offset, data, len);
490 m_current_offset += len;
495 DEFINE_REF(eDVBTSRecorder);
497 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
501 m_thread = new eDVBRecordFileThread();
502 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
504 m_demux->m_dvr_busy = 1;
508 eDVBTSRecorder::~eDVBTSRecorder()
513 m_demux->m_dvr_busy = 0;
517 RESULT eDVBTSRecorder::start()
519 std::map<int,int>::iterator i(m_pids.begin());
524 if (m_target_fd == -1)
527 if (i == m_pids.end())
532 #if HAVE_DVB_API_VERSION < 3
533 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
535 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
537 m_source_fd = ::open(filename, O_RDONLY);
541 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
545 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
547 m_source_fd = ::open(filename, O_RDONLY);
551 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
555 setBufferSize(1024*1024);
557 dmx_pes_filter_params flt;
558 #if HAVE_DVB_API_VERSION > 3
559 flt.pes_type = DMX_PES_OTHER;
560 flt.output = DMX_OUT_TSDEMUX_TAP;
562 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
563 flt.output = DMX_OUT_TAP;
567 flt.input = DMX_IN_FRONTEND;
569 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
572 eDebug("DMX_SET_PES_FILTER: %m");
573 ::close(m_source_fd);
577 ::ioctl(m_source_fd, DMX_START);
581 if (m_target_filename != "")
582 m_thread->startSaveMetaInformation(m_target_filename);
584 m_thread->start(m_source_fd, m_target_fd);
587 while (i != m_pids.end()) {
595 RESULT eDVBTSRecorder::setBufferSize(int size)
597 int res = ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, size);
599 eDebug("eDVBTSRecorder DMX_SET_BUFFER_SIZE failed(%m)");
603 RESULT eDVBTSRecorder::addPID(int pid)
605 if (m_pids.find(pid) != m_pids.end())
608 m_pids.insert(std::pair<int,int>(pid, -1));
614 RESULT eDVBTSRecorder::removePID(int pid)
616 if (m_pids.find(pid) == m_pids.end())
626 RESULT eDVBTSRecorder::setTimingPID(int pid, int type)
628 m_thread->setTimingPID(pid, type);
632 RESULT eDVBTSRecorder::setTargetFD(int fd)
638 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
640 m_target_filename = filename;
644 RESULT eDVBTSRecorder::setBoundary(off_t max)
646 return -1; // not yet implemented
649 RESULT eDVBTSRecorder::stop()
651 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
661 m_thread->stopSaveMetaInformation();
666 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
672 /* XXX: we need a lock here */
674 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
675 return m_thread->getLastPTS(pcr);
678 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
680 conn = new eConnection(this, m_event.connect(event));
684 RESULT eDVBTSRecorder::startPID(int pid)
687 int fd = m_demux->openDemux();
690 eDebug("FAILED to open demux in ts recoder (%m)");
694 #if HAVE_DVB_API_VERSION < 3
695 dmxPesFilterParams flt;
697 flt.pesType = DMX_PES_OTHER;
699 dmx_pes_filter_params flt;
701 flt.pes_type = DMX_PES_OTHER;
705 flt.input = DMX_IN_FRONTEND;
706 flt.output = DMX_OUT_TS_TAP;
708 flt.flags = DMX_IMMEDIATE_START;
710 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
713 eDebug("set pes filter failed!");
720 #if HAVE_DVB_API_VERSION > 3
722 if (::ioctl(m_source_fd, DMX_ADD_PID, &p) < 0) {
724 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
726 perror("DMX_ADD_PID");
727 if (errno == EAGAIN || errno == EINTR) {
739 void eDVBTSRecorder::stopPID(int pid)
742 if (m_pids[pid] != -1)
743 ::close(m_pids[pid]);
745 if (m_pids[pid] != -1)
748 #if HAVE_DVB_API_VERSION > 3
750 if (::ioctl(m_source_fd, DMX_REMOVE_PID, &p) < 0) {
752 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
754 perror("DMX_REMOVE_PID");
755 if (errno == EAGAIN || errno == EINTR) {
767 void eDVBTSRecorder::filepushEvent(int event)
771 case eFilePushThread::evtWriteError:
772 m_event(eventWriteError);