properly escape provider name. fixes #54.
[enigma2.git] / lib / dvb / scan.cpp
index 3c4f407bfe576a0cfaac3f31cfd29d1de4531c53..71893f95ef1dc58fa5d65194fadc52397909a4a1 100644 (file)
 #include <lib/base/estring.h>
 #include <errno.h>
 
-static bool scan_debug;
-#define SCAN_eDebug(x...) do { if (scan_debug) eDebug(x); } while(0)
-#define SCAN_eDebugNoNewLine(x...) do { if (scan_debug) eDebugNoNewLine(x); } while(0)
+#define SCAN_eDebug(x...) do { if (m_scan_debug) eDebug(x); } while(0)
+#define SCAN_eDebugNoNewLine(x...) do { if (m_scan_debug) eDebugNoNewLine(x); } while(0)
 
 DEFINE_REF(eDVBScan);
 
 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_pmt_running(false), m_abort_current_pmt(false), m_flags(0)
+       ,m_usePAT(usePAT), m_scan_debug(debug)
 {
-       scan_debug=debug;
        if (m_channel->getDemux(m_demux))
                SCAN_eDebug("scan: failed to allocate demux!");
        m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
@@ -143,10 +142,20 @@ RESULT eDVBScan::startFilter()
        if (m_ready_all & readyPAT)
                startSDT = m_ready & readyPAT;
 
+       // m_ch_current is not set, when eDVBScan is just used for a SDT update
+       if (!m_ch_current)
+       {
+               unsigned int channelFlags;
+               m_channel->getCurrentFrontendParameters(m_ch_current);
+               m_ch_current->getFlags(channelFlags);
+               if (channelFlags & iDVBFrontendParameters::flagOnlyFree)
+                       m_flags |= scanOnlyFree;
+       }
+
        m_SDT = 0;
        if (startSDT && (m_ready_all & readySDT))
        {
-               m_SDT = new eTable<ServiceDescriptionSection>();
+               m_SDT = new eTable<ServiceDescriptionSection>(m_scan_debug);
                int tsid=-1;
                if (m_ready & readyPAT && m_ready & validPAT)
                {
@@ -162,11 +171,11 @@ RESULT eDVBScan::startFilter()
                                for (; program != pat.getPrograms()->end(); ++program)
                                        m_pmts_to_read.insert(std::pair<unsigned short, service>((*program)->getProgramNumber(), service((*program)->getProgramMapPid())));
                        }
-                       m_PMT = new eTable<ProgramMapSection>();
+                       m_PMT = new eTable<ProgramMapSection>(m_scan_debug);
                        CONNECT(m_PMT->tableReady, eDVBScan::PMTready);
-                       PMTready(-1);
-
+                       PMTready(-2);
                        // KabelBW HACK ... on 618Mhz and 626Mhz the transport stream id in PAT and SDT is different
+
                        {
                                int type;
                                m_ch_current->getSystem(type);
@@ -195,8 +204,8 @@ RESULT eDVBScan::startFilter()
                m_PAT = 0;
                if (m_ready_all & readyPAT)
                {
-                       m_PAT = new eTable<ProgramAssociationSection>();
-                       if (m_PAT->start(m_demux, eDVBPATSpec()))
+                       m_PAT = new eTable<ProgramAssociationSection>(m_scan_debug);
+                       if (m_PAT->start(m_demux, eDVBPATSpec(4000)))
                                return -1;
                        CONNECT(m_PAT->tableReady, eDVBScan::PATready);
                }
@@ -204,7 +213,7 @@ RESULT eDVBScan::startFilter()
                m_NIT = 0;
                if (m_ready_all & readyNIT)
                {
-                       m_NIT = new eTable<NetworkInformationSection>();
+                       m_NIT = new eTable<NetworkInformationSection>(m_scan_debug);
                        if (m_NIT->start(m_demux, eDVBNITSpec()))
                                return -1;
                        CONNECT(m_NIT->tableReady, eDVBScan::NITready);
@@ -213,13 +222,12 @@ RESULT eDVBScan::startFilter()
                m_BAT = 0;
                if (m_ready_all & readyBAT)
                {
-                       m_BAT = new eTable<BouquetAssociationSection>();
+                       m_BAT = new eTable<BouquetAssociationSection>(m_scan_debug);
                        if (m_BAT->start(m_demux, eDVBBATSpec()))
                                return -1;
                        CONNECT(m_BAT->tableReady, eDVBScan::BATready);
                }
        }
-
        return 0;
 }
 
@@ -275,7 +283,7 @@ void eDVBScan::PMTready(int err)
                        if (pcrpid == 0xFFFF)
                                pcrpid = pmt.getPcrPid();
                        else
-                               eDebug("already have a pcrpid %04x %04x", pcrpid, pmt.getPcrPid());
+                               SCAN_eDebug("already have a pcrpid %04x %04x", pcrpid, pmt.getPcrPid());
                        ElementaryStreamInfoConstIterator es;
                        for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
                        {
@@ -299,35 +307,37 @@ void eDVBScan::PMTready(int err)
                                        for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
                                                        desc != (*es)->getDescriptors()->end(); ++desc)
                                        {
-                                               switch ((*desc)->getTag())
+                                               uint8_t tag = (*desc)->getTag();
+                                               if (!isaudio && !isvideo)
                                                {
-                                               case DTS_DESCRIPTOR:
-                                               case AAC_DESCRIPTOR:
-                                                       isaudio = 1;
-                                               case AC3_DESCRIPTOR:
-                                                       isaudio = 1;
-                                                       break;
-                                               case CA_DESCRIPTOR:
-                                                       is_scrambled = 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)
+                                                       switch (tag)
                                                        {
-                                                       case 0x41432d33:
+                                                       case DTS_DESCRIPTOR:
+                                                       case AAC_DESCRIPTOR:
+                                                       case AC3_DESCRIPTOR:
                                                                isaudio = 1;
-                                                       default:
+                                                               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)
+                                                               {
+                                                               case 0x41432d33:
+                                                                       isaudio = 1;
+                                                               default:
+                                                                       break;
+                                                               }
                                                                break;
                                                        }
-                                                       break;
-                                               }
+                                                       }
                                                }
