Exemplo n.º 1
0
    def __create(self):
        with self.__class__.session(self.__init_session()) as s:
            rc = C.ipset_data_set(C.ipset_session_data(s), C.IPSET_SETNAME,
                                  self._name)
            assert rc == 0

            C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_TYPENAME,
                             self._type)

            t = C.ipset_type_get(s, C.IPSET_CMD_CREATE)
            C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_TYPE, t)

            if self._exist:
                C.ipset_envopt_parse(s, C.IPSET_ENV_EXIST, ffi.NULL)

            family_ptr = None
            if self._family == 'inet':
                family_ptr = ffi.new("int *", C.NFPROTO_IPV4)
            elif self._family == 'inet6':
                family_ptr = ffi.new("int *", C.NFPROTO_IPV6)

            C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_FAMILY,
                             family_ptr)

            if self._netmask is not None:
                mask = ffi.new("struct in_addr *")
                mask.s_addr = int(self._netmask)
                C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_NETMASK,
                                 mask)

            rc = C.ipset_cmd(s, C.IPSET_CMD_CREATE, 0)
            assert rc == 0
Exemplo n.º 2
0
    def read(self):
        # self note: only one of video frame or audio frame gets read. Gotta check with one
        pNDI_recv = self.pNDI_recv

        video_frame = ffi.new("NDIlib_video_frame_v2_t*")

        # Loop until we get data
        while True:
            res_val = lib.NDIlib_recv_capture_v2(pNDI_recv, video_frame,
                                                 ffi.NULL, ffi.NULL, 1000)

            if res_val == FrameType.type_video:
                width = video_frame.xres
                height = video_frame.yres

                # Note: this should always be 4 * xres
                bytes_per_row = video_frame.line_stride_in_bytes
                total_bytes = bytes_per_row * height

                byte_data = np.frombuffer(
                    ffi.buffer(video_frame.p_data, total_bytes))
                new_data = np.ndarray((height, width, 4),
                                      dtype=np.uint8,
                                      buffer=byte_data)
                new_data = new_data.copy()  # prevent seg-fault

                lib.NDIlib_recv_free_video_v2(pNDI_recv, video_frame)

                return new_data
Exemplo n.º 3
0
    def get_sources(self, wait_ms=50) -> typing.Iterable[NDISource]:
        "Returns all sources that can be accessed on the current network"
        changed = lib.NDIlib_find_wait_for_sources(self.pNDI_find, wait_ms)
        if not changed:
            return self.current_sources

        # Changed, get new sources
        # num_sources = c_int()

        p_nsources = ffi.new("uint32_t *")
        sources = lib.NDIlib_find_get_current_sources(self.pNDI_find,
                                                      p_nsources)

        num_sources = p_nsources[0]
        #print(len(num_sources))
        # Update sources (create a new list so that the caller's == checks work)
        self.current_sources = []
        for i in range(num_sources):
            source = sources[i]
            self.current_sources.append(
                NDISource(raw=source,
                          name=ffi.string(source.p_ndi_name).decode('utf-8'),
                          address=ffi.string(
                              source.p_url_address).decode('utf-8')))

        return self.current_sources
Exemplo n.º 4
0
    def add(self, item):
        if self._type == 'hash:ip':
            ip = None
            cidr = None
            ip_net = ipaddress.ip_network(item)
            ip = ffi.new("union nf_inet_addr *")
            af = None
            if isinstance(ip_net, ipaddress.IPv4Network):
                af = C.AF_INET
            elif isinstance(ip_net, ipaddress.IPv6Network):
                af = C.AF_INET6
            rc = C.inet_pton(af, str(ip_net.network_address), ip)
            assert rc == 1
            cidr = int(ip_net.prefixlen)

            with self.__class__.session(self.__init_session()) as s:
                rc = C.ipset_data_set(C.ipset_session_data(s), C.IPSET_SETNAME,
                                      self._name)
                assert rc == 0

                t = C.ipset_type_get(s, C.IPSET_CMD_ADD)
                C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_TYPE, t)

                if self._exist:
                    C.ipset_envopt_parse(s, C.IPSET_ENV_EXIST, ffi.NULL)

                C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_IP, ip)

                C.ipset_data_set(C.ipset_session_data(s), C.IPSET_OPT_CIDR,
                                 ffi.new("uint8_t *", cidr))

                rc = C.ipset_cmd(s, C.IPSET_CMD_ADD, 0)
                assert rc == 0

        else:
            raise NotImplementedError(
                'Adding to {t} not implemented yet'.format(t=self._type))
        return
Exemplo n.º 5
0
def create_receiver(source: NDISource,
                    *,
                    color_format=ColorFormat.format_BGRX_BGRA,
                    bandwidth=RecvBandwith.highest):
    create_config = ffi.new("NDIlib_recv_create_v3_t *")
    create_config.source_to_connect_to = source.raw
    create_config.color_format = color_format
    create_config.bandwidth = bandwidth
    create_config.allow_video_fields = True
    create_config.p_ndi_recv_name = ffi.NULL

    pNDI_recv = lib.NDIlib_recv_create_v3(create_config)
    receiver = NDIReceiver(pNDI_recv, source)
    return receiver