2 * PremiereEpg plugin to VDR (C++)
4 * (C) 2005 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 <libsi/section.h>
33 #include <libsi/descriptor.h>
44 #define d2(x) { (x); }
49 #define PMT_SCAN_TIMEOUT 10 // seconds
50 #define PMT_SCAN_IDLE 300 // seconds
52 static const char *VERSION = "0.0.4";
53 static const char *DESCRIPTION = "Parses extended Premiere EPG data";
55 // --- cSetupPremiereEpg -------------------------------------------------------
57 const char *optPats[] = {
64 #define NUM_PATS (sizeof(optPats)/sizeof(char *))
66 class cSetupPremiereEpg {
72 cSetupPremiereEpg(void);
75 cSetupPremiereEpg SetupPE;
77 cSetupPremiereEpg::cSetupPremiereEpg(void)
84 // --- i18n --------------------------------------------------------------------
86 const tI18nPhrase Phrases[] = {
106 { "Parses extended Premiere EPG data",
107 "Liest erweiterte Premiere EPG Daten ein",
123 { "Tag option events",
124 "Options Events markieren",
140 { "Show order information",
141 "Bestellhinweise anzeigen",
157 { "Show rating information",
158 "Altersfreigaben anzeigen",
298 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
300 class cMenuSetupPremiereEpg : public cMenuSetupPage {
302 cSetupPremiereEpg data;
303 const char *optDisp[NUM_PATS];
304 char buff[NUM_PATS][32];
306 virtual void Store(void);
308 cMenuSetupPremiereEpg(void);
311 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
314 SetSection(tr("PremiereEPG"));
315 optDisp[0]=tr("off");
316 for(unsigned int i=1; i<NUM_PATS; i++) {
317 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
320 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
321 Add(new cMenuEditBoolItem(tr("Show order information"),&data.OrderInfo));
322 Add(new cMenuEditBoolItem(tr("Show rating information"),&data.RatingInfo));
325 void cMenuSetupPremiereEpg::Store(void)
328 SetupStore("OptionPattern",SetupPE.OptPat);
329 SetupStore("OrderInfo",SetupPE.OrderInfo);
330 SetupStore("RatingInfo",SetupPE.RatingInfo);
333 // --- CIT ---------------------------------------------------------------------
341 #if BYTE_ORDER == BIG_ENDIAN
342 u_char section_syntax_indicator :1;
344 u_char section_length_hi :4;
346 u_char section_length_hi :4;
348 u_char section_syntax_indicator :1;
350 u_char section_length_lo :8;
351 u_char service_id_hi :8;
352 u_char service_id_lo :8;
353 #if BYTE_ORDER == BIG_ENDIAN
355 u_char version_number :5;
356 u_char current_next_indicator :1;
358 u_char current_next_indicator :1;
359 u_char version_number :5;
362 u_char section_number :8;
363 u_char last_section_number :8;
364 u_char content_id_hi_hi :8;
365 u_char content_id_hi_lo :8;
366 u_char content_id_lo_hi :8;
367 u_char content_id_lo_lo :8;
368 u_char duration_h :8;
369 u_char duration_m :8;
370 u_char duration_s :8;
371 #if BYTE_ORDER == BIG_ENDIAN
373 u_char descriptors_loop_length_hi :4;
375 u_char descriptors_loop_length_hi :4;
378 u_char descriptors_loop_length_lo :8;
381 class CIT : public NumberedSection {
383 CIT(const unsigned char *data, bool doCopy=true) : NumberedSection(data, doCopy) {}
385 int getContentId(void) const;
386 time_t getDuration(void) const;
387 DescriptorLoop eventDescriptors;
389 virtual void Parse(void);
394 int CIT::getContentId(void) const {
395 return (HILO(s->content_id_hi)<<16) | (HILO(s->content_id_lo));
398 time_t CIT::getDuration(void) const {
399 return DVBTime::getDuration(s->duration_h,s->duration_m,s->duration_s);
402 void CIT::Parse(void) {
403 unsigned int offset=0;
404 data.setPointerAndOffset<const cit>(s, offset);
405 eventDescriptors.setData(data+offset,HILO(s->descriptors_loop_length));
408 } // end of namespace
410 // --- cDescrF2 ----------------------------------------------------------------
416 int idx, loop, nloop, index;
418 cDescrF2(SI::Descriptor *D);
419 int TransportStreamId(void) { return data.TwoBytes(2); }
420 int OrgNetworkId(void) { return data.TwoBytes(4); }
421 int ServiceId(void) { return data.TwoBytes(6); }
424 time_t StartTime(void);
425 int Index(void) { return index; }
428 cDescrF2::cDescrF2(SI::Descriptor *D)
435 void cDescrF2::Start(void)
437 idx=8; loop=0; nloop=-3; index=-1;
440 bool cDescrF2::Next(void)
445 if(idx>=d->getLength()) return false;
446 loop=0; nloop=data[idx+2];
452 time_t cDescrF2::StartTime(void)
455 return SI::DVBTime::getTime(data[idx+0],data[idx+1],data[off+0],data[off+1],data[off+2]);
458 // --- cFilterPremiereEpg ------------------------------------------------------
460 class cFilterPremiereEpg : public cFilter {
462 int pmtpid, pmtidx, pmtnext;
466 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
468 cFilterPremiereEpg(void);
469 virtual void SetStatus(bool On);
473 cFilterPremiereEpg::cFilterPremiereEpg(void)
479 void cFilterPremiereEpg::Trigger(void)
481 d(printf("trigger\n"))
482 pmtpid=0; pmtidx=0; pmtnext=0;
485 void cFilterPremiereEpg::SetStatus(bool On)
487 d(printf("setstatus %d\n",On))
488 cFilter::SetStatus(On);
492 void cFilterPremiereEpg::NextPmt(void)
497 d(printf("PMT next\n"))
500 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
502 if(Pid==0 && Tid==SI::TableIdPAT) {
504 if(!pmtnext || now>pmtnext) {
505 if(pmtpid) NextPmt();
507 SI::PAT pat(Data,false);
508 if(pat.CheckCRCAndParse()) {
509 SI::PAT::Association assoc;
511 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
512 if(!assoc.isNITPid()) {
514 pmtpid=assoc.getPid();
516 pmtnext=now+PMT_SCAN_TIMEOUT;
517 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
524 pmtnext=now+PMT_SCAN_IDLE;
525 d(printf("PMT scan idle\n"))
531 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
532 SI::PMT pmt(Data,false);
533 if(pmt.CheckCRCAndParse()) {
534 SI::PMT::Stream stream;
535 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
536 if(stream.getStreamType()==0x05) {
537 SI::CharArray data=stream.getData();
538 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
539 bool prvData=false, usrData=false;
541 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
542 switch(d->getDescriptorTag()) {
543 case SI::PrivateDataSpecifierDescriptorTag:
544 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
545 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
549 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
550 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
558 if(prvData && usrData) {
559 int pid=stream.getPid();
560 d(printf("found citpid 0x%04x",pid))
561 if(!Matches(pid,0xA0)) {
563 d(printf(" (added)"))
570 NextPmt(); pmtnext=0;
573 else if(Tid==0xA0 && Source()) {
574 SI::CIT cit(Data,false);
575 if(cit.CheckCRCAndParse()) {
576 cSchedulesLock SchedulesLock(true,10);
577 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
582 int LanguagePreferenceShort=-1;
583 int LanguagePreferenceExt=-1;
584 bool UseExtendedEventDescriptor=false;
585 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
586 SI::ShortEventDescriptor *ShortEventDescriptor=0;
587 char *order=0, *rating=0;
588 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
589 switch(d->getDescriptorTag()) {
590 case 0xF0: // order information
591 if(SetupPE.OrderInfo) {
592 static const char *text[] = {
601 const unsigned char *data=d->getData().getData()+2;
602 for(int i=0; i<5; i++) {
604 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %.*s",tr(text[i]),l,&data[1]);
607 if(p>0) order=strdup(buff);
610 case 0xF1: // parental rating
611 if(SetupPE.RatingInfo) {
614 const unsigned char *data=d->getData().getData()+2;
615 p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %d %s",tr("Rating"),data[0]+3,tr("years"));
618 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p," (%.*s)",l,&data[1]);
619 if(p>0) rating=strdup(buff);
622 case 0xF2: // transmisions
627 if(nCount==1) firstTime=f2.StartTime();
629 time_t time=f2.StartTime();
630 if(firstTime<time-5*50 || firstTime>time+5*60)
636 case SI::ExtendedEventDescriptorTag:
638 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
639 #if VDRVERSNUM < 10332
640 if(I18nIsPreferredLanguage(Setup.EPGLanguages,I18nLanguageIndex(eed->languageCode), LanguagePreferenceExt) || !ExtendedEventDescriptors) {
642 if(I18nIsPreferredLanguage(Setup.EPGLanguages,eed->languageCode, LanguagePreferenceExt) || !ExtendedEventDescriptors) {
644 delete ExtendedEventDescriptors;
645 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
646 UseExtendedEventDescriptor=true;
648 if(UseExtendedEventDescriptor) {
649 ExtendedEventDescriptors->Add(eed);
650 d=NULL; // so that it is not deleted
652 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
653 UseExtendedEventDescriptor=false;
656 case SI::ShortEventDescriptorTag:
658 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
659 #if VDRVERSNUM < 10332
660 if(I18nIsPreferredLanguage(Setup.EPGLanguages,I18nLanguageIndex(sed->languageCode), LanguagePreferenceShort) || !ShortEventDescriptor) {
662 if(I18nIsPreferredLanguage(Setup.EPGLanguages,sed->languageCode, LanguagePreferenceShort) || !ShortEventDescriptor) {
664 delete ShortEventDescriptor;
665 ShortEventDescriptor=sed;
666 d=NULL; // so that it is not deleted
678 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
679 if(d->getDescriptorTag()==0xF2) {
683 tChannelID channelID(Source(),f2.OrgNetworkId(),f2.TransportStreamId(),f2.ServiceId());
684 cChannel *channel=Channels.GetByChannelID(channelID,true);
685 if(!channel) continue;
687 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
689 pSchedule=new cSchedule(channelID);
690 Schedules->Add(pSchedule);
693 for(f2.Start(); f2.Next();) {
694 u_int16_t EventId=(cit.getContentId()<<4) | f2.Index();
695 time_t StartTime=f2.StartTime();
698 if(f2.Index()==0 && nCount>1) isOpt=true;
700 d2(printf("ch=%s id=%04x/%08x %d opt=%d/%d st=%s ",*channelID.ToString(),EventId,cit.getContentId(),f2.Index(),isOpt,optCount,stripspace(ctime(&StartTime))))
701 if(StartTime+cit.getDuration()+Setup.EPGLinger*60<time(0)) {
702 d2(printf("(old)\n"))
707 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,StartTime);
709 d2(printf("(new)\n"))
710 #if VDRVERSNUM >= 10325
711 pEvent=new cEvent(EventId);
713 pEvent=new cEvent(channelID,EventId);
715 if(!pEvent) continue;
719 d2(printf("(upd)\n"))
721 if(pEvent->TableID()==0x00) continue;
722 if(Tid==pEvent->TableID() && pEvent->Version()==cit.getVersionNumber()) continue;
724 pEvent->SetEventID(EventId);
725 pEvent->SetTableID(Tid);
726 pEvent->SetVersion(cit.getVersionNumber());
727 pEvent->SetStartTime(StartTime);
728 pEvent->SetDuration(cit.getDuration());
730 if(ShortEventDescriptor) {
732 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
734 char buffer2[sizeof(buffer)+32];
735 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
736 pEvent->SetTitle(buffer2);
739 pEvent->SetTitle(buffer);
740 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
742 if(ExtendedEventDescriptors) {
743 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
744 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
746 if(order || rating) {
747 int len=(pEvent->Description() ? strlen(pEvent->Description()) : 0) +
748 (order ? strlen(order) : 0) +
749 (rating ? strlen(rating) : 0);
752 if(pEvent->Description()) strcat(buffer,pEvent->Description());
753 if(rating) strcat(buffer,rating);
754 if(order) strcat(buffer,order);
755 pEvent->SetDescription(buffer);
758 if(newEvent) pSchedule->AddEvent(pEvent);
759 pEvent->SetComponents(NULL);
760 pEvent->FixEpgBugs();
765 Schedules->SetModified(pSchedule);
770 delete ExtendedEventDescriptors;
771 delete ShortEventDescriptor;
779 // --- cPluginPremiereEpg ------------------------------------------------------
781 class cPluginPremiereEpg : public cPlugin {
784 cFilterPremiereEpg *filter;
786 } epg[MAXDVBDEVICES];
788 cPluginPremiereEpg(void);
789 virtual const char *Version(void) { return VERSION; }
790 virtual const char *Description(void) { return tr(DESCRIPTION); }
791 virtual bool Start(void);
792 virtual void Stop(void);
793 virtual cMenuSetupPage *SetupMenu(void);
794 virtual bool SetupParse(const char *Name, const char *Value);
797 cPluginPremiereEpg::cPluginPremiereEpg(void)
799 memset(epg,0,sizeof(epg));
802 bool cPluginPremiereEpg::Start(void)
804 RegisterI18n(Phrases);
805 for(int i=0; i<MAXDVBDEVICES; i++) {
806 cDevice *dev=cDevice::GetDevice(i);
809 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
810 isyslog("Attached premiere EPG filter to device %d",i);
816 void cPluginPremiereEpg::Stop(void)
818 for(int i=0; i<MAXDVBDEVICES; i++) {
819 cDevice *dev=epg[i].device;
820 if(dev) dev->Detach(epg[i].filter);
821 delete epg[i].filter;
827 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
829 return new cMenuSetupPremiereEpg;
832 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
834 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
835 else if (!strcasecmp(Name, "OrderInfo")) SetupPE.OrderInfo = atoi(Value);
836 else if (!strcasecmp(Name, "RatingInfo")) SetupPE.RatingInfo = atoi(Value);
841 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!