remove __init__.py files and create them on the fly
[enigma2.git] / lib / service / servicedvbrecord.cpp
index 773aeb39ed9f6d0602539809502272a431725023..bded7cc9c757b602a86934b08434688e70c6802b 100644 (file)
@@ -10,6 +10,7 @@ eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref
        CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
        m_state = stateIdle;
        m_want_record = 0;
+       m_tuned = 0;
 }
 
 void eDVBServiceRecord::serviceEvent(int event)
@@ -20,6 +21,9 @@ void eDVBServiceRecord::serviceEvent(int event)
        case eDVBServicePMTHandler::eventTuned:
        {
                eDebug("tuned..");
+               m_tuned = 1;
+               if (m_state == stateRecording && m_want_record)
+                       doRecord();
                break;
        }
        case eDVBServicePMTHandler::eventNewProgramInfo:
@@ -33,11 +37,11 @@ void eDVBServiceRecord::serviceEvent(int event)
        }
 }
 
-
-RESULT eDVBServiceRecord::prepare()
+RESULT eDVBServiceRecord::prepare(const char *filename)
 {
+       m_filename = filename;
        if (m_state == stateIdle)
-               return m_service_handler.tune(m_ref);
+               return doPrepare();
        else
                return -1;
 }
@@ -55,7 +59,8 @@ RESULT eDVBServiceRecord::stop()
        eDebug("stop recording!!");
        if (m_state == stateRecording)
        {
-               m_record->stop();
+               if (m_record)
+                       m_record->stop();
                m_state = statePrepared;
        }
        
@@ -73,15 +78,39 @@ int eDVBServiceRecord::doPrepare()
                /* allocate a ts recorder if we don't already have one. */
        if (m_state == stateIdle)
        {
-               ::remove("recordings.ts");
-               int fd = ::open("recording.ts", O_WRONLY|O_CREAT, 0644);
+               m_pids_active.clear();
+               m_state = statePrepared;
+               return m_service_handler.tune(m_ref, 0);
+       }
+       return 0;
+}
+
+int eDVBServiceRecord::doRecord()
+{
+       int err = doPrepare();
+       if (err)
+               return err;
+       
+       if (!m_tuned)
+               return 0; /* try it again when we are tuned in */
+       
+       if (!m_record && m_tuned)
+       {
+
+               eDebug("Recording to %s...", m_filename.c_str());
+               ::remove(m_filename.c_str());
+               int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
                if (fd == -1)
                {
-                       eDebug("eDVBServiceRecord - can't open hardcoded recording file!");
+                       eDebug("eDVBServiceRecord - can't open recording file!");
                        return -1;
                }
+               
+                       /* turn off kernel caching strategies */
+               posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
+               
                ePtr<iDVBDemux> demux;
-               if (m_service_handler.getDemux(demux))
+               if (m_service_handler.getDataDemux(demux))
                {
                        eDebug("eDVBServiceRecord - NO DEMUX available!");
                        return -2;
@@ -93,22 +122,8 @@ int eDVBServiceRecord::doPrepare()
                        return -3;
                }
                m_record->setTargetFD(fd);
-               m_pids_active.clear();
-               m_state = statePrepared;
-       } else if ((m_state == statePrepared) || (m_state == stateRecording))
-       {
-                       /* when we're already recording, we already have a recorder allocated. */
-               assert(m_record);
+               m_record->setTargetFilename(m_filename.c_str());
        }
-       return 0;
-}
-
-int eDVBServiceRecord::doRecord()
-{
-       int err = doPrepare();
-       if (err)
-               return err;
-       
        eDebug("starting recording..");
        
        eDVBServicePMTHandler::program program;
@@ -118,6 +133,13 @@ int eDVBServiceRecord::doRecord()
        {
                std::set<int> pids_to_record;
                
+               pids_to_record.insert(0); // PAT
+               
+               if (program.pmtPid != -1)
+                       pids_to_record.insert(program.pmtPid); // PMT
+               
+               int timing_pid = -1;
+               
                eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
                if (!program.videoStreams.empty())
                {
@@ -127,6 +149,10 @@ int eDVBServiceRecord::doRecord()
                                i != program.videoStreams.end(); ++i)
                        {
                                pids_to_record.insert(i->pid);
+                               
+                               if (timing_pid == -1)
+                                       timing_pid = i->pid;
+                               
                                if (i != program.videoStreams.begin())
                                        eDebugNoNewLine(", ");
                                eDebugNoNewLine("%04x", i->pid);
@@ -142,16 +168,23 @@ int eDVBServiceRecord::doRecord()
                                i != program.audioStreams.end(); ++i)
                        {
                                pids_to_record.insert(i->pid);
+
+                               if (timing_pid == -1)
+                                       timing_pid = i->pid;
+                               
                                if (i != program.audioStreams.begin())
                                        eDebugNoNewLine(", ");
                                eDebugNoNewLine("%04x", i->pid);
                        }
                        eDebugNoNewLine(")");
                }
-               eDebug(", and the pcr pid is %04x", program.pcrPid);
+               eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
                if (program.pcrPid != 0x1fff)
                        pids_to_record.insert(program.pcrPid);
-               
+               eDebug(", and the text pid is %04x", program.textPid);
+               if (program.textPid != -1)
+                       pids_to_record.insert(program.textPid); // Videotext
+
                        /* find out which pids are NEW and which pids are obsolete.. */
                std::set<int> new_pids, obsolete_pids;
                
@@ -176,6 +209,11 @@ int eDVBServiceRecord::doRecord()
                        m_record->removePID(*i);
                }
                
+               if (timing_pid != -1)
+                       m_record->setTimingPID(timing_pid);
+               
+               m_pids_active = pids_to_record;
+               
                if (m_state != stateRecording)
                {
                        m_record->start();