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.6";
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 {
77 cSetupPremiereEpg(void);
80 cSetupPremiereEpg SetupPE;
82 cSetupPremiereEpg::cSetupPremiereEpg(void)
89 // --- i18n --------------------------------------------------------------------
91 const tI18nPhrase Phrases[] = {
111 { "Parses extended Premiere EPG data",
112 "Liest erweiterte Premiere EPG Daten ein",
128 { "Tag option events",
129 "Options Events markieren",
145 { "Show order information",
146 "Bestellhinweise anzeigen",
162 { "Show rating information",
163 "Altersfreigaben anzeigen",
303 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
305 class cMenuSetupPremiereEpg : public cMenuSetupPage {
307 cSetupPremiereEpg data;
308 const char *optDisp[NUM_PATS];
309 char buff[NUM_PATS][32];
311 virtual void Store(void);
313 cMenuSetupPremiereEpg(void);
316 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
319 SetSection(tr("PremiereEPG"));
320 optDisp[0]=tr("off");
321 for(unsigned int i=1; i<NUM_PATS; i++) {
322 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
325 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
326 Add(new cMenuEditBoolItem(tr("Show order information"),&data.OrderInfo));
327 Add(new cMenuEditBoolItem(tr("Show rating information"),&data.RatingInfo));
330 void cMenuSetupPremiereEpg::Store(void)
333 SetupStore("OptionPattern",SetupPE.OptPat);
334 SetupStore("OrderInfo",SetupPE.OrderInfo);
335 SetupStore("RatingInfo",SetupPE.RatingInfo);
338 // --- CRC16 -------------------------------------------------------------------
340 #define POLY 0xA001 // CRC16
342 unsigned int crc16(unsigned int crc, unsigned char const *p, int len)
346 for(int i=0; i<8; i++)
347 crc=(crc&1) ? (crc>>1)^POLY : (crc>>1);
352 // --- cFilterPremiereEpg ------------------------------------------------------
354 #define STARTTIME_BIAS (20*60)
356 class cFilterPremiereEpg : public cFilter {
358 int pmtpid, pmtsid, pmtidx, pmtnext;
362 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
364 cFilterPremiereEpg(void);
365 virtual void SetStatus(bool On);
369 cFilterPremiereEpg::cFilterPremiereEpg(void)
375 void cFilterPremiereEpg::Trigger(void)
377 d(printf("trigger\n"))
378 pmtpid=0; pmtidx=0; pmtnext=0;
381 void cFilterPremiereEpg::SetStatus(bool On)
383 d(printf("setstatus %d\n",On))
384 cFilter::SetStatus(On);
388 void cFilterPremiereEpg::NextPmt(void)
393 d(printf("PMT next\n"))
396 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
399 if(Pid==0 && Tid==SI::TableIdPAT) {
400 if(!pmtnext || now>pmtnext) {
401 if(pmtpid) NextPmt();
403 SI::PAT pat(Data,false);
404 if(pat.CheckCRCAndParse()) {
405 SI::PAT::Association assoc;
407 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
408 if(!assoc.isNITPid()) {
410 pmtpid=assoc.getPid();
411 pmtsid=assoc.getServiceId();
413 pmtnext=now+PMT_SCAN_TIMEOUT;
414 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
421 pmtnext=now+PMT_SCAN_IDLE;
422 d(printf("PMT scan idle\n"))
428 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
429 SI::PMT pmt(Data,false);
430 if(pmt.CheckCRCAndParse() && pmt.getServiceId()==pmtsid) {
431 SI::PMT::Stream stream;
432 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
433 if(stream.getStreamType()==0x05) {
434 SI::CharArray data=stream.getData();
435 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
436 bool prvData=false, usrData=false;
438 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
439 switch(d->getDescriptorTag()) {
440 case SI::PrivateDataSpecifierDescriptorTag:
441 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
442 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
446 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
447 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
455 if(prvData && usrData) {
456 int pid=stream.getPid();
457 d(printf("found citpid 0x%04x",pid))
458 if(!Matches(pid,0xA0)) {
460 d(printf(" (added)"))
467 NextPmt(); pmtnext=0;
470 else if(Tid==0xA0 && Source()) {
471 SI::PremiereCIT cit(Data,false);
472 if(cit.CheckCRCAndParse()) {
473 cSchedulesLock SchedulesLock(true,10);
474 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
477 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
478 SI::ShortEventDescriptor *ShortEventDescriptor=0;
479 char *order=0, *rating=0;
483 bool UseExtendedEventDescriptor=false;
484 int LanguagePreferenceShort=-1;
485 int LanguagePreferenceExt=-1;
486 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
487 switch(d->getDescriptorTag()) {
488 case 0xF0: // order information
489 if(SetupPE.OrderInfo) {
490 static const char *text[] = {
499 const unsigned char *data=d->getData().getData()+2;
500 for(int i=0; i<5; i++) {
502 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %.*s",tr(text[i]),l,&data[1]);
505 if(p>0) order=strdup(buff);
508 case 0xF1: // parental rating
509 if(SetupPE.RatingInfo) {
512 const unsigned char *data=d->getData().getData()+2;
513 p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %d %s",tr("Rating"),data[0]+3,tr("years"));
516 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p," (%.*s)",l,&data[1]);
517 if(p>0) rating=strdup(buff);
520 case SI::PremiereContentTransmissionDescriptorTag:
522 SI::PremiereContentTransmissionDescriptor *pct=(SI::PremiereContentTransmissionDescriptor *)d;
524 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
525 SI::Loop::Iterator it;
526 if(pct->startDayLoop.getNext(sd,it)) {
527 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
528 SI::Loop::Iterator it2;
529 if(sd.startTimeLoop.getNext(st,it2)) {
530 time_t StartTime=st.getStartTime(sd.getMJD());
531 if(nCount==1) firstTime=StartTime;
532 else if(firstTime<StartTime-5*50 || firstTime>StartTime+5*60)
538 case SI::ExtendedEventDescriptorTag:
540 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
541 if(I18nIsPreferredLanguage(Setup.EPGLanguages,eed->languageCode, LanguagePreferenceExt) || !ExtendedEventDescriptors) {
542 delete ExtendedEventDescriptors;
543 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
544 UseExtendedEventDescriptor=true;
546 if(UseExtendedEventDescriptor) {
547 ExtendedEventDescriptors->Add(eed);
548 d=NULL; // so that it is not deleted
550 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
551 UseExtendedEventDescriptor=false;
554 case SI::ShortEventDescriptorTag:
556 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
557 if(I18nIsPreferredLanguage(Setup.EPGLanguages,sed->languageCode, LanguagePreferenceShort) || !ShortEventDescriptor) {
558 delete ShortEventDescriptor;
559 ShortEventDescriptor=sed;
560 d=NULL; // so that it is not deleted
575 crc[0]=cit.getContentId();
576 SI::PremiereContentTransmissionDescriptor *pct;
577 for(SI::Loop::Iterator it; (pct=(SI::PremiereContentTransmissionDescriptor *)cit.eventDescriptors.getNext(it,SI::PremiereContentTransmissionDescriptorTag)); ) {
578 tChannelID channelID(Source(),pct->getOriginalNetworkId(),pct->getTransportStreamId(),pct->getServiceId());
579 cChannel *channel=Channels.GetByChannelID(channelID,true);
580 if(!channel) continue;
582 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
584 pSchedule=new cSchedule(channelID);
585 Schedules->Add(pSchedule);
589 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
591 for(SI::Loop::Iterator it; pct->startDayLoop.getNext(sd,it); ) {
593 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
594 for(SI::Loop::Iterator it2; sd.startTimeLoop.getNext(st,it2); ) {
595 time_t StartTime=st.getStartTime(mjd);
596 time_t EndTime=StartTime+cit.getDuration();
597 int runningStatus=(StartTime<now && now<EndTime) ? SI::RunningStatusRunning : ((StartTime-30<now && now<StartTime) ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusNotRunning);
599 if(index++==0 && nCount>1) isOpt=true;
600 crc[1]=isOpt ? optCount : 0;
601 crc[2]=StartTime / STARTTIME_BIAS;
602 tEventID EventId=((('P'<<8)|'W')<<16) | crc16(0,(unsigned char *)crc,sizeof(crc));
604 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))
605 if(EndTime+Setup.EPGLinger*60<now) {
606 d2(printf("(old)\n"))
611 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,-1);
613 d2(printf("(new)\n"))
614 pEvent=new cEvent(EventId);
615 if(!pEvent) continue;
619 d2(printf("(upd)\n"))
621 if(pEvent->TableID()==0x00 || pEvent->Version()==cit.getVersionNumber()) {
622 if(pEvent->RunningStatus()!=runningStatus)
623 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
627 pEvent->SetEventID(EventId);
628 pEvent->SetTableID(Tid);
629 pEvent->SetVersion(cit.getVersionNumber());
630 pEvent->SetStartTime(StartTime);
631 pEvent->SetDuration(cit.getDuration());
633 if(ShortEventDescriptor) {
635 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
637 char buffer2[sizeof(buffer)+32];
638 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
639 pEvent->SetTitle(buffer2);
642 pEvent->SetTitle(buffer);
643 d2(printf("title: %s\n",pEvent->Title()))
644 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
646 if(ExtendedEventDescriptors) {
647 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
648 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
650 if(order || rating) {
651 int len=(pEvent->Description() ? strlen(pEvent->Description()) : 0) +
652 (order ? strlen(order) : 0) +
653 (rating ? strlen(rating) : 0);
656 if(pEvent->Description()) strcat(buffer,pEvent->Description());
657 if(rating) strcat(buffer,rating);
658 if(order) strcat(buffer,order);
659 pEvent->SetDescription(buffer);
662 if(newEvent) pSchedule->AddEvent(pEvent);
663 pEvent->SetComponents(NULL);
664 pEvent->FixEpgBugs();
665 if(pEvent->RunningStatus()!=runningStatus)
666 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
667 pSchedule->DropOutdated(StartTime,EndTime,Tid,cit.getVersionNumber());
673 Schedules->SetModified(pSchedule);
678 delete ExtendedEventDescriptors;
679 delete ShortEventDescriptor;
687 // --- cPluginPremiereEpg ------------------------------------------------------
689 class cPluginPremiereEpg : public cPlugin {
692 cFilterPremiereEpg *filter;
694 } epg[MAXDVBDEVICES];
696 cPluginPremiereEpg(void);
697 virtual const char *Version(void) { return VERSION; }
698 virtual const char *Description(void) { return tr(DESCRIPTION); }
699 virtual bool Start(void);
700 virtual void Stop(void);
701 virtual cMenuSetupPage *SetupMenu(void);
702 virtual bool SetupParse(const char *Name, const char *Value);
705 cPluginPremiereEpg::cPluginPremiereEpg(void)
707 memset(epg,0,sizeof(epg));
710 bool cPluginPremiereEpg::Start(void)
712 RegisterI18n(Phrases);
713 for(int i=0; i<MAXDVBDEVICES; i++) {
714 cDevice *dev=cDevice::GetDevice(i);
717 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
718 isyslog("Attached premiere EPG filter to device %d",i);
724 void cPluginPremiereEpg::Stop(void)
726 for(int i=0; i<MAXDVBDEVICES; i++) {
727 cDevice *dev=epg[i].device;
728 if(dev) dev->Detach(epg[i].filter);
729 delete epg[i].filter;
735 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
737 return new cMenuSetupPremiereEpg;
740 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
742 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
743 else if (!strcasecmp(Name, "OrderInfo")) SetupPE.OrderInfo = atoi(Value);
744 else if (!strcasecmp(Name, "RatingInfo")) SetupPE.RatingInfo = atoi(Value);
749 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!