I'm trying to perform signaling using python websocket, then create peer2peer connection using aiortc, to achieve webRTC-based video streaming using pure python.
I have managed to send an offer from a peer to another and setRemoteDescription successfully.
client_offer_request = await websocket.recv()
offer_sdp = RTCSessionDescription(client_offer_request, type='offer')
await pc.setRemoteDescription(offer_sdp)
However, when I create answer and setLocalDescription,
# send answer
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
I get this exception.
await self.ws_handler(self, path)
File "/xxxxx/WebRTC/websocket_server.py", line 45, in negotiate
await pc.setLocalDescription(answer)
File "/xxxxx/lib/python3.6/site-packages/aiortc/rtcpeerconnection.py", line 768, in setLocalDescription
t._currentDirection = and_direction(t.direction, t._offerDirection)
File "/xxxxx/lib/python3.6/site-packages/aiortc/rtcpeerconnection.py", line 247, in and_direction
return sdp.DIRECTIONS[sdp.DIRECTIONS.index(a) & sdp.DIRECTIONS.index(b)]
ValueError: None is not in list
The code is simply like follows:
from aiortc import RTCIceCandidate, MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
import asyncio, websockets
def negotiate():
player = MediaPlayer('foo.mp4')
pc = RTCPeerConnection()
pc.addTrack(player.video)
clientOffer = await websocket.recv()
offer_sdp = RTCSessionDescription(clientOffer, type='offer')
await pc.setRemoteDescription(offer_sdp)
# send answer
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
start_server = websockets.serve(negotiate, 'localhost', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
I have resolved the issue. Since the initiative peer sends an offer which is json string like:
After receiving such a json string, the receiving peer has to parse it as json and create SDP instance accordingly:
As such, the issue is orginally from parsing the offer and has nothing to do with
setLocalDescription