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>
38 #define DMX_ADD_PID _IO('o', 51)
39 #define DMX_REMOVE_PID _IO('o', 52)
43 DMX_TAP_PES = DMX_PES_OTHER, /* for backward binary compat. */
52 #include <lib/base/eerror.h>
53 #include <lib/base/filepush.h>
54 #include <lib/dvb/idvb.h>
55 #include <lib/dvb/demux.h>
56 #include <lib/dvb/esection.h>
57 #include <lib/dvb/decoder.h>
58 #include <lib/dvb/pvrparse.h>
60 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
65 eDVBDemux::~eDVBDemux()
69 int eDVBDemux::openDemux(void)
72 #if HAVE_DVB_API_VERSION < 3
73 snprintf(filename, 128, "/dev/dvb/card%d/demux%d", adapter, demux);
75 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", adapter, demux);
77 return ::open(filename, O_RDWR);
82 RESULT eDVBDemux::setSourceFrontend(int fenum)
84 #if HAVE_DVB_API_VERSION >= 3
86 int n = DMX_SOURCE_FRONT0 + fenum;
87 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
89 eDebug("DMX_SET_SOURCE failed! - %m");
98 RESULT eDVBDemux::setSourcePVR(int pvrnum)
100 #if HAVE_DVB_API_VERSION >= 3
101 int fd = openDemux();
102 int n = DMX_SOURCE_DVR0 + pvrnum;
103 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
111 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
114 reader = new eDVBSectionReader(this, context, res);
120 RESULT eDVBDemux::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
123 reader = new eDVBPESReader(this, context, res);
129 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
133 recorder = new eDVBTSRecorder(this);
137 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
139 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
143 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
145 int fd = openDemux();
154 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
156 eDebug("DMX_GET_STC failed!");
163 eDebug("DMX_GET_STC - %lld", pts);
169 RESULT eDVBDemux::flush()
171 // FIXME: implement flushing the PVR queue here.
177 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
179 conn = new eConnection(this, m_event.connect(event));
183 void eDVBSectionReader::data(int)
185 __u8 data[4096]; // max. section size
187 r = ::read(fd, data, 4096);
190 for (j = 0; j < r; ++j)
192 if (!(rand()%FUZZING_PROPABILITY))
198 eWarning("ERROR reading section - %m\n");
203 // this check should never happen unless the driver is crappy!
205 if ((c = crc32((unsigned)-1, data, r)))
207 eDebug("crc32 failed! is %x\n", c);
214 eDebug("data.. but not active");
217 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
220 fd = demux->openDemux();
224 notifier=eSocketNotifier::create(context, fd, eSocketNotifier::Read, false);
225 CONNECT(notifier->activated, eDVBSectionReader::data);
234 DEFINE_REF(eDVBSectionReader)
236 eDVBSectionReader::~eDVBSectionReader()
242 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
249 #if HAVE_DVB_API_VERSION < 3
250 dmxSctFilterParams sct;
252 dmx_sct_filter_params sct;
256 #if HAVE_DVB_API_VERSION < 3
259 sct.flags = DMX_IMMEDIATE_START;
262 if (mask.flags & eDVBSectionFilterMask::rfCRC)
264 sct.flags |= DMX_CHECK_CRC;
270 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
271 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
272 #if HAVE_DVB_API_VERSION >= 3
273 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
274 if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
275 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
278 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
281 #if HAVE_DVB_API_VERSION < 3
282 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
285 res = ::ioctl(fd, DMX_START, 0);
296 RESULT eDVBSectionReader::stop()
302 ::ioctl(fd, DMX_STOP);
308 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
310 conn = new eConnection(this, read.connect(r));
314 void eDVBPESReader::data(int)
320 r = ::read(m_fd, buffer, 16384);
325 if (errno == EAGAIN || errno == EINTR) /* ok */
327 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
334 eWarning("PES reader not active");
340 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
343 m_fd = m_demux->openDemux();
347 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
348 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
349 m_notifier = eSocketNotifier::create(context, m_fd, eSocketNotifier::Read, false);
350 CONNECT(m_notifier->activated, eDVBPESReader::data);
359 DEFINE_REF(eDVBPESReader)
361 eDVBPESReader::~eDVBPESReader()
367 RESULT eDVBPESReader::start(int pid)
375 #if HAVE_DVB_API_VERSION < 3
376 dmxPesFilterParams flt;
378 flt.pesType = DMX_PES_OTHER;
380 dmx_pes_filter_params flt;
382 flt.pes_type = DMX_PES_OTHER;
386 flt.input = DMX_IN_FRONTEND;
387 flt.output = DMX_OUT_TAP;
389 flt.flags = DMX_IMMEDIATE_START;
391 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
394 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
400 RESULT eDVBPESReader::stop()
406 ::ioctl(m_fd, DMX_STOP);
412 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
414 conn = new eConnection(this, m_read.connect(r));
418 class eDVBRecordFileThread: public eFilePushThread
421 eDVBRecordFileThread();
422 void setTimingPID(int pid, int type);
424 void startSaveMetaInformation(const std::string &filename);
425 void stopSaveMetaInformation();
426 int getLastPTS(pts_t &pts);
428 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
430 eMPEGStreamParserTS m_ts_parser;
431 eMPEGStreamInformation m_stream_info;
432 off_t m_current_offset;
433 pts_t m_last_pcr; /* very approximate.. */
437 eDVBRecordFileThread::eDVBRecordFileThread()
438 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
440 m_current_offset = 0;
443 void eDVBRecordFileThread::setTimingPID(int pid, int type)
445 m_ts_parser.setPid(pid, type);
448 void eDVBRecordFileThread::startSaveMetaInformation(const std::string &filename)
450 m_stream_info.startSave(filename.c_str());
453 void eDVBRecordFileThread::stopSaveMetaInformation()
455 m_stream_info.stopSave();
458 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
460 return m_ts_parser.getLastPTS(pts);
463 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
465 m_ts_parser.parseData(m_current_offset, data, len);
467 m_current_offset += len;
472 DEFINE_REF(eDVBTSRecorder);
474 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
478 m_thread = new eDVBRecordFileThread();
479 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
481 m_demux->m_dvr_busy = 1;
485 eDVBTSRecorder::~eDVBTSRecorder()
490 m_demux->m_dvr_busy = 0;
494 RESULT eDVBTSRecorder::start()
499 if (m_target_fd == -1)
504 #if HAVE_DVB_API_VERSION < 3
505 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
507 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
509 m_source_fd = ::open(filename, O_RDONLY);
513 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
517 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
519 m_source_fd = ::open(filename, O_RDONLY);
523 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
527 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
529 dmx_pes_filter_params flt;
530 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
532 flt.input = DMX_IN_FRONTEND;
533 flt.output = DMX_OUT_TAP;
535 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
538 eDebug("DMX_SET_PES_FILTER: %m");
539 ::close(m_source_fd);
543 ::ioctl(m_source_fd, DMX_START);
547 if (m_target_filename != "")
548 m_thread->startSaveMetaInformation(m_target_filename);
550 m_thread->start(m_source_fd, m_target_fd);
553 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
559 RESULT eDVBTSRecorder::addPID(int pid)
561 if (m_pids.find(pid) != m_pids.end())
564 m_pids.insert(std::pair<int,int>(pid, -1));
570 RESULT eDVBTSRecorder::removePID(int pid)
572 if (m_pids.find(pid) == m_pids.end())
582 RESULT eDVBTSRecorder::setTimingPID(int pid, int type)
586 m_thread->setTimingPID(pid, type);
590 RESULT eDVBTSRecorder::setTargetFD(int fd)
596 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
598 m_target_filename = filename;
602 RESULT eDVBTSRecorder::setBoundary(off_t max)
604 return -1; // not yet implemented
607 RESULT eDVBTSRecorder::stop()
609 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
619 m_thread->stopSaveMetaInformation();
624 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
630 /* XXX: we need a lock here */
632 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
633 return m_thread->getLastPTS(pcr);
636 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
638 conn = new eConnection(this, m_event.connect(event));
642 RESULT eDVBTSRecorder::startPID(int pid)
645 int fd = m_demux->openDemux();
648 eDebug("FAILED to open demux in ts recoder (%m)");
652 #if HAVE_DVB_API_VERSION < 3
653 dmxPesFilterParams flt;
655 flt.pesType = DMX_PES_OTHER;
657 dmx_pes_filter_params flt;
659 flt.pes_type = DMX_PES_OTHER;
663 flt.input = DMX_IN_FRONTEND;
664 flt.output = DMX_OUT_TS_TAP;
666 flt.flags = DMX_IMMEDIATE_START;
668 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
671 eDebug("set pes filter failed!");
678 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
679 perror("DMX_ADD_PID");
680 if (errno == EAGAIN || errno == EINTR) {
692 void eDVBTSRecorder::stopPID(int pid)
695 if (m_pids[pid] != -1)
696 ::close(m_pids[pid]);
698 if (m_pids[pid] != -1)
701 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
702 perror("DMX_REMOVE_PID");
703 if (errno == EAGAIN || errno == EINTR) {
715 void eDVBTSRecorder::filepushEvent(int event)
719 case eFilePushThread::evtWriteError:
720 m_event(eventWriteError);