|
| 1 | + |
| 2 | +WebRTC Port Range Configuration |
| 3 | +=== |
| 4 | + |
| 5 | +# Background |
| 6 | +WebRTC by default allocates ports dynamically from the system’s ephemeral range. |
| 7 | +In enterprise or testing environments, developers often need deterministic or firewall-friendly port allocation. |
| 8 | + |
| 9 | +This API enables developers to configure the port range WebRTC uses for [ICE](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols#ice) candidates and media connections. |
| 10 | + |
| 11 | +The initial support is for **UDP**, with room to extend to **TCP** in the future. |
| 12 | + |
| 13 | +By exposing a `WebRtcPortConfiguration` object on `CoreWebView2EnvironmentOptions`, developers can set and retrieve the port range before creating the WebView2 environment. |
| 14 | + |
| 15 | +# Conceptual pages (How To) |
| 16 | + |
| 17 | +Developers can use this API to restrict WebRTC’s UDP ports to a specific range which WebRTC uses for ICE candidate and media connections. |
| 18 | + |
| 19 | +ICE stands for **Interactive Connectivity Establishment**. It is a standard method of NAT traversal used in WebRTC. It is defined in [IETF RFC 5245](https://datatracker.ietf.org/doc/html/rfc5245). ICE deals with the process of connecting media through NATs by conducting connectivity checks. |
| 20 | + |
| 21 | +Common scenarios: |
| 22 | +- Configure ports for **enterprise firewall compliance**. |
| 23 | +- Run **deterministic tests** where ICE candidate ports are predictable. |
| 24 | +- Avoid conflicts with other applications that may already use ephemeral ranges. |
| 25 | + |
| 26 | +Usage steps: |
| 27 | +1. Create `CoreWebView2EnvironmentOptions`. |
| 28 | +2. Access the `WebRtcPortConfiguration` object. |
| 29 | +3. Call `SetPortRange` for `CoreWebView2WebRtcProtocolKind.Udp`. |
| 30 | +4. Pass the options when creating the WebView2 environment. |
| 31 | + |
| 32 | + |
| 33 | +# Examples |
| 34 | +### C++ Configure UDP Port Range |
| 35 | +```cpp |
| 36 | +wil::com_ptr<ICoreWebView2EnvironmentOptions> options = |
| 37 | + Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>(); |
| 38 | + |
| 39 | +wil::com_ptr<ICoreWebView2WebRtcPortConfiguration> portConfig; |
| 40 | +CHECK_FAILURE(options->get_WebRtcPortConfiguration(&portConfig)); |
| 41 | + |
| 42 | +CHECK_FAILURE(portConfig->SetPortRange( |
| 43 | + CoreWebView2WebRtcProtocolKind::Udp, 50000, 51000)); |
| 44 | + |
| 45 | +HRESULT hr = CreateCoreWebView2EnvironmentWithOptions( |
| 46 | + subFolder, m_userDataFolder.c_str(), options.Get(), |
| 47 | + Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>( |
| 48 | + this, &AppWindow::OnCreateEnvironmentCompleted) |
| 49 | + .Get()); |
| 50 | +``` |
| 51 | +
|
| 52 | +### C# Configure UDP Port Range |
| 53 | +```csharp |
| 54 | +var options = new CoreWebView2EnvironmentOptions(); |
| 55 | +
|
| 56 | +var portConfig = options.WebRtcPortConfiguration; |
| 57 | +portConfig.SetPortRange(CoreWebView2WebRtcProtocolKind.Udp, 50000, 51000); |
| 58 | +
|
| 59 | +var environment = await CoreWebView2Environment.CreateAsync( |
| 60 | + browserExecutableFolder: subFolder, |
| 61 | + userDataFolder: m_userDataFolder, |
| 62 | + options: options); |
| 63 | +
|
| 64 | +OnCreateEnvironmentCompleted(environment); |
| 65 | +``` |
| 66 | + |
| 67 | +# API Details |
| 68 | +### C++ |
| 69 | +``` |
| 70 | +/// Additional options used to create WebView2 Environment to manage WebRTC UDP port range configuration. |
| 71 | +[uuid(0eebe393-8dcf-5bc5-a15d-d862088242e9), object, pointer_default(unique)] |
| 72 | +interface ICoreWebView2EnvironmentOptions10 : IUnknown { |
| 73 | + /// Get the WebRTC port range configuration object for configuring a custom port range. |
| 74 | + /// This configuration object can be used to set and retrieve port range configuration |
| 75 | + /// that WebRTC will use for ICE candidates and media connections. |
| 76 | + /// If no custom range is configured, WebRTC will use the operating system's default dynamic port range. |
| 77 | + /// Configuration must be completed before the environment is created. Once the environment is |
| 78 | + /// created, the port range cannot be customized. |
| 79 | + /// |
| 80 | + /// |
| 81 | + /// \snippet AppWindow.cpp WebRtcPortConfiguration |
| 82 | + [propget] HRESULT WebRtcPortConfiguration([out, retval] ICoreWebView2WebRtcPortConfiguration** value); |
| 83 | +} |
| 84 | +
|
| 85 | +/// WebRTC port configuration interface for managing WebRTC port range configuration. |
| 86 | +/// This interface provides methods to configure and retrieve custom port ranges |
| 87 | +/// that WebRTC will use for ICE candidates and media connections across different protocols. |
| 88 | +[uuid(b1ac2eb4-15b5-574f-aeb7-c51b9f1520fa), object, pointer_default(unique)] |
| 89 | +interface ICoreWebView2WebRtcPortConfiguration : IUnknown { |
| 90 | + /// The `SetPortRange` method allows you to set a custom port range for WebRTC to use |
| 91 | + /// for a specific protocol type. |
| 92 | + /// This method allows configuring a specific port range that WebRTC will use |
| 93 | + /// for ICE candidates and media connections for the specified protocol. |
| 94 | + /// |
| 95 | + /// `protocol` specifies the WebRTC protocol type (UDP, TCP, etc.). |
| 96 | + /// `minPort` and `maxPort` must be in the range 1025-65535 (inclusive). |
| 97 | + /// Calls with invalid ranges return E_INVALIDARG. |
| 98 | + /// `minPort` must be less than or equal to `maxPort`. |
| 99 | + /// If `minPort` equals `maxPort`, it represents a single port. |
| 100 | + /// |
| 101 | + /// Calling this method will replace any previously configured port range for the specified protocol. |
| 102 | + /// |
| 103 | + /// |
| 104 | + /// \snippet AppWindow.cpp WebRtcPortConfiguration |
| 105 | + HRESULT SetPortRange( |
| 106 | + [in] COREWEBVIEW2_WEB_RTC_PROTOCOL_KIND protocol, |
| 107 | + [in] UINT32 minPort, |
| 108 | + [in] UINT32 maxPort |
| 109 | + ); |
| 110 | +
|
| 111 | + /// The `GetPortRange` method gets the currently configured port range for a specific protocol. |
| 112 | + /// Returns TRUE if a custom port range is configured for the specified protocol, |
| 113 | + /// with the range values in out parameters. |
| 114 | + /// Returns FALSE if no custom range is set for the protocol (using default dynamic allocation), |
| 115 | + /// in which case the out parameter values should be ignored. |
| 116 | + /// |
| 117 | + /// |
| 118 | + /// \snippet AppWindow.cpp WebRtcPortConfiguration |
| 119 | + HRESULT GetPortRange( |
| 120 | + [in] COREWEBVIEW2_WEB_RTC_PROTOCOL_KIND protocol, |
| 121 | + [out] UINT32* minPort, |
| 122 | + [out] UINT32* maxPort |
| 123 | + , [out, retval] BOOL* value); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### C# |
| 128 | +```csharp |
| 129 | +/// <summary> |
| 130 | +/// Specifies the WebRTC protocol type for port range configuration. |
| 131 | +/// </summary> |
| 132 | +public enum CoreWebView2WebRtcProtocolKind |
| 133 | +{ |
| 134 | + /// <summary> |
| 135 | + /// UDP protocol for WebRTC media and ICE candidates. |
| 136 | + /// </summary> |
| 137 | + Udp = 0, |
| 138 | +} |
| 139 | + |
| 140 | +/// <summary> |
| 141 | +/// WebRTC port configuration interface for managing WebRTC port range configuration. |
| 142 | +/// This interface provides methods to configure and retrieve custom port ranges |
| 143 | +/// that WebRTC will use for ICE candidates and media connections across different protocols. |
| 144 | +/// </summary> |
| 145 | +public interface ICoreWebView2WebRtcPortConfiguration |
| 146 | +{ |
| 147 | + /// <summary> |
| 148 | + /// The SetPortRange method allows you to set a custom port range for WebRTC to use |
| 149 | + /// for a specific protocol type. |
| 150 | + /// This method allows configuring a specific port range that WebRTC will use |
| 151 | + /// for ICE candidates and media connections for the specified protocol. |
| 152 | + /// |
| 153 | + /// protocol specifies the WebRTC protocol type. |
| 154 | + /// minPort and maxPort must be in the range 1025-65535 (inclusive). |
| 155 | + /// Calls with invalid ranges return E_INVALIDARG. |
| 156 | + /// minPort must be less than or equal to maxPort. |
| 157 | + /// If minPort equals maxPort, it represents a single port. |
| 158 | + /// |
| 159 | + /// Calling this method will replace any previously configured port range for the specified protocol. |
| 160 | + /// </summary> |
| 161 | + /// <param name="protocol">The WebRTC protocol type</param> |
| 162 | + /// <param name="minPort">Minimum port in the range (1025-65535)</param> |
| 163 | + /// <param name="maxPort">Maximum port in the range (1025-65535)</param> |
| 164 | + void SetPortRange(CoreWebView2WebRtcProtocolKind protocol, uint minPort, uint maxPort); |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// The GetPortRange method gets the currently configured port range for a specific protocol. |
| 168 | + /// Returns true if a custom port range is configured for the specified protocol, |
| 169 | + /// with the range values in out parameters. |
| 170 | + /// Returns false if no custom range is set for the protocol (using default dynamic allocation), |
| 171 | + /// in which case the out parameter values should be ignored. |
| 172 | + /// </summary> |
| 173 | + /// <param name="protocol">The WebRTC protocol type</param> |
| 174 | + /// <param name="minPort">Output parameter for minimum port in the range</param> |
| 175 | + /// <param name="maxPort">Output parameter for maximum port in the range</param> |
| 176 | + /// <returns>True if custom range is configured, false if using default allocation</returns> |
| 177 | + bool GetPortRange(CoreWebView2WebRtcProtocolKind protocol, out uint minPort, out uint maxPort); |
| 178 | +} |
| 179 | + |
| 180 | +/// <summary> |
| 181 | +/// Additional options used to create WebView2 Environment to manage WebRTC port range configuration. |
| 182 | +/// </summary> |
| 183 | +public interface ICoreWebView2EnvironmentOptions |
| 184 | +{ |
| 185 | + /// <summary> |
| 186 | + /// Gets the WebRTC port configuration object for configuring custom port ranges. |
| 187 | + /// This configuration can be used to set and retrieve port range configuration |
| 188 | + /// that WebRTC will use for ICE candidates and media connections. |
| 189 | + /// If no range is configured, WebRTC uses the OS ephemeral port range. |
| 190 | + /// Configuration must be completed before the environment is created. Once the environment is |
| 191 | + /// setup, the port range can not be customized. |
| 192 | + /// </summary> |
| 193 | + ICoreWebView2WebRtcPortConfiguration WebRtcPortConfiguration { get; } |
| 194 | +} |
| 195 | +``` |
0 commit comments