9 #include <lib/base/thread.h>
11 #if HAVE_DVB_API_VERSION < 3
13 #ifndef DMX_SET_NEGFILTER_MASK
14 #define DMX_SET_NEGFILTER_MASK _IOW('o',48,uint8_t *)
17 #include <linux/dvb/dmx.h>
22 #include <lib/base/eerror.h>
23 #include <lib/dvb/idvb.h>
24 #include <lib/dvb/demux.h>
25 #include <lib/dvb/esection.h>
26 #include <lib/dvb/decoder.h>
28 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
33 eDVBDemux::~eDVBDemux()
39 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
42 reader = new eDVBSectionReader(this, context, res);
48 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
52 recorder = new eDVBTSRecorder(this);
56 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder)
58 decoder = new eTSMPEGDecoder(this, 0);
62 void eDVBSectionReader::data(int)
64 __u8 data[4096]; // max. section size
66 r = ::read(fd, data, 4096);
69 eWarning("ERROR reading section - %m\n");
74 // this check should never happen unless the driver is crappy!
76 if ((c = crc32((unsigned)-1, data, r)))
77 eFatal("crc32 failed! is %x\n", c);
82 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
85 #if HAVE_DVB_API_VERSION < 3
86 sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
88 sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
90 fd = ::open(filename, O_RDWR);
92 eDebug("eDVBSectionReader has fd %d", fd);
96 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read);
97 CONNECT(notifier->activated, eDVBSectionReader::data);
106 DEFINE_REF(eDVBSectionReader)
108 eDVBSectionReader::~eDVBSectionReader()
116 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
122 #if HAVE_DVB_API_VERSION < 3
123 dmxSctFilterParams sct;
125 dmx_sct_filter_params sct;
129 #if HAVE_DVB_API_VERSION < 3
132 sct.flags = DMX_IMMEDIATE_START;
134 if (mask.flags & eDVBSectionFilterMask::rfCRC)
136 sct.flags |= DMX_CHECK_CRC;
141 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
142 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
143 #if HAVE_DVB_API_VERSION >= 3
144 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
147 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
150 #if HAVE_DVB_API_VERSION < 3
151 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
154 res = ::ioctl(fd, DMX_START, 0);
165 RESULT eDVBSectionReader::stop()
170 ::ioctl(fd, DMX_STOP);
175 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
177 conn = new eConnection(this, read.connect(r));
181 DEFINE_REF(eDVBTSRecorder);
183 class eDVBTSRecorderThread: public eThread
186 eDVBTSRecorderThread();
189 void start(int sourcefd, int destfd);
192 unsigned char m_buffer[65536];
193 int m_buf_start, m_buf_end;
194 int m_fd_source, m_fd_dest;
197 eDVBTSRecorderThread::eDVBTSRecorderThread()
200 m_buf_start = m_buf_end = 0;
203 static void signal_handler(int x)
207 void eDVBTSRecorderThread::thread()
209 eDebug("RECORDING THREAD START");
210 // this is race. FIXME.
212 /* we set the signal to not restart syscalls, so we can detect our signal. */
213 struct sigaction act;
214 act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it :/
216 sigaction(SIGUSR1, &act, 0);
218 /* m_stop must be evaluated after each syscall. */
221 /* first try flushing the bufptr */
222 if (m_buf_start != m_buf_end)
224 // TODO: take care of boundaries.
225 int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
230 eDebug("eDVBTSRecorder *write error* - not yet handled");
231 // ... we would stop the thread
233 printf("TSRECORD: wrote %d bytes\n", w);
238 /* now fill our buffer. */
240 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
246 eDebug("eDVBTSRecorder *read error* - not yet handled");
248 printf("TSRECORD: read %d bytes\n", m_buf_end);
251 eDebug("RECORDING THREAD STOP");
254 void eDVBTSRecorderThread::start(int fd_source, int fd_dest)
256 m_fd_source = fd_source;
262 void eDVBTSRecorderThread::stop()
269 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
274 m_thread = new eDVBTSRecorderThread();
275 m_demux->m_dvr_busy = 1;
278 eDVBTSRecorder::~eDVBTSRecorder()
282 m_demux->m_dvr_busy = 0;
285 RESULT eDVBTSRecorder::start()
290 if (m_target_fd == -1)
294 #if HAVE_DVB_API_VERSION < 3
295 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
297 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
299 m_source_fd = ::open(filename, O_RDONLY);
303 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
307 m_thread->start(m_source_fd, m_target_fd);
310 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
316 RESULT eDVBTSRecorder::addPID(int pid)
318 if (m_pids.find(pid) != m_pids.end())
321 m_pids.insert(std::pair<int,int>(pid, -1));
327 RESULT eDVBTSRecorder::removePID(int pid)
329 if (m_pids.find(pid) == m_pids.end())
339 RESULT eDVBTSRecorder::setFormat(int format)
347 RESULT eDVBTSRecorder::setTargetFD(int fd)
353 RESULT eDVBTSRecorder::setBoundary(off_t max)
355 return -1; // not yet implemented
358 RESULT eDVBTSRecorder::stop()
360 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
372 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
374 conn = new eConnection(this, m_event.connect(event));
378 RESULT eDVBTSRecorder::startPID(int pid)
381 #if HAVE_DVB_API_VERSION < 3
382 snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
384 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
386 int fd = ::open(filename, O_RDWR);
389 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
393 #if HAVE_DVB_API_VERSION < 3
394 dmxPesFilterParams flt;
396 flt.pesType = DMX_PES_OTHER;
398 dmx_pes_filter_params flt;
400 flt.pes_type = DMX_PES_OTHER;
404 flt.input = DMX_IN_FRONTEND;
405 flt.output = DMX_OUT_TS_TAP;
407 flt.flags = DMX_IMMEDIATE_START;
409 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
412 eDebug("set pes filter failed!");
421 void eDVBTSRecorder::stopPID(int pid)
423 ::close(m_pids[pid]);