1 #include <lib/base/nconfig.h> // access to python config
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/pmt.h>
4 #include <lib/dvb/specs.h>
5 #include <lib/dvb/dvb.h>
6 #include <lib/dvb/metaparser.h>
7 #include <lib/dvb_ci/dvbci.h>
8 #include <lib/dvb/epgcache.h>
9 #include <lib/dvb/scan.h>
10 #include <lib/dvb_ci/dvbci_session.h>
11 #include <dvbsi++/ca_descriptor.h>
12 #include <dvbsi++/ca_program_map_section.h>
13 #include <dvbsi++/teletext_descriptor.h>
14 #include <dvbsi++/descriptor_tag.h>
15 #include <dvbsi++/iso639_language_descriptor.h>
16 #include <dvbsi++/stream_identifier_descriptor.h>
17 #include <dvbsi++/subtitling_descriptor.h>
18 #include <dvbsi++/teletext_descriptor.h>
19 #include <dvbsi++/video_stream_descriptor.h>
20 #include <dvbsi++/registration_descriptor.h>
21 #include <dvbsi++/ac3_descriptor.h>
23 #include <dvbsi++/simple_application_location_descriptor.h>
24 #include <dvbsi++/simple_application_boundary_descriptor.h>
25 #include <dvbsi++/transport_protocol_descriptor.h>
26 #include <dvbsi++/application_name_descriptor.h>
29 eDVBServicePMTHandler::eDVBServicePMTHandler()
30 :m_ca_servicePtr(0), m_dvb_scan(0), m_decode_demux_num(0xFF), m_no_pat_entry_delay(eTimer::create())
32 m_use_decode_demux = 0;
35 m_service_type = livetv;
37 m_ca_disabled = false;
39 eDVBResourceManager::getInstance(m_resourceManager);
40 CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
41 CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
42 CONNECT(m_no_pat_entry_delay->timeout, eDVBServicePMTHandler::sendEventNoPatEntry);
44 CONNECT(m_AIT.tableReady, eDVBServicePMTHandler::AITready);
45 CONNECT(m_OC.tableReady, eDVBServicePMTHandler::OCready);
48 eDVBServicePMTHandler::~eDVBServicePMTHandler()
53 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
56 channel->getState(state);
58 if ((m_last_channel_state != iDVBChannel::state_ok)
59 && (state == iDVBChannel::state_ok) && (!m_demux))
65 m_demux = m_pvr_demux_tmp;
66 m_pvr_demux_tmp = NULL;
68 else if (m_channel->getDemux(m_demux, (!m_use_decode_demux) ? 0 : iDVBChannel::capDecode))
69 eDebug("Allocating %s-decoding a demux for now tuned-in channel failed.", m_use_decode_demux ? "" : "non-");
72 serviceEvent(eventTuned);
76 eDebug("ok ... now we start!!");
78 if (!m_service || m_service->usePMT())
81 m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
83 m_PMT.begin(eApp, eDVBPMTSpec(m_pmt_pid, m_reference.getServiceID().get()), m_demux);
86 if ( m_service && !m_service->cacheEmpty() )
87 serviceEvent(eventNewProgramInfo);
89 } else if ((m_last_channel_state != iDVBChannel::state_failed) &&
90 (state == iDVBChannel::state_failed))
92 eDebug("tune failed.");
93 serviceEvent(eventTuneFailed);
97 void eDVBServicePMTHandler::channelEvent(iDVBChannel *channel, int event)
101 case iDVBChannel::evtPreStart:
102 serviceEvent(eventPreStart);
104 case iDVBChannel::evtEOF:
105 serviceEvent(eventEOF);
107 case iDVBChannel::evtSOF:
108 serviceEvent(eventSOF);
110 case iDVBChannel::evtFailed+3:
111 serviceEvent(eventNoDiskSpace);
118 void eDVBServicePMTHandler::registerCAService()
120 int demuxes[2] = {0,0};
122 m_demux->getCADemuxID(demuxid);
124 if (m_decode_demux_num != 0xFF)
125 demuxes[1]=m_decode_demux_num;
127 demuxes[1]=demuxes[0];
128 eDVBCAService::register_service(m_reference, demuxes, m_ca_servicePtr);
131 void eDVBServicePMTHandler::PMTready(int error)
134 serviceEvent(eventNoPMT);
138 m_have_cached_program = false;
139 serviceEvent(eventNewProgramInfo);
141 mDemuxId = m_decode_demux_num;
144 eEPGCache::getInstance()->PMTready(this);
155 eDVBCIInterfaces::getInstance()->recheckPMTHandlers();
156 eDVBCIInterfaces::getInstance()->gotPMT(this);
161 ePtr<eTable<ProgramMapSection> > ptr;
162 if (!m_PMT.getCurrent(ptr))
163 m_ca_servicePtr->buildCAPMT(ptr);
165 eDebug("eDVBServicePMTHandler cannot call buildCAPMT");
168 if (m_service_type == pvrDescramble)
169 serviceEvent(eventStartPvrDescramble);
173 void eDVBServicePMTHandler::sendEventNoPatEntry()
175 serviceEvent(eventNoPATEntry);
178 void eDVBServicePMTHandler::PATready(int)
181 ePtr<eTable<ProgramAssociationSection> > ptr;
182 if (!m_PAT.getCurrent(ptr))
184 int service_id_single = -1;
185 int pmtpid_single = -1;
188 std::vector<ProgramAssociationSection*>::const_iterator i;
189 for (i = ptr->getSections().begin(); pmtpid == -1 && i != ptr->getSections().end(); ++i)
191 const ProgramAssociationSection &pat = **i;
192 ProgramAssociationConstIterator program;
193 for (program = pat.getPrograms()->begin(); pmtpid == -1 && program != pat.getPrograms()->end(); ++program)
196 if (eServiceID((*program)->getProgramNumber()) == m_reference.getServiceID())
197 pmtpid = (*program)->getProgramMapPid();
198 if (++cnt == 1 && pmtpid_single == -1 && pmtpid == -1)
200 pmtpid_single = (*program)->getProgramMapPid();
201 service_id_single = (*program)->getProgramNumber();
204 pmtpid_single = service_id_single = -1;
207 if (pmtpid_single != -1) // only one PAT entry .. and not valid pmtpid found
209 eDebug("use single pat entry!");
210 m_reference.setServiceID(eServiceID(service_id_single));
211 pmtpid = pmtpid_single;
214 eDebug("no PAT entry found.. start delay");
215 m_no_pat_entry_delay->start(1000, true);
218 eDebug("use pmtpid %04x for service_id %04x", pmtpid, m_reference.getServiceID().get());
219 m_no_pat_entry_delay->stop();
220 m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
223 serviceEvent(eventNoPAT);
226 static void eraseHbbTVApplications(HbbTVApplicationInfoList *applications)
228 if(applications->size() == 0)
230 for(HbbTVApplicationInfoListConstIterator info = applications->begin() ; info != applications->end() ; ++info)
232 applications->clear();
235 void saveData(int orgid, unsigned char* data, int sectionLength)
238 char fileName[255] = {0};
239 sprintf(fileName, "/tmp/ait.%d", orgid);
242 eDebug("section_number %d > 0", data[6]);
245 if (data[7] > data[6]) {
246 eDebug("last_section_number %d > section_number %d", data[7], data[6]);
250 if((fd = open(fileName, O_RDWR|O_CREAT|O_TRUNC)) < 0)
252 eDebug("Fail to save a AIT Data.");
255 rc = write(fd, data, sectionLength);
256 eDebug("Save Data Len : [%d]", rc);
260 #include <dvbsi++/application_profile.h>
261 #include <dvbsi++/application_descriptor.h>
262 #include <dvbsi++/simple_application_boundary_descriptor.h>
263 #define PACK_VERSION(major,minor,micro) (((major) << 16) + ((minor) << 8) + (micro))
264 #define UNPACK_VERSION(version,major,minor,micro) { \
265 major = (version)&0xff; \
266 minor = (version>>8)&0xff; \
267 micro = (version>>16)&0xff; \
269 void eDVBServicePMTHandler::AITready(int error)
272 ePtr<eTable<ApplicationInformationSection> > ptr;
273 if (!m_AIT.getCurrent(ptr))
275 short profilecode = 0;
276 int orgid = 0, appid = 0, profileVersion = 0;
277 m_ApplicationName = m_HBBTVUrl = "";
279 eraseHbbTVApplications(&m_HbbTVApplications);
281 int sectionLength = 0;
282 for (std::vector<ApplicationInformationSection*>::const_iterator it = ptr->getSections().begin(); it != ptr->getSections().end(); ++it)
284 std::list<ApplicationInformation *>::const_iterator i = (*it)->getApplicationInformation()->begin();
285 memcpy(m_AITData, ptr->getBufferData(), 4096);
286 sectionLength = (*it)->getSectionLength() + 3;
287 eDebug("Section Length : %d, Total Section Length : %d", (*it)->getSectionLength(), sectionLength);
288 for (; i != (*it)->getApplicationInformation()->end(); ++i)
290 std::string hbbtvUrl = "", applicaionName = "";
291 std::string boundaryExtension = "";
293 int controlCode = (*i)->getApplicationControlCode();
294 const ApplicationIdentifier * applicationIdentifier = (*i)->getApplicationIdentifier();
296 orgid = applicationIdentifier->getOrganisationId();
297 appid = applicationIdentifier->getApplicationId();
298 eDebug("found applicaions ids >> pid : %x, orgid : %d, appid : %d", m_ait_pid, orgid, appid);
299 if (controlCode == 1)
300 saveData(orgid, m_AITData, sectionLength);
301 if (controlCode == 1 || controlCode == 2) /* 1:AUTOSTART, 2:ETC */
303 for (DescriptorConstIterator desc = (*i)->getDescriptors()->begin();
304 desc != (*i)->getDescriptors()->end(); ++desc)
306 switch ((*desc)->getTag())
308 case APPLICATION_DESCRIPTOR:
310 ApplicationDescriptor* applicationDescriptor = (ApplicationDescriptor*)(*desc);
311 const ApplicationProfileList* applicationProfiles = applicationDescriptor->getApplicationProfiles();
312 ApplicationProfileConstIterator interactionit = applicationProfiles->begin();
313 for(; interactionit != applicationProfiles->end(); ++interactionit)
315 profilecode = (*interactionit)->getApplicationProfile();
316 profileVersion = PACK_VERSION(
317 (*interactionit)->getVersionMajor(),
318 (*interactionit)->getVersionMinor(),
319 (*interactionit)->getVersionMicro()
324 case APPLICATION_NAME_DESCRIPTOR:
326 ApplicationNameDescriptor *nameDescriptor = (ApplicationNameDescriptor*)(*desc);
327 ApplicationNameConstIterator interactionit = nameDescriptor->getApplicationNames()->begin();
328 for(; interactionit != nameDescriptor->getApplicationNames()->end(); ++interactionit)
330 applicaionName = (*interactionit)->getApplicationName();
331 if(controlCode == 1) m_ApplicationName = applicaionName;
336 case TRANSPORT_PROTOCOL_DESCRIPTOR:
338 TransportProtocolDescriptor *transport = (TransportProtocolDescriptor*)(*desc);
339 switch (transport->getProtocolId())
341 case 1: /* object carousel */
342 if (m_dsmcc_pid >= 0)
344 m_OC.begin(eApp, eDVBDSMCCDLDataSpec(m_dsmcc_pid), m_demux);
349 case 3: /* interaction */
351 InterActionTransportConstIterator interactionit = transport->getInteractionTransports()->begin();
352 for(; interactionit != transport->getInteractionTransports()->end(); ++interactionit)
354 hbbtvUrl = (*interactionit)->getUrlBase()->getUrl();
362 case GRAPHICS_CONSTRAINTS_DESCRIPTOR:
364 case SIMPLE_APPLICATION_LOCATION_DESCRIPTOR:
366 SimpleApplicationLocationDescriptor *applicationlocation = (SimpleApplicationLocationDescriptor*)(*desc);
367 hbbtvUrl += applicationlocation->getInitialPath();
370 case APPLICATION_USAGE_DESCRIPTOR:
372 case SIMPLE_APPLICATION_BOUNDARY_DESCRIPTOR:
377 if(!hbbtvUrl.empty())
379 const char* uu = hbbtvUrl.c_str();
380 if(!strncmp(uu, "http://", 7) || !strncmp(uu, "dvb://", 6) || !strncmp(uu, "https://", 8))
382 if(controlCode == 1) m_HBBTVUrl = hbbtvUrl;
383 switch(profileVersion)
387 m_HbbTVApplications.push_back(new HbbTVApplicationInfo(controlCode, orgid, appid, hbbtvUrl, applicaionName, profilecode));
392 m_HbbTVApplications.push_back(new HbbTVApplicationInfo((-1)*controlCode, orgid, appid, hbbtvUrl, applicaionName, profilecode));
396 else if (!boundaryExtension.empty()) {
397 if(boundaryExtension.at(boundaryExtension.length()-1) != '/') {
398 boundaryExtension += "/";
400 boundaryExtension += hbbtvUrl;
401 if(controlCode == 1) m_HBBTVUrl = boundaryExtension;
402 switch(profileVersion)
406 m_HbbTVApplications.push_back(new HbbTVApplicationInfo(controlCode, orgid, appid, boundaryExtension, applicaionName, profilecode));
411 m_HbbTVApplications.push_back(new HbbTVApplicationInfo((-1)*controlCode, orgid, appid, boundaryExtension, applicaionName, profilecode));
419 if (m_HbbTVApplications.size())
421 for(HbbTVApplicationInfoListConstIterator infoiter = m_HbbTVApplications.begin() ; infoiter != m_HbbTVApplications.end() ; ++infoiter)
423 char fileName[255] = {0};
424 sprintf(fileName, "/tmp/ait.%d", (*infoiter)->m_OrgId);
425 if (access(fileName, 0) < 0)
426 saveData((*infoiter)->m_OrgId, m_AITData, sectionLength);
427 eDebug("Found : control[%d], name[%s], url[%s]",
428 (*infoiter)->m_ControlCode, (*infoiter)->m_ApplicationName.c_str(), (*infoiter)->m_HbbTVUrl.c_str());
430 serviceEvent(eventHBBTVInfo);
432 else eDebug("No found anything.");
434 /* for now, do not keep listening for table updates */
438 void eDVBServicePMTHandler::OCready(int error)
441 ePtr<eTable<OCSection> > ptr;
442 if (!m_OC.getCurrent(ptr))
444 for (std::vector<OCSection*>::const_iterator it = ptr->getSections().begin(); it != ptr->getSections().end(); ++it)
446 unsigned char* sectionData = (unsigned char*)(*it)->getData();
449 /* for now, do not keep listening for table updates */
453 PyObject *eDVBServicePMTHandler::getCaIds(bool pair)
459 if ( !getProgramInfo(prog) )
463 int cnt=prog.caids.size();
467 std::list<program::capid_pair>::iterator it(prog.caids.begin());
470 ePyObject tuple = PyTuple_New(2);
471 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(it->caid));
472 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong((it++)->capid));
473 PyList_SET_ITEM(ret, cnt, tuple);
479 std::set<program::capid_pair> set(prog.caids.begin(), prog.caids.end());
480 std::set<program::capid_pair>::iterator it(set.begin());
484 PyList_SET_ITEM(ret, cnt, PyInt_FromLong((it++)->caid));
488 return ret ? (PyObject*)ret : (PyObject*)PyList_New(0);
491 PyObject *eDVBServicePMTHandler::getHbbTVApplications(void)
493 ePyObject ret= PyList_New(0);;
494 if(m_HbbTVApplications.size())
496 for(HbbTVApplicationInfoListConstIterator infoiter = m_HbbTVApplications.begin() ; infoiter != m_HbbTVApplications.end() ; ++infoiter)
498 ePyObject tuple = PyTuple_New(6);
499 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong((*infoiter)->m_ControlCode));
500 PyTuple_SET_ITEM(tuple, 1, PyString_FromString((*infoiter)->m_ApplicationName.c_str()));
501 PyTuple_SET_ITEM(tuple, 2, PyString_FromString((*infoiter)->m_HbbTVUrl.c_str()));
502 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong((*infoiter)->m_OrgId));
503 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong((*infoiter)->m_AppId));
504 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong((*infoiter)->m_ProfileCode));
505 PyList_Append(ret, tuple);
509 return (PyObject*)ret;
512 int eDVBServicePMTHandler::getProgramInfo(program &program)
514 ePtr<eTable<ProgramMapSection> > ptr;
515 int cached_apid_ac3 = -1;
516 int cached_apid_ddp = -1;
517 int cached_apid_mpeg = -1;
518 int cached_apid_aache = -1;
519 int cached_apid_aac = -1;
520 int cached_vpid = -1;
521 int cached_tpid = -1;
524 program.videoStreams.clear();
525 program.audioStreams.clear();
528 program.textPid = -1;
530 program.isCached = false;
531 program.pmtVersion = -1;
534 program.defaultAudioStream = 0;
535 audioStream *prev_audio = 0;
537 if ( m_service && !m_service->cacheEmpty() )
539 cached_vpid = m_service->getCacheEntry(eDVBService::cVPID);
540 cached_apid_mpeg = m_service->getCacheEntry(eDVBService::cMPEGAPID);
541 cached_apid_ac3 = m_service->getCacheEntry(eDVBService::cAC3PID);
542 cached_apid_ddp = m_service->getCacheEntry(eDVBService::cDDPPID);
543 cached_apid_aache = m_service->getCacheEntry(eDVBService::cAACHEAPID);
544 cached_apid_aac = m_service->getCacheEntry(eDVBService::cAACAPID);
545 cached_tpid = m_service->getCacheEntry(eDVBService::cTPID);
548 if ( ((m_service && m_service->usePMT()) || !m_service) && !m_PMT.getCurrent(ptr))
550 if (m_have_cached_program)
552 program = m_cached_program;
557 eDVBTableSpec table_spec;
558 ptr->getSpec(table_spec);
559 program.pmtPid = table_spec.pid < 0x1fff ? table_spec.pid : -1;
560 program.pmtVersion = table_spec.version;
561 std::vector<ProgramMapSection*>::const_iterator i;
562 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
564 const ProgramMapSection &pmt = **i;
567 program.pcrPid = pmt.getPcrPid();
569 for (DescriptorConstIterator desc = pmt.getDescriptors()->begin();
570 desc != pmt.getDescriptors()->end(); ++desc)
572 if ((*desc)->getTag() == CA_DESCRIPTOR)
574 CaDescriptor *descr = (CaDescriptor*)(*desc);
575 program::capid_pair pair;
576 pair.caid = descr->getCaSystemId();
577 pair.capid = descr->getCaPid();
578 program.caids.push_back(pair);
580 else if ((*desc)->getTag() == REGISTRATION_DESCRIPTOR)
582 RegistrationDescriptor *d = (RegistrationDescriptor*)(*desc);
583 if (d->getFormatIdentifier() == 0x48444d56) // HDMV
588 ElementaryStreamInfoConstIterator es;
589 for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
591 int isaudio = 0, isvideo = 0, issubtitle = 0, forced_video = 0, forced_audio = 0, isteletext = 0;
592 int streamtype = (*es)->getType();
595 audio.component_tag=video.component_tag=-1;
596 video.type = videoStream::vtMPEG2;
597 audio.type = audioStream::atMPEG;
602 case 0x1b: // AVC Video Stream (MPEG4 H264)
603 video.type = videoStream::vtMPEG4_H264;
605 //break; fall through !!!
606 case 0x24: // H265 HEVC
609 video.type = videoStream::vtH265_HEVC;
612 //break; fall through !!!
613 case 0x10: // MPEG 4 Part 2
616 video.type = videoStream::vtMPEG4_Part2;
619 //break; fall through !!!
620 case 0x01: // MPEG 1 video
622 video.type = videoStream::vtMPEG1;
623 //break; fall through !!!
624 case 0x02: // MPEG 2 video
627 //break; fall through !!!
628 case 0x03: // MPEG 1 audio
629 case 0x04: // MPEG 2 audio:
634 //break; fall through !!!
635 case 0x0f: // MPEG 2 AAC
636 if (!isvideo && !isaudio)
639 audio.type = audioStream::atAAC;
642 //break; fall through !!!
643 case 0x11: // MPEG 4 AAC
644 if (!isvideo && !isaudio)
647 audio.type = audioStream::atAACHE;
650 case 0x80: // user private ... but bluray LPCM
651 case 0xA0: // bluray secondary LPCM
652 if (!isvideo && !isaudio && is_hdmv)
655 audio.type = audioStream::atLPCM;
657 case 0x81: // user private ... but bluray AC3
658 case 0xA1: // bluray secondary AC3
659 if (!isvideo && !isaudio && is_hdmv)
662 audio.type = audioStream::atAC3;
664 case 0x82: // bluray DTS (dvb user private...)
665 case 0xA2: // bluray secondary DTS
666 if (!isvideo && !isaudio && is_hdmv)
669 audio.type = audioStream::atDTS;
671 case 0x86: // bluray DTS-HD (dvb user private...)
672 case 0xA6: // bluray secondary DTS-HD
673 if (!isvideo && !isaudio && is_hdmv)
676 audio.type = audioStream::atDTSHD;
678 case 0x06: // PES Private
679 case 0xEA: // TS_PSI_ST_SMPTE_VC1
681 int num_descriptors = 0;
682 for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
683 desc != (*es)->getDescriptors()->end(); ++desc)
685 uint8_t tag = (*desc)->getTag();
686 /* check descriptors to get the exakt stream type. */
688 if (!forced_video && !forced_audio)
692 case AUDIO_STREAM_DESCRIPTOR:
695 case VIDEO_STREAM_DESCRIPTOR:
698 VideoStreamDescriptor *d = (VideoStreamDescriptor*)(*desc);
699 if (d->getMpeg1OnlyFlag())
700 video.type = videoStream::vtMPEG1;
703 case SUBTITLING_DESCRIPTOR:
705 SubtitlingDescriptor *d = (SubtitlingDescriptor*)(*desc);
706 const SubtitlingList *list = d->getSubtitlings();
708 s.pid = (*es)->getPid();
709 for (SubtitlingConstIterator it(list->begin()); it != list->end(); ++it)
711 s.subtitling_type = (*it)->getSubtitlingType();
712 switch(s.subtitling_type)
715 case 0x20 ... 0x23: // dvb subtitles
718 eDebug("dvb subtitle %s PID %04x with wrong subtitling type (%02x)... force 0x10!!",
719 s.language_code.c_str(), s.pid, s.subtitling_type);
720 s.subtitling_type = 0x10;
723 s.composition_page_id = (*it)->getCompositionPageId();
724 s.ancillary_page_id = (*it)->getAncillaryPageId();
725 s.language_code = (*it)->getIso639LanguageCode();
726 // eDebug("add dvb subtitle %s PID %04x, type %d, composition page %d, ancillary_page %d",
727 // s.language_code.c_str(), s.pid, s.subtitling_type, s.composition_page_id, s.ancillary_page_id);
729 program.subtitleStreams.push_back(s);
733 case TELETEXT_DESCRIPTOR:
734 if ( program.textPid == -1 || (*es)->getPid() == cached_tpid )
737 s.subtitling_type = 0x01; // EBU TELETEXT SUBTITLES
738 s.pid = program.textPid = (*es)->getPid();
739 TeletextDescriptor *d = (TeletextDescriptor*)(*desc);
741 const VbiTeletextList *list = d->getVbiTeletexts();
742 for (VbiTeletextConstIterator it(list->begin()); it != list->end(); ++it)
744 switch((*it)->getTeletextType())
746 case 0x02: // Teletext subtitle page
747 case 0x05: // Teletext subtitle page for hearing impaired pepople
748 s.language_code = (*it)->getIso639LanguageCode();
749 s.teletext_page_number = (*it)->getTeletextPageNumber();
750 s.teletext_magazine_number = (*it)->getTeletextMagazineNumber();
751 // eDebug("add teletext subtitle %s PID %04x, page number %d, magazine number %d",
752 // s.language_code.c_str(), s.pid, s.teletext_page_number, s.teletext_magazine_number);
753 program.subtitleStreams.push_back(s);
763 audio.type = audioStream::atDTS;
765 case 0x2B: // TS_PSI_DT_MPEG2_AAC
767 audio.type = audioStream::atAAC; // MPEG2-AAC
769 case 0x1C: // TS_PSI_DT_MPEG4_Audio
772 audio.type = audioStream::atAACHE; // MPEG4-AAC
776 Ac3Descriptor *ac = (Ac3Descriptor*)(*desc);
779 audio.type = audioStream::atAC3;
781 if(ac->getAc3TypeFlag())
784 uint8_t ac3type = ac->getAc3Type();
785 if( ( ac3type & 0x80 ) && ( (ac3type<<5) == 0xA0 || (ac3type<<5) == 0xC0) ) // From EN-300 468 v1.7.1 Table D.1
786 audio.type = audioStream::atDDP;
791 case ENHANCED_AC3_DESCRIPTOR:
793 audio.type = audioStream::atDDP;
797 case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
799 RegistrationDescriptor *d = (RegistrationDescriptor*)(*desc);
800 switch (d->getFormatIdentifier())
802 case 0x44545331 ... 0x44545333: // DTS1/DTS2/DTS3
804 audio.type = audioStream::atDTS;
806 case 0x41432d33: // == 'AC-3'
808 audio.type = audioStream::atAC3;
810 case 0x42535344: // == 'BSSD' (LPCM)
812 audio.type = audioStream::atLPCM;
814 case 0x56432d31: // == 'VC-1'
816 const AdditionalIdentificationInfoVector *vec = d->getAdditionalIdentificationInfo();
817 if (vec->size() > 1 && (*vec)[0] == 0x01) // subdescriptor tag
819 if ((*vec)[1] >= 0x90) // profile_level
820 video.type = videoStream::vtVC1; // advanced profile
822 video.type = videoStream::vtVC1_SM; // simple main
831 case 0x28: // TS_PSI_DT_AVC
833 video.type = videoStream::vtMPEG4_H264;
835 case 0x1B: // TS_PSI_DT_MPEG4_Video
837 video.type = videoStream::vtMPEG4_Part2;
845 case ISO_639_LANGUAGE_DESCRIPTOR:
849 const Iso639LanguageList *languages = ((Iso639LanguageDescriptor*)*desc)->getIso639Languages();
850 /* use last language code */
851 for (Iso639LanguageConstIterator i(languages->begin()); i != languages->end(); ++i, ++cnt)
854 audio.language_code = (*i)->getIso639LanguageCode();
856 audio.language_code += "/" + (*i)->getIso639LanguageCode();
860 case STREAM_IDENTIFIER_DESCRIPTOR:
861 audio.component_tag =
862 video.component_tag =
863 ((StreamIdentifierDescriptor*)*desc)->getComponentTag();
867 CaDescriptor *descr = (CaDescriptor*)(*desc);
868 program::capid_pair pair;
869 pair.caid = descr->getCaSystemId();
870 pair.capid = descr->getCaPid();
871 program.caids.push_back(pair);
878 if (!num_descriptors && streamtype == 0x06 && prev_audio)
880 prev_audio->rdsPid = (*es)->getPid();
881 eDebug("Rds PID %04x detected ? ! ?", prev_audio->rdsPid);
885 case 0x05: /* ITU-T Rec. H.222.0 | ISO/IEC 13818-1 private sections */
887 for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
888 desc != (*es)->getDescriptors()->end(); ++desc)
891 switch ((*desc)->getTag())
893 case APPLICATION_SIGNALLING_DESCRIPTOR:
894 m_ait_pid = program.aitPid = (*es)->getPid();
895 m_AIT.begin(eApp, eDVBAITSpec(program.aitPid), m_demux);
901 case 0x0b: /* ISO/IEC 13818-6 DSM-CC U-N Messages */
903 for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
904 desc != (*es)->getDescriptors()->end(); ++desc)
906 switch ((*desc)->getTag())
908 case CAROUSEL_IDENTIFIER_DESCRIPTOR:
909 m_dsmcc_pid = (*es)->getPid();
911 case STREAM_IDENTIFIER_DESCRIPTOR:
920 if (isteletext && (isaudio || isvideo))
922 eDebug("ambiguous streamtype for PID %04x detected.. forced as teletext!", (*es)->getPid());
923 continue; // continue with next PID
925 else if (issubtitle && (isaudio || isvideo))
926 eDebug("ambiguous streamtype for PID %04x detected.. forced as subtitle!", (*es)->getPid());
927 else if (isaudio && isvideo)
928 eDebug("ambiguous streamtype for PID %04x detected.. forced as video!", (*es)->getPid());
929 if (issubtitle) // continue with next PID
933 video.pid = (*es)->getPid();
934 if ( !program.videoStreams.empty() && video.pid == cached_vpid )
936 program.videoStreams.push_back(program.videoStreams[0]);
937 program.videoStreams[0] = video;
940 program.videoStreams.push_back(video);
944 audio.pid = (*es)->getPid();
946 /* if we find the cached pids, this will be our default stream */
947 if (audio.pid == cached_apid_ac3 || audio.pid == cached_apid_ddp || audio.pid == cached_apid_mpeg || audio.pid == cached_apid_aache || audio.pid == cached_apid_aac)
948 program.defaultAudioStream = program.audioStreams.size();
950 /* also, we need to know the first non-mpeg (i.e. "ac3"/dts/...) stream */
951 if ((audio.type != audioStream::atMPEG) && ((first_ac3 == -1) || (audio.pid == cached_apid_ac3) || (audio.pid == cached_apid_ddp)))
952 first_ac3 = program.audioStreams.size();
954 program.audioStreams.push_back(audio);
955 prev_audio = &program.audioStreams.back();
963 /* finally some fixup: if our default audio stream is an MPEG audio stream,
964 and we have 'defaultac3' set, use the first available ac3 stream instead.
965 (note: if an ac3 audio stream was selected before, this will be also stored
966 in 'fisrt_ac3', so we don't need to worry. */
967 bool defaultac3 = false;
968 std::string default_ac3;
970 if (!ePythonConfigQuery::getConfigValue("config.av.defaultac3", default_ac3))
971 defaultac3 = default_ac3 == "True";
973 if (defaultac3 && (first_ac3 != -1))
974 program.defaultAudioStream = first_ac3;
976 m_cached_program = program;
977 m_have_cached_program = true;
979 } else if ( m_service && !m_service->cacheEmpty() )
981 int cached_pcrpid = m_service->getCacheEntry(eDVBService::cPCRPID),
982 vpidtype = m_service->getCacheEntry(eDVBService::cVTYPE),
985 program.isCached = true;
987 if ( vpidtype == -1 )
988 vpidtype = videoStream::vtMPEG2;
989 if ( cached_vpid != -1 )
994 program.videoStreams.push_back(s);
997 if ( cached_apid_ac3 != -1 )
1000 s.type = audioStream::atAC3;
1001 s.pid = cached_apid_ac3;
1003 program.audioStreams.push_back(s);
1006 if ( cached_apid_ddp != -1 )
1009 s.type = audioStream::atDDP;
1010 s.pid = cached_apid_ddp;
1012 program.audioStreams.push_back(s);
1015 if ( cached_apid_aache != -1 )
1018 s.type = audioStream::atAACHE;
1019 s.pid = cached_apid_aache;
1021 program.audioStreams.push_back(s);
1024 if ( cached_apid_aac != -1 )
1027 s.type = audioStream::atAAC;
1028 s.pid = cached_apid_aac;
1030 program.audioStreams.push_back(s);
1033 if ( cached_apid_mpeg != -1 )
1036 s.type = audioStream::atMPEG;
1037 s.pid = cached_apid_mpeg;
1039 program.audioStreams.push_back(s);
1042 if ( cached_pcrpid != -1 )
1045 program.pcrPid = cached_pcrpid;
1047 if ( cached_tpid != -1 )
1050 program.textPid = cached_tpid;
1052 CAID_LIST &caids = m_service->m_ca;
1053 for (CAID_LIST::iterator it(caids.begin()); it != caids.end(); ++it) {
1054 program::capid_pair pair;
1056 pair.capid = -1; // not known yet
1057 program.caids.push_back(pair);
1065 int eDVBServicePMTHandler::getChannel(eUsePtr<iDVBChannel> &channel)
1067 channel = m_channel;
1074 int eDVBServicePMTHandler::getDataDemux(ePtr<iDVBDemux> &demux)
1083 int eDVBServicePMTHandler::getDecodeDemux(ePtr<iDVBDemux> &demux)
1086 /* if we're using the decoding demux as data source
1087 (for example in pvr playbacks), return that one. */
1088 if (m_use_decode_demux)
1094 ASSERT(m_channel); /* calling without a previous ::tune is certainly bad. */
1096 ret = m_channel->getDemux(demux, iDVBChannel::capDecode);
1098 demux->getCADemuxID(m_decode_demux_num);
1103 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
1105 pvr_channel = m_pvr_channel;
1112 void eDVBServicePMTHandler::SDTScanEvent(int event)
1116 case eDVBScan::evtFinish:
1118 ePtr<iDVBChannelList> db;
1119 if (m_resourceManager->getChannelList(db) != 0)
1120 eDebug("no channel list");
1124 m_reference.getChannelID(chid);
1125 if (chid == m_dvb_scan->getCurrentChannelID())
1127 m_dvb_scan->insertInto(db, true);
1128 eDebug("sdt update done!");
1131 eDebug("ignore sdt update data.... incorrect transponder tuned!!!");
1141 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *cue, bool simulate, eDVBService *service, serviceType type, bool descramble)
1144 return tuneExt(ref, use_decode_demux, s, NULL, cue, simulate, service, type, descramble);
1147 int eDVBServicePMTHandler::tuneExt(eServiceReferenceDVB &ref, int use_decode_demux, ePtr<iTsSource> &source, const char *streaminfo_file, eCueSheet *cue, bool simulate, eDVBService *service, serviceType type, bool descramble)
1151 m_use_decode_demux = use_decode_demux;
1152 m_no_pat_entry_delay->stop();
1153 m_service_type = type;
1154 m_descramble = descramble;
1156 /* use given service as backup. This is used for timeshift where we want to clone the live stream using the cache, but in fact have a PVR channel */
1157 m_service = service;
1159 /* is this a normal (non PVR) channel? */
1160 if (ref.path.empty())
1163 ref.getChannelID(chid);
1164 res = m_resourceManager->allocateChannel(chid, m_channel, simulate);
1166 eDebug("allocate Channel: res %d", res);
1168 ePtr<iDVBChannelList> db;
1169 if (!m_resourceManager->getChannelList(db))
1170 db->getService((eServiceReferenceDVB&)m_reference, m_service);
1172 if (!res && !simulate && m_descramble && !m_ca_disabled)
1173 eDVBCIInterfaces::getInstance()->addPMTHandler(this);
1174 } else if (!simulate) // no simulation of playback services
1176 if (!ref.getServiceID().get() /* incorrect sid in meta file or recordings.epl*/ )
1178 eDVBTSTools tstools;
1179 bool b = source || !tstools.openFile(ref.path.c_str(), 1);
1180 eWarning("no .meta file found, trying to find PMT pid");
1182 tstools.setSource(source, NULL);
1185 int service_id, pmt_pid;
1186 if (!tstools.findPMT(pmt_pid, service_id))
1188 eDebug("PMT pid found on pid %04x, service id %d", pmt_pid, service_id);
1189 m_reference.setServiceID(service_id);
1190 m_pmt_pid = pmt_pid;
1194 eWarning("no valid source to find PMT pid!");
1196 eDebug("alloc PVR");
1199 if (m_service_type == streamclient) ref.getChannelID(chid);
1200 res = m_resourceManager->allocatePVRChannel(chid, m_pvr_channel);
1203 eDebug("allocatePVRChannel failed!\n");
1205 else if (descramble)
1207 eDVBCIInterfaces::getInstance()->addPMTHandler(this);
1209 m_channel = m_pvr_channel;
1216 m_channel->connectStateChange(
1217 slot(*this, &eDVBServicePMTHandler::channelStateChanged),
1218 m_channelStateChanged_connection);
1219 m_last_channel_state = -1;
1220 channelStateChanged(m_channel);
1222 m_channel->connectEvent(
1223 slot(*this, &eDVBServicePMTHandler::channelEvent),
1224 m_channelEvent_connection);
1226 if (ref.path.empty())
1229 m_dvb_scan = new eDVBScan(m_channel, true, false);
1230 m_dvb_scan->connectEvent(slot(*this, &eDVBServicePMTHandler::SDTScanEvent), m_scan_event_connection);
1234 if (res == eDVBResourceManager::errAllSourcesBusy)
1235 serviceEvent(eventNoResources);
1236 else /* errChidNotFound, errNoChannelList, errChannelNotInList, errNoSourceFound */
1237 serviceEvent(eventMisconfiguration);
1243 m_pvr_channel->setCueSheet(cue);
1245 if (m_pvr_channel->getDemux(m_pvr_demux_tmp, (!m_use_decode_demux) ? 0 : iDVBChannel::capDecode))
1246 eDebug("[eDVBServicePMTHandler] Allocating %s-decoding a demux for PVR channel failed.", m_use_decode_demux ? "" : "non-");
1248 m_pvr_channel->playSource(source, streaminfo_file);
1250 m_pvr_channel->playFile(ref.path.c_str());
1257 void eDVBServicePMTHandler::free()
1261 if (m_ca_servicePtr)
1263 int demuxes[2] = {0,0};
1265 m_demux->getCADemuxID(tmp);
1267 if (m_decode_demux_num != 0xFF)
1268 demuxes[1]=m_decode_demux_num;
1270 demuxes[1]=demuxes[0];
1271 ePtr<eTable<ProgramMapSection> > ptr;
1272 m_PMT.getCurrent(ptr);
1273 eDVBCAService::unregister_service(m_reference, demuxes, ptr);
1274 m_ca_servicePtr = 0;
1278 eDVBCIInterfaces::getInstance()->removePMTHandler(this);
1282 m_pvr_channel->stopFile();
1283 m_pvr_channel->setCueSheet(0);
1297 void eDVBServicePMTHandler::addCaHandler()
1299 m_ca_disabled = false;
1302 eDVBCIInterfaces::getInstance()->addPMTHandler(this);
1305 eDVBCIInterfaces::getInstance()->recheckPMTHandlers();
1306 eDVBCIInterfaces::getInstance()->gotPMT(this);
1311 void eDVBServicePMTHandler::removeCaHandler()
1313 m_ca_disabled = true;
1315 eDVBCIInterfaces::getInstance()->removePMTHandler(this);
1318 bool eDVBServicePMTHandler::isCiConnected()
1320 eDVBCIInterfaces::getInstance()->isCiConnected(this);
1323 CAServiceMap eDVBCAService::exist;
1324 ChannelMap eDVBCAService::exist_channels;
1325 ePtr<eConnection> eDVBCAService::m_chanAddedConn;
1327 eDVBCAService::eDVBCAService()
1328 :m_buffer(512), m_prev_build_hash(0), m_sendstate(0), m_retryTimer(eTimer::create(eApp))
1330 memset(m_used_demux, 0xFF, sizeof(m_used_demux));
1331 CONNECT(m_retryTimer->timeout, eDVBCAService::sendCAPMT);
1335 eDVBCAService::~eDVBCAService()
1337 eDebug("[eDVBCAService] free service %s", m_service.toString().c_str());
1341 // begin static methods
1342 RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice )
1344 CAServiceMap::iterator it = exist.find(ref);
1345 if ( it != exist.end() )
1346 caservice = it->second;
1349 caservice = (exist[ref]=new eDVBCAService());
1350 caservice->m_service = ref;
1351 eDebug("[eDVBCAService] new service %s", ref.toString().c_str() );
1354 int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
1355 for (int i=0; i < loops; ++i)
1357 // search free demux entry
1358 int iter=0, max_demux_slots = sizeof(caservice->m_used_demux);
1360 while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
1363 if ( iter < max_demux_slots )
1365 caservice->m_used_demux[iter] = demux_nums[i] & 0xFF;
1366 eDebug("[eDVBCAService] add demux %d to slot %d service %s", caservice->m_used_demux[iter], iter, ref.toString().c_str());
1370 eDebug("[eDVBCAService] no more demux slots free for service %s!!", ref.toString().c_str());
1377 RESULT eDVBCAService::unregister_service( const eServiceReferenceDVB &ref, int demux_nums[2], eTable<ProgramMapSection> *ptr )
1379 CAServiceMap::iterator it = exist.find(ref);
1380 if ( it == exist.end() )
1382 eDebug("[eDVBCAService] try to unregister non registered %s", ref.toString().c_str());
1387 eDVBCAService *caservice = it->second;
1388 int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
1389 for (int i=0; i < loops; ++i)
1393 used_demux_slots = 0,
1394 max_demux_slots = sizeof(caservice->m_used_demux)/sizeof(int);
1395 while ( iter < max_demux_slots )
1397 if ( caservice->m_used_demux[iter] != 0xFF )
1399 if ( !freed && caservice->m_used_demux[iter] == demux_nums[i] )
1401 eDebug("[eDVBCAService] free slot %d demux %d for service %s", iter, caservice->m_used_demux[iter], caservice->m_service.toString().c_str() );
1402 caservice->m_used_demux[iter] = 0xFF;
1411 eDebug("[eDVBCAService] couldn't free demux slot for demux %d", demux_nums[i]);
1412 if (i || loops == 1)
1414 if (!used_demux_slots) // no more used.. so we remove it
1422 it->second->buildCAPMT(ptr);
1424 eDebug("[eDVBCAService] can not send updated demux info");
1432 void eDVBCAService::registerChannelCallback(eDVBResourceManager *res_mgr)
1434 res_mgr->connectChannelAdded(slot(&DVBChannelAdded), m_chanAddedConn);
1437 void eDVBCAService::DVBChannelAdded(eDVBChannel *chan)
1441 eDebug("[eDVBCAService] new channel %p!", chan);
1442 channel_data *data = new channel_data();
1443 data->m_channel = chan;
1444 data->m_prevChannelState = -1;
1445 data->m_dataDemux = -1;
1446 exist_channels[chan] = data;
1447 chan->connectStateChange(slot(&DVBChannelStateChanged), data->m_stateChangedConn);
1451 void eDVBCAService::DVBChannelStateChanged(iDVBChannel *chan)
1453 ChannelMap::iterator it =
1454 exist_channels.find(chan);
1455 if ( it != exist_channels.end() )
1458 chan->getState(state);
1459 if ( it->second->m_prevChannelState != state )
1463 case iDVBChannel::state_ok:
1465 eDebug("[eDVBCAService] channel %p running", chan);
1468 case iDVBChannel::state_release:
1470 eDebug("[eDVBCAService] remove channel %p", chan);
1471 unsigned char msg[8] = { 0x9f,0x80,0x3f,0x04,0x83,0x02,0x00,0x00 };
1472 msg[7] = it->second->m_dataDemux & 0xFF;
1474 struct sockaddr_un servaddr;
1475 memset(&servaddr, 0, sizeof(struct sockaddr_un));
1476 servaddr.sun_family = AF_UNIX;
1477 strcpy(servaddr.sun_path, "/tmp/camd.socket");
1478 clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
1479 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1482 connect(sock, (struct sockaddr *) &servaddr, clilen);
1483 fcntl(sock, F_SETFL, O_NONBLOCK);
1485 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
1486 if (write(sock, msg, 8) != 8)
1487 eDebug("[eDVBCAService] write leave transponder failed!!");
1490 exist_channels.erase(it);
1495 default: // ignore all other events
1499 it->second->m_prevChannelState = state;
1504 channel_data *eDVBCAService::getChannelData(eDVBChannelID &chid)
1506 for (ChannelMap::iterator it(exist_channels.begin()); it != exist_channels.end(); ++it)
1508 if (chid == it->second->m_channel->getChannelID())
1513 // end static methods
1515 #define CA_REPLY_DEBUG
1516 #define MAX_LENGTH_BYTES 4
1517 #define MIN_LENGTH_BYTES 1
1519 void eDVBCAService::socketCB(int what)
1521 if (what & (eSocketNotifier::Read | eSocketNotifier::Priority))
1523 char msgbuffer[4096];
1524 ssize_t length = read(m_sock, msgbuffer, sizeof(msgbuffer));
1527 if (errno != EAGAIN && errno != EINTR && errno != EBUSY)
1529 eDebug("[eSocketMMIHandler] read (%m)");
1530 what |= eSocketNotifier::Error;
1532 } else if (length == 0)
1534 what |= eSocketNotifier::Hungup;
1538 unsigned char *data = (unsigned char*)msgbuffer;
1540 // If a new message starts, then the previous message
1541 // should already have been processed. Otherwise the
1542 // previous message was incomplete and should therefore
1544 if ((len >= 1) && ((data[0] & 0xFF) != 0x9f))
1546 if ((len >= 2) && ((data[1] & 0x80) != 0x80))
1548 if ((len >= 3) && ((data[2] & 0x80) != 0x00))
1553 #ifdef CA_REPLY_DEBUG
1554 eDebug("clear buffer");
1557 #ifdef CA_REPLY_DEBUG
1558 eDebug("Put to buffer:");
1559 for (int i=0; i < len; ++i)
1560 eDebugNoNewLine("%02x ", data[i]);
1561 eDebug("\n--------");
1563 m_buffer.write( data, len );
1565 while ( m_buffer.size() >= (3 + MIN_LENGTH_BYTES) )
1567 unsigned char tmp[3+MAX_LENGTH_BYTES];
1568 m_buffer.peek(tmp, 3+MIN_LENGTH_BYTES);
1569 if (((tmp[0] & 0xFF) != 0x9f) || ((tmp[1] & 0x80) != 0x80) || ((tmp[2] & 0x80) != 0x00))
1572 #ifdef CA_REPLY_DEBUG
1573 eDebug("skip %02x", tmp[0]);
1579 int peekLength = (tmp[3] & 0x7f) + 4;
1580 if (m_buffer.size() < peekLength)
1582 m_buffer.peek(tmp, peekLength);
1585 int LengthBytes=eDVBCISession::parseLengthField(tmp+3, size);
1586 int messageLength = 3+LengthBytes+size;
1587 if ( m_buffer.size() >= messageLength )
1589 unsigned char dest[messageLength];
1590 m_buffer.read(dest, messageLength);
1591 #ifdef CA_REPLY_DEBUG
1592 eDebug("dump ca reply:");
1593 for (int i=0; i < messageLength; ++i)
1594 eDebugNoNewLine("%02x ", dest[i]);
1595 eDebug("\n--------");
1597 // /*emit*/ mmi_progress(0, dest, (const void*)(dest+3+LengthBytes), messageLength-3-LengthBytes);
1602 if (what & eSocketNotifier::Hungup) {
1603 /*eDebug("[eDVBCAService] connection closed")*/;
1607 if (what & eSocketNotifier::Error)
1608 eDebug("[eDVBCAService] connection error");
1611 void eDVBCAService::Connect()
1614 memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
1615 m_servaddr.sun_family = AF_UNIX;
1616 strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
1617 m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
1618 m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
1621 if (!connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen))
1624 fcntl(m_sock, F_SETFL, O_NONBLOCK);
1625 setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
1626 m_sn = eSocketNotifier::create(eApp, m_sock,
1627 eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Error|eSocketNotifier::Hungup);
1628 CONNECT(m_sn->activated, eDVBCAService::socketCB);
1632 // eDebug("[eDVBCAService] connect failed %m");
1635 eDebug("[eDVBCAService] create socket failed %m");
1638 void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
1643 eDVBTableSpec table_spec;
1644 ptr->getSpec(table_spec);
1646 int pmtpid = table_spec.pid,
1647 pmt_version = table_spec.version;
1649 uint8_t demux_mask = 0;
1650 int data_demux = -1;
1652 int iter=0, max_demux_slots = sizeof(m_used_demux);
1653 while ( iter < max_demux_slots )
1655 if ( m_used_demux[iter] != 0xFF )
1657 if ( m_used_demux[iter] > data_demux )
1658 data_demux = m_used_demux[iter];
1659 demux_mask |= (1 << m_used_demux[iter]);
1664 if ( data_demux == -1 )
1666 eDebug("[eDVBCAService] no data demux found for service %s", m_service.toString().c_str() );
1670 eDebug("demux %d mask %02x prevhash %08x", data_demux, demux_mask, m_prev_build_hash);
1672 unsigned int build_hash = ( pmtpid << 16);
1673 build_hash |= (demux_mask << 8);
1674 build_hash |= (pmt_version&0xFF);
1676 if ( build_hash == m_prev_build_hash )
1678 eDebug("[eDVBCAService] don't build/send the same CA PMT twice");
1682 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
1683 if ( i != ptr->getSections().end() )
1685 CaProgramMapSection capmt(*i++, m_prev_build_hash ? 0x05 /*update*/ : 0x03 /*only*/, 0x01 );
1687 while( i != ptr->getSections().end() )
1689 // eDebug("append");
1693 // add our private descriptors to capmt
1696 tmp[0]=0x84; // pmt pid
1700 capmt.injectDescriptor(tmp, false);
1702 tmp[0] = 0x82; // demux
1704 tmp[2] = demux_mask; // descramble bitmask
1705 tmp[3] = data_demux&0xFF; // read section data from demux number
1706 capmt.injectDescriptor(tmp, false);
1708 tmp[0] = 0x81; // dvbnamespace
1710 tmp[2] = m_service.getDVBNamespace().get()>>24;
1711 tmp[3]=(m_service.getDVBNamespace().get()>>16)&0xFF;
1712 tmp[4]=(m_service.getDVBNamespace().get()>>8)&0xFF;
1713 tmp[5]=m_service.getDVBNamespace().get()&0xFF;
1714 tmp[6]=m_service.getTransportStreamID().get()>>8;
1715 tmp[7]=m_service.getTransportStreamID().get()&0xFF;
1716 tmp[8]=m_service.getOriginalNetworkID().get()>>8;
1717 tmp[9]=m_service.getOriginalNetworkID().get()&0xFF;
1718 capmt.injectDescriptor(tmp, false);
1720 capmt.writeToBuffer(m_capmt);
1723 m_prev_build_hash = build_hash;
1725 if ( m_sendstate != 0xFFFFFFFF )
1730 void eDVBCAService::sendCAPMT()
1732 if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
1739 if ( m_capmt[3] & 0x80 )
1742 int lenbytes = m_capmt[3] & ~0x80;
1744 wp = (wp << 8) | m_capmt[4 + i++];
1754 if ( write(m_sock, m_capmt, wp) == wp )
1756 m_sendstate=0xFFFFFFFF;
1757 eDebug("[eDVBCAService] send %d bytes",wp);
1759 m_service.getChannelID(chid);
1760 channel_data *data = getChannelData(chid);
1763 int lenbytes = m_capmt[3] & 0x80 ? m_capmt[3] & ~0x80 : 0;
1764 data->m_dataDemux = m_capmt[24+lenbytes];
1767 for(int i=0;i<wp;i++)
1768 eDebugNoNewLine("%02x ", m_capmt[i]);
1778 m_retryTimer->start(0,true);
1779 // eDebug("[eDVBCAService] send failed .. immediate retry");
1782 m_retryTimer->start(5000,true);
1783 // eDebug("[eDVBCAService] send failed .. retry in 5 sec");
1790 static PyObject *createTuple(int pid, const char *type)
1792 PyObject *r = PyTuple_New(2);
1793 PyTuple_SET_ITEM(r, 0, PyInt_FromLong(pid));
1794 PyTuple_SET_ITEM(r, 1, PyString_FromString(type));
1798 static inline void PyList_AppendSteal(PyObject *list, PyObject *item)
1800 PyList_Append(list, item);
1804 extern void PutToDict(ePyObject &dict, const char*key, ePyObject item); // defined in dvb/frontend.cpp
1806 PyObject *eDVBServicePMTHandler::program::createPythonObject()
1808 ePyObject r = PyDict_New();
1809 ePyObject l = PyList_New(0);
1811 PyList_AppendSteal(l, createTuple(0, "pat"));
1814 PyList_AppendSteal(l, createTuple(pmtPid, "pmt"));
1816 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
1817 i(videoStreams.begin());
1818 i != videoStreams.end(); ++i)
1819 PyList_AppendSteal(l, createTuple(i->pid, "video"));
1821 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
1822 i(audioStreams.begin());
1823 i != audioStreams.end(); ++i)
1824 PyList_AppendSteal(l, createTuple(i->pid, "audio"));
1826 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
1827 i(subtitleStreams.begin());
1828 i != subtitleStreams.end(); ++i)
1829 PyList_AppendSteal(l, createTuple(i->pid, "subtitle"));
1831 PyList_AppendSteal(l, createTuple(pcrPid, "pcr"));
1834 PyList_AppendSteal(l, createTuple(textPid, "text"));
1836 PutToDict(r, "pids", l);