1 #include <lib/base/eerror.h>
2 #include <lib/dvb/pmt.h>
3 #include <lib/dvb/specs.h>
4 #include <lib/dvb/dvb.h>
5 #include <lib/dvb/metaparser.h>
6 #include <dvbsi++/ca_program_map_section.h>
8 eDVBServicePMTHandler::eDVBServicePMTHandler(int record)
12 eDVBResourceManager::getInstance(m_resourceManager);
13 CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
14 CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
15 eDebug("new PMT handler record: %d", m_record);
18 eDVBServicePMTHandler::~eDVBServicePMTHandler()
20 eDebug("delete PMT handler record: %d", m_record);
23 eDebug("unregister caservice");
25 m_demux->getCADemuxID(demux_num);
26 ePtr<eTable<ProgramMapSection> > ptr;
27 m_PMT.getCurrent(ptr);
28 eDVBCAService::unregister_demux(m_reference, demux_num, ptr);
32 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
35 channel->getState(state);
37 if ((m_last_channel_state != iDVBChannel::state_ok)
38 && (state == iDVBChannel::state_ok) && (!m_demux))
41 if (m_channel->getDemux(m_demux, m_record ? 0 : iDVBChannel::capDecode))
42 eDebug("Allocating a demux for now tuned-in channel failed.");
44 serviceEvent(eventTuned);
48 eDebug("ok ... now we start!!");
50 /* emit */ m_resourceManager->m_channelRunning(channel);
52 m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
54 if ( m_service && !m_service->cacheEmpty() )
55 serviceEvent(eventNewProgramInfo);
57 } else if ((m_last_channel_state != iDVBChannel::state_failed) &&
58 (state == iDVBChannel::state_failed))
60 eDebug("tune failed.");
61 serviceEvent(eventTuneFailed);
65 void eDVBServicePMTHandler::PMTready(int error)
68 serviceEvent(eventNoPMT);
71 serviceEvent(eventNewProgramInfo);
72 if (!m_pvr_channel && !m_ca_servicePtr) // don't send campmt to camd.socket for playbacked services
75 m_demux->getCADemuxID(demux_num);
76 eDVBCAService::register_demux(m_reference, demux_num, m_ca_servicePtr);
80 ePtr<eTable<ProgramMapSection> > ptr;
81 if (!m_PMT.getCurrent(ptr))
82 m_ca_servicePtr->buildCAPMT(ptr);
84 eDebug("eDVBServicePMTHandler cannot call buildCAPMT");
89 void eDVBServicePMTHandler::PATready(int)
92 ePtr<eTable<ProgramAssociationSection> > ptr;
93 if (!m_PAT.getCurrent(ptr))
96 std::vector<ProgramAssociationSection*>::const_iterator i;
97 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
99 const ProgramAssociationSection &pat = **i;
100 ProgramAssociationConstIterator program;
101 for (program = pat.getPrograms()->begin(); program != pat.getPrograms()->end(); ++program)
102 if (eServiceID((*program)->getProgramNumber()) == m_reference.getServiceID())
103 pmtpid = (*program)->getProgramMapPid();
106 serviceEvent(eventNoPATEntry);
108 m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
110 serviceEvent(eventNoPAT);
113 int eDVBServicePMTHandler::getProgramInfo(struct program &program)
115 ePtr<eTable<ProgramMapSection> > ptr;
117 program.videoStreams.clear();
118 program.audioStreams.clear();
121 if (!m_PMT.getCurrent(ptr))
123 eDVBTableSpec table_spec;
124 ptr->getSpec(table_spec);
125 program.pmtPid = table_spec.pid < 0x1fff ? table_spec.pid : -1;
126 std::vector<ProgramMapSection*>::const_iterator i;
127 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
129 const ProgramMapSection &pmt = **i;
130 program.pcrPid = pmt.getPcrPid();
132 ElementaryStreamInfoConstIterator es;
133 for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
135 int isaudio = 0, isvideo = 0;
139 video.pid = (*es)->getPid();
140 audio.pid = (*es)->getPid();
142 switch ((*es)->getType())
144 case 0x01: // MPEG 1 video
145 case 0x02: // MPEG 2 video
148 case 0x03: // MPEG 1 audio
149 case 0x04: // MPEG 2 audio:
151 audio.type = audioStream::atMPEG;
155 program.audioStreams.push_back(audio);
157 program.videoStreams.push_back(video);
162 else if ( m_service && !m_service->cacheEmpty() )
164 int vpid = m_service->getCachePID(eDVBService::cVPID),
165 apid_ac3 = m_service->getCachePID(eDVBService::cAPID),
166 apid_mpeg = m_service->getCachePID(eDVBService::cAC3PID),
167 pcrpid = m_service->getCachePID(eDVBService::cPCRPID),
173 program.videoStreams.push_back(s);
176 if ( apid_ac3 != -1 )
179 s.type = audioStream::atAC3;
181 program.audioStreams.push_back(s);
184 if ( apid_mpeg != -1 )
187 s.type = audioStream::atMPEG;
189 program.audioStreams.push_back(s);
195 program.pcrPid = pcrpid;
203 int eDVBServicePMTHandler::getDemux(ePtr<iDVBDemux> &demux)
212 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
214 pvr_channel = m_pvr_channel;
221 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref)
226 /* is this a normal (non PVR) channel? */
227 if (ref.path.empty())
230 ref.getChannelID(chid);
231 res = m_resourceManager->allocateChannel(chid, m_channel);
232 eDebug("allocate Channel: res %d", res);
235 eDVBMetaParser parser;
237 if (parser.parseFile(ref.path))
238 eWarning("no .meta file found, trying original service ref.");
240 m_reference = parser.m_ref;
244 res = m_resourceManager->allocatePVRChannel(m_pvr_channel);
246 eDebug("allocatePVRChannel failed!\n");
247 m_channel = m_pvr_channel;
252 m_channel->connectStateChange(
253 slot(*this, &eDVBServicePMTHandler::channelStateChanged),
254 m_channelStateChanged_connection);
255 m_last_channel_state = -1;
256 channelStateChanged(m_channel);
259 serviceEvent(eventTuneFailed);
264 m_pvr_channel->playFile(ref.path.c_str());
266 ePtr<iDVBChannelList> db;
267 if (!m_resourceManager->getChannelList(db))
268 db->getService((eServiceReferenceDVB&)m_reference, m_service);
273 std::map<eServiceReferenceDVB, eDVBCAService*> eDVBCAService::exist;
275 eDVBCAService::eDVBCAService()
276 :m_prev_build_hash(0), m_sendstate(0), m_retryTimer(eApp)
278 memset(m_used_demux, 0xFF, sizeof(m_used_demux));
279 CONNECT(m_retryTimer.timeout, eDVBCAService::sendCAPMT);
283 eDVBCAService::~eDVBCAService()
285 eDebug("[eDVBCAService] free service %s", m_service.toString().c_str());
289 RESULT eDVBCAService::register_demux( const eServiceReferenceDVB &ref, int demux_num, eDVBCAService *&caservice )
291 CAServiceMap::iterator it = exist.find(ref);
292 if ( it != exist.end() )
293 caservice = it->second;
296 caservice = (exist[ref]=new eDVBCAService());
297 caservice->m_service = ref;
298 eDebug("[eDVBCAHandler] new service %s", ref.toString().c_str() );
300 // search free demux entry
301 int iter=0, max_demux_slots = sizeof(caservice->m_used_demux);
303 while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
306 if ( iter < max_demux_slots )
308 caservice->m_used_demux[iter] = demux_num & 0xFF;
309 eDebug("[eDVBCAHandler] add demux %d to slot %d service %s", demux_num, iter, ref.toString().c_str());
313 eDebug("[eDVBCAHandler] no more demux slots free for service %s!!", ref.toString().c_str());
319 RESULT eDVBCAService::unregister_demux( const eServiceReferenceDVB &ref, int demux_num, eTable<ProgramMapSection> *ptr )
321 CAServiceMap::iterator it = exist.find(ref);
322 if ( it == exist.end() )
324 eDebug("[eDVBCAHandler] try to unregister non registered %s", ref.toString().c_str());
329 eDVBCAService *caservice = it->second;
332 used_demux_slots = 0,
333 max_demux_slots = sizeof(caservice->m_used_demux)/sizeof(int);
334 while ( iter < max_demux_slots )
336 if ( caservice->m_used_demux[iter] != 0xFF )
338 if ( !freed && caservice->m_used_demux[iter] == demux_num )
340 eDebug("[eDVBCAService] free slot %d demux %d for service %s", iter, caservice->m_used_demux[iter], caservice->m_service.toString().c_str() );
341 caservice->m_used_demux[iter] = 0xFF;
351 eDebug("[eDVBCAService] couldn't free demux slot for demux %d", demux_num);
354 if (!used_demux_slots) // no more used.. so we remove it
362 it->second->buildCAPMT(ptr);
364 eDebug("[eDVBCAHandler] can not send updated demux info");
370 void eDVBCAService::Connect()
372 memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
373 m_servaddr.sun_family = AF_UNIX;
374 strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
375 m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
376 m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
377 connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen);
378 fcntl(m_sock, F_SETFL, O_NONBLOCK);
380 setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
383 void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
388 eDVBTableSpec table_spec;
389 ptr->getSpec(table_spec);
391 int pmtpid = table_spec.pid,
392 pmt_version = table_spec.version;
394 uint8_t demux_mask = 0;
395 uint8_t first_demux_num = 0xFF;
397 int iter=0, max_demux_slots = sizeof(m_used_demux);
398 while ( iter < max_demux_slots )
400 if ( m_used_demux[iter] != 0xFF )
402 if ( first_demux_num == 0xFF )
403 first_demux_num = m_used_demux[iter];
404 demux_mask |= (1 << m_used_demux[iter]);
409 if ( first_demux_num == 0xFF )
411 eDebug("[eDVBCAService] no demux found for service %s", m_service.toString().c_str() );
415 eDebug("demux %d mask %02x prevhash %08x", first_demux_num, demux_mask, m_prev_build_hash);
417 unsigned int build_hash = (pmtpid << 16);
418 build_hash |= (demux_mask << 8);
419 build_hash |= (pmt_version&0xFF);
421 if ( build_hash == m_prev_build_hash )
423 eDebug("[eDVBCAService] don't build/send the same CA PMT twice");
427 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
428 if ( i != ptr->getSections().end() )
430 CaProgramMapSection capmt(*i++, m_prev_build_hash ? 0x05 /*update*/ : 0x03 /*only*/, 0x01 );
432 while( i != ptr->getSections().end() )
438 // add our private descriptors to capmt
441 tmp[0]=0x84; // pmt pid
445 capmt.injectDescriptor(tmp, false);
447 tmp[0] = 0x82; // demux
449 tmp[3] = first_demux_num; // read section data from demux number
450 tmp[2] = demux_mask; // descramble bitmask
451 capmt.injectDescriptor(tmp, false);
453 tmp[0] = 0x81; // dvbnamespace
455 tmp[2] = m_service.getDVBNamespace().get()>>24;
456 tmp[3]=(m_service.getDVBNamespace().get()>>16)&0xFF;
457 tmp[4]=(m_service.getDVBNamespace().get()>>8)&0xFF;
458 tmp[5]=m_service.getDVBNamespace().get()&0xFF;
459 tmp[6]=m_service.getTransportStreamID().get()>>8;
460 tmp[7]=m_service.getTransportStreamID().get()&0xFF;
461 tmp[8]=m_service.getOriginalNetworkID().get()>>8;
462 tmp[9]=m_service.getOriginalNetworkID().get()&0xFF;
463 capmt.injectDescriptor(tmp, false);
465 capmt.writeToBuffer(m_capmt);
468 m_prev_build_hash = build_hash;
470 if ( m_sendstate != 0xFFFFFFFF )
475 void eDVBCAService::sendCAPMT()
477 if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
484 if ( m_capmt[3] & 0x80 )
487 int lenbytes = m_capmt[3] & ~0x80;
489 wp |= (m_capmt[4+i] << (8 * i++));
499 if ( write(m_sock, m_capmt, wp) == wp )
501 m_sendstate=0xFFFFFFFF;
502 eDebug("[eDVBCAHandler] send %d bytes",wp);
504 for(int i=0;i<wp;i++)
505 eDebugNoNewLine("%02x ", m_capmt[i]);
515 m_retryTimer.start(0,true);
516 // eDebug("[eDVBCAHandler] send failed .. immediate retry");
519 m_retryTimer.start(5000,true);
520 // eDebug("[eDVBCAHandler] send failed .. retry in 5 sec");