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);
51 RESULT eDVBServiceRecord::prepare(const char *filename, time_t begTime, time_t endTime, int eit_event_id)
53 m_filename = filename;
56 if (m_state == stateIdle)
58 int ret = doPrepare();
61 eEPGCache::getInstance()->Lock();
62 const eit_event_struct *event = 0;
63 eServiceReferenceDVB ref = m_ref.getParentServiceReference();
66 if ( eit_event_id != -1 )
68 eDebug("query epg event id %d", eit_event_id);
69 eEPGCache::getInstance()->lookupEventId(ref, eit_event_id, event);
71 if ( !event && (begTime != -1 && endTime != -1) )
73 time_t queryTime = begTime + ((endTime-begTime)/2);
75 localtime_r(&begTime, &beg);
76 localtime_r(&endTime, &end);
77 localtime_r(&queryTime, &query);
78 eDebug("query stime %d:%d:%d, etime %d:%d:%d, qtime %d:%d:%d",
79 beg.tm_hour, beg.tm_min, beg.tm_sec,
80 end.tm_hour, end.tm_min, end.tm_sec,
81 query.tm_hour, query.tm_min, query.tm_sec);
82 eEPGCache::getInstance()->lookupEventTime(ref, queryTime, event);
86 eDebug("found event.. store to disc");
87 std::string fname = filename;
88 fname.erase(fname.length()-2, 2);
90 int fd = open(fname.c_str(), O_CREAT|O_WRONLY, 0777);
93 int evLen=HILO(event->descriptors_loop_length)+12/*EIT_LOOP_SIZE*/;
94 int wr = ::write( fd, (unsigned char*)event, evLen );
96 eDebug("eit write error (%m)");
100 eEPGCache::getInstance()->Unlock();
108 RESULT eDVBServiceRecord::prepareStreaming()
112 if (m_state == stateIdle)
117 RESULT eDVBServiceRecord::start()
120 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
121 m_event((iRecordableService*)this, evStart);
126 RESULT eDVBServiceRecord::stop()
128 eDebug("stop recording!");
129 if (m_state == stateRecording)
133 if (m_target_fd >= 0)
135 ::close(m_target_fd);
138 m_state = statePrepared;
140 eDebug("(was not recording)");
141 if (m_state == statePrepared)
146 m_event((iRecordableService*)this, evRecordStopped);
151 int eDVBServiceRecord::doPrepare()
153 /* allocate a ts recorder if we don't already have one. */
154 if (m_state == stateIdle)
156 m_pids_active.clear();
157 m_state = statePrepared;
158 return m_service_handler.tune(m_ref, 0);
163 int eDVBServiceRecord::doRecord()
165 int err = doPrepare();
168 m_error = errTuneFailed;
169 m_event((iRecordableService*)this, evRecordFailed);
174 return 0; /* try it again when we are tuned in */
176 if (!m_record && m_tuned && !m_streaming)
178 eDebug("Recording to %s...", m_filename.c_str());
179 ::remove(m_filename.c_str());
180 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
183 eDebug("eDVBServiceRecord - can't open recording file!");
184 m_error = errOpenRecordFile;
185 m_event((iRecordableService*)this, evRecordFailed);
186 return errOpenRecordFile;
189 /* turn off kernel caching strategies */
190 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
192 ePtr<iDVBDemux> demux;
193 if (m_service_handler.getDataDemux(demux))
195 eDebug("eDVBServiceRecord - NO DEMUX available!");
196 m_error = errNoDemuxAvailable;
197 m_event((iRecordableService*)this, evRecordFailed);
198 return errNoDemuxAvailable;
200 demux->createTSRecorder(m_record);
203 eDebug("eDVBServiceRecord - no ts recorder available.");
204 m_error = errNoTsRecorderAvailable;
205 m_event((iRecordableService*)this, evRecordFailed);
206 return errNoTsRecorderAvailable;
208 m_record->setTargetFD(fd);
209 m_record->setTargetFilename(m_filename.c_str());
210 m_record->connectEvent(slot(*this, &eDVBServiceRecord::recordEvent), m_con_record_event);
217 m_state = stateRecording;
218 eDebug("start streaming...");
221 eDebug("start recording...");
223 eDVBServicePMTHandler::program program;
224 if (m_service_handler.getProgramInfo(program))
225 eDebug("getting program info failed.");
228 std::set<int> pids_to_record;
230 pids_to_record.insert(0); // PAT
232 if (program.pmtPid != -1)
233 pids_to_record.insert(program.pmtPid); // PMT
237 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
238 if (!program.videoStreams.empty())
240 eDebugNoNewLine(" (");
241 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
242 i(program.videoStreams.begin());
243 i != program.videoStreams.end(); ++i)
245 pids_to_record.insert(i->pid);
247 if (timing_pid == -1)
250 if (i != program.videoStreams.begin())
251 eDebugNoNewLine(", ");
252 eDebugNoNewLine("%04x", i->pid);
254 eDebugNoNewLine(")");
256 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
257 if (!program.audioStreams.empty())
259 eDebugNoNewLine(" (");
260 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
261 i(program.audioStreams.begin());
262 i != program.audioStreams.end(); ++i)
264 pids_to_record.insert(i->pid);
266 if (timing_pid == -1)
269 if (i != program.audioStreams.begin())
270 eDebugNoNewLine(", ");
271 eDebugNoNewLine("%04x", i->pid);
273 eDebugNoNewLine(")");
275 if (!program.subtitleStreams.empty())
277 eDebugNoNewLine(" (");
278 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
279 i(program.subtitleStreams.begin());
280 i != program.subtitleStreams.end(); ++i)
282 pids_to_record.insert(i->pid);
284 if (i != program.subtitleStreams.begin())
285 eDebugNoNewLine(", ");
286 eDebugNoNewLine("%04x", i->pid);
288 eDebugNoNewLine(")");
290 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
291 if (program.pcrPid != 0x1fff)
292 pids_to_record.insert(program.pcrPid);
293 eDebug(", and the text pid is %04x", program.textPid);
294 if (program.textPid != -1)
295 pids_to_record.insert(program.textPid); // Videotext
297 /* find out which pids are NEW and which pids are obsolete.. */
298 std::set<int> new_pids, obsolete_pids;
300 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
301 m_pids_active.begin(), m_pids_active.end(),
302 std::inserter(new_pids, new_pids.begin()));
305 m_pids_active.begin(), m_pids_active.end(),
306 pids_to_record.begin(), pids_to_record.end(),
307 std::inserter(new_pids, new_pids.begin())
310 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
312 eDebug("ADD PID: %04x", *i);
313 m_record->addPID(*i);
316 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
318 eDebug("REMOVED PID: %04x", *i);
319 m_record->removePID(*i);
322 if (timing_pid != -1)
323 m_record->setTimingPID(timing_pid);
325 m_pids_active = pids_to_record;
327 if (m_state != stateRecording)
330 m_state = stateRecording;
335 m_event((iRecordableService*)this, evRecordRunning);
339 RESULT eDVBServiceRecord::frontendInfo(ePtr<iFrontendInformation> &ptr)
345 RESULT eDVBServiceRecord::connectEvent(const Slot2<void,iRecordableService*,int> &event, ePtr<eConnection> &connection)
347 connection = new eConnection((iRecordableService*)this, m_event.connect(event));
351 RESULT eDVBServiceRecord::stream(ePtr<iStreamableService> &ptr)
357 PyObject *eDVBServiceRecord::getStreamingData()
359 eDVBServicePMTHandler::program program;
360 if (!m_tuned || m_service_handler.getProgramInfo(program))
366 PyObject *r = program.createPythonObject();
367 ePtr<iDVBDemux> demux;
368 if (!m_service_handler.getDataDemux(demux))
371 demux->getCADemuxID(demux_id);
373 PyDict_SetItemString(r, "demux", PyInt_FromLong(demux_id));
379 void eDVBServiceRecord::recordEvent(int event)
383 case iDVBTSRecorder::eventWriteError:
384 eWarning("[eDVBServiceRecord] record write error");
386 m_event((iRecordableService*)this, evRecordWriteError);
389 eDebug("unhandled record event %d", event);