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