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 eDebug("data.. but not active");
85 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
88 #if HAVE_DVB_API_VERSION < 3
89 sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
91 sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
93 fd = ::open(filename, O_RDWR);
95 eDebug("eDVBSectionReader has fd %d", fd);
99 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read);
100 CONNECT(notifier->activated, eDVBSectionReader::data);
109 DEFINE_REF(eDVBSectionReader)
111 eDVBSectionReader::~eDVBSectionReader()
119 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
125 #if HAVE_DVB_API_VERSION < 3
126 dmxSctFilterParams sct;
128 dmx_sct_filter_params sct;
132 #if HAVE_DVB_API_VERSION < 3
135 sct.flags = DMX_IMMEDIATE_START;
137 if (mask.flags & eDVBSectionFilterMask::rfCRC)
139 sct.flags |= DMX_CHECK_CRC;
144 memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
145 memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
146 #if HAVE_DVB_API_VERSION >= 3
147 memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
150 res = ::ioctl(fd, DMX_SET_FILTER, &sct);
153 #if HAVE_DVB_API_VERSION < 3
154 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
157 res = ::ioctl(fd, DMX_START, 0);
168 RESULT eDVBSectionReader::stop()
174 ::ioctl(fd, DMX_STOP);
179 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
181 conn = new eConnection(this, read.connect(r));
185 DEFINE_REF(eDVBTSRecorder);
187 class eDVBTSRecorderThread: public eThread
190 eDVBTSRecorderThread();
193 void start(int sourcefd, int destfd);
196 unsigned char m_buffer[65536];
197 int m_buf_start, m_buf_end;
198 int m_fd_source, m_fd_dest;
201 eDVBTSRecorderThread::eDVBTSRecorderThread()
204 m_buf_start = m_buf_end = 0;
207 static void signal_handler(int x)
211 void eDVBTSRecorderThread::thread()
213 eDebug("RECORDING THREAD START");
214 // this is race. FIXME.
216 /* we set the signal to not restart syscalls, so we can detect our signal. */
217 struct sigaction act;
218 act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it :/
220 sigaction(SIGUSR1, &act, 0);
222 /* m_stop must be evaluated after each syscall. */
225 /* first try flushing the bufptr */
226 if (m_buf_start != m_buf_end)
228 // TODO: take care of boundaries.
229 int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
234 eDebug("eDVBTSRecorder *write error* - not yet handled");
235 // ... we would stop the thread
237 printf("TSRECORD: wrote %d bytes\n", w);
242 /* now fill our buffer. */
244 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
250 eDebug("eDVBTSRecorder *read error* - not yet handled");
252 printf("TSRECORD: read %d bytes\n", m_buf_end);
255 eDebug("RECORDING THREAD STOP");
258 void eDVBTSRecorderThread::start(int fd_source, int fd_dest)
260 m_fd_source = fd_source;
266 void eDVBTSRecorderThread::stop()
273 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
278 m_thread = new eDVBTSRecorderThread();
279 m_demux->m_dvr_busy = 1;
282 eDVBTSRecorder::~eDVBTSRecorder()
286 m_demux->m_dvr_busy = 0;
289 RESULT eDVBTSRecorder::start()
294 if (m_target_fd == -1)
298 #if HAVE_DVB_API_VERSION < 3
299 snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
301 snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
303 m_source_fd = ::open(filename, O_RDONLY);
307 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
311 m_thread->start(m_source_fd, m_target_fd);
314 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
320 RESULT eDVBTSRecorder::addPID(int pid)
322 if (m_pids.find(pid) != m_pids.end())
325 m_pids.insert(std::pair<int,int>(pid, -1));
331 RESULT eDVBTSRecorder::removePID(int pid)
333 if (m_pids.find(pid) == m_pids.end())
343 RESULT eDVBTSRecorder::setFormat(int format)
351 RESULT eDVBTSRecorder::setTargetFD(int fd)
357 RESULT eDVBTSRecorder::setBoundary(off_t max)
359 return -1; // not yet implemented
362 RESULT eDVBTSRecorder::stop()
364 for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
376 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
378 conn = new eConnection(this, m_event.connect(event));
382 RESULT eDVBTSRecorder::startPID(int pid)
385 #if HAVE_DVB_API_VERSION < 3
386 snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
388 snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
390 int fd = ::open(filename, O_RDWR);
393 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
397 #if HAVE_DVB_API_VERSION < 3
398 dmxPesFilterParams flt;
400 flt.pesType = DMX_PES_OTHER;
402 dmx_pes_filter_params flt;
404 flt.pes_type = DMX_PES_OTHER;
408 flt.input = DMX_IN_FRONTEND;
409 flt.output = DMX_OUT_TS_TAP;
411 flt.flags = DMX_IMMEDIATE_START;
413 int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
416 eDebug("set pes filter failed!");
425 void eDVBTSRecorder::stopPID(int pid)
427 ::close(m_pids[pid]);