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::start(const eDVBSectionFilterMask &mask)
257 #if HAVE_DVB_API_VERSION < 3
258 dmxSctFilterParams sct;
260 dmx_sct_filter_params sct;
264 #if HAVE_DVB_API_VERSION < 3
267 sct.flags = DMX_IMMEDIATE_START;
270 if (mask.flags & eDVBSectionFilterMask::rfCRC)
272 sct.flags |= DMX_CHECK_CRC;
278 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
279 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
280 #if HAVE_DVB_API_VERSION >= 3
281 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
282 if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
283 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
286 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
289 #if HAVE_DVB_API_VERSION < 3
290 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
293 res = ::ioctl(fd, DMX_START, 0);
304 RESULT eDVBSectionReader::stop()
310 ::ioctl(fd, DMX_STOP);
316 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
318 conn = new eConnection(this, read.connect(r));
322 void eDVBPESReader::data(int)
328 r = ::read(m_fd, buffer, 16384);
333 if (errno == EAGAIN || errno == EINTR) /* ok */
335 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
342 eWarning("PES reader not active");
348 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
351 m_fd = m_demux->openDemux();
355 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
356 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
357 m_notifier = eSocketNotifier::create(context, m_fd, eSocketNotifier::Read, false);
358 CONNECT(m_notifier->activated, eDVBPESReader::data);
367 DEFINE_REF(eDVBPESReader)
369 eDVBPESReader::~eDVBPESReader()
375 RESULT eDVBPESReader::start(int pid)
383 #if HAVE_DVB_API_VERSION < 3
384 dmxPesFilterParams flt;
386 flt.pesType = DMX_PES_OTHER;
388 dmx_pes_filter_params flt;
390 flt.pes_type = DMX_PES_OTHER;
394 flt.input = DMX_IN_FRONTEND;
395 flt.output = DMX_OUT_TAP;
397 flt.flags = DMX_IMMEDIATE_START;
399 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
402 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
408 RESULT eDVBPESReader::stop()
414 ::ioctl(m_fd, DMX_STOP);
420 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
422 conn = new eConnection(this, m_read.connect(r));
426 class eDVBRecordFileThread: public eFilePushThread
429 eDVBRecordFileThread();
430 void setTimingPID(int pid, int type);
432 void startSaveMetaInformation(const std::string &filename);
433 void stopSaveMetaInformation();
434 int getLastPTS(pts_t &pts);
436 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
438 eMPEGStreamParserTS m_ts_parser;
439 eMPEGStreamInformation m_stream_info;
440 off_t m_current_offset;
441 pts_t m_last_pcr; /* very approximate.. */
445 eDVBRecordFileThread::eDVBRecordFileThread()
446 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
448 m_current_offset = 0;
451 void eDVBRecordFileThread::setTimingPID(int pid, int type)
453 m_ts_parser.setPid(pid, type);
456 void eDVBRecordFileThread::startSaveMetaInformation(const std::string &filename)
458 m_stream_info.startSave(filename.c_str());
461 void eDVBRecordFileThread::stopSaveMetaInformation()
463 m_stream_info.stopSave();
466 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
468 return m_ts_parser.getLastPTS(pts);
471 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
473 m_ts_parser.parseData(m_current_offset, data, len);
475 m_current_offset += len;
480 DEFINE_REF(eDVBTSRecorder);
482 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
486 m_thread = new eDVBRecordFileThread();
487 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
489 m_demux->m_dvr_busy = 1;
493 eDVBTSRecorder::~eDVBTSRecorder()
498 m_demux->m_dvr_busy = 0;
502 RESULT eDVBTSRecorder::start()
504 std::map<int,int>::iterator i(m_pids.begin());
509 if (m_target_fd == -1)
512 if (i == m_pids.end())
517 #if HAVE_DVB_API_VERSION < 3
518 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
520 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
522 m_source_fd = ::open(filename, O_RDONLY);
526 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
530 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
532 m_source_fd = ::open(filename, O_RDONLY);
536 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
540 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
542 dmx_pes_filter_params flt;
543 #if HAVE_DVB_API_VERSION > 3
544 flt.pes_type = DMX_PES_OTHER;
545 flt.output = DMX_OUT_TSDEMUX_TAP;
547 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
548 flt.output = DMX_OUT_TAP;
552 flt.input = DMX_IN_FRONTEND;
554 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
557 eDebug("DMX_SET_PES_FILTER: %m");
558 ::close(m_source_fd);
562 ::ioctl(m_source_fd, DMX_START);
566 if (m_target_filename != "")
567 m_thread->startSaveMetaInformation(m_target_filename);
569 m_thread->start(m_source_fd, m_target_fd);
572 while (i != m_pids.end()) {
580 RESULT eDVBTSRecorder::addPID(int pid)
582 if (m_pids.find(pid) != m_pids.end())
585 m_pids.insert(std::pair<int,int>(pid, -1));
591 RESULT eDVBTSRecorder::removePID(int pid)
593 if (m_pids.find(pid) == m_pids.end())
603 RESULT eDVBTSRecorder::setTimingPID(int pid, int type)
605 m_thread->setTimingPID(pid, type);
609 RESULT eDVBTSRecorder::setTargetFD(int fd)
615 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
617 m_target_filename = filename;
621 RESULT eDVBTSRecorder::setBoundary(off_t max)
623 return -1; // not yet implemented
626 RESULT eDVBTSRecorder::stop()
628 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
638 m_thread->stopSaveMetaInformation();
643 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
649 /* XXX: we need a lock here */
651 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
652 return m_thread->getLastPTS(pcr);
655 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
657 conn = new eConnection(this, m_event.connect(event));
661 RESULT eDVBTSRecorder::startPID(int pid)
664 int fd = m_demux->openDemux();
667 eDebug("FAILED to open demux in ts recoder (%m)");
671 #if HAVE_DVB_API_VERSION < 3
672 dmxPesFilterParams flt;
674 flt.pesType = DMX_PES_OTHER;
676 dmx_pes_filter_params flt;
678 flt.pes_type = DMX_PES_OTHER;
682 flt.input = DMX_IN_FRONTEND;
683 flt.output = DMX_OUT_TS_TAP;
685 flt.flags = DMX_IMMEDIATE_START;
687 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
690 eDebug("set pes filter failed!");
697 #if HAVE_DVB_API_VERSION > 3
699 if (::ioctl(m_source_fd, DMX_ADD_PID, &p) < 0) {
701 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
703 perror("DMX_ADD_PID");
704 if (errno == EAGAIN || errno == EINTR) {
716 void eDVBTSRecorder::stopPID(int pid)
719 if (m_pids[pid] != -1)
720 ::close(m_pids[pid]);
722 if (m_pids[pid] != -1)
725 #if HAVE_DVB_API_VERSION > 3
727 if (::ioctl(m_source_fd, DMX_REMOVE_PID, &p) < 0) {
729 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
731 perror("DMX_REMOVE_PID");
732 if (errno == EAGAIN || errno == EINTR) {
744 void eDVBTSRecorder::filepushEvent(int event)
748 case eFilePushThread::evtWriteError:
749 m_event(eventWriteError);