plugin XEP-0167: move ICE candidates parsing/SDP line building in separate methods:

These methods will be used by incoming "Conferences" component.

rel 445
This commit is contained in:
Goffi 2024-07-05 17:18:37 +02:00
commit 6afc342875

View file

@ -183,31 +183,8 @@ def generate_sdp_from_session(session: dict, local: bool = False, port: int = 9)
sdp_lines.append(f"a=ice-pwd:{ice_data['pwd']}")
for candidate in ice_data["candidates"]:
foundation = candidate["foundation"]
component_id = candidate["component_id"]
transport = candidate["transport"]
priority = candidate["priority"]
address = candidate["address"]
candidate_port = candidate["port"]
candidate_type = candidate["type"]
candidate_line = (
f"a=candidate:{foundation} {component_id} {transport} {priority} "
f"{address} {candidate_port} typ {candidate_type}"
)
if "rel_addr" in candidate and "rel_port" in candidate:
candidate_line += (
f" raddr {candidate['rel_addr']} rport {candidate['rel_port']}"
)
if "generation" in candidate:
candidate_line += f" generation {candidate['generation']}"
if "network" in candidate:
candidate_line += f" network {candidate['network']}"
sdp_lines.append(candidate_line)
candidate_line = generate_candidate_line(candidate)
sdp_lines.append(f"a={candidate_line}")
# Generate a= lines for encryption
if "encryption" in media_data:
@ -240,6 +217,66 @@ def generate_sdp_from_session(session: dict, local: bool = False, port: int = 9)
return "\r\n".join(sdp_lines) + "\r\n"
def generate_candidate_line(candidate: dict) -> str:
"""Generate a ``candidate:`` attribute line from candidate data.
@param candidate: ICE candidate data.
@return ICE candidate attribute line.
"""
foundation = candidate["foundation"]
component_id = candidate["component_id"]
transport = candidate["transport"]
priority = candidate["priority"]
address = candidate["address"]
candidate_port = candidate["port"]
candidate_type = candidate["type"]
candidate_line = (
f"candidate:{foundation} {component_id} {transport} {priority} "
f"{address} {candidate_port} typ {candidate_type}"
)
if "rel_addr" in candidate and "rel_port" in candidate:
candidate_line += f" raddr {candidate['rel_addr']} rport {candidate['rel_port']}"
if "generation" in candidate:
candidate_line += f" generation {candidate['generation']}"
if "network" in candidate:
candidate_line += f" network {candidate['network']}"
return candidate_line
def parse_candidate(parts: list[str]) -> dict:
"""Parse parts of a ICE candidate
@param parts: Parts of the candidate line.
@return: candidate data
"""
candidate = {
"foundation": parts[0],
"component_id": int(parts[1]),
"transport": parts[2],
"priority": int(parts[3]),
"address": parts[4],
"port": int(parts[5]),
"type": parts[7],
}
for part in parts[8:]:
if part == "raddr":
candidate["rel_addr"] = parts[parts.index(part) + 1]
elif part == "rport":
candidate["rel_port"] = int(parts[parts.index(part) + 1])
elif part == "generation":
candidate["generation"] = parts[parts.index(part) + 1]
elif part == "network":
candidate["network"] = parts[parts.index(part) + 1]
return candidate
def parse_sdp(sdp: str, role: str) -> dict:
"""Parse SDP string.
@ -377,25 +414,7 @@ def parse_sdp(sdp: str, role: str) -> dict:
elif attribute == "candidate":
assert transport_data is not None
candidate = {
"foundation": parts[0],
"component_id": int(parts[1]),
"transport": parts[2],
"priority": int(parts[3]),
"address": parts[4],
"port": int(parts[5]),
"type": parts[7],
}
for part in parts[8:]:
if part == "raddr":
candidate["rel_addr"] = parts[parts.index(part) + 1]
elif part == "rport":
candidate["rel_port"] = int(parts[parts.index(part) + 1])
elif part == "generation":
candidate["generation"] = parts[parts.index(part) + 1]
elif part == "network":
candidate["network"] = parts[parts.index(part) + 1]
candidate = parse_candidate(parts)
transport_data.setdefault("candidates", []).append(candidate)