more rotor stuff
[enigma2.git] / lib / dvb / demux.cpp
1 #include <config.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <signal.h>
8
9 #include <lib/base/thread.h>
10
11 #if HAVE_DVB_API_VERSION < 3
12 #include <ost/dmx.h>
13 #ifndef DMX_SET_NEGFILTER_MASK
14         #define DMX_SET_NEGFILTER_MASK   _IOW('o',48,uint8_t *)
15 #endif
16 #else
17 #include <linux/dvb/dmx.h>
18 #endif
19
20 #include "crc32.h"
21
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>
27
28 eDVBDemux::eDVBDemux(int adapter, int demux): adapter(adapter), demux(demux)
29 {
30         m_dvr_busy = 0;
31 }
32
33 eDVBDemux::~eDVBDemux()
34 {
35 }
36
37 DEFINE_REF(eDVBDemux)
38
39 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
40 {
41         RESULT res;
42         reader = new eDVBSectionReader(this, context, res);
43         if (res)
44                 reader = 0;
45         return res;
46 }
47
48 RESULT eDVBDemux::createTSRecorder(ePtr<iDVBTSRecorder> &recorder)
49 {
50         if (m_dvr_busy)
51                 return -EBUSY;
52         recorder = new eDVBTSRecorder(this);
53         return 0;
54 }
55
56 RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder)
57 {
58         decoder = new eTSMPEGDecoder(this, 0);
59         return 0;
60 }
61
62 void eDVBSectionReader::data(int)
63 {
64         __u8 data[4096]; // max. section size
65         int r;
66         r = ::read(fd, data, 4096);
67         if(r < 0)
68         {
69                 eWarning("ERROR reading section - %m\n");
70                 return;
71         }
72         if (checkcrc)
73         {
74                         // this check should never happen unless the driver is crappy!
75                 unsigned int c;
76                 if ((c = crc32((unsigned)-1, data, r)))
77                         eFatal("crc32 failed! is %x\n", c);
78         }
79         read(data);
80 }
81
82 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
83 {
84         char filename[128];
85 #if HAVE_DVB_API_VERSION < 3
86         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
87 #else
88         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
89 #endif
90         fd = ::open(filename, O_RDWR);
91         
92         eDebug("eDVBSectionReader has fd %d", fd);
93         
94         if (fd >= 0)
95         {
96                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read);
97                 CONNECT(notifier->activated, eDVBSectionReader::data);
98                 res = 0;
99         } else
100         {
101                 perror(filename);
102                 res = errno;
103         }
104 }
105
106 DEFINE_REF(eDVBSectionReader)
107
108 eDVBSectionReader::~eDVBSectionReader()
109 {
110         if (notifier)
111                 delete notifier;
112         if (fd >= 0)
113                 ::close(fd);
114 }
115
116 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
117 {
118         RESULT res;
119         if (fd < 0)
120                 return -ENODEV;
121
122 #if HAVE_DVB_API_VERSION < 3
123         dmxSctFilterParams sct;
124 #else
125         dmx_sct_filter_params sct;
126 #endif
127         sct.pid     = mask.pid;
128         sct.timeout = 0;
129 #if HAVE_DVB_API_VERSION < 3
130         sct.flags   = 0;
131 #else
132         sct.flags   = DMX_IMMEDIATE_START;
133 #endif
134         if (mask.flags & eDVBSectionFilterMask::rfCRC)
135         {
136                 sct.flags |= DMX_CHECK_CRC;
137                 checkcrc = 1;
138         } else
139                 checkcrc = 0;
140         
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);
145 #endif
146         
147         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
148         if (!res)
149         {
150 #if HAVE_DVB_API_VERSION < 3
151                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
152                 if (!res)
153                 {
154                         res = ::ioctl(fd, DMX_START, 0);
155                         if (!res)
156                                 active = 1;
157                 }
158 #else
159                 active = 1;
160 #endif
161         }
162         return res;
163 }
164
165 RESULT eDVBSectionReader::stop()
166 {
167         if (!active)
168                 return -1;
169         
170         ::ioctl(fd, DMX_STOP);
171         
172         return 0;
173 }
174
175 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
176 {
177         conn = new eConnection(this, read.connect(r));
178         return 0;
179 }
180
181 DEFINE_REF(eDVBTSRecorder);
182
183 class eDVBTSRecorderThread: public eThread
184 {
185 public:
186         eDVBTSRecorderThread();
187         void thread();
188         void stop();
189         void start(int sourcefd, int destfd);
190 private:
191         int m_stop;
192         unsigned char m_buffer[65536];
193         int m_buf_start, m_buf_end;
194         int m_fd_source, m_fd_dest;
195 };
196
197 eDVBTSRecorderThread::eDVBTSRecorderThread()
198 {
199         m_stop = 0;
200         m_buf_start = m_buf_end = 0;
201 }
202
203 static void signal_handler(int x)
204 {
205 }
206
207 void eDVBTSRecorderThread::thread()
208 {
209         eDebug("RECORDING THREAD START");
210                 // this is race. FIXME.
211         
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 :/
215         act.sa_flags = 0;
216         sigaction(SIGUSR1, &act, 0);
217         
218                 /* m_stop must be evaluated after each syscall. */
219         while (!m_stop)
220         {
221                         /* first try flushing the bufptr */
222                 if (m_buf_start != m_buf_end)
223                 {
224                                 // TODO: take care of boundaries.
225                         int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
226                         if (w <= 0)
227                         {
228                                 if (errno == -EINTR)
229                                         continue;
230                                 eDebug("eDVBTSRecorder *write error* - not yet handled");
231                                 // ... we would stop the thread
232                         }
233                         printf("TSRECORD: wrote %d bytes\n", w);
234                         m_buf_start += w;
235                         continue;
236                 }
237                         
238                         /* now fill our buffer. */
239                 m_buf_start = 0;
240                 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
241                 if (m_buf_end < 0)
242                 {
243                         m_buf_end = 0;
244                         if (errno == EINTR)
245                                 continue;
246                         eDebug("eDVBTSRecorder *read error* - not yet handled");
247                 }
248                 printf("TSRECORD: read %d bytes\n", m_buf_end);
249         }
250         
251         eDebug("RECORDING THREAD STOP");
252 }
253
254 void eDVBTSRecorderThread::start(int fd_source, int fd_dest)
255 {
256         m_fd_source = fd_source;
257         m_fd_dest = fd_dest;
258         m_stop = 0;
259         run();
260 }
261
262 void eDVBTSRecorderThread::stop()
263 {
264         m_stop = 1;
265         sendSignal(SIGUSR1);
266         kill();
267 }
268
269 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
270 {
271         m_running = 0;
272         m_format = 0;
273         m_target_fd = -1;
274         m_thread = new eDVBTSRecorderThread();
275         m_demux->m_dvr_busy = 1;
276 }
277
278 eDVBTSRecorder::~eDVBTSRecorder()
279 {
280         stop();
281         delete m_thread;
282         m_demux->m_dvr_busy = 0;
283 }
284
285 RESULT eDVBTSRecorder::start()
286 {
287         if (m_running)
288                 return -1;
289         
290         if (m_target_fd == -1)
291                 return -2;
292                 
293         char filename[128];
294 #if HAVE_DVB_API_VERSION < 3
295         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
296 #else
297         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
298 #endif
299         m_source_fd = ::open(filename, O_RDONLY);
300         
301         if (m_source_fd < 0)
302         {
303                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
304                 return -3;
305         }
306         
307         m_thread->start(m_source_fd, m_target_fd);
308         m_running = 1;
309         
310         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
311                 startPID(i->first);
312         
313         return 0;
314 }
315
316 RESULT eDVBTSRecorder::addPID(int pid)
317 {
318         if (m_pids.find(pid) != m_pids.end())
319                 return -1;
320         
321         m_pids.insert(std::pair<int,int>(pid, -1));
322         if (m_running)
323                 startPID(pid);
324         return 0;
325 }
326
327 RESULT eDVBTSRecorder::removePID(int pid)
328 {
329         if (m_pids.find(pid) == m_pids.end())
330                 return -1;
331                 
332         if (m_running)
333                 stopPID(pid);
334         
335         m_pids.erase(pid);
336         return 0;
337 }
338
339 RESULT eDVBTSRecorder::setFormat(int format)
340 {
341         if (m_running)
342                 return -1;
343         m_format = format;
344         return 0;
345 }
346
347 RESULT eDVBTSRecorder::setTargetFD(int fd)
348 {
349         m_target_fd = fd;
350         return 0;
351 }
352
353 RESULT eDVBTSRecorder::setBoundary(off_t max)
354 {
355         return -1; // not yet implemented
356 }
357
358 RESULT eDVBTSRecorder::stop()
359 {
360         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
361                 stopPID(i->first);
362
363         if (!m_running)
364                 return -1;
365         m_thread->stop();
366         
367         close(m_source_fd);
368         
369         return 0;
370 }
371
372 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
373 {
374         conn = new eConnection(this, m_event.connect(event));
375         return 0;
376 }
377
378 RESULT eDVBTSRecorder::startPID(int pid)
379 {
380         char filename[128];
381 #if HAVE_DVB_API_VERSION < 3
382         snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
383 #else
384         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
385 #endif
386         int fd = ::open(filename, O_RDWR);
387         if (fd < 0)
388         {
389                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
390                 return -1;
391         }
392
393 #if HAVE_DVB_API_VERSION < 3
394         dmxPesFilterParams flt;
395         
396         flt.pesType = DMX_PES_OTHER;
397 #else
398         dmx_pes_filter_params flt;
399         
400         flt.pes_type = DMX_PES_OTHER;
401 #endif
402
403         flt.pid     = pid;
404         flt.input   = DMX_IN_FRONTEND;
405         flt.output  = DMX_OUT_TS_TAP;
406         
407         flt.flags   = DMX_IMMEDIATE_START;
408
409         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
410         if (res < 0)
411         {
412                 eDebug("set pes filter failed!");
413                 ::close(fd);
414                 return -1;
415         }
416         m_pids[pid] = fd;
417
418         return 0;
419 }
420
421 void eDVBTSRecorder::stopPID(int pid)
422 {
423         ::close(m_pids[pid]);
424         m_pids[pid] = -1;
425 }