+eServiceFactoryDVB::eServiceFactoryDVB()
+{
+ ePtr<eServiceCenter> sc;
+
+ eServiceCenter::getPrivInstance(sc);
+ if (sc)
+ sc->addServiceFactory(eServiceFactoryDVB::id, this);
+}
+
+eServiceFactoryDVB::~eServiceFactoryDVB()
+{
+ ePtr<eServiceCenter> sc;
+
+ eServiceCenter::getPrivInstance(sc);
+ if (sc)
+ sc->removeServiceFactory(eServiceFactoryDVB::id);
+}
+
+DEFINE_REF(eDVBServiceList);
+
+eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
+{
+}
+
+eDVBServiceList::~eDVBServiceList()
+{
+}
+
+RESULT eDVBServiceList::startQuery()
+{
+ ePtr<iDVBChannelList> db;
+ ePtr<eDVBResourceManager> res;
+
+ int err;
+ if ((err = eDVBResourceManager::getInstance(res)) != 0)
+ {
+ eDebug("no resource manager");
+ return err;
+ }
+ if ((err = res->getChannelList(db)) != 0)
+ {
+ eDebug("no channel list");
+ return err;
+ }
+
+ ePtr<eDVBChannelQuery> q;
+
+ if (!m_parent.path.empty())
+ {
+ eDVBChannelQuery::compile(q, m_parent.path);
+ if (!q)
+ {
+ eDebug("compile query failed");
+ return err;
+ }
+ }
+
+ if ((err = db->startQuery(m_query, q, m_parent)) != 0)
+ {
+ eDebug("startQuery failed");
+ return err;
+ }
+
+ return 0;
+}
+
+RESULT eDVBServiceList::getContent(PyObject *list, bool sorted)
+{
+ eServiceReferenceDVB ref;
+
+ if (!m_query || !list || !PyList_Check(list))
+ return -1;
+
+ std::list<eServiceReferenceDVB> tmplist;
+
+ while (!m_query->getNextResult(ref))
+ tmplist.push_back(ref);
+
+ if (sorted)
+ tmplist.sort(iListableServiceCompare(this));
+
+ for (std::list<eServiceReferenceDVB>::iterator it(tmplist.begin());
+ it != tmplist.end(); ++it)
+ {
+ PyObject *refobj = New_eServiceReference(*it);
+ PyList_Append(list, refobj);
+ Py_DECREF(refobj);
+ }
+ return 0;
+}
+
+RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list, bool sorted)
+{
+ eServiceReferenceDVB ref;
+
+ if (!m_query)
+ return -1;
+
+ while (!m_query->getNextResult(ref))
+ list.push_back(ref);
+
+ if (sorted)
+ list.sort(iListableServiceCompare(this));
+
+ return 0;
+}
+
+RESULT eDVBServiceList::getNext(eServiceReference &ref)
+{
+ if (!m_query)
+ return -1;
+
+ return m_query->getNextResult((eServiceReferenceDVB&)ref);
+}
+
+int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
+{
+ return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
+}
+
+RESULT eDVBServiceList::startEdit(ePtr<iMutableServiceList> &res)
+{
+ if (m_parent.flags & eServiceReference::flagDirectory) // bouquet
+ {
+ ePtr<iDVBChannelList> db;
+ ePtr<eDVBResourceManager> resm;
+
+ if (eDVBResourceManager::getInstance(resm) || resm->getChannelList(db))
+ return -1;
+
+ if (db->getBouquet(m_parent, m_bouquet) != 0)
+ return -1;
+
+ res = this;
+
+ return 0;
+ }
+ res = 0;
+ return -1;
+}
+
+RESULT eDVBServiceList::addService(eServiceReference &ref)
+{
+ if (!m_bouquet)
+ return -1;
+ return m_bouquet->addService(ref);
+}
+
+RESULT eDVBServiceList::removeService(eServiceReference &ref)
+{
+ if (!m_bouquet)
+ return -1;
+ return m_bouquet->removeService(ref);
+}
+
+RESULT eDVBServiceList::moveService(eServiceReference &ref, int pos)
+{
+ if (!m_bouquet)
+ return -1;
+ return m_bouquet->moveService(ref, pos);
+}
+
+RESULT eDVBServiceList::flushChanges()
+{
+ if (!m_bouquet)
+ return -1;
+ return m_bouquet->flushChanges();
+}
+
+RESULT eDVBServiceList::setListName(const std::string &name)
+{
+ if (!m_bouquet)
+ return -1;
+ return m_bouquet->setListName(name);
+}
+
+RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
+{
+ ePtr<eDVBService> service;
+ int r = lookupService(service, ref);
+ if (r)
+ service = 0;
+ // check resources...
+ ptr = new eDVBServicePlay(ref, service);
+ return 0;
+}
+
+RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
+{
+ if (ref.path.empty())
+ {
+ ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
+ return 0;
+ } else
+ {
+ ptr = 0;
+ return -1;
+ }
+}
+
+RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
+{
+ ePtr<eDVBServiceList> list = new eDVBServiceList(ref);
+ if (list->startQuery())
+ {
+ ptr = 0;
+ return -1;
+ }
+
+ ptr = list;
+ return 0;
+}
+
+RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
+{
+ /* is a listable service? */
+ if ((ref.flags & eServiceReference::flagDirectory) == eServiceReference::flagDirectory) // bouquet
+ {
+ if ( !ref.name.empty() ) // satellites or providers list
+ ptr = new eStaticServiceDVBInformation;
+ else // a dvb bouquet
+ ptr = new eStaticServiceDVBBouquetInformation;
+ }
+ else if (!ref.path.empty()) /* do we have a PVR service? */
+ ptr = new eStaticServiceDVBPVRInformation(ref);
+ else // normal dvb service
+ {
+ ePtr<eDVBService> service;
+ if (lookupService(service, ref)) // no eDVBService avail for this reference ( Linkage Services... )
+ ptr = new eStaticServiceDVBInformation;
+ else
+ /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
+ ptr = service;
+ }
+ return 0;
+}
+
+RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
+{
+ if (ref.path.empty())
+ {
+ ptr = 0;
+ return -1;
+ } else
+ {
+ ptr = new eDVBPVRServiceOfflineOperations(ref);
+ return 0;
+ }
+}
+
+RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServiceReference &ref)
+{
+ // TODO: handle the listing itself
+ // if (ref.... == -1) .. return "... bouquets ...";
+ // could be also done in another serviceFactory (with seperate ID) to seperate actual services and lists
+ // TODO: cache
+ ePtr<iDVBChannelList> db;
+ ePtr<eDVBResourceManager> res;
+
+ int err;
+ if ((err = eDVBResourceManager::getInstance(res)) != 0)
+ {
+ eDebug("no resource manager");
+ return err;
+ }
+ if ((err = res->getChannelList(db)) != 0)
+ {
+ eDebug("no channel list");
+ return err;
+ }
+
+ /* we are sure to have a ..DVB reference as the info() call was forwarded here according to it's ID. */
+ if ((err = db->getService((eServiceReferenceDVB&)ref, service)) != 0)
+ {
+ eDebug("getService failed!");
+ return err;
+ }
+
+ return 0;
+}
+
+eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service):
+ m_reference(ref), m_dvb_service(service), m_is_paused(0)
+{
+ m_is_pvr = !ref.path.empty();
+
+ m_timeshift_enabled = m_timeshift_active = 0;
+ m_skipmode = 0;
+
+ CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
+ CONNECT(m_service_handler_timeshift.serviceEvent, eDVBServicePlay::serviceEventTimeshift);
+ CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
+
+ m_cuesheet_changed = 0;
+ m_cutlist_enabled = 1;
+}
+
+eDVBServicePlay::~eDVBServicePlay()
+{
+}
+
+void eDVBServicePlay::gotNewEvent()
+{
+#if 0
+ // debug only
+ ePtr<eServiceEvent> m_event_now, m_event_next;
+ getEvent(m_event_now, 0);
+ getEvent(m_event_next, 1);
+
+ if (m_event_now)
+ eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
+ if (m_event_next)
+ eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
+#endif
+ m_event((iPlayableService*)this, evUpdatedEventInfo);
+}
+
+void eDVBServicePlay::serviceEvent(int event)
+{
+ switch (event)
+ {
+ case eDVBServicePMTHandler::eventTuned:
+ {
+ ePtr<iDVBDemux> m_demux;
+ if (!m_service_handler.getDataDemux(m_demux))
+ {
+ eServiceReferenceDVB &ref = (eServiceReferenceDVB&) m_reference;
+ int sid = ref.getParentServiceID().get();
+ if (!sid)
+ sid = ref.getServiceID().get();
+ if ( ref.getParentTransportStreamID().get() &&
+ ref.getParentTransportStreamID() != ref.getTransportStreamID() )
+ m_event_handler.startOther(m_demux, sid);
+ else
+ m_event_handler.start(m_demux, sid);
+ }
+ break;
+ }
+ case eDVBServicePMTHandler::eventTuneFailed:
+ {
+ eDebug("DVB service failed to tune");
+ m_event((iPlayableService*)this, evTuneFailed);
+ break;
+ }
+ case eDVBServicePMTHandler::eventNewProgramInfo:
+ {
+ eDebug("eventNewProgramInfo %d %d", m_timeshift_enabled, m_timeshift_active);
+ if (m_timeshift_enabled)
+ updateTimeshiftPids();
+ if (!m_timeshift_active)
+ updateDecoder();
+ if (m_first_program_info && m_is_pvr)
+ {
+ m_first_program_info = 0;
+ seekTo(0);
+ }
+ m_event((iPlayableService*)this, evUpdatedInfo);
+ break;
+ }
+ case eDVBServicePMTHandler::eventEOF:
+ m_event((iPlayableService*)this, evEOF);
+ break;
+ case eDVBServicePMTHandler::eventSOF:
+ m_event((iPlayableService*)this, evSOF);
+ break;
+ }
+}
+
+void eDVBServicePlay::serviceEventTimeshift(int event)
+{
+ switch (event)
+ {
+ case eDVBServicePMTHandler::eventNewProgramInfo:
+ if (m_timeshift_active)
+ updateDecoder();
+ break;
+ case eDVBServicePMTHandler::eventEOF:
+ switchToLive();
+ break;
+ }
+}
+
+RESULT eDVBServicePlay::start()
+{
+ int r;
+ /* in pvr mode, we only want to use one demux. in tv mode, we're using
+ two (one for decoding, one for data source), as we must be prepared
+ to start recording from the data demux. */
+ m_cue = new eCueSheet();
+
+ m_first_program_info = 1;
+ eServiceReferenceDVB &service = (eServiceReferenceDVB&)m_reference;
+ r = m_service_handler.tune(service, m_is_pvr, m_cue);
+
+ /* inject EIT if there is a stored one */
+ if (m_is_pvr)
+ {
+ std::string filename = service.path;
+ filename.erase(filename.length()-2, 2);
+ filename+="eit";
+ int fd = ::open( filename.c_str(), O_RDONLY );
+ if ( fd > -1 )
+ {
+ __u8 buf[4096];
+ int rd = ::read(fd, buf, 4096);
+ ::close(fd);
+ if ( rd > 12 /*EIT_LOOP_SIZE*/ )
+ {
+ Event ev(buf);
+ ePtr<eServiceEvent> event = new eServiceEvent;
+ ePtr<eServiceEvent> empty;
+ event->parseFrom(&ev, (service.getTransportStreamID().get()<<16)|service.getOriginalNetworkID().get());
+ m_event_handler.inject(event, 0);
+ m_event_handler.inject(empty, 1);
+ eDebug("injected");
+ }
+ }
+ }
+
+ if (m_is_pvr)
+ loadCuesheet();
+
+ m_event(this, evStart);
+ m_event((iPlayableService*)this, evSeekableStatusChanged);
+ return 0;
+}
+
+RESULT eDVBServicePlay::stop()
+{
+ stopTimeshift(); /* in case timeshift was enabled, remove buffer etc. */
+
+ m_service_handler_timeshift.free();
+ m_service_handler.free();
+
+ if (m_is_pvr && m_cuesheet_changed)
+ saveCuesheet();
+
+ return 0;
+}
+
+RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
+{
+ connection = new eConnection((iPlayableService*)this, m_event.connect(event));
+ return 0;
+}
+
+RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
+{
+ /* note: we check for timeshift to be enabled,
+ not neccessary active. if you pause when timeshift
+ is not active, you should activate it when unpausing */
+ if ((!m_is_pvr) && (!m_timeshift_enabled))
+ {
+ ptr = 0;
+ return -1;
+ }
+
+ ptr = this;
+ return 0;
+}
+
+RESULT eDVBServicePlay::setSlowMotion(int ratio)
+{
+ if (m_decoder)
+ return m_decoder->setSlowMotion(ratio);
+ else
+ return -1;
+}
+
+RESULT eDVBServicePlay::setFastForward(int ratio)
+{
+ int skipmode, ffratio;
+
+ if (ratio > 8)
+ {
+ skipmode = ratio;
+ ffratio = 1;
+ } else if (ratio > 0)
+ {
+ skipmode = 0;
+ ffratio = ratio;
+ } else if (!ratio)
+ {
+ skipmode = 0;
+ ffratio = 0;
+ } else // if (ratio < 0)
+ {
+ skipmode = ratio;
+ ffratio = 1;
+ }
+
+ if (m_skipmode != skipmode)
+ {
+ eDebug("setting cue skipmode to %d", skipmode);
+ if (m_cue)
+ m_cue->setSkipmode(skipmode * 90000); /* convert to 90000 per second */
+ }
+
+ m_skipmode = skipmode;
+
+ if (!m_decoder)
+ return -1;
+
+ return m_decoder->setFastForward(ffratio);
+}
+
+RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
+{
+ if (m_is_pvr || m_timeshift_enabled)
+ {
+ ptr = this;
+ return 0;
+ }
+
+ ptr = 0;
+ return -1;
+}
+
+ /* TODO: when timeshift is enabled but not active, this doesn't work. */
+RESULT eDVBServicePlay::getLength(pts_t &len)
+{
+ ePtr<iDVBPVRChannel> pvr_channel;
+
+ if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
+ return -1;
+
+ return pvr_channel->getLength(len);
+}
+
+RESULT eDVBServicePlay::pause()
+{
+ if (!m_is_paused && m_decoder)
+ {
+ m_is_paused = 1;
+ return m_decoder->freeze(0);
+ } else
+ return -1;
+}
+
+RESULT eDVBServicePlay::unpause()
+{
+ if (m_is_paused && m_decoder)
+ {
+ m_is_paused = 0;
+ return m_decoder->unfreeze();
+ } else
+ return -1;
+}
+
+RESULT eDVBServicePlay::seekTo(pts_t to)
+{
+ eDebug("eDVBServicePlay::seekTo: jump %lld", to);
+
+ if (!m_decode_demux)
+ return -1;
+
+ ePtr<iDVBPVRChannel> pvr_channel;
+
+ if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
+ return -1;
+
+ if (!m_cue)
+ return -1;
+
+ m_cue->seekTo(0, to);
+ return 0;
+}
+
+RESULT eDVBServicePlay::seekRelative(int direction, pts_t to)
+{
+ eDebug("eDVBServicePlay::seekRelative: jump %d, %lld", direction, to);
+
+ if (!m_decode_demux)
+ return -1;
+
+ ePtr<iDVBPVRChannel> pvr_channel;
+
+ if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
+ return -1;
+
+ int mode = 1;
+
+ /* HACK until we have skip-AP api */
+ if ((to > 0) && (to < 100))
+ mode = 2;
+
+ to *= direction;
+
+ if (!m_cue)
+ return 0;
+
+ m_cue->seekTo(mode, to);
+ return 0;
+}
+
+RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
+{
+ ePtr<iDVBPVRChannel> pvr_channel;
+
+ if (!m_decode_demux)
+ return -1;
+
+ if ((m_timeshift_enabled ? m_service_handler_timeshift : m_service_handler).getPVRChannel(pvr_channel))
+ return -1;
+
+ int r = 0;
+
+ /* if there is a decoder, use audio or video PTS */
+ if (m_decoder)
+ {
+ r = m_decoder->getPTS(0, pos);
+ if (r)
+ return r;
+ }
+
+ /* fixup */
+ return pvr_channel->getCurrentPosition(m_decode_demux, pos, m_decoder ? 1 : 0);
+}
+
+RESULT eDVBServicePlay::setTrickmode(int trick)
+{
+ if (m_decoder)
+ m_decoder->setTrickmode(trick);
+ return 0;
+}
+
+RESULT eDVBServicePlay::isCurrentlySeekable()