Merge branch 'master' of git.opendreambox.org:/git/enigma2
[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/specs.h>
5 #include <lib/dvb/dvb.h>
6 #include <lib/dvb/metaparser.h>
7 #include <lib/dvb_ci/dvbci.h>
8 #include <lib/dvb/epgcache.h>
9 #include <lib/dvb/scan.h>
10 #include <lib/dvb_ci/dvbci_session.h>
11 #include <dvbsi++/ca_descriptor.h>
12 #include <dvbsi++/ca_program_map_section.h>
13 #include <dvbsi++/teletext_descriptor.h>
14 #include <dvbsi++/descriptor_tag.h>
15 #include <dvbsi++/iso639_language_descriptor.h>
16 #include <dvbsi++/stream_identifier_descriptor.h>
17 #include <dvbsi++/subtitling_descriptor.h>
18 #include <dvbsi++/teletext_descriptor.h>
19 #include <dvbsi++/video_stream_descriptor.h>
20
21 eDVBServicePMTHandler::eDVBServicePMTHandler()
22         :m_ca_servicePtr(0), m_dvb_scan(0), m_decode_demux_num(0xFF)
23 {
24         m_use_decode_demux = 0;
25         m_pmt_pid = -1;
26         eDVBResourceManager::getInstance(m_resourceManager);
27         CONNECT(m_PMT.tableReady, eDVBServicePMTHandler::PMTready);
28         CONNECT(m_PAT.tableReady, eDVBServicePMTHandler::PATready);
29 }
30
31 eDVBServicePMTHandler::~eDVBServicePMTHandler()
32 {
33         free();
34 }
35
36 void eDVBServicePMTHandler::channelStateChanged(iDVBChannel *channel)
37 {
38         int state;
39         channel->getState(state);
40         
41         if ((m_last_channel_state != iDVBChannel::state_ok)
42                 && (state == iDVBChannel::state_ok) && (!m_demux))
43         {
44                 if (m_channel)
45                         if (m_channel->getDemux(m_demux, (!m_use_decode_demux) ? 0 : iDVBChannel::capDecode))
46                                 eDebug("Allocating %s-decoding a demux for now tuned-in channel failed.", m_use_decode_demux ? "" : "non-");
47                 
48                 serviceEvent(eventTuned);
49                 
50                 if (m_demux)
51                 {
52                         eDebug("ok ... now we start!!");
53
54                         if (!m_service || m_service->usePMT())
55                         {
56                                 if (m_pmt_pid == -1)
57                                         m_PAT.begin(eApp, eDVBPATSpec(), m_demux);
58                                 else
59                                         m_PMT.begin(eApp, eDVBPMTSpec(m_pmt_pid, m_reference.getServiceID().get()), m_demux);
60                         }
61
62                         if ( m_service && !m_service->cacheEmpty() )
63                                 serviceEvent(eventNewProgramInfo);
64                 }
65         } else if ((m_last_channel_state != iDVBChannel::state_failed) && 
66                         (state == iDVBChannel::state_failed))
67         {
68                 eDebug("tune failed.");
69                 serviceEvent(eventTuneFailed);
70         }
71 }
72
73 void eDVBServicePMTHandler::channelEvent(iDVBChannel *channel, int event)
74 {
75         switch (event)
76         {
77         case iDVBChannel::evtEOF:
78                 serviceEvent(eventEOF);
79                 break;
80         case iDVBChannel::evtSOF:
81                 serviceEvent(eventSOF);
82                 break;
83         default:
84                 break;
85         }
86 }
87
88 void eDVBServicePMTHandler::PMTready(int error)
89 {
90         if (error)
91                 serviceEvent(eventNoPMT);
92         else
93         {
94                 m_have_cached_program = false;
95                 serviceEvent(eventNewProgramInfo);
96                 if (!m_pvr_channel) // don't send campmt to camd.socket for playbacked services
97                 {
98                         eEPGCache::getInstance()->PMTready(this);
99                         if(!m_ca_servicePtr)
100                         {
101                                 int demuxes[2] = {0,0};
102                                 uint8_t tmp;
103                                 m_demux->getCADemuxID(tmp);
104                                 demuxes[0]=tmp;
105                                 if (m_decode_demux_num != 0xFF)
106                                         demuxes[1]=m_decode_demux_num;
107                                 else
108                                         demuxes[1]=demuxes[0];
109                                 eDVBCAService::register_service(m_reference, demuxes, m_ca_servicePtr);
110                                 eDVBCIInterfaces::getInstance()->recheckPMTHandlers();
111                         }
112                         eDVBCIInterfaces::getInstance()->gotPMT(this);
113                 }
114                 if (m_ca_servicePtr)
115                 {
116                         ePtr<eTable<ProgramMapSection> > ptr;
117                         if (!m_PMT.getCurrent(ptr))
118                                 m_ca_servicePtr->buildCAPMT(ptr);
119                         else
120                                 eDebug("eDVBServicePMTHandler cannot call buildCAPMT");
121                 }
122         }
123 }
124
125 void eDVBServicePMTHandler::PATready(int)
126 {
127         ePtr<eTable<ProgramAssociationSection> > ptr;
128         if (!m_PAT.getCurrent(ptr))
129         {
130                 int pmtpid = -1;
131                 std::vector<ProgramAssociationSection*>::const_iterator i;
132                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
133                 {
134                         const ProgramAssociationSection &pat = **i;
135                         ProgramAssociationConstIterator program;
136                         for (program = pat.getPrograms()->begin(); program != pat.getPrograms()->end(); ++program)
137                                 if (eServiceID((*program)->getProgramNumber()) == m_reference.getServiceID())
138                                         pmtpid = (*program)->getProgramMapPid();
139                 }
140                 if (pmtpid == -1)
141                         serviceEvent(eventNoPATEntry);
142                 else
143                         m_PMT.begin(eApp, eDVBPMTSpec(pmtpid, m_reference.getServiceID().get()), m_demux);
144         } else
145                 serviceEvent(eventNoPAT);
146 }
147
148 PyObject *eDVBServicePMTHandler::getCaIds()
149 {
150         ePyObject ret;
151
152         program prog;
153
154         if ( !getProgramInfo(prog) )
155         {
156                 int cnt=prog.caids.size();
157                 if (cnt)
158                 {
159                         ret=PyList_New(cnt);
160                         std::set<uint16_t>::iterator it(prog.caids.begin());
161                         while(cnt--)
162                                 PyList_SET_ITEM(ret, cnt, PyInt_FromLong(*it++));
163                 }
164         }
165
166         return ret ? (PyObject*)ret : (PyObject*)PyList_New(0);
167 }
168
169 int eDVBServicePMTHandler::getProgramInfo(struct program &program)
170 {
171         ePtr<eTable<ProgramMapSection> > ptr;
172         int cached_apid_ac3 = -1;
173         int cached_apid_mpeg = -1;
174         int cached_vpid = -1;
175         int cached_tpid = -1;
176         int ret = -1;
177
178         program.videoStreams.clear();
179         program.audioStreams.clear();
180         program.pcrPid = -1;
181         program.pmtPid = -1;
182         program.textPid = -1;
183
184         int first_ac3 = -1;
185         program.defaultAudioStream = 0;
186
187         if ( m_service && !m_service->cacheEmpty() )
188         {
189                 cached_vpid = m_service->getCacheEntry(eDVBService::cVPID);
190                 cached_apid_mpeg = m_service->getCacheEntry(eDVBService::cAPID);
191                 cached_apid_ac3 = m_service->getCacheEntry(eDVBService::cAC3PID);
192                 cached_tpid = m_service->getCacheEntry(eDVBService::cTPID);
193         }
194
195         if ( ((m_service && m_service->usePMT()) || !m_service) && !m_PMT.getCurrent(ptr))
196         {
197                 if (m_have_cached_program)
198                 {
199                         program = m_cached_program;
200                         ret = 0;
201                 }
202                 else
203                 {
204                         eDVBTableSpec table_spec;
205                         ptr->getSpec(table_spec);
206                         program.pmtPid = table_spec.pid < 0x1fff ? table_spec.pid : -1;
207                         std::vector<ProgramMapSection*>::const_iterator i;
208                         for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
209                         {
210                                 const ProgramMapSection &pmt = **i;
211                                 program.pcrPid = pmt.getPcrPid();
212                         
213                                 ElementaryStreamInfoConstIterator es;
214                                 for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
215                                 {
216                                         int isaudio = 0, isvideo = 0;
217                                         videoStream video;
218                                         audioStream audio;
219                                         audio.component_tag=video.component_tag=-1;
220                                         video.type = videoStream::vtMPEG2;
221                                         audio.type = audioStream::atMPEG;
222
223                                         switch ((*es)->getType())
224                                         {
225                                         case 0x1b: // AVC Video Stream (MPEG4 H264)
226                                                 video.type = videoStream::vtMPEG4_H264;
227                                                 isvideo = 1;
228                                                 //break; fall through !!!
229                                         case 0x10: // MPEG 4 Part 2
230                                                 if (!isvideo)
231                                                 {
232                                                         video.type = videoStream::vtMPEG4_Part2;
233                                                         isvideo = 1;
234                                                 }
235                                                 //break; fall through !!!
236                                         case 0x01: // MPEG 1 video
237                                                 if (!isvideo)
238                                                         video.type = videoStream::vtMPEG1;
239                                                 //break; fall through !!!
240                                         case 0x02: // MPEG 2 video
241                                                 isvideo = 1;
242                                                 //break; fall through !!!
243                                         case 0x03: // MPEG 1 audio
244                                         case 0x04: // MPEG 2 audio:
245                                                 if (!isvideo)
246                                                         isaudio = 1;
247                                                 //break; fall through !!!
248                                         case 0x0f: // MPEG 2 AAC
249                                                 if (!isvideo && !isaudio)
250                                                 {
251                                                         isaudio = 1;
252                                                         audio.type = audioStream::atAAC;
253                                                 }
254                                                 //break; fall through !!!
255                                         case 0x11: // MPEG 4 AAC
256                                                 if (!isvideo && !isaudio)
257                                                 {
258                                                         isaudio = 1;
259                                                         audio.type = audioStream::atAACHE;
260                                                 }
261                                         case 0x06: // PES Private
262                                         case 0x81: // user private
263                                         case 0xEA: // TS_PSI_ST_SMPTE_VC1
264                                                 for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
265                                                                 desc != (*es)->getDescriptors()->end(); ++desc)
266                                                 {
267                                                         uint8_t tag = (*desc)->getTag();
268                                                         if (!isaudio && !isvideo)
269                                                         {
270                                                                 /* PES private can contain AC-3, DTS or lots of other stuff.
271                                                                    check descriptors to get the exakt type. */
272                                                                 switch (tag)
273                                                                 {
274                                                                 case AUDIO_STREAM_DESCRIPTOR:
275                                                                         isaudio = 1;
276                                                                         break;
277                                                                 case VIDEO_STREAM_DESCRIPTOR:
278                                                                 {
279                                                                         isvideo = 1;
280                                                                         VideoStreamDescriptor *d = (VideoStreamDescriptor*)(*desc);
281                                                                         if (d->getMpeg1OnlyFlag())
282                                                                                 video.type = videoStream::vtMPEG1;
283                                                                         break;
284                                                                 }
285                                                                 case SUBTITLING_DESCRIPTOR:
286                                                                 {
287                                                                         SubtitlingDescriptor *d = (SubtitlingDescriptor*)(*desc);
288                                                                         const SubtitlingList *list = d->getSubtitlings();
289                                                                         subtitleStream s;
290                                                                         s.pid = (*es)->getPid();
291                                                                         for (SubtitlingConstIterator it(list->begin()); it != list->end(); ++it)
292                                                                         {
293                                                                                 s.subtitling_type = (*it)->getSubtitlingType();
294                                                                                 switch(s.subtitling_type)
295                                                                                 {
296                                                                                 case 0x10 ... 0x13:
297                                                                                 case 0x20 ... 0x23: // dvb subtitles
298                                                                                         break;
299                                                                                 default:
300                                                                                         eDebug("dvb subtitle %s PID %04x with wrong subtitling type (%02x)... force 0x10!!",
301                                                                                                 s.language_code.c_str(), s.pid, s.subtitling_type);
302                                                                                         s.subtitling_type = 0x10;
303                                                                                         break;
304                                                                                 }
305                                                                                 s.composition_page_id = (*it)->getCompositionPageId();
306                                                                                 s.ancillary_page_id = (*it)->getAncillaryPageId();
307                                                                                 s.language_code = (*it)->getIso639LanguageCode();
308 //                                                                              eDebug("add dvb subtitle %s PID %04x, type %d, composition page %d, ancillary_page %d",
309 //                                                                                      s.language_code.c_str(), s.pid, s.subtitling_type, s.composition_page_id, s.ancillary_page_id);
310                                                                                 program.subtitleStreams.push_back(s);
311                                                                         }
312                                                                         break;
313                                                                 }
314                                                                 case TELETEXT_DESCRIPTOR:
315                                                                         if ( program.textPid == -1 || (*es)->getPid() == cached_tpid )
316                                                                         {
317                                                                                 subtitleStream s;
318                                                                                 s.subtitling_type = 0x01; // EBU TELETEXT SUBTITLES
319                                                                                 s.pid = program.textPid = (*es)->getPid();
320                                                                                 TeletextDescriptor *d = (TeletextDescriptor*)(*desc);
321                                                                                 const VbiTeletextList *list = d->getVbiTeletexts();
322                                                                                 for (VbiTeletextConstIterator it(list->begin()); it != list->end(); ++it)
323                                                                                 {
324                                                                                         switch((*it)->getTeletextType())
325                                                                                         {
326                                                                                         case 0x02: // Teletext subtitle page
327                                                                                         case 0x05: // Teletext subtitle page for hearing impaired pepople
328                                                                                                 s.language_code = (*it)->getIso639LanguageCode();
329                                                                                                 s.teletext_page_number = (*it)->getTeletextPageNumber();
330                                                                                                 s.teletext_magazine_number = (*it)->getTeletextMagazineNumber();
331 //                                                                                              eDebug("add teletext subtitle %s PID %04x, page number %d, magazine number %d",
332 //                                                                                                      s.language_code.c_str(), s.pid, s.teletext_page_number, s.teletext_magazine_number);
333                                                                                                 program.subtitleStreams.push_back(s);
334                                                                                                 break;
335                                                                                         }
336                                                                                 }
337                                                                         }
338                                                                         break;
339                                                                 case DTS_DESCRIPTOR:
340                                                                         isaudio = 1;
341                                                                         audio.type = audioStream::atDTS;
342                                                                         break;
343                                                                 case 0x2B: // TS_PSI_DT_MPEG2_AAC
344                                                                         isaudio = 1;
345                                                                         audio.type = audioStream::atAAC; // MPEG2-AAC
346                                                                         break;
347                                                                 case 0x1C: // TS_PSI_DT_MPEG4_Audio
348                                                                 case AAC_DESCRIPTOR:
349                                                                         isaudio = 1;
350                                                                         audio.type = audioStream::atAACHE; // MPEG4-AAC
351                                                                         break;
352                                                                 case AC3_DESCRIPTOR:
353                                                                         isaudio = 1;
354                                                                         audio.type = audioStream::atAC3;
355                                                                         break;
356                                                                 case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
357                                                                 {
358                                                                                 /* libdvbsi++ doesn't yet support this descriptor type, so work around. */
359                                                                         if ((*desc)->getLength() < 4)
360                                                                                 break;
361                                                                         unsigned char descr[6];
362                                                                         (*desc)->writeToBuffer(descr);
363                                                                         int format_identifier = (descr[2] << 24) | (descr[3] << 16) | (descr[4] << 8) | (descr[5]);
364                                                                         switch (format_identifier)
365                                                                         {
366                                                                         case 0x41432d33:
367                                                                                 isaudio = 1;
368                                                                                 audio.type = audioStream::atAC3;
369                                                                                 break;
370                                                                         case 0x56432d31:
371                                                                                 if (descr[6] == 0x01) // subdescriptor tag
372                                                                                 {
373                                                                                         if (descr[7] >= 0x90) // profile_level
374                                                                                                 video.type = videoStream::vtVC1; // advanced profile
375                                                                                         else
376                                                                                                 video.type = videoStream::vtVC1_SM; // simple main
377                                                                                         isvideo = 1;
378                                                                                 }
379                                                                                 break;
380                                                                         default:
381                                                                                 break;
382                                                                         }
383                                                                         break;
384                                                                 }
385                                                                 case 0x28: // TS_PSI_DT_AVC
386                                                                         isvideo = 1;
387                                                                         video.type = videoStream::vtMPEG4_H264;
388                                                                         break;
389                                                                 case 0x1B: // TS_PSI_DT_MPEG4_Video
390                                                                         isvideo = 1;
391                                                                         video.type = videoStream::vtMPEG4_Part2;
392                                                                         break;
393                                                                 default:
394                                                                         break;
395                                                                 }
396                                                         }
397                                                         switch (tag)
398                                                         {
399                                                                 case ISO_639_LANGUAGE_DESCRIPTOR:
400                                                                         if (!isvideo)
401                                                                         {
402                                                                                 int cnt=0;
403                                                                                 const Iso639LanguageList *languages = ((Iso639LanguageDescriptor*)*desc)->getIso639Languages();
404                                                                                         /* use last language code */
405                                                                                 for (Iso639LanguageConstIterator i(languages->begin()); i != languages->end(); ++i, ++cnt)
406                                                                                 {
407                                                                                         if (cnt == 0)
408                                                                                                 audio.language_code = (*i)->getIso639LanguageCode();
409                                                                                         else
410                                                                                                 audio.language_code += "/" + (*i)->getIso639LanguageCode();
411                                                                                 }
412                                                                         }
413                                                                         break;
414                                                                 case STREAM_IDENTIFIER_DESCRIPTOR:
415                                                                         audio.component_tag =
416                                                                                 video.component_tag =
417                                                                                         ((StreamIdentifierDescriptor*)*desc)->getComponentTag();
418                                                                         break;
419                                                                 case CA_DESCRIPTOR:
420                                                                 {
421                                                                         CaDescriptor *descr = (CaDescriptor*)(*desc);
422                                                                         program.caids.insert(descr->getCaSystemId());
423                                                                         break;
424                                                                 }
425                                                                 default:
426                                                                         break;
427                                                         }
428                                                 }
429                                                 break;
430                                         }
431                                         if (isaudio)
432                                         {
433                                                 audio.pid = (*es)->getPid();
434
435                                                         /* if we find the cached pids, this will be our default stream */
436                                                 if (audio.pid == cached_apid_ac3 || audio.pid == cached_apid_mpeg)
437                                                         program.defaultAudioStream = program.audioStreams.size();
438
439                                                         /* also, we need to know the first non-mpeg (i.e. "ac3"/dts/...) stream */
440                                                 if ((audio.type != audioStream::atMPEG) && ((first_ac3 == -1) || (audio.pid == cached_apid_ac3)))
441                                                         first_ac3 = program.audioStreams.size();
442
443                                                 program.audioStreams.push_back(audio);
444                                         }
445                                         else if (isvideo)
446                                         {
447                                                 video.pid = (*es)->getPid();
448                                                 if ( !program.videoStreams.empty() && video.pid == cached_vpid )
449                                                 {
450                                                         program.videoStreams.push_back(program.videoStreams[0]);
451                                                         program.videoStreams[0] = video;
452                                                 }
453                                                 else
454                                                         program.videoStreams.push_back(video);
455                                         }
456                                         else
457                                                 continue;
458                                 }
459                                 for (DescriptorConstIterator desc = pmt.getDescriptors()->begin();
460                                         desc != pmt.getDescriptors()->end(); ++desc)
461                                 {
462                                         if ((*desc)->getTag() == CA_DESCRIPTOR)
463                                         {
464                                                 CaDescriptor *descr = (CaDescriptor*)(*desc);
465                                                 program.caids.insert(descr->getCaSystemId());
466                                         }
467                                 }
468                         }
469                         ret = 0;
470
471
472                         /* finally some fixup: if our default audio stream is an MPEG audio stream, 
473                            and we have 'defaultac3' set, use the first available ac3 stream instead.
474                            (note: if an ac3 audio stream was selected before, this will be also stored
475                            in 'fisrt_ac3', so we don't need to worry. */
476                         bool defaultac3 = false;
477                         std::string default_ac3;
478
479                         if (!ePythonConfigQuery::getConfigValue("config.av.defaultac3", default_ac3))
480                                 defaultac3 = default_ac3 == "True";
481
482                         if (defaultac3 && (first_ac3 != -1))
483                                 program.defaultAudioStream = first_ac3;
484
485                         m_cached_program = program;
486                         m_have_cached_program = true;
487                 }
488         } else if ( m_service && !m_service->cacheEmpty() )
489         {
490                 int cached_pcrpid = m_service->getCacheEntry(eDVBService::cPCRPID),
491                         vpidtype = m_service->getCacheEntry(eDVBService::cVTYPE),
492                         cnt=0;
493                 if ( vpidtype == -1 )
494                         vpidtype = videoStream::vtMPEG2;
495                 if ( cached_vpid != -1 )
496                 {
497                         videoStream s;
498                         s.pid = cached_vpid;
499                         s.type = vpidtype;
500                         program.videoStreams.push_back(s);
501                         ++cnt;
502                 }
503                 if ( cached_apid_ac3 != -1 )
504                 {
505                         audioStream s;
506                         s.type = audioStream::atAC3;
507                         s.pid = cached_apid_ac3;
508                         program.audioStreams.push_back(s);
509                         ++cnt;
510                 }
511                 if ( cached_apid_mpeg != -1 )
512                 {
513                         audioStream s;
514                         s.type = audioStream::atMPEG;
515                         s.pid = cached_apid_mpeg;
516                         program.audioStreams.push_back(s);
517                         ++cnt;
518                 }
519                 if ( cached_pcrpid != -1 )
520                 {
521                         ++cnt;
522                         program.pcrPid = cached_pcrpid;
523                 }
524                 if ( cached_tpid != -1 )
525                 {
526                         ++cnt;
527                         program.textPid = cached_tpid;
528                 }
529                 CAID_LIST &caids = m_service->m_ca;
530                 for (CAID_LIST::iterator it(caids.begin()); it != caids.end(); ++it)
531                         program.caids.insert(*it);
532                 if ( cnt )
533                         ret = 0;
534         }
535         return ret;
536 }
537
538 int eDVBServicePMTHandler::getChannel(eUsePtr<iDVBChannel> &channel)
539 {
540         channel = m_channel;
541         if (channel)
542                 return 0;
543         else
544                 return -1;
545 }
546
547 int eDVBServicePMTHandler::getDataDemux(ePtr<iDVBDemux> &demux)
548 {
549         demux = m_demux;
550         if (demux)
551                 return 0;
552         else
553                 return -1;
554 }
555
556 int eDVBServicePMTHandler::getDecodeDemux(ePtr<iDVBDemux> &demux)
557 {
558         int ret=0;
559                 /* if we're using the decoding demux as data source
560                    (for example in pvr playbacks), return that one. */
561         if (m_use_decode_demux)
562         {
563                 demux = m_demux;
564                 return ret;
565         }
566         
567         ASSERT(m_channel); /* calling without a previous ::tune is certainly bad. */
568
569         ret = m_channel->getDemux(demux, iDVBChannel::capDecode);
570         if (!ret)
571                 demux->getCADemuxID(m_decode_demux_num);
572
573         return ret;
574 }
575
576 int eDVBServicePMTHandler::getPVRChannel(ePtr<iDVBPVRChannel> &pvr_channel)
577 {
578         pvr_channel = m_pvr_channel;
579         if (pvr_channel)
580                 return 0;
581         else
582                 return -1;
583 }
584
585 void eDVBServicePMTHandler::SDTScanEvent(int event)
586 {
587         switch (event)
588         {
589                 case eDVBScan::evtFinish:
590                 {
591                         ePtr<iDVBChannelList> db;
592                         if (m_resourceManager->getChannelList(db) != 0)
593                                 eDebug("no channel list");
594                         else
595                         {
596                                 eDVBChannelID chid;
597                                 m_reference.getChannelID(chid);
598                                 if (chid == m_dvb_scan->getCurrentChannelID())
599                                 {
600                                         m_dvb_scan->insertInto(db, true);
601                                         eDebug("sdt update done!");
602                                 }
603                                 else
604                                         eDebug("ignore sdt update data.... incorrect transponder tuned!!!");
605                         }
606                         break;
607                 }
608
609                 default:
610                         break;
611         }
612 }
613
614 int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux, eCueSheet *cue, bool simulate, eDVBService *service)
615 {
616         RESULT res=0;
617         m_reference = ref;
618         
619         m_use_decode_demux = use_decode_demux;
620
621                 /* 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 */
622         m_service = service;
623         
624                 /* is this a normal (non PVR) channel? */
625         if (ref.path.empty())
626         {
627                 eDVBChannelID chid;
628                 ref.getChannelID(chid);
629                 res = m_resourceManager->allocateChannel(chid, m_channel, simulate);
630                 if (!simulate)
631                         eDebug("allocate Channel: res %d", res);
632
633                 ePtr<iDVBChannelList> db;
634                 if (!m_resourceManager->getChannelList(db))
635                         db->getService((eServiceReferenceDVB&)m_reference, m_service);
636
637                 if (!res && !simulate)
638                         eDVBCIInterfaces::getInstance()->addPMTHandler(this);
639         } else if (!simulate) // no simulation of playback services
640         {
641                 eDVBMetaParser parser;
642
643                 int ret=parser.parseFile(ref.path);
644                 if (ret || !parser.m_ref.getServiceID().get() /* incorrect sid in meta file or recordings.epl*/ )
645                 {
646                         eWarning("no .meta file found, trying to find PMT pid");
647                         eDVBTSTools tstools;
648                         if (tstools.openFile(ref.path.c_str()))
649                                 eWarning("failed to open file");
650                         else
651                         {
652                                 int service_id, pmt_pid;
653                                 if (!tstools.findPMT(pmt_pid, service_id))
654                                 {
655                                         eDebug("PMT pid found on pid %04x, service id %d", pmt_pid, service_id);
656                                         m_reference.setServiceID(service_id);
657                                         m_pmt_pid = pmt_pid;
658                                 }
659                         }
660                 } else
661                         m_reference = parser.m_ref;
662                 
663                 eDebug("alloc PVR");
664                         /* allocate PVR */
665                 res = m_resourceManager->allocatePVRChannel(m_pvr_channel);
666                 if (res)
667                         eDebug("allocatePVRChannel failed!\n");
668                 m_channel = m_pvr_channel;
669         }
670
671         if (!simulate)
672         {
673                 if (m_channel)
674                 {
675                         m_channel->connectStateChange(
676                                 slot(*this, &eDVBServicePMTHandler::channelStateChanged), 
677                                 m_channelStateChanged_connection);
678                         m_last_channel_state = -1;
679                         channelStateChanged(m_channel);
680         
681                         m_channel->connectEvent(
682                                 slot(*this, &eDVBServicePMTHandler::channelEvent), 
683                                 m_channelEvent_connection);
684
685                         if (ref.path.empty())
686                         {
687                                 m_dvb_scan = 0;
688                                 m_dvb_scan = new eDVBScan(m_channel, true, false);
689                                 m_dvb_scan->connectEvent(slot(*this, &eDVBServicePMTHandler::SDTScanEvent), m_scan_event_connection);
690                         }
691                 } else
692                 {
693                         if (res == eDVBResourceManager::errAllSourcesBusy)
694                                 serviceEvent(eventNoResources);
695                         else /* errChidNotFound, errNoChannelList, errChannelNotInList, errNoSourceFound */
696                                 serviceEvent(eventMisconfiguration);
697                         return res;
698                 }
699
700                 if (m_pvr_channel)
701                 {
702                         m_pvr_channel->setCueSheet(cue);
703                         m_pvr_channel->playFile(ref.path.c_str());
704                 }
705         }
706
707         return res;
708 }
709
710 void eDVBServicePMTHandler::free()
711 {
712         m_dvb_scan = 0;
713
714         if (m_ca_servicePtr)
715         {
716                 int demuxes[2] = {0,0};
717                 uint8_t tmp;
718                 m_demux->getCADemuxID(tmp);
719                 demuxes[0]=tmp;
720                 if (m_decode_demux_num != 0xFF)
721                         demuxes[1]=m_decode_demux_num;
722                 else
723                         demuxes[1]=demuxes[0];
724                 ePtr<eTable<ProgramMapSection> > ptr;
725                 m_PMT.getCurrent(ptr);
726                 eDVBCAService::unregister_service(m_reference, demuxes, ptr);
727                 m_ca_servicePtr = 0;
728         }
729
730         if (m_channel)
731                 eDVBCIInterfaces::getInstance()->removePMTHandler(this);
732
733         if (m_pvr_channel)
734         {
735                 m_pvr_channel->stopFile();
736                 m_pvr_channel->setCueSheet(0);
737         }
738
739         m_PMT.stop();
740         m_PAT.stop();
741         m_service = 0;
742         m_channel = 0;
743         m_pvr_channel = 0;
744         m_demux = 0;
745 }
746
747 CAServiceMap eDVBCAService::exist;
748 ChannelMap eDVBCAService::exist_channels;
749 ePtr<eConnection> eDVBCAService::m_chanAddedConn;
750
751 eDVBCAService::eDVBCAService()
752         :m_buffer(512), m_prev_build_hash(0), m_sendstate(0), m_retryTimer(eTimer::create(eApp))
753 {
754         memset(m_used_demux, 0xFF, sizeof(m_used_demux));
755         CONNECT(m_retryTimer->timeout, eDVBCAService::sendCAPMT);
756         Connect();
757 }
758
759 eDVBCAService::~eDVBCAService()
760 {
761         eDebug("[eDVBCAService] free service %s", m_service.toString().c_str());
762         ::close(m_sock);
763 }
764
765 // begin static methods
766 RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int demux_nums[2], eDVBCAService *&caservice )
767 {
768         CAServiceMap::iterator it = exist.find(ref);
769         if ( it != exist.end() )
770                 caservice = it->second;
771         else
772         {
773                 caservice = (exist[ref]=new eDVBCAService());
774                 caservice->m_service = ref;
775                 eDebug("[eDVBCAService] new service %s", ref.toString().c_str() );
776         }
777
778         int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
779         for (int i=0; i < loops; ++i)
780         {
781 // search free demux entry
782                 int iter=0, max_demux_slots = sizeof(caservice->m_used_demux);
783
784                 while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
785                         ++iter;
786
787                 if ( iter < max_demux_slots )
788                 {
789                         caservice->m_used_demux[iter] = demux_nums[i] & 0xFF;
790                         eDebug("[eDVBCAService] add demux %d to slot %d service %s", caservice->m_used_demux[iter], iter, ref.toString().c_str());
791                 }
792                 else
793                 {
794                         eDebug("[eDVBCAService] no more demux slots free for service %s!!", ref.toString().c_str());
795                         return -1;
796                 }
797         }
798         return 0;
799 }
800
801 RESULT eDVBCAService::unregister_service( const eServiceReferenceDVB &ref, int demux_nums[2], eTable<ProgramMapSection> *ptr )
802 {
803         CAServiceMap::iterator it = exist.find(ref);
804         if ( it == exist.end() )
805         {
806                 eDebug("[eDVBCAService] try to unregister non registered %s", ref.toString().c_str());
807                 return -1;
808         }
809         else
810         {
811                 eDVBCAService *caservice = it->second;
812                 int loops = demux_nums[0] != demux_nums[1] ? 2 : 1;
813                 for (int i=0; i < loops; ++i)
814                 {
815                         bool freed = false;
816                         int iter = 0,
817                                 used_demux_slots = 0,
818                                 max_demux_slots = sizeof(caservice->m_used_demux)/sizeof(int);
819                         while ( iter < max_demux_slots )
820                         {
821                                 if ( caservice->m_used_demux[iter] != 0xFF )
822                                 {
823                                         if ( !freed && caservice->m_used_demux[iter] == demux_nums[i] )
824                                         {
825                                                 eDebug("[eDVBCAService] free slot %d demux %d for service %s", iter, caservice->m_used_demux[iter], caservice->m_service.toString().c_str() );
826                                                 caservice->m_used_demux[iter] = 0xFF;
827                                                 freed=true;
828                                         }
829                                         else
830                                                 ++used_demux_slots;
831                                 }
832                                 ++iter;
833                         }
834                         if (!freed)
835                                 eDebug("[eDVBCAService] couldn't free demux slot for demux %d", demux_nums[i]);
836                         if (i || loops == 1)
837                         {
838                                 if (!used_demux_slots)  // no more used.. so we remove it
839                                 {
840                                         delete it->second;
841                                         exist.erase(it);
842                                 }
843                                 else
844                                 {
845                                         if (ptr)
846                                                 it->second->buildCAPMT(ptr);
847                                         else
848                                                 eDebug("[eDVBCAService] can not send updated demux info");
849                                 }
850                         }
851                 }
852         }
853         return 0;
854 }
855
856 void eDVBCAService::registerChannelCallback(eDVBResourceManager *res_mgr)
857 {
858         res_mgr->connectChannelAdded(slot(&DVBChannelAdded), m_chanAddedConn);
859 }
860
861 void eDVBCAService::DVBChannelAdded(eDVBChannel *chan)
862 {
863         if ( chan )
864         {
865                 eDebug("[eDVBCAService] new channel %p!", chan);
866                 channel_data *data = new channel_data();
867                 data->m_channel = chan;
868                 data->m_prevChannelState = -1;
869                 data->m_dataDemux = -1;
870                 exist_channels[chan] = data;
871                 chan->connectStateChange(slot(&DVBChannelStateChanged), data->m_stateChangedConn);
872         }
873 }
874
875 void eDVBCAService::DVBChannelStateChanged(iDVBChannel *chan)
876 {
877         ChannelMap::iterator it =
878                 exist_channels.find(chan);
879         if ( it != exist_channels.end() )
880         {
881                 int state=0;
882                 chan->getState(state);
883                 if ( it->second->m_prevChannelState != state )
884                 {
885                         switch (state)
886                         {
887                                 case iDVBChannel::state_ok:
888                                 {
889                                         eDebug("[eDVBCAService] channel %p running", chan);
890                                         break;
891                                 }
892                                 case iDVBChannel::state_release:
893                                 {
894                                         eDebug("[eDVBCAService] remove channel %p", chan);
895                                         unsigned char msg[8] = { 0x9f,0x80,0x3f,0x04,0x83,0x02,0x00,0x00 };
896                                         msg[7] = it->second->m_dataDemux & 0xFF;
897                                         int sock, clilen;
898                                         struct sockaddr_un servaddr;
899                                         memset(&servaddr, 0, sizeof(struct sockaddr_un));
900                                         servaddr.sun_family = AF_UNIX;
901                                         strcpy(servaddr.sun_path, "/tmp/camd.socket");
902                                         clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
903                                         sock = socket(PF_UNIX, SOCK_STREAM, 0);
904                                         if (sock > -1)
905                                         {
906                                                 connect(sock, (struct sockaddr *) &servaddr, clilen);
907                                                 fcntl(sock, F_SETFL, O_NONBLOCK);
908                                                 int val=1;
909                                                 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
910                                                 if (write(sock, msg, 8) != 8)
911                                                         eDebug("[eDVBCAService] write leave transponder failed!!");
912                                                 close(sock);
913                                         }
914                                         exist_channels.erase(it);
915                                         delete it->second;
916                                         it->second=0;
917                                         break;
918                                 }
919                                 default: // ignore all other events
920                                         return;
921                         }
922                         if (it->second)
923                                 it->second->m_prevChannelState = state;
924                 }
925         }
926 }
927
928 channel_data *eDVBCAService::getChannelData(eDVBChannelID &chid)
929 {
930         for (ChannelMap::iterator it(exist_channels.begin()); it != exist_channels.end(); ++it)
931         {
932                 if (chid == it->second->m_channel->getChannelID())
933                         return it->second;
934         }
935         return 0;
936 }
937 // end static methods
938
939 #define CA_REPLY_DEBUG
940 #define MAX_LENGTH_BYTES 4
941 #define MIN_LENGTH_BYTES 1
942
943 void eDVBCAService::socketCB(int what)
944 {
945         if (what & (eSocketNotifier::Read | eSocketNotifier::Priority))
946         {
947                 char msgbuffer[4096];
948                 ssize_t length = read(m_sock, msgbuffer, sizeof(msgbuffer));
949                 if (length == -1)
950                 {
951                         if (errno != EAGAIN && errno != EINTR && errno != EBUSY)
952                         {
953                                 eDebug("[eSocketMMIHandler] read (%m)");
954                                 what |= eSocketNotifier::Error;
955                         }
956                 } else if (length == 0)
957                 {
958                         what |= eSocketNotifier::Hungup;
959                 } else
960                 {
961                         int len = length;
962                         unsigned char *data = (unsigned char*)msgbuffer;
963                         int clear = 1;
964         // If a new message starts, then the previous message
965         // should already have been processed. Otherwise the
966         // previous message was incomplete and should therefore
967         // be deleted.
968                         if ((len >= 1) && ((data[0] & 0xFF) != 0x9f))
969                                 clear = 0;
970                         if ((len >= 2) && ((data[1] & 0x80) != 0x80))
971                                 clear = 0;
972                         if ((len >= 3) && ((data[2] & 0x80) != 0x00))
973                                 clear = 0;
974                         if (clear)
975                         {
976                                 m_buffer.clear();
977 #ifdef CA_REPLY_DEBUG
978                                 eDebug("clear buffer");
979 #endif
980                         }
981 #ifdef CA_REPLY_DEBUG
982                         eDebug("Put to buffer:");
983                         for (int i=0; i < len; ++i)
984                                 eDebugNoNewLine("%02x ", data[i]);
985                         eDebug("\n--------");
986 #endif
987                         m_buffer.write( data, len );
988
989                         while ( m_buffer.size() >= (3 + MIN_LENGTH_BYTES) )
990                         {
991                                 unsigned char tmp[3+MAX_LENGTH_BYTES];
992                                 m_buffer.peek(tmp, 3+MIN_LENGTH_BYTES);
993                                 if (((tmp[0] & 0xFF) != 0x9f) || ((tmp[1] & 0x80) != 0x80) || ((tmp[2] & 0x80) != 0x00))
994                                 {
995                                         m_buffer.skip(1);
996 #ifdef CA_REPLY_DEBUG
997                                         eDebug("skip %02x", tmp[0]);
998 #endif
999                                         continue;
1000                                 }
1001                                 if (tmp[3] & 0x80)
1002                                 {
1003                                         int peekLength = (tmp[3] & 0x7f) + 4;
1004                                         if (m_buffer.size() < peekLength)
1005                                                 continue;
1006                                         m_buffer.peek(tmp, peekLength);
1007                                 }
1008                                 int size=0;
1009                                 int LengthBytes=eDVBCISession::parseLengthField(tmp+3, size);
1010                                 int messageLength = 3+LengthBytes+size;
1011                                 if ( m_buffer.size() >= messageLength )
1012                                 {
1013                                         unsigned char dest[messageLength];
1014                                         m_buffer.read(dest, messageLength);
1015 #ifdef CA_REPLY_DEBUG
1016                                         eDebug("dump ca reply:");
1017                                         for (int i=0; i < messageLength; ++i)
1018                                                 eDebugNoNewLine("%02x ", dest[i]);
1019                                         eDebug("\n--------");
1020 #endif
1021 //                                      /*emit*/ mmi_progress(0, dest, (const void*)(dest+3+LengthBytes), messageLength-3-LengthBytes);
1022                                 }
1023                         }
1024                 }
1025         }
1026         if (what & eSocketNotifier::Hungup) {
1027                 /*eDebug("[eDVBCAService] connection closed")*/;
1028                 m_sendstate=1;
1029                 sendCAPMT();
1030         }
1031         if (what & eSocketNotifier::Error)
1032                 eDebug("[eDVBCAService] connection error");
1033 }
1034
1035 void eDVBCAService::Connect()
1036 {
1037         m_sn=0;
1038         memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
1039         m_servaddr.sun_family = AF_UNIX;
1040         strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
1041         m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
1042         m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
1043         if (m_sock != -1)
1044         {
1045                 if (!connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen))
1046                 {
1047                         int val=1;
1048                         fcntl(m_sock, F_SETFL, O_NONBLOCK);
1049                         setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
1050                         m_sn = eSocketNotifier::create(eApp, m_sock,
1051                                 eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Error|eSocketNotifier::Hungup);
1052                         CONNECT(m_sn->activated, eDVBCAService::socketCB);
1053                         
1054                 }
1055 //              else
1056 //                      eDebug("[eDVBCAService] connect failed %m");
1057         }
1058         else
1059                 eDebug("[eDVBCAService] create socket failed %m");
1060 }
1061
1062 void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
1063 {
1064         if (!ptr)
1065                 return;
1066
1067         eDVBTableSpec table_spec;
1068         ptr->getSpec(table_spec);
1069
1070         int pmtpid = table_spec.pid,
1071                 pmt_version = table_spec.version;
1072
1073         uint8_t demux_mask = 0;
1074         int data_demux = -1;
1075
1076         int iter=0, max_demux_slots = sizeof(m_used_demux);
1077         while ( iter < max_demux_slots )
1078         {
1079                 if ( m_used_demux[iter] != 0xFF )
1080                 {
1081                         if ( m_used_demux[iter] > data_demux )
1082                                 data_demux = m_used_demux[iter];
1083                         demux_mask |= (1 << m_used_demux[iter]);
1084                 }
1085                 ++iter;
1086         }
1087
1088         if ( data_demux == -1 )
1089         {
1090                 eDebug("[eDVBCAService] no data demux found for service %s", m_service.toString().c_str() );
1091                 return;
1092         }
1093
1094         eDebug("demux %d mask %02x prevhash %08x", data_demux, demux_mask, m_prev_build_hash);
1095
1096         unsigned int build_hash = ( pmtpid << 16);
1097         build_hash |= (demux_mask << 8);
1098         build_hash |= (pmt_version&0xFF);
1099
1100         if ( build_hash == m_prev_build_hash )
1101         {
1102                 eDebug("[eDVBCAService] don't build/send the same CA PMT twice");
1103                 return;
1104         }
1105
1106         std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
1107         if ( i != ptr->getSections().end() )
1108         {
1109                 CaProgramMapSection capmt(*i++, m_prev_build_hash ? 0x05 /*update*/ : 0x03 /*only*/, 0x01 );
1110
1111                 while( i != ptr->getSections().end() )
1112                 {
1113 //                      eDebug("append");
1114                         capmt.append(*i++);
1115                 }
1116
1117                 // add our private descriptors to capmt
1118                 uint8_t tmp[10];
1119
1120                 tmp[0]=0x84;  // pmt pid
1121                 tmp[1]=0x02;
1122                 tmp[2]=pmtpid>>8;
1123                 tmp[3]=pmtpid&0xFF;
1124                 capmt.injectDescriptor(tmp, false);
1125
1126                 tmp[0] = 0x82; // demux
1127                 tmp[1] = 0x02;
1128                 tmp[2] = demux_mask;    // descramble bitmask
1129                 tmp[3] = data_demux&0xFF; // read section data from demux number
1130                 capmt.injectDescriptor(tmp, false);
1131
1132                 tmp[0] = 0x81; // dvbnamespace
1133                 tmp[1] = 0x08;
1134                 tmp[2] = m_service.getDVBNamespace().get()>>24;
1135                 tmp[3]=(m_service.getDVBNamespace().get()>>16)&0xFF;
1136                 tmp[4]=(m_service.getDVBNamespace().get()>>8)&0xFF;
1137                 tmp[5]=m_service.getDVBNamespace().get()&0xFF;
1138                 tmp[6]=m_service.getTransportStreamID().get()>>8;
1139                 tmp[7]=m_service.getTransportStreamID().get()&0xFF;
1140                 tmp[8]=m_service.getOriginalNetworkID().get()>>8;
1141                 tmp[9]=m_service.getOriginalNetworkID().get()&0xFF;
1142                 capmt.injectDescriptor(tmp, false);
1143
1144                 capmt.writeToBuffer(m_capmt);
1145         }
1146
1147         m_prev_build_hash = build_hash;
1148
1149         if ( m_sendstate != 0xFFFFFFFF )
1150                 m_sendstate=0;
1151         sendCAPMT();
1152 }
1153
1154 void eDVBCAService::sendCAPMT()
1155 {
1156         if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
1157         {
1158                 ::close(m_sock);
1159                 Connect();
1160         }
1161
1162         int wp=0;
1163         if ( m_capmt[3] & 0x80 )
1164         {
1165                 int i=0;
1166                 int lenbytes = m_capmt[3] & ~0x80;
1167                 while(i < lenbytes)
1168                         wp = (wp << 8) | m_capmt[4 + i++];
1169                 wp+=4;
1170                 wp+=lenbytes;
1171         }
1172         else
1173         {
1174                 wp = m_capmt[3];
1175                 wp+=4;
1176         }
1177
1178         if ( write(m_sock, m_capmt, wp) == wp )
1179         {
1180                 m_sendstate=0xFFFFFFFF;
1181                 eDebug("[eDVBCAService] send %d bytes",wp);
1182                 eDVBChannelID chid;
1183                 m_service.getChannelID(chid);
1184                 channel_data *data = getChannelData(chid);
1185                 if (data)
1186                 {
1187                         int lenbytes = m_capmt[3] & 0x80 ? m_capmt[3] & ~0x80 : 0;
1188                         data->m_dataDemux = m_capmt[24+lenbytes];
1189                 }
1190 #if 1
1191                 for(int i=0;i<wp;i++)
1192                         eDebugNoNewLine("%02x ", m_capmt[i]);
1193                 eDebug("");
1194 #endif
1195         }
1196         else
1197         {
1198                 switch(m_sendstate)
1199                 {
1200                         case 0xFFFFFFFF:
1201                                 ++m_sendstate;
1202                                 m_retryTimer->start(0,true);
1203 //                              eDebug("[eDVBCAService] send failed .. immediate retry");
1204                                 break;
1205                         default:
1206                                 m_retryTimer->start(5000,true);
1207 //                              eDebug("[eDVBCAService] send failed .. retry in 5 sec");
1208                                 break;
1209                 }
1210                 ++m_sendstate;
1211         }
1212 }
1213
1214 static PyObject *createTuple(int pid, const char *type)
1215 {
1216         PyObject *r = PyTuple_New(2);
1217         PyTuple_SET_ITEM(r, 0, PyInt_FromLong(pid));
1218         PyTuple_SET_ITEM(r, 1, PyString_FromString(type));
1219         return r;
1220 }
1221
1222 static inline void PyList_AppendSteal(PyObject *list, PyObject *item)
1223 {
1224         PyList_Append(list, item);
1225         Py_DECREF(item);
1226 }
1227
1228 extern void PutToDict(ePyObject &dict, const char*key, ePyObject item); // defined in dvb/frontend.cpp
1229
1230 PyObject *eDVBServicePMTHandler::program::createPythonObject()
1231 {
1232         ePyObject r = PyDict_New();
1233         ePyObject l = PyList_New(0);
1234
1235         PyList_AppendSteal(l, createTuple(0, "pat"));
1236
1237         if (pmtPid != -1)
1238                 PyList_AppendSteal(l, createTuple(pmtPid, "pmt"));
1239
1240         for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
1241                         i(videoStreams.begin()); 
1242                         i != videoStreams.end(); ++i)
1243                 PyList_AppendSteal(l, createTuple(i->pid, "video"));
1244
1245         for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
1246                         i(audioStreams.begin()); 
1247                         i != audioStreams.end(); ++i)
1248                 PyList_AppendSteal(l, createTuple(i->pid, "audio"));
1249
1250         for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
1251                         i(subtitleStreams.begin());
1252                         i != subtitleStreams.end(); ++i)
1253                 PyList_AppendSteal(l, createTuple(i->pid, "subtitle"));
1254
1255         PyList_AppendSteal(l, createTuple(pcrPid, "pcr"));
1256
1257         if (textPid != -1)
1258                 PyList_AppendSteal(l, createTuple(textPid, "text"));
1259
1260         PutToDict(r, "pids", l);
1261
1262         return r;
1263 }