From a23fd0e97392d631ae96edf5ba90bdd8c2ffe36c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Fri, 1 Sep 2023 10:16:32 +0200 Subject: [PATCH] Use FBS Transport::Tuple everywhere (#1148) --- node/src/Transport.ts | 26 ++++++- node/src/WebRtcTransport.ts | 3 +- node/src/tests/test-WebRtcTransport.ts | 5 +- rust/src/data_structures.rs | 12 ++- rust/src/fbs.rs | 102 +++++++++++++++++++------ worker/fbs/transport.fbs | 2 +- worker/fbs/webRtcTransport.fbs | 2 +- worker/include/RTC/IceCandidate.hpp | 8 +- worker/include/RTC/TransportTuple.hpp | 3 + worker/src/RTC/IceCandidate.cpp | 15 +--- worker/src/RTC/PipeTransport.cpp | 4 +- worker/src/RTC/PlainTransport.cpp | 6 +- worker/src/RTC/TransportTuple.cpp | 42 ++++++---- 13 files changed, 155 insertions(+), 75 deletions(-) diff --git a/node/src/Transport.ts b/node/src/Transport.ts index f041359574..0d4b237dd6 100644 --- a/node/src/Transport.ts +++ b/node/src/Transport.ts @@ -1310,6 +1310,30 @@ export function parseSctpState(fbsSctpState: FbsSctpState): SctpState } } +export function parseProtocol(protocol: FbsTransport.Protocol): TransportProtocol +{ + switch (protocol) + { + case FbsTransport.Protocol.UDP: + return 'udp'; + + case FbsTransport.Protocol.TCP: + return 'tcp'; + } +} + +export function serializeProtocol(protocol: TransportProtocol): FbsTransport.Protocol +{ + switch (protocol) + { + case 'udp': + return FbsTransport.Protocol.UDP; + + case 'tcp': + return FbsTransport.Protocol.TCP; + } +} + export function parseTuple(binary: FbsTransport.Tuple): TransportTuple { return { @@ -1317,7 +1341,7 @@ export function parseTuple(binary: FbsTransport.Tuple): TransportTuple localPort : binary.localPort(), remoteIp : binary.remoteIp() ?? undefined, remotePort : binary.remotePort(), - protocol : binary.protocol()! as TransportProtocol + protocol : parseProtocol(binary.protocol()) }; } diff --git a/node/src/WebRtcTransport.ts b/node/src/WebRtcTransport.ts index 019a7cba2e..3b619c0c3c 100644 --- a/node/src/WebRtcTransport.ts +++ b/node/src/WebRtcTransport.ts @@ -4,6 +4,7 @@ import { parseSctpState, parseBaseTransportDump, parseBaseTransportStats, + parseProtocol, parseTransportTraceEventData, parseTuple, BaseTransportDump, @@ -741,7 +742,7 @@ function parseIceCandidate(binary: FbsWebRtcTransport.IceCandidate): IceCandidat foundation : binary.foundation()!, priority : binary.priority(), ip : binary.ip()!, - protocol : binary.protocol() as TransportProtocol, + protocol : parseProtocol(binary.protocol()), port : binary.port(), type : 'host', tcpType : binary.tcpType() === 'passive' ? 'passive' : undefined diff --git a/node/src/tests/test-WebRtcTransport.ts b/node/src/tests/test-WebRtcTransport.ts index 2cc379b826..fd4df0dae8 100644 --- a/node/src/tests/test-WebRtcTransport.ts +++ b/node/src/tests/test-WebRtcTransport.ts @@ -5,6 +5,7 @@ import * as mediasoup from '../'; import { Notification, Body as NotificationBody, Event } from '../fbs/notification'; import * as FbsTransport from '../fbs/transport'; import * as FbsWebRtcTransport from '../fbs/web-rtc-transport'; +import { serializeProtocol, TransportTuple } from '../Transport'; const { createWorker } = mediasoup; @@ -517,7 +518,7 @@ test('WebRtcTransport events succeed', async () => builder.clear(); const onIceSelectedTuple = jest.fn(); - const iceSelectedTuple = + const iceSelectedTuple: TransportTuple = { localIp : '1.1.1.1', localPort : 1111, @@ -536,7 +537,7 @@ test('WebRtcTransport events succeed', async () => iceSelectedTuple.localPort, iceSelectedTuple.remoteIp, iceSelectedTuple.remotePort, - iceSelectedTuple.protocol) + serializeProtocol(iceSelectedTuple.protocol)) ); notificationOffset = Notification.createNotification( diff --git a/rust/src/data_structures.rs b/rust/src/data_structures.rs index 3f3dc28ce5..eafd39039a 100644 --- a/rust/src/data_structures.rs +++ b/rust/src/data_structures.rs @@ -147,12 +147,10 @@ pub enum Protocol { } impl Protocol { - // TODO: Use the Protocol FBS type. - pub(crate) fn from_fbs(protocol: &str) -> Self { + pub(crate) fn from_fbs(protocol: transport::Protocol) -> Self { match protocol { - "tcp" => Protocol::Tcp, - "udp" => Protocol::Udp, - _ => todo!(), + transport::Protocol::Tcp => Protocol::Tcp, + transport::Protocol::Udp => Protocol::Udp, } } } @@ -284,12 +282,12 @@ impl TransportTuple { .parse() .expect("Error parsing IP address"), remote_port: tuple.remote_port, - protocol: Protocol::from_fbs(tuple.protocol.as_str()), + protocol: Protocol::from_fbs(tuple.protocol), }, None => TransportTuple::LocalOnly { local_ip: tuple.local_ip.parse().expect("Error parsing IP address"), local_port: tuple.local_port, - protocol: Protocol::from_fbs(tuple.protocol.as_str()), + protocol: Protocol::from_fbs(tuple.protocol), }, } } diff --git a/rust/src/fbs.rs b/rust/src/fbs.rs index 59a62b22ca..3094851590 100644 --- a/rust/src/fbs.rs +++ b/rust/src/fbs.rs @@ -35827,7 +35827,7 @@ mod root { /// The field `remote_port` in the table `Tuple` pub remote_port: u16, /// The field `protocol` in the table `Tuple` - pub protocol: ::planus::alloc::string::String, + pub protocol: self::Protocol, } impl Tuple { @@ -35846,13 +35846,13 @@ mod root { ::planus::Offset<::core::primitive::str>, >, field_remote_port: impl ::planus::WriteAsDefault, - field_protocol: impl ::planus::WriteAs<::planus::Offset>, + field_protocol: impl ::planus::WriteAsDefault, ) -> ::planus::Offset { let prepared_local_ip = field_local_ip.prepare(builder); let prepared_local_port = field_local_port.prepare(builder, &0); let prepared_remote_ip = field_remote_ip.prepare(builder); let prepared_remote_port = field_remote_port.prepare(builder, &0); - let prepared_protocol = field_protocol.prepare(builder); + let prepared_protocol = field_protocol.prepare(builder, &self::Protocol::Udp); let mut table_writer: ::planus::table_writer::TableWriter<14> = ::core::default::Default::default(); @@ -35860,13 +35860,15 @@ mod root { if prepared_remote_ip.is_some() { table_writer.write_entry::<::planus::Offset>(2); } - table_writer.write_entry::<::planus::Offset>(4); if prepared_local_port.is_some() { table_writer.write_entry::(1); } if prepared_remote_port.is_some() { table_writer.write_entry::(3); } + if prepared_protocol.is_some() { + table_writer.write_entry::(4); + } unsafe { table_writer.finish(builder, |object_writer| { @@ -35876,7 +35878,6 @@ mod root { { object_writer.write::<_, _, 4>(&prepared_remote_ip); } - object_writer.write::<_, _, 4>(&prepared_protocol); if let ::core::option::Option::Some(prepared_local_port) = prepared_local_port { @@ -35887,6 +35888,11 @@ mod root { { object_writer.write::<_, _, 2>(&prepared_remote_port); } + if let ::core::option::Option::Some(prepared_protocol) = + prepared_protocol + { + object_writer.write::<_, _, 1>(&prepared_protocol); + } }); } builder.current_offset() @@ -35923,7 +35929,7 @@ mod root { self.local_port, &self.remote_ip, self.remote_port, - &self.protocol, + self.protocol, ) } } @@ -36015,11 +36021,20 @@ mod root { #[allow(clippy::type_complexity)] pub fn protocol(self, value: T4) -> TupleBuilder<(T0, T1, T2, T3, T4)> where - T4: ::planus::WriteAs<::planus::Offset>, + T4: ::planus::WriteAsDefault, { let (v0, v1, v2, v3) = self.0; TupleBuilder((v0, v1, v2, v3, value)) } + + /// Sets the [`protocol` field](Tuple#structfield.protocol) to the default value. + #[inline] + #[allow(clippy::type_complexity)] + pub fn protocol_as_default( + self, + ) -> TupleBuilder<(T0, T1, T2, T3, ::planus::DefaultValue)> { + self.protocol(::planus::DefaultValue) + } } impl TupleBuilder<(T0, T1, T2, T3, T4)> { @@ -36038,7 +36053,7 @@ mod root { T1: ::planus::WriteAsDefault, T2: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, T3: ::planus::WriteAsDefault, - T4: ::planus::WriteAs<::planus::Offset>, + T4: ::planus::WriteAsDefault, > ::planus::WriteAs<::planus::Offset> for TupleBuilder<(T0, T1, T2, T3, T4)> { @@ -36055,7 +36070,7 @@ mod root { T1: ::planus::WriteAsDefault, T2: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, T3: ::planus::WriteAsDefault, - T4: ::planus::WriteAs<::planus::Offset>, + T4: ::planus::WriteAsDefault, > ::planus::WriteAsOptional<::planus::Offset> for TupleBuilder<(T0, T1, T2, T3, T4)> { @@ -36075,7 +36090,7 @@ mod root { T1: ::planus::WriteAsDefault, T2: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, T3: ::planus::WriteAsDefault, - T4: ::planus::WriteAs<::planus::Offset>, + T4: ::planus::WriteAsDefault, > ::planus::WriteAsOffset for TupleBuilder<(T0, T1, T2, T3, T4)> { #[inline] @@ -36123,8 +36138,12 @@ mod root { /// Getter for the [`protocol` field](Tuple#structfield.protocol). #[inline] - pub fn protocol(&self) -> ::planus::Result<&'a ::core::primitive::str> { - self.0.access_required(4, "Tuple", "protocol") + pub fn protocol(&self) -> ::planus::Result { + ::core::result::Result::Ok( + self.0 + .access(4, "Tuple", "protocol")? + .unwrap_or(self::Protocol::Udp), + ) } } @@ -74929,7 +74948,7 @@ mod root { /// The field `ip` in the table `IceCandidate` pub ip: ::planus::alloc::string::String, /// The field `protocol` in the table `IceCandidate` - pub protocol: ::planus::alloc::string::String, + pub protocol: super::transport::Protocol, /// The field `port` in the table `IceCandidate` pub port: u16, /// The field `type` in the table `IceCandidate` @@ -74951,7 +74970,10 @@ mod root { field_foundation: impl ::planus::WriteAs<::planus::Offset>, field_priority: impl ::planus::WriteAsDefault, field_ip: impl ::planus::WriteAs<::planus::Offset>, - field_protocol: impl ::planus::WriteAs<::planus::Offset>, + field_protocol: impl ::planus::WriteAsDefault< + super::transport::Protocol, + super::transport::Protocol, + >, field_port: impl ::planus::WriteAsDefault, field_type_: impl ::planus::WriteAsOptional< ::planus::Offset<::core::primitive::str>, @@ -74963,7 +74985,8 @@ mod root { let prepared_foundation = field_foundation.prepare(builder); let prepared_priority = field_priority.prepare(builder, &0); let prepared_ip = field_ip.prepare(builder); - let prepared_protocol = field_protocol.prepare(builder); + let prepared_protocol = + field_protocol.prepare(builder, &super::transport::Protocol::Udp); let prepared_port = field_port.prepare(builder, &0); let prepared_type_ = field_type_.prepare(builder); let prepared_tcp_type = field_tcp_type.prepare(builder); @@ -74975,7 +74998,6 @@ mod root { table_writer.write_entry::(1); } table_writer.write_entry::<::planus::Offset>(2); - table_writer.write_entry::<::planus::Offset>(3); if prepared_type_.is_some() { table_writer.write_entry::<::planus::Offset>(5); } @@ -74985,6 +75007,9 @@ mod root { if prepared_port.is_some() { table_writer.write_entry::(4); } + if prepared_protocol.is_some() { + table_writer.write_entry::(3); + } unsafe { table_writer.finish(builder, |object_writer| { @@ -74995,7 +75020,6 @@ mod root { object_writer.write::<_, _, 4>(&prepared_priority); } object_writer.write::<_, _, 4>(&prepared_ip); - object_writer.write::<_, _, 4>(&prepared_protocol); if let ::core::option::Option::Some(prepared_type_) = prepared_type_ { object_writer.write::<_, _, 4>(&prepared_type_); } @@ -75007,6 +75031,11 @@ mod root { if let ::core::option::Option::Some(prepared_port) = prepared_port { object_writer.write::<_, _, 2>(&prepared_port); } + if let ::core::option::Option::Some(prepared_protocol) = + prepared_protocol + { + object_writer.write::<_, _, 1>(&prepared_protocol); + } }); } builder.current_offset() @@ -75048,7 +75077,7 @@ mod root { &self.foundation, self.priority, &self.ip, - &self.protocol, + self.protocol, self.port, &self.type_, &self.tcp_type, @@ -75116,11 +75145,23 @@ mod root { #[allow(clippy::type_complexity)] pub fn protocol(self, value: T3) -> IceCandidateBuilder<(T0, T1, T2, T3)> where - T3: ::planus::WriteAs<::planus::Offset>, + T3: ::planus::WriteAsDefault< + super::transport::Protocol, + super::transport::Protocol, + >, { let (v0, v1, v2) = self.0; IceCandidateBuilder((v0, v1, v2, value)) } + + /// Sets the [`protocol` field](IceCandidate#structfield.protocol) to the default value. + #[inline] + #[allow(clippy::type_complexity)] + pub fn protocol_as_default( + self, + ) -> IceCandidateBuilder<(T0, T1, T2, ::planus::DefaultValue)> { + self.protocol(::planus::DefaultValue) + } } impl IceCandidateBuilder<(T0, T1, T2, T3)> { @@ -75206,7 +75247,10 @@ mod root { T0: ::planus::WriteAs<::planus::Offset>, T1: ::planus::WriteAsDefault, T2: ::planus::WriteAs<::planus::Offset>, - T3: ::planus::WriteAs<::planus::Offset>, + T3: ::planus::WriteAsDefault< + super::transport::Protocol, + super::transport::Protocol, + >, T4: ::planus::WriteAsDefault, T5: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, T6: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, @@ -75228,7 +75272,10 @@ mod root { T0: ::planus::WriteAs<::planus::Offset>, T1: ::planus::WriteAsDefault, T2: ::planus::WriteAs<::planus::Offset>, - T3: ::planus::WriteAs<::planus::Offset>, + T3: ::planus::WriteAsDefault< + super::transport::Protocol, + super::transport::Protocol, + >, T4: ::planus::WriteAsDefault, T5: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, T6: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, @@ -75250,7 +75297,10 @@ mod root { T0: ::planus::WriteAs<::planus::Offset>, T1: ::planus::WriteAsDefault, T2: ::planus::WriteAs<::planus::Offset>, - T3: ::planus::WriteAs<::planus::Offset>, + T3: ::planus::WriteAsDefault< + super::transport::Protocol, + super::transport::Protocol, + >, T4: ::planus::WriteAsDefault, T5: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, T6: ::planus::WriteAsOptional<::planus::Offset<::core::primitive::str>>, @@ -75294,8 +75344,12 @@ mod root { /// Getter for the [`protocol` field](IceCandidate#structfield.protocol). #[inline] - pub fn protocol(&self) -> ::planus::Result<&'a ::core::primitive::str> { - self.0.access_required(3, "IceCandidate", "protocol") + pub fn protocol(&self) -> ::planus::Result { + ::core::result::Result::Ok( + self.0 + .access(3, "IceCandidate", "protocol")? + .unwrap_or(super::transport::Protocol::Udp), + ) } /// Getter for the [`port` field](IceCandidate#structfield.port). diff --git a/worker/fbs/transport.fbs b/worker/fbs/transport.fbs index 1c291876cf..6e5914f366 100644 --- a/worker/fbs/transport.fbs +++ b/worker/fbs/transport.fbs @@ -79,7 +79,7 @@ table Tuple { local_port:uint16; remote_ip:string; remote_port:uint16; - protocol:string (required); + protocol:FBS.Transport.Protocol=UDP; } table SrtpParameters { diff --git a/worker/fbs/webRtcTransport.fbs b/worker/fbs/webRtcTransport.fbs index f3551139e2..6a77106c35 100644 --- a/worker/fbs/webRtcTransport.fbs +++ b/worker/fbs/webRtcTransport.fbs @@ -41,7 +41,7 @@ table IceCandidate { foundation:string (required); priority:uint32; ip:string (required); - protocol:string (required); + protocol:FBS.Transport.Protocol=UDP; port:uint16; type:string; tcp_type:string; diff --git a/worker/include/RTC/IceCandidate.hpp b/worker/include/RTC/IceCandidate.hpp index 448dc390b9..0fef17b75d 100644 --- a/worker/include/RTC/IceCandidate.hpp +++ b/worker/include/RTC/IceCandidate.hpp @@ -4,6 +4,7 @@ #include "common.hpp" #include "FBS/webRtcTransport_generated.h" #include "RTC/TcpServer.hpp" +#include "RTC/TransportTuple.hpp" #include "RTC/UdpSocket.hpp" #include #include @@ -12,12 +13,7 @@ namespace RTC { class IceCandidate { - public: - enum class Protocol - { - UDP = 1, - TCP - }; + using Protocol = TransportTuple::Protocol; public: enum class CandidateType diff --git a/worker/include/RTC/TransportTuple.hpp b/worker/include/RTC/TransportTuple.hpp index 3fc01bca78..efc37ad2bf 100644 --- a/worker/include/RTC/TransportTuple.hpp +++ b/worker/include/RTC/TransportTuple.hpp @@ -23,6 +23,9 @@ namespace RTC TCP }; + static Protocol ProtocolFromFbs(FBS::Transport::Protocol protocol); + static FBS::Transport::Protocol ProtocolToFbs(Protocol protocol); + public: TransportTuple(RTC::UdpSocket* udpSocket, const struct sockaddr* udpRemoteAddr) : udpSocket(udpSocket), udpRemoteAddr((struct sockaddr*)udpRemoteAddr), protocol(Protocol::UDP) diff --git a/worker/src/RTC/IceCandidate.cpp b/worker/src/RTC/IceCandidate.cpp index 67af1a18e4..f04aa10c21 100644 --- a/worker/src/RTC/IceCandidate.cpp +++ b/worker/src/RTC/IceCandidate.cpp @@ -13,18 +13,7 @@ namespace RTC { MS_TRACE(); - std::string protocol; - - switch (this->protocol) - { - case Protocol::UDP: - protocol = "udp"; - break; - - case Protocol::TCP: - protocol = "tcp"; - break; - } + auto protocol = TransportTuple::ProtocolToFbs(this->protocol); std::string type; @@ -56,7 +45,7 @@ namespace RTC // ip. this->ip.c_str(), // protocol. - protocol.c_str(), + protocol, // port. this->port, // type. diff --git a/worker/src/RTC/PipeTransport.cpp b/worker/src/RTC/PipeTransport.cpp index d75b396c09..bfdf7d5585 100644 --- a/worker/src/RTC/PipeTransport.cpp +++ b/worker/src/RTC/PipeTransport.cpp @@ -169,7 +169,7 @@ namespace RTC } tuple = FBS::Transport::CreateTupleDirect( - builder, localIp.c_str(), this->udpSocket->GetLocalPort(), "", 0, "udp"); + builder, localIp.c_str(), this->udpSocket->GetLocalPort(), "", 0, FBS::Transport::Protocol::UDP); } // Add srtpParameters. @@ -223,7 +223,7 @@ namespace RTC // remotePort. 0, // protocol. - "udp"); + FBS::Transport::Protocol::UDP); } // Base Transport stats. diff --git a/worker/src/RTC/PlainTransport.cpp b/worker/src/RTC/PlainTransport.cpp index b3a6b6b184..9310674bff 100644 --- a/worker/src/RTC/PlainTransport.cpp +++ b/worker/src/RTC/PlainTransport.cpp @@ -296,7 +296,7 @@ namespace RTC } tuple = FBS::Transport::CreateTupleDirect( - builder, localIp.c_str(), this->udpSocket->GetLocalPort(), nullptr, 0, "udp"); + builder, localIp.c_str(), this->udpSocket->GetLocalPort(), nullptr, 0, FBS::Transport::Protocol::UDP); } // Add rtcpTuple. @@ -322,7 +322,7 @@ namespace RTC } rtcpTuple = FBS::Transport::CreateTupleDirect( - builder, localIp.c_str(), this->rtcpUdpSocket->GetLocalPort(), nullptr, 0, "udp"); + builder, localIp.c_str(), this->rtcpUdpSocket->GetLocalPort(), nullptr, 0, FBS::Transport::Protocol::UDP); } } @@ -381,7 +381,7 @@ namespace RTC // remotePort. 0, // protocol. - "udp" + FBS::Transport::Protocol::UDP ); } diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 4150b2f7e0..12f045e49c 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -7,6 +7,32 @@ namespace RTC { + /* Static methods. */ + + TransportTuple::Protocol TransportTuple::ProtocolFromFbs(FBS::Transport::Protocol protocol) + { + switch (protocol) + { + case FBS::Transport::Protocol::UDP: + return TransportTuple::Protocol::UDP; + + case FBS::Transport::Protocol::TCP: + return TransportTuple::Protocol::TCP; + } + } + + FBS::Transport::Protocol TransportTuple::ProtocolToFbs(TransportTuple::Protocol protocol) + { + switch (protocol) + { + case TransportTuple::Protocol::UDP: + return FBS::Transport::Protocol::UDP; + + case TransportTuple::Protocol::TCP: + return FBS::Transport::Protocol::TCP; + } + } + /* Instance methods. */ flatbuffers::Offset TransportTuple::FillBuffer( @@ -27,22 +53,10 @@ namespace RTC Utils::IP::GetAddressInfo(GetRemoteAddress(), family, remoteIp, remotePort); - std::string protocol; - - // Add protocol. - switch (GetProtocol()) - { - case Protocol::UDP: - protocol = "udp"; - break; - - case Protocol::TCP: - protocol = "tcp"; - break; - } + auto protocol = TransportTuple::ProtocolToFbs(GetProtocol()); return FBS::Transport::CreateTupleDirect( - builder, localIp.c_str(), localPort, remoteIp.c_str(), remotePort, protocol.c_str()); + builder, localIp.c_str(), localPort, remoteIp.c_str(), remotePort, protocol); } void TransportTuple::Dump() const