add SeekRelative stuff
[enigma2.git] / lib / service / servicedvb.cpp
index 5618477..76b0089 100644 (file)
 
 #include <lib/service/servicedvbrecord.h>
 #include <lib/dvb/metaparser.h>
 
 #include <lib/service/servicedvbrecord.h>
 #include <lib/dvb/metaparser.h>
+#include <lib/dvb/tstools.h>
+
+class eStaticServiceDVBInformation: public iStaticServiceInformation
+{
+       DECLARE_REF(eStaticServiceDVBInformation);
+public:
+       RESULT getName(const eServiceReference &ref, std::string &name);
+       int getLength(const eServiceReference &ref);
+};
+
+DEFINE_REF(eStaticServiceDVBInformation);
+
+RESULT eStaticServiceDVBInformation::getName(const eServiceReference &ref, std::string &name)
+{
+       if ( ref.name.length() )
+       {
+               name = ref.name;
+               return 0;
+       }
+       else
+               return -1;
+}
+
+int eStaticServiceDVBInformation::getLength(const eServiceReference &ref)
+{
+       return -1;
+}
+
+class eStaticServiceDVBBouquetInformation: public iStaticServiceInformation
+{
+       DECLARE_REF(eStaticServiceDVBBouquetInformation);
+public:
+       RESULT getName(const eServiceReference &ref, std::string &name);
+       int getLength(const eServiceReference &ref);
+};
+
+DEFINE_REF(eStaticServiceDVBBouquetInformation);
+
+RESULT eStaticServiceDVBBouquetInformation::getName(const eServiceReference &ref, std::string &name)
+{
+       ePtr<iDVBChannelList> db;
+       ePtr<eDVBResourceManager> res;
+
+       int err;
+       if ((err = eDVBResourceManager::getInstance(res)) != 0)
+       {
+               eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no resource manager!");
+               return err;
+       }
+       if ((err = res->getChannelList(db)) != 0)
+       {
+               eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no channel list!");
+               return err;
+       }
+
+       const eBouquet *bouquet=0;
+       if ((err = db->getBouquet(ref, bouquet)) != 0)
+       {
+               eDebug("eStaticServiceDVBBouquetInformation::getName failed.. getBouquet failed!");
+               return -1;
+       }
+
+       if ( bouquet && bouquet->m_bouquet_name.length() )
+       {
+               name = "[Bouquet] " + bouquet->m_bouquet_name;
+               return 0;
+       }
+       else
+               return -1;
+}
+
+int eStaticServiceDVBBouquetInformation::getLength(const eServiceReference &ref)
+{
+       return -1;
+}
 
 class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
 {
 
 class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
 {
@@ -20,6 +95,7 @@ class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
 public:
        eStaticServiceDVBPVRInformation(const eServiceReference &ref);
        RESULT getName(const eServiceReference &ref, std::string &name);
 public:
        eStaticServiceDVBPVRInformation(const eServiceReference &ref);
        RESULT getName(const eServiceReference &ref, std::string &name);
+       int getLength(const eServiceReference &ref);
 };
 
 DEFINE_REF(eStaticServiceDVBPVRInformation);
 };
 
 DEFINE_REF(eStaticServiceDVBPVRInformation);
