def updateTransponders(self, transponders, read_other_section=False, netid=None, bouquettype=None):
        print >> log, "[DvbScanner] Reading transponders..."

        if self.nit_other_table_id == 0x00:
            mask = 0xFF
        else:
            mask = self.nit_current_table_id ^ self.nit_other_table_id ^ 0xFF

        print >> log, "[DvbScanner] demuxer_device", str(self.demuxer_device)
        print >> log, "[DvbScanner] nit_pid", str(self.nit_pid)
        print >> log, "[DvbScanner] nit_current_table_id", str(self.nit_current_table_id)
        print >> log, "[DvbScanner] mask", str(mask)
        print >> log, "[DvbScanner] frontend", str(self.frontend)
        fd = dvbreader.open(self.demuxer_device, self.nit_pid, self.nit_current_table_id, mask, self.frontend)
        if fd < 0:
            print >> log, "[DvbScanner] Cannot open the demuxer"
            return None

        nit_current_section_version = -1
        nit_current_section_network_id = -1
        nit_current_sections_read = []
        nit_current_sections_count = 0
        nit_current_content = []
        nit_current_completed = False

        nit_other_section_version = -1
        nit_other_section_network_id = -1
        nit_other_sections_read = []
        nit_other_sections_count = 0
        nit_other_content = []
        nit_other_completed = not read_other_section or self.nit_other_table_id == 0x00

        timeout = datetime.datetime.now()
        timeout += datetime.timedelta(0, self.TIMEOUT_SEC)
        while True:
            if datetime.datetime.now() > timeout:
                print >> log, "[DvbScanner] Timed out"
                break

            section = dvbreader.read_nit(fd, self.nit_current_table_id, self.nit_other_table_id)
            if section is None:
                time.sleep(0.1)  # no data.. so we wait a bit
                continue

            if (
                section["header"]["table_id"] == self.nit_current_table_id
                and self.dvbtype != "dvbc"
                and not nit_current_completed
            ):
                if (
                    section["header"]["version_number"] != nit_current_section_version
                    or section["header"]["network_id"] != nit_current_section_network_id
                ):
                    nit_current_section_version = section["header"]["version_number"]
                    nit_current_section_network_id = section["header"]["network_id"]
                    nit_current_sections_read = []
                    nit_current_content = []
                    nit_current_sections_count = section["header"]["last_section_number"] + 1

                if section["header"]["section_number"] not in nit_current_sections_read:
                    nit_current_sections_read.append(section["header"]["section_number"])
                    nit_current_content += section["content"]

                    if len(nit_current_sections_read) == nit_current_sections_count:
                        nit_current_completed = True

            elif (
                str(section["header"]["network_id"]) == str(netid)
                and self.dvbtype == "dvbc"
                and not nit_current_completed
            ):
                if (
                    section["header"]["version_number"] != nit_current_section_version
                    or section["header"]["network_id"] != nit_current_section_network_id
                ):
                    nit_current_section_version = section["header"]["version_number"]
                    nit_current_section_network_id = section["header"]["network_id"]
                    nit_current_sections_read = []
                    nit_current_content = []
                    nit_current_sections_count = section["header"]["last_section_number"] + 1

                if section["header"]["section_number"] not in nit_current_sections_read:
                    nit_current_sections_read.append(section["header"]["section_number"])
                    nit_current_content += section["content"]

                    if len(nit_current_sections_read) == nit_current_sections_count:
                        nit_current_completed = True
                        nit_other_completed = True

            elif section["header"]["table_id"] == self.nit_other_table_id and not nit_other_completed:
                if (
                    section["header"]["version_number"] != nit_other_section_version
                    or section["header"]["network_id"] != nit_other_section_network_id
                ):
                    nit_other_section_version = section["header"]["version_number"]
                    nit_other_section_network_id = section["header"]["network_id"]
                    nit_other_sections_read = []
                    nit_other_content = []
                    nit_other_sections_count = section["header"]["last_section_number"] + 1

                if section["header"]["section_number"] not in nit_other_sections_read:
                    nit_other_sections_read.append(section["header"]["section_number"])
                    nit_other_content += section["content"]

                    if len(nit_other_sections_read) == nit_other_sections_count:
                        nit_other_completed = True

            if nit_current_completed and nit_other_completed:
                print >> log, "[DvbScanner] Scan complete, netid: ", str(netid)
                break

        dvbreader.close(fd)

        nit_content = nit_current_content
        nit_content += nit_other_content

        transport_stream_id_list = []
        logical_channel_number_dict = {}
        logical_channel_number_dict_tmp = {}
        hd_logical_channel_number_dict_tmp = {}
        transponders_count = 0
        for transponder in nit_content:
            print "LINE:", transponder
            if len(transponder) == 5:  # lcn
                key = "%x:%x:%x" % (
                    transponder["transport_stream_id"],
                    transponder["original_network_id"],
                    transponder["service_id"],
                )
                logical_channel_number_dict_tmp[key] = transponder
                continue
            if len(transponder) == 6:  # HD lcn
                key = "%x:%x:%x" % (
                    transponder["transport_stream_id"],
                    transponder["original_network_id"],
                    transponder["service_id"],
                )
                hd_logical_channel_number_dict_tmp[key] = transponder
                continue
            transponder["services"] = {}
            transponder["dvb_type"] = self.dvbtype
            transponder["bouquet_type"] = bouquettype

            if transponder["dvb_type"] == "dvbc":  # DVB-C
                transponder["symbol_rate"] = transponder["symbol_rate"] * 100
                transponder["flags"] = 0
                if transponder["fec_inner"] != 15 and transponder["fec_inner"] > 9:
                    transponder["fec_inner"] = 0
                transponder["frequency"] = transponder["frequency"] / 10
                transponder["namespace"] = 0xFFFF0000
                transponder["inversion"] = transponder["fec_outer"]
                transponder["modulation_system"] = 0
            elif transponder["dvb_type"] == "dvbt":  # DVB-T
                transponder["namespace"] = 0xEEEE0000
                transponder["frequency"] = transponder["frequency"] * 10
                transponder["inversion"] = 0
                transponder["plpid"] = 0
                transponder["flags"] = 0
                transponder["system"] = 0
            elif transponder["dvb_type"] == "dvbs":  # DVB-S
                transponder["symbol_rate"] = transponder["symbol_rate"] * 100
                transponder["flags"] = 0
                if transponder["fec_inner"] != 15 and transponder["fec_inner"] > 9:
                    transponder["fec_inner"] = 0
                transponder["frequency"] = transponder["frequency"] * 10
                orbital_position = ((transponder["orbital_position"] >> 12) & 0x0F) * 1000
                orbital_position += ((transponder["orbital_position"] >> 8) & 0x0F) * 100
                orbital_position += ((transponder["orbital_position"] >> 4) & 0x0F) * 10
                orbital_position += transponder["orbital_position"] & 0x0F
                if orbital_position != 0 and transponder["west_east_flag"] == 0:
                    orbital_position = 3600 - orbital_position
                transponder["orbital_position"] = orbital_position
                transponder["pilot"] = 2

                if transponder["modulation_system"] == 0 and transponder["modulation_type"] == 2:
                    transponder["modulation_type"] = 1
                transponder["inversion"] = 2
                transponder["namespace"] = self.buildNamespace(transponder)

            key = "%x:%x:%x" % (
                transponder["namespace"],
                transponder["transport_stream_id"],
                transponder["original_network_id"],
            )

            if key in transponders:
                transponder["services"] = transponders[key]["services"]
            transponders[key] = transponder
            transponders_count += 1

            if transponder["transport_stream_id"] not in transport_stream_id_list:
                transport_stream_id_list.append(transponder["transport_stream_id"])

        if read_other_section:
            print >> log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x and network_id = 0x%x" % (
                transponders_count,
                nit_current_section_network_id,
                nit_other_section_network_id,
            )
        else:
            print >> log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x" % (
                transponders_count,
                nit_current_section_network_id,
            )

        if len(hd_logical_channel_number_dict_tmp) > 0 and bouquettype == "hd":
            for id in logical_channel_number_dict_tmp:
                if id in hd_logical_channel_number_dict_tmp:
                    lcntofind = hd_logical_channel_number_dict_tmp[id]["logical_channel_number"]
                    lcnreplace = logical_channel_number_dict_tmp[id]["logical_channel_number"]
                    for id2 in logical_channel_number_dict_tmp:
                        if logical_channel_number_dict_tmp[id2]["logical_channel_number"] == lcntofind:
                            logical_channel_number_dict[id] = logical_channel_number_dict_tmp[id2]
                            logical_channel_number_dict[id]["logical_channel_number"] = lcnreplace
                    logical_channel_number_dict[id] = hd_logical_channel_number_dict_tmp[id]
                else:
                    logical_channel_number_dict[id] = logical_channel_number_dict_tmp[id]
        else:
            for id in logical_channel_number_dict_tmp:
                logical_channel_number_dict[id] = logical_channel_number_dict_tmp[id]

        return {
            "transport_stream_id_list": transport_stream_id_list,
            "logical_channel_number_dict": logical_channel_number_dict,
        }
	def readNIT(self):
		adapter = 0
		demuxer_device = "/dev/dvb/adapter%d/demux%d" % (adapter, self.demuxer_id)

		nit_current_pid = 0x10
		nit_current_table_id = 0x40
		nit_other_table_id = 0x00 # don't read other table
		if nit_other_table_id == 0x00:
			mask = 0xff
		else:
			mask = nit_current_table_id ^ nit_other_table_id ^ 0xff
		nit_current_timeout = 20 # maximum time allowed to read the network information table (seconds)

		nit_current_version_number = -1
		nit_current_sections_read = []
		nit_current_sections_count = 0
		nit_current_content = []
		nit_current_completed = False

		fd = dvbreader.open(demuxer_device, nit_current_pid, nit_current_table_id, mask, self.selectedNIM)
		if fd < 0:
			print "[MakeBouquet][readNIT] Cannot open the demuxer"
			return

		timeout = datetime.datetime.now()
		timeout += datetime.timedelta(0, nit_current_timeout)

		while True:
			if datetime.datetime.now() > timeout:
				print "[MakeBouquet][readNIT] Timed out reading NIT"
				break

			section = dvbreader.read_nit(fd, nit_current_table_id, nit_other_table_id)
			if section is None:
				time.sleep(0.1)	# no data.. so we wait a bit
				continue

			if section["header"]["table_id"] == nit_current_table_id and not nit_current_completed:
				if section["header"]["version_number"] != nit_current_version_number:
					nit_current_version_number = section["header"]["version_number"]
					nit_current_sections_read = []
					nit_current_sections_count = section["header"]["last_section_number"] + 1
					nit_current_content = []

				if section["header"]["section_number"] not in nit_current_sections_read:
					nit_current_sections_read.append(section["header"]["section_number"])
					nit_current_content += section["content"]

					if len(nit_current_sections_read) == nit_current_sections_count:
						nit_current_completed = True

			if nit_current_completed:
				break

		dvbreader.close(fd)

		if not nit_current_content:
			print "[MakeBouquet][readNIT] current transponder not found"
			return

		# descriptor_tag 0x5A is DVB-T, descriptor_tag 0x7f is DVB-T
		transponders = [t for t in nit_current_content if "descriptor_tag" in t and t["descriptor_tag"] in (0x5A, 0x7f) and t["original_network_id"] == self.transponder["onid"] and t["transport_stream_id"] == self.transponder["tsid"]] # this should only ever have a length of one transponder
		print "[MakeBouquet][readNIT] transponders", transponders
		if transponders:

			if transponders[0]["descriptor_tag"] == 0x5A: # DVB-T
				self.transponder["system"] = eDVBFrontendParametersTerrestrial.System_DVB_T
			else: # must be DVB-T2
				self.transponder["system"] = eDVBFrontendParametersTerrestrial.System_DVB_T2

			if "frequency" in transponders[0] and abs((transponders[0]["frequency"]*10) - self.transponder["frequency"]) < 1000000 and self.transponder["frequency"] != transponders[0]["frequency"]*10:
				print "[MakeBouquet][readNIT] updating transponder frequency from %.03f MHz to %.03f MHz" % (self.transponder["frequency"]/1000000, transponders[0]["frequency"]/100000)
				self.transponder["frequency"] = transponders[0]["frequency"]*10

		LCNs = [t for t in nit_current_content if "descriptor_tag" in t and t["descriptor_tag"] == 0x83 and t["original_network_id"] == self.transponder["onid"]]
		print "[MakeBouquet][readNIT] LCNs", LCNs
		if LCNs:
			for LCN in LCNs:
				LCNkey = "%x:%x:%x" % (LCN["transport_stream_id"], LCN["original_network_id"], LCN["service_id"])

				if not self.ignore_visible_service_flag and "visible_service_flag" in LCN and LCN["visible_service_flag"] == 0:
					continue

				# Only write to the dict if there is no entry, or override the entry if the data comes from the same transponder the channel is located on.
				if LCNkey not in self.logical_channel_number_dict or LCN["transport_stream_id"] == self.transponder["tsid"]:
					self.logical_channel_number_dict[LCNkey] = LCN

		namespace = 0xEEEE0000
		if self.namespace_complete_terrestrial:
			namespace |= (self.transponder['frequency']/1000000)&0xFFFF
		namespacekey = "%x:%x" % (self.transponder["tsid"], self.transponder["onid"])
		self.namespace_dict[namespacekey] = namespace
	def updateTransponders(self, transponders, read_other_section = False, netid = None, bouquettype = None):
		print>>log, "[DvbScanner] Reading transponders..."

		if self.nit_other_table_id == 0x00:
			mask = 0xff
		else:
			mask = self.nit_current_table_id ^ self.nit_other_table_id ^ 0xff

		print>>log, "[DvbScanner] demuxer_device", str(self.demuxer_device)
		print>>log, "[DvbScanner] nit_pid", str(self.nit_pid)
		print>>log, "[DvbScanner] nit_current_table_id", str(self.nit_current_table_id)
		print>>log, "[DvbScanner] mask", str(mask)
		print>>log, "[DvbScanner] frontend", str(self.frontend)
		fd = dvbreader.open(self.demuxer_device, self.nit_pid, self.nit_current_table_id, mask, self.frontend)
		if fd < 0:
			print>>log, "[DvbScanner] Cannot open the demuxer"
			return None

		nit_current_section_version = -1
		nit_current_section_network_id = -1
		nit_current_sections_read = []
		nit_current_sections_count = 0
		nit_current_content = []
		nit_current_completed = False

		nit_other_section_version = -1
		nit_other_section_network_id = -1
		nit_other_sections_read = []
		nit_other_sections_count = 0
		nit_other_content = []
		nit_other_completed = not read_other_section or self.nit_other_table_id == 0x00

		timeout = datetime.datetime.now()
		timeout += datetime.timedelta(0, self.TIMEOUT_SEC)
		while True:
			if datetime.datetime.now() > timeout:
				print>>log, "[DvbScanner] Timed out"
				break

			section = dvbreader.read_nit(fd, self.nit_current_table_id, self.nit_other_table_id)
			if section is None:
				time.sleep(0.1)	# no data.. so we wait a bit
				continue


			if (section["header"]["table_id"] == self.nit_current_table_id 
				and self.dvbtype != 'dvbc' and not nit_current_completed):
				if (section["header"]["version_number"] != nit_current_section_version or section["header"]["network_id"] != nit_current_section_network_id):
					nit_current_section_version = section["header"]["version_number"]
					nit_current_section_network_id = section["header"]["network_id"]
					nit_current_sections_read = []
					nit_current_content = []
					nit_current_sections_count = section["header"]["last_section_number"] + 1

				if section["header"]["section_number"] not in nit_current_sections_read:
					nit_current_sections_read.append(section["header"]["section_number"])
					nit_current_content += section["content"]

					if len(nit_current_sections_read) == nit_current_sections_count:
						nit_current_completed = True
					
			elif (str(section["header"]["network_id"]) == str(netid) and self.dvbtype == 'dvbc' and not nit_current_completed):
				if (section["header"]["version_number"] != nit_current_section_version or section["header"]["network_id"] != nit_current_section_network_id):
					nit_current_section_version = section["header"]["version_number"]
					nit_current_section_network_id = section["header"]["network_id"]
					nit_current_sections_read = []
					nit_current_content = []
					nit_current_sections_count = section["header"]["last_section_number"] + 1
					
				if section["header"]["section_number"] not in nit_current_sections_read:
					nit_current_sections_read.append(section["header"]["section_number"])
					nit_current_content += section["content"]
					
					if len(nit_current_sections_read) == nit_current_sections_count:
						nit_current_completed = True
						nit_other_completed = True


			elif section["header"]["table_id"] == self.nit_other_table_id and not nit_other_completed:
				if (section["header"]["version_number"] != nit_other_section_version or section["header"]["network_id"] != nit_other_section_network_id):
					nit_other_section_version = section["header"]["version_number"]
					nit_other_section_network_id = section["header"]["network_id"]
					nit_other_sections_read = []
					nit_other_content = []
					nit_other_sections_count = section["header"]["last_section_number"] + 1

				if section["header"]["section_number"] not in nit_other_sections_read:
					nit_other_sections_read.append(section["header"]["section_number"])
					nit_other_content += section["content"]

					if len(nit_other_sections_read) == nit_other_sections_count:
						nit_other_completed = True

			if nit_current_completed and nit_other_completed:
				print>>log, "[DvbScanner] Scan complete, netid: ", str(netid)
				break

		dvbreader.close(fd)

		nit_content = nit_current_content
		nit_content += nit_other_content

		transport_stream_id_list = []
		logical_channel_number_dict = {}
		logical_channel_number_dict_tmp = {}
		hd_logical_channel_number_dict_tmp = {}
		service_dict_tmp ={}
		transponders_count = 0
		for transponder in nit_content:
			if len(transponder) == 4: # service
				key = "%x:%x:%x" % (transponder["transport_stream_id"], transponder["original_network_id"], transponder["service_id"])
				service_dict_tmp[key] = transponder
				continue
			if len(transponder) == 5: # lcn
				key = "%x:%x:%x" % (transponder["transport_stream_id"], transponder["original_network_id"], transponder["service_id"])
				logical_channel_number_dict_tmp[key] = transponder
				continue
			if len(transponder) == 6: # HD lcn
				key = "%x:%x:%x" % (transponder["transport_stream_id"], transponder["original_network_id"], transponder["service_id"])
				hd_logical_channel_number_dict_tmp[key] = transponder
				continue
			transponder["services"] = {}
			transponder["dvb_type"] = self.dvbtype
			transponder["bouquet_type"] = bouquettype
			
			if transponder["dvb_type"] == 'dvbc': # DVB-C
				transponder["symbol_rate"] = transponder["symbol_rate"] * 100
				transponder["flags"] = 0
				if transponder["fec_inner"] != 15 and transponder["fec_inner"] > 9:
					transponder["fec_inner"] = 0
				transponder["frequency"] = transponder["frequency"] / 10
				transponder["namespace"] = 0xFFFF0000
				transponder["inversion"] = transponder["fec_outer"]
				transponder["modulation_system"] = 0
			elif transponder["dvb_type"] == 'dvbt': # DVB-T
				transponder["namespace"] = 0xEEEE0000
				transponder["frequency"] = transponder["frequency"] * 10
				transponder["inversion"] = 0
				transponder["plpid"] = 0
				transponder["flags"] = 0
				transponder["system"] = 0
			elif transponder["dvb_type"] == 'dvbs': # DVB-S
				transponder["symbol_rate"] = transponder["symbol_rate"] * 100
				transponder["flags"] = 0
				if transponder["fec_inner"] != 15 and transponder["fec_inner"] > 9:
					transponder["fec_inner"] = 0
				transponder["frequency"] = transponder["frequency"] * 10
				orbital_position = ((transponder["orbital_position"] >> 12) & 0x0F) * 1000
				orbital_position += ((transponder["orbital_position"] >> 8) & 0x0F) * 100
				orbital_position += ((transponder["orbital_position"] >> 4) & 0x0F) * 10
				orbital_position += transponder["orbital_position"] & 0x0F
				if orbital_position != 0 and transponder["west_east_flag"] == 0:
					orbital_position = 3600 - orbital_position
				transponder["orbital_position"] = orbital_position
				transponder["pilot"] = 2

				if transponder["modulation_system"] == 0 and transponder["modulation_type"] == 2:
					transponder["modulation_type"] = 1
				transponder["inversion"] = 2
				transponder["namespace"] = self.buildNamespace(transponder)

			key = "%x:%x:%x" % (transponder["namespace"],
				transponder["transport_stream_id"],
				transponder["original_network_id"])

			if key in transponders:
				transponder["services"] = transponders[key]["services"]
			transponders[key] = transponder
			transponders_count += 1

			if transponder["transport_stream_id"] not in transport_stream_id_list:
				transport_stream_id_list.append(transponder["transport_stream_id"])

		if read_other_section:
			print>>log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x and network_id = 0x%x" % (transponders_count, nit_current_section_network_id, nit_other_section_network_id)
		else:
			print>>log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x" % (transponders_count, nit_current_section_network_id)

		if len(hd_logical_channel_number_dict_tmp) > 0 and bouquettype == 'hd':
			for id in logical_channel_number_dict_tmp:
				if id in hd_logical_channel_number_dict_tmp:
					lcntofind = hd_logical_channel_number_dict_tmp[id]["logical_channel_number"]
					lcnreplace = logical_channel_number_dict_tmp[id]["logical_channel_number"]
					for id2 in logical_channel_number_dict_tmp:
						if logical_channel_number_dict_tmp[id2]["logical_channel_number"] == lcntofind:
							logical_channel_number_dict[id] = logical_channel_number_dict_tmp[id2]
							logical_channel_number_dict[id]["logical_channel_number"] = lcnreplace
					logical_channel_number_dict[id] = hd_logical_channel_number_dict_tmp[id]
				else:
					logical_channel_number_dict[id] = logical_channel_number_dict_tmp[id]
		else:
			for id in logical_channel_number_dict_tmp:
				logical_channel_number_dict[id] = logical_channel_number_dict_tmp[id]

		return {
			"transport_stream_id_list": transport_stream_id_list,
			"logical_channel_number_dict": logical_channel_number_dict,
			"service_dict_tmp": service_dict_tmp
		}
