32 lines
836 B
Python
32 lines
836 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
from aiortc import RTCIceCandidate
|
|
|
|
|
|
def parse_ice(data: dict[str, Any]) -> RTCIceCandidate | None:
|
|
"""解析ICE候选者字符串为RTCIceCandidate对象"""
|
|
match = re.match(
|
|
r'candidate:(\S+) (\d) (\S+) (\d+) (\S+) (\d+) typ (\S+)(?: raddr (\S+) rport (\d+))?',
|
|
data["candidate"],
|
|
)
|
|
if not match:
|
|
return None
|
|
g = match.groups()
|
|
cand = RTCIceCandidate(
|
|
foundation=g[0],
|
|
component=int(g[1]),
|
|
protocol=g[2].lower(),
|
|
priority=int(g[3]),
|
|
ip=g[4],
|
|
port=int(g[5]),
|
|
type=g[6],
|
|
relatedAddress=g[7],
|
|
relatedPort=int(g[8]) if g[8] else None,
|
|
)
|
|
cand.sdpMid = data.get("sdpMid")
|
|
cand.sdpMLineIndex = data.get("sdpMLineIndex", 0)
|
|
return cand
|