webrtc/rtc_base/network_constants.h
Sameer Vijaykar 0fc5aeecfb Reduce network cost for cellular network slices.
This change makes network slices on cellular networks more preferable
than the regular network by decreasing the network cost slightly when a
slice is in use.

Network slicing is typically offered as a premium feature on 5G networks
at an additional price.

The network cost is only reduced when the underlying adapter supports
slicing, currently 5G or undifferentiated cellular. It is gated behind
the WebRTC-UnifiedCommunications field trial.

BUG=webrtc:466507512

Change-Id: I230264c95d2d9f0386a096e7ca51cf44b5684922
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/442681
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Sameer Vijaykar <samvi@google.com>
Cr-Commit-Position: refs/heads/main@{#46710}
2026-01-21 14:41:45 -08:00

92 lines
3.4 KiB
C++

/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_NETWORK_CONSTANTS_H_
#define RTC_BASE_NETWORK_CONSTANTS_H_
#include <stdint.h>
#include <string>
namespace webrtc {
constexpr uint16_t kNetworkCostMax = 999;
constexpr uint16_t kNetworkCostCellular2G = 980;
constexpr uint16_t kNetworkCostCellular3G = 910;
constexpr uint16_t kNetworkCostCellular = 900;
constexpr uint16_t kNetworkCostCellular4G = 500;
constexpr uint16_t kNetworkCostCellular5G = 250;
constexpr uint16_t kNetworkCostUnknown = 50;
constexpr uint16_t kNetworkCostLow = 10;
constexpr uint16_t kNetworkCostMin = 0;
// Add 1 to network cost of underlying network type
// so that e.g a "plain" WIFI is prefered over a VPN over WIFI
// everything else being equal.
constexpr uint16_t kNetworkCostVpn = 1;
// TODO: bugs.webrtc.org/466507512 - network slices are not currently
// differentiated so as to reduce the possibility of collisions amongst network
// costs. Differentiation may require a more extensible approach.
// Add -2 to the network cost when using network slicing so that e.g. premium 5G
// slice is preferred over regular 5G, everything else being equal.
constexpr uint16_t kNetworkCostSlice = -2;
// Aliases for correct wrap-around with network slice cost.
constexpr uint16_t kNetworkCostCellular5GSlice = // 248
kNetworkCostCellular5G + kNetworkCostSlice;
constexpr uint16_t kNetworkCostCellular5GVpnSlice = // 249
kNetworkCostCellular5G + kNetworkCostVpn + kNetworkCostSlice;
constexpr uint16_t kNetworkCostCellularSlice = // 898
kNetworkCostCellular + kNetworkCostSlice;
constexpr uint16_t kNetworkCostCellularVpnSlice = // 899
kNetworkCostCellular + kNetworkCostVpn + kNetworkCostSlice;
// alias
constexpr uint16_t kNetworkCostHigh = kNetworkCostCellular;
enum AdapterType {
// This enum resembles the one in Chromium net::ConnectionType.
ADAPTER_TYPE_UNKNOWN = 0,
ADAPTER_TYPE_ETHERNET = 1 << 0,
ADAPTER_TYPE_WIFI = 1 << 1,
ADAPTER_TYPE_CELLULAR = 1 << 2, // This is CELLULAR of unknown type.
ADAPTER_TYPE_VPN = 1 << 3,
ADAPTER_TYPE_LOOPBACK = 1 << 4,
// ADAPTER_TYPE_ANY is used for a network, which only contains a single "any
// address" IP address (INADDR_ANY for IPv4 or in6addr_any for IPv6), and can
// use any/all network interfaces. Whereas ADAPTER_TYPE_UNKNOWN is used
// when the network uses a specific interface/IP, but its interface type can
// not be determined or not fit in this enum.
ADAPTER_TYPE_ANY = 1 << 5,
ADAPTER_TYPE_CELLULAR_2G = 1 << 6,
ADAPTER_TYPE_CELLULAR_3G = 1 << 7,
ADAPTER_TYPE_CELLULAR_4G = 1 << 8,
ADAPTER_TYPE_CELLULAR_5G = 1 << 9
};
std::string AdapterTypeToString(AdapterType type);
// Useful for testing!
constexpr AdapterType kAllAdapterTypes[] = {
ADAPTER_TYPE_UNKNOWN, ADAPTER_TYPE_ETHERNET,
ADAPTER_TYPE_WIFI, ADAPTER_TYPE_CELLULAR,
ADAPTER_TYPE_VPN, ADAPTER_TYPE_LOOPBACK,
ADAPTER_TYPE_ANY, ADAPTER_TYPE_CELLULAR_2G,
ADAPTER_TYPE_CELLULAR_3G, ADAPTER_TYPE_CELLULAR_4G,
ADAPTER_TYPE_CELLULAR_5G,
};
} // namespace webrtc
#endif // RTC_BASE_NETWORK_CONSTANTS_H_