forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcaster_udp.py
More file actions
47 lines (41 loc) · 1.44 KB
/
Copy pathbroadcaster_udp.py
File metadata and controls
47 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import socket
import time
from . import capture, encoder
from utils import config
def main():
def main():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
target = ('<broadcast>', config.DEFAULT_PORT_UDP)
print(f"[UDP] Broadcasting on {target}")
except OSError as e:
print(f"[ERROR] Failed to create UDP socket: {e}")
return
# … rest of your broadcasting loop …
# Optionally start virtual display for headless
display = capture.start_virtual_display(config.FRAME_WIDTH, config.FRAME_HEIGHT)
try:
while True:
try:
img = capture.capture_screen()
frame = encoder.encode_jpeg(img)
data = encoder.compress_data(frame)
except Exception as e:
print(f"[ERROR] Failed to capture/encode frame: {e}")
time.sleep(1 / config.FRAME_RATE)
continue
# UDP max size
if len(data) > config.UDP_BUFFER_SIZE:
print(f"[WARN] Frame too large for UDP: {len(data)} bytes")
continue
sock.sendto(data, target)
time.sleep(1 / config.FRAME_RATE)
except KeyboardInterrupt:
print("[UDP] Broadcast stopped.")
finally:
if display:
display.stop()
sock.close()
if __name__ == '__main__':
main()