1 #include <lib/service/servicedvbrecord.h>
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/epgcache.h>
8 #include <netinet/in.h>
11 #error no byte order defined!
14 DEFINE_REF(eDVBServiceRecord);
16 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref)
18 CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
19 CONNECT(m_event_handler.m_eit_changed, eDVBServiceRecord::gotNewEvent);
30 void eDVBServiceRecord::serviceEvent(int event)
32 eDebug("RECORD service event %d", event);
35 case eDVBServicePMTHandler::eventTuned:
40 /* start feeding EIT updates */
41 ePtr<iDVBDemux> m_demux;
42 if (!m_service_handler.getDataDemux(m_demux))
44 eServiceReferenceDVB &ref = (eServiceReferenceDVB&) m_ref;
45 int sid = ref.getParentServiceID().get();
47 sid = ref.getServiceID().get();
48 if ( ref.getParentTransportStreamID().get() &&
49 ref.getParentTransportStreamID() != ref.getTransportStreamID() )
50 m_event_handler.startOther(m_demux, sid);
52 m_event_handler.start(m_demux, sid);
55 if (m_state == stateRecording && m_want_record)
57 m_event((iRecordableService*)this, evTunedIn);
60 case eDVBServicePMTHandler::eventTuneFailed:
62 eDebug("record failed to tune");
63 m_event((iRecordableService*)this, evTuneFailed);
66 case eDVBServicePMTHandler::eventNewProgramInfo:
68 if (m_state == stateIdle)
70 else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
72 m_event((iRecordableService*)this, evNewProgramInfo);
75 case eDVBServicePMTHandler::eventMisconfiguration:
76 m_error = errMisconfiguration;
77 m_event((iRecordableService*)this, evTuneFailed);
79 case eDVBServicePMTHandler::eventNoResources:
80 m_error = errNoResources;
81 m_event((iRecordableService*)this, evTuneFailed);
86 RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id)
88 m_filename = filename;
91 if (m_state == stateIdle)
93 int ret = doPrepare();
96 eEPGCache::getInstance()->Lock();
97 const eit_event_struct *event = 0;
98 eServiceReferenceDVB ref = m_ref.getParentServiceReference();
101 if ( eit_event_id != -1 )
103 eDebug("query epg event id %d", eit_event_id);
104 eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
106 if ( !event && (begTime != -1 && endTime != -1) )
108 time_t queryTime = begTime + ((endTime-begTime)/2);
110 localtime_r(&begTime, &beg);
111 localtime_r(&endTime, &end);
112 localtime_r(&queryTime, &query);
113 eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
114 beg.tm_hour, beg.tm_min, beg.tm_sec,
115 end.tm_hour, end.tm_min, end.tm_sec,
116 query.tm_hour, query.tm_min, query.tm_sec);
117 eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
121 eDebug("found event.. store to disc");
122 std::string fname = filename;
123 fname.erase(fname.length()-2, 2);
125 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
128 int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
129 int wr = ::write( fd, (unsigned char*)event, evLen );
131 eDebug("eit write error (%m)");
135 eEPGCache::getInstance()->Unlock();
143 RESULT eDVBServiceRecord::prepareStreaming()
147 if (m_state == stateIdle)
152 RESULT eDVBServiceRecord::start(bool simulate)
154 m_simulate = simulate;
156 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
157 m_event((iRecordableService*)this, evStart);
161 RESULT eDVBServiceRecord::stop()
164 eDebug("stop recording!");
165 if (m_state == stateRecording)
169 if (m_target_fd >= 0)
171 ::close(m_target_fd);
177 m_state = statePrepared;
178 } else if (!m_simulate)
179 eDebug("(was not recording)");
180 if (m_state == statePrepared)
185 m_event((iRecordableService*)this, evRecordStopped);
189 int eDVBServiceRecord::doPrepare()
191 /* allocate a ts recorder if we don't already have one. */
192 if (m_state == stateIdle)
194 m_pids_active.clear();
195 m_state = statePrepared;
196 return m_service_handler.tune(m_ref, 0, 0, m_simulate);
201 int eDVBServiceRecord::doRecord()
203 int err = doPrepare();
206 m_error = errTuneFailed;
207 m_event((iRecordableService*)this, evRecordFailed);
212 return 0; /* try it again when we are tuned in */
214 if (!m_record && m_tuned && !m_streaming && !m_simulate)
216 eDebug("Recording to %s...", m_filename.c_str());
217 ::remove(m_filename.c_str());
218 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
221 eDebug("eDVBServiceRecord - can't open recording file!");
222 m_error = errOpenRecordFile;
223 m_event((iRecordableService*)this, evRecordFailed);
224 return errOpenRecordFile;
227 /* turn off kernel caching strategies */
228 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
230 ePtr<iDVBDemux> demux;
231 if (m_service_handler.getDataDemux(demux))
233 eDebug("eDVBServiceRecord - NO DEMUX available!");
234 m_error = errNoDemuxAvailable;
235 m_event((iRecordableService*)this, evRecordFailed);
236 return errNoDemuxAvailable;
238 demux->createTSRecorder(m_record);
241 eDebug("eDVBServiceRecord - no ts recorder available.");
242 m_error = errNoTsRecorderAvailable;
243 m_event((iRecordableService*)this, evRecordFailed);
244 return errNoTsRecorderAvailable;
246 m_record->setTargetFD(fd);
247 m_record->setTargetFilename(m_filename.c_str());
248 m_record->connectEvent(slot(*this, &eDVBServiceRecord::recordEvent), m_con_record_event);
255 m_state = stateRecording;
256 eDebug("start streaming...");
259 eDebug("start recording...");
261 eDVBServicePMTHandler::program program;
262 if (m_service_handler.getProgramInfo(program))
263 eDebug("getting program info failed.");
266 std::set<int> pids_to_record;
268 pids_to_record.insert(0); // PAT
270 if (program.pmtPid != -1)
271 pids_to_record.insert(program.pmtPid); // PMT
275 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
276 if (!program.videoStreams.empty())
278 eDebugNoNewLine(" (");
279 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
280 i(program.videoStreams.begin());
281 i != program.videoStreams.end(); ++i)
283 pids_to_record.insert(i->pid);
285 if (timing_pid == -1)
288 if (i != program.videoStreams.begin())
289 eDebugNoNewLine(", ");
290 eDebugNoNewLine("%04x", i->pid);
292 eDebugNoNewLine(")");
294 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
295 if (!program.audioStreams.empty())
297 eDebugNoNewLine(" (");
298 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
299 i(program.audioStreams.begin());
300 i != program.audioStreams.end(); ++i)
302 pids_to_record.insert(i->pid);
304 if (timing_pid == -1)
307 if (i != program.audioStreams.begin())
308 eDebugNoNewLine(", ");
309 eDebugNoNewLine("%04x", i->pid);
311 eDebugNoNewLine(")");
313 if (!program.subtitleStreams.empty())
315 eDebugNoNewLine(" (");
316 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
317 i(program.subtitleStreams.begin());
318 i != program.subtitleStreams.end(); ++i)
320 pids_to_record.insert(i->pid);
322 if (i != program.subtitleStreams.begin())
323 eDebugNoNewLine(", ");
324 eDebugNoNewLine("%04x", i->pid);
326 eDebugNoNewLine(")");
328 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
329 if (program.pcrPid != 0x1fff)
330 pids_to_record.insert(program.pcrPid);
331 eDebug(", and the text pid is %04x", program.textPid);
332 if (program.textPid != -1)
333 pids_to_record.insert(program.textPid); // Videotext
335 /* find out which pids are NEW and which pids are obsolete.. */
336 std::set<int> new_pids, obsolete_pids;
338 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
339 m_pids_active.begin(), m_pids_active.end(),
340 std::inserter(new_pids, new_pids.begin()));
343 m_pids_active.begin(), m_pids_active.end(),
344 pids_to_record.begin(), pids_to_record.end(),
345 std::inserter(new_pids, new_pids.begin())
348 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
350 eDebug("ADD PID: %04x", *i);
351 m_record->addPID(*i);
354 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
356 eDebug("REMOVED PID: %04x", *i);
357 m_record->removePID(*i);
360 if (timing_pid != -1)
361 m_record->setTimingPID(timing_pid);
363 m_pids_active = pids_to_record;
365 if (m_state != stateRecording)
368 m_state = stateRecording;
373 m_event((iRecordableService*)this, evRecordRunning);
377 RESULT eDVBServiceRecord::frontendInfo(ePtr<iFrontendInformation> &ptr)
383 RESULT eDVBServiceRecord::connectEvent(const Slot2<void,iRecordableService*,int> &event, ePtr<eConnection> &connection)
385 connection = new eConnection((iRecordableService*)this, m_event.connect(event));
389 RESULT eDVBServiceRecord::stream(ePtr<iStreamableService> &ptr)
395 extern void PutToDict(ePyObject &dict, const char*key, long val); // defined in dvb/frontend.cpp
397 PyObject *eDVBServiceRecord::getStreamingData()
399 eDVBServicePMTHandler::program program;
400 if (!m_tuned || m_service_handler.getProgramInfo(program))
405 ePyObject r = program.createPythonObject();
406 ePtr<iDVBDemux> demux;
407 if (!m_service_handler.getDataDemux(demux))
410 if (!demux->getCADemuxID(demux_id))
411 PutToDict(r, "demux", demux_id);
417 void eDVBServiceRecord::recordEvent(int event)
421 case iDVBTSRecorder::eventWriteError:
422 eWarning("[eDVBServiceRecord] record write error");
424 m_event((iRecordableService*)this, evRecordWriteError);
427 eDebug("unhandled record event %d", event);
431 void eDVBServiceRecord::gotNewEvent()
433 ePtr<eServiceEvent> event_now;
434 m_event_handler.getEvent(event_now, 0);
439 int event_id = event_now->getEventId();
445 if (m_record->getCurrentPCR(p))
446 eDebug("getting PCR failed!");
450 m_event_timestamps[/* event_id*/ ++i] = p;
451 eDebug("pcr of eit change: %llx", p);
455 if (event_id != m_last_event_id)
456 eDebug("[eDVBServiceRecord] now running: %s (%d seconds)", event_now->getEventName().c_str(), event_now->getDuration());
458 m_last_event_id = event_id;
461 void eDVBServiceRecord::saveCutlist()
463 /* XXX: dupe of eDVBServicePlay::saveCuesheet, refactor plz */
464 std::string filename = m_filename + ".cuts";
468 if (tstools.openFile(m_filename.c_str()))
470 eDebug("[eDVBServiceRecord] saving cutlist failed because tstools failed");
474 FILE *f = fopen(filename.c_str(), "wb");
478 unsigned long long where;
481 for (std::map<int,pts_t>::iterator i(m_event_timestamps.begin()); i != m_event_timestamps.end(); ++i)
484 off_t offset = 0; // fixme, we need to note down both
485 if (tstools.fixupPTS(offset, p))
487 eDebug("[eDVBServiceRecord] fixing up PTS failed, not saving");
490 eDebug("fixed up %llx to %llx (offset %llx)", i->second, p, offset);
491 #if BYTE_ORDER == BIG_ENDIAN
496 what = htonl(2); /* mark */
497 fwrite(&where, sizeof(where), 1, f);
498 fwrite(&what, sizeof(what), 1, f);