Cache AAC pid in lamedb
[openblackhole/openblackhole-enigma2.git] / lib / dvb / pmt.cpp
1 #include <lib/base/nconfig.h> // access to python config
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/pmt.h>
4 #include <lib/dvb/cahandler.h>
5 #include <lib/dvb/specs.h>
6 #include <lib/dvb/dvb.h>
7 #include <lib/dvb/metaparser.h>
8 #include <lib/dvb_ci/dvbci.h>
9 #include <lib/dvb/epgcache.h>
10 #include <lib/dvb/scan.h>
11 #include <lib/dvb_ci/dvbci_session.h>
12 #include <dvbsi++/ca_descriptor.h>
13 #include <dvbsi++/ca_program_map_section.h>
14 #include <dvbsi++/teletext_descriptor.h>
15 #include <dvbsi++/descriptor_tag.h>
16 #include <dvbsi++/iso639_language_descriptor.h>
17 #include <dvbsi++/stream_identifier_descriptor.h>
18 #include <dvbsi++/subtitling_descriptor.h>
19 #include <dvbsi++/teletext_descriptor.h>
20 #include <dvbsi++/video_stream_descriptor.h>
21 #include <dvbsi++/registration_descriptor.h>
22 #include <dvbsi++/simple_application_location_descriptor.h>
23 #include <dvbsi++/simple_application_boundary_descriptor.h>
24 #include <dvbsi++/transport_protocol_descriptor.h>
25 #include <dvbsi++/application_name_descriptor.h>
26
27 eDVBServicePMTHandler::eDVBServicePMTHandler()
28         :m_ca_servicePtr(0), m_dvb_scan(0), m_decode_demux_num(0xFF), m_no_pat_entry_delay(eTimer::create())
29 {
30         m_use_decode_demux = 0;
31         m_pmt_pid = -1;
32         m_dsmcc_pid = -1;
33         m_service_type = livetv;
34         eDVBResourceManager::getInstance(m_resourceManager);
35         CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
36         CONNECT(m_AIT.tableReady, eDVBServicePMTHandler::AITready);
37         CONNECT(m_OC.tableReady, eDVBServicePMTHandler::OCready);
38         CONNECT(m_no_pat_entry_delay->timeout, eDVBServicePMTHandler::sendEventNoPatEntry);
39 }
40
41 eDVBServicePMTHandler::~eDVBServicePMTHandler()
42 {
43         free();
44 }
45
46 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
47 {
48         int state;
49         channel->getState(state);
50
51         if ((m_last_channel_state != iDVBChannel::state_ok)
52                 && (state == iDVBChannel::state_ok))
53         {
54                 if (!m_demux && m_channel)
55                 {
56                         if (m_pvr_demux_tmp)
57                         {
58                                 m_demux = m_pvr_demux_tmp;
59                                 m_pvr_demux_tmp = NULL;
60                         }
61                         else if (m_channel->getDemux(m_demux, (!m_use_decode_demux) ? 0 : iDVBChannel::capDecode))
62                                 eDebug("[eDVBServicePMTHandler] Allocating %s-decoding a demux for now tuned-in channel failed.", m_use_decode_demux ? "" : "non-");
63                 }
64
65                 if (m_demux)
66                 {
67                         eDebug("[eDVBServicePMTHandler] ok ... now we start!!");
68                         m_have_cached_program = false;
69
70                         if (m_service && !m_service->cacheEmpty())
71                         {
72                                 serviceEvent(eventNewProgramInfo);
73                                 if (doDescramble)
74                                 {
75                                         if (!m_ca_servicePtr)
76                                         {
77                                                 registerCAService();
78                                         }
79                                 }
80                         }
81
82                         if (!m_service || m_service->usePMT())
83                         {
84                                 if (m_pmt_pid == -1)
85                                         m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
86                                 else
87                                         m_PMT.begin(eApp, eDVBPMTSpec(m_pmt_pid, m_reference.getServiceID().get()), m_demux);
88                         }
89
90                         serviceEvent(eventTuned);
91                 }
92         } else if ((m_last_channel_state != iDVBChannel::state_failed) &&
93                         (state == iDVBChannel::state_failed))
94         {
95                 eDebug("[eDVBServicePMTHandler] tune failed.");
96                 serviceEvent(eventTuneFailed);
97         }
98 }
99
100 void eDVBServicePMTHandler::channelEvent(iDVBChannel *channel, int event)
101 {
102         switch (event)
103         {
104         case iDVBChannel::evtPreStart:
105                 serviceEvent(eventPreStart);
106                 break;
107         case iDVBChannel::evtEOF:
108                 serviceEvent(eventEOF);
109                 break;
110         case iDVBChannel::evtSOF:
111                 serviceEvent(eventSOF);
112                 break;
113         case iDVBChannel::evtStopped:
114                 serviceEvent(eventStopped);
115                 break;
116         default:
117                 break;
118         }
119 }
120
121 void eDVBServicePMTHandler::registerCAService()
122 {
123         int demuxes[2] = {0, 0};
124         uint8_t demuxid;
125         uint8_t adapterid;
126         m_demux->getCADemuxID(demuxid);
127         m_demux->getCAAdapterID(adapterid);
128         demuxes[0] = demuxid;
129         if (m_decode_demux_num != 0xFF)
130                 demuxes[1] = m_decode_demux_num;
131         else
132                 demuxes[1] = demuxes[0];
133         eDVBCAHandler::getInstance()->registerService(m_reference, adapterid, demuxes, (int)m_service_type, m_ca_servicePtr);
134 }
135
136 void eDVBServicePMTHandler::PMTready(int error)
137 {
138         if (error)
139                 serviceEvent(eventNoPMT);
140         else
141         {
142                 m_have_cached_program = false;
143                 serviceEvent(eventNewProgramInfo);
144                 switch (m_service_type)
145                 {
146                 case livetv:
147                 case recording:
148                 case scrambled_recording:
149                 case timeshift_recording:
150                 case scrambled_timeshift_recording:
151                 case streamserver:
152                 case scrambled_streamserver:
153                 case streamclient:
154                         eEPGCache::getInstance()->PMTready(this);
155                         break;
156                 default:
157                         /* do not start epg caching for other types of services */
158                         break;
159                 }
160                 if (doDescramble)
161                 {
162                         if (!m_ca_servicePtr)
163                         {
164                                 registerCAService();
165                         }
166                         eDVBCIInterfaces::getInstance()->recheckPMTHandlers();
167                         eDVBCIInterfaces::getInstance()->gotPMT(this);
168                 }
169                 if (m_ca_servicePtr)
170                 {
171                         ePtr<eTable<ProgramMapSection> > ptr;
172                         if (!m_PMT.getCurrent(ptr))
173                                 eDVBCAHandler::getInstance()->handlePMT(m_reference, ptr);
174                         else
175                                 eDebug("[eDVBServicePMTHandler] cannot call buildCAPMT");
176                 }
177         }
178 }
179
180 void eDVBServicePMTHandler::sendEventNoPatEntry()
181 {
182         serviceEvent(eventNoPATEntry);
183 }
184
185 void eDVBServicePMTHandler::PATready(int)
186 {
187         eDebug("[eDVBServicePMTHandler] PATready");
188         ePtr<eTable<ProgramAssociationSection> > ptr;
189         if (!m_PAT.getCurrent(ptr))
190         {
191                 int service_id_single = -1;
192                 int pmtpid_single = -1;
193                 int pmtpid = -1;
194                 int cnt=0;
195                 std::vector<ProgramAssociationSection*>::const_iterator i;
196                 for (i = ptr->getSections().begin(); pmtpid == -1 && i != ptr->getSections().end(); ++i)
197                 {
198                         const ProgramAssociationSection &pat = **i;
199                         ProgramAssociationConstIterator program;
200                         for (program = pat.getPrograms()->begin(); pmtpid == -1 && program != pat.getPrograms()->end(); ++program)
201                         {
202                                 if (eServiceID((*program)->getProgramNumber()) == m_reference.getServiceID())
203                                         pmtpid = (*program)->getProgramMapPid();
204                                 if (++cnt == 1 && pmtpid_single == -1 && pmtpid == -1)
205                                 {
206                                         pmtpid_single = (*program)->getProgramMapPid();
207                                         service_id_single = (*program)->getProgramNumber();
208                                 }
209                                 else
210                                         pmtpid_single = service_id_single = -1;
211                         }
212                 }
213                 if (pmtpid_single != -1) // only one PAT entry .. and not valid pmtpid found
214                 {
215                         eDebug("[eDVBServicePMTHandler] use single pat entry!");
216                         m_reference.setServiceID(eServiceID(service_id_single));
217                         pmtpid = pmtpid_single;
218                 }
219                 if (pmtpid == -1) {
220                         eDebug("[eDVBServicePMTHandler] no PAT entry found.. start delay");
221                         m_no_pat_entry_delay->start(1000, true);
222                 }
223                 else {
224                         eDebug("[eDVBServicePMTHandler] use pmtpid %04x for service_id %04x", pmtpid, m_reference.getServiceID().get());
225                         m_no_pat_entry_delay->stop();
226                         m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
227                 }
228         } else
229                 serviceEvent(eventNoPAT);
230 }
231
232 void eDVBServicePMTHandler::AITready(int error)
233 {
234         eDebug("[eDVBServicePMTHandler] AITready");
235         ePtr<eTable<ApplicationInformationSection> > ptr;
236         m_aitInfoList.clear();
237         if (!m_AIT.getCurrent(ptr))
238         {
239                 m_HBBTVUrl = "";
240                 for (std::vector<ApplicationInformationSection*>::const_iterator it = ptr->getSections().begin(); it != ptr->getSections().end(); ++it)
241                 {
242                         for (std::list<ApplicationInformation *>::const_iterator i = (*it)->getApplicationInformation()->begin(); i != (*it)->getApplicationInformation()->end(); ++i)
243                         {
244                                 struct aitInfo aitinfo;
245                                 aitinfo.id = ((ApplicationIdentifier*)(*i)->getApplicationIdentifier())->getApplicationId();
246                                 for (DescriptorConstIterator desc = (*i)->getDescriptors()->begin(); desc != (*i)->getDescriptors()->end(); ++desc)
247                                 {
248                                         switch ((*desc)->getTag())
249                                         {
250                                         case APPLICATION_DESCRIPTOR:
251                                                 break;
252                                         case APPLICATION_NAME_DESCRIPTOR:
253                                         {
254                                                 ApplicationNameDescriptor *appname = (ApplicationNameDescriptor*)(*desc);
255                                                 for (ApplicationNameConstIterator appnamesit = appname->getApplicationNames()->begin(); appnamesit != appname->getApplicationNames()->end(); ++appnamesit)
256                                                 {
257                                                         aitinfo.name = (*appnamesit)->getApplicationName();
258                                                         eDebug("[eDVBServicePMTHandler] AIT: %s", aitinfo.name.c_str());
259                                                 }
260                                                 break;
261                                         }
262                                         case TRANSPORT_PROTOCOL_DESCRIPTOR:
263                                         {
264                                                 TransportProtocolDescriptor *transport = (TransportProtocolDescriptor*)(*desc);
265                                                 switch (transport->getProtocolId())
266                                                 {
267                                                 case 1: /* object carousel */
268                                                         if (m_dsmcc_pid >= 0)
269                                                         {
270                                                                 m_OC.begin(eApp, eDVBDSMCCDLDataSpec(m_dsmcc_pid), m_demux);
271                                                         }
272                                                         break;
273                                                 case 2: /* ip */
274                                                         break;
275                                                 case 3: /* interaction */
276                                                         for (InterActionTransportConstIterator interactionit = transport->getInteractionTransports()->begin(); interactionit != transport->getInteractionTransports()->end(); ++interactionit)
277                                                         {
278                                                                 if ((*i)->getApplicationControlCode() == 0x01) /* AUTOSTART */
279                                                                 {
280                                                                         m_HBBTVUrl = (*interactionit)->getUrlBase()->getUrl();
281                                                                 }
282                                                                 aitinfo.url = (*interactionit)->getUrlBase()->getUrl();
283                                                                 break;
284                                                         }
285                                                         break;
286                                                 }
287                                                 break;
288                                         }
289                                         case GRAPHICS_CONSTRAINTS_DESCRIPTOR:
290                                                 break;
291                                         case SIMPLE_APPLICATION_LOCATION_DESCRIPTOR:
292                                         {
293                                                 SimpleApplicationLocationDescriptor *applicationlocation = (SimpleApplicationLocationDescriptor*)(*desc);
294                                                 if ((*i)->getApplicationControlCode() == 0x01) /* AUTOSTART */
295                                                 {
296                                                         m_HBBTVUrl += applicationlocation->getInitialPath();
297                                                 }
298                                                 aitinfo.url += applicationlocation->getInitialPath();
299                                                 m_aitInfoList.push_back(aitinfo);
300                                                 break;
301                                         }
302                                         case APPLICATION_USAGE_DESCRIPTOR:
303                                                 break;
304                                         case SIMPLE_APPLICATION_BOUNDARY_DESCRIPTOR:
305                                                 break;
306                                         }
307                                 }
308                         }
309                 }
310                 if (!m_HBBTVUrl.empty())
311                 {
312                         serviceEvent(eventHBBTVInfo);
313                 }
314         }
315         /* for now, do not keep listening for table updates */
316         m_AIT.stop();
317 }
318
319 void eDVBServicePMTHandler::OCready(int error)
320 {
321         eDebug("[eDVBServicePMTHandler] OCready");
322         ePtr<eTable<OCSection> > ptr;
323         if (!m_OC.getCurrent(ptr))
324         {
325                 std::string data;
326                 for (std::vector<OCSection*>::const_iterator it = ptr->getSections().begin(); it != ptr->getSections().end(); ++it)
327                 {
328                 }
329         }
330         /* for now, do not keep listening for table updates */
331         m_OC.stop();
332 }
333
334 void eDVBServicePMTHandler::getAITApplications(std::map<int, std::string> &aitlist)
335 {
336         for (std::vector<struct aitInfo>::iterator it = m_aitInfoList.begin(); it != m_aitInfoList.end(); ++it)
337         {
338                 aitlist[it->id] = it->url;
339         }
340 }
341
342 void eDVBServicePMTHandler::getCaIds(std::vector<int> &caids, std::vector<int> &ecmpids)
343 {
344         program prog;
345
346         if (!getProgramInfo(prog))
347         {
348                 for (std::list<program::capid_pair>::iterator it = prog.caids.begin(); it != prog.caids.end(); ++it)
349                 {
350                         caids.push_back(it->caid);
351                         ecmpids.push_back(it->capid);
352                 }
353         }
354 }
355
356 int eDVBServicePMTHandler::getProgramInfo(program &program)
357 {
358         ePtr<eTable<ProgramMapSection> > ptr;
359         int cached_apid_ac3 = -1;
360         int cached_apid_ddp = -1;
361         int cached_apid_mpeg = -1;
362         int cached_apid_aache = -1;
363         int cached_apid_aac = -1;
364         int cached_vpid = -1;
365         int cached_tpid = -1;
366         int ret = -1;
367         uint8_t adapter, demux;
368
369         if (m_have_cached_program)
370         {
371                 program = m_cached_program;
372                 return 0;
373         }
374
375         eDVBPMTParser::clearProgramInfo(program);
376
377         if ( m_service && !m_service->cacheEmpty() )
378         {
379                 cached_vpid = m_service->getCacheEntry(eDVBService::cVPID);
380                 cached_apid_mpeg = m_service->getCacheEntry(eDVBService::cMPEGAPID);
381                 cached_apid_ac3 = m_service->getCacheEntry(eDVBService::cAC3PID);
382                 cached_apid_ddp = m_service->getCacheEntry(eDVBService::cDDPPID);
383                 cached_apid_aache = m_service->getCacheEntry(eDVBService::cAACHEAPID);
384                 cached_apid_aac = m_service->getCacheEntry(eDVBService::cAACAPID);
385                 cached_tpid = m_service->getCacheEntry(eDVBService::cTPID);
386         }
387
388         if ( ((m_service && m_service->usePMT()) || !m_service) && eDVBPMTParser::getProgramInfo(program) >= 0)
389         {
390                 unsigned int i;
391                 int first_non_mpeg = -1;
392                 int audio_cached = -1;
393                 int autoaudio_mpeg = -1;
394                 int autoaudio_ac3 = -1;
395                 int autoaudio_ddp = -1;
396                 int autoaudio_aache = -1;
397                 int autoaudio_aac = -1;
398                 int autoaudio_level = 4;
399
400                 std::string configvalue;
401                 std::vector<std::string> autoaudio_languages;
402                 configvalue = eConfigManager::getConfigValue("config.autolanguage.audio_autoselect1");
403                 if (configvalue != "" && configvalue != "None")
404                         autoaudio_languages.push_back(configvalue);
405                 configvalue = eConfigManager::getConfigValue("config.autolanguage.audio_autoselect2");
406                 if (configvalue != "" && configvalue != "None")
407                         autoaudio_languages.push_back(configvalue);
408                 configvalue = eConfigManager::getConfigValue("config.autolanguage.audio_autoselect3");
409                 if (configvalue != "" && configvalue != "None")
410                         autoaudio_languages.push_back(configvalue);
411                 configvalue = eConfigManager::getConfigValue("config.autolanguage.audio_autoselect4");
412                 if (configvalue != "" && configvalue != "None")
413                         autoaudio_languages.push_back(configvalue);
414
415                 int autosub_txt_normal = -1;
416                 int autosub_txt_hearing = -1;
417                 int autosub_dvb_normal = -1;
418                 int autosub_dvb_hearing = -1;
419                 int autosub_level =4;
420
421                 std::vector<std::string> autosub_languages;
422                 configvalue = eConfigManager::getConfigValue("config.autolanguage.subtitle_autoselect1");
423                 if (configvalue != "" && configvalue != "None")
424                         autosub_languages.push_back(configvalue);
425                 configvalue = eConfigManager::getConfigValue("config.autolanguage.subtitle_autoselect2");
426                 if (configvalue != "" && configvalue != "None")
427                         autosub_languages.push_back(configvalue);
428                 configvalue = eConfigManager::getConfigValue("config.autolanguage.subtitle_autoselect3");
429                 if (configvalue != "" && configvalue != "None")
430                         autosub_languages.push_back(configvalue);
431                 configvalue = eConfigManager::getConfigValue("config.autolanguage.subtitle_autoselect4");
432                 if (configvalue != "" && configvalue != "None")
433                         autosub_languages.push_back(configvalue);
434
435                 m_dsmcc_pid = program.dsmccPid;
436                 if (program.aitPid >= 0)
437                 {
438                         m_AIT.begin(eApp, eDVBAITSpec(program.aitPid), m_demux);
439                 }
440
441                 for (i = 0; i < program.videoStreams.size(); i++)
442                 {
443                         if (program.videoStreams[i].pid == cached_vpid)
444                         {
445                                 /* put cached vpid at the front of the videoStreams vector */
446                                 if (i > 0)
447                                 {
448                                         videoStream tmp = program.videoStreams[i];
449                                         program.videoStreams[i] = program.videoStreams[0];
450                                         program.videoStreams[0] = tmp;
451                                 }
452                                 break;
453                         }
454                 }
455                 for (i = 0; i < program.audioStreams.size(); i++)
456                 {
457                         if (program.audioStreams[i].pid == cached_apid_ac3
458                          || program.audioStreams[i].pid == cached_apid_ddp
459                          || program.audioStreams[i].pid == cached_apid_mpeg
460                          || program.audioStreams[i].pid == cached_apid_aache
461                          || program.audioStreams[i].pid == cached_apid_aac)
462                         {
463                                 /* if we find the cached pids, this will be our default stream */
464                                 audio_cached = i;
465                         }
466                         /* also, we need to know the first non-mpeg (i.e. "ac3"/dts/...) stream */
467                         if ((program.audioStreams[i].type != audioStream::atMPEG) && ((first_non_mpeg == -1)
468                                 || (program.audioStreams[i].pid == cached_apid_ac3)
469                                 || (program.audioStreams[i].pid == cached_apid_ddp)
470                                 || (program.audioStreams[i].pid == cached_apid_aache)
471                                 || (program.audioStreams[i].pid == cached_apid_aac)))
472                         {
473                                 first_non_mpeg = i;
474                         }
475                         if (!program.audioStreams[i].language_code.empty())
476                         {
477                                 int x = 1;
478                                 for (std::vector<std::string>::iterator it = autoaudio_languages.begin();x <= autoaudio_level && it != autoaudio_languages.end();x++,it++)
479                                 {
480                                         if ((*it).find(program.audioStreams[i].language_code) != std::string::npos)
481                                         {
482                                                 if (program.audioStreams[i].type == audioStream::atMPEG && (autoaudio_level > x || autoaudio_mpeg == -1))
483                                                         autoaudio_mpeg = i;
484                                                 else if (program.audioStreams[i].type == audioStream::atAC3 && (autoaudio_level > x || autoaudio_ac3 == -1))
485                                                         autoaudio_ac3 = i;
486                                                 else if (program.audioStreams[i].type == audioStream::atDDP && (autoaudio_level > x || autoaudio_ddp == -1))
487                                                         autoaudio_ddp = i;
488                                                 else if (program.audioStreams[i].type == audioStream::atAACHE && (autoaudio_level > x || autoaudio_aache == -1))
489                                                         autoaudio_aache = i;
490                                                 else if (program.audioStreams[i].type == audioStream::atAAC && (autoaudio_level > x || autoaudio_aac == -1))
491                                                         autoaudio_aac = i;
492                                                 autoaudio_level = x;
493                                                 break;
494                                         }
495                                 }
496                         }
497                 }
498                 for (i = 0; i < program.subtitleStreams.size(); i++)
499                 {
500                         if (!program.subtitleStreams[i].language_code.empty())
501                         {
502                                 int x = 1;
503                                 for (std::vector<std::string>::iterator it2 = autosub_languages.begin();x <= autosub_level && it2 != autosub_languages.end();x++,it2++)
504                                 {
505                                         if ((*it2).find(program.subtitleStreams[i].language_code) != std::string::npos)
506                                         {
507                                                 autosub_level = x;
508                                                 if (program.subtitleStreams[i].subtitling_type >= 0x10)
509                                                 {
510                                                         /* DVB subs */
511                                                         if (program.subtitleStreams[i].subtitling_type >= 0x20)
512                                                                 autosub_dvb_hearing = i;
513                                                         else
514                                                                 autosub_dvb_normal = i;
515                                                 }
516                                                 else
517                                                 {
518                                                         /* TXT subs */
519                                                         if (program.subtitleStreams[i].subtitling_type == 0x05)
520                                                                 autosub_txt_hearing = i;
521                                                         else
522                                                                 autosub_txt_normal = i;
523                                                 }
524                                                 break;
525                                         }
526                                 }
527                         }
528                 }
529
530                 bool defaultac3 = eConfigManager::getConfigBoolValue("config.autolanguage.audio_defaultac3");
531                 bool defaultddp = eConfigManager::getConfigBoolValue("config.autolanguage.audio_defaultddp");
532                 bool useaudio_cache = eConfigManager::getConfigBoolValue("config.autolanguage.audio_usecache");
533
534                 if (useaudio_cache && audio_cached != -1)
535                         program.defaultAudioStream = audio_cached;
536                 else if (defaultac3 && autoaudio_ac3 != -1)
537                         program.defaultAudioStream = autoaudio_ac3;
538                 else if (defaultddp && autoaudio_ddp != -1)
539                         program.defaultAudioStream = autoaudio_ddp;
540                 else
541                 {
542                         if (autoaudio_mpeg != -1)
543                                 program.defaultAudioStream = autoaudio_mpeg;
544                         else if (autoaudio_ac3 != -1)
545                                 program.defaultAudioStream = autoaudio_ac3;
546                         else if (autoaudio_ddp != -1)
547                                 program.defaultAudioStream = autoaudio_ddp;
548                         else if (autoaudio_aache != -1)
549                                 program.defaultAudioStream = autoaudio_aache;
550                         else if (autoaudio_aac != -1)
551                                 program.defaultAudioStream = autoaudio_aac;
552                         else if (first_non_mpeg != -1)
553                                 program.defaultAudioStream = first_non_mpeg;
554                 }
555
556                 bool allow_hearingimpaired = eConfigManager::getConfigBoolValue("config.autolanguage.subtitle_hearingimpaired");
557                 bool default_hearingimpaired = eConfigManager::getConfigBoolValue("config.autolanguage.subtitle_defaultimpaired");
558                 bool defaultdvb = eConfigManager::getConfigBoolValue("config.autolanguage.subtitle_defaultdvb");
559                 int equallanguagemask = eConfigManager::getConfigIntValue("config.autolanguage.equal_languages");
560
561                 if (defaultdvb)
562                 {
563                         if (allow_hearingimpaired && default_hearingimpaired && autosub_dvb_hearing != -1)
564                                 program.defaultSubtitleStream = autosub_dvb_hearing;
565                         else if (autosub_dvb_normal != -1)
566                                 program.defaultSubtitleStream = autosub_dvb_normal;
567                         else if (allow_hearingimpaired && autosub_dvb_hearing != -1)
568                                 program.defaultSubtitleStream = autosub_dvb_hearing;
569                         else if (allow_hearingimpaired && default_hearingimpaired && autosub_txt_hearing != -1)
570                                 program.defaultSubtitleStream = autosub_txt_hearing;
571                         else if (autosub_txt_normal != -1)
572                                 program.defaultSubtitleStream = autosub_txt_normal;
573                         else if (allow_hearingimpaired && autosub_txt_hearing != -1)
574                                 program.defaultSubtitleStream = autosub_txt_hearing;
575                 }
576                 else
577                 {
578                         if (allow_hearingimpaired && default_hearingimpaired && autosub_txt_hearing != -1)
579                                 program.defaultSubtitleStream = autosub_txt_hearing;
580                         else if (autosub_txt_normal != -1)
581                                 program.defaultSubtitleStream = autosub_txt_normal;
582                         else if (allow_hearingimpaired && autosub_txt_hearing != -1)
583                                 program.defaultSubtitleStream = autosub_txt_hearing;
584                         else if (allow_hearingimpaired && default_hearingimpaired && autosub_dvb_hearing != -1)
585                                 program.defaultSubtitleStream = autosub_dvb_hearing;
586                         else if (autosub_dvb_normal != -1)
587                                 program.defaultSubtitleStream = autosub_dvb_normal;
588                         else if (allow_hearingimpaired && autosub_dvb_hearing != -1)
589                                 program.defaultSubtitleStream = autosub_dvb_hearing;
590                 }
591                 if (program.defaultSubtitleStream != -1 && (equallanguagemask & (1<<(autosub_level-1))) == 0 && program.subtitleStreams[program.defaultSubtitleStream].language_code.compare(program.audioStreams[program.defaultAudioStream].language_code) == 0 )
592                         program.defaultSubtitleStream = -1;
593
594                 ret = 0;
595         }
596         else if ( m_service && !m_service->cacheEmpty() )
597         {
598                 int cached_pcrpid = m_service->getCacheEntry(eDVBService::cPCRPID),
599                         vpidtype = m_service->getCacheEntry(eDVBService::cVTYPE),
600                         cnt=0;
601                 if ( vpidtype == -1 )
602                         vpidtype = videoStream::vtMPEG2;
603                 if ( cached_vpid != -1 )
604                 {
605                         videoStream s;
606                         s.pid = cached_vpid;
607                         s.type = vpidtype;
608                         program.videoStreams.push_back(s);
609                         ++cnt;
610                 }
611                 if ( cached_apid_ac3 != -1 )
612                 {
613                         audioStream s;
614                         s.type = audioStream::atAC3;
615                         s.pid = cached_apid_ac3;
616                         s.rdsPid = -1;
617                         program.audioStreams.push_back(s);
618                         ++cnt;
619                 }
620                 if ( cached_apid_ddp != -1 )
621                 {
622                         audioStream s;
623                         s.type = audioStream::atDDP;
624                         s.pid = cached_apid_ddp;
625                         s.rdsPid = -1;
626                         program.audioStreams.push_back(s);
627                         ++cnt;
628                 }
629                 if ( cached_apid_aache != -1 )
630                 {
631                         audioStream s;
632                         s.type = audioStream::atAACHE;
633                         s.pid = cached_apid_aache;
634                         s.rdsPid = -1;
635                         program.audioStreams.push_back(s);
636                         ++cnt;
637                 }
638                 if ( cached_apid_aac != -1 )
639                 {
640                         audioStream s;
641                         s.type = audioStream::atAAC;
642                         s.pid = cached_apid_aac;
643                         s.rdsPid = -1;
644                         program.audioStreams.push_back(s);
645                         ++cnt;
646                 }
647                 if ( cached_apid_mpeg != -1 )
648                 {
649                         audioStream s;
650                         s.type = audioStream::atMPEG;
651                         s.pid = cached_apid_mpeg;
652                         s.rdsPid = -1;
653                         program.audioStreams.push_back(s);
654                         ++cnt;
655                 }
656                 if ( cached_pcrpid != -1 )
657                 {
658                         ++cnt;
659                         program.pcrPid = cached_pcrpid;
660                 }
661                 if ( cached_tpid != -1 )
662                 {
663                         ++cnt;
664                         program.textPid = cached_tpid;
665                 }
666                 CAID_LIST &caids = m_service->m_ca;
667                 for (CAID_LIST::iterator it(caids.begin()); it != caids.end(); ++it) {
668                         program::capid_pair pair;
669                         pair.caid = *it;
670                         pair.capid = -1; // not known yet
671                         program.caids.push_back(pair);
672                 }
673                 if ( cnt )
674                         ret = 0;
675         }
676
677         if (m_demux)
678         {
679                 m_demux->getCAAdapterID(adapter);
680                 program.adapterId = adapter;
681                 m_demux->getCADemuxID(demux);
682                 program.demuxId = demux;
683         }
684
685         m_cached_program = program;
686         m_have_cached_program = true;
687         return ret;
688 }
689
690 int eDVBServicePMTHandler::getChannel(eUsePtr<iDVBChannel> &channel)
691 {
692         channel = m_channel;
693         if (channel)
694                 return 0;
695         else
696                 return -1;
697 }
698
699 int eDVBServicePMTHandler::getDataDemux(ePtr<iDVBDemux> &demux)
700 {
701         demux = m_demux;
702         if (demux)
703                 return 0;
704         else
705                 return -1;
706 }
707
708 int eDVBServicePMTHandler::getDecodeDemux(ePtr<iDVBDemux> &demux)
709 {
710         int ret=0;
711                 /* if we're using the decoding demux as data source
712                    (for example in pvr playbacks), return that one. */
713         if (m_use_decode_demux)
714         {
715                 demux = m_demux;
716                 return ret;
717         }
718
719         ASSERT(m_channel); /* calling without a previous ::tune is certainly bad. */
720
721         ret = m_channel->getDemux(demux, iDVBChannel::capDecode);
722         if (!ret)
723                 demux->getCADemuxID(m_decode_demux_num);
724
725         return ret;
726 }
727
728 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
729 {
730         pvr_channel = m_pvr_channel;
731         if (pvr_channel)
732                 return 0;
733         else
734                 return -1;
735 }
736
737 void eDVBServicePMTHandler::SDTScanEvent(int event)
738 {
739         switch (event)
740         {
741                 case eDVBScan::evtFinish:
742                 {
743                         ePtr<iDVBChannelList> db;
744                         if (m_resourceManager->getChannelList(db) != 0)
745                                 eDebug("[eDVBServicePMTHandler] no channel list");
746                         else
747                         {
748                                 eDVBChannelID chid;
749                                 m_reference.getChannelID(chid);
750                                 if (chid == m_dvb_scan->getCurrentChannelID())
751                                 {
752                                         m_dvb_scan->insertInto(db, true);
753                                         eDebug("[eDVBServicePMTHandler] sdt update done!");
754                                 }
755                                 else
756                                         eDebug("[eDVBServicePMTHandler] ignore sdt update data.... incorrect transponder tuned!!!");
757                         }
758                         break;
759                 }
760
761                 default:
762                         break;
763         }
764 }
765
766 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *cue, bool simulate, eDVBService *service, serviceType type, bool descramble)
767 {
768         ePtr<iTsSource> s;
769         return tuneExt(ref, use_decode_demux, s, NULL, cue, simulate, service, type, descramble);
770 }
771
772 int eDVBServicePMTHandler::tuneExt(eServiceReferenceDVB &ref, int use_decode_demux, ePtr<iTsSource> &source, const char *streaminfo_file, eCueSheet *cue, bool simulate, eDVBService *service, serviceType type, bool descramble)
773 {
774         RESULT res=0;
775         m_reference = ref;
776         m_use_decode_demux = use_decode_demux;
777         m_no_pat_entry_delay->stop();
778         m_service_type = type;
779
780         doDescramble = descramble;
781
782                 /* use given service as backup. This is used for timeshift where we want to clone the live stream using the cache, but in fact have a PVR channel */
783         m_service = service;
784
785                 /* is this a normal (non PVR) channel? */
786         if (ref.path.empty())
787         {
788                 eDVBChannelID chid;
789                 ref.getChannelID(chid);
790                 res = m_resourceManager->allocateChannel(chid, m_channel, simulate);
791                 if (!simulate)
792                         eDebug("[eDVBServicePMTHandler] allocate Channel: res %d", res);
793
794                 ePtr<iDVBChannelList> db;
795                 if (!m_resourceManager->getChannelList(db))
796                         db->getService((eServiceReferenceDVB&)m_reference, m_service);
797
798                 if (!res && !simulate)
799                         eDVBCIInterfaces::getInstance()->addPMTHandler(this);
800         } else if (!simulate) // no simulation of playback services
801         {
802                 if (!ref.getServiceID().get() /* incorrect sid in meta file or recordings.epl*/ )
803                 {
804                         eDVBTSTools tstools;
805                         bool b = source || !tstools.openFile(ref.path.c_str(), 1);
806                         eWarning("[eDVBServicePMTHandler] no .meta file found, trying to find PMT pid");
807                         if (source)
808                                 tstools.setSource(source, NULL);
809                         if (b)
810                         {
811                                 eDVBPMTParser::program program;
812                                 if (!tstools.findPMT(program))
813                                 {
814                                         m_pmt_pid = program.pmtPid;
815                                         eDebug("[eDVBServicePMTHandler] PMT pid %04x, service id %d", m_pmt_pid, program.serviceId);
816                                         m_reference.setServiceID(program.serviceId);
817                                 }
818                         }
819                         else
820                                 eWarning("[eDVBServicePMTHandler] no valid source to find PMT pid!");
821                 }
822                 eDebug("[eDVBServicePMTHandler] alloc PVR");
823                         /* allocate PVR */
824                 eDVBChannelID chid;
825                 if (m_service_type == streamclient) ref.getChannelID(chid);
826                 res = m_resourceManager->allocatePVRChannel(chid, m_pvr_channel);
827                 if (res)
828                         eDebug("[eDVBServicePMTHandler] allocatePVRChannel failed!\n");
829                 m_channel = m_pvr_channel;
830         }
831
832         if (!simulate)
833         {
834                 if (m_channel)
835                 {
836                         m_channel->connectStateChange(
837                                 slot(*this, &eDVBServicePMTHandler::channelStateChanged),
838                                 m_channelStateChanged_connection);
839                         m_last_channel_state = -1;
840                         channelStateChanged(m_channel);
841
842                         m_channel->connectEvent(
843                                 slot(*this, &eDVBServicePMTHandler::channelEvent),
844                                 m_channelEvent_connection);
845
846                         if (ref.path.empty())
847                         {
848                                 m_dvb_scan = new eDVBScan(m_channel, true, false);
849                                 if (!eConfigManager::getConfigBoolValue("config.misc.disable_background_scan"))
850                                 {
851                                         /*
852                                          * not starting a dvb scan triggers what appears to be a
853                                          * refcount bug (channel?/demux?), so we always start a scan,
854                                          * but ignore the results when background scanning is disabled
855                                          */
856                                         m_dvb_scan->connectEvent(slot(*this, &eDVBServicePMTHandler::SDTScanEvent), m_scan_event_connection);
857                                 }
858                         }
859                 } else
860                 {
861                         if (res == eDVBResourceManager::errAllSourcesBusy)
862                                 serviceEvent(eventNoResources);
863                         else /* errChidNotFound, errNoChannelList, errChannelNotInList, errNoSourceFound */
864                                 serviceEvent(eventMisconfiguration);
865                         return res;
866                 }
867
868                 if (m_pvr_channel)
869                 {
870                         m_pvr_channel->setCueSheet(cue);
871
872                         if (m_pvr_channel->getDemux(m_pvr_demux_tmp, (!m_use_decode_demux) ? 0 : iDVBChannel::capDecode))
873                                 eDebug("[eDVBServicePMTHandler] Allocating %s-decoding a demux for PVR channel failed.", m_use_decode_demux ? "" : "non-");
874                         else if (source)
875                                 m_pvr_channel->playSource(source, streaminfo_file);
876                         else
877                                 m_pvr_channel->playFile(ref.path.c_str());
878
879                         if (m_service_type == offline)
880                         {
881                                 m_pvr_channel->setOfflineDecodeMode(eConfigManager::getConfigIntValue("config.recording.offline_decode_delay"));
882                         }
883                 }
884         }
885
886         return res;
887 }
888
889 void eDVBServicePMTHandler::free()
890 {
891         m_dvb_scan = 0;
892
893         if (m_ca_servicePtr)
894         {
895                 int demuxes[2] = {0,0};
896                 uint8_t demuxid;
897                 uint8_t adapterid;
898                 m_demux->getCADemuxID(demuxid);
899                 m_demux->getCAAdapterID(adapterid);
900                 demuxes[0]=demuxid;
901                 if (m_decode_demux_num != 0xFF)
902                         demuxes[1]=m_decode_demux_num;
903                 else
904                         demuxes[1]=demuxes[0];
905                 ePtr<eTable<ProgramMapSection> > ptr;
906                 m_PMT.getCurrent(ptr);
907                 eDVBCAHandler::getInstance()->unregisterService(m_reference, adapterid, demuxes, ptr);
908                 m_ca_servicePtr = 0;
909         }
910
911         if (m_channel)
912                 eDVBCIInterfaces::getInstance()->removePMTHandler(this);
913
914         if (m_pvr_channel)
915         {
916                 m_pvr_channel->stop();
917                 m_pvr_channel->setCueSheet(0);
918         }
919
920         m_OC.stop();
921         m_AIT.stop();
922         m_PMT.stop();
923         m_PAT.stop();
924         m_service = 0;
925         m_channel = 0;
926         m_pvr_channel = 0;
927         m_demux = 0;
928 }