python - 如何要求 STUN 服务器使用 aiortc 生成 ice candidates?

标签 python webrtc 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()

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

最佳答案

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

关于python - 如何要求 STUN 服务器使用 aiortc 生成 ice candidates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62561627/

相关文章:

javascript - 无法在两个 Python aiortc 脚本之间完全建立 ICE 连接

Python av 无限循环播放文件

python - 使用 Python 的 ETL

python - 使用 Python 对 url 进行切片

javascript - 无流连接到 Kurento Room

node.js - ffmpeg : Bitstream not supported by this decoder

javascript - RTCMultiConnection 返回当前流

python - 从 PyAV av.VideoFrame 创建 HLS 视频流

python - 在 python 中使用 file.seek() 时通常将多少字节加载到内存中?

python - 如何选择pandas中每个唯一记录的第一行和最后一行