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    /// The timeout for establishing a WebRTC connection to a peer.
36    ///
37    /// After this time has elapsed, if a WebRTC connection has not been established, the connection
38    /// will fall back to using the signal server as a relay. Any timeout applied to the
39    /// [`Conn::ready`] or [`Conn::send`] futures should allow enough time for this timeout to
40    /// elapse and for the message to be sent over the relay.
41    pub webrtc_connect_timeout: std::time::Duration,
42
43    /// Test falling back to the signal relay by failing the WebRTC setup.
44    pub danger_force_signal_relay: bool,
45
46    /// Deny using the signal server as a relay if direct connections fail.
47    pub danger_deny_signal_relay: bool,
48}
49
50/// The type of credential to use for ICE servers.
51#[derive(Debug, Clone, Default, Serialize, Deserialize)]
52#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
53pub enum CredentialType {
54    /// A password is used for authentication.
55    #[default]
56    Password,
57}
58
59/// Configuration for a group of ICE servers.
60#[derive(Debug, Clone, Serialize, Deserialize, Default)]
61#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
62#[serde(rename_all = "camelCase")]
63pub struct IceServers {
64    /// The ICE server URLs to use for discovering external candidates.
65    pub urls: Vec<String>,
66
67    /// The username to use for authentication.
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub username: Option<String>,
70
71    /// The credential to use for authentication.
72    #[serde(default, skip_serializing_if = "Option::is_none")]
73    pub credential: Option<String>,
74
75    /// The credential type to use for authentication.
76    #[serde(default, skip_serializing_if = "Option::is_none")]
77    pub credential_type: Option<CredentialType>,
78}
79
80/// ICE transport policy.
81#[derive(Debug, Clone, Default, Serialize, Deserialize)]
82#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
83#[serde(rename_all = "camelCase")]
84pub enum TransportPolicy {
85    /// Any type of candidate can be used.
86    #[default]
87    All,
88    /// Only media relay candidates can be used.
89    Relay,
90}
91
92/// WebRTC config.
93///
94/// This configuration will be mapped the specific configuration used by
95/// the selected backend.
96#[derive(Debug, Clone, Serialize, Deserialize, Default)]
97#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
98#[serde(rename_all = "camelCase")]
99pub struct WebRtcConfig {
100    /// A list of ICE servers configurations.
101    pub ice_servers: Vec<IceServers>,
102
103    /// The ICE transport policy to use.
104    #[serde(default)]
105    pub ice_transport_policy: TransportPolicy,
106}
107
108#[cfg(feature = "backend-go-pion")]
109impl WebRtcConfig {
110    /// Convert this [`WebRtcConfig`] to a [`GoBuf`](tx5_go_pion::GoBuf).
111    pub fn to_go_buf(&self) -> std::io::Result<tx5_go_pion::GoBuf> {
112        serde_json::to_vec(self)
113            .map_err(|e| {
114                std::io::Error::other(format!(
115                    "failed to serialize WebRtcConfig: {}",
116                    e
117                ))
118            })?
119            .try_into()
120    }
121}