lib/base/etpm.h,cpp: simplify and cleanup code
[enigma2.git] / lib / dvb / scan.cpp
index 2a08bf79701d0ad7136683bfea48d95e3c6c010e..b37aa7141d106ddc38c74f4b7c137a222eb67f8b 100644 (file)
@@ -5,12 +5,14 @@
 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
 #include <dvbsi++/cable_delivery_system_descriptor.h>
 #include <dvbsi++/ca_identifier_descriptor.h>
+#include <dvbsi++/registration_descriptor.h>
 #include <lib/dvb/specs.h>
 #include <lib/dvb/esection.h>
 #include <lib/dvb/scan.h>
 #include <lib/dvb/frontend.h>
 #include <lib/base/eerror.h>
 #include <lib/base/estring.h>
+#include <lib/python/python.h>
 #include <errno.h>
 
 #define SCAN_eDebug(x...) do { if (m_scan_debug) eDebug(x); } while(0)
@@ -22,33 +24,118 @@ eDVBScan::eDVBScan(iDVBChannel *channel, bool usePAT, bool debug)
        :m_channel(channel), m_channel_state(iDVBChannel::state_idle)
        ,m_ready(0), m_ready_all(usePAT ? (readySDT|readyPAT) : readySDT)
        ,m_pmt_running(false), m_abort_current_pmt(false), m_flags(0)
-       ,m_usePAT(usePAT), m_scan_debug(debug)
+       ,m_usePAT(usePAT), m_scan_debug(debug), m_show_add_tsid_onid_check_failed_msg(true)
 {
        if (m_channel->getDemux(m_demux))
                SCAN_eDebug("scan: failed to allocate demux!");
        m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
+       FILE *f = fopen("/etc/enigma2/scan_tp_valid_check.py", "r");
+       if (f)
+       {
+               char code[16384];
+               size_t rd = fread(code, 1, 16383, f);
+               if (rd)
+               {
+                       code[rd]=0;
+                       m_additional_tsid_onid_check_func = Py_CompileString(code, "/etc/enigma2/scan_tp_valid_check.py", Py_file_input);
+               }
+               fclose(f);
+       }
 }
 
 eDVBScan::~eDVBScan()
 {
+       if (m_additional_tsid_onid_check_func)
+               Py_DECREF(m_additional_tsid_onid_check_func);
 }
 
 int eDVBScan::isValidONIDTSID(int orbital_position, eOriginalNetworkID onid, eTransportStreamID tsid)
 {
+       int ret;
        switch (onid.get())
        {
        case 0:
        case 0x1111:
-               return 0;
+               ret=0;
+               break;
+       case 0x13E:  // workaround for 11258H and 11470V on hotbird with same ONID/TSID (0x13E/0x578)
+               ret = orbital_position != 130 || tsid != 0x578;
+               break;
        case 1:
-               return orbital_position == 192;
+               ret = orbital_position == 192;
+               break;
        case 0x00B1:
-               return tsid != 0x00B0;
+               ret = tsid != 0x00B0;
+               break;
+       case 0x00eb:
+               ret = tsid != 0x4321;
+               break;
        case 0x0002:
-               return abs(orbital_position-282) < 6;
+               ret = abs(orbital_position-282) < 6 && tsid != 2019;
+               // 12070H and 10936V have same tsid/onid.. but even the same services are provided
+               break;
+       case 0x2000:
+               ret = tsid != 0x1000;
+               break;
+       case 0x5E: // Sirius 4.8E 12322V and 12226H
+               ret = abs(orbital_position-48) < 3 && tsid != 1;
+               break;
+       case 10100: // Eutelsat W7 36.0E 11644V and 11652V
+               ret = orbital_position != 360 || tsid != 10187;
+               break;
+       case 42: // Tuerksat 42.0E
+               ret = orbital_position != 420 || (
+                   tsid != 8 && // 11830V 12729V
+                   tsid != 5 && // 12679V 12685H
+                   tsid != 2 && // 11096V 12015H
+                   tsid != 55); // 11996V 11716V
+               break;
+       case 100: // Intelsat 10 68.5E 3808V 3796V 4012V, Amos 4.0W 10723V 11571H
+               ret = (orbital_position != 685 && orbital_position != 3560) || tsid != 1;
+               break;
+       case 70: // Thor 0.8W 11862H 12341V
+               ret = abs(orbital_position-3592) < 3 && tsid != 46;
+               break;
+       case 32: // NSS 806 (40.5W) 4059R, 3774L
+               ret = orbital_position != 3195 || tsid != 21;
+               break;
        default:
-               return onid.get() < 0xFF00;
+               ret = onid.get() < 0xFF00;
+               break;
        }
+       if (ret && m_additional_tsid_onid_check_func)
+       {
+               bool failed = true;
+               ePyObject dict = PyDict_New();
+               extern void PutToDict(ePyObject &, const char *, long);
+               PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins());
+               PutToDict(dict, "orbpos", orbital_position);
+               PutToDict(dict, "tsid", tsid.get());
+               PutToDict(dict, "onid", onid.get());
+               ePyObject r = PyEval_EvalCode((PyCodeObject*)(PyObject*)m_additional_tsid_onid_check_func, dict, dict);
+               if (r)
+               {
+                       ePyObject o = PyDict_GetItemString(dict, "ret");
+                       if (o)
+                       {
+                               if (PyInt_Check(o))
+                               {
+                                       ret = PyInt_AsLong(o);
+                                       failed = false;
+                               }
+                       }
+                       Py_DECREF(r);
+               }
+               if (failed && m_show_add_tsid_onid_check_failed_msg)
+               {
+                       eDebug("execing /etc/enigma2/scan_tp_valid_check failed!\n"
+                               "usable global variables in scan_tp_valid_check.py are 'orbpos', 'tsid', 'onid'\n"
+                               "the return value must be stored in a global var named 'ret'");
+                       m_show_add_tsid_onid_check_failed_msg=false;
+               }
+               Py_DECREF(dict);
+       }
+       return ret;
 }
 
 eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
