add possibility to set tuner priority for alternative services in Customize
[vuplus_dvbapp] / lib / dvb / dvb.cpp
index 35e5c99..b7d278c 100644 (file)
@@ -2,6 +2,7 @@
 #include <lib/base/filepush.h>
 #include <lib/dvb/idvb.h>
 #include <lib/dvb/dvb.h>
+#include <lib/dvb/pmt.h>
 #include <lib/dvb/sec.h>
 
 #include <errno.h>
@@ -9,6 +10,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/ioctl.h>
 
 DEFINE_REF(eDVBRegisteredFrontend);
 DEFINE_REF(eDVBRegisteredDemux);
@@ -17,12 +19,12 @@ DEFINE_REF(eDVBAllocatedFrontend);
 
 eDVBAllocatedFrontend::eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe): m_fe(fe)
 {
-       m_fe->m_inuse++;
+       m_fe->inc_use();
 }
 
 eDVBAllocatedFrontend::~eDVBAllocatedFrontend()
 {
-       --m_fe->m_inuse;
+       m_fe->dec_use();
 }
 
 DEFINE_REF(eDVBAllocatedDemux);
@@ -41,11 +43,29 @@ DEFINE_REF(eDVBResourceManager);
 
 eDVBResourceManager *eDVBResourceManager::instance;
 
+RESULT eDVBResourceManager::getInstance(ePtr<eDVBResourceManager> &ptr)
+{
+       if (instance)
+       {
+               ptr = instance;
+               return 0;
+       }
+       return -1;
+}
+
+ePtr<eDVBResourceManager> NewResourceManagerPtr(void)
+{
+       ePtr<eDVBResourceManager> ptr;
+       eDVBResourceManager::getInstance(ptr);
+       return ptr;
+}
+
 eDVBResourceManager::eDVBResourceManager()
+       :m_releaseCachedChannelTimer(eApp)
 {
        avail = 1;
        busy = 0;
-       m_sec = new eDVBSatelliteEquipmentControl;
+       m_sec = new eDVBSatelliteEquipmentControl(m_frontend);
        if (!instance)
                instance = this;
                
@@ -62,8 +82,20 @@ eDVBResourceManager::eDVBResourceManager()
        
        eDebug("found %d adapter, %d frontends and %d demux", 
                m_adapter.size(), m_frontend.size(), m_demux.size());
+
+       eDVBCAService::registerChannelCallback(this);
+
+       CONNECT(m_releaseCachedChannelTimer.timeout, eDVBResourceManager::releaseCachedChannel);
 }
 
+void eDVBResourceManager::feStateChanged()
+{
+       int mask=0;
+       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+               if (i->m_inuse)
+                       mask |= ( 1 << i->m_frontend->getSlotID() );
+       /* emit */ frontendUseMaskChanged(mask);
+}
 
 DEFINE_REF(eDVBAdapterLinux);
 eDVBAdapterLinux::eDVBAdapterLinux(int nr): m_nr(nr)
@@ -176,7 +208,6 @@ eDVBResourceManager::~eDVBResourceManager()
 {
        if (instance == this)
                instance = 0;
-
 }
 
 void eDVBResourceManager::addAdapter(iDVBAdapter *adapter)
@@ -194,23 +225,130 @@ void eDVBResourceManager::addAdapter(iDVBAdapter *adapter)
                        m_demux.push_back(new eDVBRegisteredDemux(demux, adapter));
        }
 
+       ePtr<eDVBRegisteredFrontend> prev_dvbt_frontend;
        for (i=0; i<num_fe; ++i)
        {
                ePtr<eDVBFrontend> frontend;
                if (!adapter->getFrontend(frontend, i))
-                       m_frontend.push_back(new eDVBRegisteredFrontend(frontend, adapter));
+               {
+                       int frontendType=0;
+                       frontend->getFrontendType(frontendType);
+                       eDVBRegisteredFrontend *new_fe = new eDVBRegisteredFrontend(frontend, adapter);
+                       CONNECT(new_fe->stateChanged, eDVBResourceManager::feStateChanged);
+                       m_frontend.push_back(new_fe);
+                       frontend->setSEC(m_sec);
+                       // we must link all dvb-t frontends ( for active antenna voltage )
+                       if (frontendType == iDVBFrontend::feTerrestrial)
+                       {
+                               if (prev_dvbt_frontend)
+                               {
+                                       prev_dvbt_frontend->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (int)new_fe);
+                                       frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (int)&(*prev_dvbt_frontend));
+                               }
+                               prev_dvbt_frontend = new_fe;
+                       }
+               }
+       }
+}
+
+PyObject *eDVBResourceManager::setFrontendSlotInformations(ePyObject list)
+{
+       if (!PyList_Check(list))
+       {
+               PyErr_SetString(PyExc_StandardError, "eDVBResourceManager::setFrontendSlotInformations argument should be a python list");
+               return NULL;
+       }
+       if ((unsigned int)PyList_Size(list) != m_frontend.size())
+       {
+               char blasel[256];
+               sprintf(blasel, "eDVBResourceManager::setFrontendSlotInformations list size incorrect %d frontends avail, but %d entries in slotlist",
+                       m_frontend.size(), PyList_Size(list));
+               PyErr_SetString(PyExc_StandardError, blasel);
+               return NULL;
        }
+       int pos=0;
+       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+       {
+               ePyObject obj = PyList_GET_ITEM(list, pos++);
+               if (!i->m_frontend->setSlotInfo(obj))
+                       return NULL;
+       }
+       Py_RETURN_NONE;
 }
 
