如何要求STUN服务器使用aiortc生成ice候选对象?

2024-09-28 13:25:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个工作的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所连接的是一个假地址)。在这之后,我不知道如何收集ice候选人。我尝试过使用iceGatherer,但运气不佳:

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

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


Tags: toimportsdp客户端asyncserverconnectsync
1条回答
网友
1楼 · 发布于 2024-09-28 13:25:30

当您调用setLocalDescription时,您发布的代码实际上已经执行了ICE候选对象收集。查看您正在打印的会话描述,您应该会看到标记为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())

相关问题 更多 >

    热门问题