- add getLength() call to iStaticServiceInformation
[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
10 #if HAVE_DVB_API_VERSION < 3
11 #include <ost/dmx.h>
12 #ifndef DMX_SET_NEGFILTER_MASK
13         #define DMX_SET_NEGFILTER_MASK   _IOW('o',48,uint8_t *)
14 #endif
15 #else
16 #include <linux/dvb/dmx.h>
17 #endif
18
19 #include "crc32.h"
20
21 #include <lib/base/eerror.h>
22 #include <lib/base/filepush.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                 {
78                         eDebug("crc32 failed! is %x\n", c);
79                         return;
80                 }
81         }
82         if (active)
83                 read(data);
84         else
85                 eDebug("data.. but not active");
86 }
87
88 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
89 {
90         char filename[128];
91 #if HAVE_DVB_API_VERSION < 3
92         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
93 #else
94         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
95 #endif
96         fd = ::open(filename, O_RDWR);
97         
98         eDebug("eDVBSectionReader has fd %d", fd);
99         
100         if (fd >= 0)
101         {
102                 notifier=new eSocketNotifier(context, fd, eSocketNotifier::Read);
103                 CONNECT(notifier->activated, eDVBSectionReader::data);
104                 res = 0;
105         } else
106         {
107                 perror(filename);
108                 res = errno;
109         }
110 }
111
112 DEFINE_REF(eDVBSectionReader)
113
114 eDVBSectionReader::~eDVBSectionReader()
115 {
116         if (notifier)
117                 delete notifier;
118         if (fd >= 0)
119                 ::close(fd);
120 }
121
122 RESULT eDVBSectionReader::start(const eDVBSectionFilterMask &mask)
123 {
124         RESULT res;
125         if (fd < 0)
126                 return -ENODEV;
127
128 #if HAVE_DVB_API_VERSION < 3
129         dmxSctFilterParams sct;
130 #else
131         dmx_sct_filter_params sct;
132 #endif
133         sct.pid     = mask.pid;
134         sct.timeout = 0;
135 #if HAVE_DVB_API_VERSION < 3
136         sct.flags   = 0;
137 #else
138         sct.flags   = DMX_IMMEDIATE_START;
139 #endif
140         if (mask.flags & eDVBSectionFilterMask::rfCRC)
141         {
142                 sct.flags |= DMX_CHECK_CRC;
143                 checkcrc = 1;
144         } else
145                 checkcrc = 0;
146         
147         memcpy(sct.filter.filter, mask.data, DMX_FILTER_SIZE);
148         memcpy(sct.filter.mask, mask.mask, DMX_FILTER_SIZE);
149 #if HAVE_DVB_API_VERSION >= 3
150         memcpy(sct.filter.mode, mask.mode, DMX_FILTER_SIZE);
151 #endif
152         
153         res = ::ioctl(fd, DMX_SET_FILTER, &sct);
154         if (!res)
155         {
156 #if HAVE_DVB_API_VERSION < 3
157                 res = ::ioctl(fd, DMX_SET_NEGFILTER_MASK, mask.mode);
158                 if (!res)
159                 {
160                         res = ::ioctl(fd, DMX_START, 0);
161                         if (!res)
162                                 active = 1;
163                 }
164 #else
165                 active = 1;
166 #endif
167         }
168         return res;
169 }
170
171 RESULT eDVBSectionReader::stop()
172 {
173         if (!active)
174                 return -1;
175
176         active=0;
177         ::ioctl(fd, DMX_STOP);
178         
179         return 0;
180 }
181
182 RESULT eDVBSectionReader::connectRead(const Slot1<void,const __u8*> &r, ePtr<eConnection> &conn)
183 {
184         conn = new eConnection(this, read.connect(r));
185         return 0;
186 }
187
188 DEFINE_REF(eDVBTSRecorder);
189
190 eDVBTSRecorder::eDVBTSRecorder(eDVBDemux *demux): m_demux(demux)
191 {
192         m_running = 0;
193         m_format = 0;
194         m_target_fd = -1;
195         m_thread = new eFilePushThread();
196         m_demux->m_dvr_busy = 1;
197 }
198
199 eDVBTSRecorder::~eDVBTSRecorder()
200 {
201         stop();
202         delete m_thread;
203         m_demux->m_dvr_busy = 0;
204 }
205
206 RESULT eDVBTSRecorder::start()
207 {
208         if (m_running)
209                 return -1;
210         
211         if (m_target_fd == -1)
212                 return -2;
213                 
214         char filename[128];
215 #if HAVE_DVB_API_VERSION < 3
216         snprintf(filename, 128, "/dev/dvb/card%d/dvr%d", m_demux->adapter, m_demux->demux);
217 #else
218         snprintf(filename, 128, "/dev/dvb/adapter%d/dvr%d", m_demux->adapter, m_demux->demux);
219 #endif
220         m_source_fd = ::open(filename, O_RDONLY);
221         
222         if (m_source_fd < 0)
223         {
224                 eDebug("FAILED to open dvr (%s) in ts recoder (%m)", filename);
225                 return -3;
226         }
227         
228         m_thread->start(m_source_fd, m_target_fd);
229         m_running = 1;
230         
231         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
232                 startPID(i->first);
233         
234         return 0;
235 }
236
237 RESULT eDVBTSRecorder::addPID(int pid)
238 {
239         if (m_pids.find(pid) != m_pids.end())
240                 return -1;
241         
242         m_pids.insert(std::pair<int,int>(pid, -1));
243         if (m_running)
244                 startPID(pid);
245         return 0;
246 }
247
248 RESULT eDVBTSRecorder::removePID(int pid)
249 {
250         if (m_pids.find(pid) == m_pids.end())
251                 return -1;
252                 
253         if (m_running)
254                 stopPID(pid);
255         
256         m_pids.erase(pid);
257         return 0;
258 }
259
260 RESULT eDVBTSRecorder::setFormat(int format)
261 {
262         if (m_running)
263                 return -1;
264         m_format = format;
265         return 0;
266 }
267
268 RESULT eDVBTSRecorder::setTargetFD(int fd)
269 {
270         m_target_fd = fd;
271         return 0;
272 }
273
274 RESULT eDVBTSRecorder::setBoundary(off_t max)
275 {
276         return -1; // not yet implemented
277 }
278
279 RESULT eDVBTSRecorder::stop()
280 {
281         for (std::map<int,int>::iterator i(m_pids.begin()); i != m_pids.end(); ++i)
282                 stopPID(i->first);
283
284         if (!m_running)
285                 return -1;
286         m_thread->stop();
287         
288         close(m_source_fd);
289         
290         return 0;
291 }
292
293 RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &conn)
294 {
295         conn = new eConnection(this, m_event.connect(event));
296         return 0;
297 }
298
299 RESULT eDVBTSRecorder::startPID(int pid)
300 {
301         char filename[128];
302 #if HAVE_DVB_API_VERSION < 3
303         snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
304 #else
305         snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
306 #endif
307         int fd = ::open(filename, O_RDWR);
308         if (fd < 0)
309         {
310                 eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
311                 return -1;
312         }
313
314 #if HAVE_DVB_API_VERSION < 3
315         dmxPesFilterParams flt;
316         
317         flt.pesType = DMX_PES_OTHER;
318 #else
319         dmx_pes_filter_params flt;
320         
321         flt.pes_type = DMX_PES_OTHER;
322 #endif
323
324         flt.pid     = pid;
325         flt.input   = DMX_IN_FRONTEND;
326         flt.output  = DMX_OUT_TS_TAP;
327         
328         flt.flags   = DMX_IMMEDIATE_START;
329
330         int res = ::ioctl(fd, DMX_SET_PES_FILTER, &flt);
331         if (res < 0)
332         {
333                 eDebug("set pes filter failed!");
334                 ::close(fd);
335                 return -1;
336         }
337         m_pids[pid] = fd;
338
339         return 0;
340 }
341
342 void eDVBTSRecorder::stopPID(int pid)
343 {
344         ::close(m_pids[pid]);
345         m_pids[pid] = -1;
346 }