-RESULT eDVBResourceManager::allocateFrontend(const eDVBChannelID &chid, ePtr<eDVBAllocatedFrontend> &fe)
+RESULT eDVBResourceManager::allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm)
 {
-               /* find first unused frontend. we ignore compatibility for now. */
+       ePtr<eDVBRegisteredFrontend> best;
+       int bestval = 0;
+
        for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
                if (!i->m_inuse)
                {
+                       int c = i->m_frontend->isCompatibleWith(feparm);
+                       if (c > bestval)
+                       {
+                               bestval = c;
+                               best = i;
+                       }
+               }
+
+       if (best)
+       {
+               fe = new eDVBAllocatedFrontend(best);
+               return 0;
+       }
+       
+       fe = 0;
+       
+       return -1;
+}
+
+RESULT eDVBResourceManager::allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int slot_index)
+{
+       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+               if (!i->m_inuse && i->m_frontend->getSlotID() == slot_index)
+               {
+                       // check if another slot linked to this is in use
+                       eDVBRegisteredFrontend *satpos_depends_to_fe =
+                               (eDVBRegisteredFrontend*) i->m_frontend->m_data[eDVBFrontend::SATPOS_DEPENDS_PTR];
+                       if ( (int)satpos_depends_to_fe != -1 )
+                       {
+                               if (satpos_depends_to_fe->m_inuse)
+                               {
+                                       eDebug("another satpos depending frontend is in use.. so allocateFrontendByIndex not possible!");
+                                       goto alloc_fe_by_id_not_possible;
+                               }
+                       }
+                       else // check linked tuners
+                       {
+                               eDVBRegisteredFrontend *next =
+                                       (eDVBRegisteredFrontend *) i->m_frontend->m_data[eDVBFrontend::LINKED_NEXT_PTR];
+                               while ( (int)next != -1 )
+                               {
+                                       if (next->m_inuse)
+                                       {
+                                               eDebug("another linked frontend is in use.. so allocateFrontendByIndex not possible!");
+                                               goto alloc_fe_by_id_not_possible;
+                                       }
+                                       next = (eDVBRegisteredFrontend *)next->m_frontend->m_data[eDVBFrontend::LINKED_NEXT_PTR];
+                               }
+                               eDVBRegisteredFrontend *prev = (eDVBRegisteredFrontend *)
+                                       i->m_frontend->m_data[eDVBFrontend::LINKED_PREV_PTR];
+                               while ( (int)prev != -1 )
+                               {
+                                       if (prev->m_inuse)
+                                       {
+                                               eDebug("another linked frontend is in use.. so allocateFrontendByIndex not possible!");
+                                               goto alloc_fe_by_id_not_possible;
+                                       }
+                                       prev = (eDVBRegisteredFrontend *)prev->m_frontend->m_data[eDVBFrontend::LINKED_PREV_PTR];
+                               }
+                       }
                        fe = new eDVBAllocatedFrontend(i);
                        return 0;
                }
+alloc_fe_by_id_not_possible:
+       fe = 0;
        return -1;
 }
 
@@ -225,17 +363,36 @@ RESULT eDVBResourceManager::allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBA
        if (i == m_demux.end())
                return -1;
                
+       int n=0;
                /* FIXME: hardware demux policy */
        if (!(cap & iDVBChannel::capDecode))
-               ++i;
+       {
+               if (m_demux.size() > 2)  /* assumed to be true, otherwise we have lost anyway */
+               {
+                       ++i, ++n;
+                       ++i, ++n;
+               }
+       }
        
-       for (; i != m_demux.end(); ++i)
-               if ((!i->m_inuse) && ((!fe) || (i->m_adapter == fe->m_adapter)))
+       for (; i != m_demux.end(); ++i, ++n)
+       {
+               int is_decode = n < 2;
+               
+               int in_use = is_decode ? (i->m_demux->getRefCount() != 2) : i->m_inuse;
+               
+               if ((!in_use) && ((!fe) || (i->m_adapter == fe->m_adapter)))
                {
+                       if ((cap & iDVBChannel::capDecode) && !is_decode)
+                               continue;
+                       
                        demux = new eDVBAllocatedDemux(i);
-                       eDebug("demux found");
+                       if (fe)
+                               demux->get().setSourceFrontend(fe->m_frontend->getDVBID());
+                       else
+                               demux->get().setSourcePVR(0);
                        return 0;
                }
+       }
        eDebug("demux not found");
        return -1;
 }
@@ -255,11 +412,24 @@ RESULT eDVBResourceManager::getChannelList(ePtr<iDVBChannelList> &list)
                return -ENOENT;
 }
 
-
 RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel)
 {
                /* first, check if a channel is already existing. */
-       
+
+       if (m_cached_channel)
+       {
+               eDVBChannel *cache_chan = (eDVBChannel*)&(*m_cached_channel);
+               if(channelid==cache_chan->getChannelID())
+               {
+                       eDebug("use cached_channel");
+                       channel = m_cached_channel;
+                       return 0;
+               }
+               m_cached_channel_state_changed_conn.disconnect();
+               m_cached_channel=0;
+               m_releaseCachedChannelTimer.stop();
+       }
+
 //     eDebug("allocate channel.. %04x:%04x", channelid.transport_stream_id.get(), channelid.original_network_id.get());
        for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
        {
@@ -272,59 +442,92 @@ RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUse
                }
        }
        
-               /* no currently available channel is tuned to this channelid. create a new one, if possible. */
-               
-               /* allocate a frontend. */
+       /* no currently available channel is tuned to this channelid. create a new one, if possible. */
+
+       if (!m_list)
+       {
+               eDebug("no channel list set!");
+               return -ENOENT;
+       }
+
+       ePtr<iDVBFrontendParameters> feparm;
+       if (m_list->getChannelFrontendData(channelid, feparm))
+       {
+               eDebug("channel not found!");
+               return -ENOENT;
+       }
+
+       /* allocate a frontend. */
        
        ePtr<eDVBAllocatedFrontend> fe;
        
-       if (allocateFrontend(channelid, fe))
+       if (allocateFrontend(fe, feparm))
                return errNoFrontend;
-       
-// will be allocated on demand:
-//     ePtr<eDVBAllocatedDemux> demux;
-//     
-//     if (allocateDemux(*fe, demux))
-//             return errNoDemux;
-       
+
        RESULT res;
-       eDVBChannel *ch;
+       ePtr<eDVBChannel> ch;
        ch = new eDVBChannel(this, fe);
 
