2 * PremiereEpg plugin to VDR (C++)
4 * (C) 2005-2008 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
27 #include <vdr/plugin.h>
28 #include <vdr/filter.h>
30 #include <vdr/channels.h>
31 #include <vdr/dvbdevice.h>
33 #include <vdr/config.h>
34 #include <libsi/section.h>
35 #include <libsi/descriptor.h>
39 #if APIVERSNUM < 10401
40 #error You need at least VDR API version 1.4.1 for this plugin
52 #define d2(x) { (x); }
57 #define PMT_SCAN_TIMEOUT 10 // seconds
58 #define PMT_SCAN_IDLE 300 // seconds
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 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
93 class cMenuSetupPremiereEpg : public cMenuSetupPage {
95 cSetupPremiereEpg data;
96 const char *optDisp[NUM_PATS];
97 char buff[NUM_PATS][32];
99 virtual void Store(void);
101 cMenuSetupPremiereEpg(void);
104 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
107 SetSection(tr("PremiereEPG"));
108 optDisp[0]=tr("off");
109 for(unsigned int i=1; i<NUM_PATS; i++) {
110 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
113 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
114 Add(new cMenuEditBoolItem(tr("Show order information"),&data.OrderInfo));
115 Add(new cMenuEditBoolItem(tr("Show rating information"),&data.RatingInfo));
116 Add(new cMenuEditBoolItem(tr("Fix EPG data"),&data.FixEpg));
119 void cMenuSetupPremiereEpg::Store(void)
122 SetupStore("OptionPattern",SetupPE.OptPat);
123 SetupStore("OrderInfo",SetupPE.OrderInfo);
124 SetupStore("RatingInfo",SetupPE.RatingInfo);
125 SetupStore("FixEpg",SetupPE.FixEpg);
128 // --- CRC16 -------------------------------------------------------------------
130 #define POLY 0xA001 // CRC16
132 unsigned int crc16(unsigned int crc, unsigned char const *p, int len)
136 for(int i=0; i<8; i++)
137 crc=(crc&1) ? (crc>>1)^POLY : (crc>>1);
142 // --- cStrBuff ----------------------------------------------------------------
151 void Printf(const char *fmt, ...) __attribute__ ((format (printf,2,3)));
152 char *Buff(void) { return (buff && pos>0) ? strdup(buff):0; }
155 cStrBuff::cStrBuff(int Size)
158 buff=MALLOC(char,size);
161 cStrBuff::~cStrBuff()
166 void cStrBuff::Printf(const char *fmt, ...)
172 int q=vsnprintf(buff+pos,s,fmt,ap);
178 // --- cFilterPremiereEpg ------------------------------------------------------
180 #define STARTTIME_BIAS (20*60)
182 class cFilterPremiereEpg : public cFilter {
184 int pmtpid, pmtsid, pmtidx, pmtnext;
188 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
190 cFilterPremiereEpg(void);
191 virtual void SetStatus(bool On);
195 cFilterPremiereEpg::cFilterPremiereEpg(void)
201 void cFilterPremiereEpg::Trigger(void)
203 d(printf("trigger\n"))
204 pmtpid=0; pmtidx=0; pmtnext=0;
207 void cFilterPremiereEpg::SetStatus(bool On)
209 d(printf("setstatus %d\n",On))
210 cFilter::SetStatus(On);
214 void cFilterPremiereEpg::NextPmt(void)
219 d(printf("PMT next\n"))
222 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
225 if(Pid==0 && Tid==SI::TableIdPAT) {
226 if(!pmtnext || now>pmtnext) {
227 if(pmtpid) NextPmt();
229 SI::PAT pat(Data,false);
230 if(pat.CheckCRCAndParse()) {
231 SI::PAT::Association assoc;
233 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
234 if(!assoc.isNITPid()) {
236 pmtpid=assoc.getPid();
237 pmtsid=assoc.getServiceId();
239 pmtnext=now+PMT_SCAN_TIMEOUT;
240 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
247 pmtnext=now+PMT_SCAN_IDLE;
248 d(printf("PMT scan idle\n"))
254 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
255 SI::PMT pmt(Data,false);
256 if(pmt.CheckCRCAndParse() && pmt.getServiceId()==pmtsid) {
257 SI::PMT::Stream stream;
258 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
259 if(stream.getStreamType()==0x05) {
260 SI::CharArray data=stream.getData();
261 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
262 bool prvData=false, usrData=false;
264 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
265 switch(d->getDescriptorTag()) {
266 case SI::PrivateDataSpecifierDescriptorTag:
267 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
268 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
272 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
273 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
281 if(prvData && usrData) {
282 int pid=stream.getPid();
283 d(printf("found citpid 0x%04x",pid))
284 if(!Matches(pid,0xA0)) {
286 d(printf(" (added)"))
293 NextPmt(); pmtnext=0;
296 else if(Tid==0xA0 && Source()) {
297 SI::PremiereCIT cit(Data,false);
298 if(cit.CheckCRCAndParse()) {
299 cSchedulesLock SchedulesLock(true,10);
300 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
303 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
304 SI::ShortEventDescriptor *ShortEventDescriptor=0;
305 char *order=0, *rating=0;
309 bool UseExtendedEventDescriptor=false;
310 int LanguagePreferenceShort=-1;
311 int LanguagePreferenceExt=-1;
312 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
313 switch(d->getDescriptorTag()) {
314 case 0xF0: // order information
315 if(SetupPE.OrderInfo) {
316 static const char *text[] = {
317 trNOOP("Ordernumber"),
324 const unsigned char *data=d->getData().getData()+2;
325 for(int i=0; i<5; i++) {
327 if(l>0) str.Printf("\n%s: %.*s",tr(text[i]),l,&data[1]);
333 case 0xF1: // parental rating
334 if(SetupPE.RatingInfo) {
336 const unsigned char *data=d->getData().getData()+2;
337 str.Printf("\n%s: %d %s",tr("Rating"),data[0]+3,tr("years"));
340 if(l>0) str.Printf(" (%.*s)",l,&data[1]);
344 case SI::PremiereContentTransmissionDescriptorTag:
346 SI::PremiereContentTransmissionDescriptor *pct=(SI::PremiereContentTransmissionDescriptor *)d;
348 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
349 SI::Loop::Iterator it;
350 if(pct->startDayLoop.getNext(sd,it)) {
351 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
352 SI::Loop::Iterator it2;
353 if(sd.startTimeLoop.getNext(st,it2)) {
354 time_t StartTime=st.getStartTime(sd.getMJD());
355 if(nCount==1) firstTime=StartTime;
356 else if(firstTime<StartTime-5*50 || firstTime>StartTime+5*60)
362 case SI::ExtendedEventDescriptorTag:
364 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
365 if(I18nIsPreferredLanguage(Setup.EPGLanguages,eed->languageCode, LanguagePreferenceExt) || !ExtendedEventDescriptors) {
366 delete ExtendedEventDescriptors;
367 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
368 UseExtendedEventDescriptor=true;
370 if(UseExtendedEventDescriptor) {
371 ExtendedEventDescriptors->Add(eed);
372 d=NULL; // so that it is not deleted
374 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
375 UseExtendedEventDescriptor=false;
378 case SI::ShortEventDescriptorTag:
380 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
381 if(I18nIsPreferredLanguage(Setup.EPGLanguages,sed->languageCode, LanguagePreferenceShort) || !ShortEventDescriptor) {
382 delete ShortEventDescriptor;
383 ShortEventDescriptor=sed;
384 d=NULL; // so that it is not deleted
399 crc[0]=cit.getContentId();
400 SI::PremiereContentTransmissionDescriptor *pct;
401 for(SI::Loop::Iterator it; (pct=(SI::PremiereContentTransmissionDescriptor *)cit.eventDescriptors.getNext(it,SI::PremiereContentTransmissionDescriptorTag)); ) {
402 int nid=pct->getOriginalNetworkId();
403 int tid=pct->getTransportStreamId();
404 int sid=pct->getServiceId();
407 if (tid==0x03 && sid==0xf0) { tid=0x02; sid=0xe0; }
408 else if(tid==0x03 && sid==0xf1) { tid=0x02; sid=0xe1; }
409 else if(tid==0x03 && sid==0xf5) { tid=0x03; sid=0xdc; }
410 else if(tid==0x04 && sid==0xd2) { tid=0x11; sid=0xe2; }
411 else if(tid==0x11 && sid==0xd3) { tid=0x11; sid=0xe3; }
412 else if(tid==0x01 && sid==0xd4) { tid=0x04; sid=0xe4; }
415 tChannelID channelID(Source(),nid,tid,sid);
416 cChannel *channel=Channels.GetByChannelID(channelID,true);
417 if(!channel) continue;
419 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
421 pSchedule=new cSchedule(channelID);
422 Schedules->Add(pSchedule);
426 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
428 for(SI::Loop::Iterator it; pct->startDayLoop.getNext(sd,it); ) {
430 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
431 for(SI::Loop::Iterator it2; sd.startTimeLoop.getNext(st,it2); ) {
432 time_t StartTime=st.getStartTime(mjd);
433 time_t EndTime=StartTime+cit.getDuration();
434 int runningStatus=(StartTime<now && now<EndTime) ? SI::RunningStatusRunning : ((StartTime-30<now && now<StartTime) ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusNotRunning);
436 if(index++==0 && nCount>1) isOpt=true;
437 crc[1]=isOpt ? optCount : 0;
438 crc[2]=StartTime / STARTTIME_BIAS;
439 tEventID EventId=((('P'<<8)|'W')<<16) | crc16(0,(unsigned char *)crc,sizeof(crc));
441 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))
442 if(EndTime+Setup.EPGLinger*60<now) {
443 d2(printf("(old)\n"))
448 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,-1);
450 d2(printf("(new)\n"))
451 pEvent=new cEvent(EventId);
452 if(!pEvent) continue;
456 d2(printf("(upd)\n"))
458 if(pEvent->TableID()==0x00 || pEvent->Version()==cit.getVersionNumber()) {
459 if(pEvent->RunningStatus()!=runningStatus)
460 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
464 pEvent->SetEventID(EventId);
465 pEvent->SetTableID(Tid);
466 pEvent->SetVersion(cit.getVersionNumber());
467 pEvent->SetStartTime(StartTime);
468 pEvent->SetDuration(cit.getDuration());
470 if(ShortEventDescriptor) {
472 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
474 char buffer2[sizeof(buffer)+32];
475 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
476 pEvent->SetTitle(buffer2);
479 pEvent->SetTitle(buffer);
480 d2(printf("title: %s\n",pEvent->Title()))
481 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
483 if(ExtendedEventDescriptors) {
484 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
485 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
487 if(order || rating) {
488 int len=(pEvent->Description() ? strlen(pEvent->Description()) : 0) +
489 (order ? strlen(order) : 0) +
490 (rating ? strlen(rating) : 0);
493 if(pEvent->Description()) strcat(buffer,pEvent->Description());
494 if(rating) strcat(buffer,rating);
495 if(order) strcat(buffer,order);
496 pEvent->SetDescription(buffer);
499 if(newEvent) pSchedule->AddEvent(pEvent);
500 pEvent->SetComponents(NULL);
501 pEvent->FixEpgBugs();
502 if(pEvent->RunningStatus()!=runningStatus)
503 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
504 pSchedule->DropOutdated(StartTime,EndTime,Tid,cit.getVersionNumber());
510 Schedules->SetModified(pSchedule);
515 delete ExtendedEventDescriptors;
516 delete ShortEventDescriptor;
524 // --- cPluginPremiereEpg ------------------------------------------------------
526 static const char *DESCRIPTION = trNOOP("Parses extended Premiere EPG data");
528 class cPluginPremiereEpg : public cPlugin {
531 cFilterPremiereEpg *filter;
533 } epg[MAXDVBDEVICES];
535 cPluginPremiereEpg(void);
536 virtual const char *Version(void) { return PluginVersion; }
537 virtual const char *Description(void) { return tr(DESCRIPTION); }
538 virtual bool Start(void);
539 virtual void Stop(void);
540 virtual cMenuSetupPage *SetupMenu(void);
541 virtual bool SetupParse(const char *Name, const char *Value);
544 cPluginPremiereEpg::cPluginPremiereEpg(void)
546 memset(epg,0,sizeof(epg));
549 bool cPluginPremiereEpg::Start(void)
551 #if APIVERSNUM < 10507
552 RegisterI18n(Phrases);
554 for(int i=0; i<MAXDVBDEVICES; i++) {
555 cDevice *dev=cDevice::GetDevice(i);
558 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
559 isyslog("Attached premiere EPG filter to device %d",i);
565 void cPluginPremiereEpg::Stop(void)
567 for(int i=0; i<MAXDVBDEVICES; i++) {
568 cDevice *dev=epg[i].device;
569 if(dev) dev->Detach(epg[i].filter);
570 delete epg[i].filter;
576 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
578 return new cMenuSetupPremiereEpg;
581 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
583 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
584 else if (!strcasecmp(Name, "OrderInfo")) SetupPE.OrderInfo = atoi(Value);
585 else if (!strcasecmp(Name, "RatingInfo")) SetupPE.RatingInfo = atoi(Value);
586 else if (!strcasecmp(Name, "FixEpg")) SetupPE.FixEpg = atoi(Value);
591 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!