1 #include <lib/service/servicedvbrecord.h>
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/epgcache.h>
4 #include <lib/dvb/metaparser.h>
9 #include <netinet/in.h>
12 #error no byte order defined!
15 DEFINE_REF(eDVBServiceRecord);
17 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref)
19 CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
20 CONNECT(m_event_handler.m_eit_changed, eDVBServiceRecord::gotNewEvent);
31 void eDVBServiceRecord::serviceEvent(int event)
33 eDebug("RECORD service event %d", event);
36 case eDVBServicePMTHandler::eventTuned:
41 /* start feeding EIT updates */
42 ePtr<iDVBDemux> m_demux;
43 if (!m_service_handler.getDataDemux(m_demux))
45 eServiceReferenceDVB &ref = (eServiceReferenceDVB&) m_ref;
46 int sid = ref.getParentServiceID().get();
48 sid = ref.getServiceID().get();
49 if ( ref.getParentTransportStreamID().get() &&
50 ref.getParentTransportStreamID() != ref.getTransportStreamID() )
51 m_event_handler.startOther(m_demux, sid);
53 m_event_handler.start(m_demux, sid);
56 if (m_state == stateRecording && m_want_record)
58 m_event((iRecordableService*)this, evTunedIn);
61 case eDVBServicePMTHandler::eventTuneFailed:
63 eDebug("record failed to tune");
64 m_event((iRecordableService*)this, evTuneFailed);
67 case eDVBServicePMTHandler::eventNewProgramInfo:
69 if (m_state == stateIdle)
71 else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
73 m_event((iRecordableService*)this, evNewProgramInfo);
76 case eDVBServicePMTHandler::eventMisconfiguration:
77 m_error = errMisconfiguration;
78 m_event((iRecordableService*)this, evTuneFailed);
80 case eDVBServicePMTHandler::eventNoResources:
81 m_error = errNoResources;
82 m_event((iRecordableService*)this, evTuneFailed);
87 RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id, const char *name, const char *descr, const char *tags)
89 m_filename = filename;
92 if (m_state == stateIdle)
94 int ret = doPrepare();
97 eServiceReferenceDVB ref = m_ref.getParentServiceReference();
98 ePtr<eDVBResourceManager> res_mgr;
100 std::string service_data;
103 if (!eDVBResourceManager::getInstance(res_mgr))
105 ePtr<iDVBChannelList> db;
106 if (!res_mgr->getChannelList(db))
108 ePtr<eDVBService> service;
109 if (!db->getService(ref, service))
112 sprintf(tmp, "f:%x", service->m_flags);
115 for (int x=0; x < eDVBService::cacheMax; ++x)
117 int entry = service->getCacheEntry((eDVBService::cacheID)x);
120 sprintf(tmp, ",c:%02d%04x", x, entry);
127 meta.m_time_create = begTime;
130 meta.m_service_data = service_data;
134 meta.m_description = descr;
135 ret = meta.updateMeta(filename) ? -255 : 0;
138 const eit_event_struct *event = 0;
139 eEPGCache::getInstance()->Lock();
140 if ( eit_event_id != -1 )
142 eDebug("query epg event id %d", eit_event_id);
143 eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
145 if ( !event && (begTime != -1 && endTime != -1) )
147 time_t queryTime = begTime + ((endTime-begTime)/2);
149 localtime_r(&begTime, &beg);
150 localtime_r(&endTime, &end);
151 localtime_r(&queryTime, &query);
152 eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
153 beg.tm_hour, beg.tm_min, beg.tm_sec,
154 end.tm_hour, end.tm_min, end.tm_sec,
155 query.tm_hour, query.tm_min, query.tm_sec);
156 eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
160 eDebug("found event.. store to disc");
161 std::string fname = filename;
162 fname.erase(fname.length()-2, 2);
164 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
167 int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
168 int wr = ::write( fd, (unsigned char*)event, evLen );
170 eDebug("eit write error (%m)");
174 eEPGCache::getInstance()->Unlock();
182 RESULT eDVBServiceRecord::prepareStreaming()
186 if (m_state == stateIdle)
191 RESULT eDVBServiceRecord::start(bool simulate)
193 m_simulate = simulate;
195 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
196 m_event((iRecordableService*)this, evStart);
200 RESULT eDVBServiceRecord::stop()
203 eDebug("stop recording!");
204 if (m_state == stateRecording)
208 if (m_target_fd >= 0)
210 ::close(m_target_fd);
216 m_state = statePrepared;
217 } else if (!m_simulate)
218 eDebug("(was not recording)");
219 if (m_state == statePrepared)
224 m_event((iRecordableService*)this, evRecordStopped);
228 int eDVBServiceRecord::doPrepare()
230 /* allocate a ts recorder if we don't already have one. */
231 if (m_state == stateIdle)
233 m_pids_active.clear();
234 m_state = statePrepared;
235 return m_service_handler.tune(m_ref, 0, 0, m_simulate);
240 int eDVBServiceRecord::doRecord()
242 int err = doPrepare();
245 m_error = errTuneFailed;
246 m_event((iRecordableService*)this, evRecordFailed);
251 return 0; /* try it again when we are tuned in */
253 if (!m_record && m_tuned && !m_streaming && !m_simulate)
255 eDebug("Recording to %s...", m_filename.c_str());
256 ::remove(m_filename.c_str());
257 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
260 eDebug("eDVBServiceRecord - can't open recording file!");
261 m_error = errOpenRecordFile;
262 m_event((iRecordableService*)this, evRecordFailed);
263 return errOpenRecordFile;
266 /* turn off kernel caching strategies */
267 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
269 ePtr<iDVBDemux> demux;
270 if (m_service_handler.getDataDemux(demux))
272 eDebug("eDVBServiceRecord - NO DEMUX available!");
273 m_error = errNoDemuxAvailable;
274 m_event((iRecordableService*)this, evRecordFailed);
275 return errNoDemuxAvailable;
277 demux->createTSRecorder(m_record);
280 eDebug("eDVBServiceRecord - no ts recorder available.");
281 m_error = errNoTsRecorderAvailable;
282 m_event((iRecordableService*)this, evRecordFailed);
283 return errNoTsRecorderAvailable;
285 m_record->setTargetFD(fd);
286 m_record->setTargetFilename(m_filename.c_str());
287 m_record->connectEvent(slot(*this, &eDVBServiceRecord::recordEvent), m_con_record_event);
294 m_state = stateRecording;
295 eDebug("start streaming...");
298 eDebug("start recording...");
300 eDVBServicePMTHandler::program program;
301 if (m_service_handler.getProgramInfo(program))
302 eDebug("getting program info failed.");
305 std::set<int> pids_to_record;
307 pids_to_record.insert(0); // PAT
309 if (program.pmtPid != -1)
310 pids_to_record.insert(program.pmtPid); // PMT
312 int timing_pid = -1, timing_pid_type = -1;
314 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
315 if (!program.videoStreams.empty())
317 eDebugNoNewLine(" (");
318 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
319 i(program.videoStreams.begin());
320 i != program.videoStreams.end(); ++i)
322 pids_to_record.insert(i->pid);
324 if (timing_pid == -1)
327 timing_pid_type = i->type;
330 if (i != program.videoStreams.begin())
331 eDebugNoNewLine(", ");
332 eDebugNoNewLine("%04x", i->pid);
334 eDebugNoNewLine(")");
336 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
337 if (!program.audioStreams.empty())
339 eDebugNoNewLine(" (");
340 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
341 i(program.audioStreams.begin());
342 i != program.audioStreams.end(); ++i)
344 pids_to_record.insert(i->pid);
346 if (timing_pid == -1)
349 timing_pid_type = -1;
352 if (i != program.audioStreams.begin())
353 eDebugNoNewLine(", ");
354 eDebugNoNewLine("%04x", i->pid);
356 eDebugNoNewLine(")");
358 if (!program.subtitleStreams.empty())
360 eDebugNoNewLine(" (");
361 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
362 i(program.subtitleStreams.begin());
363 i != program.subtitleStreams.end(); ++i)
365 pids_to_record.insert(i->pid);
367 if (i != program.subtitleStreams.begin())
368 eDebugNoNewLine(", ");
369 eDebugNoNewLine("%04x", i->pid);
371 eDebugNoNewLine(")");
373 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
374 if (program.pcrPid != 0x1fff)
375 pids_to_record.insert(program.pcrPid);
376 eDebug(", and the text pid is %04x", program.textPid);
377 if (program.textPid != -1)
378 pids_to_record.insert(program.textPid); // Videotext
380 /* find out which pids are NEW and which pids are obsolete.. */
381 std::set<int> new_pids, obsolete_pids;
383 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
384 m_pids_active.begin(), m_pids_active.end(),
385 std::inserter(new_pids, new_pids.begin()));
388 m_pids_active.begin(), m_pids_active.end(),
389 pids_to_record.begin(), pids_to_record.end(),
390 std::inserter(new_pids, new_pids.begin())
393 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
395 eDebug("ADD PID: %04x", *i);
396 m_record->addPID(*i);
399 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
401 eDebug("REMOVED PID: %04x", *i);
402 m_record->removePID(*i);
405 if (timing_pid != -1)
406 m_record->setTimingPID(timing_pid, timing_pid_type);
408 m_pids_active = pids_to_record;
410 if (m_state != stateRecording)
413 m_state = stateRecording;
418 m_event((iRecordableService*)this, evRecordRunning);
422 RESULT eDVBServiceRecord::frontendInfo(ePtr<iFrontendInformation> &ptr)
428 RESULT eDVBServiceRecord::connectEvent(const Slot2<void,iRecordableService*,int> &event, ePtr<eConnection> &connection)
430 connection = new eConnection((iRecordableService*)this, m_event.connect(event));
434 RESULT eDVBServiceRecord::stream(ePtr<iStreamableService> &ptr)
440 extern void PutToDict(ePyObject &dict, const char*key, long val); // defined in dvb/frontend.cpp
442 PyObject *eDVBServiceRecord::getStreamingData()
444 eDVBServicePMTHandler::program program;
445 if (!m_tuned || m_service_handler.getProgramInfo(program))
450 ePyObject r = program.createPythonObject();
451 ePtr<iDVBDemux> demux;
452 if (!m_service_handler.getDataDemux(demux))
455 if (!demux->getCADemuxID(demux_id))
456 PutToDict(r, "demux", demux_id);
462 void eDVBServiceRecord::recordEvent(int event)
466 case iDVBTSRecorder::eventWriteError:
467 eWarning("[eDVBServiceRecord] record write error");
469 m_event((iRecordableService*)this, evRecordWriteError);
472 eDebug("unhandled record event %d", event);
476 void eDVBServiceRecord::gotNewEvent()
478 ePtr<eServiceEvent> event_now;
479 m_event_handler.getEvent(event_now, 0);
484 int event_id = event_now->getEventId();
490 if (m_record->getCurrentPCR(p))
491 eDebug("getting PCR failed!");
495 m_event_timestamps[/* event_id*/ ++i] = p;
496 eDebug("pcr of eit change: %llx", p);
500 if (event_id != m_last_event_id)
501 eDebug("[eDVBServiceRecord] now running: %s (%d seconds)", event_now->getEventName().c_str(), event_now->getDuration());
503 m_last_event_id = event_id;
506 void eDVBServiceRecord::saveCutlist()
508 /* XXX: dupe of eDVBServicePlay::saveCuesheet, refactor plz */
509 std::string filename = m_filename + ".cuts";
513 if (tstools.openFile(m_filename.c_str()))
515 eDebug("[eDVBServiceRecord] saving cutlist failed because tstools failed");
519 FILE *f = fopen(filename.c_str(), "wb");
523 unsigned long long where;
526 for (std::map<int,pts_t>::iterator i(m_event_timestamps.begin()); i != m_event_timestamps.end(); ++i)
529 off_t offset = 0; // fixme, we need to note down both
530 if (tstools.fixupPTS(offset, p))
532 eDebug("[eDVBServiceRecord] fixing up PTS failed, not saving");
535 eDebug("fixed up %llx to %llx (offset %llx)", i->second, p, offset);
536 #if BYTE_ORDER == BIG_ENDIAN
541 what = htonl(2); /* mark */
542 fwrite(&where, sizeof(where), 1, f);
543 fwrite(&what, sizeof(what), 1, f);