-       ePtr<iDVBFrontend> myfe;
-       if (!ch->getFrontend(myfe))
-               myfe->setSEC(m_sec);
-
-       res = ch->setChannel(channelid);
+       res = ch->setChannel(channelid, feparm);
        if (res)
        {
                channel = 0;
                return errChidNotFound;
        }
-       
-       channel = ch;
+       m_cached_channel = channel = ch;
+       m_cached_channel_state_changed_conn =
+               CONNECT(ch->m_stateChanged,eDVBResourceManager::DVBChannelStateChanged);
+
        return 0;
 }
 
-RESULT eDVBResourceManager::allocateRawChannel(eUsePtr<iDVBChannel> &channel)
+void eDVBResourceManager::DVBChannelStateChanged(iDVBChannel *chan)
+{
+       int state=0;
+       chan->getState(state);
+       switch (state)
+       {
+               case iDVBChannel::state_release:
+               case iDVBChannel::state_ok:
+               {
+                       eDebug("stop release channel timer");
+                       m_releaseCachedChannelTimer.stop();
+                       break;
+               }
+               case iDVBChannel::state_last_instance:
+               {
+                       eDebug("start release channel timer");
+                       m_releaseCachedChannelTimer.start(3000, true);
+                       break;
+               }
+               default: // ignore all other events
+                       break;
+       }
+}
+
+void eDVBResourceManager::releaseCachedChannel()
+{
+       eDebug("release cached channel (timer timeout)");
+       m_cached_channel=0;
+}
+
+RESULT eDVBResourceManager::allocateRawChannel(eUsePtr<iDVBChannel> &channel, int slot_index)
 {
        ePtr<eDVBAllocatedFrontend> fe;
-       
-       if (allocateFrontend(eDVBChannelID(), fe))
+
+       if (m_cached_channel)
+       {
+               m_cached_channel_state_changed_conn.disconnect();
+               m_cached_channel=0;
+               m_releaseCachedChannelTimer.stop();
+       }
+
+       if (allocateFrontendByIndex(fe, slot_index))
                return errNoFrontend;
        
-//     ePtr<eDVBAllocatedDemux> demux;
-       //
-//     if (allocateDemux(*fe, demux))
-//             return errNoDemux;
-       
        eDVBChannel *ch;
        ch = new eDVBChannel(this, fe);
 
-       ePtr<iDVBFrontend> myfe;
-       if (!ch->getFrontend(myfe))
-               myfe->setSEC(m_sec);
-
        channel = ch;
        return 0;
 }
