better error/retry handling for recorder
[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 #include <lib/dvb/decoder.h>
12
13 #include <lib/service/servicedvbrecord.h>
14 #include <lib/dvb/metaparser.h>
15 #include <lib/dvb/tstools.h>
16
17 class eStaticServiceDVBInformation: public iStaticServiceInformation
18 {
19         DECLARE_REF(eStaticServiceDVBInformation);
20 public:
21         RESULT getName(const eServiceReference &ref, std::string &name);
22         int getLength(const eServiceReference &ref);
23 };
24
25 DEFINE_REF(eStaticServiceDVBInformation);
26
27 RESULT eStaticServiceDVBInformation::getName(const eServiceReference &ref, std::string &name)
28 {
29         eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
30         if ( !ref.name.empty() )
31         {
32                 if (service.getParentTransportStreamID().get()) // linkage subservice
33                 {
34                         ePtr<iServiceHandler> service_center;
35                         if (!eServiceCenter::getInstance(service_center))
36                         {
37                                 eServiceReferenceDVB parent = service;
38                                 parent.setTransportStreamID( service.getParentTransportStreamID() );
39                                 parent.setServiceID( service.getParentServiceID() );
40                                 parent.setParentTransportStreamID(eTransportStreamID(0));
41                                 parent.setParentServiceID(eServiceID(0));
42                                 parent.name="";
43                                 ePtr<iStaticServiceInformation> service_info;
44                                 if (!service_center->info(parent, service_info))
45                                 {
46                                         if (!service_info->getName(parent, name))
47                                         {
48                                                 // just show short name
49                                                 unsigned int pos = name.find("\xc2\x86");
50                                                 if ( pos != std::string::npos )
51                                                         name.erase(0, pos+2);
52                                                 pos = name.find("\xc2\x87");
53                                                 if ( pos != std::string::npos )
54                                                         name.erase(pos);
55                                                 name+=" - ";
56                                         }
57                                 }
58                         }
59                 }
60                 else
61                         name="";
62                 name += ref.name;
63                 return 0;
64         }
65         else
66                 return -1;
67 }
68
69 int eStaticServiceDVBInformation::getLength(const eServiceReference &ref)
70 {
71         return -1;
72 }
73
74 class eStaticServiceDVBBouquetInformation: public iStaticServiceInformation
75 {
76         DECLARE_REF(eStaticServiceDVBBouquetInformation);
77 public:
78         RESULT getName(const eServiceReference &ref, std::string &name);
79         int getLength(const eServiceReference &ref);
80 };
81
82 DEFINE_REF(eStaticServiceDVBBouquetInformation);
83
84 RESULT eStaticServiceDVBBouquetInformation::getName(const eServiceReference &ref, std::string &name)
85 {
86         ePtr<iDVBChannelList> db;
87         ePtr<eDVBResourceManager> res;
88
89         int err;
90         if ((err = eDVBResourceManager::getInstance(res)) != 0)
91         {
92                 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no resource manager!");
93                 return err;
94         }
95         if ((err = res->getChannelList(db)) != 0)
96         {
97                 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. no channel list!");
98                 return err;
99         }
100
101         eBouquet *bouquet=0;
102         if ((err = db->getBouquet(ref, bouquet)) != 0)
103         {
104                 eDebug("eStaticServiceDVBBouquetInformation::getName failed.. getBouquet failed!");
105                 return -1;
106         }
107
108         if ( bouquet && bouquet->m_bouquet_name.length() )
109         {
110                 name = bouquet->m_bouquet_name;
111                 return 0;
112         }
113         else
114                 return -1;
115 }
116
117 int eStaticServiceDVBBouquetInformation::getLength(const eServiceReference &ref)
118 {
119         return -1;
120 }
121
122 class eStaticServiceDVBPVRInformation: public iStaticServiceInformation
123 {
124         DECLARE_REF(eStaticServiceDVBPVRInformation);
125         eServiceReference m_ref;
126         eDVBMetaParser m_parser;
127 public:
128         eStaticServiceDVBPVRInformation(const eServiceReference &ref);
129         RESULT getName(const eServiceReference &ref, std::string &name);
130         int getLength(const eServiceReference &ref);
131         
132         int getInfo(const eServiceReference &ref, int w);
133         std::string getInfoString(const eServiceReference &ref,int w);
134 };
135
136 DEFINE_REF(eStaticServiceDVBPVRInformation);
137
138 eStaticServiceDVBPVRInformation::eStaticServiceDVBPVRInformation(const eServiceReference &ref)
139 {
140         m_ref = ref;
141         m_parser.parseFile(ref.path);
142 }
143
144 RESULT eStaticServiceDVBPVRInformation::getName(const eServiceReference &ref, std::string &name)
145 {
146         ASSERT(ref == m_ref);
147         name = m_parser.m_name.size() ? m_parser.m_name : ref.path;
148         return 0;
149 }
150
151 int eStaticServiceDVBPVRInformation::getLength(const eServiceReference &ref)
152 {
153         ASSERT(ref == m_ref);
154         
155         eDVBTSTools tstools;
156         
157         if (tstools.openFile(ref.path.c_str()))
158                 return 0;
159
160         pts_t len;
161         if (tstools.calcLen(len))
162                 return 0;
163
164         return len / 90000;
165 }
166
167 int eStaticServiceDVBPVRInformation::getInfo(const eServiceReference &ref, int w)
168 {
169         switch (w)
170         {
171         case iServiceInformation::sDescription:
172                 return iServiceInformation::resIsString;
173         case iServiceInformation::sTimeCreate:
174                 if (m_parser.m_time_create)
175                         return m_parser.m_time_create;
176                 else
177                         return iServiceInformation::resNA;
178         default:
179                 return iServiceInformation::resNA;
180         }
181 }
182
183 std::string eStaticServiceDVBPVRInformation::getInfoString(const eServiceReference &ref,int w)
184 {
185         switch (w)
186         {
187         case iServiceInformation::sDescription:
188                 return m_parser.m_description;
189         default:
190                 return "";
191         }
192 }
193
194 class eDVBPVRServiceOfflineOperations: public iServiceOfflineOperations
195 {
196         DECLARE_REF(eDVBPVRServiceOfflineOperations);
197         eServiceReferenceDVB m_ref;
198 public:
199         eDVBPVRServiceOfflineOperations(const eServiceReference &ref);
200         
201         RESULT deleteFromDisk(int simulate);
202         RESULT getListOfFilenames(std::list<std::string> &);
203 };
204
205 DEFINE_REF(eDVBPVRServiceOfflineOperations);
206
207 eDVBPVRServiceOfflineOperations::eDVBPVRServiceOfflineOperations(const eServiceReference &ref): m_ref((const eServiceReferenceDVB&)ref)
208 {
209 }
210
211 RESULT eDVBPVRServiceOfflineOperations::deleteFromDisk(int simulate)
212 {
213         if (simulate)
214                 return 0;
215         else
216         {
217                 std::list<std::string> res;
218                 if (getListOfFilenames(res))
219                         return -1;
220                 
221                                 /* TODO: deferred removing.. */
222                 for (std::list<std::string>::iterator i(res.begin()); i != res.end(); ++i)
223                 {
224                         eDebug("Removing %s...", i->c_str());
225                         ::unlink(i->c_str());
226                 }
227                 
228                 return 0;
229         }
230 }
231
232 RESULT eDVBPVRServiceOfflineOperations::getListOfFilenames(std::list<std::string> &res)
233 {
234         res.clear();
235         res.push_back(m_ref.path);
236         res.push_back(m_ref.path + ".meta");
237         return 0;
238 }
239
240 DEFINE_REF(eServiceFactoryDVB)
241
242 eServiceFactoryDVB::eServiceFactoryDVB()
243 {
244         ePtr<eServiceCenter> sc;
245         
246         eServiceCenter::getPrivInstance(sc);
247         if (sc)
248                 sc->addServiceFactory(eServiceFactoryDVB::id, this);
249 }
250
251 eServiceFactoryDVB::~eServiceFactoryDVB()
252 {
253         ePtr<eServiceCenter> sc;
254         
255         eServiceCenter::getPrivInstance(sc);
256         if (sc)
257                 sc->removeServiceFactory(eServiceFactoryDVB::id);
258 }
259
260 DEFINE_REF(eDVBServiceList);
261
262 eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
263 {
264 }
265
266 eDVBServiceList::~eDVBServiceList()
267 {
268 }
269
270 RESULT eDVBServiceList::startQuery()
271 {
272         ePtr<iDVBChannelList> db;
273         ePtr<eDVBResourceManager> res;
274         
275         int err;
276         if ((err = eDVBResourceManager::getInstance(res)) != 0)
277         {
278                 eDebug("no resource manager");
279                 return err;
280         }
281         if ((err = res->getChannelList(db)) != 0)
282         {
283                 eDebug("no channel list");
284                 return err;
285         }
286         
287         ePtr<eDVBChannelQuery> q;
288         
289         if (!m_parent.path.empty())
290         {
291                 eDVBChannelQuery::compile(q, m_parent.path);
292                 if (!q)
293                 {
294                         eDebug("compile query failed");
295                         return err;
296                 }
297         }
298         
299         if ((err = db->startQuery(m_query, q, m_parent)) != 0)
300         {
301                 eDebug("startQuery failed");
302                 return err;
303         }
304
305         return 0;
306 }
307
308 RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
309 {
310         eServiceReferenceDVB ref;
311         
312         if (!m_query)
313                 return -1;
314         
315         while (!m_query->getNextResult(ref))
316                 list.push_back(ref);
317         return 0;
318 }
319
320 RESULT eDVBServiceList::getNext(eServiceReference &ref)
321 {
322         if (!m_query)
323                 return -1;
324         
325         return m_query->getNextResult((eServiceReferenceDVB&)ref);
326 }
327
328 int eDVBServiceList::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
329 {
330         return m_query->compareLessEqual((const eServiceReferenceDVB&)a, (const eServiceReferenceDVB&)b);
331 }
332
333 RESULT eDVBServiceList::startEdit(ePtr<iMutableServiceList> &res)
334 {
335         if (m_parent.flags & eServiceReference::flagDirectory) // bouquet
336         {
337                 ePtr<iDVBChannelList> db;
338                 ePtr<eDVBResourceManager> resm;
339
340                 if (eDVBResourceManager::getInstance(resm) || resm->getChannelList(db))
341                         return -1;
342
343                 if (db->getBouquet(m_parent, m_bouquet) != 0)
344                         return -1;
345
346                 res = this;
347                 
348                 return 0;
349         }
350         res = 0;
351         return -1;
352 }
353
354 RESULT eDVBServiceList::addService(eServiceReference &ref)
355 {
356         if (!m_bouquet)
357                 return -1;
358         return m_bouquet->addService(ref);
359 }
360
361 RESULT eDVBServiceList::removeService(eServiceReference &ref)
362 {
363         if (!m_bouquet)
364                 return -1;
365         return m_bouquet->removeService(ref);
366 }
367
368 RESULT eDVBServiceList::moveService(eServiceReference &ref, int pos)
369 {
370         if (!m_bouquet)
371                 return -1;
372         return m_bouquet->moveService(ref, pos);
373 }
374
375 RESULT eDVBServiceList::flushChanges()
376 {
377         if (!m_bouquet)
378                 return -1;
379         return m_bouquet->flushChanges();
380 }
381
382 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
383 {
384         ePtr<eDVBService> service;
385         int r = lookupService(service, ref);
386         if (r)
387                 service = 0;
388                 // check resources...
389         ptr = new eDVBServicePlay(ref, service);
390         return 0;
391 }
392
393 RESULT eServiceFactoryDVB::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
394 {
395         if (ref.path.empty())
396         {
397                 ptr = new eDVBServiceRecord((eServiceReferenceDVB&)ref);
398                 return 0;
399         } else
400         {
401                 ptr = 0;
402                 return -1;
403         }
404 }
405
406 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
407 {
408         ePtr<eDVBServiceList> list = new eDVBServiceList(ref);
409         if (list->startQuery())
410         {
411                 ptr = 0;
412                 return -1;
413         }
414         
415         ptr = list;
416         return 0;
417 }
418
419 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
420 {
421         /* is a listable service? */
422         if ((ref.flags & eServiceReference::flagDirectory) == eServiceReference::flagDirectory) // bouquet
423         {
424                 if ( !ref.name.empty() )  // satellites or providers list
425                         ptr = new eStaticServiceDVBInformation;
426                 else // a dvb bouquet
427                         ptr = new eStaticServiceDVBBouquetInformation;
428         }
429         else if (!ref.path.empty()) /* do we have a PVR service? */
430                 ptr = new eStaticServiceDVBPVRInformation(ref);
431         else // normal dvb service
432         {
433                 ePtr<eDVBService> service;
434                 if (lookupService(service, ref)) // no eDVBService avail for this reference ( Linkage Services... )
435                         ptr = new eStaticServiceDVBInformation;
436                 else
437                         /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
438                         ptr = service;
439         }
440         return 0;
441 }
442
443 RESULT eServiceFactoryDVB::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
444 {
445         if (ref.path.empty())
446         {
447                 ptr = 0;
448                 return -1;
449         } else
450         {
451                 ptr = new eDVBPVRServiceOfflineOperations(ref);
452                 return 0;
453         }
454 }
455
456 RESULT eServiceFactoryDVB::lookupService(ePtr<eDVBService> &service, const eServiceReference &ref)
457 {
458                         // TODO: handle the listing itself
459         // if (ref.... == -1) .. return "... bouquets ...";
460         // could be also done in another serviceFactory (with seperate ID) to seperate actual services and lists
461                         // TODO: cache
462         ePtr<iDVBChannelList> db;
463         ePtr<eDVBResourceManager> res;
464         
465         int err;
466         if ((err = eDVBResourceManager::getInstance(res)) != 0)
467         {
468                 eDebug("no resource manager");
469                 return err;
470         }
471         if ((err = res->getChannelList(db)) != 0)
472         {
473                 eDebug("no channel list");
474                 return err;
475         }
476         
477                 /* we are sure to have a ..DVB reference as the info() call was forwarded here according to it's ID. */
478         if ((err = db->getService((eServiceReferenceDVB&)ref, service)) != 0)
479         {
480                 eDebug("getService failed!");
481                 return err;
482         }
483
484         return 0;
485 }
486
487 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref, eDVBService *service): 
488         m_reference(ref), m_dvb_service(service), m_service_handler(0), m_is_paused(0)
489 {
490         m_is_pvr = !ref.path.empty();
491         m_timeshift_enabled = 0;
492         
493         CONNECT(m_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
494         CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
495 }
496
497 eDVBServicePlay::~eDVBServicePlay()
498 {
499 }
500
501 void eDVBServicePlay::gotNewEvent()
502 {
503 #if 0
504                 // debug only
505         ePtr<eServiceEvent> m_event_now, m_event_next;
506         getEvent(m_event_now, 0);
507         getEvent(m_event_next, 1);
508
509         if (m_event_now)
510                 eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
511         if (m_event_next)
512                 eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
513 #endif
514         m_event((iPlayableService*)this, evUpdatedEventInfo);
515 }
516
517 void eDVBServicePlay::serviceEvent(int event)
518 {
519         switch (event)
520         {
521         case eDVBServicePMTHandler::eventTuned:
522         {
523                 ePtr<iDVBDemux> m_demux;
524                 if (!m_service_handler.getDemux(m_demux))
525                 {
526                         eServiceReferenceDVB &ref = (eServiceReferenceDVB&) m_reference;
527                         int sid = ref.getParentServiceID().get();
528                         if (!sid)
529                                 sid = ref.getServiceID().get();
530                         if ( ref.getParentTransportStreamID().get() &&
531                                 ref.getParentTransportStreamID() != ref.getTransportStreamID() )
532                                 m_event_handler.startOther(m_demux, sid);
533                         else
534                                 m_event_handler.start(m_demux, sid);
535                 }
536                 break;
537         }
538         case eDVBServicePMTHandler::eventTuneFailed:
539         {
540                 eDebug("DVB service failed to tune");
541                 m_event((iPlayableService*)this, evTuneFailed);
542                 break;
543         }
544         case eDVBServicePMTHandler::eventNewProgramInfo:
545         {
546                 int vpid = -1, apid = -1, apidtype = -1, pcrpid = -1;
547                 eDVBServicePMTHandler::program program;
548                 if (m_service_handler.getProgramInfo(program))
549                         eDebug("getting program info failed.");
550                 else
551                 {
552                         eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
553                         if (!program.videoStreams.empty())
554                         {
555                                 eDebugNoNewLine(" (");
556                                 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
557                                         i(program.videoStreams.begin()); 
558                                         i != program.videoStreams.end(); ++i)
559                                 {
560                                         if (vpid == -1)
561                                                 vpid = i->pid;
562                                         if (i != program.videoStreams.begin())
563                                                 eDebugNoNewLine(", ");
564                                         eDebugNoNewLine("%04x", i->pid);
565                                 }
566                                 eDebugNoNewLine(")");
567                         }
568                         eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
569                         if (!program.audioStreams.empty())
570                         {
571                                 eDebugNoNewLine(" (");
572                                 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
573                                         i(program.audioStreams.begin()); 
574                                         i != program.audioStreams.end(); ++i)
575                                 {
576                                         if (apid == -1)
577                                         {
578                                                 apid = i->pid;
579                                                 apidtype = i->type;
580                                         }
581                                         if (i != program.audioStreams.begin())
582                                                 eDebugNoNewLine(", ");
583                                         eDebugNoNewLine("%04x", i->pid);
584                                 }
585                                 eDebugNoNewLine(")");
586                         }
587                         eDebug(", and the pcr pid is %04x", program.pcrPid);
588                         if (program.pcrPid != 0x1fff)
589                                 pcrpid = program.pcrPid;
590                 }
591                 
592                 if (!m_decoder)
593                 {
594                         ePtr<iDVBDemux> demux;
595                         m_service_handler.getDemux(demux);
596                         if (demux)
597                                 demux->getMPEGDecoder(m_decoder);
598                 }
599
600                 if (m_decoder)
601                 {
602                         m_decoder->setVideoPID(vpid);
603                         m_current_audio_stream = 0;
604                         m_decoder->setAudioPID(apid, apidtype);
605                         if (!m_is_pvr)
606                                 m_decoder->setSyncPCR(pcrpid);
607                         else
608                                 m_decoder->setSyncPCR(-1);
609                         m_decoder->start();
610 // how we can do this better?
611 // update cache pid when the user changed the audio track or video track
612 // TODO handling of difference audio types.. default audio types..
613                                 
614                                 /* don't worry about non-existing services, nor pvr services */
615                         if (m_dvb_service && !m_is_pvr)
616                         {
617                                 if (apidtype == eDVBAudio::aMPEG)
618                                 {
619                                         m_dvb_service->setCachePID(eDVBService::cAPID, apid);
620                                         m_dvb_service->setCachePID(eDVBService::cAC3PID, -1);
621                                 }
622                                 else
623                                 {
624                                         m_dvb_service->setCachePID(eDVBService::cAPID, -1);
625                                         m_dvb_service->setCachePID(eDVBService::cAC3PID, apid);
626                                 }
627                                 m_dvb_service->setCachePID(eDVBService::cVPID, vpid);
628                                 m_dvb_service->setCachePID(eDVBService::cPCRPID, pcrpid);
629                         }
630                 }
631
632                 m_event((iPlayableService*)this, evUpdatedInfo);
633                 break;
634         }
635         }
636 }
637
638 RESULT eDVBServicePlay::start()
639 {
640         int r;
641         eDebug("starting DVB service");
642         r = m_service_handler.tune((eServiceReferenceDVB&)m_reference);
643         eDebug("tune result: %d", r);
644         m_event(this, evStart);
645         return 0;
646 }
647
648 RESULT eDVBServicePlay::stop()
649 {
650         eDebug("stopping..");
651         return 0;
652 }
653
654 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
655 {
656         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
657         return 0;
658 }
659
660 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
661 {
662         if (!m_is_pvr)
663         {
664                 ptr = 0;
665                 return -1;
666         }
667
668         ptr = this;
669         return 0;
670 }
671
672 RESULT eDVBServicePlay::setSlowMotion(int ratio)
673 {
674         if (m_decoder)
675                 return m_decoder->setSlowMotion(ratio);
676         else
677                 return -1;
678 }
679
680 RESULT eDVBServicePlay::setFastForward(int ratio)
681 {
682         if (m_decoder)
683                 return m_decoder->setFastForward(ratio);
684         else
685                 return -1;
686 }
687     
688 RESULT eDVBServicePlay::seek(ePtr<iSeekableService> &ptr)
689 {
690         if (m_is_pvr)
691         {
692                 ptr = this;
693                 return 0;
694         }
695         
696         ptr = 0;
697         return -1;
698 }
699
700 RESULT eDVBServicePlay::getLength(pts_t &len)
701 {
702         ePtr<iDVBPVRChannel> pvr_channel;
703         
704         if (m_service_handler.getPVRChannel(pvr_channel))
705         {
706                 eDebug("getPVRChannel failed!");
707                 return -1;
708         }
709         
710         return pvr_channel->getLength(len);
711 }
712
713 RESULT eDVBServicePlay::pause()
714 {
715         if (!m_is_paused && m_decoder)
716         {
717                 m_is_paused = 1;
718                 return m_decoder->freeze(0);
719         } else
720                 return -1;
721 }
722
723 RESULT eDVBServicePlay::unpause()
724 {
725         if (m_is_paused && m_decoder)
726         {
727                 m_is_paused = 0;
728                 return m_decoder->unfreeze();
729         } else
730                 return -1;
731 }
732
733 RESULT eDVBServicePlay::seekTo(pts_t to)
734 {
735         eDebug("eDVBServicePlay::seekTo: jump %lld", to);
736
737         ePtr<iDVBPVRChannel> pvr_channel;
738         
739         if (m_service_handler.getPVRChannel(pvr_channel))
740                 return -1;
741         
742         ePtr<iDVBDemux> demux;
743         m_service_handler.getDemux(demux);
744         if (!demux)
745                 return -1;
746         
747         return pvr_channel->seekTo(demux, 0, to);
748 }
749
750 RESULT eDVBServicePlay::seekRelative(int direction, pts_t to)
751 {
752         eDebug("eDVBServicePlay::seekRelative: jump %d, %lld", direction, to);
753
754         ePtr<iDVBPVRChannel> pvr_channel;
755         
756         if (m_service_handler.getPVRChannel(pvr_channel))
757                 return -1;
758         
759         to *= direction;
760         
761         ePtr<iDVBDemux> demux;
762         m_service_handler.getDemux(demux);
763         if (!demux)
764                 return -1;
765         
766         return pvr_channel->seekTo(demux, 1, to);
767 }
768
769 RESULT eDVBServicePlay::getPlayPosition(pts_t &pos)
770 {
771         ePtr<iDVBPVRChannel> pvr_channel;
772         
773         if (m_service_handler.getPVRChannel(pvr_channel))
774                 return -1;
775         
776         ePtr<iDVBDemux> demux;
777         m_service_handler.getDemux(demux);
778         if (!demux)
779                 return -1;
780         
781         return pvr_channel->getCurrentPosition(demux, pos, 1);
782 }
783
784 RESULT eDVBServicePlay::setTrickmode(int trick)
785 {
786         if (m_decoder)
787                 m_decoder->setTrickmode(trick);
788         return 0;
789 }
790
791 RESULT eDVBServicePlay::frontendStatusInfo(ePtr<iFrontendStatusInformation> &ptr)
792 {
793         ptr = this;
794         return 0;
795 }
796
797 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
798 {
799         ptr = this;
800         return 0;
801 }
802
803 RESULT eDVBServicePlay::audioTracks(ePtr<iAudioTrackSelection> &ptr)
804 {
805         ptr = this;
806         return 0;
807 }
808
809 RESULT eDVBServicePlay::subServices(ePtr<iSubserviceList> &ptr)
810 {
811         ptr = this;
812         return 0;
813 }
814
815 RESULT eDVBServicePlay::timeshift(ePtr<iTimeshiftService> &ptr)
816 {
817         if (m_timeshift_enabled || !m_is_pvr)
818         {
819                 ptr = this;
820                 return 0;
821         }
822         ptr = 0;
823         return -1;
824 }
825
826 RESULT eDVBServicePlay::getName(std::string &name)
827 {
828         if (m_dvb_service)
829         {
830                 m_dvb_service->getName(m_reference, name);
831                 if (name.empty())
832                         name = "(...)";
833         }
834         else if (!m_reference.name.empty())
835                 eStaticServiceDVBInformation().getName(m_reference, name);
836         else
837                 name = "DVB service";
838         return 0;
839 }
840
841 RESULT eDVBServicePlay::getEvent(ePtr<eServiceEvent> &evt, int nownext)
842 {
843         return m_event_handler.getEvent(evt, nownext);
844 }
845
846 int eDVBServicePlay::getInfo(int w)
847 {
848         eDVBServicePMTHandler::program program;
849
850         if (m_service_handler.getProgramInfo(program))
851                 return -1;
852         
853         switch (w)
854         {
855         case sAspect:
856                 if (!program.videoStreams.empty() && program.videoStreams[0].component_tag != -1)
857                 {
858                         ePtr<eServiceEvent> evt;
859                         if (!m_event_handler.getEvent(evt, 0))
860                         {
861                                 ePtr<eComponentData> data;
862                                 if (!evt->getComponentData(data, program.videoStreams[0].component_tag))
863                                 {
864                                         if ( data->getStreamContent() == 1 )
865                                         {
866                                                 switch(data->getComponentType())
867                                                 {
868                                                         // SD
869                                                         case 1: // 4:3 SD PAL
870                                                         case 2:
871                                                         case 3: // 16:9 SD PAL
872                                                         case 4: // > 16:9 PAL
873                                                         case 5: // 4:3 SD NTSC
874                                                         case 6: 
875                                                         case 7: // 16:9 SD NTSC
876                                                         case 8: // > 16:9 NTSC
877
878                                                         // HD
879                                                         case 9: // 4:3 HD PAL
880                                                         case 0xA:
881                                                         case 0xB: // 16:9 HD PAL
882                                                         case 0xC: // > 16:9 HD PAL
883                                                         case 0xD: // 4:3 HD NTSC
884                                                         case 0xE:
885                                                         case 0xF: // 16:9 HD NTSC
886                                                         case 0x10: // > 16:9 HD PAL
887                                                                 return data->getComponentType();
888                                                 }
889                                         }
890                                 }
891                         }
892                 }
893                 return -1;
894         case sIsCrypted: return program.isCrypted;
895         case sVideoPID: if (program.videoStreams.empty()) return -1; return program.videoStreams[0].pid;
896         case sAudioPID: if (program.audioStreams.empty()) return -1; return program.audioStreams[m_current_audio_stream].pid;
897         case sPCRPID: return program.pcrPid;
898         case sPMTPID: return program.pmtPid;
899         case sTXTPID: return -1;
900         case sSID: return ((const eServiceReferenceDVB&)m_reference).getServiceID().get();
901         case sONID: return ((const eServiceReferenceDVB&)m_reference).getOriginalNetworkID().get();
902         case sTSID: return ((const eServiceReferenceDVB&)m_reference).getTransportStreamID().get();
903         case sNamespace: return ((const eServiceReferenceDVB&)m_reference).getDVBNamespace().get();
904         case sProvider: if (!m_dvb_service) return -1; return -2;
905         default:
906                 return -1;
907         }
908 }
909
910 std::string eDVBServicePlay::getInfoString(int w)
911 {       
912         switch (w)
913         {
914         case sProvider:
915                 if (!m_dvb_service) return "";
916                 return m_dvb_service->m_provider_name;
917         default:
918                 return "";
919         }
920 }
921
922 int eDVBServicePlay::getNumberOfTracks()
923 {
924         eDVBServicePMTHandler::program program;
925         if (m_service_handler.getProgramInfo(program))
926                 return 0;
927         return program.audioStreams.size();
928 }
929
930 RESULT eDVBServicePlay::selectTrack(unsigned int i)
931 {
932         int ret = selectAudioStream(i);
933
934         if (m_decoder->start())
935                 return -5;
936
937         return ret;
938 }
939
940 RESULT eDVBServicePlay::getTrackInfo(struct iAudioTrackInfo &info, unsigned int i)
941 {
942         eDVBServicePMTHandler::program program;
943
944         if (m_service_handler.getProgramInfo(program))
945                 return -1;
946         
947         if (i >= program.audioStreams.size())
948                 return -2;
949         
950         if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atMPEG)
951                 info.m_description = "MPEG";
952         else if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atAC3)
953                 info.m_description = "AC3";
954         else  if (program.audioStreams[i].type == eDVBServicePMTHandler::audioStream::atDTS)
955                 info.m_description = "DTS";
956         else
957                 info.m_description = "???";
958
959         if (program.audioStreams[i].component_tag != -1)
960         {
961                 ePtr<eServiceEvent> evt;
962                 if (!m_event_handler.getEvent(evt, 0))
963                 {
964                         ePtr<eComponentData> data;
965                         if (!evt->getComponentData(data, program.audioStreams[i].component_tag))
966                                 info.m_language = data->getText();
967                 }
968         }
969
970         if (info.m_language.empty())
971                 info.m_language = program.audioStreams[i].language_code;
972         
973         return 0;
974 }
975
976 int eDVBServicePlay::selectAudioStream(int i)
977 {
978         eDVBServicePMTHandler::program program;
979
980         if (m_service_handler.getProgramInfo(program))
981                 return -1;
982         
983         if ((unsigned int)i >= program.audioStreams.size())
984                 return -2;
985         
986         if (!m_decoder)
987                 return -3;
988         
989         if (m_decoder->setAudioPID(program.audioStreams[i].pid, program.audioStreams[i].type))
990                 return -4;
991
992         if (m_dvb_service && !m_is_pvr)
993         {
994                 if (m_dvb_service && !m_is_pvr)
995                 {
996                         if (program.audioStreams[i].type == eDVBAudio::aMPEG)
997                         {
998                                 m_dvb_service->setCachePID(eDVBService::cAPID, program.audioStreams[i].pid);
999                                 m_dvb_service->setCachePID(eDVBService::cAC3PID, -1);
1000                         }
1001                         else
1002                         {
1003                                 m_dvb_service->setCachePID(eDVBService::cAPID, -1);
1004                                 m_dvb_service->setCachePID(eDVBService::cAC3PID, program.audioStreams[i].pid);
1005                         }
1006                 }
1007         }
1008
1009         m_current_audio_stream = i;
1010
1011         return 0;
1012 }
1013
1014 int eDVBServicePlay::getFrontendInfo(int w)
1015 {
1016         if (m_is_pvr)
1017                 return 0;
1018         eUsePtr<iDVBChannel> channel;
1019         if(m_service_handler.getChannel(channel))
1020                 return 0;
1021         ePtr<iDVBFrontend> fe;
1022         if(channel->getFrontend(fe))
1023                 return 0;
1024         return fe->readFrontendData(w);
1025 }
1026
1027 int eDVBServicePlay::getNumberOfSubservices()
1028 {
1029         ePtr<eServiceEvent> evt;
1030         if (!m_event_handler.getEvent(evt, 0))
1031                 return evt->getNumOfLinkageServices();
1032         return 0;
1033 }
1034
1035 RESULT eDVBServicePlay::getSubservice(eServiceReference &sub, unsigned int n)
1036 {
1037         ePtr<eServiceEvent> evt;
1038         if (!m_event_handler.getEvent(evt, 0))
1039         {
1040                 if (!evt->getLinkageService(sub, m_reference, n))
1041                         return 0;
1042         }
1043         sub.type=eServiceReference::idInvalid;
1044         return -1;
1045 }
1046
1047 RESULT eDVBServicePlay::startTimeshift()
1048 {
1049         if (m_timeshift_enabled)
1050                 return -1;
1051         eDebug("TIMESHIFT - start!");
1052         return 0;
1053 }
1054
1055 RESULT eDVBServicePlay::stopTimeshift()
1056 {
1057         if (!m_timeshift_enabled)
1058                 return -1;
1059         m_timeshift_enabled = 0;
1060         eDebug("timeshift disabled");
1061         return 0;
1062 }
1063
1064 DEFINE_REF(eDVBServicePlay)
1065
1066 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");