@@ -34,6 +110,71 @@ RESULT eStaticServiceDVBPVRInformation::getName(const eServiceReference &ref, st
 {
        ASSERT(ref == m_ref);
        name = m_parser.m_name.size() ? m_parser.m_name : ref.path;
 {
        ASSERT(ref == m_ref);
        name = m_parser.m_name.size() ? m_parser.m_name : ref.path;
+       return 0;
+}
+
+int eStaticServiceDVBPVRInformation::getLength(const eServiceReference &ref)
+{
+       ASSERT(ref == m_ref);
+       
+       eDVBTSTools tstools;
+       
+       if (tstools.openFile(ref.path.c_str()))
+               return 0;
+
+       pts_t len;
+       if (tstools.calcLen(len))
+               return 0;
+
+       return len / 90000;
+}
+
+
+
+class eDVBPVRServiceOfflineOperations: public iServiceOfflineOperations
+{
+       DECLARE_REF(eDVBPVRServiceOfflineOperations);
+       eServiceReferenceDVB m_ref;
+public:
+       eDVBPVRServiceOfflineOperations(const eServiceReference &ref);
+       
+       RESULT deleteFromDisk(int simulate);
+       RESULT getListOfFilenames(std::list<std::string> &);
+};
+
+DEFINE_REF(eDVBPVRServiceOfflineOperations);
+
+eDVBPVRServiceOfflineOperations::eDVBPVRServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReferenceDVB&)ref)
+{
+}
+
+RESULT eDVBPVRServiceOfflineOperations::deleteFromDisk(int simulate)
+{
+       if (simulate)
+               return 0;
+       else
+       {
+               std::list<std::string> res;
+               if (getListOfFilenames(res))
+                       return -1;
+               
+                               /* TODO: deferred removing.. */
+               for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
+               {
+                       eDebug("Removing %s...", i->c_str());
+                       ::unlink(i->c_str());
+               }
+               
+               return 0;
+       }
+}
+
+RESULT eDVBPVRServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
+{
+       res.clear();
+       res.push_back(m_ref.path);
+       res.push_back(m_ref.path + ".meta");
+       return 0;
 }
 
 DEFINE_REF(eServiceFactoryDVB)
 }
 
 DEFINE_REF(eServiceFactoryDVB)
@@ -42,7 +183,7 @@ eServiceFactoryDVB::eServiceFactoryDVB()
 {
        ePtr<eServiceCenter> sc;
        
 {
        ePtr<eServiceCenter> sc;
        
-       eServiceCenter::getInstance(sc);
+       eServiceCenter::getPrivInstance(sc);
        if (sc)
                sc->addServiceFactory(eServiceFactoryDVB::id, this);
 }
        if (sc)
                sc->addServiceFactory(eServiceFactoryDVB::id, this);
 }
@@ -51,7 +192,7 @@ eServiceFactoryDVB::~eServiceFactoryDVB()
 {
        ePtr<eServiceCenter> sc;
        
 {
        ePtr<eServiceCenter> sc;
        
-       eServiceCenter::getInstance(sc);
+       eServiceCenter::getPrivInstance(sc);
        if (sc)
                sc->removeServiceFactory(eServiceFactoryDVB::id);
 }
        if (sc)
                sc->removeServiceFactory(eServiceFactoryDVB::id);
 }
@@ -66,7 +207,7 @@ eDVBServiceList::~eDVBServiceList()
 {
 }
 
 {
 }
 
-RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
+RESULT eDVBServiceList::startQuery()
 {
        ePtr<iDVBChannelList> db;
        ePtr<eDVBResourceManager> res;
 {
        ePtr<iDVBChannelList> db;
        ePtr<eDVBResourceManager> res;
@@ -83,30 +224,50 @@ RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
                return err;
        }
        
                return err;
        }
        
-       ePtr<iDVBChannelListQuery> query;
-       
        ePtr<eDVBChannelQuery> q;
        
        ePtr<eDVBChannelQuery> q;
        
-       if (m_parent.path.size())
+       if (!m_parent.path.empty())
+       {
                eDVBChannelQuery::compile(q, m_parent.path);
                eDVBChannelQuery::compile(q, m_parent.path);
+               if (!q)
+               {
+                       eDebug("compile query failed");
+                       return err;
+               }
+       }
        
        
-       if ((err = db->startQuery(query, q)) != 0)
+       if ((err = db->startQuery(m_query, q, m_parent)) != 0)
        {
                eDebug("startQuery failed");
                return err;
        }
        {
                eDebug("startQuery failed");
                return err;
        }
-       
+
+       return 0;
+}
+
+RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
+{
        eServiceReferenceDVB ref;
        
        eServiceReferenceDVB ref;
        
-       while (!query->getNextResult(ref))
+       if (!m_query)
+               return -1;
+       
+       while (!m_query->getNextResult(ref))
                list.push_back(ref);
        return 0;
 }
 
                list.push_back(ref);
        return 0;
 }
 
-RESULT eDVBServiceList::getNext(eServiceReference &)
+RESULT eDVBServiceList::getNext(eServiceReference &ref)
 {
 {
-               /* implement me */
-       return -1;
+       if (!m_query)
+               return -1;
+       
+       return m_query->getNextResult((eServiceReferenceDVB&)ref);
+}
+
+int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
+{
+       return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
 }
 
 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
 }
 
 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
