File size: 2,230 Bytes
0258819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
# server.py (using FastAPI and aiortc)
import asyncio
import json
import uuid

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from aiortc import RTCPeerConnection, RTCSessionDescription, RTCIceCandidate

app = FastAPI()
connections = {}

@app.websocket("/ws/{room_id}/{client_id}")
async def websocket_endpoint(websocket: WebSocket, room_id: str, client_id: str):
    await websocket.accept()
    pc = RTCPeerConnection()
    connections[client_id] = pc

    @pc.on("track")
    def on_track(track):
        print("Track received")

    try:
        while True:
            message = await websocket.receive_text()
            data = json.loads(message)
            if data["type"] == "offer":
                offer = RTCSessionDescription(sdp=data["sdp"], type=data["type"])
                await pc.setRemoteDescription(offer)
                answer = await pc.createAnswer()
                await pc.setLocalDescription(answer)
                await websocket.send_text(json.dumps({"type": "answer", "sdp": pc.localDescription.sdp}))
            elif data["type"] == "answer":
                answer = RTCSessionDescription(sdp=data["sdp"], type=data["type"])
                await pc.setRemoteDescription(answer)
            elif data["type"] == "candidate":
                candidate = RTCIceCandidate(
                    component=data["candidate"]["component"],
                    foundation=data["candidate"]["foundation"],
                    ip=data["candidate"]["ip"],
                    port=data["candidate"]["port"],
                    priority=data["candidate"]["priority"],
                    protocol=data["candidate"]["protocol"],
                    type=data["candidate"]["type"],
                    sdpMid=data["candidate"]["sdpMid"],
                    sdpMLineIndex=data["candidate"]["sdpMLineIndex"],
                    usernameFragment=data["candidate"]["usernameFragment"],
                )
                await pc.addIceCandidate(candidate)
            else:
                print(f"Received unknown message: {data}")

    except WebSocketDisconnect:
        await pc.close()
        del connections[client_id]
        print(f"Client {client_id} disconnected from room {room_id}")