+                                               if (tag == CA_DESCRIPTOR)
+                                                       is_scrambled = 1;
                                        }
                                        break;
                                }
@@ -355,7 +365,7 @@ void eDVBScan::PMTready(int err)
                else
                        m_pmt_in_progress->second.serviceType = 100;
        }
-       if (err == -2) // aborted in sdt progress
+       if (err == -1) // timeout or removed by sdt
                m_pmts_to_read.erase(m_pmt_in_progress++);
        else if (m_pmt_running)
                ++m_pmt_in_progress;
@@ -366,9 +376,10 @@ 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;
                m_pmt_running = false;
                channelDone();
        }
@@ -395,7 +406,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        {
                eDVBFrontendParametersSatellite parm;
                feparm->getDVBS(parm);
-               eDebug("try to add %d %d %d %d %d %d",
+               SCAN_eDebug("try to add %d %d %d %d %d %d",
                        parm.orbital_position, parm.frequency, parm.symbol_rate, parm.polarisation, parm.fec, parm.modulation);
                break;
        }
@@ -403,7 +414,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        {
                eDVBFrontendParametersCable parm;
                feparm->getDVBC(parm);
-               eDebug("try to add %d %d %d %d",
+               SCAN_eDebug("try to add %d %d %d %d",
                        parm.frequency, parm.symbol_rate, parm.modulation, parm.fec_inner);
                break;
        }
@@ -411,7 +422,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        {
                eDVBFrontendParametersTerrestrial parm;
                feparm->getDVBT(parm);
-               eDebug("try to add %d %d %d %d %d %d %d %d",
+               SCAN_eDebug("try to add %d %d %d %d %d %d %d %d",
                        parm.frequency, parm.modulation, parm.transmission_mode, parm.hierarchy,
                        parm.guard_interval, parm.code_rate_LP, parm.code_rate_HP, parm.bandwidth);
                break;
@@ -427,11 +438,11 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
                        if (!found_count)
                        {
                                *i = feparm;  // update
-                               eDebug("update");
+                               SCAN_eDebug("update");
                        }
                        else
                        {
-                               eDebug("remove dupe");
+                               SCAN_eDebug("remove dupe");
                                m_ch_toScan.erase(i++);
                                continue;
                        }
@@ -442,7 +453,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
 
        if (found_count > 0)
        {
-               eDebug("already in todo list");
+               SCAN_eDebug("already in todo list");
                return;
        }
 
@@ -450,7 +461,7 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
                if (sameChannel(*i, feparm))
                {
-                       eDebug("successfully scanned");
+                       SCAN_eDebug("successfully scanned");
                        return;
                }
 
@@ -458,18 +469,18 @@ void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameter
        for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
                if (sameChannel(*i, feparm, true))
                {
-                       eDebug("scanned but not available");
+                       SCAN_eDebug("scanned but not available");
                        return;
                }
 
                /* ... on the current channel */
        if (sameChannel(m_ch_current, feparm))
        {
-               eDebug("is current");
+               SCAN_eDebug("is current");
                return;
        }
 
-       eDebug("really add");
+       SCAN_eDebug("really add");
                /* otherwise, add it to the todo list. */
        m_ch_toScan.push_front(feparm); // better.. then the rotor not turning wild from east to west :)
 }
@@ -490,16 +501,6 @@ void eDVBScan::channelDone()
        {
                unsigned long hash = 0;
 
-               // m_ch_current is not set, when eDVBScan is just used for a SDT update
-               if (!m_ch_current)
-               {
-                       unsigned int channelFlags;
-                       m_channel->getCurrentFrontendParameters(m_ch_current);
-                       m_ch_current->getFlags(channelFlags);
-                       if (channelFlags & iDVBFrontendParameters::flagOnlyFree)
-                               m_flags |= scanOnlyFree;
-               }
-
                m_ch_current->getHash(hash);
                
                eDVBNamespace dvbnamespace = buildNamespace(
@@ -516,7 +517,6 @@ void eDVBScan::channelDone()
        
        if (m_ready & validNIT)
        {
-               eDebug("validNIT");
                int system;
                std::list<ePtr<iDVBFrontendParameters> > m_ch_toScan_backup;
                m_ch_current->getSystem(system);
@@ -604,7 +604,7 @@ void eDVBScan::channelDone()
 
                                                if ( abs(abs(3600 - p.orbital_position) - sat.orbital_position) < 5 )
                                                {
-                                                       eDebug("found transponder with incorrect west/east flag ... correct this");
+                                                       SCAN_eDebug("found transponder with incorrect west/east flag ... correct this");
                                                        sat.orbital_position = p.orbital_position;
                                                }
 
@@ -692,16 +692,6 @@ void eDVBScan::channelDone()
                {
                        unsigned long hash = 0;
 
-                       // m_ch_current is not set, when eDVBScan is just used for a SDT update
-                       if (!m_ch_current)
-                       {
-                               unsigned int channelFlags;
-                               m_channel->getCurrentFrontendParameters(m_ch_current);
-                               m_ch_current->getFlags(channelFlags);
-                               if (channelFlags & iDVBFrontendParameters::flagOnlyFree)
-                                       m_flags |= scanOnlyFree;
-                       }
-
                        m_ch_current->getHash(hash);
 
                        m_chid_current = eDVBChannelID(
@@ -768,16 +758,20 @@ 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);
                }
 
-               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;
-                       m_event(evtNewService);
+               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));
+                       if (i.second)
+                       {
+                               m_last_service = i.first;
+                               m_event(evtNewService);
+                       }
                }
+               else
+                       SCAN_eDebug("dont add... is scrambled!");
                m_pmts_to_read.erase(m_pmt_in_progress++);
        }
 
@@ -792,7 +786,7 @@ void eDVBScan::channelDone()
        {
                if (sameChannel(*i, m_ch_current))
                {
-                       eDebug("remove dupe 2");
+                       SCAN_eDebug("remove dupe 2");
                        m_ch_toScan.erase(i++);
                        continue;
                }
@@ -841,10 +835,8 @@ void eDVBScan::insertInto(iDVBChannelList *db, bool dontRemoveOldFlags)
                std::list<ePtr<iDVBFrontendParameters> >::iterator it(m_ch_scanned.begin());
                for (;it != m_ch_scanned.end(); ++it)
                {
-                       if (m_flags & scanDontRemoveUnscanned) {
-                               eDebug("scanDontRemoveUnscanned!");
+                       if (m_flags & scanDontRemoveUnscanned)
                                db->removeServices(&(*(*it)));
-                       }
                        else
                        {
                                int system;
@@ -990,9 +982,13 @@ RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescription
                                        SCAN_eDebug("is scrambled!");
                                        add = false;
                                }
+                               else
+                                       SCAN_eDebug("is free");
                        }
-                       else
+                       else {
                                SCAN_eDebug("not found in PAT.. so we assume it is scrambled!!");
+                               add = false;
+                       }
                }
 
                if (add)