1 #include <lib/base/eerror.h>
2 #include <lib/base/object.h>
4 #include <lib/service/servicedvb.h>
5 #include <lib/service/service.h>
6 #include <lib/base/init_num.h>
7 #include <lib/base/init.h>
9 #include <lib/dvb/dvb.h>
10 #include <lib/dvb/db.h>
12 #include <lib/service/servicedvbrecord.h>
13 #include <lib/dvb/metaparser.h>
14 #include <lib/dvb/tstools.h>
16 class eStaticServiceDVBInformation: public iStaticServiceInformation
18 DECLARE_REF(eStaticServiceDVBInformation);
20 RESULT getName(const eServiceReference &ref, std::string &name);
21 int getLength(const eServiceReference &ref);
24 DEFINE_REF(eStaticServiceDVBInformation);
26 RESULT eStaticServiceDVBInformation::getName(const eServiceReference &ref, std::string &name)
28 if ( ref.name.length() )
37 int eStaticServiceDVBInformation::getLength(const eServiceReference &ref)
42 class eStaticServiceDVBBouquetInformation: public iStaticServiceInformation
44 DECLARE_REF(eStaticServiceDVBBouquetInformation);
46 RESULT getName(const eServiceReference &ref, std::string &name);
47 int getLength(const eServiceReference &ref);
50 DEFINE_REF(eStaticServiceDVBBouquetInformation);
52 RESULT eStaticServiceDVBBouquetInformation::getName(const eServiceReference &ref, std::string &name)
54 ePtr<iDVBChannelList> db;
55 ePtr<eDVBResourceManager> res;
58 if ((err = eDVBResourceManager::getInstance(res)) != 0)
60 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no resource manager!");
63 if ((err = res->getChannelList(db)) != 0)
65 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no channel list!");
70 if ((err = db->getBouquet(ref, bouquet)) != 0)
72 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. getBouquet failed!");
76 if ( bouquet && bouquet->m_bouquet_name.length() )
78 name = "[Bouquet] " + bouquet->m_bouquet_name;
85 int eStaticServiceDVBBouquetInformation::getLength(const eServiceReference &ref)
90 class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
92 DECLARE_REF(eStaticServiceDVBPVRInformation);
93 eServiceReference m_ref;
94 eDVBMetaParser m_parser;
96 eStaticServiceDVBPVRInformation(const eServiceReference &ref);
97 RESULT getName(const eServiceReference &ref, std::string &name);
98 int getLength(const eServiceReference &ref);
101 DEFINE_REF(eStaticServiceDVBPVRInformation);
103 eStaticServiceDVBPVRInformation::eStaticServiceDVBPVRInformation(const eServiceReference &ref)
106 m_parser.parseFile(ref.path);
109 RESULT eStaticServiceDVBPVRInformation::getName(const eServiceReference &ref, std::string &name)
111 ASSERT(ref == m_ref);
112 name = m_parser.m_name.size() ? m_parser.m_name : ref.path;
116 int eStaticServiceDVBPVRInformation::getLength(const eServiceReference &ref)
118 ASSERT(ref == m_ref);
122 if (tstools.openFile(ref.path.c_str()))
126 if (tstools.calcLen(len))
134 class eDVBPVRServiceOfflineOperations: public iServiceOfflineOperations
136 DECLARE_REF(eDVBPVRServiceOfflineOperations);
137 eServiceReferenceDVB m_ref;
139 eDVBPVRServiceOfflineOperations(const eServiceReference &ref);
141 RESULT deleteFromDisk(int simulate);
142 RESULT getListOfFilenames(std::list<std::string> &);
145 DEFINE_REF(eDVBPVRServiceOfflineOperations);
147 eDVBPVRServiceOfflineOperations::eDVBPVRServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReferenceDVB&)ref)
151 RESULT eDVBPVRServiceOfflineOperations::deleteFromDisk(int simulate)
157 std::list<std::string> res;
158 if (getListOfFilenames(res))
161 /* TODO: deferred removing.. */
162 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
164 eDebug("Removing %s...", i->c_str());
165 ::unlink(i->c_str());
172 RESULT eDVBPVRServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
175 res.push_back(m_ref.path);
176 res.push_back(m_ref.path + ".meta");
180 DEFINE_REF(eServiceFactoryDVB)
182 eServiceFactoryDVB::eServiceFactoryDVB()
184 ePtr<eServiceCenter> sc;
186 eServiceCenter::getPrivInstance(sc);
188 sc->addServiceFactory(eServiceFactoryDVB::id, this);
191 eServiceFactoryDVB::~eServiceFactoryDVB()
193 ePtr<eServiceCenter> sc;
195 eServiceCenter::getPrivInstance(sc);
197 sc->removeServiceFactory(eServiceFactoryDVB::id);
200 DEFINE_REF(eDVBServiceList);
202 eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
206 eDVBServiceList::~eDVBServiceList()
210 RESULT eDVBServiceList::startQuery()
212 ePtr<iDVBChannelList> db;
213 ePtr<eDVBResourceManager> res;
216 if ((err = eDVBResourceManager::getInstance(res)) != 0)
218 eDebug("no resource manager");
221 if ((err = res->getChannelList(db)) != 0)
223 eDebug("no channel list");
227 ePtr<eDVBChannelQuery> q;
229 if (!m_parent.path.empty())
231 eDVBChannelQuery::compile(q, m_parent.path);
234 eDebug("compile query failed");
239 if ((err = db->startQuery(m_query, q, m_parent)) != 0)
241 eDebug("startQuery failed");
248 RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
250 eServiceReferenceDVB ref;
255 while (!m_query->getNextResult(ref))
260 RESULT eDVBServiceList::getNext(eServiceReference &ref)
265 return m_query->getNextResult((eServiceReferenceDVB&)ref);
268 int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
270 return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
273 RESULT eDVBServiceList::startEdit(ePtr<iMutableServiceList> &res)
275 if (m_parent.flags & eServiceReference::flagDirectory) // bouquet
277 ePtr<iDVBChannelList> db;
278 ePtr<eDVBResourceManager> resm;
280 if (eDVBResourceManager::getInstance(resm) || resm->getChannelList(db))
283 if (db->getBouquet(m_parent, m_bouquet) != 0)
294 RESULT eDVBServiceList::addService(eServiceReference &ref)
298 return m_bouquet->addService(ref);
301 RESULT eDVBServiceList::removeService(eServiceReference &ref)
305 return m_bouquet->removeService(ref);
308 RESULT eDVBServiceList::moveService(eServiceReference &ref, int pos)
312 return m_bouquet->moveService(ref, pos);
315 RESULT eDVBServiceList::flushChanges()
319 return m_bouquet->flushChanges();
322 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
324 ePtr<eDVBService> service;
325 int r = lookupService(service, ref);
328 // check resources...
329 ptr = new eDVBServicePlay(ref, service);
333 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
335 if (ref.path.empty())
337 ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
346 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
348 ePtr<eDVBServiceList> list = new eDVBServiceList(ref);
349 if (list->startQuery())
359 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
361 /* do we have a PVR service? */
362 if (ref.flags & eServiceReference::flagDirectory) // bouquet
364 ptr = new eStaticServiceDVBBouquetInformation;
367 else if (!ref.path.empty())
369 ptr = new eStaticServiceDVBPVRInformation(ref);
374 ePtr<eDVBService> service;
375 int r = lookupService(service, ref);
377 ptr = new eStaticServiceDVBInformation;
379 /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
385 RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
387 if (ref.path.empty())
393 ptr = new eDVBPVRServiceOfflineOperations(ref);
398 RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServiceReference &ref)
400 // TODO: handle the listing itself
401 // if (ref.... == -1) .. return "... bouquets ...";
402 // could be also done in another serviceFactory (with seperate ID) to seperate actual services and lists
404 ePtr<iDVBChannelList> db;
405 ePtr<eDVBResourceManager> res;
408 if ((err = eDVBResourceManager::getInstance(res)) != 0)
410 eDebug("no resource manager");
413 if ((err = res->getChannelList(db)) != 0)
415 eDebug("no channel list");
419 /* we are sure to have a ..DVB reference as the info() call was forwarded here according to it's ID. */
420 if ((err = db->getService((eServiceReferenceDVB&)ref, service)) != 0)
422 eDebug("getService failed!");
429 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service):
430 m_reference(ref), m_dvb_service(service), m_service_handler(0), m_is_paused(0)
432 m_is_pvr = !ref.path.empty();
434 CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
435 CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
438 eDVBServicePlay::~eDVBServicePlay()
442 void eDVBServicePlay::gotNewEvent()
446 ePtr<eServiceEvent> m_event_now, m_event_next;
447 getEvent(m_event_now, 0);
448 getEvent(m_event_next, 1);
451 eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
453 eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
455 m_event((iPlayableService*)this, evUpdatedEventInfo);
458 void eDVBServicePlay::serviceEvent(int event)
462 case eDVBServicePMTHandler::eventTuned:
464 ePtr<iDVBDemux> m_demux;
465 if (!m_service_handler.getDemux(m_demux))
467 // eventStartedEventAcquisition
468 m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
473 case eDVBServicePMTHandler::eventTuneFailed:
475 eDebug("DVB service failed to tune");
476 m_event((iPlayableService*)this, evTuneFailed);
479 case eDVBServicePMTHandler::eventNewProgramInfo:
481 int vpid = -1, apid = -1, apidtype = -1, pcrpid = -1;
482 eDVBServicePMTHandler::program program;
483 if (m_service_handler.getProgramInfo(program))
484 eDebug("getting program info failed.");
487 eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
488 if (!program.videoStreams.empty())
490 eDebugNoNewLine(" (");
491 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
492 i(program.videoStreams.begin());
493 i != program.videoStreams.end(); ++i)
497 if (i != program.videoStreams.begin())
498 eDebugNoNewLine(", ");
499 eDebugNoNewLine("%04x", i->pid);
501 eDebugNoNewLine(")");
503 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
504 if (!program.audioStreams.empty())
506 eDebugNoNewLine(" (");
507 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
508 i(program.audioStreams.begin());
509 i != program.audioStreams.end(); ++i)
516 if (i != program.audioStreams.begin())
517 eDebugNoNewLine(", ");
518 eDebugNoNewLine("%04x", i->pid);
520 eDebugNoNewLine(")");
522 eDebug(", and the pcr pid is %04x", program.pcrPid);
523 if (program.pcrPid != 0x1fff)
524 pcrpid = program.pcrPid;
529 ePtr<iDVBDemux> demux;
530 m_service_handler.getDemux(demux);
532 demux->getMPEGDecoder(m_decoder);
537 m_decoder->setVideoPID(vpid);
538 m_current_audio_stream = 0;
539 m_decoder->setAudioPID(apid, apidtype);
541 m_decoder->setSyncPCR(pcrpid);
543 m_decoder->setSyncPCR(-1);
545 // how we can do this better?
546 // update cache pid when the user changed the audio track or video track
547 // TODO handling of difference audio types.. default audio types..
549 /* don't worry about non-existing services, nor pvr services */
550 if (m_dvb_service && !m_is_pvr)
552 m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
553 m_dvb_service->setCachePID(eDVBService::cAPID, apid);
554 m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
563 RESULT eDVBServicePlay::start()
566 eDebug("starting DVB service");
567 r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
568 eDebug("tune result: %d", r);
569 m_event(this, evStart);
573 RESULT eDVBServicePlay::stop()
575 eDebug("stopping..");
579 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
581 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
585 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
597 RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
609 RESULT eDVBServicePlay::getLength(pts_t &len)
611 ePtr<iDVBPVRChannel> pvr_channel;
613 if (m_service_handler.getPVRChannel(pvr_channel))
615 eDebug("getPVRChannel failed!");
619 return pvr_channel->getLength(len);
622 RESULT eDVBServicePlay::pause()
624 if (!m_is_paused && m_decoder)
627 return m_decoder->freeze(0);
632 RESULT eDVBServicePlay::unpause()
634 if (m_is_paused && m_decoder)
637 return m_decoder->unfreeze();
642 RESULT eDVBServicePlay::seekTo(pts_t to)
647 RESULT eDVBServicePlay::seekRelative(int direction, pts_t to)
649 eDebug("eDVBServicePlay::seekRelative: jump %d, %lld", direction, to);
651 ePtr<iDVBPVRChannel> pvr_channel;
653 if (m_service_handler.getPVRChannel(pvr_channel))
658 ePtr<iDVBDemux> demux;
659 m_service_handler.getDemux(demux);
663 return pvr_channel->seekTo(demux, 1, to);
666 RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
668 ePtr<iDVBPVRChannel> pvr_channel;
670 if (m_service_handler.getPVRChannel(pvr_channel))
673 ePtr<iDVBDemux> demux;
674 m_service_handler.getDemux(demux);
678 return pvr_channel->getCurrentPosition(demux, pos);
681 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
687 RESULT eDVBServicePlay::audioTracks(ePtr<iAudioTrackSelection> &ptr)
693 RESULT eDVBServicePlay::getName(std::string &name)
697 m_dvb_service->getName(m_reference, name);
701 name = "DVB service";
705 RESULT eDVBServicePlay::getEvent(ePtr<eServiceEvent> &evt, int nownext)
707 return m_event_handler.getEvent(evt, nownext);
710 int eDVBServicePlay::getInfo(int w)
712 eDVBServicePMTHandler::program program;
714 if (m_service_handler.getProgramInfo(program))
719 case sVideoPID: if (program.videoStreams.empty()) return -1; return program.videoStreams[0].pid;
720 case sAudioPID: if (program.audioStreams.empty()) return -1; return program.videoStreams[m_current_audio_stream].pid;
721 case sPCRPID: return program.pcrPid;
722 case sPMTPID: return program.pmtPid;
723 case sTXTPID: return -1;
725 case sSID: return ((const eServiceReferenceDVB&)m_reference).getServiceID().get();
726 case sONID: return ((const eServiceReferenceDVB&)m_reference).getOriginalNetworkID().get();
727 case sTSID: return ((const eServiceReferenceDVB&)m_reference).getTransportStreamID().get();
728 case sNamespace: return ((const eServiceReferenceDVB&)m_reference).getDVBNamespace().get();
729 case sProvider: if (!m_dvb_service) return -1; return -2;
735 std::string eDVBServicePlay::getInfoString(int w)
740 if (!m_dvb_service) return "";
741 return m_dvb_service->m_provider_name;
747 int eDVBServicePlay::getNumberOfTracks()
749 eDVBServicePMTHandler::program program;
750 if (m_service_handler.getProgramInfo(program))
752 return program.audioStreams.size();
755 RESULT eDVBServicePlay::selectTrack(unsigned int i)
757 int ret = selectAudioStream(i);
759 if (m_decoder->start())
765 RESULT eDVBServicePlay::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
767 eDVBServicePMTHandler::program program;
769 if (m_service_handler.getProgramInfo(program))
772 if (i >= program.audioStreams.size())
775 if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atMPEG)
776 info.m_description = "MPEG";
777 else if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atAC3)
778 info.m_description = "AC3";
779 else if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atDTS)
780 info.m_description = "DTS";
782 info.m_description = "???";
784 /* CHECK here for component tag override. */
785 info.m_language = program.audioStreams[i].language_code;
790 int eDVBServicePlay::selectAudioStream(int i)
792 eDVBServicePMTHandler::program program;
794 if (m_service_handler.getProgramInfo(program))
797 if (i >= program.audioStreams.size())
803 if (m_decoder->setAudioPID(program.audioStreams[i].pid, program.audioStreams[i].type))
806 m_current_audio_stream = i;
811 DEFINE_REF(eDVBServicePlay)
813 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");