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)
157 RESULT eDVBDemux::flush()
159 // FIXME: implement flushing the PVR queue here.
165 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
167 conn = new eConnection(this, m_event.connect(event));
171 void eDVBSectionReader::data(int)
173 __u8 data[4096]; // max. section size
175 r = ::read(fd, data, 4096);
178 eWarning("ERROR reading section - %m\n");
183 // this check should never happen unless the driver is crappy!
185 if ((c = crc32((unsigned)-1, data, r)))
187 eDebug("crc32 failed! is %x\n", c);
194 eDebug("data.. but not active");
197 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
200 fd = demux->openDemux();
204 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
205 CONNECT(notifier->activated, eDVBSectionReader::data);
214 DEFINE_REF(eDVBSectionReader)
216 eDVBSectionReader::~eDVBSectionReader()
224 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
231 #if HAVE_DVB_API_VERSION < 3
232 dmxSctFilterParams sct;
234 dmx_sct_filter_params sct;
238 #if HAVE_DVB_API_VERSION < 3
241 sct.flags = DMX_IMMEDIATE_START;
243 if (mask.flags & eDVBSectionFilterMask::rfCRC)
245 sct.flags |= DMX_CHECK_CRC;
250 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
251 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
252 #if HAVE_DVB_API_VERSION >= 3
253 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
254 if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
255 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
258 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
261 #if HAVE_DVB_API_VERSION < 3
262 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
265 res = ::ioctl(fd, DMX_START, 0);
276 RESULT eDVBSectionReader::stop()
282 ::ioctl(fd, DMX_STOP);
288 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
290 conn = new eConnection(this, read.connect(r));
294 void eDVBPESReader::data(int)
300 r = ::read(m_fd, buffer, 16384);
305 if (errno == EAGAIN) /* ok */
307 eWarning("ERROR reading PES (fd=%d) - %m", m_fd);
314 eWarning("PES reader not active");
320 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
323 m_fd = m_demux->openDemux();
327 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
328 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
329 m_notifier = new eSocketNotifier(context, m_fd, eSocketNotifier::Read, false);
330 CONNECT(m_notifier->activated, eDVBPESReader::data);
339 DEFINE_REF(eDVBPESReader)
341 eDVBPESReader::~eDVBPESReader()
349 RESULT eDVBPESReader::start(int pid)
357 #if HAVE_DVB_API_VERSION < 3
358 dmxPesFilterParams flt;
360 flt.pesType = DMX_PES_OTHER;
362 dmx_pes_filter_params flt;
364 flt.pes_type = DMX_PES_OTHER;
368 flt.input = DMX_IN_FRONTEND;
369 flt.output = DMX_OUT_TAP;
371 flt.flags = DMX_IMMEDIATE_START;
373 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
376 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
382 RESULT eDVBPESReader::stop()
388 ::ioctl(m_fd, DMX_STOP);
394 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
396 conn = new eConnection(this, m_read.connect(r));
400 class eDVBRecordFileThread: public eFilePushThread
403 eDVBRecordFileThread();
404 void setTimingPID(int pid);
406 void saveTimingInformation(const std::string &filename);
408 void filterRecordData(const unsigned char *data, int len);
410 eMPEGStreamParserTS m_ts_parser;
411 eMPEGStreamInformation m_stream_info;
412 off_t m_current_offset;
416 eDVBRecordFileThread::eDVBRecordFileThread()
417 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
419 m_current_offset = 0;
422 void eDVBRecordFileThread::setTimingPID(int pid)
424 m_ts_parser.setPid(pid);
427 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
429 m_stream_info.save(filename.c_str());
432 void eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len)
434 m_ts_parser.parseData(m_current_offset, data, len);
436 m_current_offset += len;
439 DEFINE_REF(eDVBTSRecorder);
441 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
445 m_thread = new eDVBRecordFileThread();
447 m_demux->m_dvr_busy = 1;
451 eDVBTSRecorder::~eDVBTSRecorder()
456 m_demux->m_dvr_busy = 0;
460 RESULT eDVBTSRecorder::start()
465 if (m_target_fd == -1)
470 #if HAVE_DVB_API_VERSION < 3
471 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
473 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
475 m_source_fd = ::open(filename, O_RDONLY);
479 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
483 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
485 m_source_fd = ::open(filename, O_RDONLY);
489 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
493 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
495 dmx_pes_filter_params flt;
496 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
498 flt.input = DMX_IN_FRONTEND;
499 flt.output = DMX_OUT_TAP;
501 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
504 eDebug("DMX_SET_PES_FILTER: %m");
505 ::close(m_source_fd);
509 ::ioctl(m_source_fd, DMX_START);
513 m_thread->start(m_source_fd, m_target_fd);
516 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
522 RESULT eDVBTSRecorder::addPID(int pid)
524 if (m_pids.find(pid) != m_pids.end())
527 m_pids.insert(std::pair<int,int>(pid, -1));
533 RESULT eDVBTSRecorder::removePID(int pid)
535 if (m_pids.find(pid) == m_pids.end())
545 RESULT eDVBTSRecorder::setTimingPID(int pid)
549 m_thread->setTimingPID(pid);
553 RESULT eDVBTSRecorder::setTargetFD(int fd)
559 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
561 m_target_filename = filename;
565 RESULT eDVBTSRecorder::setBoundary(off_t max)
567 return -1; // not yet implemented
570 RESULT eDVBTSRecorder::stop()
572 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
582 if (m_target_filename != "")
583 m_thread->saveTimingInformation(m_target_filename + ".ap");
588 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
590 conn = new eConnection(this, m_event.connect(event));
594 RESULT eDVBTSRecorder::startPID(int pid)
597 int fd = m_demux->openDemux();
600 eDebug("FAILED to open demux in ts recoder (%m)");
604 #if HAVE_DVB_API_VERSION < 3
605 dmxPesFilterParams flt;
607 flt.pesType = DMX_PES_OTHER;
609 dmx_pes_filter_params flt;
611 flt.pes_type = DMX_PES_OTHER;
615 flt.input = DMX_IN_FRONTEND;
616 flt.output = DMX_OUT_TS_TAP;
618 flt.flags = DMX_IMMEDIATE_START;
620 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
623 eDebug("set pes filter failed!");
629 if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
630 perror("DMX_ADD_PID");
637 void eDVBTSRecorder::stopPID(int pid)
640 if (m_pids[pid] != -1)
641 ::close(m_pids[pid]);
643 if (m_pids[pid] != -1)
645 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
646 perror("DMX_REMOVE_PID");