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