copy lists, not list identities... should fix saving of non-default values
[enigma2.git] / lib / dvb / pmt.cpp
index 0fc05240539226c96edf4cf58d564de1ffa6f1cd..f77675414bee2e16796a2ccae39b6cf49953319d 100644 (file)
@@ -4,38 +4,40 @@
 #include <lib/dvb/dvb.h>
 #include <lib/dvb/metaparser.h>
 #include <lib/dvb_ci/dvbci.h>
+#include <lib/dvb/epgcache.h>
 #include <dvbsi++/ca_program_map_section.h>
+#include <dvbsi++/teletext_descriptor.h>
 #include <dvbsi++/descriptor_tag.h>
 #include <dvbsi++/iso639_language_descriptor.h>
-#include <dvbsi++/component_descriptor.h>
+#include <dvbsi++/stream_identifier_descriptor.h>
 
-int eDVBServicePMTHandler::m_count;
-
-eDVBServicePMTHandler::eDVBServicePMTHandler(int record)
-       :m_ca_servicePtr(0)
+eDVBServicePMTHandler::eDVBServicePMTHandler()
+       :m_ca_servicePtr(0), m_decode_demux_num(0xFF)
 {
-       m_record = record;
+       m_use_decode_demux = 0;
        eDVBResourceManager::getInstance(m_resourceManager);
        CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
        CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
-       eDebug("new PMT handler record: %d", m_record);
-       ++m_count;
 }
 
 eDVBServicePMTHandler::~eDVBServicePMTHandler()
 {
-       eDebug("delete PMT handler record: %d", m_record);
        if (m_ca_servicePtr)
        {
                eDebug("unregister caservice");
-               uint8_t demux_num;
-               m_demux->getCADemuxID(demux_num);
+               int demuxes[2] = {0,0};
+               uint8_t tmp;
+               m_demux->getCADemuxID(tmp);
+               demuxes[0]=tmp;
+               if (m_decode_demux_num != 0xFF)
+                       demuxes[1]=m_decode_demux_num;
+               else
+                       demuxes[1]=demuxes[0];
                ePtr<eTable<ProgramMapSection> > ptr;
                m_PMT.getCurrent(ptr);
-               eDVBCAService::unregister_service(m_reference, demux_num, ptr);
+               eDVBCAService::unregister_service(m_reference, demuxes, ptr);
                eDVBCIInterfaces::getInstance()->removePMTHandler(this);
        }
-       --m_count;
 }
 
 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
@@ -47,7 +49,7 @@ void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
                && (state == iDVBChannel::state_ok) && (!m_demux))
        {
                if (m_channel)
-                       if (m_channel->getDemux(m_demux, m_record ? 0 : iDVBChannel::capDecode))
+                       if (m_channel->getDemux(m_demux, (!m_use_decode_demux) ? 0 : iDVBChannel::capDecode))
                                eDebug("Allocating a demux for now tuned-in channel failed.");
                
                serviceEvent(eventTuned);
@@ -56,8 +58,6 @@ void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
                {
                        eDebug("ok ... now we start!!");
 
-                       /* emit */ m_resourceManager->m_channelRunning(channel);
-
                        m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
 
                        if ( m_service && !m_service->cacheEmpty() )
@@ -71,6 +71,18 @@ void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
        }
 }
 
