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
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>
38 #if APIVERSNUM < 10401
39 #error You need at least VDR API version 1.4.1 for this plugin
51 #define d2(x) { (x); }
56 #define PMT_SCAN_TIMEOUT 10 // seconds
57 #define PMT_SCAN_IDLE 300 // seconds
59 // --- cSetupPremiereEpg -------------------------------------------------------
61 const char *optPats[] = {
68 #define NUM_PATS (sizeof(optPats)/sizeof(char *))
70 class cSetupPremiereEpg {
77 cSetupPremiereEpg(void);
80 cSetupPremiereEpg SetupPE;
82 cSetupPremiereEpg::cSetupPremiereEpg(void)
90 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
92 class cMenuSetupPremiereEpg : public cMenuSetupPage {
94 cSetupPremiereEpg data;
95 const char *optDisp[NUM_PATS];
96 char buff[NUM_PATS][32];
98 virtual void Store(void);
100 cMenuSetupPremiereEpg(void);
103 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
106 SetSection(tr("PremiereEPG"));
107 optDisp[0]=tr("off");
108 for(unsigned int i=1; i<NUM_PATS; i++) {
109 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
112 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
113 Add(new cMenuEditBoolItem(tr("Show order information"),&data.OrderInfo));
114 Add(new cMenuEditBoolItem(tr("Show rating information"),&data.RatingInfo));
115 Add(new cMenuEditBoolItem(tr("Fix EPG data"),&data.FixEpg));
118 void cMenuSetupPremiereEpg::Store(void)
121 SetupStore("OptionPattern",SetupPE.OptPat);
122 SetupStore("OrderInfo",SetupPE.OrderInfo);
123 SetupStore("RatingInfo",SetupPE.RatingInfo);
124 SetupStore("FixEpg",SetupPE.FixEpg);
127 // --- CRC16 -------------------------------------------------------------------
129 #define POLY 0xA001 // CRC16
131 unsigned int crc16(unsigned int crc, unsigned char const *p, int len)
135 for(int i=0; i<8; i++)
136 crc=(crc&1) ? (crc>>1)^POLY : (crc>>1);
141 // --- cFilterPremiereEpg ------------------------------------------------------
143 #define STARTTIME_BIAS (20*60)
145 class cFilterPremiereEpg : public cFilter {
147 int pmtpid, pmtsid, pmtidx, pmtnext;
151 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
153 cFilterPremiereEpg(void);
154 virtual void SetStatus(bool On);
158 cFilterPremiereEpg::cFilterPremiereEpg(void)
164 void cFilterPremiereEpg::Trigger(void)
166 d(printf("trigger\n"))
167 pmtpid=0; pmtidx=0; pmtnext=0;
170 void cFilterPremiereEpg::SetStatus(bool On)
172 d(printf("setstatus %d\n",On))
173 cFilter::SetStatus(On);
177 void cFilterPremiereEpg::NextPmt(void)
182 d(printf("PMT next\n"))
185 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
188 if(Pid==0 && Tid==SI::TableIdPAT) {
189 if(!pmtnext || now>pmtnext) {
190 if(pmtpid) NextPmt();
192 SI::PAT pat(Data,false);
193 if(pat.CheckCRCAndParse()) {
194 SI::PAT::Association assoc;
196 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
197 if(!assoc.isNITPid()) {
199 pmtpid=assoc.getPid();
200 pmtsid=assoc.getServiceId();
202 pmtnext=now+PMT_SCAN_TIMEOUT;
203 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
210 pmtnext=now+PMT_SCAN_IDLE;
211 d(printf("PMT scan idle\n"))
217 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
218 SI::PMT pmt(Data,false);
219 if(pmt.CheckCRCAndParse() && pmt.getServiceId()==pmtsid) {
220 SI::PMT::Stream stream;
221 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
222 if(stream.getStreamType()==0x05) {
223 SI::CharArray data=stream.getData();
224 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
225 bool prvData=false, usrData=false;
227 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
228 switch(d->getDescriptorTag()) {
229 case SI::PrivateDataSpecifierDescriptorTag:
230 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
231 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
235 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
236 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
244 if(prvData && usrData) {
245 int pid=stream.getPid();
246 d(printf("found citpid 0x%04x",pid))
247 if(!Matches(pid,0xA0)) {
249 d(printf(" (added)"))
256 NextPmt(); pmtnext=0;
259 else if(Tid==0xA0 && Source()) {
260 SI::PremiereCIT cit(Data,false);
261 if(cit.CheckCRCAndParse()) {
262 cSchedulesLock SchedulesLock(true,10);
263 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
266 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
267 SI::ShortEventDescriptor *ShortEventDescriptor=0;
268 char *order=0, *rating=0;
272 bool UseExtendedEventDescriptor=false;
273 int LanguagePreferenceShort=-1;
274 int LanguagePreferenceExt=-1;
275 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
276 switch(d->getDescriptorTag()) {
277 case 0xF0: // order information
278 if(SetupPE.OrderInfo) {
279 static const char *text[] = {
280 trNOOP("Ordernumber"),
288 const unsigned char *data=d->getData().getData()+2;
289 for(int i=0; i<5; i++) {
291 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %.*s",tr(text[i]),l,&data[1]);
294 if(p>0) order=strdup(buff);
297 case 0xF1: // parental rating
298 if(SetupPE.RatingInfo) {
301 const unsigned char *data=d->getData().getData()+2;
302 p+=snprintf(&buff[p],sizeof(buff)-p,"\n%s: %d %s",tr("Rating"),data[0]+3,tr("years"));
305 if(l>0) p+=snprintf(&buff[p],sizeof(buff)-p," (%.*s)",l,&data[1]);
306 if(p>0) rating=strdup(buff);
309 case SI::PremiereContentTransmissionDescriptorTag:
311 SI::PremiereContentTransmissionDescriptor *pct=(SI::PremiereContentTransmissionDescriptor *)d;
313 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
314 SI::Loop::Iterator it;
315 if(pct->startDayLoop.getNext(sd,it)) {
316 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
317 SI::Loop::Iterator it2;
318 if(sd.startTimeLoop.getNext(st,it2)) {
319 time_t StartTime=st.getStartTime(sd.getMJD());
320 if(nCount==1) firstTime=StartTime;
321 else if(firstTime<StartTime-5*50 || firstTime>StartTime+5*60)
327 case SI::ExtendedEventDescriptorTag:
329 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
330 if(I18nIsPreferredLanguage(Setup.EPGLanguages,eed->languageCode, LanguagePreferenceExt) || !ExtendedEventDescriptors) {
331 delete ExtendedEventDescriptors;
332 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
333 UseExtendedEventDescriptor=true;
335 if(UseExtendedEventDescriptor) {
336 ExtendedEventDescriptors->Add(eed);
337 d=NULL; // so that it is not deleted
339 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
340 UseExtendedEventDescriptor=false;
343 case SI::ShortEventDescriptorTag:
345 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
346 if(I18nIsPreferredLanguage(Setup.EPGLanguages,sed->languageCode, LanguagePreferenceShort) || !ShortEventDescriptor) {
347 delete ShortEventDescriptor;
348 ShortEventDescriptor=sed;
349 d=NULL; // so that it is not deleted
364 crc[0]=cit.getContentId();
365 SI::PremiereContentTransmissionDescriptor *pct;
366 for(SI::Loop::Iterator it; (pct=(SI::PremiereContentTransmissionDescriptor *)cit.eventDescriptors.getNext(it,SI::PremiereContentTransmissionDescriptorTag)); ) {
367 int nid=pct->getOriginalNetworkId();
368 int tid=pct->getTransportStreamId();
369 int sid=pct->getServiceId();
372 if (tid==0x03 && sid==0xf0) { tid=0x02; sid=0xe0; }
373 else if(tid==0x03 && sid==0xf1) { tid=0x02; sid=0xe1; }
374 else if(tid==0x03 && sid==0xf5) { tid=0x03; sid=0xdc; }
375 else if(tid==0x04 && sid==0xd2) { tid=0x11; sid=0xe2; }
376 else if(tid==0x11 && sid==0xd3) { tid=0x11; sid=0xe3; }
377 else if(tid==0x01 && sid==0xd4) { tid=0x04; sid=0xe4; }
380 tChannelID channelID(Source(),nid,tid,sid);
381 cChannel *channel=Channels.GetByChannelID(channelID,true);
382 if(!channel) continue;
384 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
386 pSchedule=new cSchedule(channelID);
387 Schedules->Add(pSchedule);
391 SI::PremiereContentTransmissionDescriptor::StartDayEntry sd;
393 for(SI::Loop::Iterator it; pct->startDayLoop.getNext(sd,it); ) {
395 SI::PremiereContentTransmissionDescriptor::StartDayEntry::StartTimeEntry st;
396 for(SI::Loop::Iterator it2; sd.startTimeLoop.getNext(st,it2); ) {
397 time_t StartTime=st.getStartTime(mjd);
398 time_t EndTime=StartTime+cit.getDuration();
399 int runningStatus=(StartTime<now && now<EndTime) ? SI::RunningStatusRunning : ((StartTime-30<now && now<StartTime) ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusNotRunning);
401 if(index++==0 && nCount>1) isOpt=true;
402 crc[1]=isOpt ? optCount : 0;
403 crc[2]=StartTime / STARTTIME_BIAS;
404 tEventID EventId=((('P'<<8)|'W')<<16) | crc16(0,(unsigned char *)crc,sizeof(crc));
406 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))
407 if(EndTime+Setup.EPGLinger*60<now) {
408 d2(printf("(old)\n"))
413 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,-1);
415 d2(printf("(new)\n"))
416 pEvent=new cEvent(EventId);
417 if(!pEvent) continue;
421 d2(printf("(upd)\n"))
423 if(pEvent->TableID()==0x00 || pEvent->Version()==cit.getVersionNumber()) {
424 if(pEvent->RunningStatus()!=runningStatus)
425 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
429 pEvent->SetEventID(EventId);
430 pEvent->SetTableID(Tid);
431 pEvent->SetVersion(cit.getVersionNumber());
432 pEvent->SetStartTime(StartTime);
433 pEvent->SetDuration(cit.getDuration());
435 if(ShortEventDescriptor) {
437 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
439 char buffer2[sizeof(buffer)+32];
440 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
441 pEvent->SetTitle(buffer2);
444 pEvent->SetTitle(buffer);
445 d2(printf("title: %s\n",pEvent->Title()))
446 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
448 if(ExtendedEventDescriptors) {
449 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
450 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
452 if(order || rating) {
453 int len=(pEvent->Description() ? strlen(pEvent->Description()) : 0) +
454 (order ? strlen(order) : 0) +
455 (rating ? strlen(rating) : 0);
458 if(pEvent->Description()) strcat(buffer,pEvent->Description());
459 if(rating) strcat(buffer,rating);
460 if(order) strcat(buffer,order);
461 pEvent->SetDescription(buffer);
464 if(newEvent) pSchedule->AddEvent(pEvent);
465 pEvent->SetComponents(NULL);
466 pEvent->FixEpgBugs();
467 if(pEvent->RunningStatus()!=runningStatus)
468 pSchedule->SetRunningStatus(pEvent,runningStatus,channel);
469 pSchedule->DropOutdated(StartTime,EndTime,Tid,cit.getVersionNumber());
475 Schedules->SetModified(pSchedule);
480 delete ExtendedEventDescriptors;
481 delete ShortEventDescriptor;
489 // --- cPluginPremiereEpg ------------------------------------------------------
491 static const char *DESCRIPTION = trNOOP("Parses extended Premiere EPG data");
493 class cPluginPremiereEpg : public cPlugin {
496 cFilterPremiereEpg *filter;
498 } epg[MAXDVBDEVICES];
500 cPluginPremiereEpg(void);
501 virtual const char *Version(void) { return PluginVersion; }
502 virtual const char *Description(void) { return tr(DESCRIPTION); }
503 virtual bool Start(void);
504 virtual void Stop(void);
505 virtual cMenuSetupPage *SetupMenu(void);
506 virtual bool SetupParse(const char *Name, const char *Value);
509 cPluginPremiereEpg::cPluginPremiereEpg(void)
511 memset(epg,0,sizeof(epg));
514 bool cPluginPremiereEpg::Start(void)
516 #if APIVERSNUM < 10507
517 RegisterI18n(Phrases);
519 for(int i=0; i<MAXDVBDEVICES; i++) {
520 cDevice *dev=cDevice::GetDevice(i);
523 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
524 isyslog("Attached premiere EPG filter to device %d",i);
530 void cPluginPremiereEpg::Stop(void)
532 for(int i=0; i<MAXDVBDEVICES; i++) {
533 cDevice *dev=epg[i].device;
534 if(dev) dev->Detach(epg[i].filter);
535 delete epg[i].filter;
541 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
543 return new cMenuSetupPremiereEpg;
546 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
548 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
549 else if (!strcasecmp(Name, "OrderInfo")) SetupPE.OrderInfo = atoi(Value);
550 else if (!strcasecmp(Name, "RatingInfo")) SetupPE.RatingInfo = atoi(Value);
551 else if (!strcasecmp(Name, "FixEpg")) SetupPE.FixEpg = atoi(Value);
556 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!