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");
318 eDVBPESReader::eDVBPESReader(eDVBDemux *demux, eMainloop *context, RESULT &res): m_demux(demux)
321 m_fd = m_demux->openDemux();
325 ::ioctl(m_fd, DMX_SET_BUFFER_SIZE, 64*1024);
326 ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
327 m_notifier = new eSocketNotifier(context, m_fd, eSocketNotifier::Read, false);
328 CONNECT(m_notifier->activated, eDVBPESReader::data);
337 DEFINE_REF(eDVBPESReader)
339 eDVBPESReader::~eDVBPESReader()
347 RESULT eDVBPESReader::start(int pid)
355 #if HAVE_DVB_API_VERSION < 3
356 dmxPesFilterParams flt;
358 flt.pesType = DMX_PES_OTHER;
360 dmx_pes_filter_params flt;
362 flt.pes_type = DMX_PES_OTHER;
366 flt.input = DMX_IN_FRONTEND;
367 flt.output = DMX_OUT_TAP;
369 flt.flags = DMX_IMMEDIATE_START;
371 res = ::ioctl(m_fd, DMX_SET_PES_FILTER, &flt);
374 eWarning("PES filter: DMX_SET_PES_FILTER - %m");
380 RESULT eDVBPESReader::stop()
386 ::ioctl(m_fd, DMX_STOP);
392 RESULT eDVBPESReader::connectRead(const Slot2<void,const __u8*,int> &r, ePtr<eConnection> &conn)
394 conn = new eConnection(this, m_read.connect(r));
398 class eDVBRecordFileThread: public eFilePushThread
401 eDVBRecordFileThread();
402 void setTimingPID(int pid);
404 void saveTimingInformation(const std::string &filename);
406 void filterRecordData(const unsigned char *data, int len);
408 eMPEGStreamParserTS m_ts_parser;
409 eMPEGStreamInformation m_stream_info;
410 off_t m_current_offset;
414 eDVBRecordFileThread::eDVBRecordFileThread()
415 :eFilePushThread(IOPRIO_CLASS_RT, 7), m_ts_parser(m_stream_info)
417 m_current_offset = 0;
420 void eDVBRecordFileThread::setTimingPID(int pid)
422 m_ts_parser.setPid(pid);
425 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
427 m_stream_info.save(filename.c_str());
430 void eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len)
432 m_ts_parser.parseData(m_current_offset, data, len);
434 m_current_offset += len;
437 DEFINE_REF(eDVBTSRecorder);
439 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
443 m_thread = new eDVBRecordFileThread();
445 m_demux->m_dvr_busy = 1;
449 eDVBTSRecorder::~eDVBTSRecorder()
454 m_demux->m_dvr_busy = 0;
458 RESULT eDVBTSRecorder::start()
463 if (m_target_fd == -1)
468 #if HAVE_DVB_API_VERSION < 3
469 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
471 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
473 m_source_fd = ::open(filename, O_RDONLY);
477 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
481 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
483 m_source_fd = ::open(filename, O_RDONLY);
487 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
491 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
493 dmx_pes_filter_params flt;
494 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
496 flt.input = DMX_IN_FRONTEND;
497 flt.output = DMX_OUT_TAP;
499 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
502 eDebug("DMX_SET_PES_FILTER: %m");
503 ::close(m_source_fd);
507 ::ioctl(m_source_fd, DMX_START);
511 m_thread->start(m_source_fd, m_target_fd);
514 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
520 RESULT eDVBTSRecorder::addPID(int pid)
522 if (m_pids.find(pid) != m_pids.end())
525 m_pids.insert(std::pair<int,int>(pid, -1));
531 RESULT eDVBTSRecorder::removePID(int pid)
533 if (m_pids.find(pid) == m_pids.end())
543 RESULT eDVBTSRecorder::setTimingPID(int pid)
547 m_thread->setTimingPID(pid);
551 RESULT eDVBTSRecorder::setTargetFD(int fd)
557 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
559 m_target_filename = filename;
563 RESULT eDVBTSRecorder::setBoundary(off_t max)
565 return -1; // not yet implemented
568 RESULT eDVBTSRecorder::stop()
570 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
580 if (m_target_filename != "")
581 m_thread->saveTimingInformation(m_target_filename + ".ap");
586 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
588 conn = new eConnection(this, m_event.connect(event));
592 RESULT eDVBTSRecorder::startPID(int pid)
595 int fd = m_demux->openDemux();
598 eDebug("FAILED to open demux in ts recoder (%m)");
602 #if HAVE_DVB_API_VERSION < 3
603 dmxPesFilterParams flt;
605 flt.pesType = DMX_PES_OTHER;
607 dmx_pes_filter_params flt;
609 flt.pes_type = DMX_PES_OTHER;
613 flt.input = DMX_IN_FRONTEND;
614 flt.output = DMX_OUT_TS_TAP;
616 flt.flags = DMX_IMMEDIATE_START;
618 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
621 eDebug("set pes filter failed!");
627 if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
628 perror("DMX_ADD_PID");
635 void eDVBTSRecorder::stopPID(int pid)
638 if (m_pids[pid] != -1)
639 ::close(m_pids[pid]);
641 if (m_pids[pid] != -1)
643 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
644 perror("DMX_REMOVE_PID");