- add "getCurrent" to service listbox
[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 DEFINE_REF(eServiceFactoryDVB)
13
14 eServiceFactoryDVB::eServiceFactoryDVB()
15 {
16         ePtr<eServiceCenter> sc;
17         
18         eServiceCenter::getInstance(sc);
19         if (sc)
20                 sc->addServiceFactory(eServiceFactoryDVB::id, this);
21 }
22
23 eServiceFactoryDVB::~eServiceFactoryDVB()
24 {
25         ePtr<eServiceCenter> sc;
26         
27         eServiceCenter::getInstance(sc);
28         if (sc)
29                 sc->removeServiceFactory(eServiceFactoryDVB::id);
30 }
31
32 DEFINE_REF(eDVBServiceList);
33
34 eDVBServiceList::eDVBServiceList(const eServiceReference &parent): m_parent(parent)
35 {
36 }
37
38 eDVBServiceList::~eDVBServiceList()
39 {
40 }
41
42 RESULT eDVBServiceList::getContent(std::list<eServiceReference> &list)
43 {
44         ePtr<iDVBChannelList> db;
45         ePtr<eDVBResourceManager> res;
46         
47         int err;
48         if ((err = eDVBResourceManager::getInstance(res)) != 0)
49         {
50                 eDebug("no resource manager");
51                 return err;
52         }
53         if ((err = res->getChannelList(db)) != 0)
54         {
55                 eDebug("no channel list");
56                 return err;
57         }
58         
59         ePtr<iDVBChannelListQuery> query;
60         
61         ePtr<eDVBChannelQuery> q;
62         
63         if (m_parent.path.size())
64                 eDVBChannelQuery::compile(q, m_parent.path);
65         
66         if ((err = db->startQuery(query, q)) != 0)
67         {
68                 eDebug("startQuery failed");
69                 return err;
70         }
71         
72         eServiceReferenceDVB ref;
73         
74         while (!query->getNextResult(ref))
75                 list.push_back(ref);
76         return 0;
77 }
78
79 RESULT eServiceFactoryDVB::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
80 {
81                 // check resources...
82         ptr = new eDVBServicePlay(ref);
83         return 0;
84 }
85
86 RESULT eServiceFactoryDVB::record(const eServiceReference &, ePtr<iRecordableService> &ptr)
87 {
88         ptr = 0;
89         return -1;
90 }
91
92 RESULT eServiceFactoryDVB::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
93 {
94         ptr = new eDVBServiceList(ref);
95         return 0;
96 }
97
98 RESULT eServiceFactoryDVB::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
99 {
100         ptr = 0;
101                         // TODO: handle the listing itself
102         // if (ref.... == -1) .. return "... bouquets ...";
103         // could be also done in another serviceFactory (with seperate ID) to seperate actual services and lists
104                         // TODO: cache
105         ePtr<iDVBChannelList> db;
106         ePtr<eDVBResourceManager> res;
107         
108         int err;
109         if ((err = eDVBResourceManager::getInstance(res)) != 0)
110         {
111                 eDebug("no resource manager");
112                 return err;
113         }
114         if ((err = res->getChannelList(db)) != 0)
115         {
116                 eDebug("no channel list");
117                 return err;
118         }
119         
120         ePtr<eDVBService> service;
121
122                 /* we are sure to have a ..DVB reference as the info() call was forwarded here according to it's ID. */
123         if ((err = db->getService((eServiceReferenceDVB&)ref, service)) != 0)
124         {
125                 eDebug("getService failed!");
126                 return err;
127         }
128         
129                 /* eDVBService has the iStaticServiceInformation interface, so we pass it here. */
130         ptr = service;
131         return 0;
132 }
133
134 eDVBServicePlay::eDVBServicePlay(const eServiceReference &ref): 
135         m_reference(ref)
136 {
137         CONNECT(m_serviceHandler.serviceEvent, eDVBServicePlay::serviceEvent);
138         eDebug("DVB start (play)");
139 }
140
141 eDVBServicePlay::~eDVBServicePlay()
142 {
143         eDebug("DVB stop (play)");
144 }
145
146 void eDVBServicePlay::serviceEvent(int event)
147 {
148         eDebug("service event %d", event);
149         switch (event)
150         {
151         case eDVBServicePMTHandler::eventNewProgramInfo:
152         {
153                 int vpid = -1, apid = -1, pcrpid = -1;
154                 eDVBServicePMTHandler::program program;
155                 if (m_serviceHandler.getProgramInfo(program))
156                         eDebug("getting program info failed.");
157                 else
158                 {
159                         eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
160                         if (!program.videoStreams.empty())
161                         {
162                                 eDebugNoNewLine(" (");
163                                 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
164                                         i(program.videoStreams.begin()); 
165                                         i != program.videoStreams.end(); ++i)
166                                 {
167                                         if (vpid == -1)
168                                                 vpid = i->pid;
169                                         if (i != program.videoStreams.begin())
170                                                 eDebugNoNewLine(", ");
171                                         eDebugNoNewLine("%04x", i->pid);
172                                 }
173                                 eDebugNoNewLine(")");
174                         }
175                         eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
176                         if (!program.audioStreams.empty())
177                         {
178                                 eDebugNoNewLine(" (");
179                                 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
180                                         i(program.audioStreams.begin()); 
181                                         i != program.audioStreams.end(); ++i)
182                                 {
183                                         if (apid == -1)
184                                                 apid = i->pid;
185                                         if (i != program.audioStreams.begin())
186                                                 eDebugNoNewLine(", ");
187                                         eDebugNoNewLine("%04x", i->pid);
188                                 }
189                                 eDebugNoNewLine(")");
190                         }
191                         eDebug(", and the pcr pid is %04x", program.pcrPid);
192                         if (program.pcrPid != 0x1fff)
193                                 pcrpid = program.pcrPid;
194                 }
195                 
196                 if (!m_decoder)
197                 {
198                         ePtr<iDVBDemux> demux;
199                         m_serviceHandler.getDemux(demux);
200                         if (demux)
201                                 demux->getMPEGDecoder(m_decoder);
202                 }
203
204                 if (m_decoder)
205                 {
206                         m_decoder->setVideoPID(vpid);
207                         m_decoder->setAudioPID(apid, 0);
208                         m_decoder->setSyncPCR(pcrpid);
209                         m_decoder->start();
210                 }
211                                 
212                 break;
213         }
214         }
215 }
216
217 RESULT eDVBServicePlay::start()
218 {
219         eDebug("starting DVB service");
220         return m_serviceHandler.tune((eServiceReferenceDVB&)m_reference);
221 }
222
223 RESULT eDVBServicePlay::stop()
224 {
225         eDebug("stopping..");
226         return 0;
227 }
228
229 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
230 {
231         return -1;
232 }
233
234 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
235 {
236                 // not yet possible, maybe later...
237         ptr = 0;
238         return -1;
239 }
240
241 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
242 {
243         ptr = this;
244         return 0;
245 }
246
247 RESULT eDVBServicePlay::getName(const eServiceReference &ref, std::string &name)
248 {
249         name = "DVB service";
250         return 0;
251 }
252
253 DEFINE_REF(eDVBServicePlay)
254
255 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");