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
39/// The type of credential to use for ICE servers.
40#[derive(Debug, Clone, Default, Serialize, Deserialize)]
41#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
42pub enum CredentialType {
43    /// A password is used for authentication.
44    #[default]
45    Password,
46}
47
48/// Configuration for a group of ICE servers.
49#[derive(Debug, Clone, Serialize, Deserialize, Default)]
50#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
51#[serde(rename_all = "camelCase")]
52pub struct IceServers {
53    /// The ICE server URLs to use for discovering external candidates.
54    pub urls: Vec<String>,
55
56    /// The username to use for authentication.
57    #[serde(default)]
58    pub username: Option<String>,
59
60    /// The credential to use for authentication.
61    #[serde(default)]
62    pub credential: Option<String>,
63
64    /// The credential type to use for authentication.
65    #[serde(default)]
66    pub credential_type: Option<CredentialType>,
67}
68
69/// ICE transport policy.
70#[derive(Debug, Clone, Default, Serialize, Deserialize)]
71#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
72pub enum TransportPolicy {
73    /// Any type of candidate can be used.
74    #[default]
75    All,
76    /// Only media relay candidates can be used.
77    Relay,
78}
79
80/// WebRTC config.
81///
82/// This configuration will be mapped the specific configuration used by
83/// the selected backend.
84#[derive(Debug, Clone, Serialize, Deserialize, Default)]
85#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
86#[serde(rename_all = "camelCase")]
87pub struct WebRtcConfig {
88    /// A list of ICE servers configurations.
89    pub ice_servers: Vec<IceServers>,
90
91    /// The ICE transport policy to use.
92    #[serde(default)]
93    pub ice_transport_policy: TransportPolicy,
94}
95
96#[cfg(feature = "backend-go-pion")]
97impl WebRtcConfig {
98    /// Convert this [`WebRtcConfig`] to a [`GoBuf`](tx5_go_pion::GoBuf).
99    pub fn to_go_buf(&self) -> std::io::Result<tx5_go_pion::GoBuf> {
100        serde_json::to_vec(self)
101            .map_err(|e| {
102                std::io::Error::other(format!(
103                    "failed to serialize WebRtcConfig: {}",
104                    e
105                ))
106            })?
107            .try_into()
108    }
109}