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