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