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>
16 #define SCAN_eDebug(x...) eDebug(x)
17 #define SCAN_eDebugNoNewLine(x...) eDebugNoNewLine(x)
21 eDVBScan::eDVBScan(iDVBChannel *channel): m_channel(channel)
23 if (m_channel->getDemux(m_demux))
24 SCAN_eDebug("scan: failed to allocate demux!");
25 m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
32 int eDVBScan::isValidONIDTSID(eOriginalNetworkID onid, eTransportStreamID tsid)
43 return tsid != 0x00B0;
45 return tsid != 0x07E8;
51 eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
53 // on valid ONIDs, ignore frequency ("sub network") part
54 if (isValidONIDTSID(onid, tsid))
56 return eDVBNamespace(hash);
59 void eDVBScan::stateChange(iDVBChannel *ch)
62 if (ch->getState(state))
64 if (m_channel_state == state)
67 if (state == iDVBChannel::state_ok)
70 m_channel_state = state;
71 } else if (state == iDVBChannel::state_unavailable)
73 m_ch_unavailable.push_back(m_ch_current);
78 RESULT eDVBScan::nextChannel()
80 ePtr<iDVBFrontend> fe;
82 m_SDT = 0; m_BAT = 0; m_NIT = 0;
85 if (m_ch_toScan.empty())
87 eDebug("no channels left to scan.");
88 eDebug("%d channels scanned, %d were unavailable.",
89 m_ch_scanned.size(), m_ch_unavailable.size());
90 eDebug("%d channels in database.", m_new_channels.size());
95 m_ch_current = m_ch_toScan.front();
96 m_ch_toScan.pop_front();
98 if (m_channel->getFrontend(fe))
104 m_channel_state = iDVBChannel::state_idle;
105 if (fe->tune(*m_ch_current))
115 RESULT eDVBScan::startFilter()
119 m_SDT = new eTable<ServiceDescriptionTable>();
120 if (m_SDT->start(m_demux, eDVBSDTSpec()))
122 CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
125 m_NIT = new eTable<NetworkInformationTable>();
126 if (m_NIT->start(m_demux, eDVBNITSpec()))
128 CONNECT(m_NIT->tableReady, eDVBScan::NITready);
130 m_BAT = new eTable<BouquetAssociationTable>();
131 if (m_BAT->start(m_demux, eDVBBATSpec()))
133 CONNECT(m_BAT->tableReady, eDVBScan::BATready);
138 void eDVBScan::SDTready(int err)
140 SCAN_eDebug("got sdt");
147 void eDVBScan::NITready(int err)
149 SCAN_eDebug("got nit, err %d", err);
156 void eDVBScan::BATready(int err)
158 SCAN_eDebug("got bat");
165 void eDVBScan::addChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
167 /* add it to the list of known channels. */
169 m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
171 /* check if we don't already have that channel ... */
173 /* ... in the list of channels to scan */
174 for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end(); ++i)
175 if (sameChannel(*i, feparm))
178 /* ... in the list of successfully scanned channels */
179 for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
180 if (sameChannel(*i, feparm))
183 /* ... in the list of unavailable channels */
184 for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
185 if (sameChannel(*i, feparm))
188 /* ... on the current channel */
189 if (sameChannel(m_ch_current, feparm))
192 /* otherwise, add it to the todo list. */
193 m_ch_toScan.push_back(feparm);
196 int eDVBScan::sameChannel(iDVBFrontendParameters *ch1, iDVBFrontendParameters *ch2) const
199 if (ch1->calculateDifference(ch2, diff))
201 if (diff < 4000) // more than 4mhz difference?
206 void eDVBScan::channelDone()
208 if (m_ready & validSDT)
210 unsigned long hash = 0;
211 m_ch_current->getHash(hash);
213 eDVBNamespace dvbnamespace = buildNamespace(
214 (**m_SDT->getSections().begin()).getOriginalNetworkId(),
215 (**m_SDT->getSections().begin()).getTransportStreamId(),
218 SCAN_eDebug("SDT: ");
219 ServiceDescriptionTableConstIterator i;
220 for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
221 processSDT(dvbnamespace, **i);
222 m_ready &= ~validSDT;
225 if (m_ready & validNIT)
227 SCAN_eDebug("dumping NIT");
228 NetworkInformationTableConstIterator i;
229 for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
231 const TransportStreamInfoVector &tsinfovec = *(*i)->getTsInfo();
233 for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin());
234 tsinfo != tsinfovec.end(); ++tsinfo)
236 SCAN_eDebug("TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
237 (*tsinfo)->getOriginalNetworkId());
239 eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
240 eTransportStreamID tsid = (*tsinfo)->getTransportStreamId();
242 for (DescriptorConstIterator desc = (*tsinfo)->getDescriptors()->begin();
243 desc != (*tsinfo)->getDescriptors()->end(); ++desc)
245 switch ((*desc)->getTag())
247 // case SERVICE_LIST_DESCRIPTOR:
248 case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
250 SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
251 SCAN_eDebug("%d kHz, %d%d%d.%d%c %s MOD:%d %d symb/s, fec %d",
253 (d.getOrbitalPosition()>>12)&0xF,
254 (d.getOrbitalPosition()>>8)&0xF,
255 (d.getOrbitalPosition()>>4)&0xF,
256 d.getOrbitalPosition()&0xF, d.getWestEastFlag()?'E':'W',
257 d.getPolarization() ? "hor" : "vert",
258 d.getModulation(), d.getSymbolRate(), d.getFecInner());
260 /* some sanity checking: below 100MHz is invalid */
261 if (d.getFrequency() < 10000)
264 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
265 eDVBFrontendParametersSatellite sat;
267 feparm->setDVBS(sat);
268 unsigned long hash=0;
269 feparm->getHash(hash);
271 eDVBNamespace ns = buildNamespace(onid, tsid, hash);
274 eDVBChannelID(ns, tsid, onid),
279 SCAN_eDebug("descr<%x>", (*desc)->getTag());
286 m_ready &= ~validNIT;
289 if ((m_ready & readyAll) != readyAll)
291 SCAN_eDebug("channel done!");
292 m_ch_scanned.push_back(m_ch_current);
296 void eDVBScan::start(const std::list<ePtr<iDVBFrontendParameters> > &known_transponders)
299 m_ch_scanned.clear();
300 m_ch_unavailable.clear();
301 m_new_channels.clear();
302 m_new_services.clear();
303 m_ch_toScan.insert(m_ch_toScan.end(), known_transponders.begin(), known_transponders.end());
307 void eDVBScan::insertInto(iDVBChannelList *db)
309 for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator
310 ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
311 db->addChannelToList(ch->first, ch->second);
312 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
313 service(m_new_services.begin()); service != m_new_services.end(); ++service)
315 ePtr<eDVBService> dvb_service;
316 if (!db->getService(service->first, dvb_service))
317 *dvb_service = *service->second;
319 db->addService(service->first, service->second);
323 RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescriptionTable &sdt)
325 const ServiceDescriptionVector &services = *sdt.getDescriptions();
326 SCAN_eDebug("ONID: %04x", sdt.getOriginalNetworkId());
327 eDVBChannelID chid(dvbnamespace, sdt.getTransportStreamId(), sdt.getOriginalNetworkId());
329 for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
331 SCAN_eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
333 eServiceReferenceDVB ref;
334 ePtr<eDVBService> service = new eDVBService;
337 ref.setServiceID((*s)->getServiceId());
339 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
340 desc != (*s)->getDescriptors()->end(); ++desc)
341 if ((*desc)->getTag() == SERVICE_DESCRIPTOR)
342 ref.setServiceType(((ServiceDescriptor&)**desc).getServiceType());
344 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
345 desc != (*s)->getDescriptors()->end(); ++desc)
347 switch ((*desc)->getTag())
349 case SERVICE_DESCRIPTOR:
351 ServiceDescriptor &d = (ServiceDescriptor&)**desc;
352 SCAN_eDebug("name '%s', provider_name '%s'", d.getServiceName().c_str(), d.getServiceProviderName().c_str());
353 service->m_service_name = d.getServiceName();
354 service->m_provider_name = d.getServiceProviderName();
357 case CA_IDENTIFIER_DESCRIPTOR:
359 CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
360 const CaSystemIdVector &caids = *d.getCaSystemIds();
361 SCAN_eDebugNoNewLine("CA ");
362 for (CaSystemIdVector::const_iterator i(caids.begin()); i != caids.end(); ++i)
364 SCAN_eDebugNoNewLine("%04x ", *i);
365 service->m_ca.insert(*i);
371 SCAN_eDebug("descr<%x>", (*desc)->getTag());
376 m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));
381 RESULT eDVBScan::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
383 connection = new eConnection(this, m_event.connect(event));
387 void eDVBScan::getStats(int &transponders_done, int &transponders_total, int &services)
389 transponders_done = m_ch_scanned.size() + m_ch_unavailable.size();
390 transponders_total = m_ch_toScan.size() + transponders_done;
391 services = m_new_services.size();