我有一个可以工作的WebRTC客户端,我想通过使用aiotrc (python)的WebRTC接收它的视频。另一个客户端作为接收方工作得很好,我们已经用浏览器对其进行了测试。
使用python配置服务器,使用收发器创建一个offer (我只想接收视频),并将offer设置为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候选人寄给收件人。在这一点上,我找不到任何关于如何使用aiortc获取ice候选者的信息。下一步是什么?
发布于 2021-01-31 20:05:13
当您调用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
已经使用了谷歌的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())
https://stackoverflow.com/questions/62561627
复制相似问题