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!");
69 const eBouquet *bouquet=0;
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);
181 DEFINE_REF(eServiceFactoryDVB)
183 eServiceFactoryDVB::eServiceFactoryDVB()
185 ePtr<eServiceCenter> sc;
187 eServiceCenter::getPrivInstance(sc);
189 sc->addServiceFactory(eServiceFactoryDVB::id, this);
192 eServiceFactoryDVB::~eServiceFactoryDVB()
194 ePtr<eServiceCenter> sc;
196 eServiceCenter::getPrivInstance(sc);
198 sc->removeServiceFactory(eServiceFactoryDVB::id);
201 DEFINE_REF(eDVBServiceList);
203 eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
207 eDVBServiceList::~eDVBServiceList()
211 RESULT eDVBServiceList::startQuery()
213 ePtr<iDVBChannelList> db;
214 ePtr<eDVBResourceManager> res;
217 if ((err = eDVBResourceManager::getInstance(res)) != 0)
219 eDebug("no resource manager");
222 if ((err = res->getChannelList(db)) != 0)
224 eDebug("no channel list");
228 ePtr<eDVBChannelQuery> q;
230 if (m_parent.path.size())
232 eDVBChannelQuery::compile(q, m_parent.path);
235 eDebug("compile query failed");
240 if ((err = db->startQuery(m_query, q, m_parent)) != 0)
242 eDebug("startQuery failed");
249 RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
251 eServiceReferenceDVB ref;
256 while (!m_query->getNextResult(ref))
261 RESULT eDVBServiceList::getNext(eServiceReference &ref)
266 return m_query->getNextResult((eServiceReferenceDVB&)ref);
269 int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
271 return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
274 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
276 ePtr<eDVBService> service;
277 int r = lookupService(service, ref);
280 // check resources...
281 ptr = new eDVBServicePlay(ref, service);
285 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
287 ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
291 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
293 ePtr<eDVBServiceList> list = new eDVBServiceList(ref);
294 if (list->startQuery())
304 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
306 /* do we have a PVR service? */
307 if (ref.flags & eServiceReference::flagDirectory) // bouquet
309 ptr = new eStaticServiceDVBBouquetInformation;
312 else if (ref.path.size())
314 ptr = new eStaticServiceDVBPVRInformation(ref);
319 ePtr<eDVBService> service;
320 int r = lookupService(service, ref);
322 ptr = new eStaticServiceDVBInformation;
324 /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
330 RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
336 RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServiceReference &ref)
338 // TODO: handle the listing itself
339 // if (ref.... == -1) .. return "... bouquets ...";
340 // could be also done in another serviceFactory (with seperate ID) to seperate actual services and lists
342 ePtr<iDVBChannelList> db;
343 ePtr<eDVBResourceManager> res;
346 if ((err = eDVBResourceManager::getInstance(res)) != 0)
348 eDebug("no resource manager");
351 if ((err = res->getChannelList(db)) != 0)
353 eDebug("no channel list");
357 /* we are sure to have a ..DVB reference as the info() call was forwarded here according to it's ID. */
358 if ((err = db->getService((eServiceReferenceDVB&)ref, service)) != 0)
360 eDebug("getService failed!");
367 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service):
368 m_reference(ref), m_dvb_service(service), m_service_handler(0)
370 m_is_pvr = !ref.path.empty();
372 CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
373 CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
376 eDVBServicePlay::~eDVBServicePlay()
380 void eDVBServicePlay::gotNewEvent()
384 ePtr<eServiceEvent> m_event_now, m_event_next;
385 getEvent(m_event_now, 0);
386 getEvent(m_event_next, 1);
389 eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
391 eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
393 m_event((iPlayableService*)this, evUpdatedEventInfo);
396 void eDVBServicePlay::serviceEvent(int event)
400 case eDVBServicePMTHandler::eventTuned:
402 ePtr<iDVBDemux> m_demux;
403 if (!m_service_handler.getDemux(m_demux))
405 // eventStartedEventAcquisition
406 m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
411 case eDVBServicePMTHandler::eventTuneFailed:
413 eDebug("DVB service failed to tune");
414 m_event((iPlayableService*)this, evTuneFailed);
417 case eDVBServicePMTHandler::eventNewProgramInfo:
419 int vpid = -1, apid = -1, pcrpid = -1;
420 eDVBServicePMTHandler::program program;
421 if (m_service_handler.getProgramInfo(program))
422 eDebug("getting program info failed.");
425 eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
426 if (!program.videoStreams.empty())
428 eDebugNoNewLine(" (");
429 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
430 i(program.videoStreams.begin());
431 i != program.videoStreams.end(); ++i)
435 if (i != program.videoStreams.begin())
436 eDebugNoNewLine(", ");
437 eDebugNoNewLine("%04x", i->pid);
439 eDebugNoNewLine(")");
441 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
442 if (!program.audioStreams.empty())
444 eDebugNoNewLine(" (");
445 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
446 i(program.audioStreams.begin());
447 i != program.audioStreams.end(); ++i)
451 if (i != program.audioStreams.begin())
452 eDebugNoNewLine(", ");
453 eDebugNoNewLine("%04x", i->pid);
455 eDebugNoNewLine(")");
457 eDebug(", and the pcr pid is %04x", program.pcrPid);
458 if (program.pcrPid != 0x1fff)
459 pcrpid = program.pcrPid;
464 ePtr<iDVBDemux> demux;
465 m_service_handler.getDemux(demux);
467 demux->getMPEGDecoder(m_decoder);
472 m_decoder->setVideoPID(vpid);
473 m_decoder->setAudioPID(apid, 0);
475 m_decoder->setSyncPCR(pcrpid);
477 m_decoder->setSyncPCR(-1);
479 // how we can do this better?
480 // update cache pid when the user changed the audio track or video track
481 // TODO handling of difference audio types.. default audio types..
483 /* don't worry about non-existing services, nor pvr services */
484 if (m_dvb_service && !m_is_pvr)
486 m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
487 m_dvb_service->setCachePID(eDVBService::cAPID, apid);
488 m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
497 RESULT eDVBServicePlay::start()
500 eDebug("starting DVB service");
501 r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
502 eDebug("tune result: %d", r);
503 m_event(this, evStart);
507 RESULT eDVBServicePlay::stop()
509 eDebug("stopping..");
513 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
515 connection = new eConnection((iPlayableService*)this, m_event.connect(event));
519 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
521 // not yet possible, maybe later...
526 RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
538 RESULT eDVBServicePlay::getLength(pts_t &len)
540 ePtr<iDVBPVRChannel> pvr_channel;
542 if (m_service_handler.getPVRChannel(pvr_channel))
544 eDebug("getPVRChannel failed!");
548 return pvr_channel->getLength(len);
551 RESULT eDVBServicePlay::seekTo(pts_t to)
556 RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
558 ePtr<iDVBPVRChannel> pvr_channel;
560 if (m_service_handler.getPVRChannel(pvr_channel))
563 return pvr_channel->getCurrentPosition(pos);
566 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
572 RESULT eDVBServicePlay::getName(std::string &name)
576 m_dvb_service->getName(m_reference, name);
580 name = "DVB service";
584 RESULT eDVBServicePlay::getEvent(ePtr<eServiceEvent> &evt, int nownext)
586 return m_event_handler.getEvent(evt, nownext);
589 DEFINE_REF(eDVBServicePlay)
591 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");