Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: feat: implementing trickle ice #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/aioice/ice.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def __init__(
use_ipv4: bool = True,
use_ipv6: bool = True,
transport_policy: TransportPolicy = TransportPolicy.ALL,
use_trickle_ice: bool = False,
) -> None:
self.ice_controlling = ice_controlling
#: Local username, automatically set to a random value.
Expand Down Expand Up @@ -366,6 +367,7 @@ def __init__(
)

self._transport_policy = transport_policy
self._use_trickle_ice = use_trickle_ice

@property
def local_candidates(self) -> List[Candidate]:
Expand Down Expand Up @@ -877,9 +879,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 @@ -916,7 +918,13 @@ 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
) -> List[Candidate]:
candidates = []

if self.stun_server:
tasks = []
for protocol in host_protocols:
Expand All @@ -933,8 +941,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 All @@ -961,7 +972,29 @@ 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 += host_candidates

# query STUN server for server-reflexive candidates (IPv4 only)
srflx_candidates = await self._query_stun_server(
host_protocols=host_protocols, timeout=timeout
)
candidates += srflx_candidates

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

return candidates

Expand Down