start decoder when we select different audio track
[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 eStaticServiceDVBInformation: public iStaticServiceInformation
17 {
18         DECLARE_REF(eStaticServiceDVBInformation);
19 public:
20         RESULT getName(const eServiceReference &ref, std::string &name);
21         int getLength(const eServiceReference &ref);
22 };
23
24 DEFINE_REF(eStaticServiceDVBInformation);
25
26 RESULT eStaticServiceDVBInformation::getName(const eServiceReference &ref, std::string &name)
27 {
28         if ( ref.name.length() )
29         {
30                 name = ref.name;
31                 return 0;
32         }
33         else
34                 return -1;
35 }
36
37 int eStaticServiceDVBInformation::getLength(const eServiceReference &ref)
38 {
39         return -1;
40 }
41
42 class eStaticServiceDVBBouquetInformation: public iStaticServiceInformation
43 {
44         DECLARE_REF(eStaticServiceDVBBouquetInformation);
45 public:
46         RESULT getName(const eServiceReference &ref, std::string &name);
47         int getLength(const eServiceReference &ref);
48 };
49
50 DEFINE_REF(eStaticServiceDVBBouquetInformation);
51
52 RESULT eStaticServiceDVBBouquetInformation::getName(const eServiceReference &ref, std::string &name)
53 {
54         ePtr<iDVBChannelList> db;
55         ePtr<eDVBResourceManager> res;
56
57         int err;
58         if ((err = eDVBResourceManager::getInstance(res)) != 0)
59         {
60                 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no resource manager!");
61                 return err;
62         }
63         if ((err = res->getChannelList(db)) != 0)
64         {
65                 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no channel list!");
66                 return err;
67         }
68
69         eBouquet *bouquet=0;
70         if ((err = db->getBouquet(ref, bouquet)) != 0)
71         {
72                 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. getBouquet failed!");
73                 return -1;
74         }
75
76         if ( bouquet && bouquet->m_bouquet_name.length() )
77         {
78                 name = "[Bouquet] " + bouquet->m_bouquet_name;
79                 return 0;
80         }
81         else
82                 return -1;
83 }
84
85 int eStaticServiceDVBBouquetInformation::getLength(const eServiceReference &ref)
86 {
87         return -1;
88 }
89
90 class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
91 {
92         DECLARE_REF(eStaticServiceDVBPVRInformation);
93         eServiceReference m_ref;
94         eDVBMetaParser m_parser;
95 public:
96         eStaticServiceDVBPVRInformation(const eServiceReference &ref);
97         RESULT getName(const eServiceReference &ref, std::string &name);
98         int getLength(const eServiceReference &ref);
99 };
100
101 DEFINE_REF(eStaticServiceDVBPVRInformation);
102
103 eStaticServiceDVBPVRInformation::eStaticServiceDVBPVRInformation(const eServiceReference &ref)
104 {
105         m_ref = ref;
106         m_parser.parseFile(ref.path);
107 }
108
109 RESULT eStaticServiceDVBPVRInformation::getName(const eServiceReference &ref, std::string &name)
110 {
111         ASSERT(ref == m_ref);
112         name = m_parser.m_name.size() ? m_parser.m_name : ref.path;
113         return 0;
114 }
115
116 int eStaticServiceDVBPVRInformation::getLength(const eServiceReference &ref)
117 {
118         ASSERT(ref == m_ref);
119         
120         eDVBTSTools tstools;
121         
122         if (tstools.openFile(ref.path.c_str()))
123                 return 0;
124
125         pts_t len;
126         if (tstools.calcLen(len))
127                 return 0;
128
129         return len / 90000;
130 }
131
132
133
134 class eDVBPVRServiceOfflineOperations: public iServiceOfflineOperations
135 {
136         DECLARE_REF(eDVBPVRServiceOfflineOperations);
137         eServiceReferenceDVB m_ref;
138 public:
139         eDVBPVRServiceOfflineOperations(const eServiceReference &ref);
140         
141         RESULT deleteFromDisk(int simulate);
142         RESULT getListOfFilenames(std::list<std::string> &);
143 };
144
145 DEFINE_REF(eDVBPVRServiceOfflineOperations);
146
147 eDVBPVRServiceOfflineOperations::eDVBPVRServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReferenceDVB&)ref)
148 {
149 }
150
151 RESULT eDVBPVRServiceOfflineOperations::deleteFromDisk(int simulate)
152 {
153         if (simulate)
154                 return 0;
155         else
156         {
157                 std::list<std::string> res;
158                 if (getListOfFilenames(res))
159                         return -1;
160                 
161                                 /* TODO: deferred removing.. */
162                 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
163                 {
164                         eDebug("Removing %s...", i->c_str());
165                         ::unlink(i->c_str());
166                 }
167                 
168                 return 0;
169         }
170 }
171
172 RESULT eDVBPVRServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
173 {
174         res.clear();
175         res.push_back(m_ref.path);
176         res.push_back(m_ref.path + ".meta");
177         return 0;
178 }
179
180 DEFINE_REF(eServiceFactoryDVB)
181
182 eServiceFactoryDVB::eServiceFactoryDVB()
183 {
184         ePtr<eServiceCenter> sc;
185         
186         eServiceCenter::getPrivInstance(sc);
187         if (sc)
188                 sc->addServiceFactory(eServiceFactoryDVB::id, this);
189 }
190
191 eServiceFactoryDVB::~eServiceFactoryDVB()
192 {
193         ePtr<eServiceCenter> sc;
194         
195         eServiceCenter::getPrivInstance(sc);
196         if (sc)
197                 sc->removeServiceFactory(eServiceFactoryDVB::id);
198 }
199
200 DEFINE_REF(eDVBServiceList);
201
202 eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
203 {
204 }
205
206 eDVBServiceList::~eDVBServiceList()
207 {
208 }
209
210 RESULT eDVBServiceList::startQuery()
211 {
212         ePtr<iDVBChannelList> db;
213         ePtr<eDVBResourceManager> res;
214         
215         int err;
216         if ((err = eDVBResourceManager::getInstance(res)) != 0)
217         {
218                 eDebug("no resource manager");
219                 return err;
220         }
221         if ((err = res->getChannelList(db)) != 0)
222         {
223                 eDebug("no channel list");
224                 return err;
225         }
226         
227         ePtr<eDVBChannelQuery> q;
228         
229         if (!m_parent.path.empty())
230         {
231                 eDVBChannelQuery::compile(q, m_parent.path);
232                 if (!q)
233                 {
234                         eDebug("compile query failed");
235                         return err;
236                 }
237         }
238         
239         if ((err = db->startQuery(m_query, q, m_parent)) != 0)
240         {
241                 eDebug("startQuery failed");
242                 return err;
243         }
244
245         return 0;
246 }
247
248 RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
249 {
250         eServiceReferenceDVB ref;
251         
252         if (!m_query)
253                 return -1;
254         
255         while (!m_query->getNextResult(ref))
256                 list.push_back(ref);
257         return 0;
258 }
259
260 RESULT eDVBServiceList::getNext(eServiceReference &ref)
261 {
262         if (!m_query)
263                 return -1;
264         
265         return m_query->getNextResult((eServiceReferenceDVB&)ref);
266 }
267
268 int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
269 {
270         return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
271 }
272
273 RESULT eDVBServiceList::startEdit(ePtr<iMutableServiceList> &res)
274 {
275         if (m_parent.flags & eServiceReference::flagDirectory) // bouquet
276         {
277                 ePtr<iDVBChannelList> db;
278                 ePtr<eDVBResourceManager> resm;
279
280                 if (eDVBResourceManager::getInstance(resm) || resm->getChannelList(db))
281                         return -1;
282
283                 if (db->getBouquet(m_parent, m_bouquet) != 0)
284                         return -1;
285
286                 res = this;
287                 
288                 return 0;
289         }
290         res = 0;
291         return -1;
292 }
293
294 RESULT eDVBServiceList::addService(eServiceReference &ref)
295 {
296         if (!m_bouquet)
297                 return -1;
298         return m_bouquet->addService(ref);
299 }
300
301 RESULT eDVBServiceList::removeService(eServiceReference &ref)
302 {
303         if (!m_bouquet)
304                 return -1;
305         return m_bouquet->removeService(ref);
306 }
307
308 RESULT eDVBServiceList::moveService(eServiceReference &ref, int pos)
309 {
310         if (!m_bouquet)
311                 return -1;
312         return m_bouquet->moveService(ref, pos);
313 }
314
315 RESULT eDVBServiceList::flushChanges()
316 {
317         if (!m_bouquet)
318                 return -1;
319         return m_bouquet->flushChanges();
320 }
321
322 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
323 {
324         ePtr<eDVBService> service;
325         int r = lookupService(service, ref);
326         if (r)
327                 service = 0;
328                 // check resources...
329         ptr = new eDVBServicePlay(ref, service);
330         return 0;
331 }
332
333 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
334 {
335         if (ref.path.empty())
336         {
337                 ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
338                 return 0;
339         } else
340         {
341                 ptr = 0;
342                 return -1;
343         }
344 }
345
346 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
347 {
348         ePtr<eDVBServiceList> list = new eDVBServiceList(ref);
349         if (list->startQuery())
350         {
351                 ptr = 0;
352                 return -1;
353         }
354         
355         ptr = list;
356         return 0;
357 }
358
359 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
360 {
361                 /* do we have a PVR service? */
362         if (ref.flags & eServiceReference::flagDirectory) // bouquet
363         {
364                 ptr = new eStaticServiceDVBBouquetInformation;
365                 return 0;
366         }
367         else if (!ref.path.empty())
368         {
369                 ptr = new eStaticServiceDVBPVRInformation(ref);
370                 return 0;
371         }
372         else
373         {
374                 ePtr<eDVBService> service;
375                 int r = lookupService(service, ref);
376                 if (r)
377                         ptr = new eStaticServiceDVBInformation;
378                 else
379                         /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
380                         ptr = service;
381                 return 0;
382         }
383 }
384
385 RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
386 {
387         if (ref.path.empty())
388         {
389                 ptr = 0;
390                 return -1;
391         } else
392         {
393                 ptr = new eDVBPVRServiceOfflineOperations(ref);
394                 return 0;
395         }
396 }
397
398 RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServiceReference &ref)
399 {
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
403                         // TODO: cache
404         ePtr<iDVBChannelList> db;
405         ePtr<eDVBResourceManager> res;
406         
407         int err;
408         if ((err = eDVBResourceManager::getInstance(res)) != 0)
409         {
410                 eDebug("no resource manager");
411                 return err;
412         }
413         if ((err = res->getChannelList(db)) != 0)
414         {
415                 eDebug("no channel list");
416                 return err;
417         }
418         
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)
421         {
422                 eDebug("getService failed!");
423                 return err;
424         }
425
426         return 0;
427 }
428
429 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service): 
430         m_reference(ref), m_dvb_service(service), m_service_handler(0), m_is_paused(0)
431 {
432         m_is_pvr = !ref.path.empty();
433         
434         CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
435         CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
436 }
437
438 eDVBServicePlay::~eDVBServicePlay()
439 {
440 }
441
442 void eDVBServicePlay::gotNewEvent()
443 {
444 #if 0
445                 // debug only
446         ePtr<eServiceEvent> m_event_now, m_event_next;
447         getEvent(m_event_now, 0);
448         getEvent(m_event_next, 1);
449
450         if (m_event_now)
451                 eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
452         if (m_event_next)
453                 eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
454 #endif
455         m_event((iPlayableService*)this, evUpdatedEventInfo);
456 }
457
458 void eDVBServicePlay::serviceEvent(int event)
459 {
460         switch (event)
461         {
462         case eDVBServicePMTHandler::eventTuned:
463         {
464                 ePtr<iDVBDemux> m_demux;
465                 if (!m_service_handler.getDemux(m_demux))
466                 {
467 //                      eventStartedEventAcquisition
468                         m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
469                 }
470 //                      eventNoEvent
471                 break;
472         }
473         case eDVBServicePMTHandler::eventTuneFailed:
474         {
475                 eDebug("DVB service failed to tune");
476                 m_event((iPlayableService*)this, evTuneFailed);
477                 break;
478         }
479         case eDVBServicePMTHandler::eventNewProgramInfo:
480         {
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.");
485                 else
486                 {
487                         eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
488                         if (!program.videoStreams.empty())
489                         {
490                                 eDebugNoNewLine(" (");
491                                 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
492                                         i(program.videoStreams.begin()); 
493                                         i != program.videoStreams.end(); ++i)
494                                 {
495                                         if (vpid == -1)
496                                                 vpid = i->pid;
497                                         if (i != program.videoStreams.begin())
498                                                 eDebugNoNewLine(", ");
499                                         eDebugNoNewLine("%04x", i->pid);
500                                 }
501                                 eDebugNoNewLine(")");
502                         }
503                         eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
504                         if (!program.audioStreams.empty())
505                         {
506                                 eDebugNoNewLine(" (");
507                                 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
508                                         i(program.audioStreams.begin()); 
509                                         i != program.audioStreams.end(); ++i)
510                                 {
511                                         if (apid == -1)
512                                         {
513                                                 apid = i->pid;
514                                                 apidtype = i->type;
515                                         }
516                                         if (i != program.audioStreams.begin())
517                                                 eDebugNoNewLine(", ");
518                                         eDebugNoNewLine("%04x", i->pid);
519                                 }
520                                 eDebugNoNewLine(")");
521                         }
522                         eDebug(", and the pcr pid is %04x", program.pcrPid);
523                         if (program.pcrPid != 0x1fff)
524                                 pcrpid = program.pcrPid;
525                 }
526                 
527                 if (!m_decoder)
528                 {
529                         ePtr<iDVBDemux> demux;
530                         m_service_handler.getDemux(demux);
531                         if (demux)
532                                 demux->getMPEGDecoder(m_decoder);
533                 }
534
535                 if (m_decoder)
536                 {
537                         m_decoder->setVideoPID(vpid);
538                         m_decoder->setAudioPID(apid, apidtype);
539                         if (!m_is_pvr)
540                                 m_decoder->setSyncPCR(pcrpid);
541                         else
542                                 m_decoder->setSyncPCR(-1);
543                         m_decoder->start();
544 // how we can do this better?
545 // update cache pid when the user changed the audio track or video track
546 // TODO handling of difference audio types.. default audio types..
547                                 
548                                 /* don't worry about non-existing services, nor pvr services */
549                         if (m_dvb_service && !m_is_pvr)
550                         {
551                                 m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
552                                 m_dvb_service->setCachePID(eDVBService::cAPID, apid);
553                                 m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
554                         }
555                 }
556                 
557                 break;
558         }
559         }
560 }
561
562 RESULT eDVBServicePlay::start()
563 {
564         int r;
565         eDebug("starting DVB service");
566         r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
567         eDebug("tune result: %d", r);
568         m_event(this, evStart);
569         return 0;
570 }
571
572 RESULT eDVBServicePlay::stop()
573 {
574         eDebug("stopping..");
575         return 0;
576 }
577
578 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
579 {
580         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
581         return 0;
582 }
583
584 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
585 {
586         if (m_is_pvr)
587         {
588                 ptr = this;
589                 return 0;
590         }
591
592         ptr = 0;
593         return -1;
594 }
595
596 RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
597 {
598         if (m_is_pvr)
599         {
600                 ptr = this;
601                 return 0;
602         }
603         
604         ptr = 0;
605         return -1;
606 }
607
608 RESULT eDVBServicePlay::getLength(pts_t &len)
609 {
610         ePtr<iDVBPVRChannel> pvr_channel;
611         
612         if (m_service_handler.getPVRChannel(pvr_channel))
613         {
614                 eDebug("getPVRChannel failed!");
615                 return -1;
616         }
617         
618         return pvr_channel->getLength(len);
619 }
620
621 RESULT eDVBServicePlay::pause()
622 {
623         if (!m_is_paused && m_decoder)
624         {
625                 m_is_paused = 1;
626                 return m_decoder->freeze(0);
627         } else
628                 return -1;
629 }
630
631 RESULT eDVBServicePlay::unpause()
632 {
633         if (m_is_paused && m_decoder)
634         {
635                 m_is_paused = 0;
636                 return m_decoder->unfreeze();
637         } else
638                 return -1;
639 }
640
641 RESULT eDVBServicePlay::seekTo(pts_t to)
642 {
643         return -1;
644 }
645
646 RESULT eDVBServicePlay::seekRelative(int direction, pts_t to)
647 {
648         eDebug("eDVBServicePlay::seekRelative: jump %d, %lld", direction, to);
649
650         ePtr<iDVBPVRChannel> pvr_channel;
651         
652         if (m_service_handler.getPVRChannel(pvr_channel))
653                 return -1;
654         
655                         /* this is of couse wrong: PTS values don't match with bytes. */
656         return pvr_channel->seekToPosition(SEEK_CUR, direction * to);
657 }
658
659 RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
660 {
661         ePtr<iDVBPVRChannel> pvr_channel;
662         
663         if (m_service_handler.getPVRChannel(pvr_channel))
664                 return -1;
665         
666         return pvr_channel->getCurrentPosition(pos);
667 }
668
669 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
670 {
671         ptr = this;
672         return 0;
673 }
674
675 RESULT eDVBServicePlay::audioTracks(ePtr<iAudioTrackSelection> &ptr)
676 {
677         ptr = this;
678         return 0;
679 }
680
681 RESULT eDVBServicePlay::getName(std::string &name)
682 {
683         if (m_dvb_service)
684         {
685                 m_dvb_service->getName(m_reference, name);
686                 if (name.empty())
687                         name = "(...)";
688         } else
689                 name = "DVB service";
690         return 0;
691 }
692
693 RESULT eDVBServicePlay::getEvent(ePtr<eServiceEvent> &evt, int nownext)
694 {
695         return m_event_handler.getEvent(evt, nownext);
696 }
697
698 int eDVBServicePlay::getNumberOfTracks()
699 {
700         eDVBServicePMTHandler::program program;
701         if (m_service_handler.getProgramInfo(program))
702                 return 0;
703         return program.audioStreams.size();
704 }
705
706 RESULT eDVBServicePlay::selectTrack(unsigned int i)
707 {
708         eDVBServicePMTHandler::program program;
709
710         if (m_service_handler.getProgramInfo(program))
711                 return -1;
712         
713         if (i >= program.audioStreams.size())
714                 return -2;
715         
716         if (!m_decoder)
717                 return -3;
718         
719         if (m_decoder->setAudioPID(program.audioStreams[i].pid, program.audioStreams[i].type))
720                 return -4;
721
722         if (m_decoder->start())
723                 return -5;
724         
725         return 0;
726 }
727
728 RESULT eDVBServicePlay::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
729 {
730         eDVBServicePMTHandler::program program;
731
732         if (m_service_handler.getProgramInfo(program))
733                 return -1;
734         
735         if (i >= program.audioStreams.size())
736                 return -2;
737         
738         if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atMPEG)
739                 info.m_description = "MPEG";
740         else if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atAC3)
741                 info.m_description = "AC3";
742         else  if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atDTS)
743                 info.m_description = "DTS";
744         else
745                 info.m_description = "???";
746         
747         return 0;
748 }
749
750 DEFINE_REF(eDVBServicePlay)
751
752 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");