@@ -122,31 +283,65 @@ RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableServ
 
 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
 {
 
 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
 {
-       ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
-       return 0;
+       if (ref.path.empty())
+       {
+               ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
+               return 0;
+       } else
+       {
+               ptr = 0;
+               return -1;
+       }
 }
 
 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
 {
 }
 
 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
 {
-       ptr = new eDVBServiceList(ref);
+       ePtr<eDVBServiceList> list = new eDVBServiceList(ref);
+       if (list->startQuery())
+       {
+               ptr = 0;
+               return -1;
+       }
+       
+       ptr = list;
        return 0;
 }
 
 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
 {
                /* do we have a PVR service? */
        return 0;
 }
 
 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
 {
                /* do we have a PVR service? */
-       if (ref.path.size())
+       if (ref.flags & eServiceReference::flagDirectory) // bouquet
+       {
+               ptr = new eStaticServiceDVBBouquetInformation;
+               return 0;
+       }
+       else if (!ref.path.empty())
        {
                ptr = new eStaticServiceDVBPVRInformation(ref);
                return 0;
        {
                ptr = new eStaticServiceDVBPVRInformation(ref);
                return 0;
-       } else
+       }
+       else
        {
                ePtr<eDVBService> service;
                int r = lookupService(service, ref);
                if (r)
        {
                ePtr<eDVBService> service;
                int r = lookupService(service, ref);
                if (r)
-                       return r;
+                       ptr = new eStaticServiceDVBInformation;
+               else
                        /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
                        /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
-               ptr = service;
+                       ptr = service;
+               return 0;
+       }
+}
+
+RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
+{
+       if (ref.path.empty())
+       {
+               ptr = 0;
+               return -1;
+       } else
+       {
+               ptr = new eDVBPVRServiceOfflineOperations(ref);
                return 0;
        }
 }
                return 0;
        }
 }
@@ -183,16 +378,16 @@ RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServ
 }
 
 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service): 
 }
 
 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service): 
-       m_reference(ref), m_dvb_service(service)
+       m_reference(ref), m_dvb_service(service), m_service_handler(0), m_is_paused(0)
 {
 {
+       m_is_pvr = !ref.path.empty();
+       
        CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
        CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
        CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
        CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
-       eDebug("DVB start (play)");
 }
 
 eDVBServicePlay::~eDVBServicePlay()
 {
 }
 
 eDVBServicePlay::~eDVBServicePlay()
 {
-       eDebug("DVB stop (play)");
 }
 
 void eDVBServicePlay::gotNewEvent()
 }
 
 void eDVBServicePlay::gotNewEvent()