Beispiel #4
0
    def readNIT(self):
        adapter = 0
        demuxer_device = "/dev/dvb/adapter%d/demux%d" % (adapter,
                                                         self.demuxer_id)
        start = time.time()  # for debug info

        nit_current_pid = 0x10
        nit_current_table_id = 0x40
        nit_other_table_id = 0x00  # don't read other table

        self.network_name = None
        self.custom_transponder_needed = True

        if nit_other_table_id == 0x00:
            mask = 0xff
        else:
            mask = nit_current_table_id ^ nit_other_table_id ^ 0xff
        nit_current_timeout = 20  # maximum time allowed to read the network information table (seconds)

        nit_current_version_number = -1
        nit_current_sections_read = []
        nit_current_sections_count = 0
        nit_current_content = []
        nit_current_completed = False

        fd = dvbreader.open(demuxer_device, nit_current_pid,
                            nit_current_table_id, mask, self.selectedNIM)
        if fd < 0:
            print "[ABM-FrequencyFinder][readNIT] Cannot open the demuxer"
            return

        timeout = datetime.datetime.now()
        timeout += datetime.timedelta(0, nit_current_timeout)

        while True:
            if datetime.datetime.now() > timeout:
                print "[ABM-FrequencyFinder][readNIT] Timed out reading NIT"
                break

            section = dvbreader.read_nit(fd, nit_current_table_id,
                                         nit_other_table_id)
            if section is None:
                time.sleep(0.1)  # no data.. so we wait a bit
                continue

            if section["header"][
                    "table_id"] == nit_current_table_id and not nit_current_completed:
                if section["header"][
                        "version_number"] != nit_current_version_number:
                    nit_current_version_number = section["header"][
                        "version_number"]
                    nit_current_sections_read = []
                    nit_current_sections_count = section["header"][
                        "last_section_number"] + 1
                    nit_current_content = []

                if section["header"][
                        "section_number"] not in nit_current_sections_read:
                    nit_current_sections_read.append(
                        section["header"]["section_number"])
                    nit_current_content += section["content"]

                    if 'network_name' in section["header"] and section[
                            "header"]["network_name"] != "Unknown":
                        self.network_name = section["header"]["network_name"]

                    if len(nit_current_sections_read
                           ) == nit_current_sections_count:
                        nit_current_completed = True

            if nit_current_completed:
                break

        dvbreader.close(fd)

        if not nit_current_content:
            print "[ABM-FrequencyFinder][readNIT] current transponder not found"
            return

        print "[ABM-FrequencyFinder][readNIT] NIT read time %.1f seconds." % (
            time.time() - start)

        # descriptor_tag 0x5A is DVB-T, descriptor_tag 0x7f is DVB-T
        transponders = [
            t for t in nit_current_content
            if "descriptor_tag" in t and t["descriptor_tag"] in (
                0x5A, 0x7f) and t["original_network_id"] == self.onid
            and t["transport_stream_id"] == self.tsid
        ]  # this should only ever have a length of one transponder
        print "[ABM-FrequencyFinder][readNIT] transponders", transponders
        if transponders:

            if transponders[0]["descriptor_tag"] == 0x5A:  # DVB-T
                self.system = eDVBFrontendParametersTerrestrial.System_DVB_T
            else:  # must be DVB-T2
                self.system = eDVBFrontendParametersTerrestrial.System_DVB_T2

            if "frequency" in transponders[0] and abs(
                (transponders[0]["frequency"] * 10) -
                    self.frequency) < 1000000:
                self.custom_transponder_needed = False
                if self.frequency != transponders[0]["frequency"] * 10:
                    print "[ABM-FrequencyFinder][readNIT] updating transponder frequency from %.03f MHz to %.03f MHz" % (
                        self.frequency / 1000000,
                        transponders[0]["frequency"] / 100000)
                    self.frequency = transponders[0]["frequency"] * 10
