set frontend state to stateTuning when tuneloop starts.. ( to avoid false lostlock...
[enigma2.git] / lib / dvb / demux.cpp
index f52bb601db6dab1d7f4303c4ccbe74d075928436..384a77e15872e4e280be0062ad60593760d1d8eb 100644 (file)
@@ -6,12 +6,23 @@
 #include <unistd.h>
 #include <signal.h>
 
-
 #if HAVE_DVB_API_VERSION < 3
 #include <ost/dmx.h>
+
 #ifndef DMX_SET_NEGFILTER_MASK
        #define DMX_SET_NEGFILTER_MASK   _IOW('o',48,uint8_t *)
 #endif
+
+#ifndef DMX_GET_STC
+       struct dmx_stc
+       {
+               unsigned int num;       /* input : which STC? O..N */
+               unsigned int base;      /* output: divisor for stc to get 90 kHz clock */
+               unsigned long long stc; /* output: src in 'base'*90 kHz units */
+       };
+       #define DMX_GET_STC             _IOR('o', 50, struct dmx_stc)
+#endif
+
 #else
 #include <linux/dvb/dmx.h>
 #endif
@@ -34,8 +45,46 @@ eDVBDemux::~eDVBDemux()
 {
 }
 
+int eDVBDemux::openDemux(void)
+{
+       char filename[128];
+#if HAVE_DVB_API_VERSION < 3
+       snprintf(filename, 128, "/dev/dvb/card%d/demux%d", adapter, demux);
+#else
+       snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", adapter, demux);
+#endif
+       return ::open(filename, O_RDWR);
+}
+
 DEFINE_REF(eDVBDemux)
 
+RESULT eDVBDemux::setSourceFrontend(int fenum)
+{
+#if HAVE_DVB_API_VERSION >= 3
+       int fd = openDemux();
+       
+       int n = DMX_SOURCE_FRONT0 + fenum;
+       int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
+       if (res)
+               eDebug("DMX_SET_SOURCE failed! - %m");
+       ::close(fd);
+       return res;
+#endif
+       return 0;
+}
+
+RESULT eDVBDemux::setSourcePVR(int pvrnum)
+{
+#if HAVE_DVB_API_VERSION >= 3
+       int fd = openDemux();
+       int n = DMX_SOURCE_DVR0 + pvrnum;
+       int res = ::ioctl(fd, DMX_SET_SOURCE, &n);
+       ::close(fd);
+       return res;
+#endif
+       return 0;
+}
+
 RESULT eDVBDemux::createSectionReader(eMainloop *context, ePtr<iDVBSectionReader> &reader)
 {
        RESULT res;
@@ -59,21 +108,15 @@ RESULT eDVBDemux::getMPEGDecoder(ePtr<iTSMPEGDecoder> &decoder)
        return 0;
 }
 
-RESULT eDVBDemux::getSTC(pts_t &pts)
+RESULT eDVBDemux::getSTC(pts_t &pts, int num)
 {
-       char filename[128];
-#if HAVE_DVB_API_VERSION < 3
-       sprintf(filename, "/dev/dvb/card%d/demux%d", adapter, demux);
-#else
-       sprintf(filename, "/dev/dvb/adapter%d/demux%d", adapter, demux);
-#endif
-       int fd = ::open(filename, O_RDWR);
+       int fd = openDemux();
        
        if (fd < 0)
                return -ENODEV;
 
        struct dmx_stc stc;
-       stc.num = 0;
+       stc.num = num;
        stc.base = 1;
        
        if (ioctl(fd, DMX_GET_STC, &stc) < 0)
@@ -131,14 +174,7 @@ void eDVBSectionReader::data(int)
 eDVBSectionReader::eDVBSectionReader(eDVBDemux *demux, eMainloop *context, RESULT &res): demux(demux)
 {
        char filename[128];
-#if HAVE_DVB_API_VERSION < 3
-       sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
-#else
-       sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
-#endif
-       fd = ::open(filename, O_RDWR);
-       
-       eDebug("eDVBSectionReader has fd %d", fd);
+       fd = demux->openDemux();
        
        if (fd >= 0)
        {
@@ -345,16 +381,10 @@ RESULT eDVBTSRecorder::connectEvent(const Slot1<void,int> &event, ePtr<eConnecti
 
 RESULT eDVBTSRecorder::startPID(int pid)
 {
-       char filename[128];
-#if HAVE_DVB_API_VERSION < 3
-       snprintf(filename, 128, "/dev/dvb/card%d/demux%d", m_demux->adapter, m_demux->demux);
-#else
-       snprintf(filename, 128, "/dev/dvb/adapter%d/demux%d", m_demux->adapter, m_demux->demux);
-#endif
-       int fd = ::open(filename, O_RDWR);
+       int fd = m_demux->openDemux();
        if (fd < 0)
        {
-               eDebug("FAILED to open demux (%s) in ts recoder (%m)", filename);
+               eDebug("FAILED to open demux in ts recoder (%m)");
                return -1;
        }
 
@@ -388,6 +418,7 @@ RESULT eDVBTSRecorder::startPID(int pid)
 
 void eDVBTSRecorder::stopPID(int pid)
 {
-       ::close(m_pids[pid]);
+       if (m_pids[pid] != -1)
+               ::close(m_pids[pid]);
        m_pids[pid] = -1;
 }