0

I need help I have attempted to implement the following code into my current project: Screen sharing in python However the client returns ValueError:String length does not equal format and resolution size. Everything seems to be working except for the image from pixels

Clarification: VidStreamBroadCast is the Server, VidStreamReceive is the client. In the client class pygame.display.flip() is generating errors, with the apparent origin in the img = pygame.image.fromstring

class VidStreamBroadCast:

    def __init__(self, conn, width=800, height=600, public="public.pem", private="private.pem"):
        self.__width = width
        self.__height = height
        self.__ViralRSA = ViralRSA.ViralRSA(public, private)
        self.__conn = conn
        thread = Thread(target=self.__retreive_screenshot, args=(self.__conn,))
        thread.start()

    def __retreive_screenshot(self, conn):
        with mss() as sct:
            # The region to capture
            rect = {'top': 0, 'left': 0, 'width': self.__width, 'height': self.__height}

            while 'recording':
                # Capture the screen
                img = sct.grab(rect)
                # Tweak the compression level here (0-9)
                pixels = compress(img.rgb, 6)

                # Send the size of the pixels length
                size = len(pixels)
                size_len = (size.bit_length() + 7) // 8
                conn.send(bytes([size_len]))

                # Send the actual pixels length
                size_bytes = size.to_bytes(size_len, 'big')
                conn.send(size_bytes)

                # Send pixels
                print(pixels)
                conn.sendall(pixels)

class VidStreamReceive:

    def __init__(self, conn, width=800, height=600, public="public.pem", private="private.pem"):
        self.__conn = conn
        self.__width = width
        self.__height = height
        self.__ViralRSA = ViralRSA.ViralRSA(public, private)
        self.__initiate_window()
        self.__watching = True

    def __receive_all(self, length):
        """ Retreive all pixels. """

        buf = b''
        while len(buf) < length:
            data = self.__conn.recv(length - len(buf))
            if not data:
                return data
            buf += data
        return buf

    def __initiate_window(self):
        pygame.init()
        screen = pygame.display.set_mode((self.__width, self.__height))
        clock = pygame.time.Clock()
        watching = True

        while watching:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    watching = False
                    self.__watching = True
                    break

            # Retreive the size of the pixels length, the pixels length and pixels
            size_len = int.from_bytes(self.__conn.recv(1), byteorder='big')
            size = int.from_bytes(self.__conn.recv(size_len), byteorder='big')
            pixels = decompress(self.__receive_all(size))
            print(pixels)

            # Create the Surface from raw pixels
            img = pygame.image.fromstring(pixels, (self.__width, self.__height), 'RGB')
            # Display the picture
            screen.blit(img, (0, 0))
            pygame.display.flip()
            clock.tick(60)

    def get_watching(self):
        return self.__watching

Note: I am aware of the ViralRSA Class, which currently does nothing it is intended for future RSA encryption. Also in case that is important the classes are called over a TLS_v1.2 SSL Socket. Additionally all surrounding socket structures seem to be working.

Edit cause apparently pygame is pyjama

AwesomeDude091
  • 71
  • 1
  • 12

0 Answers0