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
80 int n = DMX_SOURCE_FRONT0 + fenum;
81 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
83 eDebug("DMX_SET_SOURCE failed! - %m");
90 RESULT eDVBDemux::setSourcePVR(int pvrnum)
92 #if HAVE_DVB_API_VERSION >= 3
94 int n = DMX_SOURCE_DVR0 + pvrnum;
95 int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
102 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
105 reader = new eDVBSectionReader(this, context, res);
111 RESULT eDVBDemux::createPESReader(eMainloop *context, ePtr<iDVBPESReader> &reader)
114 reader = new eDVBPESReader(this, context, res);
120 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
124 recorder = new eDVBTSRecorder(this);
128 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
130 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
134 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
136 int fd = openDemux();
145 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
147 eDebug("DMX_GET_STC failed!");
154 eDebug("DMX_GET_STC - %lld", pts);
160 RESULT eDVBDemux::flush()
162 // FIXME: implement flushing the PVR queue here.
168 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
170 conn = new eConnection(this, m_event.connect(event));
174 void eDVBSectionReader::data(int)
176 __u8 data[4096]; // max. section size
178 r = ::read(fd, data, 4096);
181 eWarning("ERROR reading section - %m\n");
186 // this check should never happen unless the driver is crappy!
188 if ((c = crc32((unsigned)-1, data, r)))
190 eDebug("crc32 failed! is %x\n", c);
197 eDebug("data.. but not active");
200 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
203 fd = demux->openDemux();
207 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
208 CONNECT(notifier->activated, eDVBSectionReader::data);
217 DEFINE_REF(eDVBSectionReader)
219 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) /* 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 = new eSocketNotifier(context, m_fd, eSocketNotifier::Read, false);
333 CONNECT(m_notifier->activated, eDVBPESReader::data);
342 DEFINE_REF(eDVBPESReader)
344 eDVBPESReader::~eDVBPESReader()
352 RESULT eDVBPESReader::start(int pid)
360 #if HAVE_DVB_API_VERSION < 3
361 dmxPesFilterParams flt;
363 flt.pesType = DMX_PES_OTHER;
365 dmx_pes_filter_params flt;
367 flt.pes_type = DMX_PES_OTHER;
371 flt.input = DMX_IN_FRONTEND;
372 flt.output = DMX_OUT_TAP;
374 flt.flags = DMX_IMMEDIATE_START;
376 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
379 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
385 RESULT eDVBPESReader::stop()
391 ::ioctl(m_fd, DMX_STOP);
397 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
399 conn = new eConnection(this, m_read.connect(r));
403 class eDVBRecordFileThread: public eFilePushThread
406 eDVBRecordFileThread();
407 void setTimingPID(int pid);
409 void saveTimingInformation(const std::string &filename);
411 int filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining);
413 eMPEGStreamParserTS m_ts_parser;
414 eMPEGStreamInformation m_stream_info;
415 off_t m_current_offset;
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::filterRecordData(const unsigned char *data, int len, size_t ¤t_span_remaining)
437 m_ts_parser.parseData(m_current_offset, data, len);
439 m_current_offset += len;
444 DEFINE_REF(eDVBTSRecorder);
446 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
450 m_thread = new eDVBRecordFileThread();
451 CONNECT(m_thread->m_event, eDVBTSRecorder::filepushEvent);
453 m_demux->m_dvr_busy = 1;
457 eDVBTSRecorder::~eDVBTSRecorder()
462 m_demux->m_dvr_busy = 0;
466 RESULT eDVBTSRecorder::start()
471 if (m_target_fd == -1)
476 #if HAVE_DVB_API_VERSION < 3
477 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
479 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
481 m_source_fd = ::open(filename, O_RDONLY);
485 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
489 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
491 m_source_fd = ::open(filename, O_RDONLY);
495 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
499 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
501 dmx_pes_filter_params flt;
502 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
504 flt.input = DMX_IN_FRONTEND;
505 flt.output = DMX_OUT_TAP;
507 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
510 eDebug("DMX_SET_PES_FILTER: %m");
511 ::close(m_source_fd);
515 ::ioctl(m_source_fd, DMX_START);
519 m_thread->start(m_source_fd, m_target_fd);
522 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
528 RESULT eDVBTSRecorder::addPID(int pid)
530 if (m_pids.find(pid) != m_pids.end())
533 m_pids.insert(std::pair<int,int>(pid, -1));
539 RESULT eDVBTSRecorder::removePID(int pid)
541 if (m_pids.find(pid) == m_pids.end())
551 RESULT eDVBTSRecorder::setTimingPID(int pid)
555 m_thread->setTimingPID(pid);
559 RESULT eDVBTSRecorder::setTargetFD(int fd)
565 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
567 m_target_filename = filename;
571 RESULT eDVBTSRecorder::setBoundary(off_t max)
573 return -1; // not yet implemented
576 RESULT eDVBTSRecorder::stop()
578 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
588 if (m_target_filename != "")
589 m_thread->saveTimingInformation(m_target_filename + ".ap");
594 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
596 conn = new eConnection(this, m_event.connect(event));
600 RESULT eDVBTSRecorder::startPID(int pid)
603 int fd = m_demux->openDemux();
606 eDebug("FAILED to open demux in ts recoder (%m)");
610 #if HAVE_DVB_API_VERSION < 3
611 dmxPesFilterParams flt;
613 flt.pesType = DMX_PES_OTHER;
615 dmx_pes_filter_params flt;
617 flt.pes_type = DMX_PES_OTHER;
621 flt.input = DMX_IN_FRONTEND;
622 flt.output = DMX_OUT_TS_TAP;
624 flt.flags = DMX_IMMEDIATE_START;
626 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
629 eDebug("set pes filter failed!");
635 if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
636 perror("DMX_ADD_PID");
643 void eDVBTSRecorder::stopPID(int pid)
646 if (m_pids[pid] != -1)
647 ::close(m_pids[pid]);
649 if (m_pids[pid] != -1)
651 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
652 perror("DMX_REMOVE_PID");
658 void eDVBTSRecorder::filepushEvent(int event)
662 case eFilePushThread::evtWriteError:
663 m_event(eventWriteError);