2 * PremiereEpg plugin to VDR (C++)
4 * (C) 2005-2007 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>
37 #if APIVERSNUM < 10401
38 #error You need at least VDR API version 1.4.1 for this plugin
40 #if APIVERSNUM < 10507
53 #define d2(x) { (x); }
58 #define PMT_SCAN_TIMEOUT 10 // seconds
59 #define PMT_SCAN_IDLE 300 // seconds
61 // --- cSetupPremiereEpg -------------------------------------------------------
63 const char *optPats[] = {
70 #define NUM_PATS (sizeof(optPats)/sizeof(char *))
72 class cSetupPremiereEpg {
79 cSetupPremiereEpg(void);
82 cSetupPremiereEpg SetupPE;
84 cSetupPremiereEpg::cSetupPremiereEpg(void)
92 // --- i18n --------------------------------------------------------------------
94 #if APIVERSNUM < 10507
95 const tI18nPhrase Phrases[] = {
113 { "Parses extended Premiere EPG data",
114 "Liest erweiterte Premiere EPG Daten ein",
147 { "Tag option events",
148 "Options Events markieren",
164 { "Show order information",
165 "Bestellhinweise anzeigen",
181 { "Show rating information",
182 "Altersfreigaben anzeigen",
319 "EPG Daten korrigieren",
340 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
342 class cMenuSetupPremiereEpg : public cMenuSetupPage {
344 cSetupPremiereEpg data;
345 const char *optDisp[NUM_PATS];
346 char buff[NUM_PATS][32];
348 virtual void Store(void);
350 cMenuSetupPremiereEpg(void);
353 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
356 SetSection(tr("PremiereEPG"));
357 optDisp[0]=tr("off");
358 for(unsigned int i=1; i<NUM_PATS; i++) {
359 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
362 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
363 Add(new cMenuEditBoolItem(tr("Show order information"),&data.OrderInfo));
364 Add(new cMenuEditBoolItem(tr("Show rating information"),&data.RatingInfo));
365 Add(new cMenuEditBoolItem(tr("Fix EPG data"),&data.FixEpg));
368 void cMenuSetupPremiereEpg::Store(void)
371 SetupStore("OptionPattern",SetupPE.OptPat);
372 SetupStore("OrderInfo",SetupPE.OrderInfo);
373 SetupStore("RatingInfo",SetupPE.RatingInfo);
374 SetupStore("FixEpg",SetupPE.FixEpg);
377 // --- CRC16 -------------------------------------------------------------------
379 #define POLY 0xA001 // CRC16
381 unsigned int crc16(unsigned int crc, unsigned char const *p, int len)
385 for(int i=0; i<8; i++)
386 crc=(crc&1) ? (crc>>1)^POLY : (crc>>1);
391 // --- cFilterPremiereEpg ------------------------------------------------------
393 #define STARTTIME_BIAS (20*60)
395 class cFilterPremiereEpg : public cFilter {
397 int pmtpid, pmtsid, pmtidx, pmtnext;
401 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
403 cFilterPremiereEpg(void);
404 virtual void SetStatus(bool On);
408 cFilterPremiereEpg::cFilterPremiereEpg(void)
414 void cFilterPremiereEpg::Trigger(void)
416 d(printf("trigger\n"))
417 pmtpid=0; pmtidx=0; pmtnext=0;
420 void cFilterPremiereEpg::SetStatus(bool On)
422 d(printf("setstatus %d\n",On))
423 cFilter::SetStatus(On);
427 void cFilterPremiereEpg::NextPmt(void)
432 d(printf("PMT next\n"))
435 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
438 if(Pid==0 && Tid==SI::TableIdPAT) {
439 if(!pmtnext || now>pmtnext) {
440 if(pmtpid) NextPmt();
442 SI::PAT pat(Data,false);
443 if(pat.CheckCRCAndParse()) {
444 SI::PAT::Association assoc;
446 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
447 if(!assoc.isNITPid()) {
449 pmtpid=assoc.getPid();
450 pmtsid=assoc.getServiceId();
452 pmtnext=now+PMT_SCAN_TIMEOUT;
453 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
460 pmtnext=now+PMT_SCAN_IDLE;
461 d(printf("PMT scan idle\n"))
467 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
468 SI::PMT pmt(Data,false);
469 if(pmt.CheckCRCAndParse() && pmt.getServiceId()==pmtsid) {
470 SI::PMT::Stream stream;
471 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
472 if(stream.getStreamType()==0x05) {
473 SI::CharArray data=stream.getData();
474 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
475 bool prvData=false, usrData=false;
477 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
478 switch(d->getDescriptorTag()) {
479 case SI::PrivateDataSpecifierDescriptorTag:
480 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
481 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
485 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
486 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
494 if(prvData && usrData) {
495 int pid=stream.getPid();
496 d(printf("found citpid 0x%04x",pid))
497 if(!Matches(pid,0xA0)) {
499 d(printf(" (added)"))
506 NextPmt(); pmtnext=0;
509 else if(Tid==0xA0 && Source()) {
510 SI::PremiereCIT cit(Data,false);
511 if(cit.CheckCRCAndParse()) {
512 cSchedulesLock SchedulesLock(true,10);
513 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
516 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
517 SI::ShortEventDescriptor *ShortEventDescriptor=0;
518 char *order=0, *rating=0;
522 bool UseExtendedEventDescriptor=false;
523 int LanguagePreferenceShort=-1;
524 int LanguagePreferenceExt=-1;
525 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
526 switch(d->getDescriptorTag()) {
527 case 0xF0: // order information
528 if(SetupPE.OrderInfo) {
529 static const char *text[] = {
530 trNOOP("Ordernumber"),
538 const unsigned char *data=d->getData().getData()+2;
539 for(int i=0; i<5; i++) {
541 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %.*s",tr(text[i]),l,&data[1]);
544 if(p>0) order=strdup(buff);
547 case 0xF1: // parental rating
548 if(SetupPE.RatingInfo) {
551 const unsigned char *data=d->getData().getData()+2;
552 p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %d %s",tr("Rating"),data[0]+3,tr("years"));
555 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p," (%.*s)",l,&data[1]);
556 if(p>0) rating=strdup(buff);
559 case SI::PremiereContentTransmissionDescriptorTag:
561 SI::PremiereContentTransmissionDescriptor *pct=(SI::PremiereContentTransmissionDescriptor *)d;
563 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
564 SI::Loop::Iterator it;
565 if(pct->startDayLoop.getNext(sd,it)) {
566 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
567 SI::Loop::Iterator it2;
568 if(sd.startTimeLoop.getNext(st,it2)) {
569 time_t StartTime=st.getStartTime(sd.getMJD());
570 if(nCount==1) firstTime=StartTime;
571 else if(firstTime<StartTime-5*50 || firstTime>StartTime+5*60)
577 case SI::ExtendedEventDescriptorTag:
579 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
580 if(I18nIsPreferredLanguage(Setup.EPGLanguages,eed->languageCode, LanguagePreferenceExt) || !ExtendedEventDescriptors) {
581 delete ExtendedEventDescriptors;
582 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
583 UseExtendedEventDescriptor=true;
585 if(UseExtendedEventDescriptor) {
586 ExtendedEventDescriptors->Add(eed);
587 d=NULL; // so that it is not deleted
589 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
590 UseExtendedEventDescriptor=false;
593 case SI::ShortEventDescriptorTag:
595 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
596 if(I18nIsPreferredLanguage(Setup.EPGLanguages,sed->languageCode, LanguagePreferenceShort) || !ShortEventDescriptor) {
597 delete ShortEventDescriptor;
598 ShortEventDescriptor=sed;
599 d=NULL; // so that it is not deleted
614 crc[0]=cit.getContentId();
615 SI::PremiereContentTransmissionDescriptor *pct;
616 for(SI::Loop::Iterator it; (pct=(SI::PremiereContentTransmissionDescriptor *)cit.eventDescriptors.getNext(it,SI::PremiereContentTransmissionDescriptorTag)); ) {
617 int nid=pct->getOriginalNetworkId();
618 int tid=pct->getTransportStreamId();
619 int sid=pct->getServiceId();
622 if (tid==0x03 && sid==0xf0) { tid=0x02; sid=0xe0; }
623 else if(tid==0x03 && sid==0xf1) { tid=0x02; sid=0xe1; }
624 else if(tid==0x03 && sid==0xf5) { tid=0x03; sid=0xdc; }
625 else if(tid==0x04 && sid==0xd2) { tid=0x11; sid=0xe2; }
626 else if(tid==0x11 && sid==0xd3) { tid=0x11; sid=0xe3; }
627 else if(tid==0x01 && sid==0xd4) { tid=0x04; sid=0xe4; }
630 tChannelID channelID(Source(),nid,tid,sid);
631 cChannel *channel=Channels.GetByChannelID(channelID,true);
632 if(!channel) continue;
634 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
636 pSchedule=new cSchedule(channelID);
637 Schedules->Add(pSchedule);
641 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
643 for(SI::Loop::Iterator it; pct->startDayLoop.getNext(sd,it); ) {
645 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
646 for(SI::Loop::Iterator it2; sd.startTimeLoop.getNext(st,it2); ) {
647 time_t StartTime=st.getStartTime(mjd);
648 time_t EndTime=StartTime+cit.getDuration();
649 int runningStatus=(StartTime<now && now<EndTime) ? SI::RunningStatusRunning : ((StartTime-30<now && now<StartTime) ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusNotRunning);
651 if(index++==0 && nCount>1) isOpt=true;
652 crc[1]=isOpt ? optCount : 0;
653 crc[2]=StartTime / STARTTIME_BIAS;
654 tEventID EventId=((('P'<<8)|'W')<<16) | crc16(0,(unsigned char *)crc,sizeof(crc));
656 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))
657 if(EndTime+Setup.EPGLinger*60<now) {
658 d2(printf("(old)\n"))
663 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,-1);
665 d2(printf("(new)\n"))
666 pEvent=new cEvent(EventId);
667 if(!pEvent) continue;
671 d2(printf("(upd)\n"))
673 if(pEvent->TableID()==0x00 || pEvent->Version()==cit.getVersionNumber()) {
674 if(pEvent->RunningStatus()!=runningStatus)
675 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
679 pEvent->SetEventID(EventId);
680 pEvent->SetTableID(Tid);
681 pEvent->SetVersion(cit.getVersionNumber());
682 pEvent->SetStartTime(StartTime);
683 pEvent->SetDuration(cit.getDuration());
685 if(ShortEventDescriptor) {
687 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
689 char buffer2[sizeof(buffer)+32];
690 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
691 pEvent->SetTitle(buffer2);
694 pEvent->SetTitle(buffer);
695 d2(printf("title: %s\n",pEvent->Title()))
696 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
698 if(ExtendedEventDescriptors) {
699 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
700 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
702 if(order || rating) {
703 int len=(pEvent->Description() ? strlen(pEvent->Description()) : 0) +
704 (order ? strlen(order) : 0) +
705 (rating ? strlen(rating) : 0);
708 if(pEvent->Description()) strcat(buffer,pEvent->Description());
709 if(rating) strcat(buffer,rating);
710 if(order) strcat(buffer,order);
711 pEvent->SetDescription(buffer);
714 if(newEvent) pSchedule->AddEvent(pEvent);
715 pEvent->SetComponents(NULL);
716 pEvent->FixEpgBugs();
717 if(pEvent->RunningStatus()!=runningStatus)
718 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
719 pSchedule->DropOutdated(StartTime,EndTime,Tid,cit.getVersionNumber());
725 Schedules->SetModified(pSchedule);
730 delete ExtendedEventDescriptors;
731 delete ShortEventDescriptor;
739 // --- cPluginPremiereEpg ------------------------------------------------------
741 static const char *DESCRIPTION = trNOOP("Parses extended Premiere EPG data");
743 class cPluginPremiereEpg : public cPlugin {
746 cFilterPremiereEpg *filter;
748 } epg[MAXDVBDEVICES];
750 cPluginPremiereEpg(void);
751 virtual const char *Version(void) { return PluginVersion; }
752 virtual const char *Description(void) { return tr(DESCRIPTION); }
753 virtual bool Start(void);
754 virtual void Stop(void);
755 virtual cMenuSetupPage *SetupMenu(void);
756 virtual bool SetupParse(const char *Name, const char *Value);
759 cPluginPremiereEpg::cPluginPremiereEpg(void)
761 memset(epg,0,sizeof(epg));
764 bool cPluginPremiereEpg::Start(void)
766 #if APIVERSNUM < 10507
767 RegisterI18n(Phrases);
769 for(int i=0; i<MAXDVBDEVICES; i++) {
770 cDevice *dev=cDevice::GetDevice(i);
773 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
774 isyslog("Attached premiere EPG filter to device %d",i);
780 void cPluginPremiereEpg::Stop(void)
782 for(int i=0; i<MAXDVBDEVICES; i++) {
783 cDevice *dev=epg[i].device;
784 if(dev) dev->Detach(epg[i].filter);
785 delete epg[i].filter;
791 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
793 return new cMenuSetupPremiereEpg;
796 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
798 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
799 else if (!strcasecmp(Name, "OrderInfo")) SetupPE.OrderInfo = atoi(Value);
800 else if (!strcasecmp(Name, "RatingInfo")) SetupPE.RatingInfo = atoi(Value);
801 else if (!strcasecmp(Name, "FixEpg")) SetupPE.FixEpg = atoi(Value);
806 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!