consistently use iServiceHandler instead of eServiceCenter to use properly wrapped...
[openblackhole/openblackhole-enigma2.git] / lib / service / servicedvb.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/base/object.h>
3 #include <string>
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>
8
9 #include <lib/dvb/dvb.h>
10 #include <lib/dvb/db.h>
11
12 #include <lib/service/servicedvbrecord.h>
13 #include <lib/dvb/metaparser.h>
14 #include <lib/dvb/tstools.h>
15
16 class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
17 {
18         DECLARE_REF(eStaticServiceDVBPVRInformation);
19         eServiceReference m_ref;
20         eDVBMetaParser m_parser;
21 public:
22         eStaticServiceDVBPVRInformation(const eServiceReference &ref);
23         RESULT getName(const eServiceReference &ref, std::string &name);
24         int getLength(const eServiceReference &ref);
25 };
26
27 DEFINE_REF(eStaticServiceDVBPVRInformation);
28
29 eStaticServiceDVBPVRInformation::eStaticServiceDVBPVRInformation(const eServiceReference &ref)
30 {
31         m_ref = ref;
32         m_parser.parseFile(ref.path);
33 }
34
35 RESULT eStaticServiceDVBPVRInformation::getName(const eServiceReference &ref, std::string &name)
36 {
37         ASSERT(ref == m_ref);
38         name = m_parser.m_name.size() ? m_parser.m_name : ref.path;
39 }
40
41 int eStaticServiceDVBPVRInformation::getLength(const eServiceReference &ref)
42 {
43         ASSERT(ref == m_ref);
44         
45         eDVBTSTools tstools;
46         
47         if (tstools.openFile(ref.path.c_str()))
48                 return 0;
49
50         pts_t len;
51         if (tstools.calcLen(len))
52                 return 0;
53
54         return len / 90000;
55 }
56
57
58
59 class eDVBPVRServiceOfflineOperations: public iServiceOfflineOperations
60 {
61         DECLARE_REF(eDVBPVRServiceOfflineOperations);
62         eServiceReferenceDVB m_ref;
63 public:
64         eDVBPVRServiceOfflineOperations(const eServiceReference &ref);
65         
66         RESULT deleteFromDisk(int simulate);
67         RESULT getListOfFilenames(std::list<std::string> &);
68 };
69
70 DEFINE_REF(eDVBPVRServiceOfflineOperations);
71
72 eDVBPVRServiceOfflineOperations::eDVBPVRServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReferenceDVB&)ref)
73 {
74 }
75
76 RESULT eDVBPVRServiceOfflineOperations::deleteFromDisk(int simulate)
77 {
78         if (simulate)
79                 return 0;
80         else
81         {
82                 std::list<std::string> res;
83                 if (getListOfFilenames(res))
84                         return -1;
85                 
86                                 /* TODO: deferred removing.. */
87                 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
88                 {
89                         eDebug("Removing %s...", i->c_str());
90                         ::unlink(i->c_str());
91                 }
92                 
93                 return 0;
94         }
95 }
96
97 RESULT eDVBPVRServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
98 {
99         res.clear();
100         res.push_back(m_ref.path);
101         return 0;
102 }
103
104
105
106 DEFINE_REF(eServiceFactoryDVB)
107
108 eServiceFactoryDVB::eServiceFactoryDVB()
109 {
110         ePtr<eServiceCenter> sc;
111         
112         eServiceCenter::getPrivInstance(sc);
113         if (sc)
114                 sc->addServiceFactory(eServiceFactoryDVB::id, this);
115 }
116
117 eServiceFactoryDVB::~eServiceFactoryDVB()
118 {
119         ePtr<eServiceCenter> sc;
120         
121         eServiceCenter::getPrivInstance(sc);
122         if (sc)
123                 sc->removeServiceFactory(eServiceFactoryDVB::id);
124 }
125
126 DEFINE_REF(eDVBServiceList);
127
128 eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
129 {
130 }
131
132 eDVBServiceList::~eDVBServiceList()
133 {
134 }
135
136 RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
137 {
138         ePtr<iDVBChannelList> db;
139         ePtr<eDVBResourceManager> res;
140         
141         int err;
142         if ((err = eDVBResourceManager::getInstance(res)) != 0)
143         {
144                 eDebug("no resource manager");
145                 return err;
146         }
147         if ((err = res->getChannelList(db)) != 0)
148         {
149                 eDebug("no channel list");
150                 return err;
151         }
152         
153         ePtr<iDVBChannelListQuery> query;
154         
155         ePtr<eDVBChannelQuery> q;
156         
157         if (m_parent.path.size())
158                 eDVBChannelQuery::compile(q, m_parent.path);
159         
160         if ((err = db->startQuery(query, q)) != 0)
161         {
162                 eDebug("startQuery failed");
163                 return err;
164         }
165         
166         eServiceReferenceDVB ref;
167         
168         while (!query->getNextResult(ref))
169                 list.push_back(ref);
170         return 0;
171 }
172
173 RESULT eDVBServiceList::getNext(eServiceReference &)
174 {
175                 /* implement me */
176         return -1;
177 }
178
179 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
180 {
181         ePtr<eDVBService> service;
182         int r = lookupService(service, ref);
183         if (r)
184                 service = 0;
185                 // check resources...
186         ptr = new eDVBServicePlay(ref, service);
187         return 0;
188 }
189
190 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
191 {
192         ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
193         return 0;
194 }
195
196 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
197 {
198         ptr = new eDVBServiceList(ref);
199         return 0;
200 }
201
202 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
203 {
204                 /* do we have a PVR service? */
205         if (ref.path.size())
206         {
207                 ptr = new eStaticServiceDVBPVRInformation(ref);
208                 return 0;
209         } else
210         {
211                 ePtr<eDVBService> service;
212                 int r = lookupService(service, ref);
213                 if (r)
214                         return r;
215                         /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
216                 ptr = service;
217                 return 0;
218         }
219 }
220
221 RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
222 {
223         ptr = 0;
224         return -1;
225 }
226
227 RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServiceReference &ref)
228 {
229                         // TODO: handle the listing itself
230         // if (ref.... == -1) .. return "... bouquets ...";
231         // could be also done in another serviceFactory (with seperate ID) to seperate actual services and lists
232                         // TODO: cache
233         ePtr<iDVBChannelList> db;
234         ePtr<eDVBResourceManager> res;
235         
236         int err;
237         if ((err = eDVBResourceManager::getInstance(res)) != 0)
238         {
239                 eDebug("no resource manager");
240                 return err;
241         }
242         if ((err = res->getChannelList(db)) != 0)
243         {
244                 eDebug("no channel list");
245                 return err;
246         }
247         
248                 /* we are sure to have a ..DVB reference as the info() call was forwarded here according to it's ID. */
249         if ((err = db->getService((eServiceReferenceDVB&)ref, service)) != 0)
250         {
251                 eDebug("getService failed!");
252                 return err;
253         }
254
255         return 0;
256 }
257
258 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service): 
259         m_reference(ref), m_dvb_service(service)
260 {
261         m_is_pvr = !ref.path.empty();
262         
263         CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
264         CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
265         eDebug("DVB start (play)");
266 }
267
268 eDVBServicePlay::~eDVBServicePlay()
269 {
270         eDebug("DVB stop (play)");
271 }
272
273 void eDVBServicePlay::gotNewEvent()
274 {
275 #if 0
276                 // debug only
277         ePtr<eServiceEvent> m_event_now, m_event_next;
278         getEvent(m_event_now, 0);
279         getEvent(m_event_next, 1);
280
281         if (m_event_now)
282                 eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
283         if (m_event_next)
284                 eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
285 #endif
286         m_event((iPlayableService*)this, evUpdatedEventInfo);
287 }
288
289 void eDVBServicePlay::serviceEvent(int event)
290 {
291         eDebug("service event %d", event);
292         switch (event)
293         {
294         case eDVBServicePMTHandler::eventTuned:
295         {
296                 ePtr<iDVBDemux> m_demux;
297                 if (!m_service_handler.getDemux(m_demux))
298                 {
299 //                      eventStartedEventAcquisition
300                         m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
301                 } else
302                         eDebug("no event data available :( ");
303 //                      eventNoEvent
304                 break;
305         }
306         case eDVBServicePMTHandler::eventNewProgramInfo:
307         {
308                 int vpid = -1, apid = -1, pcrpid = -1;
309                 eDVBServicePMTHandler::program program;
310                 if (m_service_handler.getProgramInfo(program))
311                         eDebug("getting program info failed.");
312                 else
313                 {
314                         eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
315                         if (!program.videoStreams.empty())
316                         {
317                                 eDebugNoNewLine(" (");
318                                 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
319                                         i(program.videoStreams.begin()); 
320                                         i != program.videoStreams.end(); ++i)
321                                 {
322                                         if (vpid == -1)
323                                                 vpid = i->pid;
324                                         if (i != program.videoStreams.begin())
325                                                 eDebugNoNewLine(", ");
326                                         eDebugNoNewLine("%04x", i->pid);
327                                 }
328                                 eDebugNoNewLine(")");
329                         }
330                         eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
331                         if (!program.audioStreams.empty())
332                         {
333                                 eDebugNoNewLine(" (");
334                                 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
335                                         i(program.audioStreams.begin()); 
336                                         i != program.audioStreams.end(); ++i)
337                                 {
338                                         if (apid == -1)
339                                                 apid = i->pid;
340                                         if (i != program.audioStreams.begin())
341                                                 eDebugNoNewLine(", ");
342                                         eDebugNoNewLine("%04x", i->pid);
343                                 }
344                                 eDebugNoNewLine(")");
345                         }
346                         eDebug(", and the pcr pid is %04x", program.pcrPid);
347                         if (program.pcrPid != 0x1fff)
348                                 pcrpid = program.pcrPid;
349                 }
350                 
351                 if (!m_decoder)
352                 {
353                         ePtr<iDVBDemux> demux;
354                         m_service_handler.getDemux(demux);
355                         if (demux)
356                                 demux->getMPEGDecoder(m_decoder);
357                 }
358
359                 if (m_decoder)
360                 {
361                         m_decoder->setVideoPID(vpid);
362                         m_decoder->setAudioPID(apid, 0);
363                         if (!m_is_pvr)
364                                 m_decoder->setSyncPCR(pcrpid);
365                         else
366                                 m_decoder->setSyncPCR(-1);
367                         m_decoder->start();
368 // how we can do this better?
369 // update cache pid when the user changed the audio track or video track
370 // TODO handling of difference audio types.. default audio types..
371                                 
372                                 /* don't worry about non-existing services, nor pvr services */
373                         if (m_dvb_service && !m_is_pvr)
374                         {
375                                 m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
376                                 m_dvb_service->setCachePID(eDVBService::cAPID, apid);
377                                 m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
378                         }
379                 }
380                 
381                 break;
382         }
383         }
384 }
385
386 RESULT eDVBServicePlay::start()
387 {
388         int r;
389         eDebug("starting DVB service");
390         r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
391         m_event(this, evStart);
392 }
393
394 RESULT eDVBServicePlay::stop()
395 {
396         eDebug("stopping..");
397         return 0;
398 }
399
400 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
401 {
402         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
403         return 0;
404 }
405
406 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
407 {
408                 // not yet possible, maybe later...
409         ptr = 0;
410         return -1;
411 }
412
413 RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
414 {
415         if (m_is_pvr)
416         {
417                 ptr = this;
418                 return 0;
419         }
420         
421         ptr = 0;
422         return -1;
423 }
424
425 RESULT eDVBServicePlay::getLength(pts_t &len)
426 {
427         ePtr<iDVBPVRChannel> pvr_channel;
428         
429         if (m_service_handler.getPVRChannel(pvr_channel))
430         {
431                 eDebug("getPVRChannel failed!");
432                 return -1;
433         }
434         
435         return pvr_channel->getLength(len);
436 }
437
438 RESULT eDVBServicePlay::seekTo(pts_t to)
439 {
440         return -1;
441 }
442
443 RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
444 {
445         ePtr<iDVBPVRChannel> pvr_channel;
446         
447         if (m_service_handler.getPVRChannel(pvr_channel))
448                 return -1;
449         
450         return pvr_channel->getCurrentPosition(pos);
451 }
452
453 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
454 {
455         ptr = this;
456         return 0;
457 }
458
459 RESULT eDVBServicePlay::getName(std::string &name)
460 {
461         if (m_dvb_service)
462                 m_dvb_service->getName(m_reference, name);
463         else
464                 name = "DVB service";
465         return 0;
466 }
467
468 RESULT eDVBServicePlay::getEvent(ePtr<eServiceEvent> &evt, int nownext)
469 {
470         return m_event_handler.getEvent(evt, nownext);
471 }
472
473 DEFINE_REF(eDVBServicePlay)
474
475 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");