+void eDVBServicePMTHandler::channelEvent(iDVBChannel *channel, int event)
+{
+       switch (event)
+       {
+       case iDVBChannel::evtEOF:
+               serviceEvent(eventEOF);
+               break;
+       default:
+               break;
+       }
+}
+
 void eDVBServicePMTHandler::PMTready(int error)
 {
        if (error)
@@ -78,13 +90,20 @@ void eDVBServicePMTHandler::PMTready(int error)
        else
        {
                serviceEvent(eventNewProgramInfo);
+               eEPGCache::getInstance()->PMTready(this);
                if (!m_pvr_channel)
                {
                        if(!m_ca_servicePtr)   // don't send campmt to camd.socket for playbacked services
                        {
-                               uint8_t demux_num;
-                               m_demux->getCADemuxID(demux_num);
-                               eDVBCAService::register_service(m_reference, demux_num, m_ca_servicePtr);
+                               int demuxes[2] = {0,0};
+                               uint8_t tmp;
+                               m_demux->getCADemuxID(tmp);
+                               demuxes[0]=tmp;
+                               if (m_decode_demux_num != 0xFF)
+                                       demuxes[1]=m_decode_demux_num;
+                               else
+                                       demuxes[1]=demuxes[0];
+                               eDVBCAService::register_service(m_reference, demuxes, m_ca_servicePtr);
                                eDVBCIInterfaces::getInstance()->addPMTHandler(this);
                        }
                        eDVBCIInterfaces::getInstance()->gotPMT(this);
@@ -102,7 +121,6 @@ void eDVBServicePMTHandler::PMTready(int error)
 
 void eDVBServicePMTHandler::PATready(int)
 {
-       eDebug("got PAT");
        ePtr<eTable<ProgramAssociationSection> > ptr;
        if (!m_PAT.getCurrent(ptr))
        {
@@ -131,9 +149,23 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
        program.videoStreams.clear();
        program.audioStreams.clear();
        program.pcrPid = -1;
+       program.isCrypted = false;
+       program.pmtPid = -1;
+       program.textPid = -1;
 
        if (!m_PMT.getCurrent(ptr))
        {
+               int cached_apid_ac3 = -1;
+               int cached_apid_mpeg = -1;
+               int cached_vpid = -1;
+               int cached_tpid = -1;
+               if ( m_service && !m_service->cacheEmpty() )
+               {
+                       cached_vpid = m_service->getCachePID(eDVBService::cVPID);
+                       cached_apid_mpeg = m_service->getCachePID(eDVBService::cAC3PID);
+                       cached_apid_ac3 = m_service->getCachePID(eDVBService::cAPID);
+                       cached_tpid = m_service->getCachePID(eDVBService::cTPID);
+               }
                eDVBTableSpec table_spec;
                ptr->getSpec(table_spec);
                program.pmtPid = table_spec.pid < 0x1fff ? table_spec.pid : -1;
@@ -146,24 +178,29 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                        ElementaryStreamInfoConstIterator es;
                        for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
                        {
-                               int isaudio = 0, isvideo = 0;
+                               int isaudio = 0, isvideo = 0, cadescriptors = 0;
                                videoStream video;
                                audioStream audio;
-                               
+                               audio.component_tag=-1;
+                               video.component_tag=-1;
+
                                video.pid = (*es)->getPid();
                                audio.pid = (*es)->getPid();
-                               
+
                                switch ((*es)->getType())
                                {
                                case 0x01: // MPEG 1 video
                                case 0x02: // MPEG 2 video
                                        isvideo = 1;
-                                       break;
+                                       //break; fall through !!!
                                case 0x03: // MPEG 1 audio
                                case 0x04: // MPEG 2 audio:
-                                       isaudio = 1;
-                                       audio.type = audioStream::atMPEG;
-                                       break;
+                                       if (!isvideo)
+                                       {
+                                               isaudio = 1;
+                                               audio.type = audioStream::atMPEG;
+                                       }
+                                       //break; fall through !!!
                                case 0x06: // PES Private
                                                /* PES private can contain AC-3, DTS or lots of other stuff.
                                                   check descriptors to get the exact type. */
@@ -172,9 +209,33 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                        {
                                                switch ((*desc)->getTag())
                                                {
+                                               case TELETEXT_DESCRIPTOR:
+                                                       if ( program.textPid == -1 || (*es)->getPid() == cached_tpid )
+                                                               program.textPid = (*es)->getPid();
+                                                       break;
                                                case AC3_DESCRIPTOR:
-                                                       isaudio = 1;
-                                                       audio.type = audioStream::atAC3;
+                                                       if (!isvideo)
+                                                       {
+                                                               isaudio = 1;
+                                                               audio.type = audioStream::atAC3;
+                                                       }
+                                                       break;
+                                               case ISO_639_LANGUAGE_DESCRIPTOR:
+                                                       if (!isvideo)
+                                                       {
+                                                               const Iso639LanguageList *languages = ((Iso639LanguageDescriptor*)*desc)->getIso639Languages();
+                                                                       /* use last language code */
+                                                               for (Iso639LanguageConstIterator i(languages->begin()); i != languages->end(); ++i)
+                                                                       audio.language_code = (*i)->getIso639LanguageCode();
+                                                       }
+                                                       break;
+                                               case STREAM_IDENTIFIER_DESCRIPTOR:
+                                                       audio.component_tag =
+                                                               video.component_tag =
+                                                                       ((StreamIdentifierDescriptor*)*desc)->getComponentTag();
+                                                       break;
+                                               case CA_DESCRIPTOR:
+                                                       ++cadescriptors;
                                                        break;
                                                }
                                        }
@@ -182,41 +243,53 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                }
                                if (isaudio)
                                {
-                                       for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
-                                                       desc != (*es)->getDescriptors()->end(); ++desc)
+                                       if ( !program.audioStreams.empty() &&
+                                               ( audio.pid == cached_apid_ac3 || audio.pid == cached_apid_mpeg) )
                                        {
-                                               switch ((*desc)->getTag())
-                                               {
-                                               case ISO_639_LANGUAGE_DESCRIPTOR:
-                                               {
-                                                       const Iso639LanguageList *languages = ((Iso639LanguageDescriptor*)*desc)->getIso639Languages();
-                                                       
-                                                               /* use last language code */
-                                                       for (Iso639LanguageConstIterator i(languages->begin()); i != languages->end(); ++i)
-                                                               audio.language_code = (*i)->getIso639LanguageCode();
-
-                                                       break;
-                                               }
-                                               case COMPONENT_DESCRIPTOR:
-                                                       audio.component_tag = ((ComponentDescriptor*)*desc)->getComponentTag();
-                                                       break;
-                                               }
+                                               program.audioStreams.push_back(program.audioStreams[0]);
+                                               program.audioStreams[0] = audio;
+                                       }
+                                       else
+                                               program.audioStreams.push_back(audio);
+                               }
+                               else if (isvideo)
+                               {
+                                       if ( !program.videoStreams.empty() && video.pid == cached_vpid )
+                                       {
+                                               program.videoStreams.push_back(program.videoStreams[0]);
+                                               program.videoStreams[0] = video;
+                                       }
+                                       else
+                                               program.videoStreams.push_back(video);
+                               }
+                               else
+                                       continue;
+                               if ( cadescriptors > 0 )
+                                       program.isCrypted=true;
+                       }
+                       if ( !program.isCrypted )
+                       {
+                               for (DescriptorConstIterator desc = pmt.getDescriptors()->begin();
+                                       desc != pmt.getDescriptors()->end(); ++desc)
+                               {
+                                       switch ((*desc)->getTag())
+                                       {
+                                       case CA_DESCRIPTOR:
+                                               program.isCrypted=true;
+                                               break;
                                        }
-
-                                       program.audioStreams.push_back(audio);
                                }
-                               if (isvideo)
-                                       program.videoStreams.push_back(video);
+                               break;
                        }
                }
                return 0;
-       }
-       else if ( m_service && !m_service->cacheEmpty() )
+       } else if ( m_service && !m_service->cacheEmpty() )
        {
                int vpid = m_service->getCachePID(eDVBService::cVPID),
-                       apid_ac3 = m_service->getCachePID(eDVBService::cAPID),
-                       apid_mpeg = m_service->getCachePID(eDVBService::cAC3PID),
+                       apid_ac3 = m_service->getCachePID(eDVBService::cAC3PID),
+                       apid_mpeg = m_service->getCachePID(eDVBService::cAPID),
                        pcrpid = m_service->getCachePID(eDVBService::cPCRPID),
+                       tpid = m_service->getCachePID(eDVBService::cTPID),
                        cnt=0;
                if ( vpid != -1 )
                {
@@ -246,6 +319,11 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                        ++cnt;
                        program.pcrPid = pcrpid;
                }
+               if ( tpid != -1 )
+               {
+                       ++cnt;
+                       program.textPid = tpid;
+               }
                if ( cnt )
                        return 0;
        }
@@ -261,7 +339,7 @@ int eDVBServicePMTHandler::getChannel(eUsePtr<iDVBChannel> &channel)
                return -1;
 }
 
-int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
+int eDVBServicePMTHandler::getDataDemux(ePtr<iDVBDemux> &demux)
 {
        demux = m_demux;
        if (demux)
@@ -270,6 +348,24 @@ int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
                return -1;
 }
 
+int eDVBServicePMTHandler::getDecodeDemux(ePtr<iDVBDemux> &demux)
+{
+       int ret=0;
+               /* if we're using the decoding demux as data source
+                  (for example in pvr playbacks), return that one. */
+       if (m_use_decode_demux)
+       {
+               demux = m_demux;
+               return ret;
+       }
+
+       ret = m_channel->getDemux(demux, iDVBChannel::capDecode);
+       if (!ret)
+               demux->getCADemuxID(m_decode_demux_num);
+
+       return ret;
+}
+
 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
 {
        pvr_channel = m_pvr_channel;
@@ -279,11 +375,13 @@ int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
                return -1;
 }
 
-int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
+int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux)
 {
        RESULT res;
        m_reference = ref;
        
+       m_use_decode_demux = use_decode_demux;
+       
                /* is this a normal (non PVR) channel? */
        if (ref.path.empty())
        {
@@ -315,6 +413,10 @@ int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
                        m_channelStateChanged_connection);
                m_last_channel_state = -1;
                channelStateChanged(m_channel);
+
+               m_channel->connectEvent(
+                       slot(*this, &eDVBServicePMTHandler::channelEvent), 
+                       m_channelEvent_connection);
        } else
        {
                serviceEvent(eventTuneFailed);
@@ -331,6 +433,16 @@ int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
        return res;
 }
 
