tx5_connection/
config.rs

1use serde::{Deserialize, Serialize};
2use std::sync::Arc;
3
4/// The backend webrtc module to use.
5#[derive(Debug, Clone, Copy)]
6pub enum BackendModule {
7    /// Use the libdatachannel backend.
8    #[cfg(feature = "backend-libdatachannel")]
9    LibDataChannel,
10
11    /// Use the go pion backend.
12    #[cfg(feature = "backend-go-pion")]
13    GoPion,
14}
15
16impl Default for BackendModule {
17    #[allow(unreachable_code)]
18    fn default() -> Self {
19        #[cfg(feature = "backend-libdatachannel")]
20        return Self::LibDataChannel;
21        #[cfg(feature = "backend-go-pion")]
22        Self::GoPion
23    }
24}
25
26/// Tx5 connection hub config.
27#[derive(Default)]
28pub struct HubConfig {
29    /// The backend webrtc module to use.
30    pub backend_module: BackendModule,
31
32    /// The signal config to use.
33    pub signal_config: Arc<tx5_signal::SignalConfig>,
34
35    /// Test falling back to the signal relay by failing the WebRTC setup.
36    pub danger_force_signal_relay: bool,
37
38    /// Deny using the signal server as a relay if direct connections fail.
39    pub danger_deny_signal_relay: bool,
40}
41
42/// The type of credential to use for ICE servers.
43#[derive(Debug, Clone, Default, Serialize, Deserialize)]
44#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
45pub enum CredentialType {
46    /// A password is used for authentication.
47    #[default]
48    Password,
49}
50
51/// Configuration for a group of ICE servers.
52#[derive(Debug, Clone, Serialize, Deserialize, Default)]
53#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
54#[serde(rename_all = "camelCase")]
55pub struct IceServers {
56    /// The ICE server URLs to use for discovering external candidates.
57    pub urls: Vec<String>,
58
59    /// The username to use for authentication.
60    #[serde(default)]
61    pub username: Option<String>,
62
63    /// The credential to use for authentication.
64    #[serde(default)]
65    pub credential: Option<String>,
66
67    /// The credential type to use for authentication.
68    #[serde(default)]
69    pub credential_type: Option<CredentialType>,
70}
71
72/// ICE transport policy.
73#[derive(Debug, Clone, Default, Serialize, Deserialize)]
74#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
75pub enum TransportPolicy {
76    /// Any type of candidate can be used.
77    #[default]
78    All,
79    /// Only media relay candidates can be used.
80    Relay,
81}
82
83/// WebRTC config.
84///
85/// This configuration will be mapped the specific configuration used by
86/// the selected backend.
87#[derive(Debug, Clone, Serialize, Deserialize, Default)]
88#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
89#[serde(rename_all = "camelCase")]
90pub struct WebRtcConfig {
91    /// A list of ICE servers configurations.
92    pub ice_servers: Vec<IceServers>,
93
94    /// The ICE transport policy to use.
95    #[serde(default)]
96    pub ice_transport_policy: TransportPolicy,
97}
98
99#[cfg(feature = "backend-go-pion")]
100impl WebRtcConfig {
101    /// Convert this [`WebRtcConfig`] to a [`GoBuf`](tx5_go_pion::GoBuf).
102    pub fn to_go_buf(&self) -> std::io::Result<tx5_go_pion::GoBuf> {
103        serde_json::to_vec(self)
104            .map_err(|e| {
105                std::io::Error::other(format!(
106                    "failed to serialize WebRtcConfig: {}",
107                    e
108                ))
109            })?
110            .try_into()
111    }
112}