首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用aiortc请求STUN服务器生成ice候选?

如何使用aiortc请求STUN服务器生成ice候选?
EN

Stack Overflow用户
提问于 2020-06-25 02:18:25
回答 1查看 1K关注 0票数 3

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

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

代码语言:javascript
运行
复制
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,但没有成功:

代码语言:javascript
运行
复制
ice_gath = RTCIceGatherer(iceServers=[ice_server])
candidates = ice_gath.getLocalCandidates()

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

EN

回答 1

Stack Overflow用户

发布于 2021-01-31 20:05:13

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

代码语言:javascript
运行
复制
a=candidate:5dd630545cbb8dd4f09c40b43b0f2db4 1 udp 1694498815 PUBLIC.IP.ADDRESS.HERE 42162 typ srflx raddr 192.168.1.44 rport 42162

还要注意的是,默认情况下,aiortc已经使用了谷歌的STUN服务器,所以这里是示例的一个更精简的版本:

代码语言:javascript
运行
复制
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())
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62561627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档