1 #include <lib/dvb/dvbservice.h>
8 #include <lib/dvb/si.h>
9 #include <lib/dvb/decoder.h>
10 #include <lib/dvb/iso639.h>
11 #include <lib/dvb/frontend.h>
13 #include <lib/dvb/dvbci.h>
15 #include <lib/dvb/service.h>
16 #include <lib/dvb/eaudio.h>
17 #include <lib/system/info.h>
18 #include <lib/dvb/edvb.h>
19 #include <lib/dvb/record.h>
20 #include <lib/dvb/subtitling.h>
22 #if HAVE_DVB_API_VERSION < 3
24 #define DEMUX1_DEV "/dev/dvb/card0/demux1"
26 #include <linux/dvb/dmx.h>
27 #define DEMUX1_DEV "/dev/dvb/adapter0/demux1"
30 std::set
<eDVBCaPMTClient
*> eDVBCaPMTClientHandler::capmtclients
;
32 pthread_mutex_t
eDVBServiceController::availCALock
=
33 PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
;
35 PMTEntry
*eDVBServiceController::priorityAudio(PMTEntry
*audio
)
39 char *audiochannelspriority
= 0;
40 eConfig::getInstance()->getKey("/extras/audiochannelspriority", audiochannelspriority
);
42 if (audiochannelspriority
)
44 char *audiochannel
= strtok(audiochannelspriority
, "#");
45 while (audiochannel
&& audio2
== 0)
47 for (std::list
<eDVBServiceController::audioStream
>::iterator
it(audioStreams
.begin())
48 ;it
!= audioStreams
.end(); ++it
)
50 if (strcasecmp(audiochannel
, it
->text
.c_str()) == 0)
52 audio2
= it
->pmtentry
;
56 audiochannel
= strtok(NULL
, "#");
58 free(audiochannelspriority
);
65 eString
getISO639Description(char *iso
)
67 for (unsigned int i
=0; i
<sizeof(iso639
)/sizeof(*iso639
); ++i
)
69 if (!strncasecmp(iso639
[i
].iso639foreign
, iso
, 3))
70 return iso639
[i
].description1
;
71 if (!strncasecmp(iso639
[i
].iso639int
, iso
, 3))
72 return iso639
[i
].description1
;
74 return eString()+iso
[0]+iso
[1]+iso
[2];
77 void eDVBServiceController::FillPIDsFromFile(eService
*sp
)
81 eDebug("fill PIDs for %s", service
.path
.c_str());
82 int fd
=open(service
.path
.c_str(), O_RDONLY
|O_LARGEFILE
);
88 if (::read(fd
, packet
, 188) != 188)
90 // i know that THIS is not really a SIT parser :)
91 if ((packet
[0] != 0x47) || (packet
[1] != 0x40) || (packet
[2] != 0x1f) || (packet
[3] != 0x10))
94 if (memcmp(packet
+0x15, "ENIGMA", 6))
96 //failed so check another
97 if (!memcmp(packet
+0x15, "NEUTRINONG", 10))
102 // we found our private descriptor:
103 __u8
*descriptor
=packet
+0x13;
104 int len
=descriptor
[1];
105 sp
->dvb
->service_id
= eServiceID((packet
[0xf]<<8)|packet
[0x10]);
107 descriptor
+=2+nameoffset
; // skip tag, len, ENIGMA or NEUTRINONG
108 for (int i
=0; i
<len
; i
+=descriptor
[i
+1]+2)
110 int tag
=descriptor
[i
];
113 case eServiceDVB::cVPID
:
114 sp
->dvb
->set(eServiceDVB::cVPID
, (descriptor
[i
+2]<<8)|(descriptor
[i
+3]));
116 case eServiceDVB::cAPID
:
117 if (descriptor
[i
+4] == 0)
118 sp
->dvb
->set(eServiceDVB::cAPID
, (descriptor
[i
+2]<<8)|(descriptor
[i
+3]));
120 sp
->dvb
->set(eServiceDVB::cAC3PID
, (descriptor
[i
+2]<<8)|(descriptor
[i
+3]));
122 case eServiceDVB::cTPID
:
123 sp
->dvb
->set(eServiceDVB::cTPID
, (descriptor
[i
+2]<<8)|(descriptor
[i
+3]));
125 case eServiceDVB::cPCRPID
:
126 sp
->dvb
->set(eServiceDVB::cPCRPID
, (descriptor
[i
+2]<<8)|(descriptor
[i
+3]));
137 eDVBServiceController::eDVBServiceController(eDVB
&dvb
)
138 : eDVBController(dvb
)
139 , updateTDTTimer(eApp
)
140 , disableFrontendTimer(eApp
)
146 CONNECT(disableFrontendTimer
.timeout
, eDVBServiceController::disableFrontend
);
147 CONNECT(updateTDTTimer
.timeout
, eDVBServiceController::startTDT
);
148 CONNECT(dvb
.tPAT
.tableReady
, eDVBServiceController::PATready
);
149 CONNECT(dvb
.tPMT
.tableReady
, eDVBServiceController::PMTready
);
150 CONNECT(dvb
.tSDT
.tableReady
, eDVBServiceController::SDTready
);
151 CONNECT(dvb
.tEIT
.tableReady
, eDVBServiceController::EITready
);
158 calist
.setAutoDelete(true);
160 updateTDTTimer
.start(60*60*1000,true); // update time every hour to transponder time
162 eDVBCaPMTClientHandler::registerCaPMTClient(this); // static method...
165 eDVBServiceController::~eDVBServiceController()
169 eDVBCaPMTClientHandler::unregisterCaPMTClient(this); // static method...
172 void eDVBServiceController::handleEvent(const eDVBEvent
&event
)
175 static timeval last_event
;
178 gettimeofday(&now
, 0);
180 int diff
=(now
.tv_sec
-last_event
.tv_sec
)*1000000+(now
.tv_usec
-last_event
.tv_usec
);
183 char *what
="unknown";
187 case eDVBServiceEvent::eventTunedIn
: what
="eventTunedIn"; break;
188 case eDVBServiceEvent::eventServiceSwitch
: what
="ServiceSwitch"; break;
189 case eDVBServiceEvent::eventServiceTuneOK
: what
="TuneOK"; break;
190 case eDVBServiceEvent::eventServiceTuneFailed
: what
="TuneFailed"; break;
191 case eDVBServiceEvent::eventServiceGotPAT
: what
="GotPAT"; break;
192 case eDVBServiceEvent::eventServiceGotPMT
: what
="GotPMT"; break;
193 case eDVBServiceEvent::eventServiceNewPIDs
: what
="NewPIDs"; break;
194 case eDVBServiceEvent::eventServiceGotSDT
: what
="GotSDT"; break;
195 case eDVBServiceEvent::eventServiceSwitched
: what
="Switched"; break;
196 case eDVBServiceEvent::eventServiceFailed
: what
="Failed"; break;
197 default: { static char bug
[100]; sprintf(bug
, "%d", event
.type
); what
=bug
; }
200 eDebug("[PROFILE] [%s] +%dus", what
, diff
);
204 case eDVBEvent::eventTunedIn
:
205 if (transponder
==event
.transponder
)
206 dvb
.event(eDVBServiceEvent(event
.err
?eDVBServiceEvent::eventServiceTuneFailed
: eDVBServiceEvent::eventServiceTuneOK
, event
.err
, event
.transponder
));
208 case eDVBServiceEvent::eventServiceSwitch
:
210 if (!service
.path
.size()) // a normal service, not a replay
212 if (!dvb
.settings
->getTransponders())
214 eDebug("no tranponderlist");
215 service_state
=ENOENT
;
216 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceFailed
));
219 eTransponder
*n
=dvb
.settings
->getTransponders()->searchTS(service
.getDVBNamespace(), service
.getTransportStreamID(), service
.getOriginalNetworkID());
222 eDebug("no transponder %x %x", service
.getOriginalNetworkID().get(), service
.getTransportStreamID().get());
223 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceTuneFailed
));
226 if ( !(n
->state
&eTransponder::stateOK
) )
228 eDebug("couldn't tune (state is %x)", n
->state
);
229 service_state
=ENOENT
;
230 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceFailed
));
236 // dvb.setState(eDVBServiceState(eDVBServiceState::stateServiceTune));
237 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceTuneOK
, 0, n
));
240 /*emit*/ dvb
.leaveTransponder(transponder
);
243 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceTuneFailed
));
245 dvb
.setState(eDVBServiceState(eDVBServiceState::stateServiceTune
));
250 eDebug("won't tune, since its a replay.");
251 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceTuneOK
, 0, 0));
255 case eDVBServiceEvent::eventServiceTuneOK
:
257 // eDebug("apid = %04x, vpid = %04x, pcrpid = %04x, tpid = %04x", Decoder::current.apid, Decoder::current.vpid, Decoder::current.pcrpid, Decoder::current.tpid );
258 /*emit*/ dvb
.enterTransponder(event
.transponder
);
261 spSID
=service
.getServiceID().get();
262 // do we haved fixed or cached PID values?
263 eService
*sp
=eServiceInterface::getInstance()->addRef(service
);
269 FillPIDsFromFile(sp
);
272 Decoder::parms
.vpid
=sp
->dvb
->get(eServiceDVB::cVPID
);
274 int tmp
= sp
->dvb
->get(eServiceDVB::cAC3PID
);
277 Decoder::parms
.audio_type
=DECODE_AUDIO_AC3
;
278 Decoder::parms
.apid
=tmp
;
282 tmp
= sp
->dvb
->get(eServiceDVB::cAPID
);
285 Decoder::parms
.audio_type
=DECODE_AUDIO_MPEG
;
286 Decoder::parms
.apid
=tmp
;
290 Decoder::parms
.tpid
=sp
->dvb
->get(eServiceDVB::cTPID
);
291 // PCRPID ... do not set on recorded streams..
292 tmp
= sp
->dvb
->get(eServiceDVB::cPCRPID
);
293 if ( tmp
!= -1 && !service
.path
.length() )
294 Decoder::parms
.pcrpid
=tmp
;
298 if (sp
->dvb
->dxflags
& eServiceDVB::dxNoDVB
)
299 nodvb
=1; // dont use pat/pmt
301 spSID
=sp
->dvb
->service_id
.get();
303 eServiceInterface::getInstance()->removeRef(service
);
306 if ( service
.path
) // replay ?
308 dvb
.setState(eDVBServiceState(eDVBServiceState::stateServiceGetPAT
));
309 dvb
.tPAT
.start(new PAT());
313 if (nodvb
) // dont get PAT/PMT and other..
315 dvb
.tEIT
.start(new EIT(EIT::typeNowNext
, spSID
, EIT::tsActual
));
317 /*emit*/ dvb
.enterService(service
);
318 /*emit*/ dvb
.switchedService(service
, -service_state
);
319 dvb
.setState(eDVBServiceState(eDVBServiceState::stateIdle
));
324 // workaround for zap in background before recordings
325 if ( Decoder::locked
== 2 && !dvb
.recorder
)
327 eDebug("start PAT on demux1");
328 dvb
.tPAT
.start(new PAT(), DEMUX1_DEV
);
332 eDebug("start PAT on demux0");
333 dvb
.tPAT
.start(new PAT());
339 switch (service
.getServiceType())
341 case 1: // digital television service
342 case 2: // digital radio service
343 case 3: // teletext service
344 dvb
.tEIT
.start(new EIT(EIT::typeNowNext
, spSID
, EIT::tsActual
));
345 case 5: // NVOD time shifted service ( faked )
346 case 6: // mosaic service
347 dvb
.setState(eDVBServiceState(eDVBServiceState::stateServiceGetPAT
));
350 case 4: // NVOD reference service
351 dvb
.setState(eDVBServiceState(eDVBServiceState::stateServiceGetSDT
));
352 dvb
.tEIT
.start(new EIT(EIT::typeNowNext
, spSID
, EIT::tsActual
));
354 case 7: // linkage ( faked )
356 dvb
.tEIT
.start(new EIT(EIT::typeNowNext
, parentservice
.getServiceID().get(),
357 ( (service
.getTransportStreamID()==parentservice
.getTransportStreamID())
358 &&(service
.getOriginalNetworkID()==parentservice
.getOriginalNetworkID())) ? EIT::tsActual
:EIT::tsOther
));
360 dvb
.setState(eDVBServiceState(eDVBServiceState::stateServiceGetPAT
));
364 service_state
=ENOSYS
;
365 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceFailed
));
370 case eDVBServiceEvent::eventServiceTuneFailed
:
371 eDebug("[TUNE] tune failed");
372 service_state
=ENOENT
;
373 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceFailed
));
376 case eDVBServiceEvent::eventServiceGotPAT
:
379 if (dvb
.getState() != eDVBServiceState::stateServiceGetPAT
)
383 dvb
.tSDT
.start(new SDT());
385 PAT
*pat
=dvb
.tPAT
.getCurrent();
386 PATEntry
*pe
=spSID
? pat
->searchService(spSID
) : 0;
390 if ( service
.path
) // recorded ts
392 // we try to find manual the correct sid
393 int fd
= open( service
.path
.c_str(), O_RDONLY
|O_LARGEFILE
);
395 eDebug("open %s failed");
398 eDebug("parse ts file for find the correct pmtpid");
399 unsigned char *buf
= new unsigned char[256*1024]; // 256 kbyte
401 while ( pmtpid
== -1 && (rd
< 1024*1024*5) )
404 int r
= ::read( fd
, buf
, 256*1024 );
411 while ( (buf
[cnt
] != 0x47) && ((cnt
+188) < r
) && (buf
[cnt
+188] != 0x47) )
413 // eDebug("search sync byte %02x %02x, %d %d", buf[cnt], buf[cnt+188], cnt+188, r);
416 if ( buf
[cnt
] == 0x47 )
418 int pid
= ((buf
[cnt
+1]&0x3F) << 8) | buf
[cnt
+2];
419 // eDebug("addpid %d", pid);
426 for( std::set
<int>::iterator
it(pids
.begin()); pmtpid
==-1 && it
!= pids
.end(); ++it
)
428 for ( ePtrList
<PATEntry
>::iterator
i(pat
->entries
); i
!= pat
->entries
.end(); ++i
)
429 if ( i
->program_map_PID
== *it
)
432 spSID
= i
->program_number
;
433 eDebug("found pmtpid %04x for sid %d(%04x)", pmtpid
, spSID
, spSID
);
445 pmtpid
=pe
->program_map_PID
;
449 eDebug("[PAT] no pat entry");
450 service_state
=ENOENT
;
451 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceFailed
));
454 dvb
.setState(eDVBServiceState(eDVBServiceState::stateServiceGetPMT
));
456 // workaround for zap in background before recordings
457 if ( Decoder::locked
== 2 && !service
.path
&& !dvb
.recorder
)
459 eDebug("start PMT on demux1");
460 dvb
.tPMT
.start(new PMT(pmtpid
, spSID
), DEMUX1_DEV
);
464 eDebug("start PMT on demux0");
465 dvb
.tPMT
.start(new PMT(pmtpid
, spSID
));
469 case eDVBServiceEvent::eventServiceGotPMT
:
472 PMT
*pmt
=dvb
.tPMT
.ready()?dvb
.tPMT
.getCurrent():0;
478 eDVBCaPMTClientHandler::distribute_gotPMT(service
, pmt
);
480 /*emit*/ dvb
.gotPMT(pmt
);
482 if ( dvb
.tEIT
.ready() )
483 EITready(0); // fake call.. to update Audio Descriptions..
485 if (dvb
.getState()==eDVBServiceState::stateServiceGetPMT
)
486 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceSwitched
));
488 eDebug("nee, doch nicht (state ist %d)", (int)dvb
.getState());
491 case eDVBServiceEvent::eventServiceGotSDT
:
493 if (dvb
.getState() != eDVBServiceState::stateServiceGetSDT
)
496 SDT
*sdt
=dvb
.tSDT
.ready()?dvb
.tSDT
.getCurrent():0;
499 dvb
.setState(eDVBServiceState(eDVBServiceState::stateIdle
));
500 /*emit*/ dvb
.gotSDT(sdt
);
502 if (service
.getServiceType()==4)
504 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceSwitched
));
506 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceFailed
));
509 case eDVBServiceEvent::eventServiceNewPIDs
:
512 case eDVBServiceEvent::eventServiceSwitched
:
516 eString filename
= service
.path
;
517 filename
.erase(filename
.length()-2, 2);
519 int fd
= ::open( filename
.c_str(), O_RDONLY
);
523 int rd
= ::read(fd
, buf
, 4096);
525 if ( rd
> 12 /*EIT_LOOP_SIZE*/ )
529 e
->type
=EIT::typeNowNext
;
531 e
->current_next_indicator
=0;
532 e
->transport_stream_id
=service
.getTransportStreamID().get();
533 e
->original_network_id
=service
.getOriginalNetworkID().get();
534 EITEvent
*evt
= new EITEvent( (eit_event_struct
*)buf
, (e
->transport_stream_id
<<16)|e
->original_network_id
);
536 e
->events
.push_back(evt
);
543 /*emit*/ dvb
.enterService(service
);
544 case eDVBServiceEvent::eventServiceFailed
:
545 /*emit*/ dvb
.switchedService(service
, -service_state
);
546 dvb
.setState(eDVBServiceState(eDVBServiceState::stateIdle
));
551 void eDVBServiceController::PATready(int error
)
553 eDebug("PATready (%d)",error
);
554 dvb
.event(eDVBServiceEvent(error
?eDVBServiceEvent::eventServiceFailed
:eDVBServiceEvent::eventServiceGotPAT
));
557 void eDVBServiceController::SDTready(int error
)
559 eDebug("SDTready (%d)", error
);
560 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceGotSDT
));
561 if (dvb
.settings
->getTransponders())
563 SDT
*sdt
=dvb
.tSDT
.ready()?dvb
.tSDT
.getCurrent():0;
566 dvb
.settings
->getTransponders()->startHandleSDT(sdt
, service
.getDVBNamespace(), -1, -1, &freeCheckFinishedCallback
, transponder
->state
& eTransponder::stateOnlyFree
? eTransponderList::SDT_SCAN_FREE
: eTransponderList::SDT_SCAN
);
572 void eDVBServiceController::freeCheckFinished()
574 eDebug("freeCheckFinished");
577 void eDVBServiceController::PMTready(int error
)
579 eDebug("PMTready (%d)", error
);
580 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceGotPMT
));
583 void eDVBServiceController::EITready(int error
)
585 eDebug("EITready (%d)", error
);
586 bool haveAC3
=false, changed
=false;
589 EIT
*eit
=dvb
.getEIT();
591 for (ePtrList
<EITEvent
>::iterator
e(eit
->events
); e
!= eit
->events
.end(); ++e
)
593 // eDebug("running_status = %d", e->running_status );
594 if ((e
->running_status
>=2) || (!e
->running_status
) ) // currently running service
596 for (ePtrList
<Descriptor
>::iterator
d(e
->descriptor
); d
!= e
->descriptor
.end(); ++d
)
598 if (d
->Tag()==DESCR_COMPONENT
)
600 for (std::list
<audioStream
>::iterator
it(audioStreams
.begin())
601 ;it
!= audioStreams
.end(); ++it
)
603 if (((ComponentDescriptor
*)*d
)->component_tag
== it
->component_tag
)
606 eString tmp
= ((ComponentDescriptor
*)*d
)->text
;
612 else if ( it
->isDTS
)
616 if ( !haveAC3
&& (it
->isAC3
|| it
->isDTS
) )
625 if ( service
.getServiceType() == 4 ) // NVOD Service
627 delete dvb
.parentEIT
;
628 dvb
.parentEIT
= new EIT( eit
);
629 dvb
.parentEIT
->events
.setAutoDelete(true);
630 eit
->events
.setAutoDelete(false);
632 /*emit*/ dvb
.gotEIT(eit
, 0);
636 /*emit*/ dvb
.gotEIT(0, error
);
638 if ( changed
&& !(haveAC3
&& eAudio::getInstance()->getAC3default()) )
641 audio
= priorityAudio(audio
);
650 // defines for DM7000 / DM7020
651 #define FP_IOCTL_SET_RTC 0x101
652 #define FP_IOCTL_GET_RTC 0x102
654 static time_t prev_time
;
656 void setRTC(time_t time
)
658 int fd
= open("/dev/dbox/fp0", O_RDWR
);
661 if ( ::ioctl(fd
, FP_IOCTL_SET_RTC
, (void*)&time
) < 0 )
662 eDebug("FP_IOCTL_SET_RTC failed(%m)");
672 int fd
= open("/dev/dbox/fp0", O_RDWR
);
675 if ( ::ioctl(fd
, FP_IOCTL_GET_RTC
, (void*)&rtc_time
) < 0 )
676 eDebug("FP_IOCTL_GET_RTC failed(%m)");
679 return rtc_time
!= prev_time
? rtc_time
: 0;
682 void eDVBServiceController::TDTready(int error
)
684 eDebug("TDTready %d", error
);
685 // receive new TDT every 60 minutes
686 updateTDTTimer
.start(60*60*1000,true);
687 if (!error
&& transponder
)
689 std::map
<tsref
,int> &tOffsMap
= eTransponderList::getInstance()->TimeOffsetMap
;
690 std::map
< tsref
, int >::iterator
it( tOffsMap
.find( *transponder
) );
692 // current linux time
693 time_t linuxTime
= time(0);
695 // current enigma time
696 time_t nowTime
=linuxTime
+dvb
.time_difference
;
698 // difference between current enigma time and transponder time
699 int enigma_diff
= tdt
->UTC_time
-nowTime
;
703 if (timeSet
) // ref time ready?
705 // difference between reference time (current enigma time)
706 // and the transponder time
707 eDebug("[TIME] diff is %d", enigma_diff
);
708 if ( abs(enigma_diff
) < 120 )
710 eDebug("[TIME] diff < 120 .. use Transponder Time");
711 tOffsMap
[*transponder
] = 0;
712 new_diff
= enigma_diff
;
714 else if ( it
!= tOffsMap
.end() ) // correction saved?
716 eDebug("[TIME] we have correction %d", it
->second
);
717 time_t CorrectedTpTime
= tdt
->UTC_time
+it
->second
;
718 int ddiff
= CorrectedTpTime
-nowTime
;
719 eDebug("[TIME] diff after add correction is %d", ddiff
);
720 if ( abs(it
->second
) < 300 ) // stored correction < 5 min
722 eDebug("[TIME] use stored correction(<5 min)");
725 else if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020
&&
729 tOffsMap
[*transponder
] = rtc
-tdt
->UTC_time
;
730 new_diff
= rtc
-nowTime
; // set enigma time to rtc
731 eDebug("[TIME] update stored correction to %d (calced against RTC time)", rtc
-tdt
->UTC_time
);
733 else if ( abs(ddiff
) <= 120 )
735 // with stored correction calced time difference is lower 2 min
736 // this don't help when a transponder have a clock running to slow or to fast
737 // then its better to have a DM7020 with always running RTC
738 eDebug("[TIME] use stored correction(corr < 2 min)");
741 else // big change in calced correction.. hold current time and update correction
743 eDebug("[TIME] update stored correction to %d", -enigma_diff
);
744 tOffsMap
[*transponder
] = -enigma_diff
;
749 eDebug("[TIME] no correction found... store calced correction(%d)",-enigma_diff
);
750 tOffsMap
[*transponder
] = -enigma_diff
;
753 else // no time setted yet
755 if ( it
!= tOffsMap
.end() )
757 enigma_diff
+= it
->second
;
758 eDebug("[TIME] we have correction (%d)... use", it
->second
);
761 eDebug("[TIME] dont have correction.. set Transponder Diff");
762 new_diff
=enigma_diff
;
765 time_t t
= nowTime
+new_diff
;
766 lastTpTimeDifference
=tdt
->UTC_time
-t
;
770 eDebug("[TIME] not changed");
774 tm now
= *localtime(&t
);
775 eDebug("[TIME] time update to %02d:%02d:%02d",
780 dvb
.time_difference
= t
- linuxTime
; // calc our new linux_time -> enigma_time correction
781 eDebug("[TIME] time_difference is %d", dvb
.time_difference
);
783 if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020
)
786 if ( abs(dvb
.time_difference
) > 59 )
788 eDebug("[TIME] set Linux Time");
790 gettimeofday(&tnow
,0);
792 settimeofday(&tnow
,0);
793 for (ePtrList
<eMainloop
>::iterator
it(eMainloop::existing_loops
)
794 ;it
!= eMainloop::existing_loops
.end(); ++it
)
795 it
->setTimerOffset(dvb
.time_difference
);
796 dvb
.time_difference
=1;
798 else if ( !dvb
.time_difference
)
799 dvb
.time_difference
=1;
803 /*emit*/ dvb
.timeUpdated();
805 else if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020
||
806 ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
807 && eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) )
809 eDebug("[TIME] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
810 time_t rtc_time
= getRTC();
811 if ( rtc_time
) // RTC Ready?
813 tm now
= *localtime(&rtc_time
);
814 eDebug("[TIME] RTC time is %02d:%02d:%02d",
818 time_t linuxTime
=time(0);
819 time_t nowTime
=linuxTime
+dvb
.time_difference
;
820 now
= *localtime(&nowTime
);
821 eDebug("[TIME] Receiver time is %02d:%02d:%02d",
825 dvb
.time_difference
= rtc_time
- linuxTime
;
826 eDebug("[TIME] RTC to Receiver time difference is %d seconds", nowTime
- rtc_time
);
827 if ( abs(dvb
.time_difference
) > 59 )
829 eDebug("[TIME] set Linux Time to RTC Time");
831 gettimeofday(&tnow
,0);
832 tnow
.tv_sec
=rtc_time
;
833 settimeofday(&tnow
,0);
834 for (ePtrList
<eMainloop
>::iterator
it(eMainloop::existing_loops
)
835 ;it
!= eMainloop::existing_loops
.end(); ++it
)
836 it
->setTimerOffset(dvb
.time_difference
);
837 dvb
.time_difference
=1;
839 else if ( !dvb
.time_difference
)
840 dvb
.time_difference
=1;
842 eDebug("[TIME] set to RTC time");
843 /*emit*/ dvb
.timeUpdated();
846 eDebug("[TIME] shit RTC not ready :(");
850 void eDVBServiceController::scanPMT( PMT
*pmt
)
852 Decoder::parms
.pmtpid
=pmt
->pid
;
854 usedCASystems
.clear();
856 PMTEntry
*audio
=0, *ac3_audio
=0, *video
=0, *teletext
=0;
858 int audiopid
=-1, videopid
=-1, ac3pid
=-1, tpid
=-1;
860 int sac3default
=eAudio::getInstance()->getAC3default();
862 if ( Decoder::parms
.pcrpid
!= pmt
->PCR_PID
&& !service
.path
.size() )
863 Decoder::parms
.pcrpid
= pmt
->PCR_PID
;
865 // get last selected audio / video pid from pid cache
866 eService
*sp
=eServiceInterface::getInstance()->addRef(service
);
872 FillPIDsFromFile(sp
);
874 videopid
=sp
->dvb
->get(eServiceDVB::cVPID
);
875 audiopid
=sp
->dvb
->get(eServiceDVB::cAPID
);
876 ac3pid
=sp
->dvb
->get(eServiceDVB::cAC3PID
);
877 sp
->dvb
->set(eServiceDVB::cPCRPID
, Decoder::parms
.pcrpid
);
881 int isca
=checkCA(calist
, pmt
->program_info
, pmt
->program_number
);
883 audioStreams
.clear();
884 videoStreams
.clear();
885 subtitleStreams
.clear();
889 ePtrList
<PMTEntry
>::iterator TTXIt
=pmt
->streams
.end();
890 for (ePtrList
<PMTEntry
>::iterator
i(pmt
->streams
); i
!= pmt
->streams
.end(); ++i
)
896 switch (pe
->stream_type
)
898 case 1: // ISO/IEC 11172 Video
899 case 2: // ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream
900 if ( (!video
) || (pe
->elementary_PID
== videopid
) )
902 isca
+=checkCA(calist
, pe
->ES_info
, pmt
->program_number
);
903 videoStreams
.push_back(pe
);
905 case 3: // ISO/IEC 11172 Audio
906 case 4: // ISO/IEC 13818-3 Audio
910 isca
+=checkCA(calist
, pe
->ES_info
, pmt
->program_number
);
911 audioStream
stream(pe
);
912 for (ePtrList
<Descriptor
>::iterator
ii(pe
->ES_info
); ii
!= pe
->ES_info
.end(); ++ii
)
919 case DESCR_REGISTRATION
:
920 if (!memcmp(((RegistrationDescriptor
*)*ii
)->format_identifier
, "DTS", 3))
924 if ( (!teletext
) || (pe
->elementary_PID
== tpid
) )
930 case DESCR_SUBTITLING
:
931 subtitleStreams
.push_back(pe
);
933 case DESCR_ISO639_LANGUAGE
:
934 stream
.text
=getISO639Description(((ISO639LanguageDescriptor
*)*ii
)->language_code
);
936 case DESCR_STREAM_ID
:
937 stream
.component_tag
=((StreamIdentifierDescriptor
*)*ii
)->component_tag
;
939 case DESCR_LESRADIOS
:
941 LesRadiosDescriptor
*d
= (LesRadiosDescriptor
*)*ii
;
942 if ( (stream
.component_tag
>= 0 || d
->id
) && d
->name
)
943 stream
.text
.sprintf("%d.) %s", d
->id
, d
->name
.c_str());
955 stream
.text
+=" (AC3)";
957 if ( (!ac3_audio
) || (pe
->elementary_PID
== ac3pid
) )
960 else if (stream
.isDTS
)
962 stream
.text
+=" (DTS)";
964 if ( (!ac3_audio
) || (pe
->elementary_PID
== ac3pid
) )
967 else if (isAudio
&& ( (!audio
) || (pe
->elementary_PID
== audiopid
) ) )
971 stream
.text
.sprintf("PID %04x", pe
->elementary_PID
);
974 audioStreams
.push_back(stream
);
976 case 5: // private section
978 if ( content_pid
!= -1)
980 for (ePtrList
<Descriptor
>::iterator
ii(pe
->ES_info
); ii
!= pe
->ES_info
.end(); ++ii
)
984 case DESCR_PRIV_DATA_SPEC
:
985 if ( ((PrivateDataSpecifierDescriptor
*)*ii
)->private_data_specifier
== 190 )
990 UnknownDescriptor
*descr
= (UnknownDescriptor
*) *ii
;
991 if ( descr
->length() == 4 && !descr
->data
[0] && !descr
->data
[1] && descr
->data
[2] == 0xFF && descr
->data
[3] == 0xFF )
999 content_pid
= pe
->elementary_PID
;
1006 if ( content_pid
!= -1 )
1007 /*emit*/ dvb
.gotContentPid(content_pid
);
1009 // get audio priority channel
1010 audio
= priorityAudio(audio
);
1012 ePtrList
<PMTEntry
>::iterator tmp
= pmt
->streams
.end();
1015 // move teletext to the end of the list
1018 std::iter_swap(tmp
, TTXIt
);
1021 int needAC3Workaround
=0;
1022 switch (eSystemInfo::getInstance()->getHwType())
1024 case eSystemInfo::dbox2Nokia
:
1025 case eSystemInfo::dbox2Philips
:
1026 case eSystemInfo::dbox2Sagem
:
1027 needAC3Workaround
=1;
1032 if ( !needAC3Workaround
&& ac3_audio
&& ( sac3default
|| (ac3pid
!= -1) || (!audio
) ) )
1040 if ( video
->elementary_PID
!= videopid
)
1043 else if (sp
&& sp
->dvb
) // handle no more existing vpid
1045 Decoder::parms
.vpid
= -1;
1046 sp
->dvb
->set(eServiceDVB::cVPID
, -1);
1051 if ( audiopid
!= audio
->elementary_PID
)
1054 else if (sp
&& sp
->dvb
) // handle no more existing apid
1056 Decoder::parms
.apid
= -1;
1057 sp
->dvb
->set(eServiceDVB::cAPID
, -1);
1058 sp
->dvb
->set(eServiceDVB::cAC3PID
, -1);
1063 if ( tpid
!= teletext
->elementary_PID
)
1066 else if (sp
&& sp
->dvb
) // handle no more existing tpid
1068 Decoder::parms
.tpid
= -1;
1069 sp
->dvb
->set(eServiceDVB::cTPID
, -1);
1072 /*emit*/ dvb
.scrambled(isca
);
1075 eConfig::getInstance()->getKey("/elitedvb/extra/hideerror", hideerror
);
1076 if ( hideerror
|| (dvb
.recorder
&& service
.path
) )
1078 else if (isca
&& !service
.path
&& !calist
)
1081 service_state
=ENOCASYS
;
1084 if ((Decoder::parms
.vpid
==-1) && (Decoder::parms
.apid
==-1))
1085 service_state
=ENOSTREAM
;
1087 // for (ePtrList<CA>::iterator i(calist); i != calist.end(); ++i)
1088 // eDebug("CA %04x ECMPID %04x", i->casysid, i->ecmpid);
1092 // AC3 DBOX2 Workaround... buggy drivers...
1093 if ( needAC3Workaround
&& ac3_audio
&& ( sac3default
|| (ac3pid
!= -1) || (!audio
) ) )
1100 eServiceInterface::getInstance()->removeRef(service
);
1103 int eDVBServiceController::switchService(const eServiceReferenceDVB
&newservice
)
1105 if (newservice
== service
)
1107 eDebug("is same service..");
1113 #ifndef DISABLE_FILE
1114 eServiceReferenceDVB recRef
=
1115 dvb
.recorder
&& dvb
.recorder
->recRef
?
1116 dvb
.recorder
->recRef
: eServiceReferenceDVB();
1117 recRef
.data
[0] = service
.getServiceType();
1121 #ifndef DISABLE_FILE
1123 && Decoder::locked
!= 2 // leave service for (timer) zap in Background
1124 && service
!= recRef
1128 // must replace faked service types.. for capmt handlers
1129 eServiceReferenceDVB ref
=service
;
1130 switch(ref
.getServiceType())
1134 ref
.data
[0]=1; // TV
1137 eDVBCaPMTClientHandler::distribute_leaveService(ref
); // capmt handler call..
1140 /*emit*/ dvb
.leaveService(service
);
1142 // Linkage service handling..
1143 if ( newservice
.getServiceType()==7 && prevservice
)
1145 parentservice
= prevservice
;
1146 prevservice
= eServiceReferenceDVB();
1151 if ( service
.getServiceType() != 7 )
1152 prevservice
=service
; // save currentservice
1153 // when in 15 seconds no other dvb service is running disable frontend
1154 disableFrontendTimer
.start(15*1000, true);
1156 /////////////////////////////////
1160 dvb
.tEIT
.start(0); // clear eit
1161 dvb
.tPAT
.start(0); // clear tables.
1167 if ( service
&& !service
.path
)
1168 eDVBCaPMTClientHandler::distribute_enterService(service
); // capmt handler call..
1170 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceSwitch
));
1173 switch(newservice
.getServiceType())
1175 case 1: // tv service
1176 case 2: // radio service
1177 case 4: // nvod parent service
1178 case 7: // linkage service
1179 delete dvb
.parentEIT
;
1182 case 5: // nvod ref service
1183 // send Parent EIT .. for osd text..
1191 void eDVBServiceController::handlePMT(const eServiceReferenceDVB
&ref
, PMT
*pmt
)
1193 if ( CIServices
.find(ref
) != CIServices
.end() )
1195 int prevPMTVersion
= CIServices
[ref
];
1196 if ( prevPMTVersion
== pmt
->version
)
1198 eDebug("[eDVBCIHandler] dont send pmt with self pmt version");
1204 eDebug("[eDVBCIHandler] get PMT for unknown service");
1208 int sid
= pmt
->program_number
;
1210 if ( eSystemInfo::getInstance()->hasCI() )
1214 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTsetVersion
, pmt
->program_number
, pmt
->version
, -1 ));
1216 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTsetVersion
, pmt
->program_number
, pmt
->version
, -1 ));
1218 for (ePtrList
<Descriptor
>::const_iterator
i(pmt
->program_info
);
1219 i
!= pmt
->program_info
.end(); ++i
)
1221 if (i
->Tag()==9) // CADescriptor
1223 CADescriptor
*ca
=(CADescriptor
*)*i
;
1226 unsigned char *buf
=new unsigned char[ca
->data
[1]+2];
1227 memcpy(buf
, ca
->data
, ca
->data
[1]+2);
1228 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTaddDescriptor
, sid
, buf
));
1232 unsigned char *buf2
=new unsigned char[ca
->data
[1]+2];
1233 memcpy(buf2
, ca
->data
, ca
->data
[1]+2);
1234 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTaddDescriptor
, sid
, buf2
));
1239 for (ePtrList
<PMTEntry
>::iterator
i(pmt
->streams
);
1240 i
!= pmt
->streams
.end(); ++i
)
1245 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTaddPID
, pmt
->program_number
, pe
->elementary_PID
, pe
->stream_type
));
1247 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTaddPID
, pmt
->program_number
, pe
->elementary_PID
, pe
->stream_type
));
1249 switch (pe
->stream_type
)
1251 case 1: // ISO/IEC 11172 Video
1252 case 2: // ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream
1253 case 3: // ISO/IEC 11172 Audio
1254 case 4: // ISO/IEC 13818-3 Audio
1257 for (ePtrList
<Descriptor
>::const_iterator
i(pe
->ES_info
);
1258 i
!= pe
->ES_info
.end(); ++i
)
1260 if (i
->Tag()==9) // CADescriptor
1262 CADescriptor
*ca
=(CADescriptor
*)*i
;
1265 unsigned char *buf
=new unsigned char[ca
->data
[1]+2];
1266 memcpy(buf
, ca
->data
, ca
->data
[1]+2);
1267 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTaddDescriptor
, sid
, buf
));
1271 unsigned char *buf2
=new unsigned char[ca
->data
[1]+2];
1272 memcpy(buf2
, ca
->data
, ca
->data
[1]+2);
1273 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTaddDescriptor
, sid
, buf2
));
1282 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::go
));
1284 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::go
));
1285 CIServices
[ref
]=pmt
->version
;
1288 void eDVBServiceController::enterService( const eServiceReferenceDVB
&service
)
1292 if ( CIServices
.find(service
) == CIServices
.end() )
1294 // eDebug("[eDVBCIHandler] new service %s", service.toString().c_str() );
1295 CIServices
[service
]=-1;
1299 void eDVBServiceController::leaveService( const eServiceReferenceDVB
&service
)
1303 std::map
<eServiceReferenceDVB
,int>::iterator it
= CIServices
.find(service
);
1304 if ( it
!= CIServices
.end() )
1306 // eDebug("[eDVBCIHandler] leave service %s", service.toString().c_str() );
1307 CIServices
.erase(it
);
1309 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTflush
, service
.getServiceID().get() ));
1311 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::PMTflush
, service
.getServiceID().get() ));
1316 void eDVBServiceController::setPID(const PMTEntry
*entry
)
1320 int isvideo
=0, isaudio
=0, isteletext
=0, isAC3
=0;
1321 switch (entry
->stream_type
)
1323 case 1: // ISO/IEC 11172 Video
1324 case 2: // ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream
1327 case 3: // ISO/IEC 11172 Audio
1328 case 4: // ISO/IEC 13818-3 Audio
1333 for (ePtrList
<Descriptor
>::const_iterator
i(entry
->ES_info
);
1334 i
!= entry
->ES_info
.end(); ++i
)
1336 if (i
->Tag()==DESCR_AC3
)
1341 else if (i
->Tag() == DESCR_REGISTRATION
)
1343 RegistrationDescriptor
*reg
=(RegistrationDescriptor
*)*i
;
1344 if (!memcmp(reg
->format_identifier
, "DTS", 3))
1349 } else if (i
->Tag()==DESCR_TELETEXT
)
1355 eService
*sp
=eServiceInterface::getInstance()->addRef(service
);
1360 Decoder::parms
.audio_type
=DECODE_AUDIO_AC3
;
1361 Decoder::parms
.apid
=entry
->elementary_PID
;
1364 sp
->dvb
->set(eServiceDVB::cAC3PID
, entry
->elementary_PID
);
1365 sp
->dvb
->set(eServiceDVB::cAPID
, -1);
1369 Decoder::parms
.audio_type
=DECODE_AUDIO_MPEG
;
1370 Decoder::parms
.apid
=entry
->elementary_PID
;
1373 sp
->dvb
->set(eServiceDVB::cAC3PID
, -1);
1374 sp
->dvb
->set(eServiceDVB::cAPID
, entry
->elementary_PID
);
1380 Decoder::parms
.vpid
=entry
->elementary_PID
;
1382 sp
->dvb
->set(eServiceDVB::cVPID
, entry
->elementary_PID
);
1384 else if (isteletext
)
1386 Decoder::parms
.tpid
=entry
->elementary_PID
;
1388 sp
->dvb
->set(eServiceDVB::cTPID
, entry
->elementary_PID
);
1392 eServiceInterface::getInstance()->removeRef(service
);
1396 void eDVBServiceController::setDecoder()
1398 dvb
.event(eDVBServiceEvent(eDVBServiceEvent::eventServiceNewPIDs
));
1402 int eDVBServiceController::checkCA(ePtrList
<CA
> &list
, const ePtrList
<Descriptor
> &descriptors
, int sid
)
1405 for (ePtrList
<Descriptor
>::const_iterator
i(descriptors
);
1406 i
!= descriptors
.end(); ++i
)
1408 if (i
->Tag()==9) // CADescriptor
1411 CADescriptor
*ca
=(CADescriptor
*)*i
;
1414 // this is old unneeded code for camd call..
1415 // now we do this in eDVBCAHandler..
1416 Decoder::addCADescriptor((__u8
*)(ca
->data
));
1421 singleLock
s(availCALock
);
1422 if (availableCASystems
.find(ca
->CA_system_ID
) != availableCASystems
.end())
1426 usedCASystems
.insert(ca
->CA_system_ID
);
1430 for (ePtrList
<CA
>::iterator a
= list
.begin();
1431 a
!= list
.end(); a
++)
1433 if (a
->casysid
==ca
->CA_system_ID
)
1442 n
->ecmpid
=ca
->CA_PID
;
1443 n
->casysid
=ca
->CA_system_ID
;
1453 void eDVBServiceController::initCAlist()
1455 singleLock
s(availCALock
);
1456 availableCASystems
=eSystemInfo::getInstance()->getCAIDs();
1459 void eDVBServiceController::clearCAlist()
1462 singleLock
s(availCALock
);
1463 availableCASystems
.clear();
1468 DVBCI
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::getcaids
));
1470 DVBCI2
->messages
.send(eDVBCI::eDVBCIMessage(eDVBCI::eDVBCIMessage::getcaids
));
1474 void eDVBServiceController::disableFrontend()
1476 if ( transponder
&& (!service
|| service
.path
) &&
1477 #ifndef DISABLE_FILE
1480 !dvb
.getScanAPI() ) // no more service need the frontend..
1482 eDebug("no more dvb service running.. disable Frontend");
1484 eFrontend::getInstance()->savePower();
1485 if ( eDVB::getInstance()->time_difference
&& // have valid time?
1486 eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
1487 && eSystemInfo::getInstance()->hasStandbyWakeupTimer() )
1489 eDebug("set RTC when frontend is disabled..");
1490 time_t nowTime
=time(0)+eDVB::getInstance()->time_difference
;
1496 void eDVBServiceController::startTDT()
1501 CONNECT(tdt
->tableReady
, eDVBServiceController::TDTready
);
1502 if ( (Decoder::locked
== 2 && !dvb
.recorder
) ||
1503 (dvb
.recorder
&& eSystemInfo::getInstance()->canTimeshift()
1504 && eServiceInterface::getInstance()->service
.path
) )
1506 tdt
->start(DEMUX1_DEV
);
1507 eDebug("start TDT on demux1");
1512 eDebug("start TDT on demux0");