Beispiel #5
0
    def updateTransponders(self, transponders, read_other_section=False):
        print >> log, "[DvbScanner] Reading transponders..."

        if self.nit_other_table_id == 0x00:
            mask = 0xff
        else:
            mask = self.nit_current_table_id ^ self.nit_other_table_id ^ 0xff

        fd = dvbreader.open(self.demuxer_device, self.nit_pid,
                            self.nit_current_table_id, mask, self.frontend)
        if fd < 0:
            print >> log, "[DvbScanner] Cannot open the demuxer"
            return None

        nit_current_section_version = -1
        nit_current_section_network_id = -1
        nit_current_sections_read = []
        nit_current_sections_count = 0
        nit_current_content = []
        nit_current_completed = False

        nit_other_section_version = -1
        nit_other_section_network_id = -1
        nit_other_sections_read = []
        nit_other_sections_count = 0
        nit_other_content = []
        nit_other_completed = not read_other_section or self.nit_other_table_id == 0x00

        timeout = datetime.datetime.now()
        timeout += datetime.timedelta(0, self.TIMEOUT_SEC)
        while True:
            if datetime.datetime.now() > timeout:
                print >> log, "[DvbScanner] Timed out"
                break

            section = dvbreader.read_nit(fd, self.nit_current_table_id,
                                         self.nit_other_table_id)
            if section is None:
                time.sleep(0.1)  # no data.. so we wait a bit
                continue

            if section["header"][
                    "table_id"] == self.nit_current_table_id and not nit_current_completed:
                if (section["header"]["version_number"] !=
                        nit_current_section_version
                        or section["header"]["network_id"] !=
                        nit_current_section_network_id):

                    nit_current_section_version = section["header"][
                        "version_number"]
                    nit_current_section_network_id = section["header"][
                        "network_id"]
                    nit_current_sections_read = []
                    nit_current_content = []
                    nit_current_sections_count = section["header"][
                        "last_section_number"] + 1

                if section["header"][
                        "section_number"] not in nit_current_sections_read:
                    nit_current_sections_read.append(
                        section["header"]["section_number"])
                    nit_current_content += section["content"]

                    if len(nit_current_sections_read
                           ) == nit_current_sections_count:
                        nit_current_completed = True

            elif section["header"][
                    "table_id"] == self.nit_other_table_id and not nit_other_completed:
                if (section["header"]["version_number"] !=
                        nit_other_section_version
                        or section["header"]["network_id"] !=
                        nit_other_section_network_id):

                    nit_other_section_version = section["header"][
                        "version_number"]
                    nit_other_section_network_id = section["header"][
                        "network_id"]
                    nit_other_sections_read = []
                    nit_other_content = []
                    nit_other_sections_count = section["header"][
                        "last_section_number"] + 1

                if section["header"][
                        "section_number"] not in nit_other_sections_read:
                    nit_other_sections_read.append(
                        section["header"]["section_number"])
                    nit_other_content += section["content"]

                    if len(nit_other_sections_read
                           ) == nit_other_sections_count:
                        nit_other_completed = True

            if nit_current_completed and nit_other_completed:
                break

        dvbreader.close(fd)

        nit_content = nit_current_content
        nit_content += nit_other_content

        transport_stream_id_list = []
        logical_channel_number_dict = {}
        transponders_count = 0
        for transponder in nit_content:
            if len(transponder) == 5:  # lcn
                key = "%x:%x:%x" % (transponder["transport_stream_id"],
                                    transponder["original_network_id"],
                                    transponder["service_id"])
                logical_channel_number_dict[key] = transponder
                continue
            transponder["services"] = {}
            transponder["dvb_type"] = "s"
            transponder["frequency"] = transponder["frequency"] * 10
            transponder["symbol_rate"] = transponder["symbol_rate"] * 100
            if transponder["fec_inner"] != 15 and transponder["fec_inner"] > 9:
                transponder["fec_inner"] = 0

            orbital_position = (
                (transponder["orbital_position"] >> 12) & 0x0F) * 1000
            orbital_position += (
                (transponder["orbital_position"] >> 8) & 0x0F) * 100
            orbital_position += (
                (transponder["orbital_position"] >> 4) & 0x0F) * 10
            orbital_position += transponder["orbital_position"] & 0x0F
            if orbital_position != 0 and transponder["west_east_flag"] == 0:
                orbital_position = 3600 - orbital_position
            transponder["orbital_position"] = orbital_position

            if transponder["modulation_system"] == 0 and transponder[
                    "modulation_type"] == 2:
                transponder["modulation_type"] = 1

            transponder["namespace"] = self.buildNamespace(transponder)
            transponder["inversion"] = 2
            transponder["flags"] = 0
            transponder["pilot"] = 2

            key = "%x:%x:%x" % (transponder["namespace"],
                                transponder["transport_stream_id"],
                                transponder["original_network_id"])

            if key in transponders:
                transponder["services"] = transponders[key]["services"]
            transponders[key] = transponder
            transponders_count += 1

            if transponder[
                    "transport_stream_id"] not in transport_stream_id_list:
                transport_stream_id_list.append(
                    transponder["transport_stream_id"])

        if read_other_section:
            print >> log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x and network_id = 0x%x" % (
                transponders_count, nit_current_section_network_id,
                nit_other_section_network_id)
        else:
            print >> log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x" % (
                transponders_count, nit_current_section_network_id)

        return {
            "transport_stream_id_list": transport_stream_id_list,
            "logical_channel_number_dict": logical_channel_number_dict
        }