+void eDVBServicePMTHandler::free()
+{
+       m_PMT.stop();
+       m_PAT.stop();
+       m_service = 0;
+       m_channel = 0;
+       m_pvr_channel = 0;
+       m_demux = 0;
+}
+
 std::map<eServiceReferenceDVB, eDVBCAService*> eDVBCAService::exist;
 
 eDVBCAService::eDVBCAService()
@@ -347,7 +459,7 @@ eDVBCAService::~eDVBCAService()
        ::close(m_sock);
 }
 
-RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int demux_num, eDVBCAService *&caservice )
+RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice )
 {
        CAServiceMap::iterator it = exist.find(ref);
        if ( it != exist.end() )
@@ -356,73 +468,82 @@ RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int dem
        {
                caservice = (exist[ref]=new eDVBCAService());
                caservice->m_service = ref;
-               eDebug("[eDVBCAHandler] new service %s", ref.toString().c_str() );
+               eDebug("[eDVBCAService] new service %s", ref.toString().c_str() );
        }
+
+       int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
+       for (int i=0; i < loops; ++i)
+       {
 // search free demux entry
-       int iter=0, max_demux_slots = sizeof(caservice->m_used_demux);
+               int iter=0, max_demux_slots = sizeof(caservice->m_used_demux);
 
-       while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
-               ++iter;
+               while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
+                       ++iter;
 
-       if ( iter < max_demux_slots )
-       {
-               caservice->m_used_demux[iter] = demux_num & 0xFF;
-               eDebug("[eDVBCAHandler] add demux %d to slot %d service %s", demux_num, iter, ref.toString().c_str());
-       }
-       else
-       {
-               eDebug("[eDVBCAHandler] no more demux slots free for service %s!!", ref.toString().c_str());
-               return -1;
+               if ( iter < max_demux_slots )
+               {
+                       caservice->m_used_demux[iter] = demux_nums[i] & 0xFF;
+                       eDebug("[eDVBCAService] add demux %d to slot %d service %s", caservice->m_used_demux[iter], iter, ref.toString().c_str());
+               }
+               else
+               {
+                       eDebug("[eDVBCAService] no more demux slots free for service %s!!", ref.toString().c_str());
+                       return -1;
+               }
        }
        return 0;
 }
 
-RESULT eDVBCAService::unregister_service( const eServiceReferenceDVB &ref, int demux_num, eTable<ProgramMapSection> *ptr )
+RESULT eDVBCAService::unregister_service( const eServiceReferenceDVB &ref, int demux_nums[2], eTable<ProgramMapSection> *ptr )
 {
        CAServiceMap::iterator it = exist.find(ref);
        if ( it == exist.end() )
        {
-               eDebug("[eDVBCAHandler] try to unregister non registered %s", ref.toString().c_str());
+               eDebug("[eDVBCAService] try to unregister non registered %s", ref.toString().c_str());
                return -1;
        }
        else
        {
                eDVBCAService *caservice = it->second;
-               bool freed = false;
-               int iter = 0,
-                       used_demux_slots = 0,
-                       max_demux_slots = sizeof(caservice->m_used_demux)/sizeof(int);
-               while ( iter < max_demux_slots )
+               int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
+               for (int i=0; i < loops; ++i)
                {
-                       if ( caservice->m_used_demux[iter] != 0xFF )
+                       bool freed = false;
+                       int iter = 0,
+                               used_demux_slots = 0,
+                               max_demux_slots = sizeof(caservice->m_used_demux)/sizeof(int);
+                       while ( iter < max_demux_slots )
+                       {
+                               if ( caservice->m_used_demux[iter] != 0xFF )
+                               {
+                                       if ( !freed && caservice->m_used_demux[iter] == demux_nums[i] )
+                                       {
+                                               eDebug("[eDVBCAService] free slot %d demux %d for service %s", iter, caservice->m_used_demux[iter], caservice->m_service.toString().c_str() );
+                                               caservice->m_used_demux[iter] = 0xFF;
+                                               freed=true;
+                                       }
+                                       else
+                                               ++used_demux_slots;
+                               }
+                               ++iter;
+                       }
+                       if (!freed)
+                               eDebug("[eDVBCAService] couldn't free demux slot for demux %d", demux_nums[i]);
+                       if (i || loops == 1)
                        {
-                               if ( !freed && caservice->m_used_demux[iter] == demux_num )
+                               if (!used_demux_slots)  // no more used.. so we remove it
                                {
-                                       eDebug("[eDVBCAService] free slot %d demux %d for service %s", iter, caservice->m_used_demux[iter], caservice->m_service.toString().c_str() );
-                                       caservice->m_used_demux[iter] = 0xFF;
-                                       freed=true;
+                                       delete it->second;
+                                       exist.erase(it);
                                }
                                else
-                                       ++used_demux_slots;
+                               {
+                                       if (ptr)
+                                               it->second->buildCAPMT(ptr);
+                                       else
+                                               eDebug("[eDVBCAService] can not send updated demux info");
+                               }
                        }
-                       ++iter;
-               }
-               if (!freed)
-               {
-                       eDebug("[eDVBCAService] couldn't free demux slot for demux %d", demux_num);
-                       return -1;
-               }
-               if (!used_demux_slots)  // no more used.. so we remove it
-               {
-                       delete it->second;
-                       exist.erase(it);
-               }
-               else
-               {
-                       if (ptr)
-                               it->second->buildCAPMT(ptr);
-                       else
-                               eDebug("[eDVBCAHandler] can not send updated demux info");
                }
        }
        return 0;
@@ -453,32 +574,27 @@ void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
                pmt_version = table_spec.version;
 
        uint8_t demux_mask = 0;
-       uint8_t first_demux_num = 0xFF;
+       int data_demux = -1;
 
-#if 1
        int iter=0, max_demux_slots = sizeof(m_used_demux);
        while ( iter < max_demux_slots )
        {
                if ( m_used_demux[iter] != 0xFF )
                {
-                       if ( first_demux_num == 0xFF )
-                               first_demux_num = m_used_demux[iter];
+                       if ( m_used_demux[iter] > data_demux )
+                               data_demux = m_used_demux[iter];
                        demux_mask |= (1 << m_used_demux[iter]);
                }
                ++iter;
        }
-#else
-       demux_mask = 3;
-       first_demux_num = 0;
-#endif
 
-       if ( first_demux_num == 0xFF )
+       if ( data_demux == -1 )
        {
-               eDebug("[eDVBCAService] no demux found for service %s", m_service.toString().c_str() );
+               eDebug("[eDVBCAService] no data demux found for service %s", m_service.toString().c_str() );
                return;
        }
 
-       eDebug("demux %d mask %02x prevhash %08x", first_demux_num, demux_mask, m_prev_build_hash);
+       eDebug("demux %d mask %02x prevhash %08x", data_demux, demux_mask, m_prev_build_hash);
 
        unsigned int build_hash = (pmtpid << 16);
        build_hash |= (demux_mask << 8);
@@ -513,7 +629,7 @@ void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
                tmp[0] = 0x82; // demux
                tmp[1] = 0x02;
                tmp[2] = demux_mask;    // descramble bitmask
-               tmp[3] = first_demux_num; // read section data from demux number
+               tmp[3] = data_demux&0xFF; // read section data from demux number
                capmt.injectDescriptor(tmp, false);
 
                tmp[0] = 0x81; // dvbnamespace
@@ -565,7 +681,7 @@ void eDVBCAService::sendCAPMT()
        if ( write(m_sock, m_capmt, wp) == wp )
        {
                m_sendstate=0xFFFFFFFF;
-               eDebug("[eDVBCAHandler] send %d bytes",wp);
+               eDebug("[eDVBCAService] send %d bytes",wp);
 #if 1
                for(int i=0;i<wp;i++)
                        eDebugNoNewLine("%02x ", m_capmt[i]);
@@ -579,11 +695,11 @@ void eDVBCAService::sendCAPMT()
                        case 0xFFFFFFFF:
                                ++m_sendstate;
                                m_retryTimer.start(0,true);
-//                             eDebug("[eDVBCAHandler] send failed .. immediate retry");
+//                             eDebug("[eDVBCAService] send failed .. immediate retry");
                                break;
                        default:
                                m_retryTimer.start(5000,true);
-//                             eDebug("[eDVBCAHandler] send failed .. retry in 5 sec");
+//                             eDebug("[eDVBCAService] send failed .. retry in 5 sec");
                                break;
                }
                ++m_sendstate;