+void eDVBServicePMTHandler::free()
+{
+ if (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];
+ ePtr<eTable<ProgramMapSection> > ptr;
+ m_PMT.getCurrent(ptr);
+ eDVBCAService::unregister_service(m_reference, demuxes, ptr);
+ eDVBCIInterfaces::getInstance()->removePMTHandler(this);
+ m_ca_servicePtr = 0;
+ }
+
+ if (m_pvr_channel)
+ {
+ m_pvr_channel->stopFile();
+ m_pvr_channel->setCueSheet(0);
+ }
+ 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()
+ :m_prev_build_hash(0), m_sendstate(0), m_retryTimer(eApp)
+{
+ memset(m_used_demux, 0xFF, sizeof(m_used_demux));
+ CONNECT(m_retryTimer.timeout, eDVBCAService::sendCAPMT);
+ Connect();
+}
+
+eDVBCAService::~eDVBCAService()
+{
+ eDebug("[eDVBCAService] free service %s", m_service.toString().c_str());
+ ::close(m_sock);
+}
+
+RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice )
+{
+ CAServiceMap::iterator it = exist.find(ref);
+ if ( it != exist.end() )
+ caservice = it->second;
+ else
+ {
+ caservice = (exist[ref]=new eDVBCAService());
+ caservice->m_service = ref;
+ 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);
+
+ while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
+ ++iter;
+
+ 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_nums[2], eTable<ProgramMapSection> *ptr )
+{
+ CAServiceMap::iterator it = exist.find(ref);
+ if ( it == exist.end() )
+ {
+ eDebug("[eDVBCAService] try to unregister non registered %s", ref.toString().c_str());
+ return -1;
+ }
+ else
+ {
+ eDVBCAService *caservice = it->second;
+ int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
+ for (int i=0; i < loops; ++i)
+ {
+ 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 (!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("[eDVBCAService] can not send updated demux info");
+ }
+ }
+ }
+ }
+ return 0;
+}
+