Beispiel #6
0
    def readNIT(self):
        adapter = 0
        demuxer_device = "/dev/dvb/adapter%d/demux%d" % (adapter,
                                                         self.demuxer_id)

        nit_current_pid = 0x10
        nit_current_table_id = 0x40
        nit_other_table_id = 0x00  # don't read other table
        if nit_other_table_id == 0x00:
            mask = 0xff
        else:
            mask = nit_current_table_id ^ nit_other_table_id ^ 0xff
        nit_current_timeout = 20  # maximum time allowed to read the network information table (seconds)

        nit_current_version_number = -1
        nit_current_sections_read = []
        nit_current_sections_count = 0
        nit_current_content = []
        nit_current_completed = False

        fd = dvbreader.open(demuxer_device, nit_current_pid,
                            nit_current_table_id, mask, self.selectedNIM)
        if fd < 0:
            print "[MakeBouquet][readNIT] Cannot open the demuxer"
            return

        timeout = datetime.datetime.now()
        timeout += datetime.timedelta(0, nit_current_timeout)

        while True:
            if datetime.datetime.now() > timeout:
                print "[MakeBouquet][readNIT] Timed out reading NIT"
                break

            section = dvbreader.read_nit(fd, nit_current_table_id,
                                         nit_other_table_id)
            if section is None:
                time.sleep(0.1)  # no data.. so we wait a bit
                continue

            if section["header"][
                    "table_id"] == nit_current_table_id and not nit_current_completed:
                if section["header"][
                        "version_number"] != nit_current_version_number:
                    nit_current_version_number = section["header"][
                        "version_number"]
                    nit_current_sections_read = []
                    nit_current_sections_count = section["header"][
                        "last_section_number"] + 1
                    nit_current_content = []

                if section["header"][
                        "section_number"] not in nit_current_sections_read:
                    nit_current_sections_read.append(
                        section["header"]["section_number"])
                    nit_current_content += section["content"]

                    if len(nit_current_sections_read
                           ) == nit_current_sections_count:
                        nit_current_completed = True

            if nit_current_completed:
                break

        dvbreader.close(fd)

        if not nit_current_content:
            print "[MakeBouquet][readNIT] current transponder not found"
            return

        LCNs = [
            t for t in nit_current_content
            if "descriptor_tag" in t and t["descriptor_tag"] == 0x83
            and t["original_network_id"] in PROVIDERS[
                config.plugins.MisPlsLcnScan.provider.value]["onids"]
        ]
        print "[MakeBouquet][readNIT] LCNs", LCNs
        if LCNs:
            for LCN in LCNs:
                LCNkey = "%x:%x:%x" % (LCN["transport_stream_id"],
                                       LCN["original_network_id"],
                                       LCN["service_id"])

                if not self.ignore_visible_service_flag and "visible_service_flag" in LCN and LCN[
                        "visible_service_flag"] == 0:
                    continue

                # Only write to the dict if there is no entry, or override the entry if the data comes from the same transponder the channel is located on.
                if LCNkey not in self.logical_channel_number_dict or LCN[
                        "transport_stream_id"] == self.tsid:
                    self.logical_channel_number_dict[LCNkey] = LCN

        namespace = self.transpondercurrent.orbital_position << 16
        if self.namespace_complete:
            namespace |= (
                (self.transpondercurrent.frequency / 1000) & 0xFFFF) | (
                    (self.transpondercurrent.polarisation & 1) << 15)
        namespacekey = "%x:%x" % (self.tsid, self.onid)
        self.namespace_dict[namespacekey] = namespace
