| 10 | |
| 11 | |
| 12 | async def main(): |
| 13 | print("DEBUG: Starting client...") |
| 14 | reader, writer = await asyncio.open_connection("127.0.0.1", 9999) |
| 15 | print("DEBUG: Connected to server") |
| 16 | |
| 17 | pc = RTCPeerConnection() |
| 18 | print("DEBUG: Peer connection created") |
| 19 | |
| 20 | video_track_received = asyncio.Event() |
| 21 | video_task = None |
| 22 | |
| 23 | @pc.on("track") |
| 24 | async def on_track(track): |
| 25 | nonlocal video_task |
| 26 | print(f"DEBUG: Track callback triggered, kind={track.kind}") |
| 27 | logging.info("Track received: kind=%s", track.kind) |
| 28 | if track.kind != "video": |
| 29 | print("DEBUG: Track is not video, ignoring") |
| 30 | return |
| 31 | |
| 32 | print("DEBUG: Setting video_track_received event") |
| 33 | video_track_received.set() |
| 34 | |
| 35 | print("DEBUG: Starting frame receiving loop") |
| 36 | while True: |
| 37 | try: |
| 38 | frame = await track.recv() |
| 39 | print("DEBUG: Frame received") |
| 40 | img = frame.to_ndarray(format="bgr24") |
| 41 | |
| 42 | cv2.imshow("Remote camera", img) |
| 43 | # break on 'q' |
| 44 | if cv2.waitKey(1) & 0xFF == ord("q"): |
| 45 | print("DEBUG: 'q' key pressed, exiting") |
| 46 | break |
| 47 | except Exception as e: |
| 48 | logging.error("Error receiving frame: %s", e) |
| 49 | break |
| 50 | |
| 51 | logging.info("Stopping video display") |
| 52 | cv2.destroyAllWindows() |
| 53 | await pc.close() |
| 54 | |
| 55 | @pc.on("iceconnectionstatechange") |
| 56 | async def on_ice_state_change(): |
| 57 | print(f"DEBUG: ICE state changed to: {pc.iceConnectionState}") |
| 58 | logging.info("ICE state: %s", pc.iceConnectionState) |
| 59 | if pc.iceConnectionState in ("failed", "closed"): |
| 60 | await pc.close() |
| 61 | writer.close() |
| 62 | |
| 63 | # Read offer from server |
| 64 | print("DEBUG: Waiting for offer from server...") |
| 65 | line = await reader.readline() |
| 66 | print(f"DEBUG: Received line: {line[:100] if line else 'None'}...") |
| 67 | if not line: |
| 68 | logging.error("No offer received") |
| 69 | return |