proper showSinglePic error handling (thanks to Sat-Turner)
[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
567                         eDebug("unhandled DVBAPI Video Event %d", evt.type);
568         }
569 #else
570 #warning "FIXMEE!! Video Events not implemented for old api"
571 #endif
572 }
573
574 RESULT eDVBVideo::connectEvent(const Slot1<void, struct iTSMPEGDecoder::videoEvent> &event, ePtr<eConnection> &conn)
575 {
576         conn = new eConnection(this, m_event.connect(event));
577         return 0;
578 }
579
580 DEFINE_REF(eDVBPCR);
581
582 eDVBPCR::eDVBPCR(eDVBDemux *demux): m_demux(demux)
583 {
584         char filename[128];
585 #if HAVE_DVB_API_VERSION < 3
586         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
587 #else
588         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
589 #endif
590         m_fd_demux = ::open(filename, O_RDWR);
591         if (m_fd_demux < 0)
592                 eWarning("%s: %m", filename);
593 }
594
595 #if HAVE_DVB_API_VERSION < 3
596 int eDVBPCR::setPid(int pid)
597 {
598         if (m_fd_demux < 0)
599                 return -1;
600         dmx_pes_filter_params pes;
601
602         pes.pid      = pid;
603         pes.input    = DMX_IN_FRONTEND;
604         pes.output   = DMX_OUT_DECODER;
605         pes.pes_type = DMX_PES_PCR;
606         pes.flags    = 0;
607
608         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
609         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
610         {
611                 eDebug("failed (%m)");
612                 return -errno;
613         }
614         eDebug("ok");
615         return 0;
616 }
617
618 int eDVBPCR::startPid()
619 {
620         if (m_fd_demux < 0)
621                 return -1;
622         eDebugNoNewLine("DEMUX_START - pcr - ");
623         if (::ioctl(m_fd_demux, DMX_START) < 0)
624         {
625                 eDebug("failed (%m)");
626                 return -errno;
627         }
628         eDebug("ok");
629         return 0;
630 }
631 #else
632 int eDVBPCR::startPid(int pid)
633 {
634         if (m_fd_demux < 0)
635                 return -1;
636         dmx_pes_filter_params pes;
637
638         pes.pid      = pid;
639         pes.input    = DMX_IN_FRONTEND;
640         pes.output   = DMX_OUT_DECODER;
641         pes.pes_type = DMX_PES_PCR;
642         pes.flags    = 0;
643         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
644         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
645         {
646                 eDebug("failed (%m)");
647                 return -errno;
648         }
649         eDebug("ok");
650         eDebugNoNewLine("DEMUX_START - pcr - ");
651         if (::ioctl(m_fd_demux, DMX_START) < 0)
652         {
653                 eDebug("failed (%m)");
654                 return -errno;
655         }
656         eDebug("ok");
657         return 0;
658 }
659 #endif
660
661 void eDVBPCR::stop()
662 {
663         eDebugNoNewLine("DEMUX_STOP - pcr - ");
664         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
665                 eDebug("failed(%m)");
666         else
667                 eDebug("ok");
668 }
669
670 eDVBPCR::~eDVBPCR()
671 {
672         if (m_fd_demux >= 0)
673                 ::close(m_fd_demux);
674 }
675
676 DEFINE_REF(eDVBTText);
677
678 eDVBTText::eDVBTText(eDVBDemux *demux): m_demux(demux)
679 {
680         char filename[128];
681 #if HAVE_DVB_API_VERSION < 3
682         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
683 #else
684         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
685 #endif
686         m_fd_demux = ::open(filename, O_RDWR);
687         if (m_fd_demux < 0)
688                 eWarning("%s: %m", filename);
689 }
690
691 int eDVBTText::startPid(int pid)
692 {
693         if (m_fd_demux < 0)
694                 return -1;
695         dmx_pes_filter_params pes;
696
697         pes.pid      = pid;
698         pes.input    = DMX_IN_FRONTEND;
699         pes.output   = DMX_OUT_DECODER;
700         pes.pes_type = DMX_PES_TELETEXT;
701         pes.flags    = 0;
702
703         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - ttx - ", pid);
704         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
705         {
706                 eDebug("failed(%m)");
707                 return -errno;
708         }
709         eDebug("ok");
710         eDebugNoNewLine("DEMUX_START - pcr - ");
711         if (::ioctl(m_fd_demux, DMX_START) < 0)
712         {
713                 eDebug("failed(%m)");
714                 return -errno;
715         }
716         eDebug("ok");
717         return 0;
718 }
719
720 void eDVBTText::stop()
721 {
722         eDebugNoNewLine("DEMUX_STOP - ttx - ");
723         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
724                 eDebug("failed(%m)");
725         else
726                 eDebug("ok");
727 }
728
729 eDVBTText::~eDVBTText()
730 {
731         if (m_fd_demux >= 0)
732                 ::close(m_fd_demux);
733 }
734
735 DEFINE_REF(eTSMPEGDecoder);
736
737 int eTSMPEGDecoder::setState()
738 {
739         int res = 0;
740
741         int noaudio = m_is_sm || m_is_ff || m_is_trickmode;
742         int nott = noaudio; /* actually same conditions */
743
744         if ((noaudio && m_audio) || (!m_audio && !noaudio))
745                 m_changed |= changeAudio;
746
747         if ((nott && m_text) || (!m_text && !nott))
748                 m_changed |= changeText;
749
750         bool changed = !!m_changed;
751 #if HAVE_DVB_API_VERSION < 3
752         bool checkAVSync = m_changed & (changeAudio|changeVideo|changePCR);
753         if (m_changed & changeAudio && m_audio)
754                 m_audio->stopPid();
755         if (m_changed & changeVideo && m_video)
756                 m_video->stopPid();
757         if (m_changed & changePCR && m_pcr)
758         {
759                 m_pcr->stop();
760                 m_pcr=0;
761                 if (!(m_pcrpid >= 0 && m_pcrpid < 0x1ff))
762                         m_changed &= ~changePCR;
763         }
764         if (m_changed & changeAudio && m_audio)
765         {
766                 m_audio->stop();
767                 m_audio=0;
768                 if (!(m_apid >= 0 && m_apid < 0x1ff))
769                         m_changed &= ~changeAudio;
770         }
771         if (m_changed & changeVideo && m_video)
772         {
773                 m_video->stop();
774                 m_video=0;
775                 m_video_event_conn=0;
776                 if (!(m_vpid >= 0 && m_vpid < 0x1ff))
777                         m_changed &= ~changeVideo;
778         }
779         if (m_changed & changeVideo)
780         {
781                 m_video = new eDVBVideo(m_demux, m_decoder);
782                 m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
783                 if (m_video->setPid(m_vpid))
784                         res -1;
785         }
786         if (m_changed & changePCR)
787         {
788                 m_pcr = new eDVBPCR(m_demux);
789                 if (m_pcr->setPid(m_pcrpid))
790                         res = -1;
791         }
792         if (m_changed & changeAudio)
793         {
794                 m_audio = new eDVBAudio(m_demux, m_decoder);
795                 if (m_audio->setPid(m_apid, m_atype))
796                         res = -1;
797         }
798         if (m_changed & changePCR)
799         {
800                 if (m_pcr->startPid())
801                         res = -1;
802                 m_changed &= ~changePCR;
803         }
804         else if (checkAVSync && m_audio && m_video)
805         {
806                 if (m_audio->setAVSync(1))
807                         res = -1;
808         }
809         if (m_changed & changeVideo)
810         {
811                 if (m_video->startPid() || m_video->start())
812                         res = -1;
813                 m_changed &= ~changeVideo;
814         }
815         if (m_changed & changeAudio)
816         {
817                 if (m_audio->start() || m_audio->startPid())
818                         res = -1;
819                 m_changed &= ~changeAudio;
820         }
821 #else
822         if (m_changed & changePCR)
823         {
824                 if (m_pcr)
825                         m_pcr->stop();
826                 m_pcr = 0;
827                 if ((m_pcrpid >= 0) && (m_pcrpid < 0x1FFF))
828                 {
829                         m_pcr = new eDVBPCR(m_demux);
830                         if (m_pcr->startPid(m_pcrpid))
831                                 res = -1;
832                 }
833                 m_changed &= ~changePCR;
834         }
835         if (m_changed & changeVideo)
836         {
837                 eDebug("VIDEO CHANGED (to %04x)", m_vpid);
838                 if (m_video)
839                 {
840                         m_video->stop();
841                         m_video = 0;
842                         m_video_event_conn = 0;
843                 }
844                 if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
845                 {
846                         m_video = new eDVBVideo(m_demux, m_decoder);
847                         m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
848                         if (m_video->startPid(m_vpid, m_vtype))
849                                 res = -1;
850                 }
851                 m_changed &= ~changeVideo;
852         }
853         if (m_changed & changeAudio)
854         {
855                 if (m_audio)
856                         m_audio->stop();
857                 m_audio = 0;
858                 if ((m_apid >= 0) && (m_apid < 0x1FFF) && !noaudio)
859                 {
860                         m_audio = new eDVBAudio(m_demux, m_decoder);
861                         if (m_audio->startPid(m_apid, m_atype))
862                                 res = -1;
863                 }
864                 m_changed &= ~changeAudio;
865         }
866         if (m_changed & changeText)
867         {
868                 if (m_text)
869                         m_text->stop();
870                 m_text = 0;
871                 if ((m_textpid >= 0) && (m_textpid < 0x1FFF) && !nott)
872                 {
873                         m_text = new eDVBTText(m_demux);
874                         if (m_text->startPid(m_textpid))
875                                 res = -1;
876                 }
877                 m_changed &= ~changeText;
878         }
879 #endif
880         if (changed && !m_video && m_audio && m_radio_pic.length())
881                 showSinglePic(m_radio_pic.c_str());
882
883         return res;
884 }
885
886 int eTSMPEGDecoder::m_pcm_delay=-1,
887         eTSMPEGDecoder::m_ac3_delay=-1;
888
889 RESULT eTSMPEGDecoder::setPCMDelay(int delay)
890 {
891         if (m_decoder == 0 && delay != m_pcm_delay )
892         {
893                 FILE *fp = fopen("/proc/stb/audio/audio_delay_pcm", "w");
894                 if (fp)
895                 {
896                         fprintf(fp, "%x", delay*90);
897                         fclose(fp);
898                         m_pcm_delay = delay;
899                         return 0;
900                 }
901         }
902         return -1;
903 }
904
905 RESULT eTSMPEGDecoder::setAC3Delay(int delay)
906 {
907         if ( m_decoder == 0 && delay != m_ac3_delay )
908         {
909                 FILE *fp = fopen("/proc/stb/audio/audio_delay_bitstream", "w");
910                 if (fp)
911                 {
912                         fprintf(fp, "%x", delay*90);
913                         fclose(fp);
914                         m_ac3_delay = delay;
915                         return 0;
916                 }
917         }
918         return -1;
919 }
920
921 eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder)
922         :m_demux(demux), m_changed(0), m_decoder(decoder), m_video_clip_fd(-1), m_showSinglePicTimer(eApp)
923 {
924         demux->connectEvent(slot(*this, &eTSMPEGDecoder::demux_event), m_demux_event_conn);
925         CONNECT(m_showSinglePicTimer.timeout, eTSMPEGDecoder::finishShowSinglePic);
926         m_is_ff = m_is_sm = m_is_trickmode = 0;
927 }
928
929 eTSMPEGDecoder::~eTSMPEGDecoder()
930 {
931         finishShowSinglePic();
932         m_vpid = m_apid = m_pcrpid = m_textpid = pidNone;
933         m_changed = -1;
934         setState();
935 }
936
937 RESULT eTSMPEGDecoder::setVideoPID(int vpid, int type)
938 {
939         if (m_vpid != vpid)
940         {
941                 m_changed |= changeVideo;
942                 m_vpid = vpid;
943                 m_vtype = type;
944         }
945         return 0;
946 }
947
948 RESULT eTSMPEGDecoder::setAudioPID(int apid, int type)
949 {
950         if ((m_apid != apid) || (m_atype != type))
951         {
952                 m_changed |= changeAudio;
953                 m_atype = type;
954                 m_apid = apid;
955         }
956         return 0;
957 }
958
959 int eTSMPEGDecoder::m_audio_channel = -1;
960
961 RESULT eTSMPEGDecoder::setAudioChannel(int channel)
962 {
963         if (channel == -1)
964                 channel = ac_stereo;
965         if (m_decoder == 0 && m_audio_channel != channel)
966         {
967                 if (m_audio)
968                 {
969                         m_audio->setChannel(channel);
970                         m_audio_channel=channel;
971                 }
972                 else
973                         eDebug("eTSMPEGDecoder::setAudioChannel but no audio decoder exist");
974         }
975         return 0;
976 }
977
978 int eTSMPEGDecoder::getAudioChannel()
979 {
980         return m_audio_channel == -1 ? ac_stereo : m_audio_channel;
981 }
982
983 RESULT eTSMPEGDecoder::setSyncPCR(int pcrpid)
984 {
985         if (m_pcrpid != pcrpid)
986         {
987                 m_changed |= changePCR;
988                 m_pcrpid = pcrpid;
989         }
990         return 0;
991 }
992
993 RESULT eTSMPEGDecoder::setTextPID(int textpid)
994 {
995         if (m_textpid != textpid)
996         {
997                 m_changed |= changeText;
998                 m_textpid = textpid;
999         }
1000         return 0;
1001 }
1002
1003 RESULT eTSMPEGDecoder::setSyncMaster(int who)
1004 {
1005         return -1;
1006 }
1007
1008 RESULT eTSMPEGDecoder::start()
1009 {
1010         RESULT r;
1011         r = setState();
1012         if (r)
1013                 return r;
1014         return unfreeze();
1015 }
1016
1017         /* preroll is start in freezed mode. */
1018 RESULT eTSMPEGDecoder::preroll()
1019 {
1020         return setState();
1021 }
1022
1023 RESULT eTSMPEGDecoder::freeze(int cont)
1024 {
1025         if (m_video)
1026                 m_video->freeze();
1027
1028         if (m_audio)
1029                 m_audio->freeze();
1030
1031         return 0;
1032 }
1033
1034 RESULT eTSMPEGDecoder::unfreeze()
1035 {
1036         if (m_video)
1037                 m_video->unfreeze();
1038
1039         if (m_audio)
1040                 m_audio->unfreeze();
1041
1042         return 0;
1043 }
1044
1045 RESULT eTSMPEGDecoder::setSinglePictureMode(int when)
1046 {
1047         return -1;
1048 }
1049
1050 RESULT eTSMPEGDecoder::setPictureSkipMode(int what)
1051 {
1052         return -1;
1053 }
1054
1055 RESULT eTSMPEGDecoder::setFastForward(int frames_to_skip)
1056 {
1057         m_is_ff = frames_to_skip != 0;
1058
1059         setState();
1060
1061         if (m_video)
1062                 return m_video->setFastForward(frames_to_skip);
1063         else
1064                 return -1;
1065 }
1066
1067 RESULT eTSMPEGDecoder::setSlowMotion(int repeat)
1068 {
1069         m_is_sm = repeat != 0;
1070
1071         setState();
1072
1073         if (m_video)
1074                 return m_video->setSlowMotion(repeat);
1075         else
1076                 return -1;
1077 }
1078
1079 RESULT eTSMPEGDecoder::setZoom(int what)
1080 {
1081         return -1;
1082 }
1083
1084 RESULT eTSMPEGDecoder::flush()
1085 {
1086         if (m_audio)
1087                 m_audio->flush();
1088         if (m_video)
1089                 m_video->flush();
1090         return 0;
1091 }
1092
1093 void eTSMPEGDecoder::demux_event(int event)
1094 {
1095         switch (event)
1096         {
1097         case eDVBDemux::evtFlush:
1098                 flush();
1099                 break;
1100         default:
1101                 break;
1102         }
1103 }
1104
1105 RESULT eTSMPEGDecoder::setTrickmode(int what)
1106 {
1107         m_is_trickmode = what;
1108         setState();
1109         return 0;
1110 }
1111
1112 RESULT eTSMPEGDecoder::getPTS(int what, pts_t &pts)
1113 {
1114         if (what == 0) /* auto */
1115                 what = m_video ? 1 : 2;
1116
1117         if (what == 1) /* video */
1118         {
1119                 if (m_video)
1120                         return m_video->getPTS(pts);
1121                 else
1122                         return -1;
1123         }
1124
1125         if (what == 2) /* audio */
1126         {
1127                 if (m_audio)
1128                         return m_audio->getPTS(pts);
1129                 else
1130                         return -1;
1131         }
1132
1133         return -1;
1134 }
1135
1136 RESULT eTSMPEGDecoder::setRadioPic(const std::string &filename)
1137 {
1138         m_radio_pic = filename;
1139         return 0;
1140 }
1141
1142 RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
1143 {
1144         if (m_decoder == 0)
1145         {
1146                 eDebug("showSinglePic %s", filename);
1147                 int f = open(filename, O_RDONLY);
1148                 if (f >= 0)
1149                 {
1150                         struct stat s;
1151                         fstat(f, &s);
1152                         if (m_video_clip_fd == -1)
1153                                 m_video_clip_fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
1154                         if (m_video_clip_fd >= 0)
1155                         {
1156                                 bool seq_end_avail = false;
1157                                 size_t pos=0;
1158                                 unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1159                                 unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
1160                                 unsigned char iframe[s.st_size];
1161                                 unsigned char stuffing[8192];
1162                                 memset(stuffing, 0, 8192);
1163                                 read(f, iframe, s.st_size);
1164                                 if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
1165                                         eDebug("VIDEO_SELECT_SOURCE MEMORY failed (%m)");
1166                                 if (ioctl(m_video_clip_fd, VIDEO_PLAY) < 0)
1167                                         eDebug("VIDEO_PLAY failed (%m)");
1168                                 if (::ioctl(m_video_clip_fd, VIDEO_CONTINUE) < 0)
1169                                         eDebug("video: VIDEO_CONTINUE: %m");
1170                                 if (::ioctl(m_video_clip_fd, VIDEO_CLEAR_BUFFER) < 0)
1171                                         eDebug("video: VIDEO_CLEAR_BUFFER: %m");
1172                                 while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
1173                                         ++pos;
1174                                 if ((iframe[3] >> 4) != 0xE) // no pes header
1175                                         write(m_video_clip_fd, pes_header, sizeof(pes_header));
1176                                 write(m_video_clip_fd, iframe, s.st_size);
1177                                 if (!seq_end_avail)
1178                                         write(m_video_clip_fd, seq_end, sizeof(seq_end));
1179                                 write(m_video_clip_fd, stuffing, 8192);
1180                                 m_showSinglePicTimer.start(150, true);
1181                         }
1182                         close(f);
1183                 }
1184                 else
1185                 {
1186                         eDebug("couldnt open %s", filename);
1187                         return -1;
1188                 }
1189         }
1190         else
1191         {
1192                 eDebug("only show single pics on first decoder");
1193                 return -1;
1194         }
1195         return 0;
1196 }
1197
1198 void eTSMPEGDecoder::finishShowSinglePic()
1199 {
1200         if (m_video_clip_fd >= 0)
1201         {
1202                 if (ioctl(m_video_clip_fd, VIDEO_STOP, 0) < 0)
1203                         eDebug("VIDEO_STOP failed (%m)");
1204                 if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX) < 0)
1205                                 eDebug("VIDEO_SELECT_SOURCE DEMUX failed (%m)");
1206                 close(m_video_clip_fd);
1207                 m_video_clip_fd = -1;
1208         }
1209 }
1210
1211 RESULT eTSMPEGDecoder::connectVideoEvent(const Slot1<void, struct videoEvent> &event, ePtr<eConnection> &conn)
1212 {
1213         conn = new eConnection(this, m_video_event.connect(event));
1214         return 0;
1215 }
1216
1217 void eTSMPEGDecoder::video_event(struct videoEvent event)
1218 {
1219         /* emit */ m_video_event(event);
1220 }