1 #include <lib/service/servicedvbrecord.h>
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/epgcache.h>
6 DEFINE_REF(eDVBServiceRecord);
8 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref)
10 CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
19 void eDVBServiceRecord::serviceEvent(int event)
21 eDebug("RECORD service event %d", event);
24 case eDVBServicePMTHandler::eventTuned:
28 if (m_state == stateRecording && m_want_record)
30 m_event((iRecordableService*)this, evTunedIn);
33 case eDVBServicePMTHandler::eventTuneFailed:
35 eDebug("record failed to tune");
36 m_event((iRecordableService*)this, evTuneFailed);
39 case eDVBServicePMTHandler::eventNewProgramInfo:
41 if (m_state == stateIdle)
43 else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
45 m_event((iRecordableService*)this, evNewProgramInfo);
48 case eDVBServicePMTHandler::eventMisconfiguration:
49 m_error = errMisconfiguration;
50 m_event((iRecordableService*)this, evTuneFailed);
52 case eDVBServicePMTHandler::eventNoResources:
53 m_error = errNoResources;
54 m_event((iRecordableService*)this, evTuneFailed);
59 RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id)
61 m_filename = filename;
64 if (m_state == stateIdle)
66 int ret = doPrepare();
69 eEPGCache::getInstance()->Lock();
70 const eit_event_struct *event = 0;
71 eServiceReferenceDVB ref = m_ref.getParentServiceReference();
74 if ( eit_event_id != -1 )
76 eDebug("query epg event id %d", eit_event_id);
77 eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
79 if ( !event && (begTime != -1 && endTime != -1) )
81 time_t queryTime = begTime + ((endTime-begTime)/2);
83 localtime_r(&begTime, &beg);
84 localtime_r(&endTime, &end);
85 localtime_r(&queryTime, &query);
86 eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
87 beg.tm_hour, beg.tm_min, beg.tm_sec,
88 end.tm_hour, end.tm_min, end.tm_sec,
89 query.tm_hour, query.tm_min, query.tm_sec);
90 eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
94 eDebug("found event.. store to disc");
95 std::string fname = filename;
96 fname.erase(fname.length()-2, 2);
98 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
101 int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
102 int wr = ::write( fd, (unsigned char*)event, evLen );
104 eDebug("eit write error (%m)");
108 eEPGCache::getInstance()->Unlock();
116 RESULT eDVBServiceRecord::prepareStreaming()
120 if (m_state == stateIdle)
125 RESULT eDVBServiceRecord::start()
128 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
129 m_event((iRecordableService*)this, evStart);
134 RESULT eDVBServiceRecord::stop()
136 eDebug("stop recording!");
137 if (m_state == stateRecording)
141 if (m_target_fd >= 0)
143 ::close(m_target_fd);
146 m_state = statePrepared;
148 eDebug("(was not recording)");
149 if (m_state == statePrepared)
154 m_event((iRecordableService*)this, evRecordStopped);
159 int eDVBServiceRecord::doPrepare()
161 /* allocate a ts recorder if we don't already have one. */
162 if (m_state == stateIdle)
164 m_pids_active.clear();
165 m_state = statePrepared;
166 return m_service_handler.tune(m_ref, 0);
171 int eDVBServiceRecord::doRecord()
173 int err = doPrepare();
176 m_error = errTuneFailed;
177 m_event((iRecordableService*)this, evRecordFailed);
182 return 0; /* try it again when we are tuned in */
184 if (!m_record && m_tuned && !m_streaming)
186 eDebug("Recording to %s...", m_filename.c_str());
187 ::remove(m_filename.c_str());
188 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
191 eDebug("eDVBServiceRecord - can't open recording file!");
192 m_error = errOpenRecordFile;
193 m_event((iRecordableService*)this, evRecordFailed);
194 return errOpenRecordFile;
197 /* turn off kernel caching strategies */
198 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
200 ePtr<iDVBDemux> demux;
201 if (m_service_handler.getDataDemux(demux))
203 eDebug("eDVBServiceRecord - NO DEMUX available!");
204 m_error = errNoDemuxAvailable;
205 m_event((iRecordableService*)this, evRecordFailed);
206 return errNoDemuxAvailable;
208 demux->createTSRecorder(m_record);
211 eDebug("eDVBServiceRecord - no ts recorder available.");
212 m_error = errNoTsRecorderAvailable;
213 m_event((iRecordableService*)this, evRecordFailed);
214 return errNoTsRecorderAvailable;
216 m_record->setTargetFD(fd);
217 m_record->setTargetFilename(m_filename.c_str());
218 m_record->connectEvent(slot(*this, &eDVBServiceRecord::recordEvent), m_con_record_event);
225 m_state = stateRecording;
226 eDebug("start streaming...");
229 eDebug("start recording...");
231 eDVBServicePMTHandler::program program;
232 if (m_service_handler.getProgramInfo(program))
233 eDebug("getting program info failed.");
236 std::set<int> pids_to_record;
238 pids_to_record.insert(0); // PAT
240 if (program.pmtPid != -1)
241 pids_to_record.insert(program.pmtPid); // PMT
245 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
246 if (!program.videoStreams.empty())
248 eDebugNoNewLine(" (");
249 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
250 i(program.videoStreams.begin());
251 i != program.videoStreams.end(); ++i)
253 pids_to_record.insert(i->pid);
255 if (timing_pid == -1)
258 if (i != program.videoStreams.begin())
259 eDebugNoNewLine(", ");
260 eDebugNoNewLine("%04x", i->pid);
262 eDebugNoNewLine(")");
264 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
265 if (!program.audioStreams.empty())
267 eDebugNoNewLine(" (");
268 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
269 i(program.audioStreams.begin());
270 i != program.audioStreams.end(); ++i)
272 pids_to_record.insert(i->pid);
274 if (timing_pid == -1)
277 if (i != program.audioStreams.begin())
278 eDebugNoNewLine(", ");
279 eDebugNoNewLine("%04x", i->pid);
281 eDebugNoNewLine(")");
283 if (!program.subtitleStreams.empty())
285 eDebugNoNewLine(" (");
286 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
287 i(program.subtitleStreams.begin());
288 i != program.subtitleStreams.end(); ++i)
290 pids_to_record.insert(i->pid);
292 if (i != program.subtitleStreams.begin())
293 eDebugNoNewLine(", ");
294 eDebugNoNewLine("%04x", i->pid);
296 eDebugNoNewLine(")");
298 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
299 if (program.pcrPid != 0x1fff)
300 pids_to_record.insert(program.pcrPid);
301 eDebug(", and the text pid is %04x", program.textPid);
302 if (program.textPid != -1)
303 pids_to_record.insert(program.textPid); // Videotext
305 /* find out which pids are NEW and which pids are obsolete.. */
306 std::set<int> new_pids, obsolete_pids;
308 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
309 m_pids_active.begin(), m_pids_active.end(),
310 std::inserter(new_pids, new_pids.begin()));
313 m_pids_active.begin(), m_pids_active.end(),
314 pids_to_record.begin(), pids_to_record.end(),
315 std::inserter(new_pids, new_pids.begin())
318 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
320 eDebug("ADD PID: %04x", *i);
321 m_record->addPID(*i);
324 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
326 eDebug("REMOVED PID: %04x", *i);
327 m_record->removePID(*i);
330 if (timing_pid != -1)
331 m_record->setTimingPID(timing_pid);
333 m_pids_active = pids_to_record;
335 if (m_state != stateRecording)
338 m_state = stateRecording;
343 m_event((iRecordableService*)this, evRecordRunning);
347 RESULT eDVBServiceRecord::frontendInfo(ePtr<iFrontendInformation> &ptr)
353 RESULT eDVBServiceRecord::connectEvent(const Slot2<void,iRecordableService*,int> &event, ePtr<eConnection> &connection)
355 connection = new eConnection((iRecordableService*)this, m_event.connect(event));
359 RESULT eDVBServiceRecord::stream(ePtr<iStreamableService> &ptr)
365 PyObject *eDVBServiceRecord::getStreamingData()
367 eDVBServicePMTHandler::program program;
368 if (!m_tuned || m_service_handler.getProgramInfo(program))
374 PyObject *r = program.createPythonObject();
375 ePtr<iDVBDemux> demux;
376 if (!m_service_handler.getDataDemux(demux))
379 demux->getCADemuxID(demux_id);
381 PyDict_SetItemString(r, "demux", PyInt_FromLong(demux_id));
387 void eDVBServiceRecord::recordEvent(int event)
391 case iDVBTSRecorder::eventWriteError:
392 eWarning("[eDVBServiceRecord] record write error");
394 m_event((iRecordableService*)this, evRecordWriteError);
397 eDebug("unhandled record event %d", event);