update dvbapp.
[vuplus_dvbapp] / lib / dvb / dvb.cpp
index a1fe247..0a371f0 100755 (executable)
@@ -6,6 +6,8 @@
 #include <lib/dvb/sec.h>
 #include <lib/dvb/specs.h>
 
+#include <lib/dvb/fbc.h>
+
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -26,6 +28,14 @@ eDVBAllocatedFrontend::eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe): m_fe(f
 eDVBAllocatedFrontend::~eDVBAllocatedFrontend()
 {
        m_fe->dec_use();
+       if (m_fe->m_frontend->is_FBCTuner())
+       {
+               eFBCTunerManager* fbcmng = eFBCTunerManager::getInstance();
+               if (fbcmng)
+               {
+                       fbcmng->unset(m_fe);
+               }
+       }
 }
 
 DEFINE_REF(eDVBAllocatedDemux);
@@ -270,14 +280,12 @@ void eDVBResourceManager::addAdapter(iDVBAdapter *adapter)
                ePtr<eDVBFrontend> frontend;
                if (!adapter->getFrontend(frontend, i))
                {
-                       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 (frontend->supportsDeliverySystem(SYS_DVBT, false) || frontend->supportsDeliverySystem(SYS_DVBT2, false))
                        {
                                if (prev_dvbt_frontend)
                                {
@@ -295,14 +303,12 @@ void eDVBResourceManager::addAdapter(iDVBAdapter *adapter)
                ePtr<eDVBFrontend> frontend;
                if (!adapter->getFrontend(frontend, i, true))
                {
-                       int frontendType=0;
-                       frontend->getFrontendType(frontendType);
                        eDVBRegisteredFrontend *new_fe = new eDVBRegisteredFrontend(frontend, adapter);
 //                     CONNECT(new_fe->stateChanged, eDVBResourceManager::feStateChanged);
                        m_simulate_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 (frontend->supportsDeliverySystem(SYS_DVBT, false) || frontend->supportsDeliverySystem(SYS_DVBT2, false))
                        {
                                if (prev_dvbt_frontend)
                                {
@@ -355,16 +361,102 @@ PyObject *eDVBResourceManager::setFrontendSlotInformations(ePyObject list)
        Py_RETURN_NONE;
 }
 
+bool eDVBResourceManager::frontendIsCompatible(int index, const char *type)
+{
+       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+       {
+               if (i->m_frontend->getSlotID() == index)
+               {
+                       if (!strcmp(type, "DVB-S2"))
+                       {
+                               return i->m_frontend->supportsDeliverySystem(SYS_DVBS2, false);
+                       }
+                       else if (!strcmp(type, "DVB-S"))
+                       {
+                               return i->m_frontend->supportsDeliverySystem(SYS_DVBS, false);
+                       }
+                       else if (!strcmp(type, "DVB-T2"))
+                       {
+                               return i->m_frontend->supportsDeliverySystem(SYS_DVBT2, false);
+                       }
+                       else if (!strcmp(type, "DVB-T"))
+                       {
+                               return i->m_frontend->supportsDeliverySystem(SYS_DVBT, false);
+                       }
+                       else if (!strcmp(type, "DVB-C"))
+                       {
+#if DVB_API_VERSION > 5 || DVB_API_VERSION == 5 && DVB_API_VERSION_MINOR >= 6
+                               return i->m_frontend->supportsDeliverySystem(SYS_DVBC_ANNEX_A, false);
+#else
+                               return i->m_frontend->supportsDeliverySystem(SYS_DVBC_ANNEX_AC, false);
+#endif
+                       }
+               }
+       }
+       return false;
+}
+
+void eDVBResourceManager::setFrontendType(int index, const char *type)
+{
+       eDebug("[eDVBResourceManager::setFrontendType] index : %d, type : %s", index, type);
+
+       for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
+       {
+               if (i->m_frontend->getSlotID() == index)
+               {
+                       std::vector<fe_delivery_system_t> whitelist;
+                       if (!strcmp(type, "DVB-S2") || !strcmp(type, "DVB-S"))
+                       {
+                               whitelist.push_back(SYS_DVBS);
+                               whitelist.push_back(SYS_DVBS2);
+                       }
+                       else if (!strcmp(type, "DVB-T2") || !strcmp(type, "DVB-T"))
+                       {
+                               whitelist.push_back(SYS_DVBT);
+                               whitelist.push_back(SYS_DVBT2);
+                       }
+                       else if (!strcmp(type, "DVB-C"))
+                       {
+#if DVB_API_VERSION > 5 || DVB_API_VERSION == 5 && DVB_API_VERSION_MINOR >= 6
+                               whitelist.push_back(SYS_DVBC_ANNEX_A);
+#else
+                               whitelist.push_back(SYS_DVBC_ANNEX_AC);
+#endif
+                       }
+                       i->m_frontend->setDeliverySystemWhitelist(whitelist);
+                       break;
+               }
+       }
+}
+
 RESULT eDVBResourceManager::allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm, bool simulate)
 {
        eSmartPtrList<eDVBRegisteredFrontend> &frontends = simulate ? m_simulate_frontend : m_frontend;
-       ePtr<eDVBRegisteredFrontend> best;
+//     ePtr<eDVBRegisteredFrontend> best;
+       eDVBRegisteredFrontend *best = NULL;
        int bestval = 0;
        int foundone = 0;
 
+       int check_fbc_linked = 0;
+       eDVBRegisteredFrontend *fbc_fe = NULL;
+       eDVBRegisteredFrontend *best_fbc_fe = NULL;
+       eFBCTunerManager* fbcmng = eFBCTunerManager::getInstance();
+
        for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(frontends.begin()); i != frontends.end(); ++i)
        {
-               int c = i->m_frontend->isCompatibleWith(feparm);
+               int c = 0;
+               fbc_fe = NULL;
+               if (!check_fbc_linked && i->m_frontend->is_FBCTuner() && fbcmng && fbcmng->canLink(*i))
+               {
+                       check_fbc_linked = 1;
+                       c = fbcmng->isCompatibleWith(feparm, *i, fbc_fe, simulate);
+
+//                     eDebug("[eDVBResourceManager::allocateFrontend] fbcmng->isCompatibleWith slotid : %p (%d), fbc_fe : %p (%d), score : %d", (eDVBRegisteredFrontend *)*i,  i->m_frontend->getSlotID(), fbc_fe, fbc_fe?fbc_fe->m_frontend->getSlotID():-1, c);                     
+               }
+               else
+               {
+                       c = i->m_frontend->isCompatibleWith(feparm);
+               }
 
                if (c)  /* if we have at least one frontend which is compatible with the source, flag this. */
                        foundone = 1;
@@ -375,15 +467,26 @@ RESULT eDVBResourceManager::allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, eP
                        if (c > bestval)
                        {
                                bestval = c;
-                               best = i;
+//                             best = i;
+                               best = *i;
+                               best_fbc_fe = fbc_fe;
                        }
                }
-//             else
+               else
+               {
 //                     eDebug("Slot %d, score %d... but BUSY!!!!!!!!!!!", i->m_frontend->getSlotID(), c);
+               }
+
+               eDVBRegisteredFrontend *tmp = *i;
        }
 
        if (best)
        {
+               if (fbcmng && best_fbc_fe)
+               {
+                       fbcmng->addLink(best, best_fbc_fe, simulate);
+               }
+
                fe = new eDVBAllocatedFrontend(best);
                return 0;
        }
@@ -603,12 +706,6 @@ RESULT eDVBResourceManager::getChannelList(ePtr<iDVBChannelList> &list)
                if (!simulate) \
                        eDebug(x); \
        } while(0)
-//             else \
-//             { \
-//                     eDebugNoNewLine("SIMULATE:"); \
-//                     eDebug(x); \
-//             } \
-
 
 RESULT eDVBResourceManager::allocateChannel(const eDVBChannelID &channelid, eUsePtr<iDVBChannel> &channel, bool simulate)
 {
@@ -813,28 +910,41 @@ int eDVBResourceManager::canAllocateFrontend(ePtr<iDVBFrontendParameters> &fepar
        eSmartPtrList<eDVBRegisteredFrontend> &frontends = simulate ? m_simulate_frontend : m_frontend;
        ePtr<eDVBRegisteredFrontend> best;
        int bestval = 0;
+       int check_fbc_link = 0;
+       eFBCTunerManager *fbcmng = eFBCTunerManager::getInstance();
 
        for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(frontends.begin()); i != frontends.end(); ++i)
+       {
                if (!i->m_inuse)
                {
-                       int c = i->m_frontend->isCompatibleWith(feparm);
+                       int c = 0;
+                       if(fbcmng && i->m_frontend->is_FBCTuner() && fbcmng->canLink(*i) && !check_fbc_link)
+                       {
+                               check_fbc_link = 1;
+                               c = fbcmng->isCompatibleWith(feparm, *i, simulate);
+                       }
+                       else
+                       {
+                               c = i->m_frontend->isCompatibleWith(feparm);
+                       }
                        if (c > bestval)
                                bestval = c;
                }
+       }
        return bestval;
 }
 
-int tuner_type_channel_default(ePtr<iDVBChannelList> &channellist, const eDVBChannelID &chid)
+int tuner_type_channel_default(ePtr<iDVBChannelList> &channellist, const eDVBChannelID &chid, int &system)
 {
+       system = iDVBFrontend::feSatellite;
        if (channellist)
        {
                ePtr<iDVBFrontendParameters> feparm;
                if (!channellist->getChannelFrontendData(chid, feparm))
                {
-                       int system;
                        if (!feparm->getSystem(system))
                        {
-                               switch(system)
+                               switch (system)
                                {
                                        case iDVBFrontend::feSatellite:
                                                return 50000;
@@ -851,15 +961,16 @@ int tuner_type_channel_default(ePtr<iDVBChannelList> &channellist, const eDVBCha
        return 0;
 }
 
-int eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID& ignore, bool simulate)
+int eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, const eDVBChannelID& ignore, int &system, bool simulate)
 {
        std::list<active_channel> &active_channels = simulate ? m_active_simulate_channels : m_active_channels;
-       int ret=0;
+       int ret = 0;
+       system = iDVBFrontend::feSatellite;
        if (!simulate && m_cached_channel)
        {
                eDVBChannel *cache_chan = (eDVBChannel*)&(*m_cached_channel);
                if(channelid==cache_chan->getChannelID())
-                       return tuner_type_channel_default(m_list, channelid);
+                       return tuner_type_channel_default(m_list, channelid, system);
        }
 
                /* first, check if a channel is already existing. */
@@ -870,7 +981,7 @@ int eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, cons
                if (i->m_channel_id == channelid)
                {
 //                     eDebug("found shared channel..");
-                       return tuner_type_channel_default(m_list, channelid);
+                       return tuner_type_channel_default(m_list, channelid, system);
                }
        }
 
@@ -950,6 +1061,7 @@ int eDVBResourceManager::canAllocateChannel(const eDVBChannelID &channelid, cons
                eDebug("channel not found!");
                goto error;
        }
+       feparm->getSystem(system);
 
        ret = canAllocateFrontend(feparm, simulate);
 
@@ -1213,7 +1325,7 @@ void eDVBChannel::frontendStateChanged(iDVBFrontend*fe)
                }
        } else if (state == iDVBFrontend::stateFailed)
        {
-#ifdef BUILD_VUPLUS /* ikseong  */
+#ifdef BUILD_VUPLUS
                if (m_current_frontend_parameters)
                {
                        eDebug("OURSTATE: lost lock, trying to retune");
@@ -1830,7 +1942,7 @@ RESULT eDVBChannel::playSource(ePtr<iTsSource> &source, const char *streaminfo_f
                m_pvr_thread = 0;
        }
 
-       if (!source->valid())
+       if (!source->valid() && !source->isStream())
        {
                eDebug("PVR source is not valid!");
                return -ENOENT;
@@ -1873,7 +1985,7 @@ RESULT eDVBChannel::playSource(ePtr<iTsSource> &source, const char *streaminfo_f
        m_pvr_thread = new eDVBChannelFilePush();
        m_pvr_thread->enablePVRCommit(1);
        /* If the source specifies a length, it's a file. If not, it's a stream */
-       m_pvr_thread->setStreamMode(source->length() <= 0);
+       m_pvr_thread->setStreamMode(source->isStream());
        m_pvr_thread->setScatterGather(this);
 
        m_event(this, evtPreStart);