Client for communicating with the VSI video server using Python's multiprocessing connection. Provides methods to configure the stream, send/receive frames, and control the server.
| 37 | |
| 38 | |
| 39 | class VideoClient: |
| 40 | """ |
| 41 | Client for communicating with the VSI video server using Python's multiprocessing connection. |
| 42 | Provides methods to configure the stream, send/receive frames, and control the server. |
| 43 | """ |
| 44 | def __init__(self): |
| 45 | # Server command codes |
| 46 | self.SET_MODE = 1 |
| 47 | self.SET_DEVICE = 2 |
| 48 | self.SET_FILENAME = 3 |
| 49 | self.STREAM_CONFIGURE = 4 |
| 50 | self.STREAM_ENABLE = 5 |
| 51 | self.STREAM_DISABLE = 6 |
| 52 | self.FRAME_READ = 7 |
| 53 | self.FRAME_WRITE = 8 |
| 54 | self.CLOSE_SERVER = 9 |
| 55 | # Color space codes |
| 56 | self.GRAYSCALE8 = 0 |
| 57 | self.RGB888 = 1 |
| 58 | self.BGR565 = 2 |
| 59 | self.YUV420 = 3 |
| 60 | self.NV12 = 4 |
| 61 | self.NV21 = 5 |
| 62 | # Connection object |
| 63 | self.conn = None |
| 64 | |
| 65 | def connectToServer(self, address, authkey): |
| 66 | """ |
| 67 | Attempt to connect to the VSI video server at the given address with the provided authkey. |
| 68 | |
| 69 | Args: |
| 70 | address: The (IP, port) tuple for the server to connect to. |
| 71 | authkey: The authorization key for server connection. |
| 72 | Returns: |
| 73 | None |
| 74 | """ |
| 75 | for _ in range(40): |
| 76 | try: |
| 77 | self.conn = Client(address, authkey=authkey.encode('utf-8')) |
| 78 | if isinstance(self.conn, Connection): |
| 79 | break |
| 80 | else: |
| 81 | self.conn = None |
| 82 | except Exception: |
| 83 | self.conn = None |
| 84 | time.sleep(0.05) |
| 85 | |
| 86 | def setMode(self, mode): |
| 87 | """ |
| 88 | Set the mode of the video stream (input/output). |
| 89 | Args: |
| 90 | mode: 0 for input, 1 for output. |
| 91 | Returns: |
| 92 | Current mode value (0=input, 1=output). |
| 93 | """ |
| 94 | self.conn.send([self.SET_MODE, mode]) |
| 95 | current_mode = self.conn.recv() |
| 96 |