add preStart event and use it to load the cutlist
[enigma2.git] / lib / dvb / pmt.cpp
index 8e007f8d5e955ee1247a456eb257474bc1487e2f..ee89a3a43a93a55133f838c2d1aa34e27fdc2798 100644 (file)
@@ -17,6 +17,7 @@
 #include <dvbsi++/subtitling_descriptor.h>
 #include <dvbsi++/teletext_descriptor.h>
 #include <dvbsi++/video_stream_descriptor.h>
+#include <dvbsi++/registration_descriptor.h>
 
 eDVBServicePMTHandler::eDVBServicePMTHandler()
        :m_ca_servicePtr(0), m_dvb_scan(0), m_decode_demux_num(0xFF)
@@ -74,6 +75,9 @@ void eDVBServicePMTHandler::channelEvent(iDVBChannel *channel, int event)
 {
        switch (event)
        {
+       case iDVBChannel::evtPreStart:
+               serviceEvent(eventPreStart);
+               break;
        case iDVBChannel::evtEOF:
                serviceEvent(eventEOF);
                break;
@@ -183,6 +187,7 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
 
        int first_ac3 = -1;
        program.defaultAudioStream = 0;
+       audioStream *prev_audio = 0;
 
        if ( m_service && !m_service->cacheEmpty() )
        {
@@ -213,14 +218,16 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                ElementaryStreamInfoConstIterator es;
                                for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
                                {
-                                       int isaudio = 0, isvideo = 0;
+                                       int isaudio = 0, isvideo = 0, issubtitle = 0, forced_video = 0, forced_audio = 0, isteletext = 0;
+                                       int streamtype = (*es)->getType();
                                        videoStream video;
                                        audioStream audio;
                                        audio.component_tag=video.component_tag=-1;
                                        video.type = videoStream::vtMPEG2;
                                        audio.type = audioStream::atMPEG;
+                                       audio.rdsPid = -1;
 
-                                       switch ((*es)->getType())
+                                       switch (streamtype)
                                        {
                                        case 0x1b: // AVC Video Stream (MPEG4 H264)
                                                video.type = videoStream::vtMPEG4_H264;
@@ -239,17 +246,21 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                                //break; fall through !!!
                                        case 0x02: // MPEG 2 video
                                                isvideo = 1;
+                                               forced_video = 1;
                                                //break; fall through !!!
                                        case 0x03: // MPEG 1 audio
                                        case 0x04: // MPEG 2 audio:
-                                               if (!isvideo)
+                                               if (!isvideo) {
                                                        isaudio = 1;
+                                                       forced_audio = 1;
+                                               }
                                                //break; fall through !!!
                                        case 0x0f: // MPEG 2 AAC
                                                if (!isvideo && !isaudio)
                                                {
                                                        isaudio = 1;
                                                        audio.type = audioStream::atAAC;
+                                                       forced_audio = 1;
                                                }
                                                //break; fall through !!!
                                        case 0x11: // MPEG 4 AAC
@@ -257,6 +268,7 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                                {
                                                        isaudio = 1;
                                                        audio.type = audioStream::atAACHE;
+                                                       forced_audio = 1;
                                                }
                                        case 0x80: // user private ... but blueray LPCM
                                                if (!isvideo && !isaudio)
@@ -279,140 +291,152 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                                }
                                        case 0x06: // PES Private
                                        case 0xEA: // TS_PSI_ST_SMPTE_VC1
+                                       {
+                                               int num_descriptors = 0;
                                                for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
-                                                               desc != (*es)->getDescriptors()->end(); ++desc)
+                                                       desc != (*es)->getDescriptors()->end(); ++desc)
                                                {
                                                        uint8_t tag = (*desc)->getTag();
                                                        /* check descriptors to get the exakt stream type. */
-                                                       switch (tag)
-                                                       {
-                                                       case AUDIO_STREAM_DESCRIPTOR:
-                                                               isaudio = 1;
-                                                               break;
-                                                       case VIDEO_STREAM_DESCRIPTOR:
-                                                       {
-                                                               isvideo = 1;
-                                                               VideoStreamDescriptor *d = (VideoStreamDescriptor*)(*desc);
-                                                               if (d->getMpeg1OnlyFlag())
-                                                                       video.type = videoStream::vtMPEG1;
-                                                               break;
-                                                       }
-                                                       case SUBTITLING_DESCRIPTOR:
+                                                       ++num_descriptors;
+                                                       if (!forced_video && !forced_audio)
                                                        {
-                                                               SubtitlingDescriptor *d = (SubtitlingDescriptor*)(*desc);
-                                                               const SubtitlingList *list = d->getSubtitlings();
-                                                               subtitleStream s;
-                                                               s.pid = (*es)->getPid();
-                                                               for (SubtitlingConstIterator it(list->begin()); it != list->end(); ++it)
+                                                               switch (tag)
                                                                {
-                                                                       s.subtitling_type = (*it)->getSubtitlingType();
-                                                                       switch(s.subtitling_type)
-                                                                       {
-                                                                       case 0x10 ... 0x13:
-                                                                       case 0x20 ... 0x23: // dvb subtitles
-                                                                               break;
-                                                                       default:
-                                                                               eDebug("dvb subtitle %s PID %04x with wrong subtitling type (%02x)... force 0x10!!",
-                                                                                       s.language_code.c_str(), s.pid, s.subtitling_type);
-                                                                               s.subtitling_type = 0x10;
-                                                                               break;
-                                                                       }
-                                                                       s.composition_page_id = (*it)->getCompositionPageId();
-                                                                       s.ancillary_page_id = (*it)->getAncillaryPageId();
-                                                                       s.language_code = (*it)->getIso639LanguageCode();
-//                                                                     eDebug("add dvb subtitle %s PID %04x, type %d, composition page %d, ancillary_page %d",
-//                                                                             s.language_code.c_str(), s.pid, s.subtitling_type, s.composition_page_id, s.ancillary_page_id);
-                                                                       program.subtitleStreams.push_back(s);
+                                                               case AUDIO_STREAM_DESCRIPTOR:
+                                                                       isaudio = 1;
+                                                                       break;
+                                                               case VIDEO_STREAM_DESCRIPTOR:
+                                                               {
+                                                                       isvideo = 1;
+                                                                       VideoStreamDescriptor *d = (VideoStreamDescriptor*)(*desc);
+                                                                       if (d->getMpeg1OnlyFlag())
+                                                                               video.type = videoStream::vtMPEG1;
+                                                                       break;
                                                                }
-                                                               break;
-                                                       }
-                                                       case TELETEXT_DESCRIPTOR:
-                                                               if ( program.textPid == -1 || (*es)->getPid() == cached_tpid )
+                                                               case SUBTITLING_DESCRIPTOR:
                                                                {
+                                                                       SubtitlingDescriptor *d = (SubtitlingDescriptor*)(*desc);
+                                                                       const SubtitlingList *list = d->getSubtitlings();
                                                                        subtitleStream s;
-                                                                       s.subtitling_type = 0x01; // EBU TELETEXT SUBTITLES
-                                                                       s.pid = program.textPid = (*es)->getPid();
-                                                                       TeletextDescriptor *d = (TeletextDescriptor*)(*desc);
-                                                                       const VbiTeletextList *list = d->getVbiTeletexts();
-                                                                       for (VbiTeletextConstIterator it(list->begin()); it != list->end(); ++it)
+                                                                       s.pid = (*es)->getPid();
+                                                                       for (SubtitlingConstIterator it(list->begin()); it != list->end(); ++it)
                                                                        {
-                                                                               switch((*it)->getTeletextType())
+                                                                               s.subtitling_type = (*it)->getSubtitlingType();
+                                                                               switch(s.subtitling_type)
                                                                                {
-                                                                               case 0x02: // Teletext subtitle page
-                                                                               case 0x05: // Teletext subtitle page for hearing impaired pepople
-                                                                                       s.language_code = (*it)->getIso639LanguageCode();
-                                                                                       s.teletext_page_number = (*it)->getTeletextPageNumber();
-                                                                                       s.teletext_magazine_number = (*it)->getTeletextMagazineNumber();
-//                                                                                     eDebug("add teletext subtitle %s PID %04x, page number %d, magazine number %d",
-//                                                                                             s.language_code.c_str(), s.pid, s.teletext_page_number, s.teletext_magazine_number);
-                                                                                       program.subtitleStreams.push_back(s);
+                                                                               case 0x10 ... 0x13:
+                                                                               case 0x20 ... 0x23: // dvb subtitles
+                                                                                       break;
+                                                                               default:
+                                                                                       eDebug("dvb subtitle %s PID %04x with wrong subtitling type (%02x)... force 0x10!!",
+                                                                                               s.language_code.c_str(), s.pid, s.subtitling_type);
+                                                                                       s.subtitling_type = 0x10;
                                                                                        break;
                                                                                }
+                                                                               s.composition_page_id = (*it)->getCompositionPageId();
+                                                                               s.ancillary_page_id = (*it)->getAncillaryPageId();
+                                                                               s.language_code = (*it)->getIso639LanguageCode();
+//                                                                             eDebug("add dvb subtitle %s PID %04x, type %d, composition page %d, ancillary_page %d",
+//                                                                                     s.language_code.c_str(), s.pid, s.subtitling_type, s.composition_page_id, s.ancillary_page_id);
+                                                                               issubtitle=1;
+                                                                               program.subtitleStreams.push_back(s);
                                                                        }
+                                                                       break;
                                                                }
-                                                               break;
-                                                       case DTS_DESCRIPTOR:
-                                                               isaudio = 1;
-                                                               audio.type = audioStream::atDTS;
-                                                               break;
-                                                       case 0x2B: // TS_PSI_DT_MPEG2_AAC
-                                                               isaudio = 1;
-                                                               audio.type = audioStream::atAAC; // MPEG2-AAC
-                                                               break;
-                                                       case 0x1C: // TS_PSI_DT_MPEG4_Audio
-                                                       case AAC_DESCRIPTOR:
-                                                               isaudio = 1;
-                                                               audio.type = audioStream::atAACHE; // MPEG4-AAC
-                                                               break;
-                                                       case AC3_DESCRIPTOR:
-                                                               isaudio = 1;
-                                                               audio.type = audioStream::atAC3;
-                                                               break;
-                                                       case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
-                                                       {
-                                                                       /* libdvbsi++ doesn't yet support this descriptor type, so work around. */
-                                                               if ((*desc)->getLength() < 4)
+                                                               case TELETEXT_DESCRIPTOR:
+                                                                       if ( program.textPid == -1 || (*es)->getPid() == cached_tpid )
+                                                                       {
+                                                                               subtitleStream s;
+                                                                               s.subtitling_type = 0x01; // EBU TELETEXT SUBTITLES
+                                                                               s.pid = program.textPid = (*es)->getPid();
+                                                                               TeletextDescriptor *d = (TeletextDescriptor*)(*desc);
+                                                                               isteletext = 1;
+                                                                               const VbiTeletextList *list = d->getVbiTeletexts();
+                                                                               for (VbiTeletextConstIterator it(list->begin()); it != list->end(); ++it)
+                                                                               {
+                                                                                       switch((*it)->getTeletextType())
+                                                                                       {
+                                                                                       case 0x02: // Teletext subtitle page
+                                                                                       case 0x05: // Teletext subtitle page for hearing impaired pepople
+                                                                                               s.language_code = (*it)->getIso639LanguageCode();
+                                                                                               s.teletext_page_number = (*it)->getTeletextPageNumber();
+                                                                                               s.teletext_magazine_number = (*it)->getTeletextMagazineNumber();
+//                                                                                             eDebug("add teletext subtitle %s PID %04x, page number %d, magazine number %d",
+//                                                                                                     s.language_code.c_str(), s.pid, s.teletext_page_number, s.teletext_magazine_number);
+                                                                                               program.subtitleStreams.push_back(s);
+                                                                                               issubtitle=1;
+                                                                                       default:
+                                                                                               break;
+                                                                                       }
+                                                                               }
+                                                                       }
                                                                        break;
-                                                               unsigned char descr[6];
-                                                               (*desc)->writeToBuffer(descr);
-                                                               int format_identifier = (descr[2] << 24) | (descr[3] << 16) | (descr[4] << 8) | (descr[5]);
-                                                               switch (format_identifier)
-                                                               {
-                                                               case 0x44545331 ... 0x44545333: // DTS1/DTS2/DTS3
+                                                               case DTS_DESCRIPTOR:
                                                                        isaudio = 1;
                                                                        audio.type = audioStream::atDTS;
                                                                        break;
-                                                               case 0x41432d33: // == 'AC-3'
+                                                               case 0x2B: // TS_PSI_DT_MPEG2_AAC
                                                                        isaudio = 1;
-                                                                       audio.type = audioStream::atAC3;
+                                                                       audio.type = audioStream::atAAC; // MPEG2-AAC
+                                                                       break;
+                                                               case 0x1C: // TS_PSI_DT_MPEG4_Audio
+                                                               case AAC_DESCRIPTOR:
+                                                                       isaudio = 1;
+                                                                       audio.type = audioStream::atAACHE; // MPEG4-AAC
                                                                        break;
-                                                               case 0x42535344: // == 'BSSD' (LPCM)
+                                                               case AC3_DESCRIPTOR:
                                                                        isaudio = 1;
-                                                                       audio.type = audioStream::atLPCM;
+                                                                       audio.type = audioStream::atAC3;
                                                                        break;
-                                                               case 0x56432d31: // == 'VC-1'
-                                                                       if (descr[6] == 0x01) // subdescriptor tag
+                                                               case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
+                                                               {
+                                                                       RegistrationDescriptor *d = (RegistrationDescriptor*)(*desc);
+                                                                       switch (d->getFormatIdentifier())
                                                                        {
-                                                                               if (descr[7] >= 0x90) // profile_level
-                                                                                       video.type = videoStream::vtVC1; // advanced profile
-                                                                               else
-                                                                                       video.type = videoStream::vtVC1_SM; // simple main
-                                                                               isvideo = 1;
+                                                                       case 0x44545331 ... 0x44545333: // DTS1/DTS2/DTS3
+                                                                               isaudio = 1;
+                                                                               audio.type = audioStream::atDTS;
+                                                                               break;
+                                                                       case 0x41432d33: // == 'AC-3'
+                                                                               isaudio = 1;
+                                                                               audio.type = audioStream::atAC3;
+                                                                               break;
+                                                                       case 0x42535344: // == 'BSSD' (LPCM)
+                                                                               isaudio = 1;
+                                                                               audio.type = audioStream::atLPCM;
+                                                                               break;
+                                                                       case 0x56432d31: // == 'VC-1'
+                                                                       {
+                                                                               const AdditionalIdentificationInfoVector *vec = d->getAdditionalIdentificationInfo();
+                                                                               if (vec->size() > 1 && (*vec)[0] == 0x01) // subdescriptor tag
+                                                                               {
+                                                                                       if ((*vec)[1] >= 0x90) // profile_level
+                                                                                               video.type = videoStream::vtVC1; // advanced profile
+                                                                                       else
+                                                                                               video.type = videoStream::vtVC1_SM; // simple main
+                                                                                       isvideo = 1;
+                                                                               }
+                                                                       }
+                                                                       default:
+                                                                               break;
                                                                        }
                                                                        break;
+                                                               }
+                                                               case 0x28: // TS_PSI_DT_AVC
+                                                                       isvideo = 1;
+                                                                       video.type = videoStream::vtMPEG4_H264;
+                                                                       break;
+                                                               case 0x1B: // TS_PSI_DT_MPEG4_Video
+                                                                       isvideo = 1;
+                                                                       video.type = videoStream::vtMPEG4_Part2;
+                                                                       break;
                                                                default:
                                                                        break;
                                                                }
-                                                               break;
                                                        }
-                                                       case 0x28: // TS_PSI_DT_AVC
-                                                               isvideo = 1;
-                                                               video.type = videoStream::vtMPEG4_H264;
-                                                               break;
-                                                       case 0x1B: // TS_PSI_DT_MPEG4_Video
-                                                               isvideo = 1;
-                                                               video.type = videoStream::vtMPEG4_Part2;
-                                                               break;
+                                                       switch (tag)
+                                                       {
                                                        case ISO_639_LANGUAGE_DESCRIPTOR:
                                                                if (!isvideo)
                                                                {
@@ -443,21 +467,27 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                                                break;
                                                        }
                                                }
+                                               if (!num_descriptors && streamtype == 0x06 && prev_audio)
+                                               {
+                                                       prev_audio->rdsPid = (*es)->getPid();
+                                                       eDebug("Rds PID %04x detected ? ! ?", prev_audio->rdsPid);
+                                               }
+                                               prev_audio = 0;
+                                       }
+                                       default:
+                                               break;
                                        }
-                                       if (isaudio)
+                                       if (isteletext && (isaudio || isvideo)) 
                                        {
-                                               audio.pid = (*es)->getPid();
-
-                                                       /* if we find the cached pids, this will be our default stream */
-                                               if (audio.pid == cached_apid_ac3 || audio.pid == cached_apid_mpeg)
-                                                       program.defaultAudioStream = program.audioStreams.size();
-
-                                                       /* also, we need to know the first non-mpeg (i.e. "ac3"/dts/...) stream */
-                                               if ((audio.type != audioStream::atMPEG) && ((first_ac3 == -1) || (audio.pid == cached_apid_ac3)))
-                                                       first_ac3 = program.audioStreams.size();
-
-                                               program.audioStreams.push_back(audio);
+                                               eDebug("ambiguous streamtype for PID %04x detected.. forced as teletext!", (*es)->getPid());                                    
+                                               continue; // continue with next PID
                                        }
+                                       else if (issubtitle && (isaudio || isvideo))
+                                               eDebug("ambiguous streamtype for PID %04x detected.. forced as subtitle!", (*es)->getPid());
+                                       else if (isaudio && isvideo)
+                                               eDebug("ambiguous streamtype for PID %04x detected.. forced as video!", (*es)->getPid());
+                                       if (issubtitle) // continue with next PID
+                                               continue;
                                        else if (isvideo)
                                        {
                                                video.pid = (*es)->getPid();
@@ -469,6 +499,21 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                                                else
                                                        program.videoStreams.push_back(video);
                                        }
+                                       else if (isaudio)
+                                       {
+                                               audio.pid = (*es)->getPid();
+
+                                                       /* if we find the cached pids, this will be our default stream */
+                                               if (audio.pid == cached_apid_ac3 || audio.pid == cached_apid_mpeg)
+                                                       program.defaultAudioStream = program.audioStreams.size();
+
+                                                       /* also, we need to know the first non-mpeg (i.e. "ac3"/dts/...) stream */
+                                               if ((audio.type != audioStream::atMPEG) && ((first_ac3 == -1) || (audio.pid == cached_apid_ac3)))
+                                                       first_ac3 = program.audioStreams.size();
+
+                                               program.audioStreams.push_back(audio);
+                                               prev_audio = &program.audioStreams.back();
+                                       }
                                        else
                                                continue;
                                }
@@ -484,7 +529,6 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                        }
                        ret = 0;
 
-
                        /* finally some fixup: if our default audio stream is an MPEG audio stream, 
                           and we have 'defaultac3' set, use the first available ac3 stream instead.
                           (note: if an ac3 audio stream was selected before, this will be also stored
@@ -521,6 +565,7 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                        audioStream s;
                        s.type = audioStream::atAC3;
                        s.pid = cached_apid_ac3;
+                       s.rdsPid = -1;
                        program.audioStreams.push_back(s);
                        ++cnt;
                }
@@ -529,6 +574,7 @@ int eDVBServicePMTHandler::getProgramInfo(struct program &program)
                        audioStream s;
                        s.type = audioStream::atMPEG;
                        s.pid = cached_apid_mpeg;
+                       s.rdsPid = -1;
                        program.audioStreams.push_back(s);
                        ++cnt;
                }
@@ -654,10 +700,7 @@ int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux,
                        eDVBCIInterfaces::getInstance()->addPMTHandler(this);
        } else if (!simulate) // no simulation of playback services
        {
-               eDVBMetaParser parser;
-
-               int ret=parser.parseFile(ref.path);
-               if (ret || !parser.m_ref.getServiceID().get() /* incorrect sid in meta file or recordings.epl*/ )
+               if (!ref.getServiceID().get() /* incorrect sid in meta file or recordings.epl*/ )
                {
                        eWarning("no .meta file found, trying to find PMT pid");
                        eDVBTSTools tstools;
@@ -673,9 +716,7 @@ int eDVBServicePMTHandler::tune(eServiceReferenceDVB &ref, int use_decode_demux,
                                        m_pmt_pid = pmt_pid;
                                }
                        }
-               } else
-                       m_reference = parser.m_ref;
-               
+               }
                eDebug("alloc PVR");
                        /* allocate PVR */
                res = m_resourceManager->allocatePVRChannel(m_pvr_channel);