don't kill if thread isn't alive
[enigma2.git] / lib / service / servicedvbrecord.cpp
index c72b81d39e291a40c751e21df96279ed2e2edfa8..f4bf557726cbae7de71be225594cddd0f2f4c92a 100644 (file)
@@ -8,7 +8,9 @@ DEFINE_REF(eDVBServiceRecord);
 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref)
 {
        CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
-       m_recording = 0;
+       m_state = stateIdle;
+       m_want_record = 0;
+       m_tuned = 0;
 }
 
 void eDVBServiceRecord::serviceEvent(int event)
@@ -19,133 +21,186 @@ 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:
        {
-                       /* allocate a ts recorder if we don't already have one. */
-               if (!m_recording)
-               {
-                       ::remove("recordings.ts");
-                       int fd = ::open("recording.ts", O_WRONLY|O_CREAT, 0644);
-                       if (fd == -1)
-                       {
-                               eDebug("eDVBServiceRecord - can't open hardcoded recording file!");
-                               return;
-                       }
-                       ePtr<iDVBDemux> demux;
-                       if (m_service_handler.getDemux(demux))
-                       {
-                               eDebug("eDVBServiceRecord - NO DEMUX available!");
-                               return;
-                       }
-                       demux->createTSRecorder(m_record);
-                       if (!m_record)
-                       {
-                               eDebug("eDVBServiceRecord - no ts recorder available.");
-                               return;
-                       }
-                       m_record->setTargetFD(fd);
-                       m_pids_active.clear();
-               } else
-               {
-                               /* when we're already recording, we already have a recorder allocated. */
-                       assert(m_record);
-               }
-               
-
-               eDVBServicePMTHandler::program program;
-               if (m_service_handler.getProgramInfo(program))
-                       eDebug("getting program info failed.");
-               else
-               {
-                       std::set<int> pids_to_record;
-                       
-                       eDebugNoNewLine("RECORD: 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)
-                               {
-                                       pids_to_record.insert(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)
-                               {
-                                       pids_to_record.insert(i->pid);
-                                       if (i != program.audioStreams.begin())
-                                               eDebugNoNewLine(", ");
-                                       eDebugNoNewLine("%04x", i->pid);
-                               }
-                               eDebugNoNewLine(")");
-                       }
-                       eDebug(", and the pcr pid is %04x", program.pcrPid);
-                       if (program.pcrPid != 0x1fff)
-                               pids_to_record.insert(program.pcrPid);
-                       
-                               /* find out which pids are NEW and which pids are obsolete.. */
-                       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)
-                       {
-                               eDebug("ADD PID: %04x", *i);
-                               m_record->addPID(*i);
-                       }
-                       for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
-                       {
-                               eDebug("REMOVED PID: %04x", *i);
-                               m_record->removePID(*i);
-                       }
-                       
-                       if (!m_recording)
-                       {
-                               m_record->start();
-                               m_recording = 1;
-                       }
-               }
-               
-                               // notify record thread...              
+               if (m_state == stateIdle)
+                       doPrepare();
+               else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
+                       doRecord();
                break;
        }
        }
 }
 
+RESULT eDVBServiceRecord::prepare(const char *filename)
+{
+       m_filename = filename;
+       if (m_state == stateIdle)
+               return doPrepare();
+       else
+               return -1;
+}
+
 RESULT eDVBServiceRecord::start()
 {
-       eDebug("starting recording..");
-       return m_service_handler.tune(m_ref);
+       m_want_record = 1;
+               /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
+       return doRecord();
 }
 
+
 RESULT eDVBServiceRecord::stop()
 {
        eDebug("stop recording!!");
-       if (m_recording)
+       if (m_state == stateRecording)
+       {
+               if (m_record)
+                       m_record->stop();
+               m_state = statePrepared;
+       }
+       
+       if (m_state == statePrepared)
        {
-               m_record->stop();
                m_record = 0;
-               m_recording = 0;
+               m_state = stateIdle;
+       }
+       return 0;
+}
+
+
+int eDVBServiceRecord::doPrepare()
+{
+               /* allocate a ts recorder if we don't already have one. */
+       if (m_state == stateIdle)
+       {
+               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!");
+                       return -1;
+               }
+               ePtr<iDVBDemux> demux;
+               if (m_service_handler.getDataDemux(demux))
+               {
+                       eDebug("eDVBServiceRecord - NO DEMUX available!");
+                       return -2;
+               }
+               demux->createTSRecorder(m_record);
+               if (!m_record)
+               {
+                       eDebug("eDVBServiceRecord - no ts recorder available.");
+                       return -3;
+               }
+               m_record->setTargetFD(fd);
+       }
+       eDebug("starting recording..");
+       
+       eDVBServicePMTHandler::program program;
+       if (m_service_handler.getProgramInfo(program))
+               eDebug("getting program info failed.");
+       else
+       {
+               std::set<int> pids_to_record;
+               
+               pids_to_record.insert(0); // PAT
+               
+               if (program.pmtPid != -1)
+                       pids_to_record.insert(program.pmtPid); // PMT
+               
+               eDebugNoNewLine("RECORD: 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)
+                       {
+                               pids_to_record.insert(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)
+                       {
+                               pids_to_record.insert(i->pid);
+                               if (i != program.audioStreams.begin())
+                                       eDebugNoNewLine(", ");
+                               eDebugNoNewLine("%04x", i->pid);
+                       }
+                       eDebugNoNewLine(")");
+               }
+               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;
+               
+               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)
+               {
+                       eDebug("ADD PID: %04x", *i);
+                       m_record->addPID(*i);
+               }
+               for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
+               {
+                       eDebug("REMOVED PID: %04x", *i);
+                       m_record->removePID(*i);
+               }
+               
+               m_pids_active = pids_to_record;
+               
+               if (m_state != stateRecording)
+               {
+                       m_record->start();
+                       m_state = stateRecording;
+               }
        }
        return 0;
 }