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;
137 ret = meta.updateMeta(filename) ? -255 : 0;
140 const eit_event_struct *event = 0;
141 eEPGCache::getInstance()->Lock();
142 if ( eit_event_id != -1 )
144 eDebug("query epg event id %d", eit_event_id);
145 eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
147 if ( !event && (begTime != -1 && endTime != -1) )
149 time_t queryTime = begTime + ((endTime-begTime)/2);
151 localtime_r(&begTime, &beg);
152 localtime_r(&endTime, &end);
153 localtime_r(&queryTime, &query);
154 eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
155 beg.tm_hour, beg.tm_min, beg.tm_sec,
156 end.tm_hour, end.tm_min, end.tm_sec,
157 query.tm_hour, query.tm_min, query.tm_sec);
158 eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
162 eDebug("found event.. store to disc");
163 std::string fname = filename;
164 fname.erase(fname.length()-2, 2);
166 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
169 int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
170 int wr = ::write( fd, (unsigned char*)event, evLen );
172 eDebug("eit write error (%m)");
176 eEPGCache::getInstance()->Unlock();
184 RESULT eDVBServiceRecord::prepareStreaming()
188 if (m_state == stateIdle)
193 RESULT eDVBServiceRecord::start(bool simulate)
195 m_simulate = simulate;
197 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
198 m_event((iRecordableService*)this, evStart);
202 RESULT eDVBServiceRecord::stop()
205 eDebug("stop recording!");
206 if (m_state == stateRecording)
210 if (m_target_fd >= 0)
212 ::close(m_target_fd);
218 m_state = statePrepared;
219 } else if (!m_simulate)
220 eDebug("(was not recording)");
221 if (m_state == statePrepared)
226 m_event((iRecordableService*)this, evRecordStopped);
230 int eDVBServiceRecord::doPrepare()
232 /* allocate a ts recorder if we don't already have one. */
233 if (m_state == stateIdle)
235 m_pids_active.clear();
236 m_state = statePrepared;
237 return m_service_handler.tune(m_ref, 0, 0, m_simulate);
242 int eDVBServiceRecord::doRecord()
244 int err = doPrepare();
247 m_error = errTuneFailed;
248 m_event((iRecordableService*)this, evRecordFailed);
253 return 0; /* try it again when we are tuned in */
255 if (!m_record && m_tuned && !m_streaming && !m_simulate)
257 eDebug("Recording to %s...", m_filename.c_str());
258 ::remove(m_filename.c_str());
259 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
262 eDebug("eDVBServiceRecord - can't open recording file!");
263 m_error = errOpenRecordFile;
264 m_event((iRecordableService*)this, evRecordFailed);
265 return errOpenRecordFile;
268 /* turn off kernel caching strategies */
269 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
271 ePtr<iDVBDemux> demux;
272 if (m_service_handler.getDataDemux(demux))
274 eDebug("eDVBServiceRecord - NO DEMUX available!");
275 m_error = errNoDemuxAvailable;
276 m_event((iRecordableService*)this, evRecordFailed);
277 return errNoDemuxAvailable;
279 demux->createTSRecorder(m_record);
282 eDebug("eDVBServiceRecord - no ts recorder available.");
283 m_error = errNoTsRecorderAvailable;
284 m_event((iRecordableService*)this, evRecordFailed);
285 return errNoTsRecorderAvailable;
287 m_record->setTargetFD(fd);
288 m_record->setTargetFilename(m_filename.c_str());
289 m_record->connectEvent(slot(*this, &eDVBServiceRecord::recordEvent), m_con_record_event);
296 m_state = stateRecording;
297 eDebug("start streaming...");
300 eDebug("start recording...");
302 eDVBServicePMTHandler::program program;
303 if (m_service_handler.getProgramInfo(program))
304 eDebug("getting program info failed.");
307 std::set<int> pids_to_record;
309 pids_to_record.insert(0); // PAT
311 if (program.pmtPid != -1)
312 pids_to_record.insert(program.pmtPid); // PMT
314 int timing_pid = -1, timing_pid_type = -1;
316 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
317 if (!program.videoStreams.empty())
319 eDebugNoNewLine(" (");
320 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
321 i(program.videoStreams.begin());
322 i != program.videoStreams.end(); ++i)
324 pids_to_record.insert(i->pid);
326 if (timing_pid == -1)
329 timing_pid_type = i->type;
332 if (i != program.videoStreams.begin())
333 eDebugNoNewLine(", ");
334 eDebugNoNewLine("%04x", i->pid);
336 eDebugNoNewLine(")");
338 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
339 if (!program.audioStreams.empty())
341 eDebugNoNewLine(" (");
342 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
343 i(program.audioStreams.begin());
344 i != program.audioStreams.end(); ++i)
346 pids_to_record.insert(i->pid);
348 if (timing_pid == -1)
351 timing_pid_type = -1;
354 if (i != program.audioStreams.begin())
355 eDebugNoNewLine(", ");
356 eDebugNoNewLine("%04x", i->pid);
358 eDebugNoNewLine(")");
360 if (!program.subtitleStreams.empty())
362 eDebugNoNewLine(" (");
363 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
364 i(program.subtitleStreams.begin());
365 i != program.subtitleStreams.end(); ++i)
367 pids_to_record.insert(i->pid);
369 if (i != program.subtitleStreams.begin())
370 eDebugNoNewLine(", ");
371 eDebugNoNewLine("%04x", i->pid);
373 eDebugNoNewLine(")");
375 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
376 if (program.pcrPid != 0x1fff)
377 pids_to_record.insert(program.pcrPid);
378 eDebug(", and the text pid is %04x", program.textPid);
379 if (program.textPid != -1)
380 pids_to_record.insert(program.textPid); // Videotext
382 /* find out which pids are NEW and which pids are obsolete.. */
383 std::set<int> new_pids, obsolete_pids;
385 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
386 m_pids_active.begin(), m_pids_active.end(),
387 std::inserter(new_pids, new_pids.begin()));
390 m_pids_active.begin(), m_pids_active.end(),
391 pids_to_record.begin(), pids_to_record.end(),
392 std::inserter(obsolete_pids, obsolete_pids.begin())
395 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
397 eDebug("ADD PID: %04x", *i);
398 m_record->addPID(*i);
401 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
403 eDebug("REMOVED PID: %04x", *i);
404 m_record->removePID(*i);
407 if (timing_pid != -1)
408 m_record->setTimingPID(timing_pid, timing_pid_type);
410 m_pids_active = pids_to_record;
412 if (m_state != stateRecording)
415 m_state = stateRecording;
420 m_event((iRecordableService*)this, evRecordRunning);
424 RESULT eDVBServiceRecord::frontendInfo(ePtr<iFrontendInformation> &ptr)
430 RESULT eDVBServiceRecord::connectEvent(const Slot2<void,iRecordableService*,int> &event, ePtr<eConnection> &connection)
432 connection = new eConnection((iRecordableService*)this, m_event.connect(event));
436 RESULT eDVBServiceRecord::stream(ePtr<iStreamableService> &ptr)
442 extern void PutToDict(ePyObject &dict, const char*key, long val); // defined in dvb/frontend.cpp
444 PyObject *eDVBServiceRecord::getStreamingData()
446 eDVBServicePMTHandler::program program;
447 if (!m_tuned || m_service_handler.getProgramInfo(program))
452 ePyObject r = program.createPythonObject();
453 ePtr<iDVBDemux> demux;
454 if (!m_service_handler.getDataDemux(demux))
457 if (!demux->getCADemuxID(demux_id))
458 PutToDict(r, "demux", demux_id);
464 void eDVBServiceRecord::recordEvent(int event)
468 case iDVBTSRecorder::eventWriteError:
469 eWarning("[eDVBServiceRecord] record write error");
471 m_event((iRecordableService*)this, evRecordWriteError);
474 eDebug("unhandled record event %d", event);
478 void eDVBServiceRecord::gotNewEvent()
480 ePtr<eServiceEvent> event_now;
481 m_event_handler.getEvent(event_now, 0);
486 int event_id = event_now->getEventId();
492 if (m_record->getCurrentPCR(p))
493 eDebug("getting PCR failed!");
496 m_event_timestamps[event_id] = p;
497 eDebug("pcr of eit change: %llx", p);
501 if (event_id != m_last_event_id)
502 eDebug("[eDVBServiceRecord] now running: %s (%d seconds)", event_now->getEventName().c_str(), event_now->getDuration());
504 m_last_event_id = event_id;
506 m_event((iRecordableService*)this, evNewEventInfo);
509 void eDVBServiceRecord::saveCutlist()
511 /* XXX: dupe of eDVBServicePlay::saveCuesheet, refactor plz */
512 std::string filename = m_filename + ".cuts";
516 if (tstools.openFile(m_filename.c_str()))
518 eDebug("[eDVBServiceRecord] saving cutlist failed because tstools failed");
522 FILE *f = fopen(filename.c_str(), "wb");
526 unsigned long long where;
529 for (std::map<int,pts_t>::iterator i(m_event_timestamps.begin()); i != m_event_timestamps.end(); ++i)
532 off_t offset = 0; // fixme, we need to note down both
533 if (tstools.fixupPTS(offset, p))
535 eDebug("[eDVBServiceRecord] fixing up PTS failed, not saving");
538 eDebug("fixed up %llx to %llx (offset %llx)", i->second, p, offset);
539 #if BYTE_ORDER == BIG_ENDIAN
544 what = htonl(2); /* mark */
545 fwrite(&where, sizeof(where), 1, f);
546 fwrite(&what, sizeof(what), 1, f);
553 RESULT eDVBServiceRecord::subServices(ePtr<iSubserviceList> &ptr)
559 int eDVBServiceRecord::getNumberOfSubservices()
561 ePtr<eServiceEvent> evt;
562 if (!m_event_handler.getEvent(evt, 0))
563 return evt->getNumOfLinkageServices();
567 RESULT eDVBServiceRecord::getSubservice(eServiceReference &sub, unsigned int n)
569 ePtr<eServiceEvent> evt;
570 if (!m_event_handler.getEvent(evt, 0))
572 if (!evt->getLinkageService(sub, m_ref, n))
575 sub.type=eServiceReference::idInvalid;