only write .cuts file when .ts file is still accessible
[enigma2.git] / lib / service / servicedvb.cpp
index 616c7007e5ceeb9a6707d4520e152917c0ddedf5..75b55215b9158099309f3785d8ce55c5ae2fa873 100644 (file)
@@ -21,6 +21,7 @@
 #include <lib/gui/esubtitle.h>
 
 #include <sys/vfs.h>
+#include <sys/stat.h>
 
 #include <byteswap.h>
 #include <netinet/in.h>
@@ -312,6 +313,9 @@ eServiceFactoryDVB::eServiceFactoryDVB()
        eServiceCenter::getPrivInstance(sc);
        if (sc)
                sc->addServiceFactory(eServiceFactoryDVB::id, this);
+
+       m_StaticServiceDVBInfo = new eStaticServiceDVBInformation;
+       m_StaticServiceDVBBouquetInfo = new eStaticServiceDVBBouquetInformation;
 }
 
 eServiceFactoryDVB::~eServiceFactoryDVB()
@@ -506,11 +510,11 @@ RESULT eDVBServiceList::startEdit(ePtr<iMutableServiceList> &res)
        return -1;
 }
 
-RESULT eDVBServiceList::addService(eServiceReference &ref)
+RESULT eDVBServiceList::addService(eServiceReference &ref, eServiceReference before)
 {
        if (!m_bouquet)
                return -1;
-       return m_bouquet->addService(ref);
+       return m_bouquet->addService(ref, before);
 }
 
 RESULT eDVBServiceList::removeService(eServiceReference &ref)
@@ -584,9 +588,9 @@ RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServic
        if ((ref.flags & eServiceReference::flagDirectory) == eServiceReference::flagDirectory) // bouquet
        {
                if ( !ref.name.empty() )  // satellites or providers list
-                       ptr = new eStaticServiceDVBInformation;
+                       ptr = m_StaticServiceDVBInfo;
                else // a dvb bouquet
-                       ptr = new eStaticServiceDVBBouquetInformation;
+                       ptr = m_StaticServiceDVBBouquetInfo;
        }
        else if (!ref.path.empty()) /* do we have a PVR service? */
                ptr = new eStaticServiceDVBPVRInformation(ref);
@@ -594,7 +598,7 @@ RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServic
        {
                ePtr<eDVBService> service;
                if (lookupService(service, ref)) // no eDVBService avail for this reference ( Linkage Services... )
-                       ptr = new eStaticServiceDVBInformation;
+                       ptr = m_StaticServiceDVBInfo;
                else
                        /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
                        ptr = service;
@@ -794,13 +798,41 @@ RESULT eDVBServicePlay::start()
 
 RESULT eDVBServicePlay::stop()
 {
+               /* add bookmark for last play position */
+       if (m_is_pvr)
+       {
+               pts_t play_position;
+               if (!getPlayPosition(play_position))
+               {
+                               /* remove last position */
+                       for (std::multiset<struct cueEntry>::iterator i(m_cue_entries.begin()); i != m_cue_entries.end();)
+                       {
+                               if (i->what == 3) /* current play position */
+                               {
+                                       m_cue_entries.erase(i);
+                                       i = m_cue_entries.begin();
+                                       continue;
+                               } else
+                                       ++i;
+                       }
+                       
+                       m_cue_entries.insert(cueEntry(play_position, 3)); /* last play position */
+                       m_cuesheet_changed = 1;
+               }
+       }
+
        stopTimeshift(); /* in case timeshift was enabled, remove buffer etc. */
 
        m_service_handler_timeshift.free();
        m_service_handler.free();
        
        if (m_is_pvr && m_cuesheet_changed)
-               saveCuesheet();
+       {
+               struct stat s;
+                               /* save cuesheet only when main file is accessible. */
+               if (!::stat(m_reference.path.c_str(), &s))
+                       saveCuesheet();
+       }
        
        return 0;
 }
@@ -1083,6 +1115,12 @@ RESULT eDVBServicePlay::audioDelay(ePtr<iAudioDelay> &ptr)
        return 0;
 }
 
