some work on DVB-T
[enigma2.git] / lib / service / servicedvb.cpp
index 15deff62901599a5e54ab6dae751480e570346a9..94c31b4f1ab787dfe6b89519a7f7e3157c3f631a 100644 (file)
 #include <lib/dvb/db.h>
 #include <lib/dvb/decoder.h>
 
+#include <lib/components/file_eraser.h>
 #include <lib/service/servicedvbrecord.h>
+#include <lib/service/event.h>
 #include <lib/dvb/metaparser.h>
 #include <lib/dvb/tstools.h>
+#include <lib/python/python.h>
+
+#include <sys/vfs.h>
+
+#include <byteswap.h>
+#include <netinet/in.h>
+
+#include <dvbsi++/event_information_section.h>
+
+#ifndef BYTE_ORDER
+#error no byte order defined!
+#endif
+
+#define TSPATH "/media/hdd"
 
 class eStaticServiceDVBInformation: public iStaticServiceInformation
 {
@@ -170,6 +186,8 @@ int eStaticServiceDVBPVRInformation::getInfo(const eServiceReference &ref, int w
        {
        case iServiceInformation::sDescription:
                return iServiceInformation::resIsString;
+       case iServiceInformation::sServiceref:
+               return iServiceInformation::resIsString;
        case iServiceInformation::sTimeCreate:
                if (m_parser.m_time_create)
                        return m_parser.m_time_create;
@@ -186,6 +204,8 @@ std::string eStaticServiceDVBPVRInformation::getInfoString(const eServiceReferen
        {
        case iServiceInformation::sDescription:
                return m_parser.m_description;
+       case iServiceInformation::sServiceref:
+               return m_parser.m_ref.toString();
        default:
                return "";
        }
@@ -218,11 +238,18 @@ RESULT eDVBPVRServiceOfflineOperations::deleteFromDisk(int simulate)
                if (getListOfFilenames(res))
                        return -1;
                
+               eBackgroundFileEraser *eraser = eBackgroundFileEraser::getInstance();
+               if (!eraser)
+                       eDebug("FATAL !! can't get background file eraser");
+               
                                /* 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());
+                       if (eraser)
+                               eraser->erase(i->c_str());
+                       else
+                               ::unlink(i->c_str());
                }
                
                return 0;
@@ -233,7 +260,25 @@ RESULT eDVBPVRServiceOfflineOperations::getListOfFilenames(std::list<std::string
 {
        res.clear();
        res.push_back(m_ref.path);
+
+// handling for old splitted recordings (enigma 1)
+       char buf[255];
+       int slice=1;
+       while(true)
+       {
+               snprintf(buf, 255, "%s.%03d", m_ref.path.c_str(), slice++);
+               struct stat s;
+               if (stat(buf, &s) < 0)
+                       break;
+               res.push_back(buf);
+       }       
+
        res.push_back(m_ref.path + ".meta");
+       res.push_back(m_ref.path + ".ap");
+       res.push_back(m_ref.path + ".cuts");
+       std::string tmp = m_ref.path;
+       tmp.erase(m_ref.path.length()-3);
+       res.push_back(tmp + ".eit");
        return 0;
 }
 
@@ -305,7 +350,32 @@ RESULT eDVBServiceList::startQuery()
        return 0;
 }
 
-RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
+RESULT eDVBServiceList::getContent(PyObject *list, bool sorted)
+{
+       eServiceReferenceDVB ref;
+
+       if (!m_query || !list || !PyList_Check(list))
+               return -1;
+
+       std::list<eServiceReferenceDVB> tmplist;
+
+       while (!m_query->getNextResult(ref))
+               tmplist.push_back(ref);
+
+       if (sorted)
+               tmplist.sort(iListableServiceCompare(this));
+
+       for (std::list<eServiceReferenceDVB>::iterator it(tmplist.begin());
+               it != tmplist.end(); ++it)
+       {
+               PyObject *refobj = New_eServiceReference(*it);
+               PyList_Append(list, refobj);
+               Py_DECREF(refobj);
+       }
+       return 0;
+}
+
+RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list, bool sorted)
 {
        eServiceReferenceDVB ref;
        
@@ -314,6 +384,10 @@ RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
        
        while (!m_query->getNextResult(ref))
                list.push_back(ref);
+
+       if (sorted)
+               list.sort(iListableServiceCompare(this));
+
        return 0;
 }
 
@@ -379,6 +453,13 @@ RESULT eDVBServiceList::flushChanges()
        return m_bouquet->flushChanges();
 }
 
+RESULT eDVBServiceList::setListName(const std::string &name)
+{
+       if (!m_bouquet)
+               return -1;
+       return m_bouquet->setListName(name);
+}
+
 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
 {
        ePtr<eDVBService> service;
@@ -485,13 +566,19 @@ RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServ
 }
 
 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service): 
-       m_reference(ref), m_dvb_service(service), m_service_handler(0), m_is_paused(0)
+       m_reference(ref), m_dvb_service(service), m_is_paused(0)
 {
        m_is_pvr = !ref.path.empty();
-       m_timeshift_enabled = 0;
+       
+       m_timeshift_enabled = m_timeshift_active = 0;
+       m_skipmode = 0;
        
        CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
+       CONNECT(m_service_handler_timeshift.serviceEvent, eDVBServicePlay::serviceEventTimeshift);
        CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
+
+       m_cuesheet_changed = 0;
+       m_cutlist_enabled = 1;
 }
 
 eDVBServicePlay::~eDVBServicePlay()
@@ -521,7 +608,7 @@ void eDVBServicePlay::serviceEvent(int event)
        case eDVBServicePMTHandler::eventTuned:
        {
                ePtr<iDVBDemux> m_demux;
-               if (!m_service_handler.getDemux(m_demux))
+               if (!m_service_handler.getDataDemux(m_demux))
                {
                        eServiceReferenceDVB &ref = (eServiceReferenceDVB&) m_reference;
                        int sid = ref.getParentServiceID().get();
@@ -543,111 +630,100 @@ void eDVBServicePlay::serviceEvent(int event)
        }
        case eDVBServicePMTHandler::eventNewProgramInfo:
        {
-               int vpid = -1, apid = -1, apidtype = -1, pcrpid = -1;
-               eDVBServicePMTHandler::program program;
-               if (m_service_handler.getProgramInfo(program))
-                       eDebug("getting program info failed.");
-               else
+               eDebug("eventNewProgramInfo %d %d", m_timeshift_enabled, m_timeshift_active);
+               if (m_timeshift_enabled)
+                       updateTimeshiftPids();
+               if (!m_timeshift_active)
+                       updateDecoder();
+               if (m_first_program_info && m_is_pvr)
                {
-                       eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
-                       if (!program.videoStreams.empty())
-                       {
-                               eDebugNoNewLine(" (");
-                               for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
-                                       i(program.videoStreams.begin()); 
-                                       i != program.videoStreams.end(); ++i)
-                               {
-                                       if (vpid == -1)
-                                               vpid = i->pid;
-                                       if (i != program.videoStreams.begin())
-                                               eDebugNoNewLine(", ");
-                                       eDebugNoNewLine("%04x", i->pid);
-                               }
-                               eDebugNoNewLine(")");
-                       }
-                       eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
-                       if (!program.audioStreams.empty())
-                       {
-                               eDebugNoNewLine(" (");
-                               for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
-                                       i(program.audioStreams.begin()); 
-                                       i != program.audioStreams.end(); ++i)
-                               {
-                                       if (apid == -1)
-                                       {
-                                               apid = i->pid;
-                                               apidtype = i->type;
-                                       }
-                                       if (i != program.audioStreams.begin())
-                                               eDebugNoNewLine(", ");
-                                       eDebugNoNewLine("%04x", i->pid);
-                               }
-                               eDebugNoNewLine(")");
-                       }
-                       eDebug(", and the pcr pid is %04x", program.pcrPid);
-                       if (program.pcrPid != 0x1fff)
-                               pcrpid = program.pcrPid;
-               }
-               
-               if (!m_decoder)
-               {
-                       ePtr<iDVBDemux> demux;
-                       m_service_handler.getDemux(demux);
-                       if (demux)
-                               demux->getMPEGDecoder(m_decoder);
-               }
-
-               if (m_decoder)
-               {
-                       m_decoder->setVideoPID(vpid);
-                       m_current_audio_stream = 0;
-                       m_decoder->setAudioPID(apid, apidtype);
-                       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..
-                               
-                               /* don't worry about non-existing services, nor pvr services */
-                       if (m_dvb_service && !m_is_pvr)
-                       {
-                               if (apidtype == eDVBAudio::aMPEG)
-                               {
-                                       m_dvb_service->setCachePID(eDVBService::cAPID, apid);
-                                       m_dvb_service->setCachePID(eDVBService::cAC3PID, -1);
-                               }
-                               else
-                               {
-                                       m_dvb_service->setCachePID(eDVBService::cAPID, -1);
-                                       m_dvb_service->setCachePID(eDVBService::cAC3PID, apid);
-                               }
-                               m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
-                               m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
-                       }
+                       m_first_program_info = 0;
+                       seekTo(0);
                }
-
                m_event((iPlayableService*)this, evUpdatedInfo);
                break;
        }
+       case eDVBServicePMTHandler::eventEOF:
+               m_event((iPlayableService*)this, evEOF);
+               break;
+       case eDVBServicePMTHandler::eventSOF:
+               m_event((iPlayableService*)this, evSOF);
+               break;
+       }
+}
+
+void eDVBServicePlay::serviceEventTimeshift(int event)
+{
+       switch (event)
+       {
+       case eDVBServicePMTHandler::eventNewProgramInfo:
+               if (m_timeshift_active)
+                       updateDecoder();
+               break;
+       case eDVBServicePMTHandler::eventSOF:
+               m_event((iPlayableService*)this, evSOF);
+               break;
+       case eDVBServicePMTHandler::eventEOF:
+               switchToLive();
+               break;
        }
 }
 
 RESULT eDVBServicePlay::start()
 {
        int r;
-       eDebug("starting DVB service");
-       r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
-       eDebug("tune result: %d", r);
+               /* in pvr mode, we only want to use one demux. in tv mode, we're using 
+                  two (one for decoding, one for data source), as we must be prepared
+                  to start recording from the data demux. */
+       m_cue = new eCueSheet();
+
+       m_first_program_info = 1;
+       eServiceReferenceDVB &service = (eServiceReferenceDVB&)m_reference;
+       r = m_service_handler.tune(service, m_is_pvr, m_cue);
+       
+               /* inject EIT if there is a stored one */
+       if (m_is_pvr)
+       {
+               std::string filename = service.path;
+               filename.erase(filename.length()-2, 2);
+               filename+="eit";
+               int fd = ::open( filename.c_str(), O_RDONLY );
+               if ( fd > -1 )
+               {
+                       __u8 buf[4096];
+                       int rd = ::read(fd, buf, 4096);
+                       ::close(fd);
+                       if ( rd > 12 /*EIT_LOOP_SIZE*/ )
+                       {
+                               Event ev(buf);
+                               ePtr<eServiceEvent> event = new eServiceEvent;
+                               ePtr<eServiceEvent> empty;
+                               event->parseFrom(&ev, (service.getTransportStreamID().get()<<16)|service.getOriginalNetworkID().get());
+                               m_event_handler.inject(event, 0);
+                               m_event_handler.inject(empty, 1);
+                               eDebug("injected");
+                       }
+               }
+       }
+       
+       if (m_is_pvr)
+               loadCuesheet();
+
        m_event(this, evStart);
+       m_event((iPlayableService*)this, evSeekableStatusChanged);
        return 0;
 }
 
 RESULT eDVBServicePlay::stop()
 {
-       eDebug("stopping..");
+       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();
+       
        return 0;
 }
 
