use std::borrow::Cow;
use ruma_common::serde::JsonObject;
use serde::{Deserialize, Deserializer, Serialize};
#[cfg(feature = "unstable-msc4195")]
pub mod livekit;
pub mod transports;
#[allow(deprecated)] #[derive(Clone, Debug, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[serde(tag = "type")]
pub enum RtcTransport {
#[cfg(feature = "unstable-msc4195")]
#[serde(rename = "m.livekit")]
LiveKit(LiveKitRtcTransport),
#[cfg(feature = "unstable-msc4195")]
#[serde(rename = "livekit")]
#[deprecated = "Use `RtcTransport::LiveKit` instead unless you require backwards compatibility with older versions of MSC4195."]
LegacyLiveKit(LegacyLiveKitRtcTransport),
#[doc(hidden)]
#[serde(untagged)]
_Custom(CustomRtcTransport),
}
impl<'de> Deserialize<'de> for RtcTransport {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
let mut obj = JsonObject::deserialize(deserializer)?;
let transport_type = match obj.remove("type") {
Some(serde_json::Value::String(s)) => s,
Some(_) => return Err(D::Error::custom("`type` must be a string")),
None => return Err(D::Error::missing_field("type")),
};
Ok(match transport_type.as_str() {
#[cfg(feature = "unstable-msc4195")]
"m.livekit" => Self::LiveKit(
serde_json::from_value(serde_json::Value::Object(obj)).map_err(D::Error::custom)?,
),
#[cfg(feature = "unstable-msc4195")]
#[allow(deprecated)]
"livekit" => Self::LegacyLiveKit(
serde_json::from_value(serde_json::Value::Object(obj)).map_err(D::Error::custom)?,
),
_ => Self::_Custom(CustomRtcTransport { transport_type, data: obj }),
})
}
}
impl RtcTransport {
pub fn new(transport_type: &str, data: JsonObject) -> serde_json::Result<Self> {
Ok(match transport_type {
#[cfg(feature = "unstable-msc4195")]
"m.livekit" => Self::LiveKit(serde_json::from_value(serde_json::Value::Object(data))?),
#[cfg(feature = "unstable-msc4195")]
#[allow(deprecated)]
"livekit" => {
Self::LegacyLiveKit(serde_json::from_value(serde_json::Value::Object(data))?)
}
_ => Self::_Custom(CustomRtcTransport {
transport_type: transport_type.to_owned(),
data,
}),
})
}
#[cfg(feature = "unstable-msc4195")]
pub fn livekit(url: String) -> Self {
Self::LiveKit(LiveKitRtcTransport::new(url))
}
pub fn transport_type(&self) -> &str {
match self {
#[cfg(feature = "unstable-msc4195")]
Self::LiveKit(_) => "m.livekit",
#[cfg(feature = "unstable-msc4195")]
#[allow(deprecated)]
Self::LegacyLiveKit(_) => "livekit",
Self::_Custom(custom) => &custom.transport_type,
}
}
pub fn data(&self) -> Cow<'_, JsonObject> {
#[cfg(feature = "unstable-msc4195")]
fn serialize<T: Serialize>(object: &T) -> JsonObject {
use serde_json::Value as JsonValue;
match serde_json::to_value(object).expect("rtc transport type serialization to succeed")
{
JsonValue::Object(object) => object,
_ => panic!("all rtc transport types must serialize to objects"),
}
}
match self {
#[cfg(feature = "unstable-msc4195")]
Self::LiveKit(info) => Cow::Owned(serialize(info)),
#[cfg(feature = "unstable-msc4195")]
#[allow(deprecated)]
Self::LegacyLiveKit(info) => Cow::Owned(serialize(info)),
Self::_Custom(info) => Cow::Borrowed(&info.data),
}
}
}
#[cfg(feature = "unstable-msc4195")]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct LiveKitRtcTransport {
pub url: String,
#[serde(rename = "livekit_service_url", skip_serializing_if = "Option::is_none")]
#[deprecated = "Use `url` instead unless you require backwards compatibility with older versions of MSC4195."]
pub service_url: Option<String>,
}
#[cfg(feature = "unstable-msc4195")]
impl LiveKitRtcTransport {
#[allow(deprecated)]
pub fn new(url: String) -> Self {
Self { url, service_url: None }
}
}
#[cfg(feature = "unstable-msc4195")]
impl From<LiveKitRtcTransport> for RtcTransport {
fn from(value: LiveKitRtcTransport) -> Self {
Self::LiveKit(value)
}
}
#[cfg(feature = "unstable-msc4195")]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
#[deprecated = "Use `LiveKitRtcTransport` instead unless you require backwards compatibility with older versions of MSC4195."]
pub struct LegacyLiveKitRtcTransport {
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(rename = "livekit_service_url", skip_serializing_if = "Option::is_none")]
pub service_url: Option<String>,
}
#[cfg(feature = "unstable-msc4195")]
#[allow(deprecated)]
impl LegacyLiveKitRtcTransport {
pub fn new(url: String) -> Self {
Self { url: Some(url), service_url: None }
}
pub fn with_service_url(service_url: String) -> Self {
Self { url: None, service_url: Some(service_url) }
}
}
#[cfg(feature = "unstable-msc4195")]
#[allow(deprecated)]
impl From<LegacyLiveKitRtcTransport> for RtcTransport {
fn from(value: LegacyLiveKitRtcTransport) -> Self {
Self::LegacyLiveKit(value)
}
}
#[doc(hidden)]
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
pub struct CustomRtcTransport {
#[serde(rename = "type")]
transport_type: String,
#[serde(flatten)]
data: JsonObject,
}
#[cfg(test)]
mod tests {
use serde_json::{
Value as JsonValue, from_value as from_json_value, json, to_value as to_json_value,
};
use strass::assert_let;
use super::RtcTransport;
#[test]
fn serialize_roundtrip_custom_rtc_transport() {
let transport_type = "local.custom.transport";
assert_let!(
JsonValue::Object(transport_data) = json!({
"foo": "bar",
"baz": true,
})
);
let transport = RtcTransport::new(transport_type, transport_data.clone()).unwrap();
let json = json!({
"type": transport_type,
"foo": "bar",
"baz": true,
});
assert_eq!(transport.transport_type(), transport_type);
assert_eq!(*transport.data().as_ref(), transport_data);
assert_eq!(to_json_value(&transport).unwrap(), json);
assert_eq!(from_json_value::<RtcTransport>(json).unwrap(), transport);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
fn livekit_transport_new_and_from_impl() {
use super::LiveKitRtcTransport;
let url = "wss://livekit.local/".to_owned();
let inner = LiveKitRtcTransport::new(url.clone());
let transport = RtcTransport::from(inner);
assert_eq!(transport.transport_type(), "m.livekit");
assert_eq!(transport, RtcTransport::livekit(url));
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn legacy_livekit_transport_new_and_from_impl() {
use super::LegacyLiveKitRtcTransport;
let url = "wss://livekit.local/".to_owned();
let transport = RtcTransport::from(LegacyLiveKitRtcTransport::new(url.clone()));
assert_eq!(transport.transport_type(), "livekit");
assert_let!(RtcTransport::LegacyLiveKit(livekit) = transport);
assert_eq!(livekit.url, Some(url));
assert_eq!(livekit.service_url, None);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
fn serialize_roundtrip_livekit_sfu_transport() {
let transport_type = "m.livekit";
let url = "wss://livekit.local/";
let transport = RtcTransport::livekit(url.to_owned());
let json = json!({
"type": transport_type,
"url": url,
});
assert_eq!(transport.transport_type(), transport_type);
assert_eq!(to_json_value(&transport).unwrap(), json);
assert_eq!(from_json_value::<RtcTransport>(json).unwrap(), transport);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn serialize_roundtrip_legacy_livekit_sfu_transport() {
use super::LegacyLiveKitRtcTransport;
let transport_type = "livekit";
let url = "wss://livekit.local/";
let transport = RtcTransport::LegacyLiveKit(LegacyLiveKitRtcTransport::new(url.to_owned()));
let json = json!({
"type": transport_type,
"url": url,
});
assert_eq!(transport.transport_type(), transport_type);
assert_eq!(to_json_value(&transport).unwrap(), json);
assert_eq!(from_json_value::<RtcTransport>(json).unwrap(), transport);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
fn deserialize_livekit_sfu_transport_without_url_fails() {
let json = json!({
"type": "m.livekit",
"livekit_service_url": "https://livekit-jwt.local/",
});
from_json_value::<RtcTransport>(json).unwrap_err();
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn serialize_roundtrip_legacy_livekit_sfu_transport_with_service_url() {
use super::LegacyLiveKitRtcTransport;
let livekit_service_url = "https://livekit-jwt.local/";
let json = json!({
"type": "livekit",
"livekit_service_url": livekit_service_url,
});
assert_let!(Ok(RtcTransport::LegacyLiveKit(livekit)) = from_json_value(json.clone()));
assert_eq!(livekit.url, None);
assert_eq!(livekit.service_url.as_deref(), Some(livekit_service_url));
assert_eq!(
livekit,
LegacyLiveKitRtcTransport::with_service_url(livekit_service_url.to_owned())
);
assert_eq!(to_json_value(RtcTransport::LegacyLiveKit(livekit)).unwrap(), json);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn serialize_roundtrip_livekit_sfu_transport_with_url_and_service_url() {
let url = "wss://livekit.local/";
let livekit_service_url = "https://livekit-jwt.local/";
let json = json!({
"type": "m.livekit",
"url": url,
"livekit_service_url": livekit_service_url,
});
assert_let!(Ok(RtcTransport::LiveKit(livekit)) = from_json_value(json.clone()));
assert_eq!(livekit.url, url);
assert_eq!(livekit.service_url.as_deref(), Some(livekit_service_url));
assert_eq!(to_json_value(RtcTransport::LiveKit(livekit)).unwrap(), json);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn serialize_roundtrip_legacy_livekit_sfu_transport_with_url_and_service_url() {
let url = "wss://livekit.local/";
let livekit_service_url = "https://livekit-jwt.local/";
let json = json!({
"type": "livekit",
"url": url,
"livekit_service_url": livekit_service_url,
});
assert_let!(Ok(RtcTransport::LegacyLiveKit(livekit)) = from_json_value(json.clone()));
assert_eq!(livekit.url.as_deref(), Some(url));
assert_eq!(livekit.service_url.as_deref(), Some(livekit_service_url));
assert_eq!(to_json_value(RtcTransport::LegacyLiveKit(livekit)).unwrap(), json);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
fn new_livekit_sfu_transport_without_url_fails() {
assert_let!(
JsonValue::Object(data) = json!({
"livekit_service_url": "https://livekit-jwt.local/",
})
);
RtcTransport::new("m.livekit", data).unwrap_err();
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn new_livekit_sfu_transport_with_url_and_service_url() {
assert_let!(
JsonValue::Object(data) = json!({
"url": "wss://livekit.local/",
"livekit_service_url": "https://livekit-jwt.local/",
})
);
let transport = RtcTransport::new("m.livekit", data.clone()).unwrap();
assert_eq!(transport.transport_type(), "m.livekit");
assert_let!(RtcTransport::LiveKit(livekit) = &transport);
assert_eq!(livekit.url, "wss://livekit.local/");
assert_eq!(livekit.service_url.as_deref(), Some("https://livekit-jwt.local/"));
assert_eq!(*transport.data(), data);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn new_legacy_livekit_sfu_transport_with_service_url() {
assert_let!(
JsonValue::Object(data) = json!({
"livekit_service_url": "https://livekit-jwt.local/",
})
);
let transport = RtcTransport::new("livekit", data.clone()).unwrap();
assert_eq!(transport.transport_type(), "livekit");
assert_let!(RtcTransport::LegacyLiveKit(livekit) = &transport);
assert_eq!(livekit.url, None);
assert_eq!(livekit.service_url.as_deref(), Some("https://livekit-jwt.local/"));
assert_eq!(*transport.data(), data);
}
#[cfg(feature = "unstable-msc4195")]
#[test]
#[allow(deprecated)]
fn serialize_roundtrip_livekit_and_legacy_livekit_sfu_transports() {
use super::LegacyLiveKitRtcTransport;
let json = json!([
{ "type": "livekit", "livekit_service_url": "https://livekit-jwt.local/" },
{ "type": "livekit", "url": "wss://livekit.local/" },
{ "type": "m.livekit", "url": "wss://livekit.local/" },
]);
let transports = from_json_value::<Vec<RtcTransport>>(json.clone()).unwrap();
assert_eq!(
transports,
[
RtcTransport::LegacyLiveKit(LegacyLiveKitRtcTransport::with_service_url(
"https://livekit-jwt.local/".to_owned(),
)),
RtcTransport::LegacyLiveKit(LegacyLiveKitRtcTransport::new(
"wss://livekit.local/".to_owned(),
)),
RtcTransport::livekit("wss://livekit.local/".to_owned()),
]
);
assert_eq!(to_json_value(&transports).unwrap(), json);
}
}