|
| 1 | +# Port Allocator Configuration (ICE) <!-- {docsify-ignore-all} --> |
| 2 | + |
| 3 | +This guide explains how to configure the ICE port allocator using `dev.onvoid.webrtc.PortAllocatorConfig` and how to use it with `RTCConfiguration` when creating a peer connection. |
| 4 | + |
| 5 | +The Port Allocator controls: |
| 6 | +- The local ephemeral port range used for gathering ICE candidates (HOST, SRFLX, RELAY). |
| 7 | +- Transport behavior via bit flags that mirror native WebRTC PortAllocator flags (e.g., disable TCP candidates, enable IPv6, etc.). |
| 8 | + |
| 9 | +When you need to restrict the ports your application binds to (e.g., to satisfy firewall rules) or tweak which transport types are gathered, use `PortAllocatorConfig`. |
| 10 | + |
| 11 | +## API Overview |
| 12 | + |
| 13 | +`PortAllocatorConfig` exposes three fields: |
| 14 | +- `minPort` (int): Minimum UDP/TCP port to use for candidate gathering (inclusive). Set to 0 to leave unspecified. |
| 15 | +- `maxPort` (int): Maximum UDP/TCP port to use for candidate gathering (inclusive). Set to 0 to leave unspecified. |
| 16 | +- `flags` (int): Bitwise OR of allocator flags (default 0). |
| 17 | + |
| 18 | +Notes: |
| 19 | +- If both `minPort` and `maxPort` are set to non‑zero values, `minPort` must be less than or equal to `maxPort`. |
| 20 | +- A value of 0 for either `minPort` or `maxPort` means "not specified" and the native defaults are used. |
| 21 | + |
| 22 | +Convenience methods are provided to toggle specific behaviors and to combine flags: |
| 23 | +- `setFlag(int flag)`, `clearFlag(int flag)`, `isFlagEnabled(int flag)` |
| 24 | +- Boolean helpers like `setDisableTcp(boolean)`, `isTcpDisabled()`, etc. |
| 25 | + |
| 26 | +## Supported Flags |
| 27 | + |
| 28 | +The following flags mirror WebRTC's native PortAllocator flags. You can use them directly via `setFlag/clearFlag` or through the boolean helpers. |
| 29 | + |
| 30 | +- `PORTALLOCATOR_DISABLE_UDP` — Disable local UDP socket allocation for host candidates. |
| 31 | +- `PORTALLOCATOR_DISABLE_STUN` — Disable STUN candidate gathering (server reflexive). |
| 32 | +- `PORTALLOCATOR_DISABLE_RELAY` — Disable TURN relay candidate gathering. |
| 33 | +- `PORTALLOCATOR_DISABLE_TCP` — Disable local TCP candidate gathering. |
| 34 | +- `PORTALLOCATOR_ENABLE_IPV6` — Enable IPv6 support. |
| 35 | +- `PORTALLOCATOR_ENABLE_SHARED_SOCKET` — Enable shared UDP socket mode (platform/stack‑dependent behavior). |
| 36 | +- `PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE` — Include STUN retransmit attribute on requests. |
| 37 | +- `PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION` — Do not enumerate network adapters. |
| 38 | +- `PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE` — Do not generate a default local candidate. |
| 39 | +- `PORTALLOCATOR_DISABLE_UDP_RELAY` — Disable UDP TURN relay. |
| 40 | +- `PORTALLOCATOR_DISABLE_COSTLY_NETWORKS` — Avoid cellular/expensive networks for candidate gathering. |
| 41 | +- `PORTALLOCATOR_ENABLE_IPV6_ON_WIFI` — Allow IPv6 over Wi‑Fi. |
| 42 | +- `PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS` — Allow binding to any‑address (0.0.0.0/::) ports. |
| 43 | +- `PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS` — Avoid link‑local network interfaces. |
| 44 | + |
| 45 | +## Basic Usage |
| 46 | + |
| 47 | +You configure the port allocator on the `RTCConfiguration` before creating the `RTCPeerConnection`. |
| 48 | + |
| 49 | +```java |
| 50 | +RTCConfiguration cfg = new RTCConfiguration(); |
| 51 | + |
| 52 | +// Constrain ephemeral port range for HOST candidates |
| 53 | +cfg.portAllocatorConfig.minPort = 48000; |
| 54 | +cfg.portAllocatorConfig.maxPort = 48100; |
| 55 | + |
| 56 | +// Example: Disable TCP candidates, keep UDP enabled (default) |
| 57 | +cfg.portAllocatorConfig.setDisableTcp(true); |
| 58 | + |
| 59 | +// Optional: Enable IPv6 support |
| 60 | +// cfg.portAllocatorConfig.setEnableIpv6(true); |
| 61 | + |
| 62 | +RTCPeerConnection pc = factory.createPeerConnection(cfg, observer); |
| 63 | +``` |
| 64 | + |
| 65 | +## Using Flags Directly |
| 66 | + |
| 67 | +You can combine flags using bitwise OR and set them at once: |
| 68 | + |
| 69 | +```java |
| 70 | +int flags = PortAllocatorConfig.PORTALLOCATOR_DISABLE_TCP |
| 71 | + | PortAllocatorConfig.PORTALLOCATOR_DISABLE_RELAY |
| 72 | + | PortAllocatorConfig.PORTALLOCATOR_ENABLE_IPV6; |
| 73 | + |
| 74 | +RTCConfiguration cfg = new RTCConfiguration(); |
| 75 | +cfg.portAllocatorConfig.minPort = 50000; |
| 76 | +cfg.portAllocatorConfig.maxPort = 50100; |
| 77 | +cfg.portAllocatorConfig.flags = flags; |
| 78 | + |
| 79 | +RTCPeerConnection pc = factory.createPeerConnection(cfg, observer); |
| 80 | +``` |
| 81 | + |
| 82 | +Or use the fluent helpers: |
| 83 | + |
| 84 | +```java |
| 85 | +cfg.portAllocatorConfig |
| 86 | + .setDisableStun(true) |
| 87 | + .setDisableRelay(true) |
| 88 | + .setEnableSharedSocket(true); |
| 89 | +``` |
| 90 | + |
| 91 | +## Tips and Troubleshooting |
| 92 | + |
| 93 | +- Port Range Validity: Ensure `minPort <= maxPort` when both are set. If either is 0, the native default behavior applies. |
| 94 | +- Firewalls/NATs: When running behind strict firewalls, restrict the host candidate port range to an allowed window and ensure your firewall allows outbound UDP for STUN/TURN as needed. |
| 95 | +- Disabling Candidates: Disabling STUN and RELAY will limit you to host candidates, which may prevent connectivity across NATs. Use with care. |
| 96 | +- TCP Candidates: Disabling TCP can speed up gathering and reduce unwanted candidates, but may reduce connectivity options in restrictive environments. |
| 97 | +- IPv6: Enabling IPv6 may improve connectivity on IPv6‑capable networks; consider also `setEnableIpv6OnWifi(true)` when applicable. |
| 98 | + |
| 99 | +## Related API |
| 100 | + |
| 101 | +- `RTCConfiguration` — holds `portAllocatorConfig` used by `PeerConnectionFactory#createPeerConnection`. |
| 102 | +- `RTCPeerConnection` — creating a peer connection triggers ICE gathering. |
| 103 | +- `RTCIceServer` — define STUN/TURN servers for non‑host candidates. |
| 104 | + |
| 105 | +For the full API, see the JavaDoc for `PortAllocatorConfig` and `RTCConfiguration`. |
0 commit comments