2 * PremiereEpg plugin to VDR (C++)
4 * (C) 2005-2006 Stefan Huelswitt <s.huelswitt@gmx.de>
6 * This code is base on the commandline tool premiereepg2vdr
7 * (C) 2004-2005 by Axel Katzur software@katzur.de
8 * but has been rewritten from scratch
10 * This code is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This code is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 #include <vdr/plugin.h>
27 #include <vdr/filter.h>
29 #include <vdr/channels.h>
30 #include <vdr/dvbdevice.h>
32 #include <vdr/config.h>
33 #include <libsi/section.h>
34 #include <libsi/descriptor.h>
45 #define d2(x) { (x); }
50 #define PMT_SCAN_TIMEOUT 10 // seconds
51 #define PMT_SCAN_IDLE 300 // seconds
53 static const char *VERSION = "0.0.7";
54 static const char *DESCRIPTION = "Parses extended Premiere EPG data";
56 #if APIVERSNUM < 10401
57 #error You need at least VDR API version 1.4.1 for this plugin
60 // --- cSetupPremiereEpg -------------------------------------------------------
62 const char *optPats[] = {
69 #define NUM_PATS (sizeof(optPats)/sizeof(char *))
71 class cSetupPremiereEpg {
78 cSetupPremiereEpg(void);
81 cSetupPremiereEpg SetupPE;
83 cSetupPremiereEpg::cSetupPremiereEpg(void)
91 // --- i18n --------------------------------------------------------------------
93 const tI18nPhrase Phrases[] = {
113 { "Parses extended Premiere EPG data",
114 "Liest erweiterte Premiere EPG Daten ein",
130 { "Tag option events",
131 "Options Events markieren",
147 { "Show order information",
148 "Bestellhinweise anzeigen",
164 { "Show rating information",
165 "Altersfreigaben anzeigen",
302 "EPG Daten korrigieren",
322 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
324 class cMenuSetupPremiereEpg : public cMenuSetupPage {
326 cSetupPremiereEpg data;
327 const char *optDisp[NUM_PATS];
328 char buff[NUM_PATS][32];
330 virtual void Store(void);
332 cMenuSetupPremiereEpg(void);
335 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
338 SetSection(tr("PremiereEPG"));
339 optDisp[0]=tr("off");
340 for(unsigned int i=1; i<NUM_PATS; i++) {
341 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
344 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
345 Add(new cMenuEditBoolItem(tr("Show order information"),&data.OrderInfo));
346 Add(new cMenuEditBoolItem(tr("Show rating information"),&data.RatingInfo));
347 Add(new cMenuEditBoolItem(tr("Fix EPG data"),&data.FixEpg));
350 void cMenuSetupPremiereEpg::Store(void)
353 SetupStore("OptionPattern",SetupPE.OptPat);
354 SetupStore("OrderInfo",SetupPE.OrderInfo);
355 SetupStore("RatingInfo",SetupPE.RatingInfo);
356 SetupStore("FixEpg",SetupPE.FixEpg);
359 // --- CRC16 -------------------------------------------------------------------
361 #define POLY 0xA001 // CRC16
363 unsigned int crc16(unsigned int crc, unsigned char const *p, int len)
367 for(int i=0; i<8; i++)
368 crc=(crc&1) ? (crc>>1)^POLY : (crc>>1);
373 // --- cFilterPremiereEpg ------------------------------------------------------
375 #define STARTTIME_BIAS (20*60)
377 class cFilterPremiereEpg : public cFilter {
379 int pmtpid, pmtsid, pmtidx, pmtnext;
383 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
385 cFilterPremiereEpg(void);
386 virtual void SetStatus(bool On);
390 cFilterPremiereEpg::cFilterPremiereEpg(void)
396 void cFilterPremiereEpg::Trigger(void)
398 d(printf("trigger\n"))
399 pmtpid=0; pmtidx=0; pmtnext=0;
402 void cFilterPremiereEpg::SetStatus(bool On)
404 d(printf("setstatus %d\n",On))
405 cFilter::SetStatus(On);
409 void cFilterPremiereEpg::NextPmt(void)
414 d(printf("PMT next\n"))
417 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
420 if(Pid==0 && Tid==SI::TableIdPAT) {
421 if(!pmtnext || now>pmtnext) {
422 if(pmtpid) NextPmt();
424 SI::PAT pat(Data,false);
425 if(pat.CheckCRCAndParse()) {
426 SI::PAT::Association assoc;
428 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
429 if(!assoc.isNITPid()) {
431 pmtpid=assoc.getPid();
432 pmtsid=assoc.getServiceId();
434 pmtnext=now+PMT_SCAN_TIMEOUT;
435 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
442 pmtnext=now+PMT_SCAN_IDLE;
443 d(printf("PMT scan idle\n"))
449 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
450 SI::PMT pmt(Data,false);
451 if(pmt.CheckCRCAndParse() && pmt.getServiceId()==pmtsid) {
452 SI::PMT::Stream stream;
453 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
454 if(stream.getStreamType()==0x05) {
455 SI::CharArray data=stream.getData();
456 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
457 bool prvData=false, usrData=false;
459 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
460 switch(d->getDescriptorTag()) {
461 case SI::PrivateDataSpecifierDescriptorTag:
462 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
463 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
467 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
468 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
476 if(prvData && usrData) {
477 int pid=stream.getPid();
478 d(printf("found citpid 0x%04x",pid))
479 if(!Matches(pid,0xA0)) {
481 d(printf(" (added)"))
488 NextPmt(); pmtnext=0;
491 else if(Tid==0xA0 && Source()) {
492 SI::PremiereCIT cit(Data,false);
493 if(cit.CheckCRCAndParse()) {
494 cSchedulesLock SchedulesLock(true,10);
495 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
498 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
499 SI::ShortEventDescriptor *ShortEventDescriptor=0;
500 char *order=0, *rating=0;
504 bool UseExtendedEventDescriptor=false;
505 int LanguagePreferenceShort=-1;
506 int LanguagePreferenceExt=-1;
507 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
508 switch(d->getDescriptorTag()) {
509 case 0xF0: // order information
510 if(SetupPE.OrderInfo) {
511 static const char *text[] = {
520 const unsigned char *data=d->getData().getData()+2;
521 for(int i=0; i<5; i++) {
523 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %.*s",tr(text[i]),l,&data[1]);
526 if(p>0) order=strdup(buff);
529 case 0xF1: // parental rating
530 if(SetupPE.RatingInfo) {
533 const unsigned char *data=d->getData().getData()+2;
534 p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %d %s",tr("Rating"),data[0]+3,tr("years"));
537 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p," (%.*s)",l,&data[1]);
538 if(p>0) rating=strdup(buff);
541 case SI::PremiereContentTransmissionDescriptorTag:
543 SI::PremiereContentTransmissionDescriptor *pct=(SI::PremiereContentTransmissionDescriptor *)d;
545 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
546 SI::Loop::Iterator it;
547 if(pct->startDayLoop.getNext(sd,it)) {
548 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
549 SI::Loop::Iterator it2;
550 if(sd.startTimeLoop.getNext(st,it2)) {
551 time_t StartTime=st.getStartTime(sd.getMJD());
552 if(nCount==1) firstTime=StartTime;
553 else if(firstTime<StartTime-5*50 || firstTime>StartTime+5*60)
559 case SI::ExtendedEventDescriptorTag:
561 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
562 if(I18nIsPreferredLanguage(Setup.EPGLanguages,eed->languageCode, LanguagePreferenceExt) || !ExtendedEventDescriptors) {
563 delete ExtendedEventDescriptors;
564 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
565 UseExtendedEventDescriptor=true;
567 if(UseExtendedEventDescriptor) {
568 ExtendedEventDescriptors->Add(eed);
569 d=NULL; // so that it is not deleted
571 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
572 UseExtendedEventDescriptor=false;
575 case SI::ShortEventDescriptorTag:
577 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
578 if(I18nIsPreferredLanguage(Setup.EPGLanguages,sed->languageCode, LanguagePreferenceShort) || !ShortEventDescriptor) {
579 delete ShortEventDescriptor;
580 ShortEventDescriptor=sed;
581 d=NULL; // so that it is not deleted
596 crc[0]=cit.getContentId();
597 SI::PremiereContentTransmissionDescriptor *pct;
598 for(SI::Loop::Iterator it; (pct=(SI::PremiereContentTransmissionDescriptor *)cit.eventDescriptors.getNext(it,SI::PremiereContentTransmissionDescriptorTag)); ) {
599 int nid=pct->getOriginalNetworkId();
600 int tid=pct->getTransportStreamId();
601 int sid=pct->getServiceId();
604 if (tid==0x03 && sid==0xf0) { tid=0x02; sid=0xe0; }
605 else if(tid==0x03 && sid==0xf1) { tid=0x02; sid=0xe1; }
606 else if(tid==0x03 && sid==0xf5) { tid=0x03; sid=0xdc; }
607 else if(tid==0x04 && sid==0xd2) { tid=0x11; sid=0xe2; }
608 else if(tid==0x11 && sid==0xd3) { tid=0x11; sid=0xe3; }
611 tChannelID channelID(Source(),nid,tid,sid);
612 cChannel *channel=Channels.GetByChannelID(channelID,true);
613 if(!channel) continue;
615 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
617 pSchedule=new cSchedule(channelID);
618 Schedules->Add(pSchedule);
622 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
624 for(SI::Loop::Iterator it; pct->startDayLoop.getNext(sd,it); ) {
626 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
627 for(SI::Loop::Iterator it2; sd.startTimeLoop.getNext(st,it2); ) {
628 time_t StartTime=st.getStartTime(mjd);
629 time_t EndTime=StartTime+cit.getDuration();
630 int runningStatus=(StartTime<now && now<EndTime) ? SI::RunningStatusRunning : ((StartTime-30<now && now<StartTime) ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusNotRunning);
632 if(index++==0 && nCount>1) isOpt=true;
633 crc[1]=isOpt ? optCount : 0;
634 crc[2]=StartTime / STARTTIME_BIAS;
635 tEventID EventId=((('P'<<8)|'W')<<16) | crc16(0,(unsigned char *)crc,sizeof(crc));
637 d2(printf("%s R%d %04x/%.4x %d %d/%d %s +%d ",*channelID.ToString(),runningStatus,EventId&0xFFFF,cit.getContentId(),index,isOpt,optCount,stripspace(ctime(&StartTime)),(int)cit.getDuration()/60))
638 if(EndTime+Setup.EPGLinger*60<now) {
639 d2(printf("(old)\n"))
644 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,-1);
646 d2(printf("(new)\n"))
647 pEvent=new cEvent(EventId);
648 if(!pEvent) continue;
652 d2(printf("(upd)\n"))
654 if(pEvent->TableID()==0x00 || pEvent->Version()==cit.getVersionNumber()) {
655 if(pEvent->RunningStatus()!=runningStatus)
656 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
660 pEvent->SetEventID(EventId);
661 pEvent->SetTableID(Tid);
662 pEvent->SetVersion(cit.getVersionNumber());
663 pEvent->SetStartTime(StartTime);
664 pEvent->SetDuration(cit.getDuration());
666 if(ShortEventDescriptor) {
668 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
670 char buffer2[sizeof(buffer)+32];
671 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
672 pEvent->SetTitle(buffer2);
675 pEvent->SetTitle(buffer);
676 d2(printf("title: %s\n",pEvent->Title()))
677 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
679 if(ExtendedEventDescriptors) {
680 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
681 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
683 if(order || rating) {
684 int len=(pEvent->Description() ? strlen(pEvent->Description()) : 0) +
685 (order ? strlen(order) : 0) +
686 (rating ? strlen(rating) : 0);
689 if(pEvent->Description()) strcat(buffer,pEvent->Description());
690 if(rating) strcat(buffer,rating);
691 if(order) strcat(buffer,order);
692 pEvent->SetDescription(buffer);
695 if(newEvent) pSchedule->AddEvent(pEvent);
696 pEvent->SetComponents(NULL);
697 pEvent->FixEpgBugs();
698 if(pEvent->RunningStatus()!=runningStatus)
699 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
700 pSchedule->DropOutdated(StartTime,EndTime,Tid,cit.getVersionNumber());
706 Schedules->SetModified(pSchedule);
711 delete ExtendedEventDescriptors;
712 delete ShortEventDescriptor;
720 // --- cPluginPremiereEpg ------------------------------------------------------
722 class cPluginPremiereEpg : public cPlugin {
725 cFilterPremiereEpg *filter;
727 } epg[MAXDVBDEVICES];
729 cPluginPremiereEpg(void);
730 virtual const char *Version(void) { return VERSION; }
731 virtual const char *Description(void) { return tr(DESCRIPTION); }
732 virtual bool Start(void);
733 virtual void Stop(void);
734 virtual cMenuSetupPage *SetupMenu(void);
735 virtual bool SetupParse(const char *Name, const char *Value);
738 cPluginPremiereEpg::cPluginPremiereEpg(void)
740 memset(epg,0,sizeof(epg));
743 bool cPluginPremiereEpg::Start(void)
745 RegisterI18n(Phrases);
746 for(int i=0; i<MAXDVBDEVICES; i++) {
747 cDevice *dev=cDevice::GetDevice(i);
750 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
751 isyslog("Attached premiere EPG filter to device %d",i);
757 void cPluginPremiereEpg::Stop(void)
759 for(int i=0; i<MAXDVBDEVICES; i++) {
760 cDevice *dev=epg[i].device;
761 if(dev) dev->Detach(epg[i].filter);
762 delete epg[i].filter;
768 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
770 return new cMenuSetupPremiereEpg;
773 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
775 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
776 else if (!strcasecmp(Name, "OrderInfo")) SetupPE.OrderInfo = atoi(Value);
777 else if (!strcasecmp(Name, "RatingInfo")) SetupPE.RatingInfo = atoi(Value);
778 else if (!strcasecmp(Name, "FixEpg")) SetupPE.FixEpg = atoi(Value);
783 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!