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/dvb/idvb.h>
62 #include <lib/dvb/demux.h>
63 #include <lib/dvb/esection.h>
64 #include <lib/dvb/decoder.h>
66 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
71 eDVBDemux::~eDVBDemux()
75 int eDVBDemux::openDemux(void)
78 #if HAVE_DVB_API_VERSION < 3
79 snprintf(filename, 128, "/dev/dvb/card%d/demux%d", adapter, demux);
81 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", adapter, demux);
83 return ::open(filename, O_RDWR);
86 int eDVBDemux::openDVR(int flags)
89 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", adapter, demux);
90 return ::open(filename, flags);
95 RESULT eDVBDemux::setSourceFrontend(int fenum)
97 #if HAVE_DVB_API_VERSION >= 3
99 if (fd < 0) return -1;
100 int n = DMX_SOURCE_FRONT0 + fenum;
101 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
103 eDebug("DMX_SET_SOURCE failed! - %m");
112 RESULT eDVBDemux::setSourcePVR(int pvrnum)
114 #if HAVE_DVB_API_VERSION >= 3
115 int fd = openDemux();
116 if (fd < 0) return -1;
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 index)
154 decoder = new eTSMPEGDecoder(this, index);
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 eDVBRecordFileThread::eDVBRecordFileThread()
449 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
451 m_current_offset = 0;
454 void eDVBRecordFileThread::setTimingPID(int pid, int type)
456 m_ts_parser.setPid(pid, type);
459 void eDVBRecordFileThread::startSaveMetaInformation(const std::string &filename)
461 m_stream_info.startSave(filename.c_str());
464 void eDVBRecordFileThread::stopSaveMetaInformation()
466 m_stream_info.stopSave();
469 void eDVBRecordFileThread::enableAccessPoints(bool enable)
471 m_ts_parser.enableAccessPoints(enable);
474 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
476 return m_ts_parser.getLastPTS(pts);
479 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
481 m_ts_parser.parseData(m_current_offset, data, len);
483 m_current_offset += len;
488 DEFINE_REF(eDVBTSRecorder);
490 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
494 m_thread = new eDVBRecordFileThread();
495 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
497 m_demux->m_dvr_busy = 1;
501 eDVBTSRecorder::~eDVBTSRecorder()
506 m_demux->m_dvr_busy = 0;
510 RESULT eDVBTSRecorder::start()
512 std::map<int,int>::iterator i(m_pids.begin());
517 if (m_target_fd == -1)
520 if (i == m_pids.end())
525 #if HAVE_DVB_API_VERSION < 3
526 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
528 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
530 m_source_fd = ::open(filename, O_RDONLY);
534 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
538 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
540 m_source_fd = ::open(filename, O_RDONLY);
544 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
548 setBufferSize(1024*1024);
550 dmx_pes_filter_params flt;
551 #if HAVE_DVB_API_VERSION > 3
552 flt.pes_type = DMX_PES_OTHER;
553 flt.output = DMX_OUT_TSDEMUX_TAP;
555 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
556 flt.output = DMX_OUT_TAP;
560 flt.input = DMX_IN_FRONTEND;
562 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
565 eDebug("DMX_SET_PES_FILTER: %m");
566 ::close(m_source_fd);
571 ::ioctl(m_source_fd, DMX_START);
575 if (m_target_filename != "")
576 m_thread->startSaveMetaInformation(m_target_filename);
578 m_thread->start(m_source_fd, m_target_fd);
581 while (i != m_pids.end()) {
589 RESULT eDVBTSRecorder::setBufferSize(int size)
591 int res = ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, size);
593 eDebug("eDVBTSRecorder DMX_SET_BUFFER_SIZE failed(%m)");
597 RESULT eDVBTSRecorder::addPID(int pid)
599 if (m_pids.find(pid) != m_pids.end())
602 m_pids.insert(std::pair<int,int>(pid, -1));
608 RESULT eDVBTSRecorder::removePID(int pid)
610 if (m_pids.find(pid) == m_pids.end())
620 RESULT eDVBTSRecorder::setTimingPID(int pid, int type)
622 m_thread->setTimingPID(pid, type);
626 RESULT eDVBTSRecorder::setTargetFD(int fd)
632 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
634 m_target_filename = filename;
636 std::string target_path = m_target_filename;
637 std::string::size_type filePos = target_path.rfind('/');
638 m_thread->setTSPath(target_path.erase(filePos));
643 RESULT eDVBTSRecorder::enableAccessPoints(bool enable)
645 m_thread->enableAccessPoints(enable);
649 RESULT eDVBTSRecorder::setBoundary(off_t max)
651 return -1; // not yet implemented
654 RESULT eDVBTSRecorder::setTimeshift(bool enable)
656 m_thread->setTimeshift(enable);
659 RESULT eDVBTSRecorder::stop()
663 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
669 #if HAVE_DVB_API_VERSION >= 5
670 /* workaround for record thread stop */
671 if (m_source_fd >= 0)
673 if (::ioctl(m_source_fd, DMX_STOP) < 0)
678 if (::close(m_source_fd) < 0)
690 if (m_source_fd >= 0)
692 ::close(m_source_fd);
698 m_thread->stopSaveMetaInformation();
702 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
708 /* XXX: we need a lock here */
710 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
711 return m_thread->getLastPTS(pcr);
714 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
716 conn = new eConnection(this, m_event.connect(event));
720 RESULT eDVBTSRecorder::startPID(int pid)
723 int fd = m_demux->openDemux();
726 eDebug("FAILED to open demux in ts recoder (%m)");
730 #if HAVE_DVB_API_VERSION < 3
731 dmxPesFilterParams flt;
733 flt.pesType = DMX_PES_OTHER;
735 dmx_pes_filter_params flt;
737 flt.pes_type = DMX_PES_OTHER;
741 flt.input = DMX_IN_FRONTEND;
742 flt.output = DMX_OUT_TS_TAP;
744 flt.flags = DMX_IMMEDIATE_START;
746 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
749 eDebug("set pes filter failed!");
756 #if HAVE_DVB_API_VERSION > 3
758 if (::ioctl(m_source_fd, DMX_ADD_PID, &p) < 0) {
760 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
762 perror("DMX_ADD_PID");
763 if (errno == EAGAIN || errno == EINTR) {
775 void eDVBTSRecorder::stopPID(int pid)
778 if (m_pids[pid] != -1)
779 ::close(m_pids[pid]);
781 if (m_pids[pid] != -1)
784 #if HAVE_DVB_API_VERSION > 3
786 if (::ioctl(m_source_fd, DMX_REMOVE_PID, &p) < 0) {
788 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
790 perror("DMX_REMOVE_PID");
791 if (errno == EAGAIN || errno == EINTR) {
803 void eDVBTSRecorder::filepushEvent(int event)
807 case eFilePushThread::evtWriteError:
808 m_event(eventWriteError);