@@ -659,7 +735,10 @@ RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &ev
 
 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
 {
-       if (!m_is_pvr)
+               /* note: we check for timeshift to be enabled,
+                  not neccessary active. if you pause when timeshift
+                  is not active, you should activate it when unpausing */
+       if ((!m_is_pvr) && (!m_timeshift_enabled))
        {
                ptr = 0;
                return -1;
@@ -679,15 +758,44 @@ RESULT eDVBServicePlay::setSlowMotion(int ratio)
 
 RESULT eDVBServicePlay::setFastForward(int ratio)
 {
-       if (m_decoder)
-               return m_decoder->setFastForward(ratio);
-       else
+       int skipmode, ffratio;
+       
+       if (ratio > 8)
+       {
+               skipmode = ratio;
+               ffratio = 1;
+       } else if (ratio > 0)
+       {
+               skipmode = 0;
+               ffratio = ratio;
+       } else if (!ratio)
+       {
+               skipmode = 0;
+               ffratio = 0;
+       } else // if (ratio < 0)
+       {
+               skipmode = ratio;
+               ffratio = 1;
+       }
+
+       if (m_skipmode != skipmode)
+       {
+               eDebug("setting cue skipmode to %d", skipmode);
+               if (m_cue)
+                       m_cue->setSkipmode(skipmode * 90000); /* convert to 90000 per second */
+       }
+       
+       m_skipmode = skipmode;
+       
+       if (!m_decoder)
                return -1;
+
+       return m_decoder->setFastForward(ffratio);
 }
     
 RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
 {
-       if (m_is_pvr)
+       if (m_is_pvr || m_timeshift_enabled)
        {
                ptr = this;
                return 0;
@@ -697,15 +805,13 @@ RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
        return -1;
 }
 
+       /* TODO: when timeshift is enabled but not active, this doesn't work. */
 RESULT eDVBServicePlay::getLength(pts_t &len)
 {
        ePtr<iDVBPVRChannel> pvr_channel;
        
-       if (m_service_handler.getPVRChannel(pvr_channel))
-       {
-               eDebug("getPVRChannel failed!");
+       if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
                return -1;
-       }
        
        return pvr_channel->getLength(len);
 }
@@ -733,52 +839,71 @@ RESULT eDVBServicePlay::unpause()
 RESULT eDVBServicePlay::seekTo(pts_t to)
 {
        eDebug("eDVBServicePlay::seekTo: jump %lld", to);
+       
+       if (!m_decode_demux)
+               return -1;
 
        ePtr<iDVBPVRChannel> pvr_channel;
        
-       if (m_service_handler.getPVRChannel(pvr_channel))
+       if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
                return -1;
        
-       ePtr<iDVBDemux> demux;
-       m_service_handler.getDemux(demux);
-       if (!demux)
+       if (!m_cue)
                return -1;
        
-       return pvr_channel->seekTo(demux, 0, to);
+       m_cue->seekTo(0, to);
+       return 0;
 }
 
 RESULT eDVBServicePlay::seekRelative(int direction, pts_t to)
 {
        eDebug("eDVBServicePlay::seekRelative: jump %d, %lld", direction, to);
+       
+       if (!m_decode_demux)
+               return -1;
 
        ePtr<iDVBPVRChannel> pvr_channel;
        
-       if (m_service_handler.getPVRChannel(pvr_channel))
+       if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
                return -1;
        
+       int mode = 1;
+       
+                       /* HACK until we have skip-AP api */
+       if ((to > 0) && (to < 100))
+               mode = 2;
+       
        to *= direction;
        
-       ePtr<iDVBDemux> demux;
-       m_service_handler.getDemux(demux);
-       if (!demux)
-               return -1;
+       if (!m_cue)
+               return 0;
        
-       return pvr_channel->seekTo(demux, 1, to);
+       m_cue->seekTo(mode, to);
+       return 0;
 }
 
 RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
 {
        ePtr<iDVBPVRChannel> pvr_channel;
        
-       if (m_service_handler.getPVRChannel(pvr_channel))
+       if (!m_decode_demux)
                return -1;
        
-       ePtr<iDVBDemux> demux;
-       m_service_handler.getDemux(demux);
-       if (!demux)
+       if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
                return -1;
        
-       return pvr_channel->getCurrentPosition(demux, pos, 1);
+       int r = 0;
+
+               /* if there is a decoder, use audio or video PTS */
+       if (m_decoder)
+       {
+               r = m_decoder->getPTS(0, pos);
+               if (r)
+                       return r;
+       }
+       
+               /* fixup */
+       return pvr_channel->getCurrentPosition(m_decode_demux, pos, m_decoder ? 1 : 0);
 }
 
 RESULT eDVBServicePlay::setTrickmode(int trick)
@@ -788,6 +913,11 @@ RESULT eDVBServicePlay::setTrickmode(int trick)
        return 0;
 }
 
+RESULT eDVBServicePlay::isCurrentlySeekable()
+{
+       return m_is_pvr || m_timeshift_active;
+}
+
 RESULT eDVBServicePlay::frontendStatusInfo(ePtr<iFrontendStatusInformation> &ptr)
 {
        ptr = this;
@@ -814,7 +944,34 @@ RESULT eDVBServicePlay::subServices(ePtr<iSubserviceList> &ptr)
 
 RESULT eDVBServicePlay::timeshift(ePtr<iTimeshiftService> &ptr)
 {
+       ptr = 0;
        if (m_timeshift_enabled || !m_is_pvr)
+       {
+               if (!m_timeshift_enabled)
+               {
+                               /* we need enough diskspace */
+                       struct statfs fs;
+                       if (statfs(TSPATH "/.", &fs) < 0)
+                       {
+                               eDebug("statfs failed!");
+                               return -2;
+                       }
+               
+                       if (((off_t)fs.f_bavail) * ((off_t)fs.f_bsize) < 1024*1024*1024LL)
+                       {
+                               eDebug("not enough diskspace for timeshift! (less than 1GB)");
+                               return -3;
+                       }
+               }
+               ptr = this;
+               return 0;
+       }
+       return -1;
+}
+
+RESULT eDVBServicePlay::cueSheet(ePtr<iCueSheet> &ptr)
+{
+       if (m_is_pvr)
        {
                ptr = this;
                return 0;
@@ -825,6 +982,11 @@ RESULT eDVBServicePlay::timeshift(ePtr<iTimeshiftService> &ptr)
 
 RESULT eDVBServicePlay::getName(std::string &name)
 {
+       if (m_is_pvr)
+       {
+               ePtr<iStaticServiceInformation> i = new eStaticServiceDVBPVRInformation(m_reference);
+               return i->getName(m_reference, name);
+       }
        if (m_dvb_service)
        {
                m_dvb_service->getName(m_reference, name);
@@ -896,7 +1058,7 @@ int eDVBServicePlay::getInfo(int w)
        case sAudioPID: if (program.audioStreams.empty()) return -1; return program.audioStreams[m_current_audio_stream].pid;
        case sPCRPID: return program.pcrPid;
        case sPMTPID: return program.pmtPid;
-       case sTXTPID: return -1;
+       case sTXTPID: return program.textPid;
        case sSID: return ((const eServiceReferenceDVB&)m_reference).getServiceID().get();
        case sONID: return ((const eServiceReferenceDVB&)m_reference).getOriginalNetworkID().get();
        case sTSID: return ((const eServiceReferenceDVB&)m_reference).getTransportStreamID().get();
@@ -991,18 +1153,14 @@ int eDVBServicePlay::selectAudioStream(int i)
 
        if (m_dvb_service && !m_is_pvr)
        {
-               if (m_dvb_service && !m_is_pvr)
+               if (program.audioStreams[i].type == eDVBAudio::aMPEG)
                {
-                       if (program.audioStreams[i].type == eDVBAudio::aMPEG)
-                       {
-                               m_dvb_service->setCachePID(eDVBService::cAPID, program.audioStreams[i].pid);
-                               m_dvb_service->setCachePID(eDVBService::cAC3PID, -1);
-                       }
-                       else
-                       {
-                               m_dvb_service->setCachePID(eDVBService::cAPID, -1);
-                               m_dvb_service->setCachePID(eDVBService::cAC3PID, program.audioStreams[i].pid);
-                       }
+                       m_dvb_service->setCachePID(eDVBService::cAPID, program.audioStreams[i].pid);
+                       m_dvb_service->setCachePID(eDVBService::cAC3PID, -1);
+               }       else
+               {
+                       m_dvb_service->setCachePID(eDVBService::cAPID, -1);
+                       m_dvb_service->setCachePID(eDVBService::cAC3PID, program.audioStreams[i].pid);
                }
        }
 
@@ -1024,6 +1182,52 @@ int eDVBServicePlay::getFrontendInfo(int w)
        return fe->readFrontendData(w);
 }
 
+PyObject *eDVBServicePlay::getFrontendData(bool original)
+{
+       PyObject *ret=0;
+
+       eUsePtr<iDVBChannel> channel;
+       if(!m_service_handler.getChannel(channel))
+       {
+               ePtr<iDVBFrontend> fe;
+               if(!channel->getFrontend(fe))
+               {
+                       ret = fe->readTransponderData(original);
+                       if (ret)
+                       {
+                               ePtr<iDVBFrontendParameters> feparm;
+                               channel->getCurrentFrontendParameters(feparm);
+                               if (feparm)
+                               {
+                                       eDVBFrontendParametersSatellite osat;
+                                       if (!feparm->getDVBS(osat))
+                                       {
+                                               void PutToDict(PyObject *, const char*, long);
+                                               void PutToDict(PyObject *, const char*, const char*);
+                                               PutToDict(ret, "orbital_position", osat.orbital_position);
+                                               const char *tmp = "UNKNOWN";
+                                               switch(osat.polarisation)
+                                               {
+                                                       case eDVBFrontendParametersSatellite::Polarisation::Horizontal: tmp="HORIZONTAL"; break;
+                                                       case eDVBFrontendParametersSatellite::Polarisation::Vertical: tmp="VERTICAL"; break;
+                                                       case eDVBFrontendParametersSatellite::Polarisation::CircularLeft: tmp="CIRCULAR_LEFT"; break;
+                                                       case eDVBFrontendParametersSatellite::Polarisation::CircularRight: tmp="CIRCULAR_RIGHT"; break;
+                                                       default:break;
+                                               }
+                                               PutToDict(ret, "polarization", tmp);
+                                       }
+                               }
+                       }
+               }
+       }
+       if (!ret)
+       {
+               ret = Py_None;
+               Py_INCREF(ret);
+       }
+       return ret;
+}
+
 int eDVBServicePlay::getNumberOfSubservices()
 {
        ePtr<eServiceEvent> evt;
@@ -1046,9 +1250,40 @@ RESULT eDVBServicePlay::getSubservice(eServiceReference &sub, unsigned int n)
 
 RESULT eDVBServicePlay::startTimeshift()
 {
+       ePtr<iDVBDemux> demux;
+       
+       eDebug("Start timeshift!");
+       
        if (m_timeshift_enabled)
                return -1;
-       eDebug("TIMESHIFT - start!");
+       
+               /* start recording with the data demux. */
+       if (m_service_handler.getDataDemux(demux))
+               return -2;
+
+       demux->createTSRecorder(m_record);
+       if (!m_record)
+               return -3;
+
+       char templ[]=TSPATH "/timeshift.XXXXXX";
+       m_timeshift_fd = mkstemp(templ);
+       m_timeshift_file = templ;
+       
+       eDebug("recording to %s", templ);
+       
+       if (m_timeshift_fd < 0)
+       {
+               m_record = 0;
+               return -4;
+       }
+               
+       m_record->setTargetFD(m_timeshift_fd);
+
+       m_timeshift_enabled = 1;
+       
+       updateTimeshiftPids();
+       m_record->start();
+
        return 0;
 }
 
@@ -1056,11 +1291,400 @@ RESULT eDVBServicePlay::stopTimeshift()
 {
        if (!m_timeshift_enabled)
                return -1;
+       
+       switchToLive();
+       
        m_timeshift_enabled = 0;
-       eDebug("timeshift disabled");
+       
+       m_record->stop();
+       m_record = 0;
+       
+       close(m_timeshift_fd);
+       eDebug("remove timeshift file");
+       remove(m_timeshift_file.c_str());
+       
        return 0;
 }
 
+int eDVBServicePlay::isTimeshiftActive()
+{
+       return m_timeshift_enabled && m_timeshift_active;
+}
+
+RESULT eDVBServicePlay::activateTimeshift()
+{
+       if (!m_timeshift_enabled)
+               return -1;
+       
+       if (!m_timeshift_active)
+       {
+               switchToTimeshift();
+               return 0;
+       }
+       
+       return -2;
+}
+
+PyObject *eDVBServicePlay::getCutList()
+{
+       PyObject *list = PyList_New(0);
+       
+       for (std::multiset<struct cueEntry>::iterator i(m_cue_entries.begin()); i != m_cue_entries.end(); ++i)
+       {
+               PyObject *tuple = PyTuple_New(2);
+               PyTuple_SetItem(tuple, 0, PyLong_FromLongLong(i->where));
+               PyTuple_SetItem(tuple, 1, PyInt_FromLong(i->what));
+               PyList_Append(list, tuple);
+               Py_DECREF(tuple);
+       }
+       
+       return list;
+}
+
+void eDVBServicePlay::setCutList(PyObject *list)
+{
+       if (!PyList_Check(list))
+               return;
+       int size = PyList_Size(list);
+       int i;
+       
+       m_cue_entries.clear();
+       
+       for (i=0; i<size; ++i)
+       {
+               PyObject *tuple = PyList_GetItem(list, i);
+               if (!PyTuple_Check(tuple))
+               {
+                       eDebug("non-tuple in cutlist");
+                       continue;
+               }
+               if (PyTuple_Size(tuple) != 2)
+               {
+                       eDebug("cutlist entries need to be a 2-tuple");
+                       continue;
+               }
+               PyObject *ppts = PyTuple_GetItem(tuple, 0), *ptype = PyTuple_GetItem(tuple, 1);
+               if (!(PyLong_Check(ppts) && PyInt_Check(ptype)))
+               {
+                       eDebug("cutlist entries need to be (pts, type)-tuples (%d %d)", PyLong_Check(ppts), PyInt_Check(ptype));
+                       continue;
+               }
+               pts_t pts = PyLong_AsLongLong(ppts);
+               int type = PyInt_AsLong(ptype);
+               m_cue_entries.insert(cueEntry(pts, type));
+               eDebug("adding %08llx, %d", pts, type);
+       }
+       m_cuesheet_changed = 1;
+       
+       cutlistToCuesheet();
+       m_event((iPlayableService*)this, evCuesheetChanged);
+}
+
+void eDVBServicePlay::setCutListEnable(int enable)
+{
+       m_cutlist_enabled = enable;
+       cutlistToCuesheet();
+}
+
+void eDVBServicePlay::updateTimeshiftPids()
+{
+       if (!m_record)
+               return;
+       
+       eDVBServicePMTHandler::program program;
+       if (m_service_handler.getProgramInfo(program))
+               return;
+       else
+       {
+               std::set<int> pids_to_record;
+               pids_to_record.insert(0); // PAT
+               if (program.pmtPid != -1)
+                       pids_to_record.insert(program.pmtPid); // PMT
+
+               if (program.textPid != -1)
+                       pids_to_record.insert(program.textPid); // Videotext
+
+               for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
+                       i(program.videoStreams.begin()); 
+                       i != program.videoStreams.end(); ++i)
+                       pids_to_record.insert(i->pid);
+
+               for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
+                       i(program.audioStreams.begin()); 
+                       i != program.audioStreams.end(); ++i)
+                               pids_to_record.insert(i->pid);
+
+               std::set<int> new_pids, obsolete_pids;
+               
+               std::set_difference(pids_to_record.begin(), pids_to_record.end(), 
+                               m_pids_active.begin(), m_pids_active.end(),
+                               std::inserter(new_pids, new_pids.begin()));
+               
+               std::set_difference(
+                               m_pids_active.begin(), m_pids_active.end(),
+                               pids_to_record.begin(), pids_to_record.end(), 
+                               std::inserter(new_pids, new_pids.begin())
+                               );
+
+               for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
+                       m_record->addPID(*i);
+
+               for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
+                       m_record->removePID(*i);
+       }
+}
+
+void eDVBServicePlay::switchToLive()
+{
+       if (!m_timeshift_active)
+               return;
+       
+       m_decoder = 0;
+       m_decode_demux = 0;
+               /* free the timeshift service handler, we need the resources */
+       m_service_handler_timeshift.free();
+       m_timeshift_active = 0;
+       
+       m_event((iPlayableService*)this, evSeekableStatusChanged);
+       
+       updateDecoder();
+}
+
+void eDVBServicePlay::switchToTimeshift()
+{
+       if (m_timeshift_active)
+               return;
+       
+       m_decode_demux = 0;
+       m_decoder = 0;
+       
+       m_timeshift_active = 1;
+
+       m_event((iPlayableService*)this, evSeekableStatusChanged);
+       
+       eServiceReferenceDVB r = (eServiceReferenceDVB&)m_reference;
+       r.path = m_timeshift_file;
+       
+       m_service_handler_timeshift.tune(r, 1, m_cue); /* use the decoder demux for everything */
+       updateDecoder(); /* mainly to switch off PCR */
+}
+
+void eDVBServicePlay::updateDecoder()
+{
+       int vpid = -1, apid = -1, apidtype = -1, pcrpid = -1, tpid = -1;
+       eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
+
+       eDVBServicePMTHandler::program program;
+       if (h.getProgramInfo(program))
+               eDebug("getting program info failed.");
+       else
+       {
+               eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
+               if (!program.videoStreams.empty())
+               {
+                       eDebugNoNewLine(" (");
+                       for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
+                               i(program.videoStreams.begin()); 
+                               i != program.videoStreams.end(); ++i)
+                       {
+                               if (vpid == -1)
+                                       vpid = i->pid;
+                               if (i != program.videoStreams.begin())
+                                       eDebugNoNewLine(", ");
+                               eDebugNoNewLine("%04x", i->pid);
+                       }
+                       eDebugNoNewLine(")");
+               }
+               eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
+               if (!program.audioStreams.empty())
+               {
+                       eDebugNoNewLine(" (");
+                       for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
+                               i(program.audioStreams.begin()); 
+                               i != program.audioStreams.end(); ++i)
+                       {
+                               if (apid == -1)
+                               {
+                                       apid = i->pid;
+                                       apidtype = i->type;
+                               }
+                               if (i != program.audioStreams.begin())
+                                       eDebugNoNewLine(", ");
+                               eDebugNoNewLine("%04x", i->pid);
+                       }
+                       eDebugNoNewLine(")");
+               }
+               eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
+               pcrpid = program.pcrPid;
+               eDebug(", and the text pid is %04x", program.textPid);
+               tpid = program.textPid;
+       }
+
+       if (!m_decoder)
+       {
+               h.getDecodeDemux(m_decode_demux);
+               if (m_decode_demux)
+                       m_decode_demux->getMPEGDecoder(m_decoder);
+               if (m_cue)
+                       m_cue->setDecodingDemux(m_decode_demux, m_decoder);
+       }
+
+       if (m_decoder)
+       {
+               m_decoder->setVideoPID(vpid);
+               m_current_audio_stream = 0;
+               m_decoder->setAudioPID(apid, apidtype);
+               if (!(m_is_pvr || m_timeshift_active))
+                       m_decoder->setSyncPCR(pcrpid);
+               else
+                       m_decoder->setSyncPCR(-1);
+               m_decoder->setTextPID(tpid);
+               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..
+                               
+               /* don't worry about non-existing services, nor pvr services */
+               if (m_dvb_service && !m_is_pvr)
+               {
+                       if (apidtype == eDVBAudio::aMPEG)
+                       {
+                               m_dvb_service->setCachePID(eDVBService::cAPID, apid);
+                               m_dvb_service->setCachePID(eDVBService::cAC3PID, -1);
+                       }
+                       else
+                       {
+                               m_dvb_service->setCachePID(eDVBService::cAPID, -1);
+                               m_dvb_service->setCachePID(eDVBService::cAC3PID, apid);
+                       }
+                       m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
+                       m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
+                       m_dvb_service->setCachePID(eDVBService::cTPID, tpid);
+               }
+       }
+}
+
+void eDVBServicePlay::loadCuesheet()
+{
+       std::string filename = m_reference.path + ".cuts";
+       
+       m_cue_entries.clear();
+
+       FILE *f = fopen(filename.c_str(), "rb");
+
+       if (f)
+       {
+               eDebug("loading cuts..");
+               while (1)
+               {
+                       unsigned long long where;
+                       unsigned int what;
+                       
+                       if (!fread(&where, sizeof(where), 1, f))
+                               break;
+                       if (!fread(&what, sizeof(what), 1, f))
+                               break;
+                       
+#if BYTE_ORDER == LITTLE_ENDIAN
+                       where = bswap_64(where);
+#endif
+                       what = ntohl(what);
+                       
+                       if (what > 2)
+                               break;
+                       
+                       m_cue_entries.insert(cueEntry(where, what));
+               }
+               fclose(f);
+               eDebug("%d entries", m_cue_entries.size());
+       } else
+               eDebug("cutfile not found!");
+       
+       m_cuesheet_changed = 0;
+       cutlistToCuesheet();
+       m_event((iPlayableService*)this, evCuesheetChanged);
+}
+
+void eDVBServicePlay::saveCuesheet()
+{
+       std::string filename = m_reference.path + ".cuts";
+       
+       FILE *f = fopen(filename.c_str(), "wb");
+
+       if (f)
+       {
+               unsigned long long where;
+               int what;
+
+               for (std::multiset<cueEntry>::iterator i(m_cue_entries.begin()); i != m_cue_entries.end(); ++i)
+               {
+#if BYTE_ORDER == BIG_ENDIAN
+                       where = i->where;
+#else
+                       where = bswap_64(i->where);
+#endif
+                       what = htonl(i->what);
+                       fwrite(&where, sizeof(where), 1, f);
+                       fwrite(&what, sizeof(what), 1, f);
+                       
+               }
+               fclose(f);
+       }
+       
+       m_cuesheet_changed = 0;
+}
+
+void eDVBServicePlay::cutlistToCuesheet()
+{
+       if (!m_cue)
+       {
+               eDebug("no cue sheet");
+               return;
+       }       
+       m_cue->clear();
+       
+       if (!m_cutlist_enabled)
+       {
+               m_cue->commitSpans();
+               eDebug("cutlists where disabled");
+               return;
+       }
+
+       pts_t in = 0, out = 0, length = 0;
+       
+       getLength(length);
+               
+       std::multiset<cueEntry>::iterator i(m_cue_entries.begin());
+       
+       while (1)
+       {
+               if (i == m_cue_entries.end())
+                       out = length;
+               else {
+                       if (i->what == 0) /* in */
+                       {
+                               in = i++->where;
+                               continue;
+                       } else if (i->what == 1) /* out */
+                               out = i++->where;
+                       else /* mark */
+                       {
+                               i++;
+                               continue;
+                       }
+               }
+               
+               if (in != out)
+                       m_cue->addSourceSpan(in, out);
+               
+               in = length;
+               
+               if (i == m_cue_entries.end())
+                       break;
+       }
+       m_cue->commitSpans();
+}
+
 DEFINE_REF(eDVBServicePlay)
 
 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");