@@ -135,7 +222,7 @@ RESULT eDVBScan::nextChannel()
 RESULT eDVBScan::startFilter()
 {
        bool startSDT=true;
-       assert(m_demux);
+       ASSERT(m_demux);
 
                        /* only start required filters filter */
 
@@ -161,7 +248,7 @@ RESULT eDVBScan::startFilter()
                {
                        std::vector<ProgramAssociationSection*>::const_iterator i =
                                m_PAT->getSections().begin();
-                       assert(i != m_PAT->getSections().end());
+                       ASSERT(i != m_PAT->getSections().end());
                        tsid = (*i)->getTableIdExtension(); // in PAT this is the transport stream id
                        m_pat_tsid = eTransportStreamID(tsid);
                        for (; i != m_PAT->getSections().end(); ++i)
@@ -205,7 +292,7 @@ RESULT eDVBScan::startFilter()
                if (m_ready_all & readyPAT)
                {
                        m_PAT = new eTable<ProgramAssociationSection>(m_scan_debug);
-                       if (m_PAT->start(m_demux, eDVBPATSpec()))
+                       if (m_PAT->start(m_demux, eDVBPATSpec(4000)))
                                return -1;
                        CONNECT(m_PAT->tableReady, eDVBScan::PATready);
                }
@@ -277,6 +364,7 @@ void eDVBScan::PMTready(int err)
                bool have_video = false;
                unsigned short pcrpid = 0xFFFF;
                std::vector<ProgramMapSection*>::const_iterator i;
+
                for (i = m_PMT->getSections().begin(); i != m_PMT->getSections().end(); ++i)
                {
                        const ProgramMapSection &pmt = **i;
@@ -287,64 +375,82 @@ void eDVBScan::PMTready(int err)
                        ElementaryStreamInfoConstIterator es;
                        for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
                        {
-                               int isaudio = 0, isvideo = 0, is_scrambled = 0;
+                               int isaudio = 0, isvideo = 0, is_scrambled = 0, forced_audio = 0, forced_video = 0;
                                switch ((*es)->getType())
                                {
                                case 0x1b: // AVC Video Stream (MPEG4 H264)
+                               case 0x10: // MPEG 4 Part 2
                                case 0x01: // MPEG 1 video
                                case 0x02: // MPEG 2 video
                                        isvideo = 1;
+                                       forced_video = 1;
                                        //break; fall through !!!
                                case 0x03: // MPEG 1 audio
-                               case 0x04: // MPEG 2 audio:
-                                       if (!isvideo)
+                               case 0x04: // MPEG 2 audio
+                               case 0x0f: // MPEG 2 AAC
+                               case 0x11: // MPEG 4 AAC
+                                       if (!isvideo) 
+                                       {
+                                               forced_audio = 1;
                                                isaudio = 1;
-                                       //break; fall through !!!
+                                       }
                                case 0x06: // PES Private
                                case 0x81: // user private
-                                               /* PES private can contain AC-3, DTS or lots of other stuff.
-                                                  check descriptors to get the exact type. */
+                               case 0xEA: // TS_PSI_ST_SMPTE_VC1
                                        for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
                                                        desc != (*es)->getDescriptors()->end(); ++desc)
                                        {
                                                uint8_t tag = (*desc)->getTag();
-                                               if (!isaudio && !isvideo)
+                                               /* PES private can contain AC-3, DTS or lots of other stuff.
+                                                  check descriptors to get the exakt type. */
+                                               if (!forced_video && !forced_audio)
                                                {
                                                        switch (tag)
                                                        {
-                                                       case DTS_DESCRIPTOR:
+                                                       case 0x1C: // TS_PSI_DT_MPEG4_Audio
+                                                       case 0x2B: // TS_PSI_DT_MPEG2_AAC
                                                        case AAC_DESCRIPTOR:
                                                        case AC3_DESCRIPTOR:
+                                                       case DTS_DESCRIPTOR:
+                                                       case AUDIO_STREAM_DESCRIPTOR:
                                                                isaudio = 1;
                                                                break;
+                                                       case 0x28: // TS_PSI_DT_AVC
+                                                       case 0x1B: // TS_PSI_DT_MPEG4_Video
+                                                       case VIDEO_STREAM_DESCRIPTOR:
+                                                               isvideo = 1;
+                                                               break;
                                                        case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
                                                        {
-                                                                       /* libdvbsi++ doesn't yet support this descriptor type, so work around. */
-                                                               if ((*desc)->getLength() != 4)
-                                                                       break;
-                                                               unsigned char descr[6];
-                                                               (*desc)->writeToBuffer(descr);
-                                                               int format_identifier = (descr[2] << 24) | (descr[3] << 16) | (descr[4] << 8) | (descr[5]);
-                                                               switch (format_identifier)
+                                                               RegistrationDescriptor *d = (RegistrationDescriptor*)(*desc);
+                                                               switch (d->getFormatIdentifier())
                                                                {
-                                                               case 0x41432d33:
+                                                               case 0x44545331 ... 0x44545333: // DTS1/DTS2/DTS3
+                                                               case 0x41432d33: // == 'AC-3'
+                                                               case 0x42535344: // == 'BSSD' (LPCM)
                                                                        isaudio = 1;
+                                                                       break;
+                                                               case 0x56432d31: // == 'VC-1'
+                                                                       isvideo = 1;
+                                                                       break;
                                                                default:
                                                                        break;
                                                                }
-                                                               break;
                                                        }
+                                                       default:
+                                                               break;
                                                        }
                                                }
                                                if (tag == CA_DESCRIPTOR)
                                                        is_scrambled = 1;
                                        }
+                               default:
                                        break;
                                }
-                               if (isaudio)
-                                       have_audio = true;
-                               else if (isvideo)
+                               if (isvideo)
                                        have_video = true;
+                               else if (isaudio)
+                                       have_audio = true;
                                else
                                        continue;
                                if (is_scrambled)
@@ -376,7 +482,7 @@ void eDVBScan::PMTready(int err)
        }
 
        if (m_pmt_in_progress != m_pmts_to_read.end())
-               m_PMT->start(m_demux, eDVBPMTSpec(m_pmt_in_progress->second.pmtPid, m_pmt_in_progress->first));
+               m_PMT->start(m_demux, eDVBPMTSpec(m_pmt_in_progress->second.pmtPid, m_pmt_in_progress->first, 4000));
        else
        {
                m_PMT = 0;
@@ -682,9 +788,12 @@ void eDVBScan::channelDone()
                   here, and not before.
                */
 
+       int type;
+       if (m_ch_current->getSystem(type))
+               type = -1;
+
        for (m_pmt_in_progress = m_pmts_to_read.begin(); m_pmt_in_progress != m_pmts_to_read.end();)
        {
-               int type;
                eServiceReferenceDVB ref;
                ePtr<eDVBService> service = new eDVBService;
 
@@ -710,7 +819,7 @@ void eDVBScan::channelDone()
                ref.setServiceID(m_pmt_in_progress->first);
                ref.setServiceType(m_pmt_in_progress->second.serviceType);
 
-               if (!m_ch_current->getSystem(type))
+               if (type != -1)
                {
                        char sname[255];
                        char pname[255];
@@ -758,12 +867,13 @@ void eDVBScan::channelDone()
                        SCAN_eDebug("name '%s', provider_name '%s'", sname, pname);
                        service->m_service_name = convertDVBUTF8(sname);
                        service->genSortName();
-                       service->m_provider_name = pname;
+                       service->m_provider_name = convertDVBUTF8(pname);
                }
 
                if (!(m_flags & scanOnlyFree) || !m_pmt_in_progress->second.scrambled) {
                        SCAN_eDebug("add not scrambled!");
-                       std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i = m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
+                       std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i =
+                               m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
                        if (i.second)
                        {
                                m_last_service = i.first;
@@ -778,10 +888,36 @@ void eDVBScan::channelDone()
        if (!m_chid_current)
                eWarning("SCAN: the current channel's ID was not corrected - not adding channel.");
        else
+       {
                addKnownGoodChannel(m_chid_current, m_ch_current);
-       
+               if (m_chid_current)
+               {
+                       switch(type)
+                       {
+                               case iDVBFrontend::feSatellite:
+                               case iDVBFrontend::feTerrestrial:
+                               case iDVBFrontend::feCable:
+                               {
+                                       ePtr<iDVBFrontend> fe;
+                                       if (!m_channel->getFrontend(fe))
+                                       {
+                                               ePyObject tp_dict = PyDict_New();
+                                               fe->getTransponderData(tp_dict, false);
+//                                             eDebug("add tuner data for tsid %04x, onid %04x, ns %08x",
+//                                                     m_chid_current.transport_stream_id.get(), m_chid_current.original_network_id.get(),
+//                                                     m_chid_current.dvbnamespace.get());
+                                               m_tuner_data.insert(std::pair<eDVBChannelID, ePyObjectWrapper>(m_chid_current, tp_dict));
+                                               Py_DECREF(tp_dict);
+                                       }
+                               }
+                               default:
+                                       break;
+                       }
+               }
+       }
+
        m_ch_scanned.push_back(m_ch_current);
-       
+
        for (std::list<ePtr<iDVBFrontendParameters> >::iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end();)
        {
                if (sameChannel(*i, m_ch_current))
@@ -803,6 +939,7 @@ void eDVBScan::start(const eSmartPtrList<iDVBFrontendParameters> &known_transpon
        m_ch_scanned.clear();
        m_ch_unavailable.clear();
        m_new_channels.clear();
+       m_tuner_data.clear();
        m_new_services.clear();
        m_last_service = m_new_services.end();
 
@@ -920,11 +1057,36 @@ void eDVBScan::insertInto(iDVBChannelList *db, bool dontRemoveOldFlags)
        for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
                        ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
        {
+               int system;
+               ch->second->getSystem(system);
+               std::map<eDVBChannelID, ePyObjectWrapper>::iterator it = m_tuner_data.find(ch->first);
+
+               switch(system)
+               {
+                       case iDVBFrontend::feTerrestrial:
+                       {
+                               eDVBFrontendParameters *p = (eDVBFrontendParameters*)&(*ch->second);
+                               eDVBFrontendParametersTerrestrial parm;
+                               int freq = PyInt_AsLong(PyDict_GetItemString(it->second, "frequency"));
+                               p->getDVBT(parm);
+//                             eDebug("corrected freq for tsid %04x, onid %04x, ns %08x is %d, old was %d",
+//                                     ch->first.transport_stream_id.get(), ch->first.original_network_id.get(),
+//                                     ch->first.dvbnamespace.get(), freq, parm.frequency);
+                               parm.frequency = freq;
+                               p->setDVBT(parm);
+                               break;
+                       }
+                       case iDVBFrontend::feSatellite: // no update of any transponder parameter yet
+                       case iDVBFrontend::feCable:
+                               break;
+               }
+
                if (m_flags & scanOnlyFree)
                {
                        eDVBFrontendParameters *ptr = (eDVBFrontendParameters*)&(*ch->second);
                        ptr->setFlags(iDVBFrontendParameters::flagOnlyFree);
                }
+
                db->addChannelToList(ch->first, ch->second);
        }
 
@@ -1034,7 +1196,8 @@ RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescription
                                }
                        }
 
-                       std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i = m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
+                       std::pair<std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator, bool> i =
+                               m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
 
                        if (i.second)
                        {