@@ -213,7 +408,6 @@ void eDVBServicePlay::gotNewEvent()
 
 void eDVBServicePlay::serviceEvent(int event)
 {
 
 void eDVBServicePlay::serviceEvent(int event)
 {
-       eDebug("service event %d", event);
        switch (event)
        {
        case eDVBServicePMTHandler::eventTuned:
        switch (event)
        {
        case eDVBServicePMTHandler::eventTuned:
@@ -223,11 +417,16 @@ void eDVBServicePlay::serviceEvent(int event)
                {
 //                     eventStartedEventAcquisition
                        m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
                {
 //                     eventStartedEventAcquisition
                        m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
-               } else
-                       eDebug("no event data available :( ");
+               }
 //                     eventNoEvent
                break;
        }
 //                     eventNoEvent
                break;
        }
+       case eDVBServicePMTHandler::eventTuneFailed:
+       {
+               eDebug("DVB service failed to tune");
+               m_event((iPlayableService*)this, evTuneFailed);
+               break;
+       }
        case eDVBServicePMTHandler::eventNewProgramInfo:
        {
                int vpid = -1, apid = -1, pcrpid = -1;
        case eDVBServicePMTHandler::eventNewProgramInfo:
        {
                int vpid = -1, apid = -1, pcrpid = -1;
@@ -285,14 +484,22 @@ void eDVBServicePlay::serviceEvent(int event)
                {
                        m_decoder->setVideoPID(vpid);
                        m_decoder->setAudioPID(apid, 0);
                {
                        m_decoder->setVideoPID(vpid);
                        m_decoder->setAudioPID(apid, 0);
-                       m_decoder->setSyncPCR(pcrpid);
+                       if (!m_is_pvr)
+                               m_decoder->setSyncPCR(pcrpid);
+                       else
+                               m_decoder->setSyncPCR(-1);
                        m_decoder->start();
 // how we can do this better?
 // update cache pid when the user changed the audio track or video track
 // TODO handling of difference audio types.. default audio types..
                        m_decoder->start();
 // how we can do this better?
 // update cache pid when the user changed the audio track or video track
 // TODO handling of difference audio types.. default audio types..
-                       m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
-                       m_dvb_service->setCachePID(eDVBService::cAPID, apid);
-                       m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
+                               
+                               /* don't worry about non-existing services, nor pvr services */
+                       if (m_dvb_service && !m_is_pvr)
+                       {
+                               m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
+                               m_dvb_service->setCachePID(eDVBService::cAPID, apid);
+                               m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
+                       }
                }
                
                break;
                }
                
                break;
@@ -302,9 +509,12 @@ void eDVBServicePlay::serviceEvent(int event)
 
 RESULT eDVBServicePlay::start()
 {
 
 RESULT eDVBServicePlay::start()
 {
+       int r;
        eDebug("starting DVB service");
        eDebug("starting DVB service");
+       r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
+       eDebug("tune result: %d", r);
        m_event(this, evStart);
        m_event(this, evStart);
-       return m_service_handler.tune((eServiceReferenceDVB&)m_reference);
+       return 0;
 }
 
 RESULT eDVBServicePlay::stop()
 }
 
 RESULT eDVBServicePlay::stop()
@@ -321,11 +531,88 @@ RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &ev
 
 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
 {
 
 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
 {
-               // not yet possible, maybe later...
+       if (m_is_pvr)
+       {
+               ptr = this;
+               return 0;
+       }
+
+       ptr = 0;
+       return -1;
+}
+
+RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
+{
+       if (m_is_pvr)
+       {
+               ptr = this;
+               return 0;
+       }
+       
        ptr = 0;
        return -1;
 }
 
        ptr = 0;
        return -1;
 }
 
+RESULT eDVBServicePlay::getLength(pts_t &len)
+{
+       ePtr<iDVBPVRChannel> pvr_channel;
+       
+       if (m_service_handler.getPVRChannel(pvr_channel))
+       {
+               eDebug("getPVRChannel failed!");
+               return -1;
+       }
+       
+       return pvr_channel->getLength(len);
+}
+
+RESULT eDVBServicePlay::pause()
+{
+       if (!m_is_paused && m_decoder)
+       {
+               m_is_paused = 1;
+               return m_decoder->freeze(0);
+       } else
+               return -1;
+}
+
+RESULT eDVBServicePlay::unpause()
+{
+       if (m_is_paused && m_decoder)
+       {
+               m_is_paused = 0;
+               return m_decoder->unfreeze();
+       } else
+               return -1;
+}
+
+RESULT eDVBServicePlay::seekTo(pts_t to)
+{
+       return -1;
+}
+
+RESULT eDVBServicePlay::seekRelative(int direction, pts_t to)
+{
+       eDebug("eDVBServicePlay::seekRelative: jump %d, %lld", direction, to);
+
+       ePtr<iDVBPVRChannel> pvr_channel;
+       
+       if (m_service_handler.getPVRChannel(pvr_channel))
+               return -1;
+       
+       return pvr_channel->seekToPosition(SEEK_CUR, to);
+}
+
+RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
+{
+       ePtr<iDVBPVRChannel> pvr_channel;
+       
+       if (m_service_handler.getPVRChannel(pvr_channel))
+               return -1;
+       
+       return pvr_channel->getCurrentPosition(pos);
+}
+
 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
 {
        ptr = this;
 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
 {
        ptr = this;
@@ -335,8 +622,11 @@ RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
 RESULT eDVBServicePlay::getName(std::string &name)
 {
        if (m_dvb_service)
 RESULT eDVBServicePlay::getName(std::string &name)
 {
        if (m_dvb_service)
+       {
                m_dvb_service->getName(m_reference, name);
                m_dvb_service->getName(m_reference, name);
-       else
+               if (name.empty())
+                       name = "(...)";
+       } else
                name = "DVB service";
        return 0;
 }
                name = "DVB service";
        return 0;
 }