- started work on EIT
[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_service_handler.serviceEvent, eDVBServicePlay::serviceEvent);
138         CONNECT(m_event_handler.m_eit_changed, eDVBServicePlay::gotNewEvent);
139         eDebug("DVB start (play)");
140 }
141
142 eDVBServicePlay::~eDVBServicePlay()
143 {
144         eDebug("DVB stop (play)");
145 }
146
147 void eDVBServicePlay::gotNewEvent()
148 {
149                 // debug only
150         ePtr<eServiceEvent> m_event_now, m_event_next;
151         getEvent(m_event_now, 0);
152         getEvent(m_event_next, 1);
153
154         if (m_event_now)
155                 eDebug("now running: %s (%d seconds :)", m_event_now->m_event_name.c_str(), m_event_now->m_duration);
156         if (m_event_next)
157                 eDebug("next running: %s (%d seconds :)", m_event_next->m_event_name.c_str(), m_event_next->m_duration);
158 }
159
160 void eDVBServicePlay::serviceEvent(int event)
161 {
162         eDebug("service event %d", event);
163         switch (event)
164         {
165         case eDVBServicePMTHandler::eventTuned:
166         {
167                 ePtr<iDVBDemux> m_demux;
168                 if (!m_service_handler.getDemux(m_demux))
169                 {
170 //                      eventStartedEventAcquisition
171                         m_event_handler.start(m_demux, ((eServiceReferenceDVB&)m_reference).getServiceID().get());
172                 } else
173                         eDebug("no event data available :( ");
174 //                      eventNoEvent
175                 break;
176         }
177         case eDVBServicePMTHandler::eventNewProgramInfo:
178         {
179                 int vpid = -1, apid = -1, pcrpid = -1;
180                 eDVBServicePMTHandler::program program;
181                 if (m_service_handler.getProgramInfo(program))
182                         eDebug("getting program info failed.");
183                 else
184                 {
185                         eDebugNoNewLine("have %d video stream(s)", program.videoStreams.size());
186                         if (!program.videoStreams.empty())
187                         {
188                                 eDebugNoNewLine(" (");
189                                 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
190                                         i(program.videoStreams.begin()); 
191                                         i != program.videoStreams.end(); ++i)
192                                 {
193                                         if (vpid == -1)
194                                                 vpid = i->pid;
195                                         if (i != program.videoStreams.begin())
196                                                 eDebugNoNewLine(", ");
197                                         eDebugNoNewLine("%04x", i->pid);
198                                 }
199                                 eDebugNoNewLine(")");
200                         }
201                         eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
202                         if (!program.audioStreams.empty())
203                         {
204                                 eDebugNoNewLine(" (");
205                                 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
206                                         i(program.audioStreams.begin()); 
207                                         i != program.audioStreams.end(); ++i)
208                                 {
209                                         if (apid == -1)
210                                                 apid = i->pid;
211                                         if (i != program.audioStreams.begin())
212                                                 eDebugNoNewLine(", ");
213                                         eDebugNoNewLine("%04x", i->pid);
214                                 }
215                                 eDebugNoNewLine(")");
216                         }
217                         eDebug(", and the pcr pid is %04x", program.pcrPid);
218                         if (program.pcrPid != 0x1fff)
219                                 pcrpid = program.pcrPid;
220                 }
221                 
222                 if (!m_decoder)
223                 {
224                         ePtr<iDVBDemux> demux;
225                         m_service_handler.getDemux(demux);
226                         if (demux)
227                                 demux->getMPEGDecoder(m_decoder);
228                 }
229
230                 if (m_decoder)
231                 {
232                         m_decoder->setVideoPID(vpid);
233                         m_decoder->setAudioPID(apid, 0);
234                         m_decoder->setSyncPCR(pcrpid);
235                         m_decoder->start();
236                 }
237                 
238                 break;
239         }
240         }
241 }
242
243 RESULT eDVBServicePlay::start()
244 {
245         eDebug("starting DVB service");
246         return m_service_handler.tune((eServiceReferenceDVB&)m_reference);
247 }
248
249 RESULT eDVBServicePlay::stop()
250 {
251         eDebug("stopping..");
252         return 0;
253 }
254
255 RESULT eDVBServicePlay::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
256 {
257         return -1;
258 }
259
260 RESULT eDVBServicePlay::pause(ePtr<iPauseableService> &ptr)
261 {
262                 // not yet possible, maybe later...
263         ptr = 0;
264         return -1;
265 }
266
267 RESULT eDVBServicePlay::info(ePtr<iServiceInformation> &ptr)
268 {
269         ptr = this;
270         return 0;
271 }
272
273 RESULT eDVBServicePlay::getName(const eServiceReference &ref, std::string &name)
274 {
275         name = "DVB service";
276         return 0;
277 }
278
279 RESULT eDVBServicePlay::getEvent(ePtr<eServiceEvent> &evt, int nownext)
280 {
281         return m_event_handler.getEvent(evt, nownext);
282 }
283
284 DEFINE_REF(eDVBServicePlay)
285
286 eAutoInitPtr<eServiceFactoryDVB> init_eServiceFactoryDVB(eAutoInitNumbers::service+1, "eServiceFactoryDVB");