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.2";
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 {
70 cSetupPremiereEpg(void);
73 cSetupPremiereEpg SetupPE;
75 cSetupPremiereEpg::cSetupPremiereEpg(void)
80 // --- i18n --------------------------------------------------------------------
82 const tI18nPhrase Phrases[] = {
102 { "Parses extended Premiere EPG data",
103 "Liest erweiterte Premiere EPG Daten ein",
119 { "Tag option events",
120 "Options Events markieren",
140 // --- cMenuSetupPremiereEpg ------------------------------------------------------------
142 class cMenuSetupPremiereEpg : public cMenuSetupPage {
144 cSetupPremiereEpg data;
145 const char *optDisp[NUM_PATS];
146 char buff[NUM_PATS][32];
148 virtual void Store(void);
150 cMenuSetupPremiereEpg(void);
153 cMenuSetupPremiereEpg::cMenuSetupPremiereEpg(void)
156 SetSection(tr("PremiereEPG"));
157 optDisp[0]=tr("off");
158 for(unsigned int i=1; i<NUM_PATS; i++) {
159 snprintf(buff[i],sizeof(buff[i]),optPats[i],"Event",1);
162 Add(new cMenuEditStraItem(tr("Tag option events"),&data.OptPat,NUM_PATS,optDisp));
165 void cMenuSetupPremiereEpg::Store(void)
168 SetupStore("OptionPattern",SetupPE.OptPat);
171 // --- CIT ---------------------------------------------------------------------
179 #if BYTE_ORDER == BIG_ENDIAN
180 u_char section_syntax_indicator :1;
182 u_char section_length_hi :4;
184 u_char section_length_hi :4;
186 u_char section_syntax_indicator :1;
188 u_char section_length_lo :8;
189 u_char service_id_hi :8;
190 u_char service_id_lo :8;
191 #if BYTE_ORDER == BIG_ENDIAN
193 u_char version_number :5;
194 u_char current_next_indicator :1;
196 u_char current_next_indicator :1;
197 u_char version_number :5;
200 u_char section_number :8;
201 u_char last_section_number :8;
202 u_char content_id_hi_hi :8;
203 u_char content_id_hi_lo :8;
204 u_char content_id_lo_hi :8;
205 u_char content_id_lo_lo :8;
206 u_char duration_h :8;
207 u_char duration_m :8;
208 u_char duration_s :8;
209 #if BYTE_ORDER == BIG_ENDIAN
211 u_char descriptors_loop_length_hi :4;
213 u_char descriptors_loop_length_hi :4;
216 u_char descriptors_loop_length_lo :8;
219 class CIT : public NumberedSection {
221 CIT(const unsigned char *data, bool doCopy=true) : NumberedSection(data, doCopy) {}
223 int getContentId(void) const;
224 time_t getDuration(void) const;
225 DescriptorLoop eventDescriptors;
227 virtual void Parse(void);
232 int CIT::getContentId(void) const {
233 return (HILO(s->content_id_hi)<<16) | (HILO(s->content_id_lo));
236 time_t CIT::getDuration(void) const {
237 return DVBTime::getDuration(s->duration_h,s->duration_m,s->duration_s);
240 void CIT::Parse(void) {
241 unsigned int offset=0;
242 data.setPointerAndOffset<const cit>(s, offset);
243 eventDescriptors.setData(data+offset,HILO(s->descriptors_loop_length));
246 } // end of namespace
248 // --- cDescrF2 ----------------------------------------------------------------
254 int idx, loop, nloop, index;
256 cDescrF2(SI::Descriptor *D);
257 int TransportStreamId(void) { return data.TwoBytes(2); }
258 int OrgNetworkId(void) { return data.TwoBytes(4); }
259 int ServiceId(void) { return data.TwoBytes(6); }
262 time_t StartTime(void);
263 int Index(void) { return index; }
266 cDescrF2::cDescrF2(SI::Descriptor *D)
273 void cDescrF2::Start(void)
275 idx=8; loop=0; nloop=-3; index=-1;
278 bool cDescrF2::Next(void)
283 if(idx>=d->getLength()) return false;
284 loop=0; nloop=data[idx+2];
290 time_t cDescrF2::StartTime(void)
293 return SI::DVBTime::getTime(data[idx+0],data[idx+1],data[off+0],data[off+1],data[off+2]);
296 // --- cFilterPremiereEpg ------------------------------------------------------
298 class cFilterPremiereEpg : public cFilter {
300 int pmtpid, pmtidx, pmtnext;
304 virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
306 cFilterPremiereEpg(void);
307 virtual void SetStatus(bool On);
311 cFilterPremiereEpg::cFilterPremiereEpg(void)
317 void cFilterPremiereEpg::Trigger(void)
319 d(printf("trigger\n"))
320 pmtpid=0; pmtidx=0; pmtnext=0;
323 void cFilterPremiereEpg::SetStatus(bool On)
325 d(printf("setstatus %d\n",On))
326 cFilter::SetStatus(On);
330 void cFilterPremiereEpg::NextPmt(void)
335 d(printf("PMT next\n"))
338 void cFilterPremiereEpg::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
340 if(Pid==0 && Tid==SI::TableIdPAT) {
342 if(!pmtnext || now>pmtnext) {
343 if(pmtpid) NextPmt();
345 SI::PAT pat(Data,false);
346 if(pat.CheckCRCAndParse()) {
347 SI::PAT::Association assoc;
349 for(SI::Loop::Iterator it; pat.associationLoop.getNext(assoc,it);) {
350 if(!assoc.isNITPid()) {
352 pmtpid=assoc.getPid();
354 pmtnext=now+PMT_SCAN_TIMEOUT;
355 d(printf("PMT pid now 0x%04x (idx=%d)\n",pmtpid,pmtidx))
362 pmtnext=now+PMT_SCAN_IDLE;
363 d(printf("PMT scan idle\n"))
369 else if(pmtpid>0 && Pid==pmtpid && Tid==SI::TableIdPMT && Source() && Transponder()) {
370 SI::PMT pmt(Data,false);
371 if(pmt.CheckCRCAndParse()) {
372 SI::PMT::Stream stream;
373 for(SI::Loop::Iterator it; pmt.streamLoop.getNext(stream,it); ) {
374 if(stream.getStreamType()==0x05) {
375 SI::CharArray data=stream.getData();
376 if((data[1]&0xE0)==0xE0 && (data[3]&0xF0)==0xF0) {
377 bool prvData=false, usrData=false;
379 for(SI::Loop::Iterator it; (d=stream.streamDescriptors.getNext(it)); ) {
380 switch(d->getDescriptorTag()) {
381 case SI::PrivateDataSpecifierDescriptorTag:
382 d(printf("prv: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
383 if(d->getLength()==6 && d->getData().FourBytes(2)==0x000000be)
387 d(printf("usr: %d %08x\n",d->getLength(),d->getData().FourBytes(2)))
388 if(d->getLength()==6 && d->getData().FourBytes(2)==0x0000ffff)
396 if(prvData && usrData) {
397 int pid=stream.getPid();
398 d(printf("found citpid 0x%04x",pid))
399 if(!Matches(pid,0xA0)) {
401 d(printf(" (added)"))
408 NextPmt(); pmtnext=0;
411 else if(Tid==0xA0 && Source()) {
412 SI::CIT cit(Data,false);
413 if(cit.CheckCRCAndParse()) {
414 cSchedulesLock SchedulesLock(true,10);
415 cSchedules *Schedules=(cSchedules *)cSchedules::Schedules(SchedulesLock);
420 int LanguagePreferenceShort=-1;
421 int LanguagePreferenceExt=-1;
422 bool UseExtendedEventDescriptor=false;
423 SI::ExtendedEventDescriptors *ExtendedEventDescriptors=0;
424 SI::ShortEventDescriptor *ShortEventDescriptor=0;
425 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
426 switch(d->getDescriptorTag()) {
432 if(nCount==1) firstTime=f2.StartTime();
434 time_t time=f2.StartTime();
435 if(firstTime<time-5*50 || firstTime>time+5*60)
441 case SI::ExtendedEventDescriptorTag:
443 SI::ExtendedEventDescriptor *eed=(SI::ExtendedEventDescriptor *)d;
444 if(I18nIsPreferredLanguage(Setup.EPGLanguages,I18nLanguageIndex(eed->languageCode), LanguagePreferenceExt) || !ExtendedEventDescriptors) {
445 delete ExtendedEventDescriptors;
446 ExtendedEventDescriptors=new SI::ExtendedEventDescriptors;
447 UseExtendedEventDescriptor=true;
449 if(UseExtendedEventDescriptor) {
450 ExtendedEventDescriptors->Add(eed);
451 d=NULL; // so that it is not deleted
453 if(eed->getDescriptorNumber()==eed->getLastDescriptorNumber())
454 UseExtendedEventDescriptor=false;
457 case SI::ShortEventDescriptorTag:
459 SI::ShortEventDescriptor *sed=(SI::ShortEventDescriptor *)d;
460 if(I18nIsPreferredLanguage(Setup.EPGLanguages,I18nLanguageIndex(sed->languageCode), LanguagePreferenceShort) || !ShortEventDescriptor) {
461 delete ShortEventDescriptor;
462 ShortEventDescriptor=sed;
463 d=NULL; // so that it is not deleted
475 for(SI::Loop::Iterator it; (d=cit.eventDescriptors.getNext(it)); ) {
476 if(d->getDescriptorTag()==0xF2) {
480 tChannelID channelID(Source(),f2.OrgNetworkId(),f2.TransportStreamId(),f2.ServiceId());
481 cChannel *channel=Channels.GetByChannelID(channelID,true);
482 if(!channel) continue;
484 cSchedule *pSchedule=(cSchedule *)Schedules->GetSchedule(channelID);
486 pSchedule=new cSchedule(channelID);
487 Schedules->Add(pSchedule);
490 for(f2.Start(); f2.Next();) {
491 u_int16_t EventId=(cit.getContentId()<<4) | f2.Index();
492 time_t StartTime=f2.StartTime();
495 if(f2.Index()==0 && nCount>1) isOpt=true;
497 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))))
498 if(StartTime+cit.getDuration()+Setup.EPGLinger*60<time(0)) {
499 d2(printf("(old)\n"))
504 cEvent *pEvent=(cEvent *)pSchedule->GetEvent(EventId,StartTime);
506 d2(printf("(new)\n"))
507 #if VDRVERSNUM >= 10325
508 pEvent=new cEvent(EventId);
510 pEvent=new cEvent(channelID,EventId);
512 if(!pEvent) continue;
516 d2(printf("(upd)\n"))
518 if(pEvent->TableID()==0x00) continue;
519 if(Tid==pEvent->TableID() && pEvent->Version()==cit.getVersionNumber()) continue;
521 pEvent->SetEventID(EventId);
522 pEvent->SetTableID(Tid);
523 pEvent->SetVersion(cit.getVersionNumber());
524 pEvent->SetStartTime(StartTime);
525 pEvent->SetDuration(cit.getDuration());
527 if(ShortEventDescriptor) {
529 ShortEventDescriptor->name.getText(buffer,sizeof(buffer));
531 char buffer2[sizeof(buffer)+32];
532 snprintf(buffer2,sizeof(buffer2),optPats[SetupPE.OptPat],buffer,optCount);
533 pEvent->SetTitle(buffer2);
536 pEvent->SetTitle(buffer);
537 pEvent->SetShortText(ShortEventDescriptor->text.getText(buffer,sizeof(buffer)));
539 if(ExtendedEventDescriptors) {
540 char buffer[ExtendedEventDescriptors->getMaximumTextLength(": ")+1];
541 pEvent->SetDescription(ExtendedEventDescriptors->getText(buffer,sizeof(buffer),": "));
544 if(newEvent) pSchedule->AddEvent(pEvent);
545 pEvent->SetComponents(NULL);
546 pEvent->FixEpgBugs();
551 Schedules->SetModified(pSchedule);
556 delete ExtendedEventDescriptors;
557 delete ShortEventDescriptor;
563 // --- cPluginPremiereEpg ------------------------------------------------------
565 class cPluginPremiereEpg : public cPlugin {
568 cFilterPremiereEpg *filter;
570 } epg[MAXDVBDEVICES];
572 cPluginPremiereEpg(void);
573 virtual const char *Version(void) { return VERSION; }
574 virtual const char *Description(void) { return tr(DESCRIPTION); }
575 virtual bool Start(void);
576 virtual void Stop(void);
577 virtual cMenuSetupPage *SetupMenu(void);
578 virtual bool SetupParse(const char *Name, const char *Value);
581 cPluginPremiereEpg::cPluginPremiereEpg(void)
583 memset(epg,0,sizeof(epg));
586 bool cPluginPremiereEpg::Start(void)
588 RegisterI18n(Phrases);
589 for(int i=0; i<MAXDVBDEVICES; i++) {
590 cDevice *dev=cDevice::GetDevice(i);
593 dev->AttachFilter(epg[i].filter=new cFilterPremiereEpg);
594 isyslog("Attached premiere EPG filter to device %d",i);
600 void cPluginPremiereEpg::Stop(void)
602 for(int i=0; i<MAXDVBDEVICES; i++) {
603 cDevice *dev=epg[i].device;
604 if(dev) dev->Detach(epg[i].filter);
605 delete epg[i].filter;
611 cMenuSetupPage *cPluginPremiereEpg::SetupMenu(void)
613 return new cMenuSetupPremiereEpg;
616 bool cPluginPremiereEpg::SetupParse(const char *Name, const char *Value)
618 if (!strcasecmp(Name, "OptionPattern")) SetupPE.OptPat = atoi(Value);
623 VDRPLUGINCREATOR(cPluginPremiereEpg); // Don't touch this!