1 #include <lib/service/servicedvbrecord.h>
2 #include <lib/base/eerror.h>
6 DEFINE_REF(eDVBServiceRecord);
8 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref), m_service_handler(1)
10 CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
15 void eDVBServiceRecord::serviceEvent(int event)
17 eDebug("RECORD service event %d", event);
20 case eDVBServicePMTHandler::eventTuned:
25 eDebug("Recording to %s...", m_filename.c_str());
26 ::remove(m_filename.c_str());
27 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT, 0644);
30 eDebug("eDVBServiceRecord - can't open hardcoded recording file!");
34 ePtr<iDVBDemux> demux;
35 if (m_service_handler.getDemux(demux))
37 eDebug("eDVBServiceRecord - NO DEMUX available!");
41 demux->createTSRecorder(m_record);
44 eDebug("eDVBServiceRecord - no ts recorder available.");
48 m_record->setTargetFD(fd);
52 case eDVBServicePMTHandler::eventNewProgramInfo:
54 if (m_state == stateIdle)
56 else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
64 RESULT eDVBServiceRecord::prepare(const char *filename)
66 m_filename = filename;
67 if (m_state == stateIdle)
76 RESULT eDVBServiceRecord::start()
79 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
84 RESULT eDVBServiceRecord::stop()
86 eDebug("stop recording!!");
87 if (m_state == stateRecording)
90 m_state = statePrepared;
93 if (m_state == statePrepared)
102 int eDVBServiceRecord::doPrepare()
104 /* allocate a ts recorder if we don't already have one. */
105 if (m_state == stateIdle)
107 m_pids_active.clear();
108 m_state = statePrepared;
109 m_service_handler.tune(m_ref);
114 int eDVBServiceRecord::doRecord()
116 int err = doPrepare();
122 eDebug("demux not available (tune failed?). cannot record.");
125 eDebug("starting recording..");
127 eDVBServicePMTHandler::program program;
128 if (m_service_handler.getProgramInfo(program))
129 eDebug("getting program info failed.");
132 std::set<int> pids_to_record;
134 pids_to_record.insert(0); // PAT
136 if (program.pmtPid != -1)
137 pids_to_record.insert(program.pmtPid); // PMT
139 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
140 if (!program.videoStreams.empty())
142 eDebugNoNewLine(" (");
143 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
144 i(program.videoStreams.begin());
145 i != program.videoStreams.end(); ++i)
147 pids_to_record.insert(i->pid);
148 if (i != program.videoStreams.begin())
149 eDebugNoNewLine(", ");
150 eDebugNoNewLine("%04x", i->pid);
152 eDebugNoNewLine(")");
154 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
155 if (!program.audioStreams.empty())
157 eDebugNoNewLine(" (");
158 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
159 i(program.audioStreams.begin());
160 i != program.audioStreams.end(); ++i)
162 pids_to_record.insert(i->pid);
163 if (i != program.audioStreams.begin())
164 eDebugNoNewLine(", ");
165 eDebugNoNewLine("%04x", i->pid);
167 eDebugNoNewLine(")");
169 eDebug(", and the pcr pid is %04x", program.pcrPid);
170 if (program.pcrPid != 0x1fff)
171 pids_to_record.insert(program.pcrPid);
173 /* find out which pids are NEW and which pids are obsolete.. */
174 std::set<int> new_pids, obsolete_pids;
176 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
177 m_pids_active.begin(), m_pids_active.end(),
178 std::inserter(new_pids, new_pids.begin()));
181 m_pids_active.begin(), m_pids_active.end(),
182 pids_to_record.begin(), pids_to_record.end(),
183 std::inserter(new_pids, new_pids.begin())
186 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
188 eDebug("ADD PID: %04x", *i);
189 m_record->addPID(*i);
191 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
193 eDebug("REMOVED PID: %04x", *i);
194 m_record->removePID(*i);
197 if (m_state != stateRecording)
200 m_state = stateRecording;