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::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
115 recorder = new eDVBTSRecorder(this);
119 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder, int primary)
121 decoder = new eTSMPEGDecoder(this, primary ? 0 : 1);
125 RESULT eDVBDemux::getSTC(pts_t &pts, int num)
127 int fd = openDemux();
136 if (ioctl(fd, DMX_GET_STC, &stc) < 0)
148 RESULT eDVBDemux::flush()
150 // FIXME: implement flushing the PVR queue here.
156 RESULT eDVBDemux::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
158 conn = new eConnection(this, m_event.connect(event));
162 void eDVBSectionReader::data(int)
164 __u8 data[4096]; // max. section size
166 r = ::read(fd, data, 4096);
169 eWarning("ERROR reading section - %m\n");
174 // this check should never happen unless the driver is crappy!
176 if ((c = crc32((unsigned)-1, data, r)))
178 eDebug("crc32 failed! is %x\n", c);
185 eDebug("data.. but not active");
188 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
191 fd = demux->openDemux();
195 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read, false);
196 CONNECT(notifier->activated, eDVBSectionReader::data);
205 DEFINE_REF(eDVBSectionReader)
207 eDVBSectionReader::~eDVBSectionReader()
215 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
222 #if HAVE_DVB_API_VERSION < 3
223 dmxSctFilterParams sct;
225 dmx_sct_filter_params sct;
229 #if HAVE_DVB_API_VERSION < 3
232 sct.flags = DMX_IMMEDIATE_START;
234 if (mask.flags & eDVBSectionFilterMask::rfCRC)
236 sct.flags |= DMX_CHECK_CRC;
241 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
242 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
243 #if HAVE_DVB_API_VERSION >= 3
244 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
245 if (::ioctl(fd, DMX_SET_BUFFER_SIZE, 8192*8) < 0)
246 eDebug("DMX_SET_BUFFER_SIZE failed(%m)");
249 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
252 #if HAVE_DVB_API_VERSION < 3
253 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
256 res = ::ioctl(fd, DMX_START, 0);
267 RESULT eDVBSectionReader::stop()
273 ::ioctl(fd, DMX_STOP);
279 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
281 conn = new eConnection(this, read.connect(r));
285 class eDVBRecordFileThread: public eFilePushThread
288 eDVBRecordFileThread();
289 void setTimingPID(int pid);
291 void saveTimingInformation(const std::string &filename);
293 void filterRecordData(const unsigned char *data, int len);
295 eMPEGStreamParserTS m_ts_parser;
296 eMPEGStreamInformation m_stream_info;
297 off_t m_current_offset;
301 eDVBRecordFileThread::eDVBRecordFileThread()
302 : m_ts_parser(m_stream_info)
304 m_current_offset = 0;
307 void eDVBRecordFileThread::setTimingPID(int pid)
309 m_ts_parser.setPid(pid);
312 void eDVBRecordFileThread::saveTimingInformation(const std::string &filename)
314 m_stream_info.save(filename.c_str());
317 void eDVBRecordFileThread::filterRecordData(const unsigned char *data, int len)
319 m_ts_parser.parseData(m_current_offset, data, len);
321 m_current_offset += len;
324 DEFINE_REF(eDVBTSRecorder);
326 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
330 m_thread = new eDVBRecordFileThread();
332 m_demux->m_dvr_busy = 1;
336 eDVBTSRecorder::~eDVBTSRecorder()
341 m_demux->m_dvr_busy = 0;
345 RESULT eDVBTSRecorder::start()
350 if (m_target_fd == -1)
355 #if HAVE_DVB_API_VERSION < 3
356 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
358 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
360 m_source_fd = ::open(filename, O_RDONLY);
364 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
368 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
370 m_source_fd = ::open(filename, O_RDONLY);
374 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
378 ::ioctl(m_source_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
380 dmx_pes_filter_params flt;
381 flt.pes_type = (dmx_pes_type_t)DMX_TAP_TS;
383 flt.input = DMX_IN_FRONTEND;
384 flt.output = DMX_OUT_TAP;
386 int res = ::ioctl(m_source_fd, DMX_SET_PES_FILTER, &flt);
389 eDebug("DMX_SET_PES_FILTER: %m");
390 ::close(m_source_fd);
394 ::ioctl(m_source_fd, DMX_START);
398 m_thread->start(m_source_fd, m_target_fd);
401 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
407 RESULT eDVBTSRecorder::addPID(int pid)
409 if (m_pids.find(pid) != m_pids.end())
412 m_pids.insert(std::pair<int,int>(pid, -1));
418 RESULT eDVBTSRecorder::removePID(int pid)
420 if (m_pids.find(pid) == m_pids.end())
430 RESULT eDVBTSRecorder::setTimingPID(int pid)
434 m_thread->setTimingPID(pid);
438 RESULT eDVBTSRecorder::setTargetFD(int fd)
444 RESULT eDVBTSRecorder::setTargetFilename(const char *filename)
446 m_target_filename = filename;
450 RESULT eDVBTSRecorder::setBoundary(off_t max)
452 return -1; // not yet implemented
455 RESULT eDVBTSRecorder::stop()
457 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
467 if (m_target_filename != "")
468 m_thread->saveTimingInformation(m_target_filename + ".ap");
473 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
475 conn = new eConnection(this, m_event.connect(event));
479 RESULT eDVBTSRecorder::startPID(int pid)
482 int fd = m_demux->openDemux();
485 eDebug("FAILED to open demux in ts recoder (%m)");
489 #if HAVE_DVB_API_VERSION < 3
490 dmxPesFilterParams flt;
492 flt.pesType = DMX_PES_OTHER;
494 dmx_pes_filter_params flt;
496 flt.pes_type = DMX_PES_OTHER;
500 flt.input = DMX_IN_FRONTEND;
501 flt.output = DMX_OUT_TS_TAP;
503 flt.flags = DMX_IMMEDIATE_START;
505 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
508 eDebug("set pes filter failed!");
514 if (::ioctl(m_source_fd, DMX_ADD_PID, pid))
515 perror("DMX_ADD_PID");
522 void eDVBTSRecorder::stopPID(int pid)
525 if (m_pids[pid] != -1)
526 ::close(m_pids[pid]);
528 if (m_pids[pid] != -1)
530 if (::ioctl(m_source_fd, DMX_REMOVE_PID, pid))
531 perror("DMX_REMOVE_PID");