From 803e32deaf9156253e2c78719db67dcdd590828d Mon Sep 17 00:00:00 2001 From: Renan Prata Date: Thu, 21 Apr 2022 22:04:15 -0300 Subject: [PATCH] feat: implementing trickle ice --- src/aioice/ice.py | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/aioice/ice.py b/src/aioice/ice.py index f33a8dc..136e12c 100644 --- a/src/aioice/ice.py +++ b/src/aioice/ice.py @@ -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. @@ -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]: """ @@ -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() @@ -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: @@ -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( @@ -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