8 #if HAVE_DVB_API_VERSION < 3
11 #ifndef DMX_SET_NEGFILTER_MASK
12 #define DMX_SET_NEGFILTER_MASK _IOW('o',48,uint8_t *)
18 unsigned int num; /* input : which STC? O..N */
19 unsigned int base; /* output: divisor for stc to get 90 kHz clock */
20 unsigned long long stc; /* output: src in 'base'*90 kHz units */
22 #define DMX_GET_STC _IOR('o', 50, struct dmx_stc)
26 #include <linux/dvb/dmx.h>
31 #define DMX_ADD_PID _IO('o', 51)
32 #define DMX_REMOVE_PID _IO('o', 52)
36 DMX_TAP_PES = DMX_PES_OTHER, /* for backward binary compat. */
45 #include <lib/base/eerror.h>
46 #include <lib/base/filepush.h>
47 #include <lib/dvb/idvb.h>
48 #include <lib/dvb/demux.h>
49 #include <lib/dvb/esection.h>
50 #include <lib/dvb/decoder.h>
51 #include <lib/dvb/pvrparse.h>
53 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
58 eDVBDemux::~eDVBDemux()
62 int eDVBDemux::openDemux(void)
65 #if HAVE_DVB_API_VERSION < 3
66 snprintf(filename, 128, "/dev/dvb/card%d/demux%d", adapter, demux);
68 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", adapter, demux);
70 return ::open(filename, O_RDWR);
75 RESULT eDVBDemux::setSourceFrontend(int fenum)
77 #if HAVE_DVB_API_VERSION >= 3
79 int n = DMX_SOURCE_FRONT0 + fenum;
80 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
82 eDebug("DMX_SET_SOURCE failed! - %m");
91 RESULT eDVBDemux::setSourcePVR(int pvrnum)
93 #if HAVE_DVB_API_VERSION >= 3
95 int n = DMX_SOURCE_DVR0 + pvrnum;
96 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
104 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
107 reader = new eDVBSectionReader(this, context, res);
113 RESULT eDVBDemux::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
116 reader = new eDVBPESReader(this, context, res);
122 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
126 recorder = new eDVBTSRecorder(this);
130 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
132 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
136 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
138 int fd = openDemux();
147 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
149 eDebug("DMX_GET_STC failed!");
156 eDebug("DMX_GET_STC - %lld", pts);
162 RESULT eDVBDemux::flush()
164 // FIXME: implement flushing the PVR queue here.
170 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
172 conn = new eConnection(this, m_event.connect(event));
176 void eDVBSectionReader::data(int)
178 __u8 data[4096]; // max. section size
180 r = ::read(fd, data, 4096);
183 eWarning("ERROR reading section - %m\n");
188 // this check should never happen unless the driver is crappy!
190 if ((c = crc32((unsigned)-1, data, r)))
192 eDebug("crc32 failed! is %x\n", c);
199 eDebug("data.. but not active");
202 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
205 fd = demux->openDemux();
209 notifier=eSocketNotifier::create(context, fd, eSocketNotifier::Read, false);
210 CONNECT(notifier->activated, eDVBSectionReader::data);
219 DEFINE_REF(eDVBSectionReader)
221 eDVBSectionReader::~eDVBSectionReader()
227 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
234 #if HAVE_DVB_API_VERSION < 3
235 dmxSctFilterParams sct;
237 dmx_sct_filter_params sct;
241 #if HAVE_DVB_API_VERSION < 3
244 sct.flags = DMX_IMMEDIATE_START;
246 if (mask.flags & eDVBSectionFilterMask::rfCRC)
248 sct.flags |= DMX_CHECK_CRC;
253 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
254 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
255 #if HAVE_DVB_API_VERSION >= 3
256 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
257 if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
258 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
261 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
264 #if HAVE_DVB_API_VERSION < 3
265 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
268 res = ::ioctl(fd, DMX_START, 0);
279 RESULT eDVBSectionReader::stop()
285 ::ioctl(fd, DMX_STOP);
291 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
293 conn = new eConnection(this, read.connect(r));
297 void eDVBPESReader::data(int)
303 r = ::read(m_fd, buffer, 16384);
308 if (errno == EAGAIN || errno == EINTR) /* ok */
310 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
317 eWarning("PES reader not active");
323 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
326 m_fd = m_demux->openDemux();
330 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
331 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
332 m_notifier = eSocketNotifier::create(context, m_fd, eSocketNotifier::Read, false);
333 CONNECT(m_notifier->activated, eDVBPESReader::data);
342 DEFINE_REF(eDVBPESReader)
344 eDVBPESReader::~eDVBPESReader()
350 RESULT eDVBPESReader::start(int pid)
358 #if HAVE_DVB_API_VERSION < 3
359 dmxPesFilterParams flt;
361 flt.pesType = DMX_PES_OTHER;
363 dmx_pes_filter_params flt;
365 flt.pes_type = DMX_PES_OTHER;
369 flt.input = DMX_IN_FRONTEND;
370 flt.output = DMX_OUT_TAP;
372 flt.flags = DMX_IMMEDIATE_START;
374 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
377 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
383 RESULT eDVBPESReader::stop()
389 ::ioctl(m_fd, DMX_STOP);
395 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
397 conn = new eConnection(this, m_read.connect(r));
401 class eDVBRecordFileThread: public eFilePushThread
404 eDVBRecordFileThread();
405 void setTimingPID(int pid);
407 void saveTimingInformation(const std::string &filename);
408 int getLastPTS(pts_t &pts);
410 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
412 eMPEGStreamParserTS m_ts_parser;
413 eMPEGStreamInformation m_stream_info;
414 off_t m_current_offset;
415 pts_t m_last_pcr; /* very approximate.. */
419 eDVBRecordFileThread::eDVBRecordFileThread()
420 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
422 m_current_offset = 0;
425 void eDVBRecordFileThread::setTimingPID(int pid)
427 m_ts_parser.setPid(pid);
430 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
432 m_stream_info.save(filename.c_str());
435 int eDVBRecordFileThread::getLastPTS(pts_t &pts)
437 return m_ts_parser.getLastPTS(pts);
440 int eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
442 m_ts_parser.parseData(m_current_offset, data, len);
444 m_current_offset += len;
449 DEFINE_REF(eDVBTSRecorder);
451 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
455 m_thread = new eDVBRecordFileThread();
456 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
458 m_demux->m_dvr_busy = 1;
462 eDVBTSRecorder::~eDVBTSRecorder()
467 m_demux->m_dvr_busy = 0;
471 RESULT eDVBTSRecorder::start()
476 if (m_target_fd == -1)
481 #if HAVE_DVB_API_VERSION < 3
482 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
484 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
486 m_source_fd = ::open(filename, O_RDONLY);
490 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
494 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
496 m_source_fd = ::open(filename, O_RDONLY);
500 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
504 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
506 dmx_pes_filter_params flt;
507 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
509 flt.input = DMX_IN_FRONTEND;
510 flt.output = DMX_OUT_TAP;
512 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
515 eDebug("DMX_SET_PES_FILTER: %m");
516 ::close(m_source_fd);
520 ::ioctl(m_source_fd, DMX_START);
524 m_thread->start(m_source_fd, m_target_fd);
527 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
533 RESULT eDVBTSRecorder::addPID(int pid)
535 if (m_pids.find(pid) != m_pids.end())
538 m_pids.insert(std::pair<int,int>(pid, -1));
544 RESULT eDVBTSRecorder::removePID(int pid)
546 if (m_pids.find(pid) == m_pids.end())
556 RESULT eDVBTSRecorder::setTimingPID(int pid)
560 m_thread->setTimingPID(pid);
564 RESULT eDVBTSRecorder::setTargetFD(int fd)
570 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
572 m_target_filename = filename;
576 RESULT eDVBTSRecorder::setBoundary(off_t max)
578 return -1; // not yet implemented
581 RESULT eDVBTSRecorder::stop()
583 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
593 if (m_target_filename != "")
594 m_thread->saveTimingInformation(m_target_filename + ".ap");
599 RESULT eDVBTSRecorder::getCurrentPCR(pts_t &pcr)
605 /* XXX: we need a lock here */
607 /* we don't filter PCR data, so just use the last received PTS, which is not accurate, but better than nothing */
608 return m_thread->getLastPTS(pcr);
611 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
613 conn = new eConnection(this, m_event.connect(event));
617 RESULT eDVBTSRecorder::startPID(int pid)
620 int fd = m_demux->openDemux();
623 eDebug("FAILED to open demux in ts recoder (%m)");
627 #if HAVE_DVB_API_VERSION < 3
628 dmxPesFilterParams flt;
630 flt.pesType = DMX_PES_OTHER;
632 dmx_pes_filter_params flt;
634 flt.pes_type = DMX_PES_OTHER;
638 flt.input = DMX_IN_FRONTEND;
639 flt.output = DMX_OUT_TS_TAP;
641 flt.flags = DMX_IMMEDIATE_START;
643 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
646 eDebug("set pes filter failed!");
653 if (::ioctl(m_source_fd, DMX_ADD_PID, pid) < 0) {
654 perror("DMX_ADD_PID");
655 if (errno == EAGAIN || errno == EINTR) {
667 void eDVBTSRecorder::stopPID(int pid)
670 if (m_pids[pid] != -1)
671 ::close(m_pids[pid]);
673 if (m_pids[pid] != -1)
676 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid) < 0) {
677 perror("DMX_REMOVE_PID");
678 if (errno == EAGAIN || errno == EINTR) {
690 void eDVBTSRecorder::filepushEvent(int event)
694 case eFilePushThread::evtWriteError:
695 m_event(eventWriteError);