+
+void eDVBServicePMTHandler::free()
+{
+ m_dvb_scan = 0;
+ delete m_dvb_scan;
+
+ 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);
+ m_ca_servicePtr = 0;
+ }
+
+ if (m_channel)
+ eDVBCIInterfaces::getInstance()->removePMTHandler(this);
+
+ 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;
+}
+
+void eDVBCAService::Connect()
+{
+ memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
+ m_servaddr.sun_family = AF_UNIX;
+ strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
+ m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
+ m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen);
+ fcntl(m_sock, F_SETFL, O_NONBLOCK);
+ int val=1;
+ setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
+}
+
+void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
+{
+ if (!ptr)
+ return;
+
+ eDVBTableSpec table_spec;
+ ptr->getSpec(table_spec);
+
+ int pmtpid = table_spec.pid,
+ pmt_version = table_spec.version;
+
+ uint8_t demux_mask = 0;
+ int data_demux = -1;
+
+ int iter=0, max_demux_slots = sizeof(m_used_demux);
+ while ( iter < max_demux_slots )
+ {
+ if ( m_used_demux[iter] != 0xFF )
+ {
+ if ( m_used_demux[iter] > data_demux )
+ data_demux = m_used_demux[iter];
+ demux_mask |= (1 << m_used_demux[iter]);
+ }
+ ++iter;
+ }
+
+ if ( data_demux == -1 )
+ {
+ eDebug("[eDVBCAService] no data demux found for service %s", m_service.toString().c_str() );
+ return;
+ }
+
+ 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);
+ build_hash |= (pmt_version&0xFF);
+
+ if ( build_hash == m_prev_build_hash )
+ {
+ eDebug("[eDVBCAService] don't build/send the same CA PMT twice");
+ return;
+ }
+
+ std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
+ if ( i != ptr->getSections().end() )
+ {
+ CaProgramMapSection capmt(*i++, m_prev_build_hash ? 0x05 /*update*/ : 0x03 /*only*/, 0x01 );
+
+ while( i != ptr->getSections().end() )
+ {
+// eDebug("append");
+ capmt.append(*i++);
+ }
+
+ // add our private descriptors to capmt
+ uint8_t tmp[10];
+
+ tmp[0]=0x84; // pmt pid
+ tmp[1]=0x02;
+ tmp[2]=pmtpid>>8;
+ tmp[3]=pmtpid&0xFF;
+ capmt.injectDescriptor(tmp, false);
+
+ tmp[0] = 0x82; // demux
+ tmp[1] = 0x02;
+ tmp[2] = demux_mask; // descramble bitmask
+ tmp[3] = data_demux&0xFF; // read section data from demux number
+ capmt.injectDescriptor(tmp, false);
+
+ tmp[0] = 0x81; // dvbnamespace
+ tmp[1] = 0x08;
+ tmp[2] = m_service.getDVBNamespace().get()>>24;
+ tmp[3]=(m_service.getDVBNamespace().get()>>16)&0xFF;
+ tmp[4]=(m_service.getDVBNamespace().get()>>8)&0xFF;
+ tmp[5]=m_service.getDVBNamespace().get()&0xFF;
+ tmp[6]=m_service.getTransportStreamID().get()>>8;
+ tmp[7]=m_service.getTransportStreamID().get()&0xFF;
+ tmp[8]=m_service.getOriginalNetworkID().get()>>8;
+ tmp[9]=m_service.getOriginalNetworkID().get()&0xFF;
+ capmt.injectDescriptor(tmp, false);
+
+ capmt.writeToBuffer(m_capmt);
+ }
+
+ m_prev_build_hash = build_hash;
+
+ if ( m_sendstate != 0xFFFFFFFF )
+ m_sendstate=0;
+ sendCAPMT();
+}
+
+void eDVBCAService::sendCAPMT()
+{
+ if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
+ {
+ ::close(m_sock);
+ Connect();
+ }
+
+ int wp=0;
+ if ( m_capmt[3] & 0x80 )
+ {
+ int i=0;
+ int lenbytes = m_capmt[3] & ~0x80;
+ while(i < lenbytes)
+ wp = (wp << 8) | m_capmt[4 + i++];
+ wp+=4;
+ wp+=lenbytes;
+ }
+ else
+ {
+ wp = m_capmt[3];
+ wp+=4;
+ }
+
+ if ( write(m_sock, m_capmt, wp) == wp )
+ {
+ m_sendstate=0xFFFFFFFF;
+ eDebug("[eDVBCAService] send %d bytes",wp);
+#if 1
+ for(int i=0;i<wp;i++)
+ eDebugNoNewLine("%02x ", m_capmt[i]);
+ eDebug("");
+#endif
+ }
+ else
+ {
+ switch(m_sendstate)
+ {
+ case 0xFFFFFFFF:
+ ++m_sendstate;
+ m_retryTimer.start(0,true);
+// eDebug("[eDVBCAService] send failed .. immediate retry");
+ break;
+ default:
+ m_retryTimer.start(5000,true);
+// eDebug("[eDVBCAService] send failed .. retry in 5 sec");
+ break;
+ }
+ ++m_sendstate;
+ }
+}
+
+static PyObject *createTuple(int pid, const char *type)
+{
+ PyObject *r = PyTuple_New(2);
+ PyTuple_SetItem(r, 0, PyInt_FromLong(pid));
+ PyTuple_SetItem(r, 1, PyString_FromString(type));
+ return r;
+}
+
+static inline void PyList_AppendSteal(PyObject *list, PyObject *item)
+{
+ PyList_Append(list, item);
+ Py_DECREF(item);
+}
+
+PyObject *eDVBServicePMTHandler::program::createPythonObject()
+{
+ PyObject *r = PyDict_New();
+
+ PyObject *l = PyList_New(0);
+
+ PyList_AppendSteal(l, createTuple(0, "pat"));
+
+ if (pmtPid != -1)
+ PyList_AppendSteal(l, createTuple(pmtPid, "pmt"));
+
+ for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
+ i(videoStreams.begin());
+ i != videoStreams.end(); ++i)
+ PyList_AppendSteal(l, createTuple(i->pid, "video"));
+
+ for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
+ i(audioStreams.begin());
+ i != audioStreams.end(); ++i)
+ PyList_AppendSteal(l, createTuple(i->pid, "audio"));
+
+ for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
+ i(subtitleStreams.begin());
+ i != subtitleStreams.end(); ++i)
+ PyList_AppendSteal(l, createTuple(i->pid, "subtitle"));
+
+ PyList_AppendSteal(l, createTuple(pcrPid, "pcr"));
+
+ if (textPid != -1)
+ PyList_AppendSteal(l, createTuple(textPid, "text"));
+
+ PyDict_SetItemString(r, "pids", l);
+ return r;
+}