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 int n = DMX_SOURCE_FRONT0 + fenum;
102 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
104 eDebug("DMX_SET_SOURCE failed! - %m");
113 RESULT eDVBDemux::setSourcePVR(int pvrnum)
115 #if HAVE_DVB_API_VERSION >= 3
116 int fd = openDemux();
117 int n = DMX_SOURCE_DVR0 + pvrnum;
118 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
126 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
129 reader = new eDVBSectionReader(this, context, res);
135 RESULT eDVBDemux::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
138 reader = new eDVBPESReader(this, context, res);
144 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
148 recorder = new eDVBTSRecorder(this);
152 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
154 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
158 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
160 int fd = openDemux();
169 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
171 eDebug("DMX_GET_STC failed!");
178 eDebug("DMX_GET_STC - %lld", pts);
184 RESULT eDVBDemux::flush()
186 // FIXME: implement flushing the PVR queue here.
192 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
194 conn = new eConnection(this, m_event.connect(event));
198 void eDVBSectionReader::data(int)
200 __u8 data[4096]; // max. section size
202 r = ::read(fd, data, 4096);
205 for (j = 0; j < r; ++j)
207 if (!(rand()%FUZZING_PROPABILITY))
213 eWarning("ERROR reading section - %m\n");
218 // this check should never happen unless the driver is crappy!
220 if ((c = crc32((unsigned)-1, data, r)))
222 eDebug("crc32 failed! is %x\n", c);
229 eDebug("data.. but not active");
232 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
235 fd = demux->openDemux();
239 notifier=eSocketNotifier::create(context, fd, eSocketNotifier::Read, false);
240 CONNECT(notifier->activated, eDVBSectionReader::data);
249 DEFINE_REF(eDVBSectionReader)
251 eDVBSectionReader::~eDVBSectionReader()
257 RESULT eDVBSectionReader::setBufferSize(int size)
259 int res=::ioctl(fd, DMX_SET_BUFFER_SIZE, size);
261 eDebug("eDVBSectionReader DMX_SET_BUFFER_SIZE failed(%m)");
265 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
272 #if HAVE_DVB_API_VERSION < 3
273 dmxSctFilterParams sct;
275 dmx_sct_filter_params sct;
279 #if HAVE_DVB_API_VERSION < 3
282 sct.flags = DMX_IMMEDIATE_START;
285 if (mask.flags & eDVBSectionFilterMask::rfCRC)
287 sct.flags |= DMX_CHECK_CRC;
293 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
294 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
295 #if HAVE_DVB_API_VERSION >= 3
296 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
297 setBufferSize(8192*8);
300 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
303 #if HAVE_DVB_API_VERSION < 3
304 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
307 res = ::ioctl(fd, DMX_START, 0);
318 RESULT eDVBSectionReader::stop()
324 ::ioctl(fd, DMX_STOP);
330 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
332 conn = new eConnection(this, read.connect(r));
336 void eDVBPESReader::data(int)
342 r = ::read(m_fd, buffer, 16384);
347 if (errno == EAGAIN || errno == EINTR) /* ok */
349 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
356 eWarning("PES reader not active");
362 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
365 m_fd = m_demux->openDemux();
369 setBufferSize(64*1024);
370 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
371 m_notifier = eSocketNotifier::create(context, m_fd, eSocketNotifier::Read, false);
372 CONNECT(m_notifier->activated, eDVBPESReader::data);
381 RESULT eDVBPESReader::setBufferSize(int size)
383 int res = ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, size);
385 eDebug("eDVBPESReader DMX_SET_BUFFER_SIZE failed(%m)");
389 DEFINE_REF(eDVBPESReader)
391 eDVBPESReader::~eDVBPESReader()
397 RESULT eDVBPESReader::start(int pid)
405 #if HAVE_DVB_API_VERSION < 3
406 dmxPesFilterParams flt;
408 flt.pesType = DMX_PES_OTHER;
410 dmx_pes_filter_params flt;
412 flt.pes_type = DMX_PES_OTHER;
416 flt.input = DMX_IN_FRONTEND;
417 flt.output = DMX_OUT_TAP;
419 flt.flags = DMX_IMMEDIATE_START;
421 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
424 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
430 RESULT eDVBPESReader::stop()
436 ::ioctl(m_fd, DMX_STOP);
442 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
444 conn = new eConnection(this, m_read.connect(r));
448 class eDVBRecordFileThread: public eFilePushThread
451 eDVBRecordFileThread();
452 void setTimingPID(int pid, int type);
454 void startSaveMetaInformation(const std::string &filename);
455 void stopSaveMetaInformation();
456 int getLastPTS(pts_t &pts);
458 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
460 eMPEGStreamParserTS m_ts_parser;
461 eMPEGStreamInformation m_stream_info;
462 off_t m_current_offset;
463 pts_t m_last_pcr; /* very approximate.. */
467 eDVBRecordFileThread::eDVBRecordFileThread()
468 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
470 m_current_offset = 0;
473 void eDVBRecordFileThread::setTimingPID(int pid, int type)
475 m_ts_parser.setPid(pid, type);
478 void eDVBRecordFileThread::startSaveMetaInformation(const std::string &filename)
480 m_stream_info.startSave(filename.c_str());
483 void eDVBRecordFileThread::stopSaveMetaInformation()
485 m_stream_info.stopSave();
488 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
490 return m_ts_parser.getLastPTS(pts);
493 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
495 m_ts_parser.parseData(m_current_offset, data, len);
497 m_current_offset += len;
502 DEFINE_REF(eDVBTSRecorder);
504 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
508 m_thread = new eDVBRecordFileThread();
509 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
511 m_demux->m_dvr_busy = 1;
515 eDVBTSRecorder::~eDVBTSRecorder()
520 m_demux->m_dvr_busy = 0;
524 RESULT eDVBTSRecorder::start()
526 std::map<int,int>::iterator i(m_pids.begin());
531 if (m_target_fd == -1)
534 if (i == m_pids.end())
539 #if HAVE_DVB_API_VERSION < 3
540 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
542 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
544 m_source_fd = ::open(filename, O_RDONLY);
548 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
552 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
554 m_source_fd = ::open(filename, O_RDONLY);
558 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
562 setBufferSize(1024*1024);
564 dmx_pes_filter_params flt;
565 #if HAVE_DVB_API_VERSION > 3
566 flt.pes_type = DMX_PES_OTHER;
567 flt.output = DMX_OUT_TSDEMUX_TAP;
569 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
570 flt.output = DMX_OUT_TAP;
574 flt.input = DMX_IN_FRONTEND;
576 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
579 eDebug("DMX_SET_PES_FILTER: %m");
580 ::close(m_source_fd);
584 ::ioctl(m_source_fd, DMX_START);
588 if (m_target_filename != "")
589 m_thread->startSaveMetaInformation(m_target_filename);
591 m_thread->start(m_source_fd, m_target_fd);
594 while (i != m_pids.end()) {
602 RESULT eDVBTSRecorder::setBufferSize(int size)
604 int res = ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, size);
606 eDebug("eDVBTSRecorder DMX_SET_BUFFER_SIZE failed(%m)");
610 RESULT eDVBTSRecorder::addPID(int pid)
612 if (m_pids.find(pid) != m_pids.end())
615 m_pids.insert(std::pair<int,int>(pid, -1));
621 RESULT eDVBTSRecorder::removePID(int pid)
623 if (m_pids.find(pid) == m_pids.end())
633 RESULT eDVBTSRecorder::setTimingPID(int pid, int type)
635 m_thread->setTimingPID(pid, type);
639 RESULT eDVBTSRecorder::setTargetFD(int fd)
645 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
647 m_target_filename = filename;
651 RESULT eDVBTSRecorder::setBoundary(off_t max)
653 return -1; // not yet implemented
656 RESULT eDVBTSRecorder::stop()
660 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
666 #if HAVE_DVB_API_VERSION >= 5
667 /* workaround for record thread stop */
668 if (::ioctl(m_source_fd, DMX_STOP) < 0)
673 if (::close(m_source_fd) < 0)
682 ::close(m_source_fd);
687 m_thread->stopSaveMetaInformation();
691 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
697 /* XXX: we need a lock here */
699 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
700 return m_thread->getLastPTS(pcr);
703 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
705 conn = new eConnection(this, m_event.connect(event));
709 RESULT eDVBTSRecorder::startPID(int pid)
712 int fd = m_demux->openDemux();
715 eDebug("FAILED to open demux in ts recoder (%m)");
719 #if HAVE_DVB_API_VERSION < 3
720 dmxPesFilterParams flt;
722 flt.pesType = DMX_PES_OTHER;
724 dmx_pes_filter_params flt;
726 flt.pes_type = DMX_PES_OTHER;
730 flt.input = DMX_IN_FRONTEND;
731 flt.output = DMX_OUT_TS_TAP;
733 flt.flags = DMX_IMMEDIATE_START;
735 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
738 eDebug("set pes filter failed!");
745 #if HAVE_DVB_API_VERSION > 3
747 if (::ioctl(m_source_fd, DMX_ADD_PID, &p) < 0) {
749 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
751 perror("DMX_ADD_PID");
752 if (errno == EAGAIN || errno == EINTR) {
764 void eDVBTSRecorder::stopPID(int pid)
767 if (m_pids[pid] != -1)
768 ::close(m_pids[pid]);
770 if (m_pids[pid] != -1)
773 #if HAVE_DVB_API_VERSION > 3
775 if (::ioctl(m_source_fd, DMX_REMOVE_PID, &p) < 0) {
777 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
779 perror("DMX_REMOVE_PID");
780 if (errno == EAGAIN || errno == EINTR) {
792 void eDVBTSRecorder::filepushEvent(int event)
796 case eFilePushThread::evtWriteError:
797 m_event(eventWriteError);