small fix
[enigma2.git] / lib / dvb / decoder.cpp
1 #include <lib/base/ebase.h>
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/decoder.h>
4 #if HAVE_DVB_API_VERSION < 3 
5 #define audioStatus audio_status
6 #define videoStatus video_status
7 #define pesType pes_type
8 #define playState play_state
9 #define audioStreamSource_t audio_stream_source_t
10 #define videoStreamSource_t video_stream_source_t
11 #define streamSource stream_source
12 #define dmxPesFilterParams dmx_pes_filter_params
13 #define DMX_PES_VIDEO0 DMX_PES_VIDEO
14 #define DMX_PES_AUDIO0 DMX_PES_AUDIO
15 #define DMX_PES_VIDEO1 DMX_PES_VIDEO
16 #define DMX_PES_AUDIO1 DMX_PES_AUDIO
17 #include <ost/dmx.h>
18 #include <ost/video.h>
19 #include <ost/audio.h>
20 #else
21 #include <linux/dvb/audio.h>
22 #include <linux/dvb/video.h>
23 #include <linux/dvb/dmx.h>
24 #endif
25
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32
33         /* these are quite new... */
34 #ifndef AUDIO_GET_PTS
35 #define AUDIO_GET_PTS              _IOR('o', 19, __u64)
36 #define VIDEO_GET_PTS              _IOR('o', 57, __u64)
37 #endif
38
39 DEFINE_REF(eDVBAudio);
40
41 eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev)
42         :m_demux(demux), m_dev(dev), m_is_freezed(0)
43 {
44         char filename[128];
45 #if HAVE_DVB_API_VERSION < 3
46         sprintf(filename, "/dev/dvb/card%d/audio%d", demux->adapter, dev);
47 #else
48         sprintf(filename, "/dev/dvb/adapter%d/audio%d", demux->adapter, dev);
49 #endif
50         m_fd = ::open(filename, O_RDWR);
51         if (m_fd < 0)
52                 eWarning("%s: %m", filename);
53 #if HAVE_DVB_API_VERSION < 3
54         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
55 #else
56         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
57 #endif
58         m_fd_demux = ::open(filename, O_RDWR);
59         if (m_fd_demux < 0)
60                 eWarning("%s: %m", filename);
61 }
62
63 #if HAVE_DVB_API_VERSION < 3
64 int eDVBAudio::setPid(int pid, int type)
65 {
66         if ((m_fd < 0) || (m_fd_demux < 0))
67                 return -1;
68
69         int bypass = 0;
70
71         switch (type)
72         {
73         case aMPEG:
74                 bypass = 1;
75                 break;
76         case aAC3:
77                 bypass = 0;
78                 break;
79                 /*
80         case aDTS:
81                 bypass = 2;
82                 break;
83                 */
84         }
85
86         if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
87                 eDebug("failed (%m)");
88
89         dmx_pes_filter_params pes;
90
91         pes.pid      = pid;
92         pes.input    = DMX_IN_FRONTEND;
93         pes.output   = DMX_OUT_DECODER;
94         pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
95         pes.flags    = 0;
96         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - audio - ", pid);
97         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
98         {
99                 eDebug("failed (%m)");
100                 return -errno;
101         }
102         eDebug("ok");
103
104         return 0;
105 }
106
107 int eDVBAudio::startPid()
108 {
109         eDebugNoNewLine("DEMUX_START - audio - ");
110         if (::ioctl(m_fd_demux, DMX_START) < 0)
111         {
112                 eDebug("failed (%m)");
113                 return -errno;
114         }
115         eDebug("ok");
116         return 0;
117 }
118
119 int eDVBAudio::start()
120 {
121         eDebugNoNewLine("AUDIO_PLAY - ");
122         if (::ioctl(m_fd, AUDIO_PLAY) < 0)
123         {
124                 eDebug("failed (%m)");
125                 return -errno;
126         }
127         eDebug("ok");
128         return 0;
129 }
130
131 int eDVBAudio::stopPid()
132 {
133         eDebugNoNewLine("DEMUX_STOP - audio - ");
134         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
135         {
136                 eDebug("failed (%m)");
137                 return -errno;
138         }
139         eDebug("ok");
140         return 0;
141 }
142
143 int eDVBAudio::setAVSync(int val)
144 {
145         eDebugNoNewLine("AUDIO_SET_AV_SYNC - ");
146         if (::ioctl(m_fd, AUDIO_SET_AV_SYNC, val) < 0)
147         {
148                 eDebug("failed (%m)");
149                 return -errno;
150         }
151         eDebug("ok");
152         return 0;
153 }
154 #else
155 int eDVBAudio::startPid(int pid, int type)
156 {
157         if ((m_fd < 0) || (m_fd_demux < 0))
158                 return -1;
159         dmx_pes_filter_params pes;
160
161         pes.pid      = pid;
162         pes.input    = DMX_IN_FRONTEND;
163         pes.output   = DMX_OUT_DECODER;
164         pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
165         pes.flags    = 0;
166         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - audio - ", pid);
167         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
168         {
169                 eDebug("failed (%m)");
170                 return -errno;
171         }
172         eDebug("ok");
173         eDebugNoNewLine("DEMUX_START - audio - ");
174         if (::ioctl(m_fd_demux, DMX_START) < 0)
175         {
176                 eDebug("failed (%m)");
177                 return -errno;
178         }
179         eDebug("ok");
180         int bypass = 0;
181
182         switch (type)
183         {
184         case aMPEG:
185                 bypass = 1;
186                 break;
187         case aAC3:
188                 bypass = 0;
189                 break;
190                 /*
191         case aDTS:
192                 bypass = 2;
193                 break;
194                 */
195         }
196
197         eDebugNoNewLine("AUDIO_SET_BYPASS - ");
198         if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
199                 eDebug("failed (%m)");
200         else
201                 eDebug("ok");
202         freeze();
203
204         eDebugNoNewLine("AUDIO_PLAY - ");
205         if (::ioctl(m_fd, AUDIO_PLAY) < 0)
206                 eDebug("failed (%m)");
207         else
208                 eDebug("ok");
209         return 0;
210 }
211 #endif
212
213 void eDVBAudio::stop()
214 {
215 #if HAVE_DVB_API_VERSION > 2
216         flush();
217 #endif
218         eDebugNoNewLine("AUDIO_STOP - ");
219         if (::ioctl(m_fd, AUDIO_STOP) < 0)
220                 eDebug("failed (%m)");
221         else
222                 eDebug("ok");
223 #if HAVE_DVB_API_VERSION > 2
224         eDebugNoNewLine("DEMUX_STOP - audio - ");
225         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
226                 eDebug("failed (%m)");
227         else
228                 eDebug("ok");
229 #endif
230 }
231
232 void eDVBAudio::flush()
233 {
234         eDebugNoNewLine("AUDIO_CLEAR_BUFFER - ");
235         if (::ioctl(m_fd, AUDIO_CLEAR_BUFFER) < 0)
236                 eDebug("failed (%m)");
237         else
238                 eDebug("ok");
239 }
240
241 void eDVBAudio::freeze()
242 {
243         if (!m_is_freezed)
244         {
245                 eDebugNoNewLine("AUDIO_PAUSE - ");
246                 if (::ioctl(m_fd, AUDIO_PAUSE) < 0)
247                         eDebug("failed (%m)");
248                 else
249                         eDebug("ok");
250                 m_is_freezed=1;
251         }
252 }
253
254 void eDVBAudio::unfreeze()
255 {
256         if (m_is_freezed)
257         {
258                 eDebugNoNewLine("AUDIO_CONTINUE - ");
259                 if (::ioctl(m_fd, AUDIO_CONTINUE) < 0)
260                         eDebug("failed (%m)");
261                 else
262                         eDebug("ok");
263                 m_is_freezed=0;
264         }
265 }
266
267 void eDVBAudio::setChannel(int channel)
268 {
269         int val = AUDIO_STEREO;
270         switch (channel)
271         {
272         case aMonoLeft: val = AUDIO_MONO_LEFT; break;
273         case aMonoRight: val = AUDIO_MONO_RIGHT; break;
274         default: break;
275         }
276         eDebugNoNewLine("AUDIO_CHANNEL_SELECT(%d) - ", val);
277         if (::ioctl(m_fd, AUDIO_CHANNEL_SELECT, val) < 0)
278                 eDebug("failed (%m)");
279         else
280                 eDebug("ok");
281 }
282
283 int eDVBAudio::getPTS(pts_t &now)
284 {
285         if (::ioctl(m_fd, AUDIO_GET_PTS, &now) < 0)
286                 eDebug("AUDIO_GET_PTS failed (%m)");
287         return 0;
288 }
289
290 eDVBAudio::~eDVBAudio()
291 {
292         unfreeze();
293         if (m_fd >= 0)
294                 ::close(m_fd);
295         if (m_fd_demux >= 0)
296                 ::close(m_fd_demux);
297 }
298
299 DEFINE_REF(eDVBVideo);
300
301 eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev)
302         :m_demux(demux), m_dev(dev), m_is_slow_motion(0), m_is_fast_forward(0), m_is_freezed(0)
303 {
304         char filename[128];
305 #if HAVE_DVB_API_VERSION < 3
306         sprintf(filename, "/dev/dvb/card%d/video%d", demux->adapter, dev);
307         m_fd_video = ::open("/dev/video", O_RDWR);
308         if (m_fd_video < 0)
309                 eWarning("/dev/video: %m");
310 #else
311         sprintf(filename, "/dev/dvb/adapter%d/video%d", demux->adapter, dev);
312 #endif
313         m_fd = ::open(filename, O_RDWR);
314         if (m_fd < 0)
315                 eWarning("%s: %m", filename);
316         else
317         {
318                 m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Priority);
319                 CONNECT(m_sn->activated, eDVBVideo::video_event);
320         }
321         eDebug("Video Device: %s", filename);
322 #if HAVE_DVB_API_VERSION < 3
323         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
324 #else
325         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
326 #endif
327         m_fd_demux = ::open(filename, O_RDWR);
328         if (m_fd_demux < 0)
329                 eWarning("%s: %m", filename);
330         eDebug("demux device: %s", filename);
331 }
332
333 // not finally values i think.. !!
334 #define VIDEO_STREAMTYPE_MPEG2 0
335 #define VIDEO_STREAMTYPE_MPEG4_H264 1
336
337 #if HAVE_DVB_API_VERSION < 3
338 int eDVBVideo::setPid(int pid)
339 {
340         if ((m_fd < 0) || (m_fd_demux < 0))
341                 return -1;
342         dmx_pes_filter_params pes;
343
344         pes.pid      = pid;
345         pes.input    = DMX_IN_FRONTEND;
346         pes.output   = DMX_OUT_DECODER;
347         pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
348         pes.flags    = 0;
349         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - video - ", pid);
350         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
351         {
352                 eDebug("failed (%m)");
353                 return -errno;
354         }
355         eDebug("ok");
356         return 0;
357 }
358
359 int eDVBVideo::startPid()
360 {
361         eDebugNoNewLine("DEMUX_START - video - ");
362         if (::ioctl(m_fd_demux, DMX_START) < 0)
363         {
364                 eDebug("failed (%m)");
365                 return -errno;
366         }
367         eDebug("ok");
368         return 0;
369 }
370
371 int eDVBVideo::start()
372 {
373         eDebugNoNewLine("VIDEO_PLAY - ");
374         if (::ioctl(m_fd, VIDEO_PLAY) < 0)
375         {
376                 eDebug("failed (%m)");
377                 return -errno;
378         }
379         eDebug("ok");
380         return 0;
381 }
382
383 int eDVBVideo::stopPid()
384 {
385         eDebugNoNewLine("DEMUX_STOP - video - ");
386         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
387         {
388                 eDebug("failed (%m)");
389                 return -errno;
390         }
391         eDebug("ok");
392         return 0;
393 }
394 #else
395 int eDVBVideo::startPid(int pid, int type)
396 {
397         if ((m_fd < 0) || (m_fd_demux < 0))
398                 return -1;
399         dmx_pes_filter_params pes;
400
401         eDebugNoNewLine("VIDEO_SET_STREAMTYPE %d - ",type == MPEG4_H264 ? VIDEO_STREAMTYPE_MPEG4_H264 : VIDEO_STREAMTYPE_MPEG2);
402         if (::ioctl(m_fd, VIDEO_SET_STREAMTYPE,
403                 type == MPEG4_H264 ? VIDEO_STREAMTYPE_MPEG4_H264 : VIDEO_STREAMTYPE_MPEG2) < 0)
404                 eDebug("failed (%m)");
405         else
406                 eDebug("ok");
407
408         pes.pid      = pid;
409         pes.input    = DMX_IN_FRONTEND;
410         pes.output   = DMX_OUT_DECODER;
411         pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
412         pes.flags    = 0;
413         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - video - ", pid);
414         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
415         {
416                 eDebug("failed (%m)");
417                 return -errno;
418         }
419         eDebug("ok");
420         eDebugNoNewLine("DEMUX_START - video - ");
421         if (::ioctl(m_fd_demux, DMX_START) < 0)
422         {
423                 eDebug("failed (%m)");
424                 return -errno;
425         }
426         eDebug("ok");
427         freeze();
428         eDebugNoNewLine("VIDEO_PLAY - ");
429         if (::ioctl(m_fd, VIDEO_PLAY) < 0)
430                 eDebug("failed (%m)");
431         else
432                 eDebug("ok");
433         return 0;
434 }
435 #endif
436
437 void eDVBVideo::stop()
438 {
439 #if HAVE_DVB_API_VERSION > 2
440         eDebugNoNewLine("DEMUX_STOP - video - ");
441         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
442                 eDebug("failed (%m)");
443         else
444                 eDebug("ok");
445 #endif
446         eDebugNoNewLine("VIDEO_STOP - ");
447         if (::ioctl(m_fd, VIDEO_STOP, 1) < 0)
448                 eDebug("failed (%m)");
449         else
450                 eDebug("ok");
451 }
452
453 void eDVBVideo::flush()
454 {
455         eDebugNoNewLine("VIDEO_CLEAR_BUFFER - ");
456         if (::ioctl(m_fd, VIDEO_CLEAR_BUFFER) < 0)
457                 eDebug("failed (%m)");
458         else
459                 eDebug("ok");
460 }
461
462 void eDVBVideo::freeze()
463 {
464         if (!m_is_freezed)
465         {
466                 eDebugNoNewLine("VIDEO_FREEZE - ");
467                 if (::ioctl(m_fd, VIDEO_FREEZE) < 0)
468                         eDebug("failed (%m)");
469                 else
470                         eDebug("ok");
471                 m_is_freezed=1;
472         }
473 }
474
475 void eDVBVideo::unfreeze()
476 {
477         if (m_is_freezed)
478         {
479                 eDebugNoNewLine("VIDEO_CONTINUE - ");
480                 if (::ioctl(m_fd, VIDEO_CONTINUE) < 0)
481                         eDebug("failed (%m)");
482                 else
483                         eDebug("ok");
484                 m_is_freezed=0;
485         }
486 }
487
488 int eDVBVideo::setSlowMotion(int repeat)
489 {
490         eDebugNoNewLine("VIDEO_SLOWMOTION - ");
491         m_is_slow_motion = repeat;
492         int ret = ::ioctl(m_fd, VIDEO_SLOWMOTION, repeat);
493         if (ret < 0)
494                 eDebug("failed(%m)");
495         else
496                 eDebug("ok");
497         return ret;
498 }
499
500 int eDVBVideo::setFastForward(int skip)
501 {
502         eDebugNoNewLine("VIDEO_FAST_FORWARD - ");
503         m_is_fast_forward = skip;
504         int ret = ::ioctl(m_fd, VIDEO_FAST_FORWARD, skip);
505         if (ret < 0)
506                 eDebug("failed(%m)");
507         else
508                 eDebug("ok");
509         return ret;
510 }
511
512 int eDVBVideo::getPTS(pts_t &now)
513 {
514 #if HAVE_DVB_API_VERSION < 3
515         #define VIDEO_GET_PTS_OLD           _IOR('o', 1, unsigned int*)
516         unsigned int pts;
517         int ret = ::ioctl(m_fd_video, VIDEO_GET_PTS_OLD, &pts);
518         now = pts;
519         now *= 2;
520 #else
521         int ret = ::ioctl(m_fd, VIDEO_GET_PTS, &now);
522 #endif
523         if (ret < 0)
524                 eDebug("VIDEO_GET_PTS failed(%m)");
525         return ret;
526 }
527
528 eDVBVideo::~eDVBVideo()
529 {
530         if (m_sn)
531                 delete m_sn;
532         if (m_is_slow_motion)
533                 setSlowMotion(0);
534         if (m_is_fast_forward)
535                 setFastForward(0);
536         unfreeze();
537         if (m_fd >= 0)
538                 ::close(m_fd);
539         if (m_fd_demux >= 0)
540                 ::close(m_fd_demux);
541 #if HAVE_DVB_API_VERSION < 3
542         if (m_fd_video >= 0)
543                 ::close(m_fd_video);
544 #endif
545 }
546
547 void eDVBVideo::video_event(int)
548 {
549 #if HAVE_DVB_API_VERSION >= 3
550         struct video_event evt;
551         eDebugNoNewLine("VIDEO_GET_EVENT - ");
552         if (::ioctl(m_fd, VIDEO_GET_EVENT, &evt) < 0)
553                 eDebug("failed (%m)");
554         else
555         {
556                 eDebug("ok");
557                 if (evt.type == VIDEO_EVENT_SIZE_CHANGED)
558                 {
559                         struct iTSMPEGDecoder::videoEvent event;
560                         event.type = iTSMPEGDecoder::videoEvent::eventSizeChanged;
561                         event.aspect = evt.u.size.aspect_ratio;
562                         event.height = evt.u.size.h;
563                         event.width = evt.u.size.w;
564                         /* emit */ m_event(event);
565                 }
566                 else if (evt.type == VIDEO_EVENT_FRAME_RATE_CHANGED)
567                 {
568                         struct iTSMPEGDecoder::videoEvent event;
569                         event.type = iTSMPEGDecoder::videoEvent::eventFrameRateChanged;
570                         event.framerate = evt.u.frame_rate;
571                         /* emit */ m_event(event);
572                 }
573                 else if (evt.type == 16 /*VIDEO_EVENT_PROGRESSIVE_CHANGED*/)
574                 {
575                         struct iTSMPEGDecoder::videoEvent event;
576                         event.type = iTSMPEGDecoder::videoEvent::eventProgressiveChanged;
577                         event.progressive = evt.u.frame_rate;
578                         /* emit */ m_event(event);
579                 }
580                 else
581                         eDebug("unhandled DVBAPI Video Event %d", evt.type);
582         }
583 #else
584 #warning "FIXMEE!! Video Events not implemented for old api"
585 #endif
586 }
587
588 RESULT eDVBVideo::connectEvent(const Slot1<void, struct iTSMPEGDecoder::videoEvent> &event, ePtr<eConnection> &conn)
589 {
590         conn = new eConnection(this, m_event.connect(event));
591         return 0;
592 }
593
594 DEFINE_REF(eDVBPCR);
595
596 eDVBPCR::eDVBPCR(eDVBDemux *demux): m_demux(demux)
597 {
598         char filename[128];
599 #if HAVE_DVB_API_VERSION < 3
600         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
601 #else
602         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
603 #endif
604         m_fd_demux = ::open(filename, O_RDWR);
605         if (m_fd_demux < 0)
606                 eWarning("%s: %m", filename);
607 }
608
609 #if HAVE_DVB_API_VERSION < 3
610 int eDVBPCR::setPid(int pid)
611 {
612         if (m_fd_demux < 0)
613                 return -1;
614         dmx_pes_filter_params pes;
615
616         pes.pid      = pid;
617         pes.input    = DMX_IN_FRONTEND;
618         pes.output   = DMX_OUT_DECODER;
619         pes.pes_type = DMX_PES_PCR;
620         pes.flags    = 0;
621
622         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
623         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
624         {
625                 eDebug("failed (%m)");
626                 return -errno;
627         }
628         eDebug("ok");
629         return 0;
630 }
631
632 int eDVBPCR::startPid()
633 {
634         if (m_fd_demux < 0)
635                 return -1;
636         eDebugNoNewLine("DEMUX_START - pcr - ");
637         if (::ioctl(m_fd_demux, DMX_START) < 0)
638         {
639                 eDebug("failed (%m)");
640                 return -errno;
641         }
642         eDebug("ok");
643         return 0;
644 }
645 #else
646 int eDVBPCR::startPid(int pid)
647 {
648         if (m_fd_demux < 0)
649                 return -1;
650         dmx_pes_filter_params pes;
651
652         pes.pid      = pid;
653         pes.input    = DMX_IN_FRONTEND;
654         pes.output   = DMX_OUT_DECODER;
655         pes.pes_type = DMX_PES_PCR;
656         pes.flags    = 0;
657         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
658         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
659         {
660                 eDebug("failed (%m)");
661                 return -errno;
662         }
663         eDebug("ok");
664         eDebugNoNewLine("DEMUX_START - pcr - ");
665         if (::ioctl(m_fd_demux, DMX_START) < 0)
666         {
667                 eDebug("failed (%m)");
668                 return -errno;
669         }
670         eDebug("ok");
671         return 0;
672 }
673 #endif
674
675 void eDVBPCR::stop()
676 {
677         eDebugNoNewLine("DEMUX_STOP - pcr - ");
678         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
679                 eDebug("failed(%m)");
680         else
681                 eDebug("ok");
682 }
683
684 eDVBPCR::~eDVBPCR()
685 {
686         if (m_fd_demux >= 0)
687                 ::close(m_fd_demux);
688 }
689
690 DEFINE_REF(eDVBTText);
691
692 eDVBTText::eDVBTText(eDVBDemux *demux): m_demux(demux)
693 {
694         char filename[128];
695 #if HAVE_DVB_API_VERSION < 3
696         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
697 #else
698         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
699 #endif
700         m_fd_demux = ::open(filename, O_RDWR);
701         if (m_fd_demux < 0)
702                 eWarning("%s: %m", filename);
703 }
704
705 int eDVBTText::startPid(int pid)
706 {
707         if (m_fd_demux < 0)
708                 return -1;
709         dmx_pes_filter_params pes;
710
711         pes.pid      = pid;
712         pes.input    = DMX_IN_FRONTEND;
713         pes.output   = DMX_OUT_DECODER;
714         pes.pes_type = DMX_PES_TELETEXT;
715         pes.flags    = 0;
716
717         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - ttx - ", pid);
718         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
719         {
720                 eDebug("failed(%m)");
721                 return -errno;
722         }
723         eDebug("ok");
724         eDebugNoNewLine("DEMUX_START - pcr - ");
725         if (::ioctl(m_fd_demux, DMX_START) < 0)
726         {
727                 eDebug("failed(%m)");
728                 return -errno;
729         }
730         eDebug("ok");
731         return 0;
732 }
733
734 void eDVBTText::stop()
735 {
736         eDebugNoNewLine("DEMUX_STOP - ttx - ");
737         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
738                 eDebug("failed(%m)");
739         else
740                 eDebug("ok");
741 }
742
743 eDVBTText::~eDVBTText()
744 {
745         if (m_fd_demux >= 0)
746                 ::close(m_fd_demux);
747 }
748
749 DEFINE_REF(eTSMPEGDecoder);
750
751 int eTSMPEGDecoder::setState()
752 {
753         int res = 0;
754
755         int noaudio = m_is_sm || m_is_ff || m_is_trickmode;
756         int nott = noaudio; /* actually same conditions */
757
758         if ((noaudio && m_audio) || (!m_audio && !noaudio))
759                 m_changed |= changeAudio;
760
761         if ((nott && m_text) || (!m_text && !nott))
762                 m_changed |= changeText;
763
764         bool changed = !!m_changed;
765 #if HAVE_DVB_API_VERSION < 3
766         bool checkAVSync = m_changed & (changeAudio|changeVideo|changePCR);
767         if (m_changed & changeAudio && m_audio)
768                 m_audio->stopPid();
769         if (m_changed & changeVideo && m_video)
770                 m_video->stopPid();
771         if (m_changed & changePCR && m_pcr)
772         {
773                 m_pcr->stop();
774                 m_pcr=0;
775                 if (!(m_pcrpid >= 0 && m_pcrpid < 0x1ff))
776                         m_changed &= ~changePCR;
777         }
778         if (m_changed & changeAudio && m_audio)
779         {
780                 m_audio->stop();
781                 m_audio=0;
782                 if (!(m_apid >= 0 && m_apid < 0x1ff))
783                         m_changed &= ~changeAudio;
784         }
785         if (m_changed & changeVideo && m_video)
786         {
787                 m_video->stop();
788                 m_video=0;
789                 m_video_event_conn=0;
790                 if (!(m_vpid >= 0 && m_vpid < 0x1ff))
791                         m_changed &= ~changeVideo;
792         }
793         if (m_changed & changeVideo)
794         {
795                 m_video = new eDVBVideo(m_demux, m_decoder);
796                 m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
797                 if (m_video->setPid(m_vpid))
798                         res -1;
799         }
800         if (m_changed & changePCR)
801         {
802                 m_pcr = new eDVBPCR(m_demux);
803                 if (m_pcr->setPid(m_pcrpid))
804                         res = -1;
805         }
806         if (m_changed & changeAudio)
807         {
808                 m_audio = new eDVBAudio(m_demux, m_decoder);
809                 if (m_audio->setPid(m_apid, m_atype))
810                         res = -1;
811         }
812         if (m_changed & changePCR)
813         {
814                 if (m_pcr->startPid())
815                         res = -1;
816                 m_changed &= ~changePCR;
817         }
818         else if (checkAVSync && m_audio && m_video)
819         {
820                 if (m_audio->setAVSync(1))
821                         res = -1;
822         }
823         if (m_changed & changeVideo)
824         {
825                 if (m_video->startPid() || m_video->start())
826                         res = -1;
827                 m_changed &= ~changeVideo;
828         }
829         if (m_changed & changeAudio)
830         {
831                 if (m_audio->start() || m_audio->startPid())
832                         res = -1;
833                 m_changed &= ~changeAudio;
834         }
835 #else
836         if (m_changed & changePCR)
837         {
838                 if (m_pcr)
839                         m_pcr->stop();
840                 m_pcr = 0;
841                 if ((m_pcrpid >= 0) && (m_pcrpid < 0x1FFF))
842                 {
843                         m_pcr = new eDVBPCR(m_demux);
844                         if (m_pcr->startPid(m_pcrpid))
845                                 res = -1;
846                 }
847                 m_changed &= ~changePCR;
848         }
849         if (m_changed & changeVideo)
850         {
851                 eDebug("VIDEO CHANGED (to %04x)", m_vpid);
852                 if (m_video)
853                 {
854                         m_video->stop();
855                         m_video = 0;
856                         m_video_event_conn = 0;
857                 }
858                 if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
859                 {
860                         m_video = new eDVBVideo(m_demux, m_decoder);
861                         m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
862                         if (m_video->startPid(m_vpid, m_vtype))
863                                 res = -1;
864                 }
865                 m_changed &= ~changeVideo;
866         }
867         if (m_changed & changeAudio)
868         {
869                 if (m_audio)
870                         m_audio->stop();
871                 m_audio = 0;
872                 if ((m_apid >= 0) && (m_apid < 0x1FFF) && !noaudio)
873                 {
874                         m_audio = new eDVBAudio(m_demux, m_decoder);
875                         if (m_audio->startPid(m_apid, m_atype))
876                                 res = -1;
877                 }
878                 m_changed &= ~changeAudio;
879         }
880         if (m_changed & changeText)
881         {
882                 if (m_text)
883                         m_text->stop();
884                 m_text = 0;
885                 if ((m_textpid >= 0) && (m_textpid < 0x1FFF) && !nott)
886                 {
887                         m_text = new eDVBTText(m_demux);
888                         if (m_text->startPid(m_textpid))
889                                 res = -1;
890                 }
891                 m_changed &= ~changeText;
892         }
893 #endif
894         if (changed && !m_video && m_audio && m_radio_pic.length())
895                 showSinglePic(m_radio_pic.c_str());
896
897         return res;
898 }
899
900 int eTSMPEGDecoder::m_pcm_delay=-1,
901         eTSMPEGDecoder::m_ac3_delay=-1;
902
903 RESULT eTSMPEGDecoder::setPCMDelay(int delay)
904 {
905         if (m_decoder == 0 && delay != m_pcm_delay )
906         {
907                 FILE *fp = fopen("/proc/stb/audio/audio_delay_pcm", "w");
908                 if (fp)
909                 {
910                         fprintf(fp, "%x", delay*90);
911                         fclose(fp);
912                         m_pcm_delay = delay;
913                         return 0;
914                 }
915         }
916         return -1;
917 }
918
919 RESULT eTSMPEGDecoder::setAC3Delay(int delay)
920 {
921         if ( m_decoder == 0 && delay != m_ac3_delay )
922         {
923                 FILE *fp = fopen("/proc/stb/audio/audio_delay_bitstream", "w");
924                 if (fp)
925                 {
926                         fprintf(fp, "%x", delay*90);
927                         fclose(fp);
928                         m_ac3_delay = delay;
929                         return 0;
930                 }
931         }
932         return -1;
933 }
934
935 eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder)
936         :m_demux(demux), m_changed(0), m_decoder(decoder), m_video_clip_fd(-1), m_showSinglePicTimer(eApp)
937 {
938         demux->connectEvent(slot(*this, &eTSMPEGDecoder::demux_event), m_demux_event_conn);
939         CONNECT(m_showSinglePicTimer.timeout, eTSMPEGDecoder::finishShowSinglePic);
940         m_is_ff = m_is_sm = m_is_trickmode = 0;
941 }
942
943 eTSMPEGDecoder::~eTSMPEGDecoder()
944 {
945         finishShowSinglePic();
946         m_vpid = m_apid = m_pcrpid = m_textpid = pidNone;
947         m_changed = -1;
948         setState();
949 }
950
951 RESULT eTSMPEGDecoder::setVideoPID(int vpid, int type)
952 {
953         if (m_vpid != vpid)
954         {
955                 m_changed |= changeVideo;
956                 m_vpid = vpid;
957                 m_vtype = type;
958         }
959         return 0;
960 }
961
962 RESULT eTSMPEGDecoder::setAudioPID(int apid, int type)
963 {
964         if ((m_apid != apid) || (m_atype != type))
965         {
966                 m_changed |= changeAudio;
967                 m_atype = type;
968                 m_apid = apid;
969         }
970         return 0;
971 }
972
973 int eTSMPEGDecoder::m_audio_channel = -1;
974
975 RESULT eTSMPEGDecoder::setAudioChannel(int channel)
976 {
977         if (channel == -1)
978                 channel = ac_stereo;
979         if (m_decoder == 0 && m_audio_channel != channel)
980         {
981                 if (m_audio)
982                 {
983                         m_audio->setChannel(channel);
984                         m_audio_channel=channel;
985                 }
986                 else
987                         eDebug("eTSMPEGDecoder::setAudioChannel but no audio decoder exist");
988         }
989         return 0;
990 }
991
992 int eTSMPEGDecoder::getAudioChannel()
993 {
994         return m_audio_channel == -1 ? ac_stereo : m_audio_channel;
995 }
996
997 RESULT eTSMPEGDecoder::setSyncPCR(int pcrpid)
998 {
999         if (m_pcrpid != pcrpid)
1000         {
1001                 m_changed |= changePCR;
1002                 m_pcrpid = pcrpid;
1003         }
1004         return 0;
1005 }
1006
1007 RESULT eTSMPEGDecoder::setTextPID(int textpid)
1008 {
1009         if (m_textpid != textpid)
1010         {
1011                 m_changed |= changeText;
1012                 m_textpid = textpid;
1013         }
1014         return 0;
1015 }
1016
1017 RESULT eTSMPEGDecoder::setSyncMaster(int who)
1018 {
1019         return -1;
1020 }
1021
1022 RESULT eTSMPEGDecoder::start()
1023 {
1024         RESULT r;
1025         r = setState();
1026         if (r)
1027                 return r;
1028         return unfreeze();
1029 }
1030
1031         /* preroll is start in freezed mode. */
1032 RESULT eTSMPEGDecoder::preroll()
1033 {
1034         return setState();
1035 }
1036
1037 RESULT eTSMPEGDecoder::freeze(int cont)
1038 {
1039         if (m_video)
1040                 m_video->freeze();
1041
1042         if (m_audio)
1043                 m_audio->freeze();
1044
1045         return 0;
1046 }
1047
1048 RESULT eTSMPEGDecoder::unfreeze()
1049 {
1050         if (m_video)
1051                 m_video->unfreeze();
1052
1053         if (m_audio)
1054                 m_audio->unfreeze();
1055
1056         return 0;
1057 }
1058
1059 RESULT eTSMPEGDecoder::setSinglePictureMode(int when)
1060 {
1061         return -1;
1062 }
1063
1064 RESULT eTSMPEGDecoder::setPictureSkipMode(int what)
1065 {
1066         return -1;
1067 }
1068
1069 RESULT eTSMPEGDecoder::setFastForward(int frames_to_skip)
1070 {
1071         m_is_ff = frames_to_skip != 0;
1072
1073         setState();
1074
1075         if (m_video)
1076                 return m_video->setFastForward(frames_to_skip);
1077         else
1078                 return -1;
1079 }
1080
1081 RESULT eTSMPEGDecoder::setSlowMotion(int repeat)
1082 {
1083         m_is_sm = repeat != 0;
1084
1085         setState();
1086
1087         if (m_video)
1088                 return m_video->setSlowMotion(repeat);
1089         else
1090                 return -1;
1091 }
1092
1093 RESULT eTSMPEGDecoder::setZoom(int what)
1094 {
1095         return -1;
1096 }
1097
1098 RESULT eTSMPEGDecoder::flush()
1099 {
1100         if (m_audio)
1101                 m_audio->flush();
1102         if (m_video)
1103                 m_video->flush();
1104         return 0;
1105 }
1106
1107 void eTSMPEGDecoder::demux_event(int event)
1108 {
1109         switch (event)
1110         {
1111         case eDVBDemux::evtFlush:
1112                 flush();
1113                 break;
1114         default:
1115                 break;
1116         }
1117 }
1118
1119 RESULT eTSMPEGDecoder::setTrickmode(int what)
1120 {
1121         m_is_trickmode = what;
1122         setState();
1123         return 0;
1124 }
1125
1126 RESULT eTSMPEGDecoder::getPTS(int what, pts_t &pts)
1127 {
1128         if (what == 0) /* auto */
1129                 what = m_video ? 1 : 2;
1130
1131         if (what == 1) /* video */
1132         {
1133                 if (m_video)
1134                         return m_video->getPTS(pts);
1135                 else
1136                         return -1;
1137         }
1138
1139         if (what == 2) /* audio */
1140         {
1141                 if (m_audio)
1142                         return m_audio->getPTS(pts);
1143                 else
1144                         return -1;
1145         }
1146
1147         return -1;
1148 }
1149
1150 RESULT eTSMPEGDecoder::setRadioPic(const std::string &filename)
1151 {
1152         m_radio_pic = filename;
1153         return 0;
1154 }
1155
1156 RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
1157 {
1158         if (m_decoder == 0)
1159         {
1160                 eDebug("showSinglePic %s", filename);
1161                 int f = open(filename, O_RDONLY);
1162                 if (f >= 0)
1163                 {
1164                         struct stat s;
1165                         fstat(f, &s);
1166                         if (m_video_clip_fd == -1)
1167                                 m_video_clip_fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
1168                         if (m_video_clip_fd >= 0)
1169                         {
1170                                 bool seq_end_avail = false;
1171                                 size_t pos=0;
1172                                 unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1173                                 unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
1174                                 unsigned char iframe[s.st_size];
1175                                 unsigned char stuffing[8192];
1176                                 memset(stuffing, 0, 8192);
1177                                 read(f, iframe, s.st_size);
1178                                 if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
1179                                         eDebug("VIDEO_SELECT_SOURCE MEMORY failed (%m)");
1180                                 if (ioctl(m_video_clip_fd, VIDEO_PLAY) < 0)
1181                                         eDebug("VIDEO_PLAY failed (%m)");
1182                                 if (::ioctl(m_video_clip_fd, VIDEO_CONTINUE) < 0)
1183                                         eDebug("video: VIDEO_CONTINUE: %m");
1184                                 if (::ioctl(m_video_clip_fd, VIDEO_CLEAR_BUFFER) < 0)
1185                                         eDebug("video: VIDEO_CLEAR_BUFFER: %m");
1186                                 while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
1187                                         ++pos;
1188                                 if ((iframe[3] >> 4) != 0xE) // no pes header
1189                                         write(m_video_clip_fd, pes_header, sizeof(pes_header));
1190                                 else
1191                                         iframe[4] = iframe[5] = 0x00;
1192                                 write(m_video_clip_fd, iframe, s.st_size);
1193                                 if (!seq_end_avail)
1194                                         write(m_video_clip_fd, seq_end, sizeof(seq_end));
1195                                 write(m_video_clip_fd, stuffing, 8192);
1196                                 m_showSinglePicTimer.start(150, true);
1197                         }
1198                         close(f);
1199                 }
1200                 else
1201                 {
1202                         eDebug("couldnt open %s", filename);
1203                         return -1;
1204                 }
1205         }
1206         else
1207         {
1208                 eDebug("only show single pics on first decoder");
1209                 return -1;
1210         }
1211         return 0;
1212 }
1213
1214 void eTSMPEGDecoder::finishShowSinglePic()
1215 {
1216         if (m_video_clip_fd >= 0)
1217         {
1218                 if (ioctl(m_video_clip_fd, VIDEO_STOP, 0) < 0)
1219                         eDebug("VIDEO_STOP failed (%m)");
1220                 if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX) < 0)
1221                                 eDebug("VIDEO_SELECT_SOURCE DEMUX failed (%m)");
1222                 close(m_video_clip_fd);
1223                 m_video_clip_fd = -1;
1224         }
1225 }
1226
1227 RESULT eTSMPEGDecoder::connectVideoEvent(const Slot1<void, struct videoEvent> &event, ePtr<eConnection> &conn)
1228 {
1229         conn = new eConnection(this, m_video_event.connect(event));
1230         return 0;
1231 }
1232
1233 void eTSMPEGDecoder::video_event(struct videoEvent event)
1234 {
1235         /* emit */ m_video_event(event);
1236 }