+ if (!evt->getLinkageService(sub, m_reference, n))
+ return 0;
+ }
+ sub.type=eServiceReference::idInvalid;
+ return -1;
+}
+
+RESULT eDVBServicePlay::startTimeshift()
+{
+ ePtr<iDVBDemux> demux;
+
+ eDebug("Start timeshift!");
+
+ if (m_timeshift_enabled)
+ return -1;
+
+ /* start recording with the data demux. */
+ if (m_service_handler.getDataDemux(demux))
+ return -2;
+
+ demux->createTSRecorder(m_record);
+ if (!m_record)
+ return -3;
+
+ char templ[]=TSPATH "/timeshift.XXXXXX";
+ m_timeshift_fd = mkstemp(templ);
+ m_timeshift_file = templ;
+
+ eDebug("recording to %s", templ);
+
+ if (m_timeshift_fd < 0)
+ {
+ m_record = 0;
+ return -4;
+ }
+
+ m_record->setTargetFD(m_timeshift_fd);
+
+ m_timeshift_enabled = 1;
+
+ updateTimeshiftPids();
+ m_record->start();
+
+ return 0;
+}
+
+RESULT eDVBServicePlay::stopTimeshift()
+{
+ if (!m_timeshift_enabled)
+ return -1;
+
+ switchToLive();
+
+ m_timeshift_enabled = 0;
+
+ m_record->stop();
+ m_record = 0;
+
+ close(m_timeshift_fd);
+ eDebug("remove timeshift file");
+ remove(m_timeshift_file.c_str());
+
+ return 0;
+}
+
+int eDVBServicePlay::isTimeshiftActive()
+{
+ return m_timeshift_enabled && m_timeshift_active;
+}
+
+RESULT eDVBServicePlay::activateTimeshift()
+{
+ if (!m_timeshift_enabled)
+ return -1;
+
+ if (!m_timeshift_active)
+ {
+ switchToTimeshift();
+ return 0;
+ }
+
+ return -2;
+}
+
+PyObject *eDVBServicePlay::getCutList()
+{
+ PyObject *list = PyList_New(0);
+
+ for (std::multiset<struct cueEntry>::iterator i(m_cue_entries.begin()); i != m_cue_entries.end(); ++i)
+ {
+ PyObject *tuple = PyTuple_New(2);
+ PyTuple_SetItem(tuple, 0, PyLong_FromLongLong(i->where));
+ PyTuple_SetItem(tuple, 1, PyInt_FromLong(i->what));
+ PyList_Append(list, tuple);
+ Py_DECREF(tuple);
+ }
+
+ return list;
+}
+
+void eDVBServicePlay::setCutList(PyObject *list)
+{
+ if (!PyList_Check(list))
+ return;
+ int size = PyList_Size(list);
+ int i;
+
+ m_cue_entries.clear();
+
+ for (i=0; i<size; ++i)
+ {
+ PyObject *tuple = PyList_GetItem(list, i);
+ if (!PyTuple_Check(tuple))
+ {
+ eDebug("non-tuple in cutlist");
+ continue;
+ }
+ if (PyTuple_Size(tuple) != 2)
+ {
+ eDebug("cutlist entries need to be a 2-tuple");
+ continue;
+ }
+ PyObject *ppts = PyTuple_GetItem(tuple, 0), *ptype = PyTuple_GetItem(tuple, 1);
+ if (!(PyLong_Check(ppts) && PyInt_Check(ptype)))
+ {
+ eDebug("cutlist entries need to be (pts, type)-tuples (%d %d)", PyLong_Check(ppts), PyInt_Check(ptype));
+ continue;
+ }
+ pts_t pts = PyLong_AsLongLong(ppts);
+ int type = PyInt_AsLong(ptype);
+ m_cue_entries.insert(cueEntry(pts, type));
+ eDebug("adding %08llx, %d", pts, type);
+ }
+ m_cuesheet_changed = 1;
+
+ cutlistToCuesheet();
+ m_event((iPlayableService*)this, evCuesheetChanged);
+}
+
+void eDVBServicePlay::setCutListEnable(int enable)
+{
+ m_cutlist_enabled = enable;
+ cutlistToCuesheet();
+}
+
+void eDVBServicePlay::updateTimeshiftPids()
+{
+ if (!m_record)
+ return;
+
+ eDVBServicePMTHandler::program program;
+ if (m_service_handler.getProgramInfo(program))
+ return;
+ else
+ {
+ std::set<int> pids_to_record;
+ pids_to_record.insert(0); // PAT
+ if (program.pmtPid != -1)
+ pids_to_record.insert(program.pmtPid); // PMT
+
+ if (program.textPid != -1)
+ pids_to_record.insert(program.textPid); // Videotext
+
+ for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
+ i(program.videoStreams.begin());
+ i != program.videoStreams.end(); ++i)
+ pids_to_record.insert(i->pid);
+
+ for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
+ i(program.audioStreams.begin());
+ i != program.audioStreams.end(); ++i)
+ pids_to_record.insert(i->pid);
+
+ std::set<int> new_pids, obsolete_pids;
+
+ std::set_difference(pids_to_record.begin(), pids_to_record.end(),
+ m_pids_active.begin(), m_pids_active.end(),
+ std::inserter(new_pids, new_pids.begin()));
+
+ std::set_difference(
+ m_pids_active.begin(), m_pids_active.end(),
+ pids_to_record.begin(), pids_to_record.end(),
+ std::inserter(new_pids, new_pids.begin())
+ );
+
+ for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
+ m_record->addPID(*i);
+
+ for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
+ m_record->removePID(*i);
+ }
+}
+
+void eDVBServicePlay::switchToLive()
+{
+ if (!m_timeshift_active)
+ return;
+
+ m_cue = 0;
+ m_decoder = 0;
+ m_decode_demux = 0;
+ m_teletext_parser = 0;
+ m_radiotext_parser = 0;
+ m_new_subtitle_page_connection = 0;
+ m_radiotext_updated_connection = 0;
+
+ /* free the timeshift service handler, we need the resources */
+ m_service_handler_timeshift.free();
+ m_timeshift_active = 0;
+
+ m_event((iPlayableService*)this, evSeekableStatusChanged);
+
+ updateDecoder();
+}
+
+void eDVBServicePlay::switchToTimeshift()
+{
+ if (m_timeshift_active)
+ return;
+
+ m_decode_demux = 0;
+ m_decoder = 0;
+ m_teletext_parser = 0;
+ m_radiotext_parser = 0;
+ m_new_subtitle_page_connection = 0;
+ m_radiotext_updated_connection = 0;
+
+ m_timeshift_active = 1;
+
+ m_event((iPlayableService*)this, evSeekableStatusChanged);
+
+ eServiceReferenceDVB r = (eServiceReferenceDVB&)m_reference;
+ r.path = m_timeshift_file;
+
+ m_cue = new eCueSheet();
+ m_service_handler_timeshift.tune(r, 1, m_cue); /* use the decoder demux for everything */
+ updateDecoder(); /* mainly to switch off PCR */
+}
+
+void eDVBServicePlay::updateDecoder()
+{
+ int vpid = -1, vpidtype = -1, apid = -1, apidtype = -1, pcrpid = -1, tpid = -1, achannel = -1, ac3_delay=-1, pcm_delay=-1;
+
+ eDVBServicePMTHandler &h = m_timeshift_active ? m_service_handler_timeshift : m_service_handler;
+
+ bool defaultac3=false;
+ std::string default_ac3;
+
+ if (!ePythonConfigQuery::getConfigValue("config.av.defaultac3", default_ac3))
+ defaultac3 = default_ac3 == "enable";
+
+ eDVBServicePMTHandler::program program;
+ if (h.getProgramInfo(program))
+ eDebug("getting program info failed.");
+ else
+ {
+ eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
+ if (!program.videoStreams.empty())