如何在 aiortc (python) 中将答案设置为 LocalDescription?

How to set an answer as LocalDescription in aiortc (python)?

我正在尝试使用 python websocket 执行信令,然后使用 aiortc 创建点对点连接,以实现 基于webRTC的视频流使用纯python.

我已经成功地从一个同行向另一个同行发送报价,并且 setRemoteDescription 成功。

client_offer_request = await websocket.recv()
offer_sdp = RTCSessionDescription(client_offer_request, type='offer')
await pc.setRemoteDescription(offer_sdp)

但是,当我创建答案和 setLocalDescription 时,

# send answer
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)

我得到这个异常。

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

代码简单如下:

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()

我已经解决了这个问题。 由于发起节点发送了一个 json 字符串的报价,例如:

{'sdp': 'v=0\r\no=- 3841969261 3841969261 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0\r\na=msid-semantic:WMS *\r\nm=video 34067 UDP/TLS/RTP/SAVPF 97 98 99 100 101 102\r\nc=IN \na=sendrecv\r\na=extmap:1 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=mid:0\r\na=\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=rtcp-mux\r\na=ssrc-group: 124673822\r\na=\r\na=rtpmap:97 VP8/90000\r\na=rtcp-fb:97 nack\r\na=rtcp-fb:97 nack pli\r\na=rtcp-fb:97 goog-remb\r\na=rtpmap:98 rtx/90000\r\na=fmtp:98 apt=97\r\na=rtpmap:99 H264/90000\r\na=rtcp-fb:99 nack\r\na=rtcp-fb:99 nack pli\r\na=rtcp-fb:99 goog-remb\r\na=fmtp:99 packetization-mode=1;level-asymmetry-allowed=1;profile-level-id=42001f\r\na=rtpmap:100 rtx/90000\r\na=fmtp:100 apt=99\r\na=rtpmap:101 H264/90000\r\na=rtcp-fb:101 nack\r\na=rtcp-fb:101 nack pli\r\na=rtcp-fb:101 goog-remb\r\na=fmtp:101 packetization-mode=1;level-asymmetry-allowed=1;profile-level-id=42e01f\r\na=rtpmap:102 rtx/90000\r\na=fmtp:102 apt=101\r\r\n', 'type': 'offer'} 

接收到这样的json字符串后,接收方必须将其解析为json并相应地创建SDP实例:

client_offer_request = await websocket.recv()
offer = json.loads(client_offer_request)
offer_sdp = RTCSessionDescription(offer["sdp"], offer["type"])

因此,问题最初来自解析报价,与 setLocalDescription

无关