@@ -333,20 +536,23 @@ RESULT eDVBResourceManager::allocateRawChannel(eUsePtr<iDVBChannel> &channel)
 RESULT eDVBResourceManager::allocatePVRChannel(eUsePtr<iDVBPVRChannel> &channel)
 {
        ePtr<eDVBAllocatedDemux> demux;
-       
-//     if (allocateDemux(0, demux))
-//             return errNoDemux;
-       
+
+       if (m_cached_channel && m_releaseCachedChannelTimer.isActive())
+       {
+               m_cached_channel_state_changed_conn.disconnect();
+               m_cached_channel=0;
+               m_releaseCachedChannelTimer.stop();
+       }
+
        eDVBChannel *ch;
        ch = new eDVBChannel(this, 0);
-       
+
        channel = ch;
        return 0;
 }
 
 RESULT eDVBResourceManager::addChannel(const eDVBChannelID &chid, eDVBChannel *ch)
 {
-       eDebug("add channel %p", ch);
        m_active_channels.push_back(active_channel(chid, ch));
        /* emit */ m_channelAdded(ch);
        return 0;
@@ -376,6 +582,250 @@ RESULT eDVBResourceManager::connectChannelAdded(const Slot1<void,eDVBChannel*> &
        return 0;
 }
 
+int eDVBResourceManager::canAllocateFrontend(ePtr<iDVBFrontendParameters> &feparm)
+{
+       ePtr<eDVBRegisteredFrontend> best;
+       int bestval = 0;
+
+       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+               if (!i->m_inuse)
+               {
+                       int c = i->m_frontend->isCompatibleWith(feparm);
+                       if (c > bestval)
+                               bestval = c;
+               }
+       return bestval;
+}
+
+int tuner_type_channel_default(ePtr<iDVBChannelList> &channellist, const eDVBChannelID &chid)
+{
+       if (channellist)
+       {
+               ePtr<iDVBFrontendParameters> feparm;
+               if (!channellist->getChannelFrontendData(chid, feparm))
+               {
+                       int system;
+                       if (!feparm->getSystem(system))
+                       {
+                               switch(system)
+                               {
+                                       case iDVBFrontend::feSatellite:
+                                               return 50000;
+                                       case iDVBFrontend::feCable:
+                                               return 40000;
+                                       case iDVBFrontend::feTerrestrial:
+                                               return 30000;
+                                       default:
+                                               break;
+                               }
+                       }
+               }
+       }
+       return 0;
+}
+
+int eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID& ignore)
+{
+       int ret=0;
+       if (m_cached_channel)
+       {
+               eDVBChannel *cache_chan = (eDVBChannel*)&(*m_cached_channel);
+               if(channelid==cache_chan->getChannelID())
+                       return tuner_type_channel_default(m_list, channelid);
+       }
+
+               /* first, check if a channel is already existing. */
+//     eDebug("allocate channel.. %04x:%04x", channelid.transport_stream_id.get(), channelid.original_network_id.get());
+       for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
+       {
+//             eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
+               if (i->m_channel_id == channelid)
+               {
+//                     eDebug("found shared channel..");
+                       return tuner_type_channel_default(m_list, channelid);
+               }
+       }
+
+       int *decremented_cached_channel_fe_usecount=NULL,
+               *decremented_fe_usecount=NULL;
+
+       for (std::list<active_channel>::iterator i(m_active_channels.begin()); i != m_active_channels.end(); ++i)
+       {
+//             eDebug("available channel.. %04x:%04x", i->m_channel_id.transport_stream_id.get(), i->m_channel_id.original_network_id.get());
+               if (i->m_channel_id == ignore)
+               {
+                       eDVBChannel *channel = (eDVBChannel*) &(*i->m_channel);
+                       // one eUsePtr<iDVBChannel> is used in eDVBServicePMTHandler
+                       // another on eUsePtr<iDVBChannel> is used in the eDVBScan instance used in eDVBServicePMTHandler (for SDT scan)
+                       // so we must check here if usecount is 3 (when the channel is equal to the cached channel)
+                       // or 2 when the cached channel is not equal to the compared channel
+                       if (channel == &(*m_cached_channel) ? channel->getUseCount() == 3 : channel->getUseCount() == 2)  // channel only used once..
+                       {
+                               ePtr<iDVBFrontend> fe;
+                               if (!i->m_channel->getFrontend(fe))
+                               {
+                                       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator ii(m_frontend.begin()); ii != m_frontend.end(); ++ii)
+                                       {
+                                               if ( &(*fe) == &(*ii->m_frontend) )
+                                               {
+                                                       --ii->m_inuse;
+                                                       decremented_fe_usecount = &ii->m_inuse;
+                                                       if (channel == &(*m_cached_channel))
+                                                               decremented_cached_channel_fe_usecount = decremented_fe_usecount;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
+                       break;
+               }
+       }
+
+       if (!decremented_cached_channel_fe_usecount)
+       {
+               if (m_cached_channel)
+               {
+                       eDVBChannel *channel = (eDVBChannel*) &(*m_cached_channel);
+                       if (channel->getUseCount() == 1)
+                       {
+                               ePtr<iDVBFrontend> fe;
+                               if (!channel->getFrontend(fe))
+                               {
+                                       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator ii(m_frontend.begin()); ii != m_frontend.end(); ++ii)
+                                       {
+                                               if ( &(*fe) == &(*ii->m_frontend) )
+                                               {
+                                                       --ii->m_inuse;
+                                                       decremented_cached_channel_fe_usecount = &ii->m_inuse;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       else
+               decremented_cached_channel_fe_usecount=NULL;
+
+       ePtr<iDVBFrontendParameters> feparm;
+
+       if (!m_list)
+       {
+               eDebug("no channel list set!");
+               goto error;
+       }
+
+       if (m_list->getChannelFrontendData(channelid, feparm))
+       {
+               eDebug("channel not found!");
+               goto error;
+       }
+
+       ret = canAllocateFrontend(feparm);
+
+error:
+       if (decremented_fe_usecount)
+               ++(*decremented_fe_usecount);
+       if (decremented_cached_channel_fe_usecount)
+               ++(*decremented_cached_channel_fe_usecount);
+
+       return ret;
+}
+
+class eDVBChannelFilePush: public eFilePushThread
+{
+public:
+       void setIFrameSearch(int enabled) { m_iframe_search = enabled; m_iframe_state = 0; }
+protected:
+       int m_iframe_search, m_iframe_state, m_pid;
+       int filterRecordData(const unsigned char *data, int len, size_t &current_span_remaining);
+};
+
+int eDVBChannelFilePush::filterRecordData(const unsigned char *_data, int len, size_t &current_span_remaining)
+{
+#if 0 /* not yet */
+       if (!m_iframe_search)
+               return len;
+
+       unsigned char *data = (unsigned char*)_data; /* remove that const. we know what we are doing. */
+
+       eDebug("filterRecordData, size=%d (mod 188=%d), first byte is %02x", len, len %188, data[0]);
+
+       unsigned char *d = data;
+       while ((d = (unsigned char*)memmem(d, data + len - d, "\x00\x00\x01", 3)))
+       {
+               int offset = d - data;
+               int ts_offset = offset - offset % 188; /* offset to the start of TS packet */
+               unsigned char *ts = data + ts_offset;
+               int pid = ((ts[1] << 8) | ts[2]) & 0x1FFF;
+
+               if ((d[3] == 0) && (m_pid == pid))  /* picture start */
+               {
+                       int picture_type = (d[5] >> 3) & 7;
+                       d += 4;
+                       
+                       eDebug("%d-frame at %d, offset in TS packet: %d, pid=%04x", picture_type, offset, offset % 188, pid);
+
+                       if (m_iframe_state == 1)
+                       {
+                                       /* we are allowing data, and stop allowing data on the next frame. 
+                                          we now found a frame. so stop here. */
+                               memset(data + offset, 0, 188 - (offset%188)); /* zero out rest of TS packet */
+                               current_span_remaining = 0;
+                               m_iframe_state = 0;
+                               unsigned char *fts = ts + 188;
+                               while (fts < (data + len))
+                               {
+                                       fts[1] |= 0x1f;
+                                       fts[2] |= 0xff; /* drop packet */
+                                       fts += 188;
+                               }
+
+                               return len; // ts_offset + 188; /* deliver this packet, but not more. */
+                       } else
+                       {
+                               if (picture_type != 1) /* we are only interested in I frames */
+                                       continue;
+                       
+                               unsigned char *fts = data;
+                               while (fts < ts)
+                               {
+                                       fts[1] |= 0x1f;
+                                       fts[2] |= 0xff; /* drop packet */
+                               
+                                       fts += 188;
+                               }
+                                               /* force payload only */
+                               ts[3] &= ~0x30;
+                               ts[3] |=  0x10;
+                               
+//                             memset(ts + 4, 0xFF, (offset % 188) - 4);
+
+                               m_iframe_state = 1;
+                       }
+               } else if ((d[3] & 0xF0) == 0xE0) /* video stream */
+               {
+                       if (m_pid != pid)
+                       {
+                               eDebug("now locked to pid %04x", pid);
+                               m_pid = pid;
+                       }
+//                     m_pid = 0x6e;
+                       d += 4;
+               } else
+                       d += 4; /* ignore */
+
+       }
+
+       if (m_iframe_state == 1)
+               return len;
+       else
+               return 0; /* we need find an iframe first */
+#else
+       return len;
+#endif
+}
+
 DEFINE_REF(eDVBChannel);
 
 eDVBChannel::eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *frontend): m_state(state_idle), m_mgr(mgr)
@@ -384,6 +834,8 @@ eDVBChannel::eDVBChannel(eDVBResourceManager *mgr, eDVBAllocatedFrontend *fronte
 
        m_pvr_thread = 0;
        
+       m_skipmode_n = m_skipmode_m = 0;
+       
        if (m_frontend)
                m_frontend->get().connectStateChange(slot(*this, &eDVBChannel::frontendStateChanged), m_conn_frontendStateChanged);
 }
@@ -392,19 +844,12 @@ eDVBChannel::~eDVBChannel()
 {
        if (m_channel_id)
                m_mgr->removeChannel(this);
-       
-       if (m_pvr_thread)
-       {
-               m_pvr_thread->stop();
-               ::close(m_pvr_fd_src);
-               ::close(m_pvr_fd_dst);
-               delete m_pvr_thread;
-       }
+
+       stopFile();
 }
 
 void eDVBChannel::frontendStateChanged(iDVBFrontend*fe)
 {
-       eDebug("fe state changed!");
        int state, ourstate = 0;
        
                /* if we are already in shutdown, don't change state. */
@@ -422,10 +867,24 @@ void eDVBChannel::frontendStateChanged(iDVBFrontend*fe)
        {
                eDebug("OURSTATE: tuning");
                ourstate = state_tuning;
+       } else if (state == iDVBFrontend::stateLostLock)
+       {
+                       /* on managed channels, we try to retune in order to re-acquire lock. */
+               if (m_current_frontend_parameters)
+               {
+                       eDebug("OURSTATE: lost lock, trying to retune");
+                       ourstate = state_tuning;
+                       m_frontend->get().tune(*m_current_frontend_parameters);
+               } else
+                       /* on unmanaged channels, we don't do this. the client will do this. */
+               {
+                       eDebug("OURSTATE: lost lock, unavailable now.");
+                       ourstate = state_unavailable;
+               }
        } else if (state == iDVBFrontend::stateFailed)
        {
-               eDebug("OURSTATE: failed/unavailable");
-               ourstate = state_unavailable;
+               eDebug("OURSTATE: failed");
+               ourstate = state_failed;
        } else
                eFatal("state unknown");
        
@@ -436,9 +895,304 @@ void eDVBChannel::frontendStateChanged(iDVBFrontend*fe)
        }
 }
 
+void eDVBChannel::pvrEvent(int event)
+{
+       switch (event)
+       {
+       case eFilePushThread::evtEOF:
+               eDebug("eDVBChannel: End of file!");
+               m_event(this, evtEOF);
+               break;
+       case eFilePushThread::evtUser: /* start */
+               eDebug("SOF");
+               m_event(this, evtSOF);
+               break;
+       }
+}
+
+void eDVBChannel::cueSheetEvent(int event)
+{
+       switch (event)
+       {
+       case eCueSheet::evtSeek:
+               eDebug("seek.");
+               flushPVR(m_cue->m_decoding_demux);
+               break;
+       case eCueSheet::evtSkipmode:
+       {
+               {
+                       m_cue->m_lock.WrLock();
+                       m_cue->m_seek_requests.push_back(std::pair<int, pts_t>(1, 0)); /* resync */
+                       m_cue->m_lock.Unlock();
+                       eRdLocker l(m_cue->m_lock);
+                       if (m_cue->m_skipmode_ratio)
+                       {
+                               int bitrate = m_tstools.calcBitrate(); /* in bits/s */
+                               eDebug("skipmode ratio is %lld:90000, bitrate is %d bit/s", m_cue->m_skipmode_ratio, bitrate);
+                                               /* i agree that this might look a bit like black magic. */
+                               m_skipmode_n = 512*1024; /* must be 1 iframe at least. */
+                               m_skipmode_m = bitrate / 8 / 90000 * m_cue->m_skipmode_ratio / 8;
+                               
+                               if (m_cue->m_skipmode_ratio < 0)
+                                       m_skipmode_m -= m_skipmode_n;
+       
+                               eDebug("resolved to: %d %d", m_skipmode_m, m_skipmode_n);
+                               
+                               if (abs(m_skipmode_m) < abs(m_skipmode_n))
+                               {
+                                       eWarning("something is wrong with this calculation");
+                                       m_skipmode_n = m_skipmode_m = 0;
+                               }
+                               
+                       } else
+                       {
+                               eDebug("skipmode ratio is 0, normal play");
+                               m_skipmode_n = m_skipmode_m = 0;
+                       }
+               }
+               ASSERT(m_pvr_thread);
+               m_pvr_thread->setIFrameSearch(m_skipmode_n != 0);
+               eDebug("flush pvr");
+               flushPVR(m_cue->m_decoding_demux);
+               eDebug("done");
+               break;
+       }
+       case eCueSheet::evtSpanChanged:
+       {
+               m_source_span.clear();
+               for (std::list<std::pair<pts_t, pts_t> >::const_iterator i(m_cue->m_spans.begin()); i != m_cue->m_spans.end(); ++i)
+               {
+                       off_t offset_in, offset_out;
+                       pts_t pts_in = i->first, pts_out = i->second;
+                       if (m_tstools.getOffset(offset_in, pts_in) || m_tstools.getOffset(offset_out, pts_out))
+                       {
+                               eDebug("span translation failed.\n");
+                               continue;
+                       }
+                       eDebug("source span: %llx .. %llx, translated to %llx..%llx", pts_in, pts_out, offset_in, offset_out);
+                       m_source_span.push_back(std::pair<off_t, off_t>(offset_in, offset_out));
+               }
+               break;
+       }
+       }
+}
+
+       /* align toward zero */
+static inline long long align(long long x, int align)
+{
+       int sign = x < 0;
+
+       if (sign)
+               x = -x;
+
+       x -= x % align;
+
+       if (sign)
+               x = -x;
+
+       return x;
+}
+
+       /* remember, this gets called from another thread. */
+void eDVBChannel::getNextSourceSpan(off_t current_offset, size_t bytes_read, off_t &start, size_t &size)
+{
+       const int blocksize = 188;
+       unsigned int max = align(10*1024*1024, blocksize);
+       current_offset = align(current_offset, blocksize);
+       
+       if (!m_cue)
+       {
+               eDebug("no cue sheet. forcing normal play");
+               start = current_offset;
+               size = max;
+               return;
+       }
+
+       m_cue->m_lock.RdLock();
+       if (!m_cue->m_decoding_demux)
+       {
+               start = current_offset;
+               size = max;
+               eDebug("getNextSourceSpan, no decoding demux. forcing normal play");
+               m_cue->m_lock.Unlock();
+               return;
+       }
+
+       if (m_skipmode_n)
+       {
+               eDebug("skipmode %d:%d", m_skipmode_m, m_skipmode_n);
+               max = align(m_skipmode_n, blocksize);
+       }
+
+       eDebug("getNextSourceSpan, current offset is %08llx, m_skipmode_m = %d!", current_offset, m_skipmode_m);
+
+       current_offset += align(m_skipmode_m, blocksize);
+
+       while (!m_cue->m_seek_requests.empty())
+       {
+               std::pair<int, pts_t> seek = m_cue->m_seek_requests.front();
+               m_cue->m_lock.Unlock();
+               m_cue->m_lock.WrLock();
+               m_cue->m_seek_requests.pop_front();
+               m_cue->m_lock.Unlock();
+               m_cue->m_lock.RdLock();
+               int relative = seek.first;
+               pts_t pts = seek.second;
+
+               pts_t now = 0;
+               if (relative)
+               {
+                       if (!m_cue->m_decoder)
+                       {
+                               eDebug("no decoder - can't seek relative");
+                               continue;
+                       }
+                       if (m_cue->m_decoder->getPTS(0, now))
+                       {
+                               eDebug("decoder getPTS failed, can't seek relative");
+                               continue;
+                       }
+                       if (getCurrentPosition(m_cue->m_decoding_demux, now, 1))
+                       {
+                               eDebug("seekTo: getCurrentPosition failed!");
+                               continue;
+                       }
+               } else if (pts < 0) /* seek relative to end */
+               {
+                       pts_t len;
+                       if (!getLength(len))
+                       {
+                               eDebug("seeking relative to end. len=%lld, seek = %lld", len, pts);
+                               pts += len;
+                       } else
+                       {
+                               eWarning("getLength failed - can't seek relative to end!");
+                               continue;
+                       }
+               }
+               
+               if (relative == 1) /* pts relative */
+               {
+                       pts += now;
+                       if (pts < 0)
+                               pts = 0;
+               }
+
+               if (relative != 2)
+                       if (pts < 0)
+                               pts = 0;
+               
+               if (relative == 2) /* AP relative */
+               {
+                       eDebug("AP relative seeking: %lld, at %lld", pts, now);
+                       pts_t nextap;
+                       if (m_tstools.getNextAccessPoint(nextap, now, pts))
+                       {
+                               pts = now - 90000; /* approx. 1s */
+                               eDebug("AP relative seeking failed!");
+                       } else
+                       {
+                               eDebug("next ap is %llx\n", pts);
+                               pts = nextap;
+                       }
+               }
+               
+               off_t offset = 0;
+               if (m_tstools.getOffset(offset, pts))
+               {
+                       eDebug("get offset for pts=%lld failed!", pts);
+                       continue;
+               }
+
+               eDebug("ok, resolved skip (rel: %d, diff %lld), now at %08llx", relative, pts, offset);
+               current_offset = align(offset, blocksize); /* in case tstools return non-aligned offset */
+       }
+
+       m_cue->m_lock.Unlock();
+
+       for (std::list<std::pair<off_t, off_t> >::const_iterator i(m_source_span.begin()); i != m_source_span.end(); ++i)
+       {
+               long long aligned_start = align(i->first, blocksize);
+               long long aligned_end = align(i->second, blocksize);
+       
+               if ((current_offset >= aligned_start) && (current_offset < aligned_end))
+               {
+                       start = current_offset;
+                               /* max can not exceed max(size_t). aligned_end - current_offset, however, can. */
+                       if ((aligned_end - current_offset) > max)
+                               size = max;
+                       else
+                               size = aligned_end - current_offset;
+                       eDebug("HIT, %lld < %lld < %lld, size: %d", i->first, current_offset, i->second, size);
+                       return;
+               }
+               if (current_offset < aligned_start)
+               {
+                               /* ok, our current offset is in an 'out' zone. */
+                       if ((m_skipmode_m >= 0) || (i == m_source_span.begin()))
+                       {
+                                       /* in normal playback, just start at the next zone. */
+                               start = i->first;
+
+                                       /* size is not 64bit! */
+                               if ((i->second - i->first) > max)
+                                       size = max;
+                               else
+                                       size = aligned_end - aligned_start;
+
+                               eDebug("skip");
+                               if (m_skipmode_m < 0)
+                               {
+                                       eDebug("reached SOF");
+                                               /* reached SOF */
+                                       m_skipmode_m = 0;
+                                       m_pvr_thread->sendEvent(eFilePushThread::evtUser);
+                               }
+                       } else
+                       {
+                                       /* when skipping reverse, however, choose the zone before. */
+                               --i;
+                               eDebug("skip to previous block, which is %llx..%llx", i->first, i->second);
+                               size_t len;
+
+                               aligned_start = align(i->first, blocksize);
+                               aligned_end = align(i->second, blocksize);
+
+                               if ((aligned_end - aligned_start) > max)
+                                       len = max;
+                               else
+                                       len = aligned_end - aligned_start;
+
+                               start = aligned_end - len;
+                               eDebug("skipping to %llx, %d", start, len);
+                       }
+
+                       eDebug("result: %llx, %x (%llx %llx)", start, size, aligned_start, aligned_end);
+                       return;
+               }
+       }
+
+       if ((current_offset < -m_skipmode_m) && (m_skipmode_m < 0))
+       {
+               eDebug("reached SOF");
+               m_skipmode_m = 0;
+               m_pvr_thread->sendEvent(eFilePushThread::evtUser);
+       }
+
+       start = current_offset;
+       size = max;
+
+       eDebug("END OF CUESHEET. (%08llx, %d)", start, size);
+       return;
+}
+
 void eDVBChannel::AddUse()
 {
-       ++m_use_count;
+       if (++m_use_count > 1 && m_state == state_last_instance)
+       {
+               m_state = state_ok;
+               m_stateChanged(this);
+       }
 }
 
 void eDVBChannel::ReleaseUse()
@@ -448,9 +1202,14 @@ void eDVBChannel::ReleaseUse()
                m_state = state_release;
                m_stateChanged(this);
        }
+       else if (m_use_count == 1)
+       {
+               m_state = state_last_instance;
+               m_stateChanged(this);
+       }
 }
 
-RESULT eDVBChannel::setChannel(const eDVBChannelID &channelid)
+RESULT eDVBChannel::setChannel(const eDVBChannelID &channelid, ePtr<iDVBFrontendParameters> &feparm)
 {
        if (m_channel_id)
                m_mgr->removeChannel(this);
@@ -458,25 +1217,6 @@ RESULT eDVBChannel::setChannel(const eDVBChannelID &channelid)
        if (!channelid)
                return 0;
 
-       ePtr<iDVBChannelList> list;
-       
-       if (m_mgr->getChannelList(list))
-       {
-               eDebug("no channel list set!");
-               return -ENOENT;
-       }
-       
-       eDebug("tuning to chid: ns: %08x tsid %04x onid %04x",
-               channelid.dvbnamespace.get(), channelid.transport_stream_id.get(), channelid.original_network_id.get());
-
-       ePtr<iDVBFrontendParameters> feparm;
-       if (list->getChannelFrontendData(channelid, feparm))
-       {
-               eDebug("channel not found!");
-               return -ENOENT;
-       }
-       eDebug("allocateChannel: channel found..");
-       
        if (!m_frontend)
        {
                eDebug("no frontend to tune!");
@@ -486,7 +1226,19 @@ RESULT eDVBChannel::setChannel(const eDVBChannelID &channelid)
        m_channel_id = channelid;
        m_mgr->addChannel(channelid, this);
        m_state = state_tuning;
-       return m_frontend->get().tune(*feparm);
+                       /* if tuning fails, shutdown the channel immediately. */
+       int res;
+       res = m_frontend->get().tune(*feparm);
+       m_current_frontend_parameters = feparm;
+       
+       if (res)
+       {
+               m_state = state_release;
+               m_stateChanged(this);
+               return res;
+       }
+       
+       return 0;
 }
 
 RESULT eDVBChannel::connectStateChange(const Slot1<void,iDVBChannel*> &stateChange, ePtr<eConnection> &connection)
@@ -495,6 +1247,12 @@ RESULT eDVBChannel::connectStateChange(const Slot1<void,iDVBChannel*> &stateChan
        return 0;
 }
 
+RESULT eDVBChannel::connectEvent(const Slot2<void,iDVBChannel*,int> &event, ePtr<eConnection> &connection)
+{
+       connection = new eConnection((iDVBChannel*)this, m_event.connect(event));
+       return 0;
+}
+
 RESULT eDVBChannel::getState(int &state)
 {
        state = m_state;
@@ -515,20 +1273,36 @@ RESULT eDVBChannel::getDemux(ePtr<iDVBDemux> &demux, int cap)
                demux = 0;
                
                if (m_mgr->allocateDemux(m_frontend ? (eDVBRegisteredFrontend*)*m_frontend : (eDVBRegisteredFrontend*)0, our_demux, cap))
-                       return 0;
+                       return -1;
        }
        
        demux = *our_demux;
+               /* don't hold a reference to the decoding demux, we don't need it. */
+               
+               /* FIXME: by dropping the 'allocated demux' in favour of the 'iDVBDemux',
+                  the refcount is lost. thus, decoding demuxes are never allocated. 
+                  
+                  this poses a big problem for PiP. */
+       if (cap & capDecode)
+               our_demux = 0;
        return 0;
 }
 
 RESULT eDVBChannel::getFrontend(ePtr<iDVBFrontend> &frontend)
 {
+       frontend = 0;
+       if (!m_frontend)
+               return -ENODEV;
        frontend = &m_frontend->get();
        if (frontend)
                return 0;
-       else
-               return -ENODEV;
+       return -ENODEV;
+}
+
+RESULT eDVBChannel::getCurrentFrontendParameters(ePtr<iDVBFrontendParameters> &param)
+{
+       param = m_current_frontend_parameters;
+       return 0;
 }
 
 RESULT eDVBChannel::playFile(const char *file)
@@ -547,83 +1321,172 @@ RESULT eDVBChannel::playFile(const char *file)
                   THEN DO A REAL FIX HERE! */
        
                /* (this codepath needs to be improved anyway.) */
+#if HAVE_DVB_API_VERSION < 3
+       m_pvr_fd_dst = open("/dev/pvr", O_WRONLY);
+#else
        m_pvr_fd_dst = open("/dev/misc/pvr", O_WRONLY);
+#endif
        if (m_pvr_fd_dst < 0)
        {
-               eDebug("can't open /dev/misc/pvr - you need to buy the new(!) $$$ box! (%m)");
+               eDebug("can't open /dev/misc/pvr - you need to buy the new(!) $$$ box! (%m)"); // or wait for the driver to be improved.
                return -ENODEV;
        }
-       
-       m_pvr_fd_src = open(file, O_RDONLY);
-       if (m_pvr_fd_src < 0)
+
+       m_pvr_thread = new eDVBChannelFilePush();
+       m_pvr_thread->enablePVRCommit(1);
+       m_pvr_thread->setStreamMode(1);
+       m_pvr_thread->setScatterGather(this);
+
+       if (m_pvr_thread->start(file, m_pvr_fd_dst))
        {
-               eDebug("can't open PVR m_pvr_fd_src file %s (%m)", file);
-               close(m_pvr_fd_dst);
+               delete m_pvr_thread;
+               m_pvr_thread = 0;
+               eDebug("can't open PVR file %s (%m)", file);
                return -ENOENT;
        }
-       
+       CONNECT(m_pvr_thread->m_event, eDVBChannel::pvrEvent);
+
        m_state = state_ok;
        m_stateChanged(this);
-       
-       m_pvr_thread = new eFilePushThread();
-       m_pvr_thread->start(m_pvr_fd_src, m_pvr_fd_dst);
 
        return 0;
 }
 
+void eDVBChannel::stopFile()
+{
+       if (m_pvr_thread)
+       {
+               m_pvr_thread->stop();
+               ::close(m_pvr_fd_dst);
+               delete m_pvr_thread;
+               m_pvr_thread = 0;
+       }
+}
+
+void eDVBChannel::setCueSheet(eCueSheet *cuesheet)
+{
+       m_conn_cueSheetEvent = 0;
+       m_cue = cuesheet;
+       if (m_cue)
+               m_cue->connectEvent(slot(*this, &eDVBChannel::cueSheetEvent), m_conn_cueSheetEvent);
+}
+
 RESULT eDVBChannel::getLength(pts_t &len)
 {
        return m_tstools.calcLen(len);
 }
 
-RESULT eDVBChannel::getCurrentPosition(pts_t &pos)
+RESULT eDVBChannel::getCurrentPosition(iDVBDemux *decoding_demux, pts_t &pos, int mode)
 {
-       if (!m_decoder_demux)
+       if (!decoding_demux)
                return -1;
        
-       off_t begin = 0;
-               /* getPTS for offset 0 is cached, so it doesn't harm. */
-       int r = m_tstools.getPTS(begin, pos);
-       if (r)
-       {
-               eDebug("tstools getpts(0) failed!");
-               return r;
-       }
-       
        pts_t now;
        
-       r = m_decoder_demux->get().getSTC(now);
-
-       if (r)
+       int r;
+       
+       if (mode == 0) /* demux */
        {
-               eDebug("demux getSTC failed");
-               return -1;
-       }
+               r = decoding_demux->getSTC(now, 0);
+               if (r)
+               {
+                       eDebug("demux getSTC failed");
+                       return -1;
+               }
+       } else
+               now = pos; /* fixup supplied */
        
-//     eDebug("STC: %08llx PTS: %08llx, diff %lld", now, pos, now - pos);
-               /* when we are less than 10 seconds before the start, return 0. */
-               /* (we're just waiting for the timespam to start) */
-       if ((now < pos) && ((pos - now) < 90000 * 10))
+       off_t off = 0; /* TODO: fixme */
+       r = m_tstools.fixupPTS(off, now);
+       if (r)
        {
-               pos = 0;
-               return 0;
+               eDebug("fixup PTS failed");
+               return -1;
        }
        
-       if (now < pos) /* wrap around */
-               pos = now + ((pts_t)1)<<33 - pos;
-       else
-               pos = now - pos;
+       pos = now;
        
        return 0;
 }
 
-RESULT eDVBChannel::seekTo(pts_t &pts)
+void eDVBChannel::flushPVR(iDVBDemux *decoding_demux)
 {
+                       /* when seeking, we have to ensure that all buffers are flushed.
+                          there are basically 3 buffers:
+                          a.) the filepush's internal buffer
+                          b.) the PVR buffer (before demux)
+                          c.) the ratebuffer (after demux)
+                          
+                          it's important to clear them in the correct order, otherwise
+                          the ratebuffer (for example) would immediately refill from
+                          the not-yet-flushed PVR buffer.
+                       */
+
        m_pvr_thread->pause();
-       if (m_decoder_demux)
-               m_decoder_demux->get().flush();
+               /* flush internal filepush buffer */
+       m_pvr_thread->flush();
+               /* HACK: flush PVR buffer */
+       ::ioctl(m_pvr_fd_dst, 0);
+       
+               /* flush ratebuffers (video, audio) */
+       if (decoding_demux)
+               decoding_demux->flush();
+
                /* demux will also flush all decoder.. */
-//     m_pvr_thread->seek(pts);
+               /* resume will re-query the SG */
        m_pvr_thread->resume();
 }
 
+DEFINE_REF(eCueSheet);
+
+eCueSheet::eCueSheet()
+{
+       m_skipmode_ratio = 0;
+}
+
+void eCueSheet::seekTo(int relative, const pts_t &pts)
+{
+       m_lock.WrLock();
+       m_seek_requests.push_back(std::pair<int, pts_t>(relative, pts));
+       m_lock.Unlock();
+       m_event(evtSeek);
+}
+       
+void eCueSheet::clear()
+{
+       m_lock.WrLock();
+       m_spans.clear();
+       m_lock.Unlock();
+}
+
+void eCueSheet::addSourceSpan(const pts_t &begin, const pts_t &end)
+{
+       m_lock.WrLock();
+       m_spans.push_back(std::pair<pts_t, pts_t>(begin, end));
+       m_lock.Unlock();
+}
+
+void eCueSheet::commitSpans()
+{
+       m_event(evtSpanChanged);
+}
+
+void eCueSheet::setSkipmode(const pts_t &ratio)
+{
+       m_lock.WrLock();
+       m_skipmode_ratio = ratio;
+       m_lock.Unlock();
+       m_event(evtSkipmode);
+}
+
+void eCueSheet::setDecodingDemux(iDVBDemux *demux, iTSMPEGDecoder *decoder)
+{
+       m_decoding_demux = demux;
+       m_decoder = decoder;
+}
+
+RESULT eCueSheet::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
+{
+       connection = new eConnection(this, m_event.connect(event));
+       return 0;
+}