Beispiel #7
0
    def readNIT(self):
        adapter = 0
        demuxer_device = "/dev/dvb/adapter%d/demux%d" % (adapter,
                                                         self.demuxer_id)

        nit_current_pid = 0x10
        nit_current_table_id = 0x40
        nit_other_table_id = 0x00  # don't read other table
        if nit_other_table_id == 0x00:
            mask = 0xff
        else:
            mask = nit_current_table_id ^ nit_other_table_id ^ 0xff
        nit_current_timeout = 20  # maximum time allowed to read the network information table (seconds)

        nit_current_version_number = -1
        nit_current_sections_read = []
        nit_current_sections_count = 0
        nit_current_content = []
        nit_current_completed = False

        fd = dvbreader.open(demuxer_device, nit_current_pid,
                            nit_current_table_id, mask, self.selectedNIM)
        if fd < 0:
            print("[MakeBouquet][readNIT] Cannot open the demuxer")
            return

        timeout = datetime.datetime.now()
        timeout += datetime.timedelta(0, nit_current_timeout)

        while True:
            if datetime.datetime.now() > timeout:
                print("[MakeBouquet][readNIT] Timed out reading NIT")
                break

            section = dvbreader.read_nit(fd, nit_current_table_id,
                                         nit_other_table_id)
            if section is None:
                time.sleep(0.1)  # no data.. so we wait a bit
                continue

            if section["header"][
                    "table_id"] == nit_current_table_id and not nit_current_completed:
                if section["header"][
                        "version_number"] != nit_current_version_number:
                    nit_current_version_number = section["header"][
                        "version_number"]
                    nit_current_sections_read = []
                    nit_current_sections_count = section["header"][
                        "last_section_number"] + 1
                    nit_current_content = []

                if section["header"][
                        "section_number"] not in nit_current_sections_read:
                    nit_current_sections_read.append(
                        section["header"]["section_number"])
                    nit_current_content += section["content"]

                    if len(nit_current_sections_read
                           ) == nit_current_sections_count:
                        nit_current_completed = True

            if nit_current_completed:
                break

        dvbreader.close(fd)

        if not nit_current_content:
            print("[MakeBouquet][readNIT] current transponder not found")
            return

        # descriptor_tag 0x5A is DVB-T, descriptor_tag 0x7f is DVB-T
        transponders = [
            t for t in nit_current_content
            if "descriptor_tag" in t and t["descriptor_tag"] in (0x5A, 0x7f)
            and t["original_network_id"] == self.transponder["onid"]
            and t["transport_stream_id"] == self.transponder["tsid"]
        ]  # this should only ever have a length of one transponder
        print("[MakeBouquet][readNIT] transponders", transponders)
        if transponders:

            if transponders[0]["descriptor_tag"] == 0x5A:  # DVB-T
                self.transponder[
                    "system"] = eDVBFrontendParametersTerrestrial.System_DVB_T
            else:  # must be DVB-T2
                self.transponder[
                    "system"] = eDVBFrontendParametersTerrestrial.System_DVB_T2

            if "frequency" in transponders[0] and abs(
                (transponders[0]["frequency"] * 10) - self.
                    transponder["frequency"]) < 1000000 and self.transponder[
                        "frequency"] != transponders[0]["frequency"] * 10:
                print(
                    "[MakeBouquet][readNIT] updating transponder frequency from %.03f MHz to %.03f MHz"
                    % (self.transponder["frequency"] // 1000000,
                       transponders[0]["frequency"] // 100000))
                self.transponder[
                    "frequency"] = transponders[0]["frequency"] * 10

        # LCNs = [t for t in nit_current_content if "descriptor_tag" in t and t["descriptor_tag"] == self.lcndescriptor and t["original_network_id"] == self.transponder["onid"]]
        LCNs = [
            t for t in nit_current_content if "descriptor_tag" in t
            and t["descriptor_tag"] == self.lcndescriptor and (
                self.lcndescriptor == 0x83 or (self.lcndescriptor == 0x87 and (
                    "channel_list_id" in t and t["channel_list_id"] ==
                    self.channel_list_id or self.channel_list_id == 0)))
            and t["original_network_id"] == self.transponder["onid"]
        ]

        print("[MakeBouquet][readNIT] LCNs", LCNs)
        if LCNs:
            for LCN in LCNs:
                LCNkey = "%x:%x:%x" % (LCN["transport_stream_id"],
                                       LCN["original_network_id"],
                                       LCN["service_id"])

                if not self.ignore_visible_service_flag and "visible_service_flag" in LCN and LCN[
                        "visible_service_flag"] == 0:
                    continue

                # Only write to the dict if there is no entry, or override the entry if the data comes from the same transponder the channel is located on.
                if LCNkey not in self.logical_channel_number_dict or LCN[
                        "transport_stream_id"] == self.transponder["tsid"]:
                    self.logical_channel_number_dict[LCNkey] = LCN

        namespace = 0xEEEE0000
        if self.namespace_complete_terrestrial:
            namespace |= (self.transponder['frequency'] // 1000000) & 0xFFFF
        namespacekey = "%x:%x" % (self.transponder["tsid"],
                                  self.transponder["onid"])
        self.namespace_dict[namespacekey] = namespace
	def updateTransponders(self, transponders, read_other_section = False):
		print>>log, "[DvbScanner] Reading transponders..."

		if self.nit_other_table_id == 0x00:
			mask = 0xff
		else:
			mask = self.nit_current_table_id ^ self.nit_other_table_id ^ 0xff

		fd = dvbreader.open(self.demuxer_device, self.nit_pid, self.nit_current_table_id, mask, self.frontend)
		if fd < 0:
			print>>log, "[DvbScanner] Cannot open the demuxer"
			return None

		nit_current_section_version = -1
		nit_current_section_network_id = -1
		nit_current_sections_read = []
		nit_current_sections_count = 0
		nit_current_content = []
		nit_current_completed = False

		nit_other_section_version = -1
		nit_other_section_network_id = -1
		nit_other_sections_read = []
		nit_other_sections_count = 0
		nit_other_content = []
		nit_other_completed = not read_other_section or self.nit_other_table_id == 0x00

		timeout = datetime.datetime.now()
		timeout += datetime.timedelta(0, self.TIMEOUT_SEC)
		while True:
			if datetime.datetime.now() > timeout:
				print>>log, "[DvbScanner] Timed out"
				break

			section = dvbreader.read_nit(fd, self.nit_current_table_id, self.nit_other_table_id)
			if section is None:
				time.sleep(0.1)	# no data.. so we wait a bit
				continue

			if section["header"]["table_id"] == self.nit_current_table_id and not nit_current_completed:
				if (section["header"]["version_number"] != nit_current_section_version
					or section["header"]["network_id"] != nit_current_section_network_id):

					nit_current_section_version = section["header"]["version_number"]
					nit_current_section_network_id = section["header"]["network_id"]
					nit_current_sections_read = []
					nit_current_content = []
					nit_current_sections_count = section["header"]["last_section_number"] + 1

				if section["header"]["section_number"] not in nit_current_sections_read:
					nit_current_sections_read.append(section["header"]["section_number"])
					nit_current_content += section["content"]

					if len(nit_current_sections_read) == nit_current_sections_count:
						nit_current_completed = True

			elif section["header"]["table_id"] == self.nit_other_table_id and not nit_other_completed:
				if (section["header"]["version_number"] != nit_other_section_version
					or section["header"]["network_id"] != nit_other_section_network_id):

					nit_other_section_version = section["header"]["version_number"]
					nit_other_section_network_id = section["header"]["network_id"]
					nit_other_sections_read = []
					nit_other_content = []
					nit_other_sections_count = section["header"]["last_section_number"] + 1

				if section["header"]["section_number"] not in nit_other_sections_read:
					nit_other_sections_read.append(section["header"]["section_number"])
					nit_other_content += section["content"]

					if len(nit_other_sections_read) == nit_other_sections_count:
						nit_other_completed = True

			if nit_current_completed and nit_other_completed:
				break

		dvbreader.close(fd)

		nit_content = nit_current_content
		nit_content += nit_other_content

		transport_stream_id_list = []
		logical_channel_number_dict = {}
		transponders_count = 0
		for transponder in nit_content:
			if len(transponder) == 5: # lcn
				key = "%x:%x:%x" % (transponder["transport_stream_id"], transponder["original_network_id"], transponder["service_id"])
				logical_channel_number_dict[key] = transponder
				continue
			transponder["services"] = {}
			transponder["dvb_type"] = "s"
			transponder["frequency"] = transponder["frequency"] * 10
			transponder["symbol_rate"] = transponder["symbol_rate"] * 100
			if transponder["fec_inner"] != 15 and transponder["fec_inner"] > 9:
				transponder["fec_inner"] = 0

			orbital_position = ((transponder["orbital_position"] >> 12) & 0x0F) * 1000
			orbital_position += ((transponder["orbital_position"] >> 8) & 0x0F) * 100
			orbital_position += ((transponder["orbital_position"] >> 4) & 0x0F) * 10
			orbital_position += transponder["orbital_position"] & 0x0F
			if orbital_position != 0 and transponder["west_east_flag"] == 0:
				orbital_position = 3600 - orbital_position
			transponder["orbital_position"] = orbital_position

			if transponder["modulation_system"] == 0 and transponder["modulation_type"] == 2:
				transponder["modulation_type"] = 1

			transponder["namespace"] = self.buildNamespace(transponder)
			transponder["inversion"] = 2
			transponder["flags"] = 0
			transponder["pilot"] = 2

			key = "%x:%x:%x" % (transponder["namespace"],
				transponder["transport_stream_id"],
				transponder["original_network_id"])

			if key in transponders:
				transponder["services"] = transponders[key]["services"]
			transponders[key] = transponder
			transponders_count += 1

			if transponder["transport_stream_id"] not in transport_stream_id_list:
				transport_stream_id_list.append(transponder["transport_stream_id"])

		if read_other_section:
			print>>log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x and network_id = 0x%x" % (transponders_count, nit_current_section_network_id, nit_other_section_network_id)
		else:
			print>>log, "[DvbScanner] Added/Updated %d transponders with network_id = 0x%x" % (transponders_count, nit_current_section_network_id)

		return {
			"transport_stream_id_list": transport_stream_id_list,
			"logical_channel_number_dict": logical_channel_number_dict
		}
	def readNIT(self):
		adapter = 0
		demuxer_device = "/dev/dvb/adapter%d/demux%d" % (adapter, self.demuxer_id)
		start = time.time() # for debug info

		nit_current_pid = 0x10
		nit_current_table_id = 0x40
		nit_other_table_id = 0x00 # don't read other table

		self.network_name = None
		self.custom_transponder_needed = True

		if nit_other_table_id == 0x00:
			mask = 0xff
		else:
			mask = nit_current_table_id ^ nit_other_table_id ^ 0xff
		nit_current_timeout = 20 # maximum time allowed to read the network information table (seconds)

		nit_current_version_number = -1
		nit_current_sections_read = []
		nit_current_sections_count = 0
		nit_current_content = []
		nit_current_completed = False

		fd = dvbreader.open(demuxer_device, nit_current_pid, nit_current_table_id, mask, self.selectedNIM)
		if fd < 0:
			print "[ABM-FrequencyFinder][readNIT] Cannot open the demuxer"
			return

		timeout = datetime.datetime.now()
		timeout += datetime.timedelta(0, nit_current_timeout)

		while True:
			if datetime.datetime.now() > timeout:
				print "[ABM-FrequencyFinder][readNIT] Timed out reading NIT"
				break

			section = dvbreader.read_nit(fd, nit_current_table_id, nit_other_table_id)
			if section is None:
				time.sleep(0.1)	# no data.. so we wait a bit
				continue

			if section["header"]["table_id"] == nit_current_table_id and not nit_current_completed:
				if section["header"]["version_number"] != nit_current_version_number:
					nit_current_version_number = section["header"]["version_number"]
					nit_current_sections_read = []
					nit_current_sections_count = section["header"]["last_section_number"] + 1
					nit_current_content = []

				if section["header"]["section_number"] not in nit_current_sections_read:
					nit_current_sections_read.append(section["header"]["section_number"])
					nit_current_content += section["content"]

					if 'network_name' in section["header"] and section["header"]["network_name"] != "Unknown":
						self.network_name = section["header"]["network_name"]

					if len(nit_current_sections_read) == nit_current_sections_count:
						nit_current_completed = True

			if nit_current_completed:
				break

		dvbreader.close(fd)

		if not nit_current_content:
			print "[ABM-FrequencyFinder][readNIT] current transponder not found"
			return

		print "[ABM-FrequencyFinder][readNIT] NIT read time %.1f seconds." % (time.time() - start)

		# descriptor_tag 0x5A is DVB-T, descriptor_tag 0x7f is DVB-T
		transponders = [t for t in nit_current_content if "descriptor_tag" in t and t["descriptor_tag"] in (0x5A, 0x7f) and t["original_network_id"] == self.onid and t["transport_stream_id"] == self.tsid] # this should only ever have a length of one transponder
		print "[ABM-FrequencyFinder][readNIT] transponders", transponders
		if transponders:

			if transponders[0]["descriptor_tag"] == 0x5A: # DVB-T
				self.system = eDVBFrontendParametersTerrestrial.System_DVB_T
			else: # must be DVB-T2
				self.system = eDVBFrontendParametersTerrestrial.System_DVB_T2

			if "frequency" in transponders[0] and abs((transponders[0]["frequency"]*10) - self.frequency) < 1000000:
				self.custom_transponder_needed = False
				if self.frequency != transponders[0]["frequency"]*10:
					print "[ABM-FrequencyFinder][readNIT] updating transponder frequency from %.03f MHz to %.03f MHz" % (self.frequency/1000000, transponders[0]["frequency"]/100000)
					self.frequency = transponders[0]["frequency"]*10