如何要求STUN服务器使用aiortc生成冰候选者?



我有一个工作的WebRTC客户端,我想使用aiotrc(python(通过WebRTC接收它的视频。另一个客户端作为接收者工作正常,我们已经用浏览器对其进行了测试。

使用 python,我配置服务器,我用收发器创建一个报价(我只想接收视频(,并将报价设置为 localDescription:

import json
import socketio
import asyncio
from asgiref.sync import async_to_sync
from aiortc import RTCPeerConnection, RTCSessionDescription, RTCIceCandidate, RTCConfiguration, RTCIceServer, RTCIceGatherer, RTCRtpTransceiver
session_id = 'default'
sio = socketio.Client()
ice_server = RTCIceServer(urls='stun:stun.l.google.com:19302')
pc = RTCPeerConnection(configuration=RTCConfiguration(iceServers=[ice_server]))
pc.addTransceiver("video", direction="recvonly")
def connect():
sio.connect('https://192.168.10.123', namespaces=['/live'])
connect()
@async_to_sync
async def set_local_description():
await pc.setLocalDescription(await pc.createOffer())
print('Local description set to: ', pc.localDescription)
#send_signaling_message(json.dumps({'sdp':{'sdp': pc.localDescription.sdp, 'type':pc.localDescription.type}}))

set_local_description()

(在这种情况下,socket.io 连接的位置是假地址(。在这一点之后,我不知道如何收集冰候选者。我尝试使用iceGatherer,但没有运气:

ice_gath = RTCIceGatherer(iceServers=[ice_server])
candidates = ice_gath.getLocalCandidates()

我必须将冰候选者发送给收件人。在这一点上,我找不到任何关于如何使用aiortc获得冰候选者的信息。下一步是什么?

您发布的代码实际上已经执行了 ICE 候选收集,当您调用setLocalDescription时。查看您正在打印的会话描述,您应该会看到标记为srflx的候选项,这意味着"服务器反射":从 STUN 服务器的角度来看,这些是您的 IP 地址,例如:

a=candidate:5dd630545cbb8dd4f09c40b43b0f2db4 1 udp 1694498815 PUBLIC.IP.ADDRESS.HERE 42162 typ srflx raddr 192.168.1.44 rport 42162

另请注意,默认情况下,aiortc已经使用了Google的STUN服务器,因此这是示例的简化版本:

import asyncio
from aiortc import RTCPeerConnection

async def dump_local_description():
pc = RTCPeerConnection()
pc.addTransceiver("video", direction="recvonly")
await pc.setLocalDescription(await pc.createOffer())
print(pc.localDescription.sdp)

loop = asyncio.get_event_loop()
loop.run_until_complete(dump_local_description())

最新更新