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)
116 RESULT eDVBServiceRecord::start()
119 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
120 m_event((iRecordableService*)this, evStart);
125 RESULT eDVBServiceRecord::stop()
127 eDebug("stop recording!!");
128 if (m_state == stateRecording)
132 if (m_target_fd >= 0)
134 ::close(m_target_fd);
137 m_state = statePrepared;
139 if (m_state == statePrepared)
144 m_event((iRecordableService*)this, evRecordStopped);
149 int eDVBServiceRecord::doPrepare()
151 /* allocate a ts recorder if we don't already have one. */
152 if (m_state == stateIdle)
154 m_pids_active.clear();
155 m_state = statePrepared;
156 return m_service_handler.tune(m_ref, 0);
161 int eDVBServiceRecord::doRecord()
163 int err = doPrepare();
166 m_error = errTuneFailed;
167 m_event((iRecordableService*)this, evRecordFailed);
172 return 0; /* try it again when we are tuned in */
174 if (!m_record && m_tuned && !m_streaming)
176 eDebug("Recording to %s...", m_filename.c_str());
177 ::remove(m_filename.c_str());
178 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
181 eDebug("eDVBServiceRecord - can't open recording file!");
182 m_error = errOpenRecordFile;
183 m_event((iRecordableService*)this, evRecordFailed);
184 return errOpenRecordFile;
187 /* turn off kernel caching strategies */
188 posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
190 ePtr<iDVBDemux> demux;
191 if (m_service_handler.getDataDemux(demux))
193 eDebug("eDVBServiceRecord - NO DEMUX available!");
194 m_error = errNoDemuxAvailable;
195 m_event((iRecordableService*)this, evRecordFailed);
196 return errNoDemuxAvailable;
198 demux->createTSRecorder(m_record);
201 eDebug("eDVBServiceRecord - no ts recorder available.");
202 m_error = errNoTsRecorderAvailable;
203 m_event((iRecordableService*)this, evRecordFailed);
204 return errNoTsRecorderAvailable;
206 m_record->setTargetFD(fd);
207 m_record->setTargetFilename(m_filename.c_str());
213 m_state = stateRecording;
214 eDebug("start streaming...");
217 eDebug("start recording...");
219 eDVBServicePMTHandler::program program;
220 if (m_service_handler.getProgramInfo(program))
221 eDebug("getting program info failed.");
224 std::set<int> pids_to_record;
226 pids_to_record.insert(0); // PAT
228 if (program.pmtPid != -1)
229 pids_to_record.insert(program.pmtPid); // PMT
233 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
234 if (!program.videoStreams.empty())
236 eDebugNoNewLine(" (");
237 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
238 i(program.videoStreams.begin());
239 i != program.videoStreams.end(); ++i)
241 pids_to_record.insert(i->pid);
243 if (timing_pid == -1)
246 if (i != program.videoStreams.begin())
247 eDebugNoNewLine(", ");
248 eDebugNoNewLine("%04x", i->pid);
250 eDebugNoNewLine(")");
252 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
253 if (!program.audioStreams.empty())
255 eDebugNoNewLine(" (");
256 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
257 i(program.audioStreams.begin());
258 i != program.audioStreams.end(); ++i)
260 pids_to_record.insert(i->pid);
262 if (timing_pid == -1)
265 if (i != program.audioStreams.begin())
266 eDebugNoNewLine(", ");
267 eDebugNoNewLine("%04x", i->pid);
269 eDebugNoNewLine(")");
271 if (!program.subtitleStreams.empty())
273 eDebugNoNewLine(" (");
274 for (std::vector<eDVBServicePMTHandler::subtitleStream>::const_iterator
275 i(program.subtitleStreams.begin());
276 i != program.subtitleStreams.end(); ++i)
278 pids_to_record.insert(i->pid);
280 if (i != program.subtitleStreams.begin())
281 eDebugNoNewLine(", ");
282 eDebugNoNewLine("%04x", i->pid);
284 eDebugNoNewLine(")");
286 eDebugNoNewLine(", and the pcr pid is %04x", program.pcrPid);
287 if (program.pcrPid != 0x1fff)
288 pids_to_record.insert(program.pcrPid);
289 eDebug(", and the text pid is %04x", program.textPid);
290 if (program.textPid != -1)
291 pids_to_record.insert(program.textPid); // Videotext
293 /* find out which pids are NEW and which pids are obsolete.. */
294 std::set<int> new_pids, obsolete_pids;
296 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
297 m_pids_active.begin(), m_pids_active.end(),
298 std::inserter(new_pids, new_pids.begin()));
301 m_pids_active.begin(), m_pids_active.end(),
302 pids_to_record.begin(), pids_to_record.end(),
303 std::inserter(new_pids, new_pids.begin())
306 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
308 eDebug("ADD PID: %04x", *i);
309 m_record->addPID(*i);
312 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
314 eDebug("REMOVED PID: %04x", *i);
315 m_record->removePID(*i);
318 if (timing_pid != -1)
319 m_record->setTimingPID(timing_pid);
321 m_pids_active = pids_to_record;
323 if (m_state != stateRecording)
326 m_state = stateRecording;
331 m_event((iRecordableService*)this, evRecordRunning);
335 RESULT eDVBServiceRecord::frontendInfo(ePtr<iFrontendInformation> &ptr)
341 RESULT eDVBServiceRecord::connectEvent(const Slot2<void,iRecordableService*,int> &event, ePtr<eConnection> &connection)
343 connection = new eConnection((iRecordableService*)this, m_event.connect(event));
347 static PyObject *createTuple(int pid, const char *type)
349 PyObject *r = PyTuple_New(2);
350 PyTuple_SetItem(r, 0, PyInt_FromLong(pid));
351 PyTuple_SetItem(r, 1, PyString_FromString(type));
355 RESULT eDVBServiceRecord::stream(ePtr<iStreamableService> &ptr)
361 PyObject *eDVBServiceRecord::getStreamingData()
363 eDVBServicePMTHandler::program program;
364 if (!m_tuned || m_service_handler.getProgramInfo(program))
370 PyObject *r = program.createPythonObject();
371 ePtr<iDVBDemux> demux;
372 if (!m_service_handler.getDataDemux(demux))
375 demux->getCADemuxID(demux_id);
377 PyDict_SetItemString(r, "demux", PyInt_FromLong(demux_id));