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);
88 int eDVBDemux::openDVR(int flags)
91 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", adapter, demux);
92 return ::open(filename, flags);
97 RESULT eDVBDemux::setSourceFrontend(int fenum)
99 #if HAVE_DVB_API_VERSION >= 3
100 int fd = openDemux();
101 if (fd < 0) return -1;
102 int n = DMX_SOURCE_FRONT0 + fenum;
103 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
105 eDebug("DMX_SET_SOURCE failed! - %m");
114 RESULT eDVBDemux::setSourcePVR(int pvrnum)
116 #if HAVE_DVB_API_VERSION >= 3
117 int fd = openDemux();
118 if (fd < 0) return -1;
119 int n = DMX_SOURCE_DVR0 + pvrnum;
120 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
128 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
131 reader = new eDVBSectionReader(this, context, res);
137 RESULT eDVBDemux::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
140 reader = new eDVBPESReader(this, context, res);
146 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
150 recorder = new eDVBTSRecorder(this);
154 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
156 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
160 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
162 int fd = openDemux();
171 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
173 eDebug("DMX_GET_STC failed!");
180 eDebug("DMX_GET_STC - %lld", pts);
186 RESULT eDVBDemux::flush()
188 // FIXME: implement flushing the PVR queue here.
194 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
196 conn = new eConnection(this, m_event.connect(event));
200 void eDVBSectionReader::data(int)
202 __u8 data[4096]; // max. section size
204 r = ::read(fd, data, 4096);
207 for (j = 0; j < r; ++j)
209 if (!(rand()%FUZZING_PROPABILITY))
215 eWarning("ERROR reading section - %m\n");
220 // this check should never happen unless the driver is crappy!
222 if ((c = crc32((unsigned)-1, data, r)))
224 eDebug("crc32 failed! is %x\n", c);
231 eDebug("data.. but not active");
234 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
237 fd = demux->openDemux();
241 notifier=eSocketNotifier::create(context, fd, eSocketNotifier::Read, false);
242 CONNECT(notifier->activated, eDVBSectionReader::data);
251 DEFINE_REF(eDVBSectionReader)
253 eDVBSectionReader::~eDVBSectionReader()
259 RESULT eDVBSectionReader::setBufferSize(int size)
261 int res=::ioctl(fd, DMX_SET_BUFFER_SIZE, size);
263 eDebug("eDVBSectionReader DMX_SET_BUFFER_SIZE failed(%m)");
267 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
274 #if HAVE_DVB_API_VERSION < 3
275 dmxSctFilterParams sct;
277 dmx_sct_filter_params sct;
281 #if HAVE_DVB_API_VERSION < 3
284 sct.flags = DMX_IMMEDIATE_START;
287 if (mask.flags & eDVBSectionFilterMask::rfCRC)
289 sct.flags |= DMX_CHECK_CRC;
295 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
296 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
297 #if HAVE_DVB_API_VERSION >= 3
298 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
299 setBufferSize(8192*8);
302 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
305 #if HAVE_DVB_API_VERSION < 3
306 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
309 res = ::ioctl(fd, DMX_START, 0);
320 RESULT eDVBSectionReader::stop()
326 ::ioctl(fd, DMX_STOP);
332 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
334 conn = new eConnection(this, read.connect(r));
338 void eDVBPESReader::data(int)
344 r = ::read(m_fd, buffer, 16384);
349 if (errno == EAGAIN || errno == EINTR) /* ok */
351 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
358 eWarning("PES reader not active");
364 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
367 m_fd = m_demux->openDemux();
371 setBufferSize(64*1024);
372 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
373 m_notifier = eSocketNotifier::create(context, m_fd, eSocketNotifier::Read, false);
374 CONNECT(m_notifier->activated, eDVBPESReader::data);
383 RESULT eDVBPESReader::setBufferSize(int size)
385 int res = ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, size);
387 eDebug("eDVBPESReader DMX_SET_BUFFER_SIZE failed(%m)");
391 DEFINE_REF(eDVBPESReader)
393 eDVBPESReader::~eDVBPESReader()
399 RESULT eDVBPESReader::start(int pid)
407 #if HAVE_DVB_API_VERSION < 3
408 dmxPesFilterParams flt;
410 flt.pesType = DMX_PES_OTHER;
412 dmx_pes_filter_params flt;
414 flt.pes_type = DMX_PES_OTHER;
418 flt.input = DMX_IN_FRONTEND;
419 flt.output = DMX_OUT_TAP;
421 flt.flags = DMX_IMMEDIATE_START;
423 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
426 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
432 RESULT eDVBPESReader::stop()
438 ::ioctl(m_fd, DMX_STOP);
444 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
446 conn = new eConnection(this, m_read.connect(r));
450 class eDVBRecordFileThread: public eFilePushThread
453 eDVBRecordFileThread();
454 void setTimingPID(int pid, int type);
456 void startSaveMetaInformation(const std::string &filename);
457 void stopSaveMetaInformation();
458 void enableAccessPoints(bool enable);
459 int getLastPTS(pts_t &pts);
461 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
463 eMPEGStreamParserTS m_ts_parser;
464 eMPEGStreamInformation m_stream_info;
465 off_t m_current_offset;
466 pts_t m_last_pcr; /* very approximate.. */
470 eDVBRecordFileThread::eDVBRecordFileThread()
471 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
473 m_current_offset = 0;
476 void eDVBRecordFileThread::setTimingPID(int pid, int type)
478 m_ts_parser.setPid(pid, type);
481 void eDVBRecordFileThread::startSaveMetaInformation(const std::string &filename)
483 m_stream_info.startSave(filename.c_str());
486 void eDVBRecordFileThread::stopSaveMetaInformation()
488 m_stream_info.stopSave();
491 void eDVBRecordFileThread::enableAccessPoints(bool enable)
493 m_ts_parser.enableAccessPoints(enable);
496 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
498 return m_ts_parser.getLastPTS(pts);
501 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
503 m_ts_parser.parseData(m_current_offset, data, len);
505 m_current_offset += len;
510 DEFINE_REF(eDVBTSRecorder);
512 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
516 m_thread = new eDVBRecordFileThread();
517 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
519 m_demux->m_dvr_busy = 1;
523 eDVBTSRecorder::~eDVBTSRecorder()
528 m_demux->m_dvr_busy = 0;
532 RESULT eDVBTSRecorder::start()
534 std::map<int,int>::iterator i(m_pids.begin());
539 if (m_target_fd == -1)
542 if (i == m_pids.end())
547 #if HAVE_DVB_API_VERSION < 3
548 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
550 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
552 m_source_fd = ::open(filename, O_RDONLY);
556 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
560 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
562 m_source_fd = ::open(filename, O_RDONLY);
566 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
570 setBufferSize(1024*1024);
572 dmx_pes_filter_params flt;
573 #if HAVE_DVB_API_VERSION > 3
574 flt.pes_type = DMX_PES_OTHER;
575 flt.output = DMX_OUT_TSDEMUX_TAP;
577 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
578 flt.output = DMX_OUT_TAP;
582 flt.input = DMX_IN_FRONTEND;
584 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
587 eDebug("DMX_SET_PES_FILTER: %m");
588 ::close(m_source_fd);
593 ::ioctl(m_source_fd, DMX_START);
597 if (m_target_filename != "")
598 m_thread->startSaveMetaInformation(m_target_filename);
600 m_thread->start(m_source_fd, m_target_fd);
603 while (i != m_pids.end()) {
611 RESULT eDVBTSRecorder::setBufferSize(int size)
613 int res = ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, size);
615 eDebug("eDVBTSRecorder DMX_SET_BUFFER_SIZE failed(%m)");
619 RESULT eDVBTSRecorder::addPID(int pid)
621 if (m_pids.find(pid) != m_pids.end())
624 m_pids.insert(std::pair<int,int>(pid, -1));
630 RESULT eDVBTSRecorder::removePID(int pid)
632 if (m_pids.find(pid) == m_pids.end())
642 RESULT eDVBTSRecorder::setTimingPID(int pid, int type)
644 m_thread->setTimingPID(pid, type);
648 RESULT eDVBTSRecorder::setTargetFD(int fd)
654 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
656 m_target_filename = filename;
658 std::string target_path = m_target_filename;
659 std::string::size_type filePos = target_path.rfind('/');
660 m_thread->setTSPath(target_path.erase(filePos));
665 RESULT eDVBTSRecorder::enableAccessPoints(bool enable)
667 m_thread->enableAccessPoints(enable);
671 RESULT eDVBTSRecorder::setBoundary(off_t max)
673 return -1; // not yet implemented
676 RESULT eDVBTSRecorder::setTimeshift(bool enable)
678 m_thread->setTimeshift(enable);
681 RESULT eDVBTSRecorder::stop()
685 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
691 #if HAVE_DVB_API_VERSION >= 5
692 /* workaround for record thread stop */
693 if (m_source_fd >= 0)
695 if (::ioctl(m_source_fd, DMX_STOP) < 0)
700 if (::close(m_source_fd) < 0)
712 if (m_source_fd >= 0)
714 ::close(m_source_fd);
720 m_thread->stopSaveMetaInformation();
724 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
730 /* XXX: we need a lock here */
732 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
733 return m_thread->getLastPTS(pcr);
736 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
738 conn = new eConnection(this, m_event.connect(event));
742 RESULT eDVBTSRecorder::startPID(int pid)
745 int fd = m_demux->openDemux();
748 eDebug("FAILED to open demux in ts recoder (%m)");
752 #if HAVE_DVB_API_VERSION < 3
753 dmxPesFilterParams flt;
755 flt.pesType = DMX_PES_OTHER;
757 dmx_pes_filter_params flt;
759 flt.pes_type = DMX_PES_OTHER;
763 flt.input = DMX_IN_FRONTEND;
764 flt.output = DMX_OUT_TS_TAP;
766 flt.flags = DMX_IMMEDIATE_START;
768 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
771 eDebug("set pes filter failed!");
778 #if HAVE_DVB_API_VERSION > 3
780 if (::ioctl(m_source_fd, DMX_ADD_PID, &p) < 0) {
782 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
784 perror("DMX_ADD_PID");
785 if (errno == EAGAIN || errno == EINTR) {
797 void eDVBTSRecorder::stopPID(int pid)
800 if (m_pids[pid] != -1)
801 ::close(m_pids[pid]);
803 if (m_pids[pid] != -1)
806 #if HAVE_DVB_API_VERSION > 3
808 if (::ioctl(m_source_fd, DMX_REMOVE_PID, &p) < 0) {
810 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
812 perror("DMX_REMOVE_PID");
813 if (errno == EAGAIN || errno == EINTR) {
825 void eDVBTSRecorder::filepushEvent(int event)
829 case eFilePushThread::evtWriteError:
830 m_event(eventWriteError);