+RESULT eDVBServicePlay::radioText(ePtr<iRadioText> &ptr)
+{
+       ptr = this;
+       return 0;
+}
+
 RESULT eDVBServicePlay::getName(std::string &name)
 {
        if (m_is_pvr)
@@ -1162,7 +1200,7 @@ int eDVBServicePlay::getInfo(int w)
        case sIsCrypted: return program.isCrypted();
        case sVideoPID: if (program.videoStreams.empty()) return -1; return program.videoStreams[0].pid;
        case sVideoType: if (program.videoStreams.empty()) return -1; return program.videoStreams[0].type;
-       case sAudioPID: if (program.audioStreams.empty()) return -1; return program.audioStreams[m_current_audio_stream].pid;
+       case sAudioPID: if (program.audioStreams.empty()) return -1; return program.audioStreams[0].pid;
        case sPCRPID: return program.pcrPid;
        case sPMTPID: return program.pmtPid;
        case sTXTPID: return program.textPid;
@@ -1273,21 +1311,23 @@ int eDVBServicePlay::selectAudioStream(int i)
        if (m_decoder->setAudioPID(program.audioStreams[i].pid, program.audioStreams[i].type))
                return -4;
 
+       if (m_radiotext_parser)
+               m_radiotext_parser->start(program.audioStreams[i].pid);
+
        if (m_dvb_service && !m_is_pvr)
        {
                if (program.audioStreams[i].type == eDVBAudio::aMPEG)
                {
                        m_dvb_service->setCacheEntry(eDVBService::cAPID, program.audioStreams[i].pid);
                        m_dvb_service->setCacheEntry(eDVBService::cAC3PID, -1);
-               }       else
+               }
+               else
                {
                        m_dvb_service->setCacheEntry(eDVBService::cAPID, -1);
                        m_dvb_service->setCacheEntry(eDVBService::cAC3PID, program.audioStreams[i].pid);
                }
        }
 
-       m_current_audio_stream = i;
-
        return 0;
 }
 
@@ -1307,6 +1347,22 @@ RESULT eDVBServicePlay::selectChannel(int i)
        return 0;
 }
 
+std::string eDVBServicePlay::getRadioText(int x)
+{
+       if (m_radiotext_parser)
+               switch(x)
+               {
+                       case 0:
+                               return m_radiotext_parser->getCurrentText();
+               }
+       return "";
+}
+
+void eDVBServicePlay::radioTextUpdated()
+{
+       m_event((iPlayableService*)this, evUpdatedRadioText);
+}
+
 int eDVBServiceBase::getFrontendInfo(int w)
 {
        eUsePtr<iDVBChannel> channel;
@@ -1579,7 +1635,9 @@ void eDVBServicePlay::switchToLive()
        m_decoder = 0;
        m_decode_demux = 0;
        m_teletext_parser = 0;
+       m_radiotext_parser = 0;
        m_new_subtitle_page_connection = 0;
+       m_radiotext_updated_connection = 0;
        
                /* free the timeshift service handler, we need the resources */
        m_service_handler_timeshift.free();
@@ -1598,8 +1656,10 @@ void eDVBServicePlay::switchToTimeshift()
        m_decode_demux = 0;
        m_decoder = 0;
        m_teletext_parser = 0;
+       m_radiotext_parser = 0;
        m_new_subtitle_page_connection = 0;
-       
+       m_radiotext_updated_connection = 0;
+
        m_timeshift_active = 1;
 
        m_event((iPlayableService*)this, evSeekableStatusChanged);
@@ -1726,10 +1786,21 @@ void eDVBServicePlay::updateDecoder()
                m_decoder->setPCMDelay(pcm_delay == -1 ? 0 : pcm_delay);
 
                m_decoder->setVideoPID(vpid, vpidtype);
-               m_current_audio_stream = 0;
                m_decoder->setAudioPID(apid, apidtype);
                if (!(m_is_pvr || m_timeshift_active || !m_is_primary))
+               {
                        m_decoder->setSyncPCR(pcrpid);
+                       if (apid != -1)
+                       {
+                               ePtr<iDVBDemux> data_demux;
+                               if (!h.getDataDemux(data_demux))
+                               {
+                                       m_radiotext_parser = new eDVBRadioTextParser(data_demux);
+                                       m_radiotext_parser->connectUpdatedRadiotext(slot(*this, &eDVBServicePlay::radioTextUpdated), m_radiotext_updated_connection);
+                                       m_radiotext_parser->start(apid);
+                               }
+                       }
+               }
                else
                        m_decoder->setSyncPCR(-1);
 
@@ -1806,7 +1877,7 @@ void eDVBServicePlay::loadCuesheet()
 #endif
                        what = ntohl(what);
                        
-                       if (what > 2)
+                       if (what > 3)
                                break;
                        
                        m_cue_entries.insert(cueEntry(where, what));
@@ -1862,7 +1933,7 @@ void eDVBServicePlay::cutlistToCuesheet()
        if (!m_cutlist_enabled)
        {
                m_cue->commitSpans();
-               eDebug("cutlists where disabled");
+               eDebug("cutlists were disabled");
                return;
        }
 
@@ -1883,7 +1954,7 @@ void eDVBServicePlay::cutlistToCuesheet()
                                continue;
                        } else if (i->what == 1) /* out */
                                out = i++->where;
-                       else /* mark */
+                       else /* mark (2) or last play position (3) */
                        {
                                i++;
                                continue;