fixed refcount of eListboxPythonStringContent
[enigma2.git] / lib / dvb / scan.cpp
1 #include <lib/dvb/idvb.h>
2 #include <lib/dvb_si/sdt.h>
3 #include <lib/dvb_si/nit.h>
4 #include <lib/dvb_si/bat.h>
5 #include <lib/dvb_si/descriptor_tag.h>
6 #include <lib/dvb_si/service_descriptor.h>
7 #include <lib/dvb_si/satellite_delivery_system_descriptor.h>
8 #include <lib/dvb_si/ca_identifier_descriptor.h>
9 #include <lib/dvb/specs.h>
10 #include <lib/dvb/esection.h>
11 #include <lib/dvb/scan.h>
12 #include <lib/dvb/frontend.h>
13 #include <lib/base/eerror.h>
14 #include <errno.h>
15
16 eDVBScan::eDVBScan(iDVBChannel *channel): m_channel(channel)
17 {
18         if (m_channel->getDemux(m_demux))
19                 eDebug("scan: failed to allocate demux!");
20         m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
21 }
22
23 eDVBScan::~eDVBScan()
24 {
25 }
26
27 int eDVBScan::isValidONIDTSID(eOriginalNetworkID onid, eTransportStreamID tsid)
28 {
29         switch (onid.get())
30         {
31         case 0:
32         case 0xFFFF:
33         case 0x1111:
34                 return 0;
35         case 1:
36                 return tsid>1;
37         case 0x00B1:
38                 return tsid != 0x00B0;
39         case 0x0002:
40                 return tsid != 0x07E8;
41         default:
42                 return 1;
43         }
44 }
45
46 eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
47 {
48                 // on valid ONIDs, ignore frequency ("sub network") part
49         if (isValidONIDTSID(onid, tsid))
50                 hash &= ~0xFFFF;
51         return eDVBNamespace(hash);
52 }
53
54 void eDVBScan::stateChange(iDVBChannel *ch)
55 {
56         int state;
57         if (ch->getState(state))
58                 return;
59         if (m_channel_state == state)
60                 return;
61         
62         if (state == iDVBChannel::state_ok)
63         {
64                 startFilter();
65                 m_channel_state = state;
66         } else if (state == iDVBChannel::state_unavailable)
67         {
68                 m_ch_unavailable.push_back(m_ch_current);
69                 nextChannel();
70         }
71 }
72
73 RESULT eDVBScan::nextChannel()
74 {
75         ePtr<iDVBFrontend> fe;
76
77         m_SDT = 0; m_BAT = 0; m_NIT = 0;
78
79         m_ready = readyBAT;
80         if (m_ch_toScan.empty())
81         {
82                 eDebug("no channels left to scan.");
83                 eDebug("%d channels scanned, %d were unavailable.", 
84                                 m_ch_scanned.size(), m_ch_unavailable.size());
85                 eDebug("%d channels in database.", m_new_channels.size());
86                 m_event(evtFinish);
87                 return -ENOENT;
88         }
89         
90         m_ch_current = m_ch_toScan.front();
91         m_ch_toScan.pop_front();
92         
93         if (m_channel->getFrontend(fe))
94                 return -ENOTSUP;
95         
96         m_channel_state = iDVBChannel::state_idle;
97         if (fe->tune(*m_ch_current))
98                 return -EINVAL;
99                 
100         m_event(evtUpdate);
101         return 0;
102 }
103
104 RESULT eDVBScan::startFilter()
105 {
106         assert(m_demux);
107         
108         m_SDT = new eTable<ServiceDescriptionTable>();
109         if (m_SDT->start(m_demux, eDVBSDTSpec()))
110                 return -1;
111         CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
112
113         m_NIT = 0;
114         m_NIT = new eTable<NetworkInformationTable>();
115         if (m_NIT->start(m_demux, eDVBNITSpec()))
116                 return -1;
117         CONNECT(m_NIT->tableReady, eDVBScan::NITready);
118         
119         m_BAT = new eTable<BouquetAssociationTable>();
120         if (m_BAT->start(m_demux, eDVBBATSpec()))
121                 return -1;
122         CONNECT(m_BAT->tableReady, eDVBScan::BATready);
123         
124         return 0;
125 }
126
127 void eDVBScan::SDTready(int err)
128 {
129         eDebug("got sdt");
130         m_ready |= readySDT;
131         if (!err)
132                 m_ready |= validSDT;
133         channelDone();
134 }
135
136 void eDVBScan::NITready(int err)
137 {
138         eDebug("got nit, err %d", err);
139         m_ready |= readyNIT;
140         if (!err)
141                 m_ready |= validNIT;
142         channelDone();
143 }
144
145 void eDVBScan::BATready(int err)
146 {
147         eDebug("got bat");
148         m_ready |= readyBAT;
149         if (!err)
150                 m_ready |= validBAT;
151         channelDone();
152 }
153
154 void eDVBScan::addChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
155 {
156                 /* add it to the list of known channels. */
157         if (chid)
158                 m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
159         
160                 /* check if we don't already have that channel ... */
161                 
162                 /* ... in the list of channels to scan */
163         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end(); ++i)
164                 if (sameChannel(*i, feparm))
165                         return;
166
167                 /* ... in the list of successfully scanned channels */
168         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
169                 if (sameChannel(*i, feparm))
170                         return;
171                 
172                 /* ... in the list of unavailable channels */
173         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
174                 if (sameChannel(*i, feparm))
175                         return;
176
177                 /* ... on the current channel */
178         if (sameChannel(m_ch_current, feparm))
179                 return;
180
181                 /* otherwise, add it to the todo list. */
182         m_ch_toScan.push_back(feparm);
183 }
184
185 int eDVBScan::sameChannel(iDVBFrontendParameters *ch1, iDVBFrontendParameters *ch2) const
186 {
187         int diff;
188         if (ch1->calculateDifference(ch2, diff))
189                 return 0;
190         if (diff < 4000) // more than 4mhz difference?
191                 return 1;
192         return 0;
193 }
194
195 void eDVBScan::channelDone()
196 {
197         if (m_ready & validSDT)
198         {
199                 unsigned long hash = 0;
200                 m_ch_current->getHash(hash);
201                 
202                 eDVBNamespace dvbnamespace = buildNamespace(
203                         (**m_SDT->getSections().begin()).getOriginalNetworkId(),
204                         (**m_SDT->getSections().begin()).getTransportStreamId(),
205                         hash);
206                 
207                 eDebug("SDT: ");
208                 ServiceDescriptionTableConstIterator i;
209                 for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
210                         processSDT(dvbnamespace, **i);
211                 m_ready &= ~validSDT;
212         }
213         
214         if (m_ready & validNIT)
215         {
216                 eDebug("dumping NIT");
217                 NetworkInformationTableConstIterator i;
218                 for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
219                 {
220                         const TransportStreamInfoVector &tsinfovec = *(*i)->getTsInfo();
221                         
222                         for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin()); 
223                                 tsinfo != tsinfovec.end(); ++tsinfo)
224                         {
225                                 eDebug("TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
226                                         (*tsinfo)->getOriginalNetworkId());
227                                 
228                                 eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
229                                 eTransportStreamID tsid = (*tsinfo)->getTransportStreamId();
230                                 
231                                 for (DescriptorConstIterator desc = (*tsinfo)->getDescriptors()->begin();
232                                                 desc != (*tsinfo)->getDescriptors()->end(); ++desc)
233                                 {
234                                         switch ((*desc)->getTag())
235                                         {
236 //                                      case SERVICE_LIST_DESCRIPTOR:
237                                         case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
238                                         {
239                                                 SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
240                                                 eDebug("%d kHz, %d%d%d.%d%c %s MOD:%d %d symb/s, fec %d", 
241                                                                 d.getFrequency(), 
242                                                                 (d.getOrbitalPosition()>>12)&0xF,
243                                                                 (d.getOrbitalPosition()>>8)&0xF,
244                                                                 (d.getOrbitalPosition()>>4)&0xF,
245                                                                 d.getOrbitalPosition()&0xF, d.getWestEastFlag()?'E':'W',
246                                                                 d.getPolarization() ? "hor" : "vert",
247                                                                 d.getModulation(), d.getSymbolRate(), d.getFecInner());
248                                                 
249                                                         /* some sanity checking: below 100MHz is invalid */
250                                                 if (d.getFrequency() < 10000)
251                                                         break;
252                                                 
253                                                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
254                                                 eDVBFrontendParametersSatellite sat;
255                                                 sat.set(d);
256                                                 feparm->setDVBS(sat);
257                                                 unsigned long hash=0;
258                                                 feparm->getHash(hash);
259                                                 
260                                                 eDVBNamespace ns = buildNamespace(onid, tsid, hash);
261
262                                                 addChannel(
263                                                                 eDVBChannelID(ns, tsid, onid),
264                                                                 feparm);
265                                                 break;
266                                         }
267                                         default:
268                                                 eDebug("descr<%x>", (*desc)->getTag());
269                                                 break;
270                                         }
271                                 }
272                                 
273                         }
274                 }
275                 m_ready &= ~validNIT;
276         }
277         
278         if ((m_ready  & readyAll) != readyAll)
279                 return;
280         eDebug("channel done!");
281         m_ch_scanned.push_back(m_ch_current);
282         nextChannel();
283 }
284
285 void eDVBScan::start(const std::list<ePtr<iDVBFrontendParameters> > &known_transponders)
286 {
287         m_ch_toScan.clear();
288         m_ch_scanned.clear();
289         m_ch_unavailable.clear();
290         m_new_channels.clear();
291         m_new_services.clear();
292         m_ch_toScan.insert(m_ch_toScan.end(), known_transponders.begin(), known_transponders.end());
293         nextChannel();
294 }
295
296 void eDVBScan::insertInto(eDVBDB *db)
297 {
298         for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
299                         ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
300                 db->addChannelToList(ch->first, ch->second);
301         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
302                 service(m_new_services.begin()); service != m_new_services.end(); ++service)
303         {
304                 ePtr<eDVBService> dvb_service;
305                 if (!db->getService(service->first, dvb_service))
306                         *dvb_service = *service->second;
307                 else
308                         db->addService(service->first, service->second);
309         }
310 }
311
312 RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescriptionTable &sdt)
313 {
314         const ServiceDescriptionVector &services = *sdt.getDescriptions();
315         eDebug("ONID: %04x", sdt.getOriginalNetworkId());
316         eDVBChannelID chid(dvbnamespace, sdt.getTransportStreamId(), sdt.getOriginalNetworkId());
317         
318         for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
319         {
320                 eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
321
322                 eServiceReferenceDVB ref;
323                 ePtr<eDVBService> service = new eDVBService;
324                 
325                 ref.set(chid);
326                 ref.setServiceID((*s)->getServiceId());
327
328                 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
329                                 desc != (*s)->getDescriptors()->end(); ++desc)
330                         if ((*desc)->getTag() == SERVICE_DESCRIPTOR)
331                                 ref.setServiceType(((ServiceDescriptor&)**desc).getServiceType());
332                 
333                 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
334                                 desc != (*s)->getDescriptors()->end(); ++desc)
335                 {
336                         switch ((*desc)->getTag())
337                         {
338                         case SERVICE_DESCRIPTOR:
339                         {
340                                 ServiceDescriptor &d = (ServiceDescriptor&)**desc;
341                                 eDebug("name '%s', provider_name '%s'", d.getServiceName().c_str(), d.getServiceProviderName().c_str());
342                                 service->m_service_name = d.getServiceName();
343                                 service->m_provider_name = d.getServiceProviderName();
344                                 break;
345                         }
346                         case CA_IDENTIFIER_DESCRIPTOR:
347                         {
348                                 CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
349                                 const CaSystemIdVector &caids = *d.getCaSystemIds();
350                                 eDebugNoNewLine("CA ");
351                                 for (CaSystemIdVector::const_iterator i(caids.begin()); i != caids.end(); ++i)
352                                 {
353                                         eDebugNoNewLine("%04x ", *i);
354                                         service->m_ca.insert(*i);
355                                 }
356                                 eDebug("");
357                                 break;
358                         }
359                         default:
360                                 eDebug("descr<%x>", (*desc)->getTag());
361                                 break;
362                         }
363                 }
364                 
365                 m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));               
366         }
367         return 0;
368 }
369
370 RESULT eDVBScan::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
371 {
372         connection = new eConnection(this, m_event.connect(event));
373         return 0;
374 }