Python aiortc what do I do after creating an offer and receiving an answer

316 Views Asked by At

I'm trying to create an application with a client and server which are both written in python where the server creates a RTCPeerConnection and TCPSocketSignal on localhost and sends it to the client. The client then send back an answer after receiving the offer. Now, I want to set up a channel of communication (I think data channel) to send over frames from the server to the client and have the client send over other messages back to the server. I'm pretty confused on both the client and server sides on what to do after responding to the offer with an answer.

In server.py this is what I'm doing.

async def create_offer(frame_obj):
    pc = RTCPeerConnection()
    pc.addTrack(frame_obj)
    offer = await pc.createOffer()
    await pc.setLocalDescription(offer)
    return pc

offer = await create_offer(ball)
signal = TcpSocketSignaling("localhost", 4000)
await signal.send(offer.localDescription)
answer = await signal.receive()
channel = conn.createDataChannel("messages")
channel._setReadyState("open")
obj = await signal.receive()
if isinstance(obj, RTCSessionDescription):
    await offer.setRemoteDescription(obj)
    await offer.setLocalDescription(await offer.createAnswer())
    await signal.send(offer.localDescription)
elif isinstance(obj, RTCIceCandidate):
    print(obj)
    offer.addIceCandidate(obj)

This is what I'm doing in client.py currently and I'm not sure how to (create if needed) and send a RTCIceCandidate to the server on a dataChannel so that I can start sending the information/frames that I need to send. Is this not the approach I should be taking?

async def create_answer(offer):
    pc = RTCPeerConnection()
    await pc.setRemoteDescription(offer)
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)
    return pc

signal = TcpSocketSignaling("localhost", 4000)
offer = await signal.receive()
answer = await create_answer(offer)
await signal.send(answer.localDescription)

// Unsure what to do from here on
await signal.connect()
channel = conn.createDataChannel("messages")
channel._setReadyState("open")
# ice_candidate = RTCIceCandidate(component="rtp",foundation="unique",ip="127.0.0.1",port=4000,protocol="tcp",priority=50,type="relay")
# await conn.addIceCandidate(ice_candidate)
ice_candidates = conn._RTCPeerConnection__iceTransports
for value in ice_candidates:
    ice_candidate = value
    await channel.send(ice_candidate)

0

There are 0 best solutions below