Skip to content

Commit

Permalink
feat: implementing trickle ice
Browse files Browse the repository at this point in the history
  • Loading branch information
rprata committed Apr 22, 2022
1 parent f08956b commit 803e32d
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/aioice/ice.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def __init__(
turn_transport: str = "udp",
use_ipv4: bool = True,
use_ipv6: bool = True,
use_trickle_ice: bool = True,
) -> None:
self.ice_controlling = ice_controlling
#: Local username, automatically set to a random value.
Expand Down Expand Up @@ -341,6 +342,8 @@ def __init__(
self._use_ipv4 = use_ipv4
self._use_ipv6 = use_ipv6

self._use_trickle_ice = use_trickle_ice

@property
def local_candidates(self) -> List[Candidate]:
"""
Expand Down Expand Up @@ -843,9 +846,9 @@ def _find_pair(
return pair
return None

async def get_component_candidates(
self, component: int, addresses: List[str], timeout: int = 5
) -> List[Candidate]:
async def get_host_candidates(
self, component: int, addresses: List[str]
) -> Tuple[List[Candidate], List[StunProtocol]]:
candidates = []
loop = asyncio.get_event_loop()

Expand Down Expand Up @@ -882,8 +885,14 @@ async def get_component_candidates(
candidates.append(protocol.local_candidate)
self._protocols += host_protocols

# query STUN server for server-reflexive candidates (IPv4 only)
return candidates, host_protocols

async def query_stun_server(
self, host_protocols: List[StunProtocol], timeout: int = 5
) -> Optional[List[Candidate]]:
candidates = None
if self.stun_server:
candidates = []
tasks = []
for protocol in host_protocols:
if ipaddress.ip_address(protocol.local_candidate.host).version == 4:
Expand All @@ -899,8 +908,11 @@ async def get_component_candidates(
]
for task in pending:
task.cancel()
return candidates

async def connect_turn_server(self, component: int) -> Optional[Candidate]:
candidate = None

# connect to TURN server
if self.turn_server:
# create transport
_, protocol = await turn.create_turn_endpoint(
Expand Down Expand Up @@ -928,7 +940,30 @@ async def get_component_candidates(
related_address=related_address[0],
related_port=related_address[1],
)
candidates.append(protocol.local_candidate)
candidate = protocol.local_candidate
return candidate

async def get_component_candidates(
self, component: int, addresses: List[str], timeout: int = 5
) -> List[Candidate]:
candidates = []

host_candidates, host_protocols = await self.get_host_candidates(
component, addresses
)
candidates.append(host_candidates)

# query STUN server for server-reflexive candidates (IPv4 only)
srflx_candidates = await self.query_stun_server(
host_protocols=host_protocols, timeout=timeout
)
if srflx_candidates:
candidates.append(srflx_candidates)

# connect to TURN server
relay_cadidate = await self.connect_turn_server(component=component)
if relay_cadidate:
candidates.append(relay_cadidate)

return candidates

Expand Down

0 comments on commit 803e32d

Please sign in to comment.